From 0e66b3f8d24f4e80642c91a481a1306dd306f86c Mon Sep 17 00:00:00 2001 From: Federico Iosue Date: Sun, 10 Mar 2024 14:53:04 +0100 Subject: [PATCH] Improved recent camera runtime permission code, included videos --- omniNotes/src/main/AndroidManifest.xml | 1 + .../feio/android/omninotes/BaseActivity.java | 3 + .../android/omninotes/DetailFragment.java | 70 +++++++++++-------- omniNotes/src/main/res/raw/changelog.xml | 1 + 4 files changed, 46 insertions(+), 29 deletions(-) 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); - + private void requestCameraPermission(int requestCode) { + if (BuildHelper.isAboveOrEqual(Build.VERSION_CODES.M)) { + if (ContextCompat.checkSelfPermission(mainActivity, permission.CAMERA) != PERMISSION_GRANTED) { + if (requestCode == TAKE_PHOTO) { + requestCameraPermissionLauncher.launch(permission.CAMERA); + } else if (requestCode == TAKE_VIDEO) { + requestCameraVideoPermissionLauncher.launch(permission.CAMERA); + } } else { - // Permission already granted - takePhoto(); + if (requestCode == TAKE_PHOTO) { + takePhoto(); + } else if (requestCode == TAKE_VIDEO) { + takeVideo(); + } } } else { - // Runtime permissions not needed before Marshmallow - takePhoto(); + if (requestCode == TAKE_PHOTO) { + takePhoto(); + } else if (requestCode == TAKE_VIDEO) { + takeVideo(); + } } } - private final ActivityResultLauncher requestPermissionLauncher = + + private final ActivityResultLauncher requestCameraPermissionLauncher = registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> { - if (isGranted) { - // Permission granted + if (Boolean.TRUE.equals(isGranted)) { takePhoto(); } else { - // Permission denied - Toast.makeText(mainActivity,"Permission denied",Toast.LENGTH_SHORT).show(); + mainActivity.showToast(R.string.permission_not_granted, LENGTH_SHORT); + } + }); + + private final ActivityResultLauncher requestCameraVideoPermissionLauncher = + registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> { + if (Boolean.TRUE.equals(isGranted)) { + takeVideo(); + } else { + mainActivity.showToast(R.string.permission_not_granted, LENGTH_SHORT); } }); @@ -2331,11 +2345,9 @@ private class AttachmentOnClickListener implements OnClickListener { @Override public void onClick(View v) { - switch (v.getId()) { - // Photo from camera case R.id.camera: - takePhoto(); + requestCameraPermission(TAKE_PHOTO); break; case R.id.recording: if (!isRecording) { @@ -2351,7 +2363,7 @@ public void onClick(View v) { } break; case R.id.video: - takeVideo(); + requestCameraPermission(TAKE_VIDEO); break; case R.id.files: startGetContentAction(); 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])