Loading services/core/java/com/android/server/MountServiceIdler.java +7 −20 Original line number Diff line number Diff line Loading @@ -27,7 +27,6 @@ import android.content.ComponentName; import android.content.Context; import android.os.RemoteException; import android.util.Slog; import java.util.concurrent.TimeUnit; public class MountServiceIdler extends JobService { private static final String TAG = "MountServiceIdler"; Loading @@ -49,7 +48,7 @@ public class MountServiceIdler extends JobService { mStarted = false; } } // ... and try again right away or tomorrow // ... and try again tomorrow scheduleIdlePass(MountServiceIdler.this); } }; Loading Loading @@ -99,36 +98,24 @@ public class MountServiceIdler extends JobService { public static void scheduleIdlePass(Context context) { JobScheduler tm = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); final long today3AM = MidnightInTime(0, 0).getTimeInMillis(); final long today4AM = MidnightInTime(0, 1).getTimeInMillis(); long nextScheduleTime, maxScheduleTime; if (System.currentTimeMillis() > today3AM && System.currentTimeMillis() < today4AM) { nextScheduleTime = TimeUnit.SECONDS.toMillis(10); maxScheduleTime = today4AM - System.currentTimeMillis(); } else { final long tomorrow3AM = MidnightInTime(1, 0).getTimeInMillis(); final long twodays3AM = MidnightInTime(2, 0).getTimeInMillis(); nextScheduleTime = tomorrow3AM - System.currentTimeMillis(); // 3AM tomorrow maxScheduleTime = twodays3AM - System.currentTimeMillis(); // 3AM in two days } Calendar calendar = tomorrowMidnight(); final long timeToMidnight = calendar.getTimeInMillis() - System.currentTimeMillis(); JobInfo.Builder builder = new JobInfo.Builder(MOUNT_JOB_ID, sIdleService); builder.setRequiresDeviceIdle(true); builder.setRequiresCharging(true); builder.setMinimumLatency(nextScheduleTime); builder.setOverrideDeadline(maxScheduleTime); builder.setMinimumLatency(timeToMidnight); tm.schedule(builder.build()); } private static Calendar MidnightInTime(int nDays, int nHours) { private static Calendar tomorrowMidnight() { Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.set(Calendar.HOUR_OF_DAY, 3 + nHours); calendar.set(Calendar.HOUR_OF_DAY, 3); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); calendar.add(Calendar.DAY_OF_MONTH, nDays); calendar.add(Calendar.DAY_OF_MONTH, 1); return calendar; } } Loading
services/core/java/com/android/server/MountServiceIdler.java +7 −20 Original line number Diff line number Diff line Loading @@ -27,7 +27,6 @@ import android.content.ComponentName; import android.content.Context; import android.os.RemoteException; import android.util.Slog; import java.util.concurrent.TimeUnit; public class MountServiceIdler extends JobService { private static final String TAG = "MountServiceIdler"; Loading @@ -49,7 +48,7 @@ public class MountServiceIdler extends JobService { mStarted = false; } } // ... and try again right away or tomorrow // ... and try again tomorrow scheduleIdlePass(MountServiceIdler.this); } }; Loading Loading @@ -99,36 +98,24 @@ public class MountServiceIdler extends JobService { public static void scheduleIdlePass(Context context) { JobScheduler tm = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); final long today3AM = MidnightInTime(0, 0).getTimeInMillis(); final long today4AM = MidnightInTime(0, 1).getTimeInMillis(); long nextScheduleTime, maxScheduleTime; if (System.currentTimeMillis() > today3AM && System.currentTimeMillis() < today4AM) { nextScheduleTime = TimeUnit.SECONDS.toMillis(10); maxScheduleTime = today4AM - System.currentTimeMillis(); } else { final long tomorrow3AM = MidnightInTime(1, 0).getTimeInMillis(); final long twodays3AM = MidnightInTime(2, 0).getTimeInMillis(); nextScheduleTime = tomorrow3AM - System.currentTimeMillis(); // 3AM tomorrow maxScheduleTime = twodays3AM - System.currentTimeMillis(); // 3AM in two days } Calendar calendar = tomorrowMidnight(); final long timeToMidnight = calendar.getTimeInMillis() - System.currentTimeMillis(); JobInfo.Builder builder = new JobInfo.Builder(MOUNT_JOB_ID, sIdleService); builder.setRequiresDeviceIdle(true); builder.setRequiresCharging(true); builder.setMinimumLatency(nextScheduleTime); builder.setOverrideDeadline(maxScheduleTime); builder.setMinimumLatency(timeToMidnight); tm.schedule(builder.build()); } private static Calendar MidnightInTime(int nDays, int nHours) { private static Calendar tomorrowMidnight() { Calendar calendar = Calendar.getInstance(); calendar.setTimeInMillis(System.currentTimeMillis()); calendar.set(Calendar.HOUR_OF_DAY, 3 + nHours); calendar.set(Calendar.HOUR_OF_DAY, 3); calendar.set(Calendar.MINUTE, 0); calendar.set(Calendar.SECOND, 0); calendar.set(Calendar.MILLISECOND, 0); calendar.add(Calendar.DAY_OF_MONTH, nDays); calendar.add(Calendar.DAY_OF_MONTH, 1); return calendar; } }