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; |
| 30 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 31 | public class MountServiceIdler extends JobService { |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 32 | private static final String TAG = "MountServiceIdler"; |
| 33 | |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 34 | private static ComponentName sIdleService = |
Christopher Tate | f8ad7a9 | 2014-06-17 15:07:01 -0700 | [diff] [blame] | 35 | new ComponentName("android", MountServiceIdler.class.getName()); |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 36 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 37 | private static int MOUNT_JOB_ID = 808; |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 38 | |
| 39 | private boolean mStarted; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 40 | private JobParameters mJobParams; |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 41 | private Runnable mFinishCallback = new Runnable() { |
| 42 | @Override |
| 43 | public void run() { |
| 44 | Slog.i(TAG, "Got mount service completion callback"); |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 45 | synchronized (mFinishCallback) { |
| 46 | if (mStarted) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 47 | jobFinished(mJobParams, false); |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 48 | mStarted = false; |
| 49 | } |
| 50 | } |
| 51 | // ... and try again tomorrow |
| 52 | scheduleIdlePass(MountServiceIdler.this); |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 53 | } |
| 54 | }; |
| 55 | |
| 56 | @Override |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 57 | public boolean onStartJob(JobParameters params) { |
Dianne Hackborn | 57a873f | 2014-12-04 13:58:46 -0800 | [diff] [blame] | 58 | // 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 Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 62 | ActivityManager.getService().performIdleMaintenance(); |
Dianne Hackborn | 57a873f | 2014-12-04 13:58:46 -0800 | [diff] [blame] | 63 | } catch (RemoteException e) { |
| 64 | } |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 65 | // 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 Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 69 | mJobParams = params; |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 70 | MountService ms = MountService.sSelf; |
| 71 | if (ms != null) { |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 72 | synchronized (mFinishCallback) { |
| 73 | mStarted = true; |
| 74 | } |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 75 | ms.runIdleMaintenance(mFinishCallback); |
| 76 | } |
| 77 | return ms != null; |
| 78 | } |
| 79 | |
| 80 | @Override |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 81 | public boolean onStopJob(JobParameters params) { |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 82 | // 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] | 83 | // 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] | 84 | // the callback from the mount service. |
| 85 | synchronized (mFinishCallback) { |
| 86 | mStarted = false; |
| 87 | } |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Schedule the idle job that will ping the mount service |
| 93 | */ |
| 94 | public static void scheduleIdlePass(Context context) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 95 | JobScheduler tm = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 96 | |
| 97 | Calendar calendar = tomorrowMidnight(); |
| 98 | final long timeToMidnight = calendar.getTimeInMillis() - System.currentTimeMillis(); |
| 99 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 100 | JobInfo.Builder builder = new JobInfo.Builder(MOUNT_JOB_ID, sIdleService); |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 101 | builder.setRequiresDeviceIdle(true); |
| 102 | builder.setRequiresCharging(true); |
| 103 | builder.setMinimumLatency(timeToMidnight); |
| 104 | tm.schedule(builder.build()); |
| 105 | } |
| 106 | |
| 107 | private static Calendar tomorrowMidnight() { |
| 108 | Calendar calendar = Calendar.getInstance(); |
| 109 | calendar.setTimeInMillis(System.currentTimeMillis()); |
Dianne Hackborn | 57a873f | 2014-12-04 13:58:46 -0800 | [diff] [blame] | 110 | calendar.set(Calendar.HOUR_OF_DAY, 3); |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 111 | calendar.set(Calendar.MINUTE, 0); |
| 112 | calendar.set(Calendar.SECOND, 0); |
| 113 | calendar.set(Calendar.MILLISECOND, 0); |
| 114 | calendar.add(Calendar.DAY_OF_MONTH, 1); |
| 115 | return calendar; |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 116 | } |
| 117 | } |