Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 19 | import java.util.Calendar; |
| 20 | |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 21 | import android.app.ActivityManager; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 22 | import android.app.job.JobInfo; |
| 23 | import android.app.job.JobParameters; |
| 24 | import android.app.job.JobScheduler; |
| 25 | import android.app.job.JobService; |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 26 | import android.content.ComponentName; |
| 27 | import android.content.Context; |
Dianne Hackborn | 57a873f | 2014-12-04 13:58:46 -0800 | [diff] [blame] | 28 | import android.os.RemoteException; |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 29 | import android.util.Slog; |
Jaegeuk Kim | 8975f78 | 2019-04-25 05:00:36 -0700 | [diff] [blame] | 30 | import java.util.concurrent.TimeUnit; |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 31 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 32 | public class MountServiceIdler extends JobService { |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 33 | private static final String TAG = "MountServiceIdler"; |
| 34 | |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 35 | private static ComponentName sIdleService = |
Christopher Tate | f8ad7a9 | 2014-06-17 15:07:01 -0700 | [diff] [blame] | 36 | new ComponentName("android", MountServiceIdler.class.getName()); |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 37 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 38 | private static int MOUNT_JOB_ID = 808; |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 39 | |
| 40 | private boolean mStarted; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 41 | private JobParameters mJobParams; |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 42 | private Runnable mFinishCallback = new Runnable() { |
| 43 | @Override |
| 44 | public void run() { |
| 45 | Slog.i(TAG, "Got mount service completion callback"); |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 46 | synchronized (mFinishCallback) { |
| 47 | if (mStarted) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 48 | jobFinished(mJobParams, false); |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 49 | mStarted = false; |
| 50 | } |
| 51 | } |
Jaegeuk Kim | 8975f78 | 2019-04-25 05:00:36 -0700 | [diff] [blame] | 52 | // ... and try again right away or tomorrow |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 53 | scheduleIdlePass(MountServiceIdler.this); |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 54 | } |
| 55 | }; |
| 56 | |
| 57 | @Override |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 58 | public boolean onStartJob(JobParameters params) { |
Dianne Hackborn | 57a873f | 2014-12-04 13:58:46 -0800 | [diff] [blame] | 59 | // First have the activity manager do its idle maintenance. (Yes this job |
| 60 | // is really more than just mount, some day it should be renamed to be system |
| 61 | // idleer). |
| 62 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 63 | ActivityManager.getService().performIdleMaintenance(); |
Dianne Hackborn | 57a873f | 2014-12-04 13:58:46 -0800 | [diff] [blame] | 64 | } catch (RemoteException e) { |
| 65 | } |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 66 | // The mount service will run an fstrim operation asynchronously |
| 67 | // on a designated separate thread, so we provide it with a callback |
| 68 | // that lets us cleanly end our idle timeslice. It's safe to call |
| 69 | // finishIdle() from any thread. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 70 | mJobParams = params; |
Sudheer Shanka | 2250d56 | 2016-11-07 15:41:02 -0800 | [diff] [blame] | 71 | StorageManagerService ms = StorageManagerService.sSelf; |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 72 | if (ms != null) { |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 73 | synchronized (mFinishCallback) { |
| 74 | mStarted = true; |
| 75 | } |
Jin Qian | 49a5838 | 2017-10-24 17:48:00 -0700 | [diff] [blame] | 76 | ms.runIdleMaint(mFinishCallback); |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 77 | } |
| 78 | return ms != null; |
| 79 | } |
| 80 | |
| 81 | @Override |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 82 | public boolean onStopJob(JobParameters params) { |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 83 | // Once we kick off the fstrim we aren't actually interruptible; just note |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 84 | // that we don't need to call jobFinished(), and let everything happen in |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 85 | // the callback from the mount service. |
Jin Qian | 49a5838 | 2017-10-24 17:48:00 -0700 | [diff] [blame] | 86 | StorageManagerService ms = StorageManagerService.sSelf; |
| 87 | if (ms != null) { |
| 88 | ms.abortIdleMaint(mFinishCallback); |
| 89 | synchronized (mFinishCallback) { |
| 90 | mStarted = false; |
| 91 | } |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 92 | } |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Schedule the idle job that will ping the mount service |
| 98 | */ |
| 99 | public static void scheduleIdlePass(Context context) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 100 | JobScheduler tm = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 101 | |
Jaegeuk Kim | 8975f78 | 2019-04-25 05:00:36 -0700 | [diff] [blame] | 102 | final long today3AM = offsetFromTodayMidnight(0, 3).getTimeInMillis(); |
| 103 | final long today4AM = offsetFromTodayMidnight(0, 4).getTimeInMillis(); |
| 104 | final long tomorrow3AM = offsetFromTodayMidnight(1, 3).getTimeInMillis(); |
| 105 | |
| 106 | long nextScheduleTime; |
| 107 | if (System.currentTimeMillis() > today3AM && System.currentTimeMillis() < today4AM) { |
| 108 | nextScheduleTime = TimeUnit.SECONDS.toMillis(10); |
| 109 | } else { |
| 110 | nextScheduleTime = tomorrow3AM - System.currentTimeMillis(); // 3AM tomorrow |
| 111 | } |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 112 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 113 | JobInfo.Builder builder = new JobInfo.Builder(MOUNT_JOB_ID, sIdleService); |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 114 | builder.setRequiresDeviceIdle(true); |
Jaegeuk Kim | 8975f78 | 2019-04-25 05:00:36 -0700 | [diff] [blame] | 115 | builder.setRequiresBatteryNotLow(true); |
| 116 | builder.setMinimumLatency(nextScheduleTime); |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 117 | tm.schedule(builder.build()); |
| 118 | } |
| 119 | |
Jaegeuk Kim | 8975f78 | 2019-04-25 05:00:36 -0700 | [diff] [blame] | 120 | private static Calendar offsetFromTodayMidnight(int nDays, int nHours) { |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 121 | Calendar calendar = Calendar.getInstance(); |
| 122 | calendar.setTimeInMillis(System.currentTimeMillis()); |
Jaegeuk Kim | 8975f78 | 2019-04-25 05:00:36 -0700 | [diff] [blame] | 123 | calendar.set(Calendar.HOUR_OF_DAY, nHours); |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 124 | calendar.set(Calendar.MINUTE, 0); |
| 125 | calendar.set(Calendar.SECOND, 0); |
| 126 | calendar.set(Calendar.MILLISECOND, 0); |
Jaegeuk Kim | 8975f78 | 2019-04-25 05:00:36 -0700 | [diff] [blame] | 127 | calendar.add(Calendar.DAY_OF_MONTH, nDays); |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 128 | return calendar; |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 129 | } |
| 130 | } |