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 19 import android.app.job.JobInfo; 20 import android.app.job.JobParameters; 21 import android.app.job.JobScheduler; 22 import android.app.job.JobService; 23 import android.content.ComponentName; 24 import android.content.Context; 25 import android.util.Slog; 26 27 import java.util.concurrent.TimeUnit; 28 29 public class SmartStorageMaintIdler extends JobService { 30 private static final String TAG = "SmartStorageMaintIdler"; 31 32 private static final ComponentName SMART_STORAGE_MAINT_SERVICE = 33 new ComponentName("android", SmartStorageMaintIdler.class.getName()); 34 35 private static final int SMART_MAINT_JOB_ID = 2808; 36 37 private boolean mStarted; 38 private JobParameters mJobParams; 39 private final Runnable mFinishCallback = new Runnable() { 40 @Override 41 public void run() { 42 Slog.i(TAG, "Got smart storage maintenance service completion callback"); 43 if (mStarted) { 44 jobFinished(mJobParams, false); 45 mStarted = false; 46 } 47 // ... and try again in a next period 48 scheduleSmartIdlePass(SmartStorageMaintIdler.this, 49 StorageManagerService.sSmartIdleMaintPeriod); 50 } 51 }; 52 53 @Override onStartJob(JobParameters params)54 public boolean onStartJob(JobParameters params) { 55 mJobParams = params; 56 StorageManagerService ms = StorageManagerService.sSelf; 57 if (ms != null) { 58 mStarted = true; 59 ms.runSmartIdleMaint(mFinishCallback); 60 } 61 return ms != null; 62 } 63 64 @Override onStopJob(JobParameters params)65 public boolean onStopJob(JobParameters params) { 66 mStarted = false; 67 return false; 68 } 69 70 /** 71 * Schedule the smart storage idle maintenance job 72 */ scheduleSmartIdlePass(Context context, int nMinutes)73 public static void scheduleSmartIdlePass(Context context, int nMinutes) { 74 StorageManagerService ms = StorageManagerService.sSelf; 75 if ((ms == null) || ms.isPassedLifetimeThresh()) { 76 return; 77 } 78 79 JobScheduler tm = context.getSystemService(JobScheduler.class); 80 81 long nextScheduleTime = TimeUnit.MINUTES.toMillis(nMinutes); 82 83 JobInfo.Builder builder = new JobInfo.Builder(SMART_MAINT_JOB_ID, 84 SMART_STORAGE_MAINT_SERVICE); 85 86 builder.setMinimumLatency(nextScheduleTime); 87 tm.schedule(builder.build()); 88 } 89 } 90