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 | |
Narayan Kamath | 8d82825 | 2018-01-11 15:22:37 +0000 | [diff] [blame] | 21 | import android.annotation.Nullable; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 22 | import android.app.AlarmManager; |
Christopher Tate | 2fcbe21 | 2015-12-09 16:00:37 -0800 | [diff] [blame] | 23 | import android.app.AlarmManager.OnAlarmListener; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 24 | import android.content.Context; |
Narayan Kamath | 8d82825 | 2018-01-11 15:22:37 +0000 | [diff] [blame] | 25 | import android.os.Process; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 26 | import android.os.UserHandle; |
| 27 | import android.os.WorkSource; |
Jeff Sharkey | 01bb530 | 2018-02-21 20:12:40 -0700 | [diff] [blame] | 28 | import android.util.Log; |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 29 | import android.util.Slog; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 30 | import android.util.TimeUtils; |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 31 | import android.util.proto.ProtoOutputStream; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 32 | |
Jeff Sharkey | fee8c7b | 2018-02-21 22:18:45 -0700 | [diff] [blame] | 33 | import com.android.internal.util.IndentingPrintWriter; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 34 | import com.android.server.job.JobSchedulerService; |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 35 | import com.android.server.job.StateControllerProto; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 36 | |
| 37 | import java.util.Iterator; |
| 38 | import java.util.LinkedList; |
| 39 | import java.util.List; |
| 40 | import java.util.ListIterator; |
Jeff Sharkey | fee8c7b | 2018-02-21 22:18:45 -0700 | [diff] [blame] | 41 | import java.util.function.Predicate; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 42 | |
| 43 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 44 | * 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] | 45 | * delay has been satisfied. |
| 46 | */ |
Dianne Hackborn | 6466c1c | 2017-06-13 10:33:19 -0700 | [diff] [blame] | 47 | public final class TimeController extends StateController { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 48 | private static final String TAG = "JobScheduler.Time"; |
Jeff Sharkey | 01bb530 | 2018-02-21 20:12:40 -0700 | [diff] [blame] | 49 | private static final boolean DEBUG = JobSchedulerService.DEBUG |
| 50 | || Log.isLoggable(TAG, Log.DEBUG); |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 51 | |
Christopher Tate | 2fcbe21 | 2015-12-09 16:00:37 -0800 | [diff] [blame] | 52 | /** Deadline alarm tag for logging purposes */ |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 53 | private final String DEADLINE_TAG = "*job.deadline*"; |
Christopher Tate | 2fcbe21 | 2015-12-09 16:00:37 -0800 | [diff] [blame] | 54 | /** Delay alarm tag for logging purposes */ |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 55 | private final String DELAY_TAG = "*job.delay*"; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 56 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 57 | private long mNextJobExpiredElapsedMillis; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 58 | private long mNextDelayExpiredElapsedMillis; |
| 59 | |
Narayan Kamath | 8d82825 | 2018-01-11 15:22:37 +0000 | [diff] [blame] | 60 | private final boolean mChainedAttributionEnabled; |
| 61 | |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 62 | private AlarmManager mAlarmService = null; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 63 | /** List of tracked jobs, sorted asc. by deadline */ |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 64 | private final List<JobStatus> mTrackedJobs = new LinkedList<>(); |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 65 | |
Jeff Sharkey | ac2e8ef | 2018-02-22 16:06:44 -0700 | [diff] [blame^] | 66 | public TimeController(JobSchedulerService service) { |
| 67 | super(service); |
Christopher Tate | 2fcbe21 | 2015-12-09 16:00:37 -0800 | [diff] [blame] | 68 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 69 | mNextJobExpiredElapsedMillis = Long.MAX_VALUE; |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 70 | mNextDelayExpiredElapsedMillis = Long.MAX_VALUE; |
Jeff Sharkey | ac2e8ef | 2018-02-22 16:06:44 -0700 | [diff] [blame^] | 71 | mChainedAttributionEnabled = WorkSource.isChainedBatteryAttributionEnabled(mContext); |
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, |
Narayan Kamath | 8d82825 | 2018-01-11 15:22:37 +0000 | [diff] [blame] | 114 | deriveWorkSource(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 | } |
Narayan Kamath | 8d82825 | 2018-01-11 15:22:37 +0000 | [diff] [blame] | 180 | setDeadlineExpiredAlarmLocked(nextExpiryTime, |
| 181 | deriveWorkSource(nextExpiryUid, nextExpiryPackageName)); |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 182 | } |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 185 | private boolean evaluateDeadlineConstraint(JobStatus job, long nowElapsedMillis) { |
| 186 | final long jobDeadline = job.getLatestRunTimeElapsed(); |
| 187 | |
| 188 | if (jobDeadline <= nowElapsedMillis) { |
| 189 | if (job.hasTimingDelayConstraint()) { |
| 190 | job.setTimingDelayConstraintSatisfied(true); |
| 191 | } |
| 192 | job.setDeadlineConstraintSatisfied(true); |
| 193 | return true; |
| 194 | } |
| 195 | return false; |
| 196 | } |
| 197 | |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 198 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 199 | * Handles alarm that notifies us that a job's delay has expired. Iterates through the list of |
| 200 | * tracked jobs and marks them as ready as appropriate. |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 201 | */ |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 202 | private void checkExpiredDelaysAndResetAlarm() { |
| 203 | synchronized (mLock) { |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 204 | final long nowElapsedMillis = sElapsedRealtimeClock.millis(); |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 205 | long nextDelayTime = Long.MAX_VALUE; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 206 | int nextDelayUid = 0; |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 207 | String nextDelayPackageName = null; |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 208 | boolean ready = false; |
| 209 | Iterator<JobStatus> it = mTrackedJobs.iterator(); |
| 210 | while (it.hasNext()) { |
| 211 | final JobStatus job = it.next(); |
| 212 | if (!job.hasTimingDelayConstraint()) { |
| 213 | continue; |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 214 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 215 | if (evaluateTimingDelayConstraint(job, nowElapsedMillis)) { |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 216 | if (canStopTrackingJobLocked(job)) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 217 | it.remove(); |
| 218 | } |
| 219 | if (job.isReady()) { |
| 220 | ready = true; |
| 221 | } |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 222 | } else if (!job.isConstraintSatisfied(JobStatus.CONSTRAINT_TIMING_DELAY)) { |
| 223 | // If this job still doesn't have its delay constraint satisfied, |
| 224 | // 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] | 225 | final long jobDelayTime = job.getEarliestRunTime(); |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 226 | if (nextDelayTime > jobDelayTime) { |
| 227 | nextDelayTime = jobDelayTime; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 228 | nextDelayUid = job.getSourceUid(); |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 229 | nextDelayPackageName = job.getSourcePackageName(); |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 230 | } |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 231 | } |
| 232 | } |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 233 | if (ready) { |
| 234 | mStateChangedListener.onControllerStateChanged(); |
| 235 | } |
Narayan Kamath | 8d82825 | 2018-01-11 15:22:37 +0000 | [diff] [blame] | 236 | setDelayExpiredAlarmLocked(nextDelayTime, |
| 237 | deriveWorkSource(nextDelayUid, nextDelayPackageName)); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | private WorkSource deriveWorkSource(int uid, @Nullable String packageName) { |
| 242 | if (mChainedAttributionEnabled) { |
| 243 | WorkSource ws = new WorkSource(); |
| 244 | ws.createWorkChain() |
| 245 | .addNode(uid, packageName) |
| 246 | .addNode(Process.SYSTEM_UID, "JobScheduler"); |
| 247 | return ws; |
| 248 | } else { |
| 249 | return packageName == null ? new WorkSource(uid) : new WorkSource(uid, packageName); |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 250 | } |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 253 | private boolean evaluateTimingDelayConstraint(JobStatus job, long nowElapsedMillis) { |
| 254 | final long jobDelayTime = job.getEarliestRunTime(); |
| 255 | if (jobDelayTime <= nowElapsedMillis) { |
| 256 | job.setTimingDelayConstraintSatisfied(true); |
| 257 | return true; |
| 258 | } |
| 259 | return false; |
| 260 | } |
| 261 | |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 262 | private void maybeUpdateAlarmsLocked(long delayExpiredElapsed, long deadlineExpiredElapsed, |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 263 | WorkSource ws) { |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 264 | if (delayExpiredElapsed < mNextDelayExpiredElapsedMillis) { |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 265 | setDelayExpiredAlarmLocked(delayExpiredElapsed, ws); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 266 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 267 | if (deadlineExpiredElapsed < mNextJobExpiredElapsedMillis) { |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 268 | setDeadlineExpiredAlarmLocked(deadlineExpiredElapsed, ws); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
| 272 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 273 | * 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] | 274 | * delay will expire. |
Shreyas Basarge | 4b7e339c | 2016-01-20 16:25:07 +0000 | [diff] [blame] | 275 | * This alarm <b>will</b> wake up the phone. |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 276 | */ |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 277 | private void setDelayExpiredAlarmLocked(long alarmTimeElapsedMillis, WorkSource ws) { |
Matthew Williams | a9f993c | 2014-09-11 11:31:05 -0700 | [diff] [blame] | 278 | alarmTimeElapsedMillis = maybeAdjustAlarmTime(alarmTimeElapsedMillis); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 279 | mNextDelayExpiredElapsedMillis = alarmTimeElapsedMillis; |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 280 | updateAlarmWithListenerLocked(DELAY_TAG, mNextDelayExpiredListener, |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 281 | mNextDelayExpiredElapsedMillis, ws); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 285 | * 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] | 286 | * deadline will expire. |
| 287 | * This alarm <b>will</b> wake up the phone. |
| 288 | */ |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 289 | private void setDeadlineExpiredAlarmLocked(long alarmTimeElapsedMillis, WorkSource ws) { |
Matthew Williams | a9f993c | 2014-09-11 11:31:05 -0700 | [diff] [blame] | 290 | alarmTimeElapsedMillis = maybeAdjustAlarmTime(alarmTimeElapsedMillis); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 291 | mNextJobExpiredElapsedMillis = alarmTimeElapsedMillis; |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 292 | updateAlarmWithListenerLocked(DEADLINE_TAG, mDeadlineExpiredListener, |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 293 | mNextJobExpiredElapsedMillis, ws); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 294 | } |
| 295 | |
Matthew Williams | a9f993c | 2014-09-11 11:31:05 -0700 | [diff] [blame] | 296 | private long maybeAdjustAlarmTime(long proposedAlarmTimeElapsedMillis) { |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 297 | final long earliestWakeupTimeElapsed = sElapsedRealtimeClock.millis(); |
Matthew Williams | a9f993c | 2014-09-11 11:31:05 -0700 | [diff] [blame] | 298 | if (proposedAlarmTimeElapsedMillis < earliestWakeupTimeElapsed) { |
| 299 | return earliestWakeupTimeElapsed; |
| 300 | } |
| 301 | return proposedAlarmTimeElapsedMillis; |
| 302 | } |
| 303 | |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 304 | private void updateAlarmWithListenerLocked(String tag, OnAlarmListener listener, |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 305 | long alarmTimeElapsed, WorkSource ws) { |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 306 | ensureAlarmServiceLocked(); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 307 | if (alarmTimeElapsed == Long.MAX_VALUE) { |
Christopher Tate | 2fcbe21 | 2015-12-09 16:00:37 -0800 | [diff] [blame] | 308 | mAlarmService.cancel(listener); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 309 | } else { |
| 310 | if (DEBUG) { |
Christopher Tate | 2fcbe21 | 2015-12-09 16:00:37 -0800 | [diff] [blame] | 311 | Slog.d(TAG, "Setting " + tag + " for: " + alarmTimeElapsed); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 312 | } |
Shreyas Basarge | 4b7e339c | 2016-01-20 16:25:07 +0000 | [diff] [blame] | 313 | mAlarmService.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, alarmTimeElapsed, |
Adam Lesinski | c746b38 | 2017-10-13 14:31:08 -0700 | [diff] [blame] | 314 | AlarmManager.WINDOW_HEURISTIC, 0, tag, listener, null, ws); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 315 | } |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Christopher Tate | 2fcbe21 | 2015-12-09 16:00:37 -0800 | [diff] [blame] | 318 | // Job/delay expiration alarm handling |
| 319 | |
| 320 | private final OnAlarmListener mDeadlineExpiredListener = new OnAlarmListener() { |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 321 | @Override |
Christopher Tate | 2fcbe21 | 2015-12-09 16:00:37 -0800 | [diff] [blame] | 322 | public void onAlarm() { |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 323 | if (DEBUG) { |
Christopher Tate | 2fcbe21 | 2015-12-09 16:00:37 -0800 | [diff] [blame] | 324 | Slog.d(TAG, "Deadline-expired alarm fired"); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 325 | } |
Christopher Tate | 2fcbe21 | 2015-12-09 16:00:37 -0800 | [diff] [blame] | 326 | checkExpiredDeadlinesAndResetAlarm(); |
| 327 | } |
| 328 | }; |
| 329 | |
| 330 | private final OnAlarmListener mNextDelayExpiredListener = new OnAlarmListener() { |
| 331 | @Override |
| 332 | public void onAlarm() { |
| 333 | if (DEBUG) { |
| 334 | Slog.d(TAG, "Delay-expired alarm fired"); |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 335 | } |
Christopher Tate | 2fcbe21 | 2015-12-09 16:00:37 -0800 | [diff] [blame] | 336 | checkExpiredDelaysAndResetAlarm(); |
Matthew Williams | 6de79e2 | 2014-05-01 10:47:00 -0700 | [diff] [blame] | 337 | } |
| 338 | }; |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 339 | |
| 340 | @Override |
Jeff Sharkey | fee8c7b | 2018-02-21 22:18:45 -0700 | [diff] [blame] | 341 | public void dumpControllerStateLocked(IndentingPrintWriter pw, |
| 342 | Predicate<JobStatus> predicate) { |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 343 | final long nowElapsed = sElapsedRealtimeClock.millis(); |
Jeff Sharkey | fee8c7b | 2018-02-21 22:18:45 -0700 | [diff] [blame] | 344 | pw.println("Elapsed clock: " + nowElapsed); |
| 345 | |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 346 | pw.print("Next delay alarm in "); |
| 347 | TimeUtils.formatDuration(mNextDelayExpiredElapsedMillis, nowElapsed, pw); |
| 348 | pw.println(); |
| 349 | pw.print("Next deadline alarm in "); |
| 350 | TimeUtils.formatDuration(mNextJobExpiredElapsedMillis, nowElapsed, pw); |
| 351 | pw.println(); |
Jeff Sharkey | fee8c7b | 2018-02-21 22:18:45 -0700 | [diff] [blame] | 352 | pw.println(); |
| 353 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 354 | for (JobStatus ts : mTrackedJobs) { |
Jeff Sharkey | fee8c7b | 2018-02-21 22:18:45 -0700 | [diff] [blame] | 355 | if (!predicate.test(ts)) { |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 356 | continue; |
| 357 | } |
Jeff Sharkey | fee8c7b | 2018-02-21 22:18:45 -0700 | [diff] [blame] | 358 | pw.print("#"); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 359 | ts.printUniqueId(pw); |
| 360 | pw.print(" from "); |
| 361 | UserHandle.formatUid(pw, ts.getSourceUid()); |
| 362 | pw.print(": Delay="); |
| 363 | if (ts.hasTimingDelayConstraint()) { |
| 364 | TimeUtils.formatDuration(ts.getEarliestRunTime(), nowElapsed, pw); |
| 365 | } else { |
| 366 | pw.print("N/A"); |
| 367 | } |
| 368 | pw.print(", Deadline="); |
| 369 | if (ts.hasDeadlineConstraint()) { |
| 370 | TimeUtils.formatDuration(ts.getLatestRunTimeElapsed(), nowElapsed, pw); |
| 371 | } else { |
| 372 | pw.print("N/A"); |
| 373 | } |
| 374 | pw.println(); |
Matthew Williams | effacfa | 2014-06-05 20:56:40 -0700 | [diff] [blame] | 375 | } |
| 376 | } |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 377 | |
| 378 | @Override |
Jeff Sharkey | fee8c7b | 2018-02-21 22:18:45 -0700 | [diff] [blame] | 379 | public void dumpControllerStateLocked(ProtoOutputStream proto, long fieldId, |
| 380 | Predicate<JobStatus> predicate) { |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 381 | final long token = proto.start(fieldId); |
| 382 | final long mToken = proto.start(StateControllerProto.TIME); |
| 383 | |
| 384 | final long nowElapsed = sElapsedRealtimeClock.millis(); |
| 385 | proto.write(StateControllerProto.TimeController.NOW_ELAPSED_REALTIME, nowElapsed); |
| 386 | proto.write(StateControllerProto.TimeController.TIME_UNTIL_NEXT_DELAY_ALARM_MS, |
| 387 | mNextDelayExpiredElapsedMillis - nowElapsed); |
| 388 | proto.write(StateControllerProto.TimeController.TIME_UNTIL_NEXT_DEADLINE_ALARM_MS, |
| 389 | mNextJobExpiredElapsedMillis - nowElapsed); |
| 390 | |
| 391 | for (JobStatus ts : mTrackedJobs) { |
Jeff Sharkey | fee8c7b | 2018-02-21 22:18:45 -0700 | [diff] [blame] | 392 | if (!predicate.test(ts)) { |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 393 | continue; |
| 394 | } |
| 395 | final long tsToken = proto.start(StateControllerProto.TimeController.TRACKED_JOBS); |
| 396 | ts.writeToShortProto(proto, StateControllerProto.TimeController.TrackedJob.INFO); |
| 397 | |
| 398 | proto.write(StateControllerProto.TimeController.TrackedJob.HAS_TIMING_DELAY_CONSTRAINT, |
| 399 | ts.hasTimingDelayConstraint()); |
| 400 | proto.write(StateControllerProto.TimeController.TrackedJob.DELAY_TIME_REMAINING_MS, |
| 401 | ts.getEarliestRunTime() - nowElapsed); |
| 402 | |
| 403 | proto.write(StateControllerProto.TimeController.TrackedJob.HAS_DEADLINE_CONSTRAINT, |
| 404 | ts.hasDeadlineConstraint()); |
| 405 | proto.write(StateControllerProto.TimeController.TrackedJob.TIME_REMAINING_UNTIL_DEADLINE_MS, |
| 406 | ts.getLatestRunTimeElapsed() - nowElapsed); |
| 407 | |
| 408 | proto.end(tsToken); |
| 409 | } |
| 410 | |
| 411 | proto.end(mToken); |
| 412 | proto.end(token); |
| 413 | } |
| 414 | } |