Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -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 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 17 | package com.android.server.job.controllers; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 18 | |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 19 | import static com.android.server.job.JobSchedulerService.sElapsedRealtimeClock; |
| 20 | |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 21 | import android.app.AlarmManager; |
Christopher Tate | 2fcbe21 | 2015-12-09 16:00:37 -0800 | [diff] [blame] | 22 | import android.app.AlarmManager.OnAlarmListener; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 23 | import android.content.Context; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 24 | import android.os.UserHandle; |
| 25 | import android.os.WorkSource; |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 26 | import android.util.Slog; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 27 | import android.util.TimeUtils; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 28 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 29 | import com.android.server.job.JobSchedulerService; |
| 30 | import com.android.server.job.StateChangedListener; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 31 | |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 32 | import java.io.PrintWriter; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 33 | import java.util.Iterator; |
| 34 | import java.util.LinkedList; |
| 35 | import java.util.List; |
| 36 | import java.util.ListIterator; |
| 37 | |
| 38 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 39 | * This class sets an alarm for the next expiring job, and determines whether a job's minimum |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 40 | * delay has been satisfied. |
| 41 | */ |
Dianne Hackborn | 6466c1c | 2017-06-13 10:33:19 -0700 | [diff] [blame] | 42 | public final class TimeController extends StateController { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 43 | private static final String TAG = "JobScheduler.Time"; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 44 | |
Christopher Tate | 2fcbe21 | 2015-12-09 16:00:37 -0800 | [diff] [blame] | 45 | /** Deadline alarm tag for logging purposes */ |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 46 | private final String DEADLINE_TAG = "*job.deadline*"; |
Christopher Tate | 2fcbe21 | 2015-12-09 16:00:37 -0800 | [diff] [blame] | 47 | /** Delay alarm tag for logging purposes */ |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 48 | private final String DELAY_TAG = "*job.delay*"; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 49 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 50 | private long mNextJobExpiredElapsedMillis; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 51 | private long mNextDelayExpiredElapsedMillis; |
| 52 | |
| 53 | private AlarmManager mAlarmService = null; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 54 | /** List of tracked jobs, sorted asc. by deadline */ |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 55 | private final List<JobStatus> mTrackedJobs = new LinkedList<>(); |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 56 | /** Singleton. */ |
| 57 | private static TimeController mSingleton; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 58 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 59 | public static synchronized TimeController get(JobSchedulerService jms) { |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 60 | if (mSingleton == null) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 61 | mSingleton = new TimeController(jms, jms.getContext(), jms.getLock()); |
Matthew Williams | 9b9244b6 | 2014-05-14 11:06:04 -0700 | [diff] [blame] | 62 | } |
| 63 | return mSingleton; |
| 64 | } |
| 65 | |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 66 | private TimeController(StateChangedListener stateChangedListener, Context context, |
| 67 | Object lock) { |
| 68 | super(stateChangedListener, context, lock); |
Christopher Tate | 2fcbe21 | 2015-12-09 16:00:37 -0800 | [diff] [blame] | 69 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 70 | mNextJobExpiredElapsedMillis = Long.MAX_VALUE; |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 71 | mNextDelayExpiredElapsedMillis = Long.MAX_VALUE; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 75 | * Check if the job has a timing constraint, and if so determine where to insert it in our |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 76 | * list. |
| 77 | */ |
| 78 | @Override |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 79 | public void maybeStartTrackingJobLocked(JobStatus job, JobStatus lastJob) { |
| 80 | if (job.hasTimingDelayConstraint() || job.hasDeadlineConstraint()) { |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 81 | maybeStopTrackingJobLocked(job, null, false); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 82 | |
| 83 | // First: check the constraints now, because if they are already satisfied |
| 84 | // then there is no need to track it. This gives us a fast path for a common |
| 85 | // pattern of having a job with a 0 deadline constraint ("run immediately"). |
| 86 | // Unlike most controllers, once one of our constraints has been satisfied, it |
| 87 | // will never be unsatisfied (our time base can not go backwards). |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 88 | final long nowElapsedMillis = sElapsedRealtimeClock.millis(); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 89 | if (job.hasDeadlineConstraint() && evaluateDeadlineConstraint(job, nowElapsedMillis)) { |
| 90 | return; |
| 91 | } else if (job.hasTimingDelayConstraint() && evaluateTimingDelayConstraint(job, |
| 92 | nowElapsedMillis)) { |
| 93 | return; |
| 94 | } |
| 95 | |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 96 | boolean isInsert = false; |
| 97 | ListIterator<JobStatus> it = mTrackedJobs.listIterator(mTrackedJobs.size()); |
| 98 | while (it.hasPrevious()) { |
| 99 | JobStatus ts = it.previous(); |
| 100 | if (ts.getLatestRunTimeElapsed() < job.getLatestRunTimeElapsed()) { |
| 101 | // Insert |
| 102 | isInsert = true; |
| 103 | break; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 104 | } |
| 105 | } |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 106 | if (isInsert) { |
| 107 | it.next(); |
| 108 | } |
| 109 | it.add(job); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 110 | job.setTrackingController(JobStatus.TRACKING_TIME); |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 111 | maybeUpdateAlarmsLocked( |
| 112 | job.hasTimingDelayConstraint() ? job.getEarliestRunTime() : Long.MAX_VALUE, |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 113 | job.hasDeadlineConstraint() ? job.getLatestRunTimeElapsed() : Long.MAX_VALUE, |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 114 | new WorkSource(job.getSourceUid(), job.getSourcePackageName())); |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 115 | } |
| 116 | } |
| 117 | |
| 118 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 119 | * When we stop tracking a job, we only need to update our alarms if the job we're no longer |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 120 | * tracking was the one our alarms were based off of. |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 121 | */ |
| 122 | @Override |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 123 | public void maybeStopTrackingJobLocked(JobStatus job, JobStatus incomingJob, |
| 124 | boolean forUpdate) { |
| 125 | if (job.clearTrackingController(JobStatus.TRACKING_TIME)) { |
| 126 | if (mTrackedJobs.remove(job)) { |
| 127 | checkExpiredDelaysAndResetAlarm(); |
| 128 | checkExpiredDeadlinesAndResetAlarm(); |
| 129 | } |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 134 | * Determines whether this controller can stop tracking the given job. |
| 135 | * The controller is no longer interested in a job once its time constraint is satisfied, and |
| 136 | * the job's deadline is fulfilled - unlike other controllers a time constraint can't toggle |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 137 | * back and forth. |
| 138 | */ |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 139 | private boolean canStopTrackingJobLocked(JobStatus job) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 140 | return (!job.hasTimingDelayConstraint() || |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 141 | (job.satisfiedConstraints&JobStatus.CONSTRAINT_TIMING_DELAY) != 0) && |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 142 | (!job.hasDeadlineConstraint() || |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 143 | (job.satisfiedConstraints&JobStatus.CONSTRAINT_DEADLINE) != 0); |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 146 | private void ensureAlarmServiceLocked() { |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 147 | if (mAlarmService == null) { |
| 148 | mAlarmService = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 153 | * Checks list of jobs for ones that have an expired deadline, sending them to the JobScheduler |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 154 | * if so, removing them from this list, and updating the alarm for the next expiry time. |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 155 | */ |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 156 | private void checkExpiredDeadlinesAndResetAlarm() { |
| 157 | synchronized (mLock) { |
| 158 | long nextExpiryTime = Long.MAX_VALUE; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 159 | int nextExpiryUid = 0; |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 160 | String nextExpiryPackageName = null; |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 161 | final long nowElapsedMillis = sElapsedRealtimeClock.millis(); |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 162 | |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 163 | Iterator<JobStatus> it = mTrackedJobs.iterator(); |
| 164 | while (it.hasNext()) { |
| 165 | JobStatus job = it.next(); |
| 166 | if (!job.hasDeadlineConstraint()) { |
| 167 | continue; |
| 168 | } |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 169 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 170 | if (evaluateDeadlineConstraint(job, nowElapsedMillis)) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 171 | mStateChangedListener.onRunJobNow(job); |
| 172 | it.remove(); |
| 173 | } else { // Sorted by expiry time, so take the next one and stop. |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 174 | nextExpiryTime = job.getLatestRunTimeElapsed(); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 175 | nextExpiryUid = job.getSourceUid(); |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 176 | nextExpiryPackageName = job.getSourcePackageName(); |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 177 | break; |
| 178 | } |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 179 | } |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 180 | setDeadlineExpiredAlarmLocked(nextExpiryTime, nextExpiryPackageName != null |
| 181 | ? new WorkSource(nextExpiryUid, nextExpiryPackageName) |
| 182 | : new WorkSource(nextExpiryUid)); |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 183 | } |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 184 | } |
| 185 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 186 | private boolean evaluateDeadlineConstraint(JobStatus job, long nowElapsedMillis) { |
| 187 | final long jobDeadline = job.getLatestRunTimeElapsed(); |
| 188 | |
| 189 | if (jobDeadline <= nowElapsedMillis) { |
| 190 | if (job.hasTimingDelayConstraint()) { |
| 191 | job.setTimingDelayConstraintSatisfied(true); |
| 192 | } |
| 193 | job.setDeadlineConstraintSatisfied(true); |
| 194 | return true; |
| 195 | } |
| 196 | return false; |
| 197 | } |
| 198 | |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 199 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 200 | * Handles alarm that notifies us that a job's delay has expired. Iterates through the list of |
| 201 | * tracked jobs and marks them as ready as appropriate. |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 202 | */ |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 203 | private void checkExpiredDelaysAndResetAlarm() { |
| 204 | synchronized (mLock) { |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 205 | final long nowElapsedMillis = sElapsedRealtimeClock.millis(); |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 206 | long nextDelayTime = Long.MAX_VALUE; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 207 | int nextDelayUid = 0; |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 208 | String nextDelayPackageName = null; |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 209 | boolean ready = false; |
| 210 | Iterator<JobStatus> it = mTrackedJobs.iterator(); |
| 211 | while (it.hasNext()) { |
| 212 | final JobStatus job = it.next(); |
| 213 | if (!job.hasTimingDelayConstraint()) { |
| 214 | continue; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 215 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 216 | if (evaluateTimingDelayConstraint(job, nowElapsedMillis)) { |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 217 | if (canStopTrackingJobLocked(job)) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 218 | it.remove(); |
| 219 | } |
| 220 | if (job.isReady()) { |
| 221 | ready = true; |
| 222 | } |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 223 | } else if (!job.isConstraintSatisfied(JobStatus.CONSTRAINT_TIMING_DELAY)) { |
| 224 | // If this job still doesn't have its delay constraint satisfied, |
| 225 | // then see if it is the next upcoming delay time for the alarm. |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 226 | final long jobDelayTime = job.getEarliestRunTime(); |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 227 | if (nextDelayTime > jobDelayTime) { |
| 228 | nextDelayTime = jobDelayTime; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 229 | nextDelayUid = job.getSourceUid(); |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 230 | nextDelayPackageName = job.getSourcePackageName(); |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 231 | } |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 232 | } |
| 233 | } |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 234 | if (ready) { |
| 235 | mStateChangedListener.onControllerStateChanged(); |
| 236 | } |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 237 | setDelayExpiredAlarmLocked(nextDelayTime, nextDelayPackageName != null |
| 238 | ? new WorkSource(nextDelayUid, nextDelayPackageName) |
| 239 | : new WorkSource(nextDelayUid)); |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 240 | } |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 243 | private boolean evaluateTimingDelayConstraint(JobStatus job, long nowElapsedMillis) { |
| 244 | final long jobDelayTime = job.getEarliestRunTime(); |
| 245 | if (jobDelayTime <= nowElapsedMillis) { |
| 246 | job.setTimingDelayConstraintSatisfied(true); |
| 247 | return true; |
| 248 | } |
| 249 | return false; |
| 250 | } |
| 251 | |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 252 | private void maybeUpdateAlarmsLocked(long delayExpiredElapsed, long deadlineExpiredElapsed, |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 253 | WorkSource ws) { |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 254 | if (delayExpiredElapsed < mNextDelayExpiredElapsedMillis) { |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 255 | setDelayExpiredAlarmLocked(delayExpiredElapsed, ws); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 256 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 257 | if (deadlineExpiredElapsed < mNextJobExpiredElapsedMillis) { |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 258 | setDeadlineExpiredAlarmLocked(deadlineExpiredElapsed, ws); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
| 262 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 263 | * Set an alarm with the {@link android.app.AlarmManager} for the next time at which a job's |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 264 | * delay will expire. |
Shreyas Basarge | 4b7e339c | 2016-01-20 16:25:07 +0000 | [diff] [blame] | 265 | * This alarm <b>will</b> wake up the phone. |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 266 | */ |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 267 | private void setDelayExpiredAlarmLocked(long alarmTimeElapsedMillis, WorkSource ws) { |
Matthew Williams | a9f993c | 2014-09-11 11:31:05 -0700 | [diff] [blame] | 268 | alarmTimeElapsedMillis = maybeAdjustAlarmTime(alarmTimeElapsedMillis); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 269 | mNextDelayExpiredElapsedMillis = alarmTimeElapsedMillis; |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 270 | updateAlarmWithListenerLocked(DELAY_TAG, mNextDelayExpiredListener, |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 271 | mNextDelayExpiredElapsedMillis, ws); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 275 | * Set an alarm with the {@link android.app.AlarmManager} for the next time at which a job's |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 276 | * deadline will expire. |
| 277 | * This alarm <b>will</b> wake up the phone. |
| 278 | */ |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 279 | private void setDeadlineExpiredAlarmLocked(long alarmTimeElapsedMillis, WorkSource ws) { |
Matthew Williams | a9f993c | 2014-09-11 11:31:05 -0700 | [diff] [blame] | 280 | alarmTimeElapsedMillis = maybeAdjustAlarmTime(alarmTimeElapsedMillis); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 281 | mNextJobExpiredElapsedMillis = alarmTimeElapsedMillis; |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 282 | updateAlarmWithListenerLocked(DEADLINE_TAG, mDeadlineExpiredListener, |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 283 | mNextJobExpiredElapsedMillis, ws); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Matthew Williams | a9f993c | 2014-09-11 11:31:05 -0700 | [diff] [blame] | 286 | private long maybeAdjustAlarmTime(long proposedAlarmTimeElapsedMillis) { |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 287 | final long earliestWakeupTimeElapsed = sElapsedRealtimeClock.millis(); |
Matthew Williams | a9f993c | 2014-09-11 11:31:05 -0700 | [diff] [blame] | 288 | if (proposedAlarmTimeElapsedMillis < earliestWakeupTimeElapsed) { |
| 289 | return earliestWakeupTimeElapsed; |
| 290 | } |
| 291 | return proposedAlarmTimeElapsedMillis; |
| 292 | } |
| 293 | |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 294 | private void updateAlarmWithListenerLocked(String tag, OnAlarmListener listener, |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 295 | long alarmTimeElapsed, WorkSource ws) { |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 296 | ensureAlarmServiceLocked(); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 297 | if (alarmTimeElapsed == Long.MAX_VALUE) { |
Christopher Tate | 2fcbe21 | 2015-12-09 16:00:37 -0800 | [diff] [blame] | 298 | mAlarmService.cancel(listener); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 299 | } else { |
| 300 | if (DEBUG) { |
Christopher Tate | 2fcbe21 | 2015-12-09 16:00:37 -0800 | [diff] [blame] | 301 | Slog.d(TAG, "Setting " + tag + " for: " + alarmTimeElapsed); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 302 | } |
Shreyas Basarge | 4b7e339c | 2016-01-20 16:25:07 +0000 | [diff] [blame] | 303 | mAlarmService.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, alarmTimeElapsed, |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 304 | AlarmManager.WINDOW_HEURISTIC, 0, tag, listener, null, ws); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 305 | } |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Christopher Tate | 2fcbe21 | 2015-12-09 16:00:37 -0800 | [diff] [blame] | 308 | // Job/delay expiration alarm handling |
| 309 | |
| 310 | private final OnAlarmListener mDeadlineExpiredListener = new OnAlarmListener() { |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 311 | @Override |
Christopher Tate | 2fcbe21 | 2015-12-09 16:00:37 -0800 | [diff] [blame] | 312 | public void onAlarm() { |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 313 | if (DEBUG) { |
Christopher Tate | 2fcbe21 | 2015-12-09 16:00:37 -0800 | [diff] [blame] | 314 | Slog.d(TAG, "Deadline-expired alarm fired"); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 315 | } |
Christopher Tate | 2fcbe21 | 2015-12-09 16:00:37 -0800 | [diff] [blame] | 316 | checkExpiredDeadlinesAndResetAlarm(); |
| 317 | } |
| 318 | }; |
| 319 | |
| 320 | private final OnAlarmListener mNextDelayExpiredListener = new OnAlarmListener() { |
| 321 | @Override |
| 322 | public void onAlarm() { |
| 323 | if (DEBUG) { |
| 324 | Slog.d(TAG, "Delay-expired alarm fired"); |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 325 | } |
Christopher Tate | 2fcbe21 | 2015-12-09 16:00:37 -0800 | [diff] [blame] | 326 | checkExpiredDelaysAndResetAlarm(); |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 327 | } |
| 328 | }; |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 329 | |
| 330 | @Override |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 331 | public void dumpControllerStateLocked(PrintWriter pw, int filterUid) { |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 332 | final long nowElapsed = sElapsedRealtimeClock.millis(); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 333 | pw.print("Alarms: now="); |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 334 | pw.print(sElapsedRealtimeClock.millis()); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 335 | pw.println(); |
| 336 | pw.print("Next delay alarm in "); |
| 337 | TimeUtils.formatDuration(mNextDelayExpiredElapsedMillis, nowElapsed, pw); |
| 338 | pw.println(); |
| 339 | pw.print("Next deadline alarm in "); |
| 340 | TimeUtils.formatDuration(mNextJobExpiredElapsedMillis, nowElapsed, pw); |
| 341 | pw.println(); |
| 342 | pw.print("Tracking "); |
| 343 | pw.print(mTrackedJobs.size()); |
| 344 | pw.println(":"); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 345 | for (JobStatus ts : mTrackedJobs) { |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 346 | if (!ts.shouldDump(filterUid)) { |
| 347 | continue; |
| 348 | } |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 349 | pw.print(" #"); |
| 350 | ts.printUniqueId(pw); |
| 351 | pw.print(" from "); |
| 352 | UserHandle.formatUid(pw, ts.getSourceUid()); |
| 353 | pw.print(": Delay="); |
| 354 | if (ts.hasTimingDelayConstraint()) { |
| 355 | TimeUtils.formatDuration(ts.getEarliestRunTime(), nowElapsed, pw); |
| 356 | } else { |
| 357 | pw.print("N/A"); |
| 358 | } |
| 359 | pw.print(", Deadline="); |
| 360 | if (ts.hasDeadlineConstraint()) { |
| 361 | TimeUtils.formatDuration(ts.getLatestRunTimeElapsed(), nowElapsed, pw); |
| 362 | } else { |
| 363 | pw.print("N/A"); |
| 364 | } |
| 365 | pw.println(); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 366 | } |
| 367 | } |
| 368 | } |