Skip to content

Commit 8cd87ad

Browse files
dbrantmisaochan
authored andcommitted
Prevent memory leak(s) from QuizChecker. (commons-app#2656)
1 parent 3ceaaa9 commit 8cd87ad

File tree

2 files changed

+11
-13
lines changed

2 files changed

+11
-13
lines changed

app/src/main/java/fr/free/nrw/commons/contributions/MainActivity.java

+1
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,7 @@ protected void onResume() {
506506

507507
@Override
508508
protected void onDestroy() {
509+
quizChecker.cleanup();
509510
locationManager.unregisterLocationManager();
510511
// Remove ourself from hashmap to prevent memory leaks
511512
locationManager = null;

app/src/main/java/fr/free/nrw/commons/quiz/QuizChecker.java

+10-13
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import android.annotation.SuppressLint;
44
import android.app.Activity;
5-
import android.content.Context;
65
import android.content.Intent;
76

87
import javax.inject.Inject;
@@ -20,8 +19,6 @@
2019
import io.reactivex.schedulers.Schedulers;
2120
import timber.log.Timber;
2221

23-
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
24-
2522
/**
2623
* fetches the number of images uploaded and number of images reverted.
2724
* Then it calculates the percentage of the images reverted
@@ -37,7 +34,6 @@ public class QuizChecker {
3734
private boolean isUploadCountFetched;
3835

3936
private CompositeDisposable compositeDisposable = new CompositeDisposable();
40-
public Context context;
4137

4238
private final SessionManager sessionManager;
4339
private final OkHttpJsonApiClient okHttpJsonApiClient;
@@ -50,16 +46,13 @@ public class QuizChecker {
5046

5147
/**
5248
* constructor to set the parameters for quiz
53-
* @param context context
5449
* @param sessionManager
5550
* @param okHttpJsonApiClient instance of MediaWikiApi
5651
*/
5752
@Inject
58-
public QuizChecker(Context context,
59-
SessionManager sessionManager,
53+
public QuizChecker(SessionManager sessionManager,
6054
OkHttpJsonApiClient okHttpJsonApiClient,
6155
@Named("default_preferences") JsonKvStore revertKvStore) {
62-
this.context = context;
6356
this.sessionManager = sessionManager;
6457
this.okHttpJsonApiClient = okHttpJsonApiClient;
6558
this.revertKvStore = revertKvStore;
@@ -70,6 +63,10 @@ public void initQuizCheck(Activity activity) {
7063
setRevertCount(activity);
7164
}
7265

66+
public void cleanup() {
67+
compositeDisposable.clear();
68+
}
69+
7370
/**
7471
* to fet the total number of images uploaded
7572
*/
@@ -151,10 +148,10 @@ private void calculateRevertParameter(Activity activity) {
151148
@SuppressLint("StringFormatInvalid")
152149
private void callQuiz(Activity activity) {
153150
DialogUtil.showAlertDialog(activity,
154-
context.getResources().getString(R.string.quiz),
155-
context.getResources().getString(R.string.quiz_alert_message, REVERT_PERCENTAGE_FOR_MESSAGE),
156-
context.getResources().getString(R.string.about_translate_proceed),
157-
context.getResources().getString(android.R.string.cancel),
151+
activity.getString(R.string.quiz),
152+
activity.getString(R.string.quiz_alert_message, REVERT_PERCENTAGE_FOR_MESSAGE),
153+
activity.getString(R.string.about_translate_proceed),
154+
activity.getString(android.R.string.cancel),
158155
() -> startQuizActivity(activity), null);
159156
}
160157

@@ -163,7 +160,7 @@ private void startQuizActivity(Activity activity) {
163160
revertKvStore.putInt(REVERT_SHARED_PREFERENCE, newRevetSharedPrefs);
164161
int newUploadCount = totalUploadCount + revertKvStore.getInt(UPLOAD_SHARED_PREFERENCE, 0);
165162
revertKvStore.putInt(UPLOAD_SHARED_PREFERENCE, newUploadCount);
166-
Intent i = new Intent(context, WelcomeActivity.class);
163+
Intent i = new Intent(activity, WelcomeActivity.class);
167164
i.putExtra("isQuiz", true);
168165
activity.startActivity(i);
169166
}

0 commit comments

Comments
 (0)