Skip to content

Commit 7b5c5ce

Browse files
committed
Fix nearby leaks D
1 parent ea57855 commit 7b5c5ce

File tree

2 files changed

+37
-34
lines changed

2 files changed

+37
-34
lines changed

app/src/main/java/fr/free/nrw/commons/upload/DetectUnwantedPicturesAsync.java

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
package fr.free.nrw.commons.upload;
22

3+
import android.app.Activity;
34
import android.content.Context;
5+
import android.content.Intent;
46
import android.graphics.BitmapRegionDecoder;
57
import android.net.Uri;
68
import android.os.AsyncTask;
9+
import android.support.v7.app.AlertDialog;
710

811
import java.io.IOException;
12+
import java.lang.ref.WeakReference;
913

14+
import fr.free.nrw.commons.R;
15+
import fr.free.nrw.commons.contributions.ContributionsActivity;
1016
import fr.free.nrw.commons.utils.ImageUtils;
1117
import timber.log.Timber;
1218

@@ -21,16 +27,13 @@
2127

2228
public class DetectUnwantedPicturesAsync extends AsyncTask<Void, Void, ImageUtils.Result> {
2329

24-
interface Callback {
25-
void onResult(ImageUtils.Result result);
26-
}
27-
28-
private final Callback callback;
2930
private final String imageMediaFilePath;
31+
public final WeakReference<Activity> activityWeakReference;
3032

31-
DetectUnwantedPicturesAsync(String imageMediaFilePath, Callback callback) {
32-
this.callback = callback;
33+
DetectUnwantedPicturesAsync(WeakReference<Activity> activityWeakReference, String imageMediaFilePath) {
34+
//this.callback = callback;
3335
this.imageMediaFilePath = imageMediaFilePath;
36+
this.activityWeakReference = activityWeakReference;
3437
}
3538

3639
@Override
@@ -53,7 +56,29 @@ protected ImageUtils.Result doInBackground(Void... voids) {
5356
@Override
5457
protected void onPostExecute(ImageUtils.Result result) {
5558
super.onPostExecute(result);
56-
//callback to UI so that it can take necessary decision based on the result obtained
57-
callback.onResult(result);
59+
Activity activity = activityWeakReference.get();
60+
61+
if (result != ImageUtils.Result.IMAGE_OK) {
62+
//show appropriate error message
63+
String errorMessage = result == ImageUtils.Result.IMAGE_DARK ? activity.getString(R.string.upload_image_too_dark) : activity.getString(R.string.upload_image_blurry);
64+
AlertDialog.Builder errorDialogBuilder = new AlertDialog.Builder(activity);
65+
errorDialogBuilder.setMessage(errorMessage);
66+
errorDialogBuilder.setTitle(activity.getString(R.string.warning));
67+
errorDialogBuilder.setPositiveButton(activity.getString(R.string.no), (dialogInterface, i) -> {
68+
//user does not wish to upload the picture, take them back to ContributionsActivity
69+
Intent intent = new Intent(activity, ContributionsActivity.class);
70+
dialogInterface.dismiss();
71+
activity.startActivity(intent);
72+
});
73+
errorDialogBuilder.setNegativeButton(activity.getString(R.string.yes), (dialogInterface, i) -> {
74+
//user wishes to go ahead with the upload of this picture, just dismiss this dialog
75+
dialogInterface.dismiss();
76+
});
77+
78+
AlertDialog errorDialog = errorDialogBuilder.create();
79+
if (!activity.isFinishing()) {
80+
errorDialog.show();
81+
}
82+
}
5883
}
5984
}

app/src/main/java/fr/free/nrw/commons/upload/ShareActivity.java

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -415,31 +415,9 @@ private void performPreUploadProcessingOfFile() {
415415

416416
private void performUnwantedPictureDetectionProcess() {
417417
String imageMediaFilePath = FileUtils.getPath(this,mediaUri);
418-
DetectUnwantedPicturesAsync detectUnwantedPicturesAsync = new DetectUnwantedPicturesAsync(imageMediaFilePath, result -> {
419-
420-
if (result != ImageUtils.Result.IMAGE_OK) {
421-
//show appropriate error message
422-
String errorMessage = result == ImageUtils.Result.IMAGE_DARK ? getString(R.string.upload_image_too_dark) : getString(R.string.upload_image_blurry);
423-
AlertDialog.Builder errorDialogBuilder = new AlertDialog.Builder(this);
424-
errorDialogBuilder.setMessage(errorMessage);
425-
errorDialogBuilder.setTitle(getString(R.string.warning));
426-
errorDialogBuilder.setPositiveButton(getString(R.string.no), (dialogInterface, i) -> {
427-
//user does not wish to upload the picture, take them back to ContributionsActivity
428-
Intent intent = new Intent(ShareActivity.this, ContributionsActivity.class);
429-
dialogInterface.dismiss();
430-
startActivity(intent);
431-
});
432-
errorDialogBuilder.setNegativeButton(getString(R.string.yes), (dialogInterface, i) -> {
433-
//user wishes to go ahead with the upload of this picture, just dismiss this dialog
434-
dialogInterface.dismiss();
435-
});
436-
437-
AlertDialog errorDialog = errorDialogBuilder.create();
438-
if (!isFinishing()) {
439-
errorDialog.show();
440-
}
441-
}
442-
});
418+
DetectUnwantedPicturesAsync detectUnwantedPicturesAsync
419+
= new DetectUnwantedPicturesAsync(new WeakReference<Activity>(this)
420+
, imageMediaFilePath);
443421

444422
detectUnwantedPicturesAsync.execute();
445423
}

0 commit comments

Comments
 (0)