Skip to content

Commit

Permalink
Merge pull request #21 from SanojPunchihewa/dev
Browse files Browse the repository at this point in the history
Add Lifecycle observer to unregister the update state listener
  • Loading branch information
SanojPunchihewa authored Oct 6, 2019
2 parents 48c7b03 + 91f80df commit 456b0ad
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 17 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Activity> mActivityWeakReference;
private WeakReference<AppCompatActivity> mActivityWeakReference;

private static UpdateManager instance;

Expand All @@ -36,13 +40,14 @@ public class UpdateManager {
// Returns an intent object that you use to check for an update.
private Task<AppUpdateInfo> 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);
}
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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();
}
}

0 comments on commit 456b0ad

Please sign in to comment.