From 75c5396a6f9c1d52dc37231d26f4deb8e7d7329b Mon Sep 17 00:00:00 2001 From: Sanoj Punchihewa Date: Sun, 6 Oct 2019 18:24:11 +0530 Subject: [PATCH 1/2] Add Lifecycle observer to unregister the update state listener --- .../updatemanager/UpdateManager.java | 49 +++++++++++++------ 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/updatemanager/src/main/java/com/sanojpunchihewa/updatemanager/UpdateManager.java b/updatemanager/src/main/java/com/sanojpunchihewa/updatemanager/UpdateManager.java index 3805de2..d4f85ee 100644 --- a/updatemanager/src/main/java/com/sanojpunchihewa/updatemanager/UpdateManager.java +++ b/updatemanager/src/main/java/com/sanojpunchihewa/updatemanager/UpdateManager.java @@ -7,6 +7,10 @@ import android.content.IntentSender; import android.util.Log; import android.view.View; +import androidx.appcompat.app.AppCompatActivity; +import androidx.lifecycle.Lifecycle.Event; +import androidx.lifecycle.LifecycleObserver; +import androidx.lifecycle.OnLifecycleEvent; import com.google.android.material.snackbar.Snackbar; import com.google.android.play.core.appupdate.AppUpdateInfo; import com.google.android.play.core.appupdate.AppUpdateManager; @@ -19,11 +23,11 @@ import com.google.android.play.core.tasks.Task; import java.lang.ref.WeakReference; -public class UpdateManager { +public class UpdateManager implements LifecycleObserver { private static final String TAG = "InAppUpdateManager"; - private WeakReference mActivityWeakReference; + private WeakReference mActivityWeakReference; private static UpdateManager instance; @@ -36,13 +40,14 @@ public class UpdateManager { // Returns an intent object that you use to check for an update. private Task appUpdateInfoTask; - private UpdateManager(Activity activity) { + private UpdateManager(AppCompatActivity activity) { mActivityWeakReference = new WeakReference<>(activity); this.appUpdateManager = AppUpdateManagerFactory.create(getActivity()); this.appUpdateInfoTask = appUpdateManager.getAppUpdateInfo(); + activity.getLifecycle().addObserver(this); } - public static UpdateManager Builder(Activity activity) { + public static UpdateManager Builder(AppCompatActivity activity) { if (instance == null) { instance = new UpdateManager(activity); } @@ -107,18 +112,19 @@ private void startUpdate(AppUpdateInfo appUpdateInfo) { // } // } - private void setUpListener() { - InstallStateUpdatedListener listener = new InstallStateUpdatedListener() { - @Override - public void onStateUpdate(InstallState installState) { - if (installState.installStatus() == InstallStatus.DOWNLOADED) { - // After the update is downloaded, show a notification - // and request user confirmation to restart the app. - Log.d(TAG, "An update has been downloaded"); - popupSnackbarForCompleteUpdate(); - } + private InstallStateUpdatedListener listener = new InstallStateUpdatedListener() { + @Override + public void onStateUpdate(InstallState installState) { + if (installState.installStatus() == InstallStatus.DOWNLOADED) { + // After the update is downloaded, show a notification + // and request user confirmation to restart the app. + Log.d(TAG, "An update has been downloaded"); + popupSnackbarForCompleteUpdate(); } - }; + } + }; + + private void setUpListener() { appUpdateManager.registerListener(listener); } @@ -204,9 +210,20 @@ private Activity getActivity() { return mActivityWeakReference.get(); } + private void unregisterListener() { + if (appUpdateManager != null && listener != null) { + appUpdateManager.unregisterListener(listener); + Log.d(TAG, "Unregistered the install state listener"); + } + } + public interface onVersionCheckListener { void onReceiveVersionCode(int code); } -} + @OnLifecycleEvent(Event.ON_DESTROY) + private void onDestroy() { + unregisterListener(); + } +} \ No newline at end of file From 91f80dfa514e9bb2c44b2cb0db6f694976d3ab34 Mon Sep 17 00:00:00 2001 From: Sanoj Punchihewa Date: Sun, 6 Oct 2019 18:31:24 +0530 Subject: [PATCH 2/2] Update version code in docs --- CHANGELOG.md | 3 +++ README.md | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 54b56d3..2e6268b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ All notable changes to InAppUpdater will be documented in this file. +## [1.0.5-alpha.1] - 2019-10-06 +- Add Lifecycle observer to unregister the update state listener + ## [1.0.4] - 2019-09-22 - Fix fading of Material TextInputLayout diff --git a/README.md b/README.md index e28574a..ba8c613 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ allprojects { ### Step 2: Add the dependency ```Gradle dependencies { - implementation 'com.github.SanojPunchihewa:InAppUpdater:1.0.4' + implementation 'com.github.SanojPunchihewa:InAppUpdater:1.0.5-alpha.1' } ```