Skip to content

Commit

Permalink
Merge branch 'feature/update-helper-looping-fix-VIALA-792' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy.norman committed Mar 21, 2018
2 parents 04c76ce + a1b2851 commit 67e92cb
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions app/src/main/java/com/voipgrid/vialer/util/UpdateHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.preference.PreferenceManager;

Expand Down Expand Up @@ -41,6 +39,16 @@ public class UpdateHelper extends AsyncTask<Void, Void, Void> {
private final static String VERSION_CODE = "version_code";
private Boolean succesfulMigrate = true;

/**
* The milliseconds since a migration was last attempted.
*/
private static long lastMigrationAttempt = 0;

/**
* The time between migration attempts.
*/
private static final long MIGRATION_ATTEMPT_TIMEOUT_MS = (60 * 60 * 24) * 1000;

public UpdateHelper(Context context, OnUpdateCompleted listener) {
mContext = context;
mJsonStorage = new JsonStorage(mContext);
Expand All @@ -52,6 +60,7 @@ public UpdateHelper(Context context, OnUpdateCompleted listener) {

@Override
protected Void doInBackground(Void... params) {
lastMigrationAttempt = System.currentTimeMillis();
int currentVersion = BuildConfig.VERSION_CODE;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext);
int lastVersion = prefs.getInt(VERSION_CODE, 0);
Expand All @@ -73,6 +82,8 @@ protected Void doInBackground(Void... params) {
}

public static boolean requiresUpdate(Context context) {
if(lastMigrationAttempt > (System.currentTimeMillis() - MIGRATION_ATTEMPT_TIMEOUT_MS) ) return false;

int lastVersion = PreferenceManager.getDefaultSharedPreferences(context).getInt(VERSION_CODE, 0);

return BuildConfig.VERSION_CODE != lastVersion;
Expand Down Expand Up @@ -145,9 +156,16 @@ private void enableSecureCalling() {

try {
Response response = call.execute();
if(!response.isSuccessful()) succesfulMigrate = false;
if(!response.isSuccessful()) {
handleApiFailure(response.code());
}
} catch (IOException e) {
succesfulMigrate = false;
handleApiFailure(0);
}
}

private void handleApiFailure(int code) {
mRemoteLogger.e("Enabling secure calling failed with code " + code);
succesfulMigrate = false;
}
}

0 comments on commit 67e92cb

Please sign in to comment.