Refactoring

This commit is contained in:
M66B 2019-02-27 11:16:38 +00:00
parent 9ea21370e4
commit 6451036b25
2 changed files with 22 additions and 23 deletions

View File

@ -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

View File

@ -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<TupleAccountStats>() {
@ -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) {