blob: 1891ba9b1742c917b6d363c3d433c237fc3672e9 [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
Sudheer Shankadc589ac2016-11-10 15:30:17 -080021import android.app.ActivityManager;
Christopher Tate7060b042014-06-09 19:50:00 -070022import android.app.job.JobInfo;
23import android.app.job.JobParameters;
24import android.app.job.JobScheduler;
25import android.app.job.JobService;
Christopher Tate115afda2014-06-06 19:06:26 -070026import android.content.ComponentName;
27import android.content.Context;
Dianne Hackborn57a873f2014-12-04 13:58:46 -080028import android.os.RemoteException;
Christopher Tated417d622013-08-19 16:14:25 -070029import android.util.Slog;
30
Christopher Tate7060b042014-06-09 19:50:00 -070031public class MountServiceIdler extends JobService {
Christopher Tated417d622013-08-19 16:14:25 -070032 private static final String TAG = "MountServiceIdler";
33
Christopher Tate115afda2014-06-06 19:06:26 -070034 private static ComponentName sIdleService =
Christopher Tatef8ad7a92014-06-17 15:07:01 -070035 new ComponentName("android", MountServiceIdler.class.getName());
Christopher Tate115afda2014-06-06 19:06:26 -070036
Christopher Tate7060b042014-06-09 19:50:00 -070037 private static int MOUNT_JOB_ID = 808;
Christopher Tate115afda2014-06-06 19:06:26 -070038
39 private boolean mStarted;
Christopher Tate7060b042014-06-09 19:50:00 -070040 private JobParameters mJobParams;
Christopher Tated417d622013-08-19 16:14:25 -070041 private Runnable mFinishCallback = new Runnable() {
42 @Override
43 public void run() {
44 Slog.i(TAG, "Got mount service completion callback");
Christopher Tate115afda2014-06-06 19:06:26 -070045 synchronized (mFinishCallback) {
46 if (mStarted) {
Christopher Tate7060b042014-06-09 19:50:00 -070047 jobFinished(mJobParams, false);
Christopher Tate115afda2014-06-06 19:06:26 -070048 mStarted = false;
49 }
50 }
51 // ... and try again tomorrow
52 scheduleIdlePass(MountServiceIdler.this);
Christopher Tated417d622013-08-19 16:14:25 -070053 }
54 };
55
56 @Override
Christopher Tate7060b042014-06-09 19:50:00 -070057 public boolean onStartJob(JobParameters params) {
Dianne Hackborn57a873f2014-12-04 13:58:46 -080058 // First have the activity manager do its idle maintenance. (Yes this job
59 // is really more than just mount, some day it should be renamed to be system
60 // idleer).
61 try {
Sudheer Shankadc589ac2016-11-10 15:30:17 -080062 ActivityManager.getService().performIdleMaintenance();
Dianne Hackborn57a873f2014-12-04 13:58:46 -080063 } catch (RemoteException e) {
64 }
Christopher Tated417d622013-08-19 16:14:25 -070065 // The mount service will run an fstrim operation asynchronously
66 // on a designated separate thread, so we provide it with a callback
67 // that lets us cleanly end our idle timeslice. It's safe to call
68 // finishIdle() from any thread.
Christopher Tate7060b042014-06-09 19:50:00 -070069 mJobParams = params;
Sudheer Shanka2250d562016-11-07 15:41:02 -080070 StorageManagerService ms = StorageManagerService.sSelf;
Christopher Tated417d622013-08-19 16:14:25 -070071 if (ms != null) {
Christopher Tate115afda2014-06-06 19:06:26 -070072 synchronized (mFinishCallback) {
73 mStarted = true;
74 }
Jin Qian49a58382017-10-24 17:48:00 -070075 ms.runIdleMaint(mFinishCallback);
Christopher Tated417d622013-08-19 16:14:25 -070076 }
77 return ms != null;
78 }
79
80 @Override
Christopher Tate7060b042014-06-09 19:50:00 -070081 public boolean onStopJob(JobParameters params) {
Christopher Tate115afda2014-06-06 19:06:26 -070082 // Once we kick off the fstrim we aren't actually interruptible; just note
Christopher Tate7060b042014-06-09 19:50:00 -070083 // that we don't need to call jobFinished(), and let everything happen in
Christopher Tate115afda2014-06-06 19:06:26 -070084 // the callback from the mount service.
Jin Qian49a58382017-10-24 17:48:00 -070085 StorageManagerService ms = StorageManagerService.sSelf;
86 if (ms != null) {
87 ms.abortIdleMaint(mFinishCallback);
88 synchronized (mFinishCallback) {
89 mStarted = false;
90 }
Christopher Tate115afda2014-06-06 19:06:26 -070091 }
92 return false;
93 }
94
95 /**
96 * Schedule the idle job that will ping the mount service
97 */
98 public static void scheduleIdlePass(Context context) {
Christopher Tate7060b042014-06-09 19:50:00 -070099 JobScheduler tm = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
Christopher Tate115afda2014-06-06 19:06:26 -0700100
101 Calendar calendar = tomorrowMidnight();
102 final long timeToMidnight = calendar.getTimeInMillis() - System.currentTimeMillis();
103
Christopher Tate7060b042014-06-09 19:50:00 -0700104 JobInfo.Builder builder = new JobInfo.Builder(MOUNT_JOB_ID, sIdleService);
Christopher Tate115afda2014-06-06 19:06:26 -0700105 builder.setRequiresDeviceIdle(true);
106 builder.setRequiresCharging(true);
107 builder.setMinimumLatency(timeToMidnight);
108 tm.schedule(builder.build());
109 }
110
111 private static Calendar tomorrowMidnight() {
112 Calendar calendar = Calendar.getInstance();
113 calendar.setTimeInMillis(System.currentTimeMillis());
Dianne Hackborn57a873f2014-12-04 13:58:46 -0800114 calendar.set(Calendar.HOUR_OF_DAY, 3);
Christopher Tate115afda2014-06-06 19:06:26 -0700115 calendar.set(Calendar.MINUTE, 0);
116 calendar.set(Calendar.SECOND, 0);
117 calendar.set(Calendar.MILLISECOND, 0);
118 calendar.add(Calendar.DAY_OF_MONTH, 1);
119 return calendar;
Christopher Tated417d622013-08-19 16:14:25 -0700120 }
121}