Changeset View
Changeset View
Standalone View
Standalone View
src/it/reyboz/bustorino/data/DBUpdateWorker.java
Show First 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | public class DBUpdateWorker extends Worker{ | ||||
public static final int ERROR_FETCHING_VERSION = 4; | public static final int ERROR_FETCHING_VERSION = 4; | ||||
public static final int ERROR_DOWNLOADING_STOPS = 5; | public static final int ERROR_DOWNLOADING_STOPS = 5; | ||||
public static final int ERROR_DOWNLOADING_LINES = 6; | public static final int ERROR_DOWNLOADING_LINES = 6; | ||||
public static final String SUCCESS_REASON_KEY = "SUCCESS_REASON"; | public static final String SUCCESS_REASON_KEY = "SUCCESS_REASON"; | ||||
public static final int SUCCESS_NO_ACTION_NEEDED = 9; | public static final int SUCCESS_NO_ACTION_NEEDED = 9; | ||||
public static final int SUCCESS_UPDATE_DONE = 1; | public static final int SUCCESS_UPDATE_DONE = 1; | ||||
private final int notifi_ID=62341; | private final static int NOTIFIC_ID =32198; | ||||
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 | private static final long UPDATE_MIN_DELAY= 9*24*3600; //9 days | ||||
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); | ||||
//Use the new notification channels | |||||
Notifications.createNotificationChannel(con,con.getString(R.string.database_notification_channel), | |||||
con.getString(R.string.database_notification_channel_desc), NotificationManagerCompat.IMPORTANCE_LOW, | |||||
Notifications.DB_UPDATE_CHANNELS_ID | |||||
); | |||||
final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getApplicationContext()); | |||||
final int notification_ID = 32198; | |||||
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); | final long lastDBUpdateTime = shPr.getLong(DatabaseUpdate.DB_LAST_UPDATE_KEY, 0); | ||||
long currentTime = System.currentTimeMillis()/1000; | long currentTime = System.currentTimeMillis()/1000; | ||||
final int notificationID = showNotification(); | //showNotification(notificationManager, notification_ID); | ||||
final NotificationCompat.Builder builder = new NotificationCompat.Builder(con, | |||||
Notifications.DB_UPDATE_CHANNELS_ID) | |||||
.setContentTitle(con.getString(R.string.database_update_msg_notif)) | |||||
.setProgress(0,0,true) | |||||
.setPriority(NotificationCompat.PRIORITY_LOW); | |||||
builder.setSmallIcon(R.drawable.ic_bus_orange); | |||||
notificationManager.notify(notification_ID,builder.build()); | |||||
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) | 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 || currentTime > lastDBUpdateTime + UPDATE_MIN_DELAY ) | if (!(current_DB_version < new_DB_version || currentTime > lastDBUpdateTime + UPDATE_MIN_DELAY ) | ||||
&& !isUpdateCompulsory) { | && !isUpdateCompulsory) { | ||||
//don't need to update | //don't need to update | ||||
cancelNotification(notificationID); | cancelNotification(notification_ID); | ||||
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); | ||||
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(notification_ID); | ||||
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; | currentTime = System.currentTimeMillis()/1000; | ||||
editor.putLong(DatabaseUpdate.DB_LAST_UPDATE_KEY, currentTime); | editor.putLong(DatabaseUpdate.DB_LAST_UPDATE_KEY, currentTime); | ||||
editor.apply(); | editor.apply(); | ||||
cancelNotification(notificationID); | cancelNotification(notification_ID); | ||||
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) | ||||
.setRequiresCharging(false).build(); | .setRequiresCharging(false).build(); | ||||
} | } | ||||
public static WorkRequest newFirstTimeWorkRequest(){ | public static WorkRequest newFirstTimeWorkRequest(){ | ||||
return new OneTimeWorkRequest.Builder(DBUpdateWorker.class) | return new OneTimeWorkRequest.Builder(DBUpdateWorker.class) | ||||
.setBackoffCriteria(BackoffPolicy.LINEAR, 15, TimeUnit.SECONDS) | .setBackoffCriteria(BackoffPolicy.LINEAR, 15, TimeUnit.SECONDS) | ||||
//.setInputData(new Data.Builder().putBoolean()) | //.setInputData(new Data.Builder().putBoolean()) | ||||
.build(); | .build(); | ||||
} | } | ||||
/* | |||||
private int showNotification(){ | private int showNotification(@NonNull final NotificationManagerCompat notificManager, final int notification_ID, | ||||
final NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), Notifications.DEFAULT_CHANNEL_ID) | final String channel_ID){ | ||||
final NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), channel_ID) | |||||
.setContentTitle("Libre BusTO - Updating Database") | .setContentTitle("Libre BusTO - Updating Database") | ||||
.setProgress(0,0,true) | .setProgress(0,0,true) | ||||
.setPriority(NotificationCompat.PRIORITY_LOW); | .setPriority(NotificationCompat.PRIORITY_LOW); | ||||
builder.setSmallIcon(R.drawable.ic_bus_orange); | builder.setSmallIcon(R.drawable.ic_bus_orange); | ||||
final NotificationManagerCompat notifcManager = NotificationManagerCompat.from(getApplicationContext()); | |||||
final int notification_ID = 32198; | |||||
notifcManager.notify(notification_ID,builder.build()); | notificManager.notify(notification_ID,builder.build()); | ||||
return notification_ID; | return notification_ID; | ||||
} | } | ||||
*/ | |||||
private void cancelNotification(int notificationID){ | private void cancelNotification(int notificationID){ | ||||
final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getApplicationContext()); | final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getApplicationContext()); | ||||
notificationManager.cancel(notificationID); | notificationManager.cancel(notificationID); | ||||
} | } | ||||
} | } |
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