Skip to content

Instantly share code, notes, and snippets.

View varundwarkani's full-sized avatar

Varun Dwarkani varundwarkani

View GitHub Profile
@varundwarkani
varundwarkani / InAppUpdateTask.java
Created March 11, 2020 17:18
In-App Update Task
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.
@varundwarkani
varundwarkani / InAppUpdateDownloadListener.java
Created March 11, 2020 17:19
InAppUpdateDownloadListener
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);
@varundwarkani
varundwarkani / InAppUpdateVariables.java
Created March 11, 2020 17:20
InAppUpdateVariables
private AppUpdateManager appUpdateManager;
private int UPDATE_REQUEST_CODE = 23;
InstallStateUpdatedListener listener;
appUpdateManager = AppUpdateManagerFactory.create(getApplicationContext());
@varundwarkani
varundwarkani / InAppUpdateStartInAppUpdate.java
Created March 11, 2020 17:24
InAppUpdateStartInAppUpdate
//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 {
@varundwarkani
varundwarkani / InAppUpdateStartInAppUpdate.java
Created March 11, 2020 17:24
InAppUpdateStartInAppUpdate
//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 {
@varundwarkani
varundwarkani / InAppUpdateListener.java
Created March 11, 2020 17:27
InAppUpdateListener
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.
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();
@varundwarkani
varundwarkani / backupDatabase.java
Created May 8, 2020 17:42
Backup Room Database Method in Android
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 {
@varundwarkani
varundwarkani / checkAndDeleteBackupFile.java
Created May 8, 2020 17:54
checkAndDeleteBackupFile() method is used to delete the oldest backup file and keep the count of file to maximum of MAXIMUM_DATABASE_FILE
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++) {
@varundwarkani
varundwarkani / backupDatabaseForRestore.java
Created May 8, 2020 18:05
For creating a temporary backup of the database before we restore the database file.
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.");