Clean conditionaly when disabled

pull/187/head
M66B 5 years ago
parent a979846b79
commit d0487795e0

@ -139,7 +139,7 @@ public class ApplicationEx extends Application implements SharedPreferences.OnSh
DisconnectBlacklist.init(this); DisconnectBlacklist.init(this);
WorkerWatchdog.init(this); WorkerWatchdog.init(this);
WorkerCleanup.queue(this); WorkerCleanup.init(this);
registerReceiver(onScreenOff, new IntentFilter(Intent.ACTION_SCREEN_OFF)); registerReceiver(onScreenOff, new IntentFilter(Intent.ACTION_SCREEN_OFF));

@ -147,6 +147,7 @@ public class FragmentOptionsSynchronize extends FragmentBase implements SharedPr
public void onCheckedChanged(CompoundButton compoundButton, boolean checked) { public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
prefs.edit().putBoolean("enabled", checked).apply(); prefs.edit().putBoolean("enabled", checked).apply();
ServiceSynchronize.reschedule(getContext()); ServiceSynchronize.reschedule(getContext());
WorkerCleanup.init(getContext());
} }
}); });

@ -454,6 +454,8 @@ public class ServiceSynchronize extends ServiceBase implements SharedPreferences
// Stop service // Stop service
stopSelf(); stopSelf();
EntityLog.log(ServiceSynchronize.this, "### stop self eventId=" + eventId); EntityLog.log(ServiceSynchronize.this, "### stop self eventId=" + eventId);
WorkerCleanup.cleanupConditionally(ServiceSynchronize.this);
} }
} }
}); });

@ -69,6 +69,24 @@ public class WorkerCleanup extends Worker {
return Result.success(); return Result.success();
} }
static void cleanupConditionally(Context context) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean enabled = prefs.getBoolean("enabled", true);
if (enabled) {
Log.i("Skip cleanup enabled=" + enabled);
return;
}
long now = new Date().getTime();
long last_cleanup = prefs.getLong("last_cleanup", 0);
if (last_cleanup + CLEANUP_INTERVAL * 3600 * 1000L > now) {
Log.i("Skip cleanup last=" + new Date(last_cleanup));
return;
}
cleanup(context, false);
}
static void cleanup(Context context, boolean manual) { static void cleanup(Context context, boolean manual) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean fts = prefs.getBoolean("fts", true); boolean fts = prefs.getBoolean("fts", true);
@ -279,15 +297,19 @@ public class WorkerCleanup extends Worker {
} finally { } finally {
Log.i("End cleanup"); Log.i("End cleanup");
long now = new Date().getTime();
prefs.edit() prefs.edit()
.remove("crash_report_count") .remove("crash_report_count")
.putLong("last_cleanup", new Date().getTime()) .putLong("last_cleanup", now)
.apply(); .apply();
} }
} }
static void queue(Context context) { static void init(Context context) {
try { try {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean enabled = prefs.getBoolean("enabled", true);
if (enabled) {
Log.i("Queuing " + getName() + " every " + CLEANUP_INTERVAL + " hours"); Log.i("Queuing " + getName() + " every " + CLEANUP_INTERVAL + " hours");
PeriodicWorkRequest workRequest = PeriodicWorkRequest workRequest =
@ -298,18 +320,13 @@ public class WorkerCleanup extends Worker {
.enqueueUniquePeriodicWork(getName(), ExistingPeriodicWorkPolicy.KEEP, workRequest); .enqueueUniquePeriodicWork(getName(), ExistingPeriodicWorkPolicy.KEEP, workRequest);
Log.i("Queued " + getName()); Log.i("Queued " + getName());
} catch (IllegalStateException ex) { } else {
// https://issuetracker.google.com/issues/138465476
Log.w(ex);
}
}
static void cancel(Context context) {
try {
Log.i("Cancelling " + getName()); Log.i("Cancelling " + getName());
WorkManager.getInstance(context).cancelUniqueWork(getName()); WorkManager.getInstance(context).cancelUniqueWork(getName());
Log.i("Cancelled " + getName()); Log.i("Cancelled " + getName());
}
} catch (IllegalStateException ex) { } catch (IllegalStateException ex) {
// https://issuetracker.google.com/issues/138465476
Log.w(ex); Log.w(ex);
} }
} }

Loading…
Cancel
Save