From 6451036b2509e825bfa86d06af64e8573f4e1f79 Mon Sep 17 00:00:00 2001 From: M66B Date: Wed, 27 Feb 2019 11:16:38 +0000 Subject: [PATCH] Refactoring --- .../main/java/eu/faircode/email/JobDaily.java | 21 ++++++++-------- .../eu/faircode/email/ServiceSynchronize.java | 24 +++++++++---------- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/eu/faircode/email/JobDaily.java b/app/src/main/java/eu/faircode/email/JobDaily.java index 71545e3610..5d5573a9ec 100644 --- a/app/src/main/java/eu/faircode/email/JobDaily.java +++ b/app/src/main/java/eu/faircode/email/JobDaily.java @@ -42,22 +42,23 @@ public class JobDaily extends JobService { private static final long CACHE_IMAGE_DURATION = 3 * 24 * 3600 * 1000L; // milliseconds private static final long KEEP_LOG_DURATION = 24 * 3600 * 1000L; // milliseconds - public static void schedule(Context context, boolean enabled) { + public static void schedule(Context context) { JobScheduler scheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); JobInfo.Builder job = new JobInfo.Builder(Helper.JOB_DAILY, new ComponentName(context, JobDaily.class)) .setPeriodic(CLEANUP_INTERVAL) .setRequiresDeviceIdle(true); - if (enabled) - if (scheduler.schedule(job.build()) == JobScheduler.RESULT_SUCCESS) - Log.i("Scheduled daily job"); - else - Log.e("Scheduling daily job failed"); - else { - Log.i("Cancelled daily job"); - scheduler.cancel(Helper.JOB_DAILY); - } + if (scheduler.schedule(job.build()) == JobScheduler.RESULT_SUCCESS) + Log.i("Scheduled daily job"); + else + Log.e("Scheduling daily job failed"); + } + + public static void cancel(Context context) { + JobScheduler scheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); + scheduler.cancel(Helper.JOB_DAILY); + Log.i("Cancelled daily job"); } @Override diff --git a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java index 674ecc8f60..077127104e 100644 --- a/app/src/main/java/eu/faircode/email/ServiceSynchronize.java +++ b/app/src/main/java/eu/faircode/email/ServiceSynchronize.java @@ -180,6 +180,8 @@ public class ServiceSynchronize extends LifecycleService { // builder.addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED); cm.registerNetworkCallback(builder.build(), serviceManager); + JobDaily.schedule(this); + DB db = DB.getInstance(this); db.account().liveStats().observe(this, new Observer() { @@ -295,6 +297,8 @@ public class ServiceSynchronize extends LifecycleService { Widget.update(this, -1); + JobDaily.cancel(this); + stopForeground(true); NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); @@ -336,7 +340,7 @@ public class ServiceSynchronize extends LifecycleService { case "init": // Network events will manage the service - serviceManager.service_init(intent.getBooleanExtra("schedule", false)); + serviceManager.service_init(intent.getBooleanExtra("boot", false)); break; case "schedule": @@ -3050,17 +3054,13 @@ public class ServiceSynchronize extends LifecycleService { return prefs.getBoolean("enabled", true); } - private void service_init(boolean schedule) { - boolean enabled = isEnabled(); - EntityLog.log(ServiceSynchronize.this, - "Service init schedule=" + schedule + " enabled=" + enabled); + private void service_init(boolean boot) { + EntityLog.log(ServiceSynchronize.this, "Service init boot=" + boot); - if (schedule) { + if (boot) next_schedule(); - JobDaily.schedule(ServiceSynchronize.this, enabled); - } - if (!enabled) + if (!isEnabled()) stopSelf(); } @@ -3085,8 +3085,6 @@ public class ServiceSynchronize extends LifecycleService { if (started) queue_reload(false, "service destroy"); } - - JobDaily.schedule(ServiceSynchronize.this, false); } private void start() { @@ -3384,11 +3382,11 @@ public class ServiceSynchronize extends LifecycleService { } } - public static void init(Context context, boolean schedule) { + public static void init(Context context, boolean boot) { ContextCompat.startForegroundService(context, new Intent(context, ServiceSynchronize.class) .setAction("init") - .putExtra("schedule", schedule)); + .putExtra("boot", boot)); } public static void schedule(Context context) {