-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppUpdate.java
61 lines (55 loc) · 2.39 KB
/
AppUpdate.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
appUpdateManager = AppUpdateManagerFactory.create(this);
appUpdateManager.registerListener(listener);
appUpdateManager.getAppUpdateInfo().addOnSuccessListener(new OnSuccessListener<AppUpdateInfo>() {
@Override
public void onSuccess(AppUpdateInfo appUpdateInfo) {
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE
&& appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)){
requestUpdate(appUpdateInfo);
Toast.makeText(MainActivity.this, "Kindly Update the App", Toast.LENGTH_SHORT).show();
}
else if (appUpdateInfo.updateAvailability() == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS){
notifyUser();
}
else if (appUpdateInfo.installStatus() == InstallStatus.DOWNLOADED){
notifyUser();
}
}
});
InstallStateUpdatedListener listener = new InstallStateUpdatedListener() {
@Override
public void onStateUpdate(InstallState installState) {
if (installState.installStatus() == InstallStatus.DOWNLOADED){
notifyUser();
}
else if (installState.installStatus() == InstallStatus.INSTALLED){
if (appUpdateManager != null){
appUpdateManager.unregisterListener(listener);
}
}
}
};
private void requestUpdate(AppUpdateInfo appUpdateInfo){
try {
appUpdateManager.startUpdateFlowForResult(appUpdateInfo,AppUpdateType.FLEXIBLE,17326);
} catch (IntentSender.SendIntentException e) {
e.printStackTrace();
}
}
private void notifyUser() {
Snackbar snackbar =
Snackbar.make(findViewById(android.R.id.content),
"An update has just been downloaded.",
Snackbar.LENGTH_INDEFINITE);
snackbar.setAction("INSTALL", new View.OnClickListener() {
@Override
public void onClick(View view) {
if (appUpdateManager != null) {
appUpdateManager.completeUpdate();
}
}
});
snackbar.setActionTextColor(
getResources().getColor(android.R.color.white));
snackbar.show();
}