Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/data/DBUpdateWorker.java
Show First 20 Lines • Show All 47 Lines • ▼ Show 20 Lines | public class DBUpdateWorker extends Worker{ | ||||
public static final int SUCCESS_UPDATE_DONE = 1; | public static final int SUCCESS_UPDATE_DONE = 1; | ||||
private final int notifi_ID=62341; | private final int notifi_ID=62341; | ||||
public static final String FORCED_UPDATE = "FORCED-UPDATE"; | public static final String FORCED_UPDATE = "FORCED-UPDATE"; | ||||
public static final String DEBUG_TAG = "Busto-UpdateWorker"; | public static final String DEBUG_TAG = "Busto-UpdateWorker"; | ||||
private static final long UPDATE_MIN_DELAY= 3*7*24*3600; //3 weeks | |||||
public DBUpdateWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) { | public DBUpdateWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) { | ||||
super(context, workerParams); | super(context, workerParams); | ||||
} | } | ||||
@SuppressLint("RestrictedApi") | @SuppressLint("RestrictedApi") | ||||
@NonNull | @NonNull | ||||
@Override | @Override | ||||
public Result doWork() { | public Result doWork() { | ||||
//register Notification channel | //register Notification channel | ||||
final Context con = getApplicationContext(); | final Context con = getApplicationContext(); | ||||
Notifications.createDefaultNotificationChannel(con); | Notifications.createDefaultNotificationChannel(con); | ||||
final SharedPreferences shPr = con.getSharedPreferences(con.getString(R.string.mainSharedPreferences),MODE_PRIVATE); | final SharedPreferences shPr = con.getSharedPreferences(con.getString(R.string.mainSharedPreferences),MODE_PRIVATE); | ||||
final int current_DB_version = shPr.getInt(DatabaseUpdate.DB_VERSION_KEY,-10); | final int current_DB_version = shPr.getInt(DatabaseUpdate.DB_VERSION_KEY,-10); | ||||
final int new_DB_version = DatabaseUpdate.getNewVersion(); | final int new_DB_version = DatabaseUpdate.getNewVersion(); | ||||
final boolean isUpdateCompulsory = getInputData().getBoolean(FORCED_UPDATE,false); | final boolean isUpdateCompulsory = getInputData().getBoolean(FORCED_UPDATE,false); | ||||
final long lastDBUpdateTime = shPr.getLong(DatabaseUpdate.DB_LAST_UPDATE_KEY, 0); | |||||
long currentTime = System.currentTimeMillis()/1000; | |||||
final int notificationID = showNotification(); | final int notificationID = showNotification(); | ||||
Log.d(DEBUG_TAG, "Have previous version: "+current_DB_version +" and new version "+new_DB_version); | Log.d(DEBUG_TAG, "Have previous version: "+current_DB_version +" and new version "+new_DB_version); | ||||
Log.d(DEBUG_TAG, "Update compulsory: "+isUpdateCompulsory); | Log.d(DEBUG_TAG, "Update compulsory: "+isUpdateCompulsory); | ||||
/* | |||||
SKIP CHECK (Reason: The Old API might fail at any moment) | |||||
if (new_DB_version < 0){ | if (new_DB_version < 0){ | ||||
//there has been an error | //there has been an error | ||||
final Data out = new Data.Builder().putInt(ERROR_REASON_KEY, ERROR_FETCHING_VERSION) | final Data out = new Data.Builder().putInt(ERROR_REASON_KEY, ERROR_FETCHING_VERSION) | ||||
.putInt(ERROR_CODE_KEY,new_DB_version).build(); | .putInt(ERROR_CODE_KEY,new_DB_version).build(); | ||||
cancelNotification(notificationID); | cancelNotification(notificationID); | ||||
return ListenableWorker.Result.failure(out); | return ListenableWorker.Result.failure(out); | ||||
} | } | ||||
*/ | |||||
//we got a good version | //we got a good version | ||||
if (current_DB_version >= new_DB_version && !isUpdateCompulsory) { | if (!(current_DB_version < new_DB_version || currentTime > lastDBUpdateTime + UPDATE_MIN_DELAY ) | ||||
&& !isUpdateCompulsory) { | |||||
//don't need to update | //don't need to update | ||||
cancelNotification(notificationID); | cancelNotification(notificationID); | ||||
return ListenableWorker.Result.success(new Data.Builder(). | return ListenableWorker.Result.success(new Data.Builder(). | ||||
putInt(SUCCESS_REASON_KEY, SUCCESS_NO_ACTION_NEEDED).build()); | putInt(SUCCESS_REASON_KEY, SUCCESS_NO_ACTION_NEEDED).build()); | ||||
} | } | ||||
//start the real update | //start the real update | ||||
AtomicReference<Fetcher.Result> resultAtomicReference = new AtomicReference<>(); | AtomicReference<Fetcher.Result> resultAtomicReference = new AtomicReference<>(); | ||||
DatabaseUpdate.setDBUpdatingFlag(con, shPr,true); | DatabaseUpdate.setDBUpdatingFlag(con, shPr,true); | ||||
Show All 14 Lines | public Result doWork() { | ||||
} | } | ||||
cancelNotification(notificationID); | cancelNotification(notificationID); | ||||
return ListenableWorker.Result.failure(dataBuilder.build()); | return ListenableWorker.Result.failure(dataBuilder.build()); | ||||
} | } | ||||
Log.d(DEBUG_TAG, "Update finished successfully!"); | Log.d(DEBUG_TAG, "Update finished successfully!"); | ||||
//update the version in the shared preference | //update the version in the shared preference | ||||
final SharedPreferences.Editor editor = shPr.edit(); | final SharedPreferences.Editor editor = shPr.edit(); | ||||
editor.putInt(DatabaseUpdate.DB_VERSION_KEY, new_DB_version); | editor.putInt(DatabaseUpdate.DB_VERSION_KEY, new_DB_version); | ||||
currentTime = System.currentTimeMillis()/1000; | |||||
editor.putLong(DatabaseUpdate.DB_LAST_UPDATE_KEY, currentTime); | |||||
editor.apply(); | editor.apply(); | ||||
cancelNotification(notificationID); | cancelNotification(notificationID); | ||||
return ListenableWorker.Result.success(new Data.Builder().putInt(SUCCESS_REASON_KEY, SUCCESS_UPDATE_DONE).build()); | return ListenableWorker.Result.success(new Data.Builder().putInt(SUCCESS_REASON_KEY, SUCCESS_UPDATE_DONE).build()); | ||||
} | } | ||||
public static Constraints getWorkConstraints(){ | public static Constraints getWorkConstraints(){ | ||||
return new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED) | return new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED) | ||||
Show All 30 Lines |
Public contents are in Creative Commons Attribution-ShareAlike 4.0 (CC-BY-SA) or GNU Free Documentation License (at your option) unless otherwise noted. · Contact / Register