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 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 21 | import android.app.job.JobInfo; |
| 22 | import android.app.job.JobParameters; |
| 23 | import android.app.job.JobScheduler; |
| 24 | import android.app.job.JobService; |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 25 | import android.content.ComponentName; |
| 26 | import android.content.Context; |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 27 | import android.util.Slog; |
| 28 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 29 | public class MountServiceIdler extends JobService { |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 30 | private static final String TAG = "MountServiceIdler"; |
| 31 | |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 32 | private static ComponentName sIdleService = |
Christopher Tate | f8ad7a9 | 2014-06-17 15:07:01 -0700 | [diff] [blame] | 33 | new ComponentName("android", MountServiceIdler.class.getName()); |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 34 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 35 | private static int MOUNT_JOB_ID = 808; |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 36 | |
| 37 | private boolean mStarted; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 38 | private JobParameters mJobParams; |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 39 | private Runnable mFinishCallback = new Runnable() { |
| 40 | @Override |
| 41 | public void run() { |
| 42 | Slog.i(TAG, "Got mount service completion callback"); |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 43 | synchronized (mFinishCallback) { |
| 44 | if (mStarted) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 45 | jobFinished(mJobParams, false); |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 46 | mStarted = false; |
| 47 | } |
| 48 | } |
| 49 | // ... and try again tomorrow |
| 50 | scheduleIdlePass(MountServiceIdler.this); |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 51 | } |
| 52 | }; |
| 53 | |
| 54 | @Override |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 55 | public boolean onStartJob(JobParameters params) { |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 56 | // 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 Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 60 | mJobParams = params; |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 61 | MountService ms = MountService.sSelf; |
| 62 | if (ms != null) { |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 63 | synchronized (mFinishCallback) { |
| 64 | mStarted = true; |
| 65 | } |
Christopher Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 66 | ms.runIdleMaintenance(mFinishCallback); |
| 67 | } |
| 68 | return ms != null; |
| 69 | } |
| 70 | |
| 71 | @Override |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 72 | public boolean onStopJob(JobParameters params) { |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 73 | // 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] | 74 | // 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] | 75 | // 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 Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 86 | JobScheduler tm = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE); |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 87 | |
| 88 | Calendar calendar = tomorrowMidnight(); |
| 89 | final long timeToMidnight = calendar.getTimeInMillis() - System.currentTimeMillis(); |
| 90 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 91 | JobInfo.Builder builder = new JobInfo.Builder(MOUNT_JOB_ID, sIdleService); |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 92 | 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()); |
Christopher Tate | f6f8cca | 2014-12-02 10:55:09 -0800 | [diff] [blame] | 101 | calendar.set(Calendar.HOUR_OF_DAY, 0); |
Christopher Tate | 115afda | 2014-06-06 19:06:26 -0700 | [diff] [blame] | 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 Tate | d417d62 | 2013-08-19 16:14:25 -0700 | [diff] [blame] | 107 | } |
| 108 | } |