diff --git a/app/build.gradle b/app/build.gradle index c2a62495fa..dfda74ceb7 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -84,7 +84,16 @@ 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.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/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); + } } } diff --git a/app/src/main/java/protect/card_locker/ScanActivity.java b/app/src/main/java/protect/card_locker/ScanActivity.java index 41cf3c1fca..f90b371817 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,22 +230,35 @@ 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) { + private void showCameraPermissionMissingText ( boolean show){ customBarcodeScannerBinding.cameraPermissionDeniedLayout.cameraPermissionDeniedClickableArea.setOnClickListener(show ? v -> { navigateToSystemPermissionSetting(); } : null);