Skip to content

Commit

Permalink
fix for #944 : Ask for camera permission before launching gallery
Browse files Browse the repository at this point in the history
  • Loading branch information
Pratistha Sinha committed Dec 26, 2023
1 parent 3c36042 commit da4bd03
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<String> 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);
Expand Down Expand Up @@ -2310,7 +2347,7 @@ public void onClick(View v) {
takeVideo();
break;
case R.id.files:
startGetContentAction();
startGetContentAction();
break;
case R.id.sketch:
takeSketch(null);
Expand Down

0 comments on commit da4bd03

Please sign in to comment.