Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/data/DBUpdateWorker.java
Show First 20 Lines • Show All 57 Lines • ▼ Show 20 Lines | public Result doWork() { | ||||
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); | ||||
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 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 && !isUpdateCompulsory) { | ||||
//don't need to update | //don't need to update | ||||
cancelNotification(notificationID); | cancelNotification(notificationID); | ||||
return 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); | ||||
final DatabaseUpdate.Result resultUpdate = DatabaseUpdate.performDBUpdate(con,resultAtomicReference); | final DatabaseUpdate.Result resultUpdate = DatabaseUpdate.performDBUpdate(con,resultAtomicReference); | ||||
DatabaseUpdate.setDBUpdatingFlag(con, shPr,false); | DatabaseUpdate.setDBUpdatingFlag(con, shPr,false); | ||||
if (resultUpdate != DatabaseUpdate.Result.DONE){ | if (resultUpdate != DatabaseUpdate.Result.DONE){ | ||||
Fetcher.result result = resultAtomicReference.get(); | Fetcher.Result result = resultAtomicReference.get(); | ||||
final Data.Builder dataBuilder = new Data.Builder(); | final Data.Builder dataBuilder = new Data.Builder(); | ||||
switch (resultUpdate){ | switch (resultUpdate){ | ||||
case ERROR_STOPS_DOWNLOAD: | case ERROR_STOPS_DOWNLOAD: | ||||
dataBuilder.put(ERROR_REASON_KEY, ERROR_DOWNLOADING_STOPS); | dataBuilder.put(ERROR_REASON_KEY, ERROR_DOWNLOADING_STOPS); | ||||
break; | break; | ||||
case ERROR_LINES_DOWNLOAD: | case ERROR_LINES_DOWNLOAD: | ||||
dataBuilder.put(ERROR_REASON_KEY, ERROR_DOWNLOADING_LINES); | dataBuilder.put(ERROR_REASON_KEY, ERROR_DOWNLOADING_LINES); | ||||
break; | break; | ||||
} | } | ||||
cancelNotification(notificationID); | cancelNotification(notificationID); | ||||
return 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); | ||||
editor.apply(); | editor.apply(); | ||||
cancelNotification(notificationID); | cancelNotification(notificationID); | ||||
return 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) | ||||
.setRequiresCharging(false).build(); | .setRequiresCharging(false).build(); | ||||
} | } | ||||
public static WorkRequest newFirstTimeWorkRequest(){ | public static WorkRequest newFirstTimeWorkRequest(){ | ||||
Show All 26 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