refactor(android): remove query img usage (#907)

* refactor: remove unnecessary duplicate image checks and queryImgDb usage

* remove unused imageType parameter, because it's a private API anyway
This commit is contained in:
Norman Breau 2024-10-28 13:32:35 -03:00 committed by GitHub
parent 0d86764b90
commit c208754c08
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -127,7 +127,6 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
private boolean allowEdit; // Should we allow the user to crop the image. private boolean allowEdit; // Should we allow the user to crop the image.
public CallbackContext callbackContext; public CallbackContext callbackContext;
private int numPics;
private MediaScannerConnection conn; // Used to update gallery app with newly-written files private MediaScannerConnection conn; // Used to update gallery app with newly-written files
private Uri scanMe; // Uri of image to be added to content store private Uri scanMe; // Uri of image to be added to content store
@ -307,9 +306,6 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
public void takePicture(int returnType, int encodingType) public void takePicture(int returnType, int encodingType)
{ {
// Save the number of images currently on disk for later
this.numPics = queryImgDB(whichContentStore()).getCount();
// Let's use the intent and see what happens // Let's use the intent and see what happens
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
@ -336,8 +332,6 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
LOG.d(LOG_TAG, "Error: You don't have a default camera. Your device may not be CTS complaint."); LOG.d(LOG_TAG, "Error: You don't have a default camera. Your device may not be CTS complaint.");
} }
} }
// else
// LOG.d(LOG_TAG, "ERROR: You must use the CordovaInterface for this to work correctly. Please implement it in your activity");
} }
/** /**
@ -559,12 +553,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
return; return;
} }
this.processPicture(bitmap, this.encodingType); this.processPicture(bitmap, this.encodingType);
if (!this.saveToPhotoAlbum) {
checkForDuplicateImage(DATA_URL);
}
} }
// If sending filename back // If sending filename back
@ -626,7 +615,7 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
throw new IllegalStateException(); throw new IllegalStateException();
} }
this.cleanup(FILE_URI, this.imageUri, galleryUri, bitmap); this.cleanup(this.imageUri, galleryUri, bitmap);
bitmap = null; bitmap = null;
input.close(); input.close();
} }
@ -1207,26 +1196,12 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
} }
} }
/**
* Creates a cursor that can be used to determine how many images we have.
*
* @return a cursor
*/
private Cursor queryImgDB(Uri contentStore) {
return this.cordova.getActivity().getContentResolver().query(
contentStore,
new String[]{MediaStore.Images.Media._ID},
null,
null,
null);
}
/** /**
* Cleans up after picture taking. Checking for duplicates and that kind of stuff. * Cleans up after picture taking. Checking for duplicates and that kind of stuff.
* *
* @param newImage * @param newImage
*/ */
private void cleanup(int imageType, Uri oldImage, Uri newImage, Bitmap bitmap) { private void cleanup(Uri oldImage, Uri newImage, Bitmap bitmap) {
if (bitmap != null) { if (bitmap != null) {
bitmap.recycle(); bitmap.recycle();
} }
@ -1234,7 +1209,6 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
// Clean up initial camera-written image file. // Clean up initial camera-written image file.
(new File(FileHelper.stripFileProtocol(oldImage.toString()))).delete(); (new File(FileHelper.stripFileProtocol(oldImage.toString()))).delete();
checkForDuplicateImage(imageType);
// Scan for the gallery to update pic refs in gallery // Scan for the gallery to update pic refs in gallery
if (this.saveToPhotoAlbum && newImage != null) { if (this.saveToPhotoAlbum && newImage != null) {
this.scanForGallery(newImage); this.scanForGallery(newImage);
@ -1243,37 +1217,6 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
System.gc(); System.gc();
} }
/**
* Used to find out if we are in a situation where the Camera Intent adds to images
* to the content store. If we are using a FILE_URI and the number of images in the DB
* increases by 2 we have a duplicate, when using a DATA_URL the number is 1.
*
* @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;
}
// delete the duplicate file if the difference is 2 for file URI or 1 for Data URL
if ((currentNumOfImages - numPics) == diff) {
cursor.moveToLast();
@SuppressLint("Range")
int id = Integer.valueOf(cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media._ID)));
if (diff == 2) {
id--;
}
Uri uri = Uri.parse(contentStore + "/" + id);
this.cordova.getActivity().getContentResolver().delete(uri, null, null);
cursor.close();
}
}
/** /**
* Determine if we are storing the images in internal or external storage * Determine if we are storing the images in internal or external storage
* *
@ -1377,7 +1320,6 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
state.putInt("targetHeight", this.targetHeight); state.putInt("targetHeight", this.targetHeight);
state.putInt("encodingType", this.encodingType); state.putInt("encodingType", this.encodingType);
state.putInt("mediaType", this.mediaType); state.putInt("mediaType", this.mediaType);
state.putInt("numPics", this.numPics);
state.putBoolean("allowEdit", this.allowEdit); state.putBoolean("allowEdit", this.allowEdit);
state.putBoolean("correctOrientation", this.correctOrientation); state.putBoolean("correctOrientation", this.correctOrientation);
state.putBoolean("saveToPhotoAlbum", this.saveToPhotoAlbum); state.putBoolean("saveToPhotoAlbum", this.saveToPhotoAlbum);
@ -1401,7 +1343,6 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
this.targetHeight = state.getInt("targetHeight"); this.targetHeight = state.getInt("targetHeight");
this.encodingType = state.getInt("encodingType"); this.encodingType = state.getInt("encodingType");
this.mediaType = state.getInt("mediaType"); this.mediaType = state.getInt("mediaType");
this.numPics = state.getInt("numPics");
this.allowEdit = state.getBoolean("allowEdit"); this.allowEdit = state.getBoolean("allowEdit");
this.correctOrientation = state.getBoolean("correctOrientation"); this.correctOrientation = state.getBoolean("correctOrientation");
this.saveToPhotoAlbum = state.getBoolean("saveToPhotoAlbum"); this.saveToPhotoAlbum = state.getBoolean("saveToPhotoAlbum");