blob: a7767a4fbd660576d7801787a5c347c079bd1361 [file] [log] [blame]
Neil Fullercd1a1092017-09-13 21:59:59 +01001/*
2 * Copyright (C) 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
17package com.android.server.timezone;
18
19import com.android.server.LocalServices;
20
21import android.app.job.JobInfo;
22import android.app.job.JobParameters;
23import android.app.job.JobScheduler;
24import android.app.job.JobService;
25import android.content.ComponentName;
26import android.content.Context;
27import android.util.Slog;
28
29/**
30 * A JobService used to trigger time zone rules update work when a device falls idle.
31 */
32public final class TimeZoneUpdateIdler extends JobService {
33
34 private static final String TAG = "timezone.TimeZoneUpdateIdler";
35
36 /** The static job ID used to handle on-idle work. */
37 // Must be unique within UID (system service)
38 private static final int TIME_ZONE_UPDATE_IDLE_JOB_ID = 27042305;
39
40 @Override
41 public boolean onStartJob(JobParameters params) {
42 RulesManagerService rulesManagerService =
43 LocalServices.getService(RulesManagerService.class);
44
45 Slog.d(TAG, "onStartJob() called");
46
47 // Note: notifyIdle() explicitly handles canceling / re-scheduling so no need to reschedule
48 // here.
49 rulesManagerService.notifyIdle();
50
51 // Everything is handled synchronously. We are done.
52 return false;
53 }
54
55 @Override
56 public boolean onStopJob(JobParameters params) {
57 // Reschedule if stopped unless it was cancelled due to unschedule().
58 boolean reschedule = params.getStopReason() != JobParameters.REASON_CANCELED;
59 Slog.d(TAG, "onStopJob() called: Reschedule=" + reschedule);
60 return reschedule;
61 }
62
63 /**
64 * Schedules the TimeZoneUpdateIdler job service to run once.
65 *
66 * @param context Context to use to get a job scheduler.
67 */
68 public static void schedule(Context context, long minimumDelayMillis) {
69 // Request that the JobScheduler tell us when the device falls idle.
70 JobScheduler jobScheduler =
71 (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
72
73 // The TimeZoneUpdateIdler will send an intent that will trigger the Receiver.
74 ComponentName idlerJobServiceName =
75 new ComponentName(context, TimeZoneUpdateIdler.class);
76
77 // We require the device is idle, but also that it is charging to be as non-invasive as
78 // we can.
79 JobInfo.Builder jobInfoBuilder =
80 new JobInfo.Builder(TIME_ZONE_UPDATE_IDLE_JOB_ID, idlerJobServiceName)
81 .setRequiresDeviceIdle(true)
82 .setRequiresCharging(true)
83 .setMinimumLatency(minimumDelayMillis);
84
85 Slog.d(TAG, "schedule() called: minimumDelayMillis=" + minimumDelayMillis);
86 jobScheduler.schedule(jobInfoBuilder.build());
87 }
88
89 /**
90 * Unschedules the TimeZoneUpdateIdler job service.
91 *
92 * @param context Context to use to get a job scheduler.
93 */
94 public static void unschedule(Context context) {
95 JobScheduler jobScheduler =
96 (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
97 Slog.d(TAG, "unschedule() called");
98 jobScheduler.cancel(TIME_ZONE_UPDATE_IDLE_JOB_ID);
99 }
100}