Skip to content

Commit

Permalink
Fix Context leak in LatestAppVersion task, #64, #66
Browse files Browse the repository at this point in the history
  • Loading branch information
pengrad committed Dec 21, 2016
1 parent 4fbf69b commit 942101d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Void, Void, Update> {
private Context context;
private WeakReference<Context> contextRef;
private LibraryPreferences libraryPreferences;
private Boolean fromUtils;
private UpdateFrom updateFrom;
private GitHub gitHub;
private String xmlOrJsonUrl;
private AppUpdater.LibraryListener listener;
private WeakReference<AppUpdater.LibraryListener> 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 {
Expand All @@ -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);
}
}
}
}
Expand Down

0 comments on commit 942101d

Please sign in to comment.