Skip to content

Commit

Permalink
(android) catch RecoverableSecurityException
Browse files Browse the repository at this point in the history
  • Loading branch information
dongourdet committed Dec 1, 2021
1 parent bf12b39 commit fa95703
Showing 1 changed file with 54 additions and 18 deletions.
72 changes: 54 additions & 18 deletions src/android/CameraLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@ Licensed to the Apache Software Foundation (ASF) under one

import android.Manifest;
import android.app.Activity;
import android.app.PendingIntent;
import android.app.RecoverableSecurityException;
import android.content.ActivityNotFoundException;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.content.IntentSender;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.database.Cursor;
Expand Down Expand Up @@ -78,6 +81,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
private static final int PICTURE = 0; // allow selection of still pictures only. DEFAULT. Will return format specified via DestinationType
private static final int VIDEO = 1; // allow selection of video only, ONLY RETURNS URL
private static final int ALLMEDIA = 2; // allow selection from all media types
private static final int RECOVERABLE_DELETE_REQUEST = 3; // Result of Recoverable Security Exception

private static final int JPEG = 0; // Take a picture of type JPEG
private static final int PNG = 1; // Take a picture of type PNG
Expand Down Expand Up @@ -133,7 +137,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
private String croppedFilePath;
private ExifHelper exifData; // Exif data from source
private String applicationId;

private Uri pendingDeleteMediaUri;

/**
* Executes the request and returns PluginResult.
Expand Down Expand Up @@ -873,6 +877,15 @@ public void run() {
} else {
this.failPicture("Selection did not complete!");
}
} else if(requestCode == RECOVERABLE_DELETE_REQUEST){
// retry media store deletion ...
ContentResolver contentResolver = this.cordova.getActivity().getContentResolver();
try {
contentResolver.delete(this.pendingDeleteMediaUri, null, null);
} catch (Exception e) {
LOG.e(LOG_TAG, "Unable to delete media store file after permission was granted");
}
this.pendingDeleteMediaUri = null;
}
}

Expand Down Expand Up @@ -1222,26 +1235,49 @@ private void cleanup(int imageType, Uri oldImage, Uri newImage, Bitmap bitmap) {
* @param type FILE_URI or DATA_URL
*/
private void checkForDuplicateImage(int type) {
int diff = 1;
Uri contentStore = whichContentStore();
Cursor cursor = queryImgDB(contentStore);
int currentNumOfImages = cursor.getCount();

if (type == FILE_URI && this.saveToPhotoAlbum) {
diff = 2;
int diff = 1;
Uri contentStore = whichContentStore();
Cursor cursor = queryImgDB(contentStore);
int currentNumOfImages = cursor.getCount();

if (type == FILE_URI && this.saveToPhotoAlbum) {
diff = 2;
}

// delete the duplicate file if the difference is 2 for file URI or 1 for Data URL
if ((currentNumOfImages - numPics) == diff) {
cursor.moveToLast();
int id = Integer.valueOf(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media._ID)));
if (diff == 2) {
id--;
}

// delete the duplicate file if the difference is 2 for file URI or 1 for Data URL
if ((currentNumOfImages - numPics) == diff) {
cursor.moveToLast();
int id = Integer.valueOf(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media._ID)));
if (diff == 2) {
id--;
Uri uri = Uri.parse(contentStore + "/" + id);
try {
this.cordova.getActivity().getContentResolver().delete(uri, null, null);
} catch (SecurityException securityException) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
RecoverableSecurityException recoverableSecurityException;
if (securityException instanceof RecoverableSecurityException) {
recoverableSecurityException = (RecoverableSecurityException) securityException;
} else {
throw new RuntimeException(securityException.getMessage(), securityException);
}
PendingIntent pendingIntent = recoverableSecurityException.getUserAction().getActionIntent();
this.cordova.setActivityResultCallback(this);
this.pendingDeleteMediaUri = uri;
try {
this.cordova.getActivity().startIntentSenderForResult(pendingIntent.getIntentSender(),
RECOVERABLE_DELETE_REQUEST, null, 0, 0,
0, null);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
Uri uri = Uri.parse(contentStore + "/" + id);
this.cordova.getActivity().getContentResolver().delete(uri, null, null);
cursor.close();
} else {
throw new RuntimeException(securityException.getMessage(), securityException);
}
}
cursor.close();
}
}

/**
Expand Down

0 comments on commit fa95703

Please sign in to comment.