From da4bd03a7e639ae9281e53b1dadc58a0b2faacd8 Mon Sep 17 00:00:00 2001 From: Pratistha Sinha Date: Tue, 26 Dec 2023 16:39:12 +0530 Subject: [PATCH] fix for #944 : Ask for camera permission before launching gallery --- build.gradle | 2 +- .../android/omninotes/DetailFragment.java | 43 +++++++++++++++++-- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/build.gradle b/build.gradle index 35e969d13..33d65d2f0 100644 --- a/build.gradle +++ b/build.gradle @@ -23,7 +23,7 @@ buildscript { maven { url "https://plugins.gradle.org/m2/" } } dependencies { - classpath 'com.android.tools.build:gradle:8.2.0' + classpath 'com.android.tools.build:gradle:8.1.1' classpath "org.sonarsource.scanner.gradle:sonarqube-gradle-plugin:3.2.0" classpath 'com.adarshr:gradle-test-logger-plugin:2.1.0' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.8.10" diff --git a/omniNotes/src/main/java/it/feio/android/omninotes/DetailFragment.java b/omniNotes/src/main/java/it/feio/android/omninotes/DetailFragment.java index 3bc06b2cb..e0af93665 100644 --- a/omniNotes/src/main/java/it/feio/android/omninotes/DetailFragment.java +++ b/omniNotes/src/main/java/it/feio/android/omninotes/DetailFragment.java @@ -72,6 +72,7 @@ import android.content.Context; import android.content.Intent; import android.content.pm.ActivityInfo; +import android.content.pm.PackageManager; import android.content.res.Configuration; import android.graphics.Bitmap; import android.graphics.BitmapFactory; @@ -85,6 +86,7 @@ import android.media.ThumbnailUtils; import android.net.Uri; import android.os.AsyncTask; +import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.provider.MediaStore; @@ -109,6 +111,10 @@ import android.widget.CheckBox; import android.widget.ImageView; import android.widget.Toast; + +import androidx.activity.result.ActivityResultLauncher; +import androidx.activity.result.contract.ActivityResultContracts; +import androidx.core.content.ContextCompat; import androidx.core.util.Pair; import androidx.drawerlayout.widget.DrawerLayout; import androidx.fragment.app.FragmentTransaction; @@ -117,6 +123,8 @@ import com.neopixl.pixlui.components.edittext.EditText; import com.pixplicity.easyprefs.library.Prefs; import com.pushbullet.android.extension.MessagingExtension; +import com.tbruyelle.rxpermissions.RxPermissions; + import de.greenrobot.event.EventBus; import de.keyboardsurfer.android.widget.crouton.Style; import it.feio.android.checklistview.exceptions.ViewNotSupportedException; @@ -182,6 +190,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; +import java.util.Collections; import java.util.Date; import java.util.List; import org.apache.commons.collections4.CollectionUtils; @@ -505,12 +514,12 @@ private void handleIntents() { // Sub-action is to take a photo if (IntentChecker.checkAction(i, ACTION_WIDGET_TAKE_PHOTO)) { - takePhoto(); + checkAndRequestPermissions(); } } if (IntentChecker.checkAction(i, ACTION_FAB_TAKE_PHOTO)) { - takePhoto(); + checkAndRequestPermissions(); } // Handles third party apps requests of sharing @@ -1324,7 +1333,35 @@ private void takePhoto() { intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.putExtra(MediaStore.EXTRA_OUTPUT, FileProviderHelper.getFileProvider(f)); startActivityForResult(intent, TAKE_PHOTO); + + } + + private void checkAndRequestPermissions() { + if (Build.VERSION.SDK_INT >= 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(); + } } + 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); @@ -2310,7 +2347,7 @@ public void onClick(View v) { takeVideo(); break; case R.id.files: - startGetContentAction(); + startGetContentAction(); break; case R.id.sketch: takeSketch(null);