Clean conditionaly when disabled

This commit is contained in:
M66B 2020-10-17 15:57:01 +02:00
parent a979846b79
commit d0487795e0
4 changed files with 41 additions and 21 deletions

View File

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

View File

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

View File

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

View File

@ -69,6 +69,24 @@ public class WorkerCleanup extends Worker {
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) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean fts = prefs.getBoolean("fts", true);
@ -279,41 +297,40 @@ public class WorkerCleanup extends Worker {
} finally {
Log.i("End cleanup");
long now = new Date().getTime();
prefs.edit()
.remove("crash_report_count")
.putLong("last_cleanup", new Date().getTime())
.putLong("last_cleanup", now)
.apply();
}
}
static void queue(Context context) {
static void init(Context context) {
try {
Log.i("Queuing " + getName() + " every " + CLEANUP_INTERVAL + " hours");
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
boolean enabled = prefs.getBoolean("enabled", true);
if (enabled) {
Log.i("Queuing " + getName() + " every " + CLEANUP_INTERVAL + " hours");
PeriodicWorkRequest workRequest =
new PeriodicWorkRequest.Builder(WorkerCleanup.class, CLEANUP_INTERVAL, TimeUnit.HOURS)
.setInitialDelay(CLEANUP_INTERVAL, TimeUnit.HOURS)
.build();
WorkManager.getInstance(context)
.enqueueUniquePeriodicWork(getName(), ExistingPeriodicWorkPolicy.KEEP, workRequest);
PeriodicWorkRequest workRequest =
new PeriodicWorkRequest.Builder(WorkerCleanup.class, CLEANUP_INTERVAL, TimeUnit.HOURS)
.setInitialDelay(CLEANUP_INTERVAL, TimeUnit.HOURS)
.build();
WorkManager.getInstance(context)
.enqueueUniquePeriodicWork(getName(), ExistingPeriodicWorkPolicy.KEEP, workRequest);
Log.i("Queued " + getName());
Log.i("Queued " + getName());
} else {
Log.i("Cancelling " + getName());
WorkManager.getInstance(context).cancelUniqueWork(getName());
Log.i("Cancelled " + getName());
}
} catch (IllegalStateException ex) {
// https://issuetracker.google.com/issues/138465476
Log.w(ex);
}
}
static void cancel(Context context) {
try {
Log.i("Cancelling " + getName());
WorkManager.getInstance(context).cancelUniqueWork(getName());
Log.i("Cancelled " + getName());
} catch (IllegalStateException ex) {
Log.w(ex);
}
}
private static String getName() {
return WorkerCleanup.class.getSimpleName();
}