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