Skip to content

Commit

Permalink
refactor(android): remove query img usage (apache#907)
Browse files Browse the repository at this point in the history
* refactor: remove unnecessary duplicate image checks and queryImgDb usage

* remove unused imageType parameter, because it's a private API anyway
  • Loading branch information
breautek authored Oct 28, 2024
1 parent 0d86764 commit c208754
Showing 1 changed file with 2 additions and 61 deletions.
63 changes: 2 additions & 61 deletions src/android/CameraLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ public class CameraLauncher extends CordovaPlugin implements MediaScannerConnect
private boolean allowEdit; // Should we allow the user to crop the image.

public CallbackContext callbackContext;
private int numPics;

private MediaScannerConnection conn; // Used to update gallery app with newly-written files
private Uri scanMe; // Uri of image to be added to content store
Expand Down Expand Up @@ -307,9 +306,6 @@ public void callTakePicture(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
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

Expand All @@ -336,8 +332,6 @@ public void takePicture(int returnType, int encodingType)
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");
}

/**
Expand Down Expand Up @@ -559,12 +553,7 @@ private void processResultFromCamera(int destType, Intent intent) throws IOExcep
return;
}


this.processPicture(bitmap, this.encodingType);

if (!this.saveToPhotoAlbum) {
checkForDuplicateImage(DATA_URL);
}
}

// If sending filename back
Expand Down Expand Up @@ -626,7 +615,7 @@ else if (destType == FILE_URI) {
throw new IllegalStateException();
}

this.cleanup(FILE_URI, this.imageUri, galleryUri, bitmap);
this.cleanup(this.imageUri, galleryUri, bitmap);
bitmap = null;
input.close();
}
Expand Down Expand Up @@ -1207,34 +1196,19 @@ public static int calculateSampleSize(int srcWidth, int srcHeight, int dstWidth,
}
}

/**
* 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.
*
* @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) {
bitmap.recycle();
}

// Clean up initial camera-written image file.
(new File(FileHelper.stripFileProtocol(oldImage.toString()))).delete();

checkForDuplicateImage(imageType);
// Scan for the gallery to update pic refs in gallery
if (this.saveToPhotoAlbum && newImage != null) {
this.scanForGallery(newImage);
Expand All @@ -1243,37 +1217,6 @@ private void cleanup(int imageType, Uri oldImage, Uri newImage, Bitmap bitmap) {
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
*
Expand Down Expand Up @@ -1377,7 +1320,6 @@ public Bundle onSaveInstanceState() {
state.putInt("targetHeight", this.targetHeight);
state.putInt("encodingType", this.encodingType);
state.putInt("mediaType", this.mediaType);
state.putInt("numPics", this.numPics);
state.putBoolean("allowEdit", this.allowEdit);
state.putBoolean("correctOrientation", this.correctOrientation);
state.putBoolean("saveToPhotoAlbum", this.saveToPhotoAlbum);
Expand All @@ -1401,7 +1343,6 @@ public void onRestoreStateForActivityResult(Bundle state, CallbackContext callba
this.targetHeight = state.getInt("targetHeight");
this.encodingType = state.getInt("encodingType");
this.mediaType = state.getInt("mediaType");
this.numPics = state.getInt("numPics");
this.allowEdit = state.getBoolean("allowEdit");
this.correctOrientation = state.getBoolean("correctOrientation");
this.saveToPhotoAlbum = state.getBoolean("saveToPhotoAlbum");
Expand Down

0 comments on commit c208754

Please sign in to comment.