From 942101d7e253669ad232a9556304d6e1ff07f9bd Mon Sep 17 00:00:00 2001 From: Stas Parshin Date: Wed, 21 Dec 2016 15:06:28 +0700 Subject: [PATCH] Fix Context leak in LatestAppVersion task, #64, #66 --- .../javiersantos/appupdater/AppUpdater.java | 5 +++ .../javiersantos/appupdater/UtilsAsync.java | 43 +++++++++++++------ 2 files changed, 36 insertions(+), 12 deletions(-) diff --git a/library/src/main/java/com/github/javiersantos/appupdater/AppUpdater.java b/library/src/main/java/com/github/javiersantos/appupdater/AppUpdater.java index 0c0261b8..ed963a53 100644 --- a/library/src/main/java/com/github/javiersantos/appupdater/AppUpdater.java +++ b/library/src/main/java/com/github/javiersantos/appupdater/AppUpdater.java @@ -1,5 +1,6 @@ package com.github.javiersantos.appupdater; +import android.app.Activity; import android.content.Context; import android.support.annotation.DrawableRes; import android.support.annotation.NonNull; @@ -300,6 +301,10 @@ public void start() { latestAppVersion = new UtilsAsync.LatestAppVersion(context, false, updateFrom, gitHub, xmlOrJsonUrl, new LibraryListener() { @Override public void onSuccess(Update update) { + if (context instanceof Activity && ((Activity) context).isFinishing()) { + return; + } + if (UtilsLibrary.isUpdateAvailable(UtilsLibrary.getAppInstalledVersion(context), update.getLatestVersion())) { Integer successfulChecks = libraryPreferences.getSuccessfulChecks(); if (UtilsLibrary.isAbleToShow(successfulChecks, showEvery)) { diff --git a/library/src/main/java/com/github/javiersantos/appupdater/UtilsAsync.java b/library/src/main/java/com/github/javiersantos/appupdater/UtilsAsync.java index e9a4e453..68ec0901 100644 --- a/library/src/main/java/com/github/javiersantos/appupdater/UtilsAsync.java +++ b/library/src/main/java/com/github/javiersantos/appupdater/UtilsAsync.java @@ -8,32 +8,38 @@ import com.github.javiersantos.appupdater.objects.GitHub; import com.github.javiersantos.appupdater.objects.Update; +import java.lang.ref.WeakReference; + class UtilsAsync { static class LatestAppVersion extends AsyncTask { - private Context context; + private WeakReference contextRef; private LibraryPreferences libraryPreferences; private Boolean fromUtils; private UpdateFrom updateFrom; private GitHub gitHub; private String xmlOrJsonUrl; - private AppUpdater.LibraryListener listener; + private WeakReference listenerRef; public LatestAppVersion(Context context, Boolean fromUtils, UpdateFrom updateFrom, GitHub gitHub, String xmlOrJsonUrl, AppUpdater.LibraryListener listener) { - this.context = context; + this.contextRef = new WeakReference<>(context); this.libraryPreferences = new LibraryPreferences(context); this.fromUtils = fromUtils; this.updateFrom = updateFrom; this.gitHub = gitHub; this.xmlOrJsonUrl = xmlOrJsonUrl; - this.listener = listener; + this.listenerRef = new WeakReference<>(listener); } @Override protected void onPreExecute() { super.onPreExecute(); - if (UtilsLibrary.isNetworkAvailable(context)) { + Context context = contextRef.get(); + AppUpdater.LibraryListener listener = listenerRef.get(); + if (context == null || listener == null) { + cancel(true); + } else if (UtilsLibrary.isNetworkAvailable(context)) { if (!fromUtils && !libraryPreferences.getAppUpdaterShow()) { cancel(true); } else { @@ -60,28 +66,41 @@ protected void onPreExecute() { protected Update doInBackground(Void... voids) { if (updateFrom == UpdateFrom.XML || updateFrom == UpdateFrom.JSON) { Update update = UtilsLibrary.getLatestAppVersion(updateFrom, xmlOrJsonUrl); - if (update != null) { + if (update != null) { return update; } else { AppUpdaterError error = updateFrom == UpdateFrom.XML ? AppUpdaterError.XML_ERROR : AppUpdaterError.JSON_ERROR; - listener.onFailed(error); + AppUpdater.LibraryListener listener = listenerRef.get(); + if (listener != null) { + listener.onFailed(error); + } cancel(true); return null; } } else { - return UtilsLibrary.getLatestAppVersionHttp(context, updateFrom, gitHub); + Context context = contextRef.get(); + if (context != null) { + return UtilsLibrary.getLatestAppVersionHttp(context, updateFrom, gitHub); + } else { + cancel(true); + return null; + } } } @Override protected void onPostExecute(Update update) { super.onPostExecute(update); - if (UtilsLibrary.isStringAVersion(update.getLatestVersion())) { - listener.onSuccess(update); - } else { - listener.onFailed(AppUpdaterError.UPDATE_VARIES_BY_DEVICE); + + AppUpdater.LibraryListener listener = listenerRef.get(); + if (listener != null) { + if (UtilsLibrary.isStringAVersion(update.getLatestVersion())) { + listener.onSuccess(update); + } else { + listener.onFailed(AppUpdaterError.UPDATE_VARIES_BY_DEVICE); + } } } }