Skip to content

Commit

Permalink
Merge pull request #955 from pratistha-05/improvement/camera_permission
Browse files Browse the repository at this point in the history
improvement: adding runtime permission issue: #944
  • Loading branch information
federicoiosue authored Mar 10, 2024
2 parents 018c585 + 7e0e224 commit dbfc00d
Showing 1 changed file with 39 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,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 @@ -86,6 +87,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 @@ -110,6 +112,9 @@
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.activity.result.contract.ActivityResultContracts.RequestPermission;
import androidx.core.util.Pair;
import androidx.drawerlayout.widget.DrawerLayout;
Expand All @@ -119,6 +124,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 @@ -185,6 +192,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 @@ -508,12 +516,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 @@ -1327,8 +1335,36 @@ 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);
if (!IntentChecker.isAvailable(mainActivity, takeVideoIntent, new String[]{FEATURE_CAMERA})) {
Expand Down Expand Up @@ -2318,7 +2354,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 dbfc00d

Please sign in to comment.