From 4af18fe512d1019b8fdb88a6af6c7e69ff0d0774 Mon Sep 17 00:00:00 2001 From: kevinMerizalde Date: Thu, 4 May 2023 17:33:13 +0100 Subject: [PATCH 1/4] Add a BarCode or QRCode by an exiting image with PhotoPicker this new photoPicker activity only is generated if the Android version is bigger than 11 --- app/build.gradle | 11 ++++ .../protect/card_locker/ScanActivity.java | 55 ++++++++++++------- 2 files changed, 47 insertions(+), 19 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index c2a62495fa..e24234529f 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -84,7 +84,18 @@ android { } dependencies { + constraints { + implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.8.0") { + because("kotlin-stdlib-jdk7 is now a part of kotlin-stdlib") + } + implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.0") { + because("kotlin-stdlib-jdk8 is now a part of kotlin-stdlib") + } + } // AndroidX + implementation 'androidx.activity:activity:1.7.1' + implementation 'androidx.activity:activity-compose:1.7.1' + implementation 'androidx.activity:activity-ktx:1.7.1' implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.exifinterface:exifinterface:1.3.6' diff --git a/app/src/main/java/protect/card_locker/ScanActivity.java b/app/src/main/java/protect/card_locker/ScanActivity.java index 41cf3c1fca..0b061f7b38 100644 --- a/app/src/main/java/protect/card_locker/ScanActivity.java +++ b/app/src/main/java/protect/card_locker/ScanActivity.java @@ -7,6 +7,7 @@ import android.content.pm.PackageManager; import android.graphics.Color; import android.net.Uri; +import android.os.Build; import android.os.Bundle; import android.provider.Settings; import android.util.DisplayMetrics; @@ -19,6 +20,7 @@ import android.widget.Toast; import androidx.activity.result.ActivityResultLauncher; +import androidx.activity.result.PickVisualMediaRequest; import androidx.activity.result.contract.ActivityResultContracts; import androidx.annotation.NonNull; import androidx.appcompat.widget.Toolbar; @@ -64,6 +66,7 @@ public class ScanActivity extends CatimaAppCompatActivity { private ActivityResultLauncher manualAddLauncher; // can't use the pre-made contract because that launches the file manager for image type instead of gallery private ActivityResultLauncher photoPickerLauncher; + private ActivityResultLauncher pickMedia; private void extractIntentFields(Intent intent) { final Bundle b = intent.getExtras(); @@ -87,6 +90,7 @@ protected void onCreate(Bundle savedInstanceState) { manualAddLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> handleActivityResult(Utils.SELECT_BARCODE_REQUEST, result.getResultCode(), result.getData())); photoPickerLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> handleActivityResult(Utils.BARCODE_IMPORT_FROM_IMAGE_FILE, result.getResultCode(), result.getData())); + pickMedia = registerForActivityResult(new ActivityResultContracts.PickVisualMedia(),result -> handleActivityResult(Utils.BARCODE_IMPORT_FROM_IMAGE_FILE, result != null? Activity.RESULT_OK : Activity.RESULT_CANCELED, new Intent().setData(result))); customBarcodeScannerBinding.addFromImage.setOnClickListener(this::addFromImage); customBarcodeScannerBinding.addManually.setOnClickListener(this::addManually); @@ -226,28 +230,41 @@ public void addFromImage(View view) { } private void addFromImageAfterPermission() { - Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); - photoPickerIntent.setType("image/*"); - Intent contentIntent = new Intent(Intent.ACTION_GET_CONTENT); - contentIntent.setType("image/*"); - - Intent chooserIntent = Intent.createChooser(photoPickerIntent, getString(R.string.addFromImage)); - chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { contentIntent }); - try { - photoPickerLauncher.launch(chooserIntent); - } catch (ActivityNotFoundException e) { - Toast.makeText(getApplicationContext(), R.string.failedLaunchingPhotoPicker, Toast.LENGTH_LONG).show(); - Log.e(TAG, "No activity found to handle intent", e); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + // Use the new photo picker on devices where it is available + try { + // Registers a photo picker activity launcher in single-select mode. + pickMedia.launch(new PickVisualMediaRequest.Builder() + .setMediaType(ActivityResultContracts.PickVisualMedia.ImageOnly.INSTANCE) + .build()); + } catch (ActivityNotFoundException e) { + Toast.makeText(getApplicationContext(), R.string.failedLaunchingPhotoPicker, Toast.LENGTH_LONG).show(); + Log.e(TAG, "No activity found to handle intent", e); + } + } else { + Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); + photoPickerIntent.setType("image/*"); + Intent contentIntent = new Intent(Intent.ACTION_GET_CONTENT); + contentIntent.setType("image/*"); + + Intent chooserIntent = Intent.createChooser(photoPickerIntent, getString(R.string.addFromImage)); + chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{contentIntent}); + try { + photoPickerLauncher.launch(chooserIntent); + } catch (ActivityNotFoundException e) { + Toast.makeText(getApplicationContext(), R.string.failedLaunchingPhotoPicker, Toast.LENGTH_LONG).show(); + Log.e(TAG, "No activity found to handle intent", e); + } } } - private void showCameraPermissionMissingText(boolean show) { - customBarcodeScannerBinding.cameraPermissionDeniedLayout.cameraPermissionDeniedClickableArea.setOnClickListener(show ? v -> { - navigateToSystemPermissionSetting(); - } : null); - customBarcodeScannerBinding.cardInputContainer.setBackgroundColor(show ? obtainThemeAttribute(com.google.android.material.R.attr.colorSurface) : Color.TRANSPARENT); - customBarcodeScannerBinding.cameraPermissionDeniedLayout.getRoot().setVisibility(show ? View.VISIBLE : View.GONE); - } + private void showCameraPermissionMissingText ( boolean show){ + customBarcodeScannerBinding.cameraPermissionDeniedLayout.cameraPermissionDeniedClickableArea.setOnClickListener(show ? v -> { + navigateToSystemPermissionSetting(); + } : null); + customBarcodeScannerBinding.cardInputContainer.setBackgroundColor(show ? obtainThemeAttribute(com.google.android.material.R.attr.colorSurface) : Color.TRANSPARENT); + customBarcodeScannerBinding.cameraPermissionDeniedLayout.getRoot().setVisibility(show ? View.VISIBLE : View.GONE); + } private void scaleScreen() { DisplayMetrics displayMetrics = new DisplayMetrics(); From 00fe6467ad935aea5da653019b2589fd4f9fba89 Mon Sep 17 00:00:00 2001 From: kevinMerizalde Date: Thu, 4 May 2023 19:01:48 +0100 Subject: [PATCH 2/4] Set icon of a card from a gallery image - front and back image from a gallery image this new photoPicker activity only is generated if the Android version is bigger than 11. is not installing anything on the phone because we are using only compatible android versions. --- .../card_locker/LoyaltyCardEditActivity.java | 43 ++++++++++++++----- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java b/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java index 91e4515ffe..31ccbdb338 100644 --- a/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java +++ b/app/src/main/java/protect/card_locker/LoyaltyCardEditActivity.java @@ -68,6 +68,7 @@ import java.util.concurrent.Callable; import androidx.activity.result.ActivityResultLauncher; +import androidx.activity.result.PickVisualMediaRequest; import androidx.activity.result.contract.ActivityResultContracts; import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -177,6 +178,7 @@ public class LoyaltyCardEditActivity extends CatimaAppCompatActivity { ActivityResultLauncher mPhotoTakerLauncher; ActivityResultLauncher mPhotoPickerLauncher; + ActivityResultLauncher pickMedia; ActivityResultLauncher mCardIdAndBarCodeEditorLauncher; ActivityResultLauncher mCropperLauncher; @@ -596,6 +598,12 @@ public void onTabReselected(TabLayout.Tab tab) { } }); + pickMedia = registerForActivityResult(new ActivityResultContracts.PickVisualMedia(),result -> { + if(result != null){ + startCropperUri(result); + } + }); + mCardIdAndBarCodeEditorLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> { if (result.getResultCode() == RESULT_OK) { Intent intent = result.getData(); @@ -1135,18 +1143,31 @@ private void takePhotoForCard(int type) { private void selectImageFromGallery(int type) { mRequestedImage = type; - Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); - photoPickerIntent.setType("image/*"); - Intent contentIntent = new Intent(Intent.ACTION_GET_CONTENT); - contentIntent.setType("image/*"); - Intent chooserIntent = Intent.createChooser(photoPickerIntent, getString(R.string.addFromImage)); - chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { contentIntent }); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + // Use the new photo picker on devices where it is available + try { + // Registers a photo picker activity launcher in single-select mode. + pickMedia.launch(new PickVisualMediaRequest.Builder() + .setMediaType(ActivityResultContracts.PickVisualMedia.ImageOnly.INSTANCE) + .build()); + } catch (ActivityNotFoundException e) { + Toast.makeText(getApplicationContext(), R.string.failedLaunchingPhotoPicker, Toast.LENGTH_LONG).show(); + Log.e(TAG, "No activity found to handle intent", e); + } + } else { + Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); + photoPickerIntent.setType("image/*"); + Intent contentIntent = new Intent(Intent.ACTION_GET_CONTENT); + contentIntent.setType("image/*"); + Intent chooserIntent = Intent.createChooser(photoPickerIntent, getString(R.string.addFromImage)); + chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{contentIntent}); - try { - mPhotoPickerLauncher.launch(chooserIntent); - } catch (ActivityNotFoundException e) { - Toast.makeText(getApplicationContext(), R.string.failedLaunchingPhotoPicker, Toast.LENGTH_LONG).show(); - Log.e(TAG, "No activity found to handle intent", e); + try { + mPhotoPickerLauncher.launch(chooserIntent); + } catch (ActivityNotFoundException e) { + Toast.makeText(getApplicationContext(), R.string.failedLaunchingPhotoPicker, Toast.LENGTH_LONG).show(); + Log.e(TAG, "No activity found to handle intent", e); + } } } From 80ca501ab21563b0fa8de04a4e42dc8a678146de Mon Sep 17 00:00:00 2001 From: kevinMerizalde Date: Sun, 7 May 2023 18:37:29 +0100 Subject: [PATCH 3/4] resolving format error --- .../java/protect/card_locker/ScanActivity.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/protect/card_locker/ScanActivity.java b/app/src/main/java/protect/card_locker/ScanActivity.java index 0b061f7b38..f90b371817 100644 --- a/app/src/main/java/protect/card_locker/ScanActivity.java +++ b/app/src/main/java/protect/card_locker/ScanActivity.java @@ -258,13 +258,13 @@ private void addFromImageAfterPermission() { } } - private void showCameraPermissionMissingText ( boolean show){ - customBarcodeScannerBinding.cameraPermissionDeniedLayout.cameraPermissionDeniedClickableArea.setOnClickListener(show ? v -> { - navigateToSystemPermissionSetting(); - } : null); - customBarcodeScannerBinding.cardInputContainer.setBackgroundColor(show ? obtainThemeAttribute(com.google.android.material.R.attr.colorSurface) : Color.TRANSPARENT); - customBarcodeScannerBinding.cameraPermissionDeniedLayout.getRoot().setVisibility(show ? View.VISIBLE : View.GONE); - } + private void showCameraPermissionMissingText ( boolean show){ + customBarcodeScannerBinding.cameraPermissionDeniedLayout.cameraPermissionDeniedClickableArea.setOnClickListener(show ? v -> { + navigateToSystemPermissionSetting(); + } : null); + customBarcodeScannerBinding.cardInputContainer.setBackgroundColor(show ? obtainThemeAttribute(com.google.android.material.R.attr.colorSurface) : Color.TRANSPARENT); + customBarcodeScannerBinding.cameraPermissionDeniedLayout.getRoot().setVisibility(show ? View.VISIBLE : View.GONE); + } private void scaleScreen() { DisplayMetrics displayMetrics = new DisplayMetrics(); From be1558083600879658563b8a1dfaf43be766df95 Mon Sep 17 00:00:00 2001 From: kevinMerizalde Date: Sun, 7 May 2023 18:59:42 +0100 Subject: [PATCH 4/4] removing the unused libraries --- app/build.gradle | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index e24234529f..dfda74ceb7 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -94,8 +94,6 @@ dependencies { } // AndroidX implementation 'androidx.activity:activity:1.7.1' - implementation 'androidx.activity:activity-compose:1.7.1' - implementation 'androidx.activity:activity-ktx:1.7.1' implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'androidx.constraintlayout:constraintlayout:2.1.4' implementation 'androidx.exifinterface:exifinterface:1.3.6'