This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
com.google.android.play.core.tasks.Task<AppUpdateInfo> appUpdateInfoTask = appUpdateManager.getAppUpdateInfo(); | |
// Checks that the platform will allow the specified type of update. | |
appUpdateInfoTask.addOnSuccessListener(new OnSuccessListener<AppUpdateInfo>() { | |
@Override | |
public void onSuccess(AppUpdateInfo appUpdateInfo) { | |
if (compulsory) { | |
if (appUpdateInfo.updateAvailability() == UpdateAvailability.UPDATE_AVAILABLE | |
// For a flexible update, use AppUpdateType.FLEXIBLE | |
&& appUpdateInfo.isUpdateTypeAllowed(IMMEDIATE)) { | |
// Request the update. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
listener = new InstallStateUpdatedListener() { | |
@Override | |
public void onStateUpdate(InstallState state) { | |
if (state.installStatus() == InstallStatus.DOWNLOADED) { | |
//Display Snackbar/dialog and ask the user to update | |
} | |
} | |
}; | |
appUpdateManager.registerListener(listener); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private AppUpdateManager appUpdateManager; | |
private int UPDATE_REQUEST_CODE = 23; | |
InstallStateUpdatedListener listener; | |
appUpdateManager = AppUpdateManagerFactory.create(getApplicationContext()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Boolean Compulsory is used to decide whether to start immediate update or flexible update. | |
private void startInAppUpdate(AppUpdateInfo appUpdateInfo, boolean compulsory) { | |
try { | |
if (compulsory) { | |
appUpdateManager.startUpdateFlowForResult( | |
appUpdateInfo, | |
IMMEDIATE, | |
this, | |
UPDATE_REQUEST_CODE); | |
} else { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Boolean Compulsory is used to decide whether to start immediate update or flexible update. | |
private void startInAppUpdate(AppUpdateInfo appUpdateInfo, boolean compulsory) { | |
try { | |
if (compulsory) { | |
appUpdateManager.startUpdateFlowForResult( | |
appUpdateInfo, | |
IMMEDIATE, | |
this, | |
UPDATE_REQUEST_CODE); | |
} else { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void setInAppUpdateSuccessListener(final boolean compulsory) { | |
if (appUpdateManager == null) { | |
appUpdateManager = AppUpdateManagerFactory.create(getApplicationContext()); | |
} | |
appUpdateManager.getAppUpdateInfo().addOnSuccessListener(new OnSuccessListener<AppUpdateInfo>() { | |
@Override | |
public void onSuccess(AppUpdateInfo appUpdateInfo) { | |
if (appUpdateInfo.updateAvailability() == UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS) { | |
try { | |
//call startInAppUpdate() from here as the user might have started an update before but did not download it completely. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void popupSnackbarForCompleteUpdate() { | |
if (!isFinishing()) { | |
new AlertDialog.Builder(this) | |
.setMessage(getString(R.string.inapp_downloaded)) | |
.setCancelable(false) | |
.setPositiveButton(getString(R.string.install), new DialogInterface.OnClickListener() { | |
@Override | |
public void onClick(DialogInterface dialog, int which) { | |
appUpdateManager.unregisterListener(listener); | |
appUpdateManager.completeUpdate(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void backupDatabase(Context context) { | |
AppDatabase appDatabase = AppDatabase.getAppDatabase(context); | |
appDatabase.close(); | |
File dbfile = context.getDatabasePath(DATABASE_NAME); | |
File sdir = new File(getFilePath(context, 0), "backup"); | |
String fileName = FILE_NAME + getDateFromMillisForBackup(System.currentTimeMillis()); | |
String sfpath = sdir.getPath() + File.separator + fileName; | |
if (!sdir.exists()) { | |
sdir.mkdirs(); | |
} else { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void checkAndDeleteBackupFile(File directory, String path) { | |
//This is to prevent deleting extra file being deleted which is mentioned in previous comment lines. | |
File currentDateFile = new File(path); | |
int fileIndex = -1; | |
long lastModifiedTime = System.currentTimeMillis(); | |
if (!currentDateFile.exists()) { | |
File[] files = directory.listFiles(); | |
if (files != null && files.length >= MAXIMUM_DATABASE_FILE) { | |
for (int i = 0; i < files.length; i++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void backupDatabaseForRestore(Activity activity, Context context) { | |
File dbfile = activity.getDatabasePath(DATABASE_NAME); | |
File sdir = new File(getFilePath(context, 0), "backup"); | |
String sfpath = sdir.getPath() + File.separator + BACKUP_RESTORE_ROLLBACK_FILE_NAME; | |
if (!sdir.exists()) { | |
sdir.mkdirs(); | |
} | |
File savefile = new File(sfpath); | |
if (savefile.exists()) { | |
Log.d(LOGGER, "Backup Restore - File exists. Deleting it and then creating new file."); |
OlderNewer