Skip to content

Commit 09763f9

Browse files
Merge pull request #1390 from commons-app/fixNearbyLeaks
Fix nearby leaks, at least some ot them
2 parents 9b2f192 + 7b5c5ce commit 09763f9

File tree

4 files changed

+48
-37
lines changed

4 files changed

+48
-37
lines changed

app/src/main/java/fr/free/nrw/commons/nearby/NearbyActivity.java

+8-3
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,6 @@ protected void onStart() {
262262
@Override
263263
protected void onStop() {
264264
super.onStop();
265-
locationManager.removeLocationListener(this);
266-
locationManager.unregisterLocationManager();
267265
}
268266

269267
@Override
@@ -292,8 +290,13 @@ public void onPause() {
292290
// to the retained fragment object to perform its own cleanup.
293291
removeMapFragment();
294292
removeListFragment();
295-
unregisterReceiver(broadcastReceiver);
293+
296294
}
295+
unregisterReceiver(broadcastReceiver);
296+
broadcastReceiver = null;
297+
locationManager.removeLocationListener(this);
298+
locationManager.unregisterLocationManager();
299+
297300
}
298301

299302
private void addNetworkBroadcastReceiver() {
@@ -422,6 +425,7 @@ private void removeMapFragment() {
422425
if (nearbyMapFragment != null) {
423426
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
424427
fm.beginTransaction().remove(nearbyMapFragment).commit();
428+
nearbyMapFragment = null;
425429
}
426430
}
427431

@@ -433,6 +437,7 @@ private void removeListFragment() {
433437
if (nearbyListFragment != null) {
434438
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
435439
fm.beginTransaction().remove(nearbyListFragment).commit();
440+
nearbyListFragment = null;
436441
}
437442
}
438443

app/src/main/java/fr/free/nrw/commons/nearby/NearbyMapFragment.java

+3
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,9 @@ public void onDestroyView() {
823823
if (mapView != null) {
824824
mapView.onDestroy();
825825
}
826+
selected = null;
827+
currentLocationMarker = null;
828+
826829
super.onDestroyView();
827830
}
828831

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

+34-9
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

+3-25
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)