blob: 215d92d8a49b8b3ba4e1c03ba1c10548028bec0a [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 =
Christopher Tatef8ad7a92014-06-17 15:07:01 -070033 new ComponentName("android", MountServiceIdler.class.getName());
Christopher Tate115afda2014-06-06 19:06:26 -070034
Christopher Tate7060b042014-06-09 19:50:00 -070035 private static int MOUNT_JOB_ID = 808;
Christopher Tate115afda2014-06-06 19:06:26 -070036
37 private boolean mStarted;
Christopher Tate7060b042014-06-09 19:50:00 -070038 private JobParameters mJobParams;
Christopher Tated417d622013-08-19 16:14:25 -070039 private Runnable mFinishCallback = new Runnable() {
40 @Override
41 public void run() {
42 Slog.i(TAG, "Got mount service completion callback");
Christopher Tate115afda2014-06-06 19:06:26 -070043 synchronized (mFinishCallback) {
44 if (mStarted) {
Christopher Tate7060b042014-06-09 19:50:00 -070045 jobFinished(mJobParams, false);
Christopher Tate115afda2014-06-06 19:06:26 -070046 mStarted = false;
47 }
48 }
49 // ... and try again tomorrow
50 scheduleIdlePass(MountServiceIdler.this);
Christopher Tated417d622013-08-19 16:14:25 -070051 }
52 };
53
54 @Override
Christopher Tate7060b042014-06-09 19:50:00 -070055 public boolean onStartJob(JobParameters params) {
Christopher Tated417d622013-08-19 16:14:25 -070056 // The mount service will run an fstrim operation asynchronously
57 // on a designated separate thread, so we provide it with a callback
58 // that lets us cleanly end our idle timeslice. It's safe to call
59 // finishIdle() from any thread.
Christopher Tate7060b042014-06-09 19:50:00 -070060 mJobParams = params;
Christopher Tated417d622013-08-19 16:14:25 -070061 MountService ms = MountService.sSelf;
62 if (ms != null) {
Christopher Tate115afda2014-06-06 19:06:26 -070063 synchronized (mFinishCallback) {
64 mStarted = true;
65 }
Christopher Tated417d622013-08-19 16:14:25 -070066 ms.runIdleMaintenance(mFinishCallback);
67 }
68 return ms != null;
69 }
70
71 @Override
Christopher Tate7060b042014-06-09 19:50:00 -070072 public boolean onStopJob(JobParameters params) {
Christopher Tate115afda2014-06-06 19:06:26 -070073 // Once we kick off the fstrim we aren't actually interruptible; just note
Christopher Tate7060b042014-06-09 19:50:00 -070074 // that we don't need to call jobFinished(), and let everything happen in
Christopher Tate115afda2014-06-06 19:06:26 -070075 // the callback from the mount service.
76 synchronized (mFinishCallback) {
77 mStarted = false;
78 }
79 return false;
80 }
81
82 /**
83 * Schedule the idle job that will ping the mount service
84 */
85 public static void scheduleIdlePass(Context context) {
Christopher Tate7060b042014-06-09 19:50:00 -070086 JobScheduler tm = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
Christopher Tate115afda2014-06-06 19:06:26 -070087
88 Calendar calendar = tomorrowMidnight();
89 final long timeToMidnight = calendar.getTimeInMillis() - System.currentTimeMillis();
90
Christopher Tate7060b042014-06-09 19:50:00 -070091 JobInfo.Builder builder = new JobInfo.Builder(MOUNT_JOB_ID, sIdleService);
Christopher Tate115afda2014-06-06 19:06:26 -070092 builder.setRequiresDeviceIdle(true);
93 builder.setRequiresCharging(true);
94 builder.setMinimumLatency(timeToMidnight);
95 tm.schedule(builder.build());
96 }
97
98 private static Calendar tomorrowMidnight() {
99 Calendar calendar = Calendar.getInstance();
100 calendar.setTimeInMillis(System.currentTimeMillis());
101 calendar.set(Calendar.HOUR, 0);
102 calendar.set(Calendar.MINUTE, 0);
103 calendar.set(Calendar.SECOND, 0);
104 calendar.set(Calendar.MILLISECOND, 0);
105 calendar.add(Calendar.DAY_OF_MONTH, 1);
106 return calendar;
Christopher Tated417d622013-08-19 16:14:25 -0700107 }
108}