blob: bcb6e9ee9d68a8c44ca386ee1c7c4f1d42f9e27d [file] [log] [blame]
Christopher Tated417d622013-08-19 16:14:25 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Christopher Tate115afda2014-06-06 19:06:26 -070019import java.util.Calendar;
20
Christopher Tate7060b042014-06-09 19:50:00 -070021import android.app.job.JobInfo;
22import android.app.job.JobParameters;
23import android.app.job.JobScheduler;
24import android.app.job.JobService;
Christopher Tate115afda2014-06-06 19:06:26 -070025import android.content.ComponentName;
26import android.content.Context;
Christopher Tated417d622013-08-19 16:14:25 -070027import android.util.Slog;
28
Christopher Tate7060b042014-06-09 19:50:00 -070029public class MountServiceIdler extends JobService {
Christopher Tated417d622013-08-19 16:14:25 -070030 private static final String TAG = "MountServiceIdler";
31
Christopher Tate115afda2014-06-06 19:06:26 -070032 private static ComponentName sIdleService =
33 new ComponentName(MountServiceIdler.class.getPackage().getName(),
34 MountServiceIdler.class.getName());
35
Christopher Tate7060b042014-06-09 19:50:00 -070036 private static int MOUNT_JOB_ID = 808;
Christopher Tate115afda2014-06-06 19:06:26 -070037
38 private boolean mStarted;
Christopher Tate7060b042014-06-09 19:50:00 -070039 private JobParameters mJobParams;
Christopher Tated417d622013-08-19 16:14:25 -070040 private Runnable mFinishCallback = new Runnable() {
41 @Override
42 public void run() {
43 Slog.i(TAG, "Got mount service completion callback");
Christopher Tate115afda2014-06-06 19:06:26 -070044 synchronized (mFinishCallback) {
45 if (mStarted) {
Christopher Tate7060b042014-06-09 19:50:00 -070046 jobFinished(mJobParams, false);
Christopher Tate115afda2014-06-06 19:06:26 -070047 mStarted = false;
48 }
49 }
50 // ... and try again tomorrow
51 scheduleIdlePass(MountServiceIdler.this);
Christopher Tated417d622013-08-19 16:14:25 -070052 }
53 };
54
55 @Override
Christopher Tate7060b042014-06-09 19:50:00 -070056 public boolean onStartJob(JobParameters params) {
Christopher Tated417d622013-08-19 16:14:25 -070057 // The mount service will run an fstrim operation asynchronously
58 // on a designated separate thread, so we provide it with a callback
59 // that lets us cleanly end our idle timeslice. It's safe to call
60 // finishIdle() from any thread.
Christopher Tate7060b042014-06-09 19:50:00 -070061 mJobParams = params;
Christopher Tated417d622013-08-19 16:14:25 -070062 MountService ms = MountService.sSelf;
63 if (ms != null) {
Christopher Tate115afda2014-06-06 19:06:26 -070064 synchronized (mFinishCallback) {
65 mStarted = true;
66 }
Christopher Tated417d622013-08-19 16:14:25 -070067 ms.runIdleMaintenance(mFinishCallback);
68 }
69 return ms != null;
70 }
71
72 @Override
Christopher Tate7060b042014-06-09 19:50:00 -070073 public boolean onStopJob(JobParameters params) {
Christopher Tate115afda2014-06-06 19:06:26 -070074 // Once we kick off the fstrim we aren't actually interruptible; just note
Christopher Tate7060b042014-06-09 19:50:00 -070075 // that we don't need to call jobFinished(), and let everything happen in
Christopher Tate115afda2014-06-06 19:06:26 -070076 // the callback from the mount service.
77 synchronized (mFinishCallback) {
78 mStarted = false;
79 }
80 return false;
81 }
82
83 /**
84 * Schedule the idle job that will ping the mount service
85 */
86 public static void scheduleIdlePass(Context context) {
Christopher Tate7060b042014-06-09 19:50:00 -070087 JobScheduler tm = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
Christopher Tate115afda2014-06-06 19:06:26 -070088
89 Calendar calendar = tomorrowMidnight();
90 final long timeToMidnight = calendar.getTimeInMillis() - System.currentTimeMillis();
91
Christopher Tate7060b042014-06-09 19:50:00 -070092 JobInfo.Builder builder = new JobInfo.Builder(MOUNT_JOB_ID, sIdleService);
Christopher Tate115afda2014-06-06 19:06:26 -070093 builder.setRequiresDeviceIdle(true);
94 builder.setRequiresCharging(true);
95 builder.setMinimumLatency(timeToMidnight);
96 tm.schedule(builder.build());
97 }
98
99 private static Calendar tomorrowMidnight() {
100 Calendar calendar = Calendar.getInstance();
101 calendar.setTimeInMillis(System.currentTimeMillis());
102 calendar.set(Calendar.HOUR, 0);
103 calendar.set(Calendar.MINUTE, 0);
104 calendar.set(Calendar.SECOND, 0);
105 calendar.set(Calendar.MILLISECOND, 0);
106 calendar.add(Calendar.DAY_OF_MONTH, 1);
107 return calendar;
Christopher Tated417d622013-08-19 16:14:25 -0700108 }
109}