1 /*
2  * Copyright 2017 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.display;
18 
19 
20 import android.app.job.JobInfo;
21 import android.app.job.JobParameters;
22 import android.app.job.JobScheduler;
23 import android.app.job.JobService;
24 import android.content.ComponentName;
25 import android.content.Context;
26 import android.hardware.display.DisplayManagerInternal;
27 import android.util.Slog;
28 
29 import com.android.server.LocalServices;
30 
31 import java.util.concurrent.TimeUnit;
32 
33 /**
34  * JobService used to persists brightness slider events when the device
35  * is idle and charging.
36  */
37 public class BrightnessIdleJob extends JobService {
38 
39     // Must be unique within the system server uid.
40     private static final int JOB_ID = 3923512;
41 
scheduleJob(Context context)42     public static void scheduleJob(Context context) {
43         JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
44 
45         JobInfo pending = jobScheduler.getPendingJob(JOB_ID);
46         JobInfo jobInfo =
47                 new JobInfo.Builder(JOB_ID, new ComponentName(context, BrightnessIdleJob.class))
48                         .setRequiresDeviceIdle(true)
49                         .setRequiresCharging(true)
50                         .setPeriodic(TimeUnit.HOURS.toMillis(24)).build();
51 
52         if (pending != null && !pending.equals(jobInfo)) {
53             jobScheduler.cancel(JOB_ID);
54             pending = null;
55         }
56 
57         if (pending == null) {
58             jobScheduler.schedule(jobInfo);
59         }
60     }
61 
cancelJob(Context context)62     public static void cancelJob(Context context) {
63         JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
64         jobScheduler.cancel(JOB_ID);
65     }
66 
67     @Override
onStartJob(JobParameters params)68     public boolean onStartJob(JobParameters params) {
69         if (BrightnessTracker.DEBUG) {
70             Slog.d(BrightnessTracker.TAG, "Scheduled write of brightness events");
71         }
72         DisplayManagerInternal dmi = LocalServices.getService(DisplayManagerInternal.class);
73         dmi.persistBrightnessTrackerState();
74         return false;
75     }
76 
77     @Override
onStopJob(JobParameters params)78     public boolean onStopJob(JobParameters params) {
79         return false;
80     }
81 }