From 58e4b1832f01c66f9cbb06532ce379e1667dc519 Mon Sep 17 00:00:00 2001 From: Federico Iosue Date: Sun, 10 Mar 2024 17:21:28 +0100 Subject: [PATCH] Improved recent camera runtime permission code, included videos --- .../testutils/BaseAndroidTestCase.java | 7 +- omniNotes/src/main/AndroidManifest.xml | 1 + .../feio/android/omninotes/BaseActivity.java | 3 + .../android/omninotes/DetailFragment.java | 108 +++++++----------- .../omninotes/helpers/PermissionsHelper.java | 14 +-- omniNotes/src/main/res/raw/changelog.xml | 1 + omniNotes/src/main/res/values/strings.xml | 3 +- 7 files changed, 63 insertions(+), 74 deletions(-) diff --git a/omniNotes/src/androidTest/java/it/feio/android/omninotes/testutils/BaseAndroidTestCase.java b/omniNotes/src/androidTest/java/it/feio/android/omninotes/testutils/BaseAndroidTestCase.java index eb56d631c..c9b6fff4a 100644 --- a/omniNotes/src/androidTest/java/it/feio/android/omninotes/testutils/BaseAndroidTestCase.java +++ b/omniNotes/src/androidTest/java/it/feio/android/omninotes/testutils/BaseAndroidTestCase.java @@ -64,6 +64,7 @@ import org.junit.After; import org.junit.Before; import org.junit.BeforeClass; +import org.junit.Rule; public class BaseAndroidTestCase { @@ -96,6 +97,10 @@ public void tearDown() { } } + @Rule + public GrantPermissionRule mRuntimePermissionRule = GrantPermissionRule + .grant(android.Manifest.permission.CAMERA); + private static void grantPermissions() { GrantPermissionRule.grant(ACCESS_COARSE_LOCATION, ACCESS_FINE_LOCATION, READ_EXTERNAL_STORAGE, RECORD_AUDIO); if (BuildHelper.isBelowOrEqual(VERSION_CODES.Q)) { @@ -188,7 +193,7 @@ protected void createCategory(String categoryName) { * @param clazz utility class to verify. */ protected static void assertUtilityClassWellDefined(final Class clazz) - throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { + throws NoSuchMethodException, InstantiationException, IllegalAccessException { assertUtilityClassWellDefined(clazz, false, false); } diff --git a/omniNotes/src/main/AndroidManifest.xml b/omniNotes/src/main/AndroidManifest.xml index c21ca25b0..f360e00fb 100644 --- a/omniNotes/src/main/AndroidManifest.xml +++ b/omniNotes/src/main/AndroidManifest.xml @@ -37,6 +37,7 @@ + = Build.VERSION_CODES.M) { - if (ContextCompat.checkSelfPermission(mainActivity, Manifest.permission.READ_EXTERNAL_STORAGE) - != PackageManager.PERMISSION_GRANTED) { - // Permission is not granted - requestPermissionLauncher.launch(Manifest.permission.READ_EXTERNAL_STORAGE); - } else { - // Permission already granted - takePhoto(); - } - } else { - // Runtime permissions not needed before Marshmallow - takePhoto(); - } + PermissionsHelper.requestPermission(getActivity(), CAMERA, + R.string.permission_camera, binding.snackbarPlaceholder, () -> { + // Checks for created file validity + File f = StorageHelper.createNewAttachmentFile(mainActivity, MIME_TYPE_IMAGE_EXT); + if (f == null) { + mainActivity.showMessage(R.string.error, ONStyle.ALERT); + return; + } + attachmentUri = Uri.fromFile(f); + intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + intent.putExtra(MediaStore.EXTRA_OUTPUT, FileProviderHelper.getFileProvider(f)); + startActivityForResult(intent, TAKE_PHOTO); + }); } - private final ActivityResultLauncher requestPermissionLauncher = - registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> { - if (isGranted) { - // Permission granted - takePhoto(); - } else { - // Permission denied - Toast.makeText(mainActivity,"Permission denied",Toast.LENGTH_SHORT).show(); - } - }); private void takeVideo() { - Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); - if (!IntentChecker.isAvailable(mainActivity, takeVideoIntent, new String[]{FEATURE_CAMERA})) { + var takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); + if (!IntentChecker.isAvailable(mainActivity, takeVideoIntent, + new String[]{FEATURE_CAMERA})) { mainActivity.showMessage(R.string.feature_not_available_on_this_device, ONStyle.ALERT); return; } - // File is stored in custom ON folder to speedup the attachment - File f = StorageHelper.createNewAttachmentFile(mainActivity, MIME_TYPE_VIDEO_EXT); - if (f == null) { - mainActivity.showMessage(R.string.error, ONStyle.ALERT); - return; - } - attachmentUri = Uri.fromFile(f); - takeVideoIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); - takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, FileProviderHelper.getFileProvider(f)); - String maxVideoSizeStr = "".equals(Prefs.getString("settings_max_video_size", "")) - ? "0" : Prefs.getString("settings_max_video_size", ""); - long maxVideoSize = parseLong(maxVideoSizeStr) * 1024L * 1024L; - takeVideoIntent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, maxVideoSize); - startActivityForResult(takeVideoIntent, TAKE_VIDEO); + + PermissionsHelper.requestPermission(getActivity(), CAMERA, + R.string.permission_camera, binding.snackbarPlaceholder, () -> { + // File is stored in custom ON folder to speedup the attachment + var f = StorageHelper.createNewAttachmentFile(mainActivity, MIME_TYPE_VIDEO_EXT); + if (f == null) { + mainActivity.showMessage(R.string.error, ONStyle.ALERT); + return; + } + attachmentUri = Uri.fromFile(f); + takeVideoIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); + takeVideoIntent.putExtra(MediaStore.EXTRA_OUTPUT, FileProviderHelper.getFileProvider(f)); + var maxVideoSizeStr = "".equals(Prefs.getString("settings_max_video_size", "")) + ? "0" : Prefs.getString("settings_max_video_size", ""); + long maxVideoSize = parseLong(maxVideoSizeStr) * 1024L * 1024L; + takeVideoIntent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, maxVideoSize); + startActivityForResult(takeVideoIntent, TAKE_VIDEO); + }); } private void takeSketch(Attachment attachment) { @@ -2331,10 +2312,9 @@ private class AttachmentOnClickListener implements OnClickListener { @Override public void onClick(View v) { - switch (v.getId()) { - // Photo from camera case R.id.camera: +// requestCameraPermission(TAKE_PHOTO); takePhoto(); break; case R.id.recording: diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/helpers/PermissionsHelper.java b/omniNotes/src/main/java/it/feio/android/omninotes/helpers/PermissionsHelper.java index 9f0bb534e..189503ab6 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/helpers/PermissionsHelper.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/helpers/PermissionsHelper.java @@ -18,11 +18,11 @@ package it.feio.android.omninotes.helpers; import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE; +import static android.content.pm.PackageManager.PERMISSION_GRANTED; import static com.google.android.material.snackbar.BaseTransientBottomBar.LENGTH_INDEFINITE; import static com.google.android.material.snackbar.BaseTransientBottomBar.LENGTH_LONG; import android.app.Activity; -import android.content.pm.PackageManager; import android.os.Build.VERSION_CODES; import android.view.View; import androidx.core.app.ActivityCompat; @@ -37,16 +37,14 @@ public class PermissionsHelper { public static void requestPermission(Activity activity, String permission, - int rationaleDescription, View - messageView, OnPermissionRequestedListener onPermissionRequestedListener) { + int rationaleDescription, View messageView, OnPermissionRequestedListener onPermissionRequestedListener) { if (skipPermissionRequest(permission)) { onPermissionRequestedListener.onPermissionGranted(); return; } - if (ContextCompat.checkSelfPermission(activity, permission) - != PackageManager.PERMISSION_GRANTED) { + if (ContextCompat.checkSelfPermission(activity, permission) != PERMISSION_GRANTED) { if (ActivityCompat.shouldShowRequestPermissionRationale(activity, permission)) { Snackbar.make(messageView, rationaleDescription, LENGTH_INDEFINITE) @@ -72,13 +70,13 @@ private static void requestPermissionExecute(Activity activity, String permissio RxPermissions.getInstance(activity) .request(permission) .subscribe(granted -> { - if (granted && onPermissionRequestedListener != null) { + if (Boolean.TRUE.equals(granted) && onPermissionRequestedListener != null) { onPermissionRequestedListener.onPermissionGranted(); } else { - String msg = activity.getString(R.string.permission_not_granted) + ": " + permission; + var msg = activity.getString(R.string.permission_not_granted) + ": " + permission; Snackbar.make(messageView, msg, LENGTH_LONG).show(); } }); } -} +} \ No newline at end of file diff --git a/omniNotes/src/main/res/raw/changelog.xml b/omniNotes/src/main/res/raw/changelog.xml index 7941f5c6a..3b3cd0c73 100644 --- a/omniNotes/src/main/res/raw/changelog.xml +++ b/omniNotes/src/main/res/raw/changelog.xml @@ -20,6 +20,7 @@ changeDate="Mar 8, 2024" versionName="6.3.2"> [b]Happy Women Day!![/b] + [i]Improved![/i] Ask for camera permission at runtime (thanks to [a href='https://github.com/pratistha-05']Pratistha Sinha[/a] and [a href='https://github.com/akashs056']Akash Subramanian[/a]) [i]Improved![/i] When trying to pin a note without notifications permissions enabled a message will be shown [i]Improved![/i] Updated Gradle build tools to 8.3.0 [u]Fix[/u] Protected notes are no more searchable nor their tags (from content) will appear anywhere content's secrecy (thanks to [a href='https://github.com/XYIheng']Yiheng Xiong[/a]) diff --git a/omniNotes/src/main/res/values/strings.xml b/omniNotes/src/main/res/values/strings.xml index 101de9ead..65f6c72ac 100644 --- a/omniNotes/src/main/res/values/strings.xml +++ b/omniNotes/src/main/res/values/strings.xml @@ -219,8 +219,9 @@ Show archived notes Permission to write on external storage is needed to allow backups on public folders Permission to read on external storage is needed to allow attaching files from there - Permission to use microphone is needed to record audio notes + Permission to use microphone is needed to record audio notes Permission to access to location is needed to add position informations to your notes + Permission to use camera is required to take photos or videos Permission not granted Checklist Text note