blob: dbdce70dda0361b38799b9564ab2e91651446487 [file] [log] [blame]
Christopher Tate7060b042014-06-09 19:50:00 -07001/*
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
17package com.android.server.job.controllers;
18
Kweku Adamsbbb04592019-08-05 15:17:23 -070019import static com.android.server.job.JobSchedulerService.ACTIVE_INDEX;
Jeff Sharkeyd0fff2e2017-11-07 16:55:06 -070020import static com.android.server.job.JobSchedulerService.sElapsedRealtimeClock;
21
Shreyas Basarge968ac752016-01-11 23:09:26 +000022import android.app.AppGlobals;
Christopher Tate7060b042014-06-09 19:50:00 -070023import android.app.job.JobInfo;
Dianne Hackborn7da13d72017-04-04 17:17:35 -070024import android.app.job.JobWorkItem;
Dianne Hackborna47223f2017-03-30 13:49:13 -070025import android.content.ClipData;
Christopher Tate7060b042014-06-09 19:50:00 -070026import android.content.ComponentName;
Jeff Sharkey76a02412017-10-24 16:55:04 -060027import android.net.Network;
Dianne Hackborn1a30bd92016-01-11 11:05:00 -080028import android.net.Uri;
Shreyas Basarge968ac752016-01-11 23:09:26 +000029import android.os.RemoteException;
Christopher Tate7060b042014-06-09 19:50:00 -070030import android.os.UserHandle;
Makoto Onuki0e1ce972019-10-09 12:51:55 -070031import android.text.format.DateFormat;
Dianne Hackborn1a30bd92016-01-11 11:05:00 -080032import android.util.ArraySet;
Christopher Tate616541d2017-07-26 14:27:38 -070033import android.util.Pair;
Dianne Hackborna47223f2017-03-30 13:49:13 -070034import android.util.Slog;
Kweku Adams8845d012018-12-11 20:06:45 -080035import android.util.StatsLog;
Dianne Hackborn1a30bd92016-01-11 11:05:00 -080036import android.util.TimeUtils;
Kweku Adams85f2fbc2017-12-18 12:04:12 -080037import android.util.proto.ProtoOutputStream;
Christopher Tate7060b042014-06-09 19:50:00 -070038
Christopher Tatea732f012017-10-26 17:26:53 -070039import com.android.server.LocalServices;
Dianne Hackborn342e6032017-04-13 18:04:31 -070040import com.android.server.job.GrantedUriPermissions;
Christopher Tatea732f012017-10-26 17:26:53 -070041import com.android.server.job.JobSchedulerInternal;
Christopher Tate616541d2017-07-26 14:27:38 -070042import com.android.server.job.JobSchedulerService;
Kweku Adams8845d012018-12-11 20:06:45 -080043import com.android.server.job.JobServerProtoEnums;
Kweku Adams85f2fbc2017-12-18 12:04:12 -080044import com.android.server.job.JobStatusDumpProto;
45import com.android.server.job.JobStatusShortInfoProto;
Dianne Hackborn342e6032017-04-13 18:04:31 -070046
Christopher Tate7060b042014-06-09 19:50:00 -070047import java.io.PrintWriter;
Dianne Hackborn7da13d72017-04-04 17:17:35 -070048import java.util.ArrayList;
Dianne Hackborna47223f2017-03-30 13:49:13 -070049import java.util.Arrays;
Makoto Onuki15407842018-01-19 14:23:11 -080050import java.util.function.Predicate;
Christopher Tate7060b042014-06-09 19:50:00 -070051
52/**
53 * Uniquely identifies a job internally.
54 * Created from the public {@link android.app.job.JobInfo} object when it lands on the scheduler.
55 * Contains current state of the requirements of the job, as well as a function to evaluate
56 * whether it's ready to run.
57 * This object is shared among the various controllers - hence why the different fields are atomic.
58 * This isn't strictly necessary because each controller is only interested in a specific field,
59 * and the receivers that are listening for global state change will all run on the main looper,
60 * but we don't enforce that so this is safer.
Kweku Adamsd14ce4d2018-10-19 11:40:55 -070061 *
62 * Test: atest com.android.server.job.controllers.JobStatusTest
Christopher Tate7060b042014-06-09 19:50:00 -070063 * @hide
64 */
Dianne Hackbornb0001f62016-02-16 10:30:33 -080065public final class JobStatus {
Dianne Hackborna47223f2017-03-30 13:49:13 -070066 static final String TAG = "JobSchedulerService";
Christopher Tate616541d2017-07-26 14:27:38 -070067 static final boolean DEBUG = JobSchedulerService.DEBUG;
Dianne Hackborna47223f2017-03-30 13:49:13 -070068
Christopher Tate7060b042014-06-09 19:50:00 -070069 public static final long NO_LATEST_RUNTIME = Long.MAX_VALUE;
70 public static final long NO_EARLIEST_RUNTIME = 0L;
71
Kweku Adamsae9d6be2020-01-14 16:10:14 -080072 public static final int CONSTRAINT_CHARGING = JobInfo.CONSTRAINT_FLAG_CHARGING; // 1 < 0
73 public static final int CONSTRAINT_IDLE = JobInfo.CONSTRAINT_FLAG_DEVICE_IDLE; // 1 << 2
74 public static final int CONSTRAINT_BATTERY_NOT_LOW =
75 JobInfo.CONSTRAINT_FLAG_BATTERY_NOT_LOW; // 1 << 1
Kweku Adams32f44762018-11-02 16:58:32 -070076 static final int CONSTRAINT_STORAGE_NOT_LOW = JobInfo.CONSTRAINT_FLAG_STORAGE_NOT_LOW; // 1 << 3
Dianne Hackborna06ec6a2017-02-13 10:08:42 -080077 static final int CONSTRAINT_TIMING_DELAY = 1<<31;
78 static final int CONSTRAINT_DEADLINE = 1<<30;
Kweku Adamsae9d6be2020-01-14 16:10:14 -080079 public static final int CONSTRAINT_CONNECTIVITY = 1 << 28;
Dianne Hackborna06ec6a2017-02-13 10:08:42 -080080 static final int CONSTRAINT_CONTENT_TRIGGER = 1<<26;
Kweku Adamsab8a9012019-01-28 15:42:09 -080081 static final int CONSTRAINT_DEVICE_NOT_DOZING = 1 << 25; // Implicit constraint
82 static final int CONSTRAINT_WITHIN_QUOTA = 1 << 24; // Implicit constraint
83 static final int CONSTRAINT_BACKGROUND_NOT_RESTRICTED = 1 << 22; // Implicit constraint
Christopher Tate60977f42017-04-13 13:48:46 -070084
Kweku Adams8845d012018-12-11 20:06:45 -080085 /**
86 * The constraints that we want to log to statsd.
87 *
88 * Constraints that can be inferred from other atoms have been excluded to avoid logging too
89 * much information and to reduce redundancy:
90 *
91 * * CONSTRAINT_CHARGING can be inferred with PluggedStateChanged (Atom #32)
92 * * CONSTRAINT_BATTERY_NOT_LOW can be inferred with BatteryLevelChanged (Atom #30)
93 * * CONSTRAINT_CONNECTIVITY can be partially inferred with ConnectivityStateChanged
94 * (Atom #98) and BatterySaverModeStateChanged (Atom #20).
95 * * CONSTRAINT_DEVICE_NOT_DOZING can be mostly inferred with DeviceIdleModeStateChanged
96 * (Atom #21)
97 * * CONSTRAINT_BACKGROUND_NOT_RESTRICTED can be inferred with BatterySaverModeStateChanged
98 * (Atom #20)
99 */
100 private static final int STATSD_CONSTRAINTS_TO_LOG = CONSTRAINT_CONTENT_TRIGGER
101 | CONSTRAINT_DEADLINE
102 | CONSTRAINT_IDLE
103 | CONSTRAINT_STORAGE_NOT_LOW
104 | CONSTRAINT_TIMING_DELAY
105 | CONSTRAINT_WITHIN_QUOTA;
106
Yao Chen26217e32019-04-04 16:34:24 -0700107 // TODO(b/129954980)
108 private static final boolean STATS_LOG_ENABLED = false;
109
Christopher Tate5d346052016-03-08 12:56:08 -0800110 // Soft override: ignore constraints like time that don't affect API availability
111 public static final int OVERRIDE_SOFT = 1;
112 // Full override: ignore all constraints including API-affecting like connectivity
113 public static final int OVERRIDE_FULL = 2;
114
Dianne Hackborn8db0fc12016-04-12 13:48:25 -0700115 /** If not specified, trigger update delay is 10 seconds. */
116 public static final long DEFAULT_TRIGGER_UPDATE_DELAY = 10*1000;
117
118 /** The minimum possible update delay is 1/2 second. */
119 public static final long MIN_TRIGGER_UPDATE_DELAY = 500;
120
Kweku Adamsae9d6be2020-01-14 16:10:14 -0800121 /** If not specified, trigger maximum delay is 2 minutes. */
Dianne Hackborn8db0fc12016-04-12 13:48:25 -0700122 public static final long DEFAULT_TRIGGER_MAX_DELAY = 2*60*1000;
123
124 /** The minimum possible update delay is 1 second. */
125 public static final long MIN_TRIGGER_MAX_DELAY = 1000;
126
Christopher Tate7060b042014-06-09 19:50:00 -0700127 final JobInfo job;
Christopher Tate72a0b2a2018-03-19 17:24:32 -0700128 /**
129 * Uid of the package requesting this job. This can differ from the "source"
130 * uid when the job was scheduled on the app's behalf, such as with the jobs
131 * that underly Sync Manager operation.
132 */
Shreyas Basarge8e64e2e2016-02-12 15:49:31 +0000133 final int callingUid;
Dianne Hackborn1085ff62016-02-23 17:04:58 -0800134 final String batteryName;
Christopher Tate7060b042014-06-09 19:50:00 -0700135
Christopher Tate72a0b2a2018-03-19 17:24:32 -0700136 /**
137 * Identity of the app in which the job is hosted.
138 */
Shreyas Basarge8e64e2e2016-02-12 15:49:31 +0000139 final String sourcePackageName;
140 final int sourceUserId;
141 final int sourceUid;
Dianne Hackborn1085ff62016-02-23 17:04:58 -0800142 final String sourceTag;
143
144 final String tag;
Shreyas Basarge968ac752016-01-11 23:09:26 +0000145
Dianne Hackborn342e6032017-04-13 18:04:31 -0700146 private GrantedUriPermissions uriPerms;
Dianne Hackborna47223f2017-03-30 13:49:13 -0700147 private boolean prepared;
148
Christopher Tateb164f012017-04-26 15:41:37 -0700149 static final boolean DEBUG_PREPARE = true;
150 private Throwable unpreparedPoint = null;
151
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800152 /**
153 * Earliest point in the future at which this job will be eligible to run. A value of 0
154 * indicates there is no delay constraint. See {@link #hasTimingDelayConstraint()}.
155 */
156 private final long earliestRunTimeElapsedMillis;
157 /**
158 * Latest point in the future at which this job must be run. A value of {@link Long#MAX_VALUE}
159 * indicates there is no deadline constraint. See {@link #hasDeadlineConstraint()}.
160 */
161 private final long latestRunTimeElapsedMillis;
162
Kweku Adamsc9793092019-04-30 11:54:48 -0700163 /**
164 * Valid only for periodic jobs. The original latest point in the future at which this
165 * job was expected to run.
166 */
167 private long mOriginalLatestRunTimeElapsedMillis;
168
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800169 /** How many times this job has failed, used to compute back-off. */
170 private final int numFailures;
171
Christopher Tatea732f012017-10-26 17:26:53 -0700172 /**
Christopher Tatea732f012017-10-26 17:26:53 -0700173 * Which app standby bucket this job's app is in. Updated when the app is moved to a
174 * different bucket.
175 */
176 private int standbyBucket;
177
178 /**
179 * Debugging: timestamp if we ever defer this job based on standby bucketing, this
180 * is when we did so.
181 */
182 private long whenStandbyDeferred;
183
Kweku Adams41cb3322019-06-26 14:42:26 -0700184 /** The first time this job was force batched. */
185 private long mFirstForceBatchedTimeElapsed;
186
Christopher Tate7060b042014-06-09 19:50:00 -0700187 // Constraints.
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800188 final int requiredConstraints;
Kweku Adamsd14ce4d2018-10-19 11:40:55 -0700189 private final int mRequiredConstraintsOfInterest;
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800190 int satisfiedConstraints = 0;
Kweku Adamsd14ce4d2018-10-19 11:40:55 -0700191 private int mSatisfiedConstraintsOfInterest = 0;
Kweku Adamsae9d6be2020-01-14 16:10:14 -0800192 /**
193 * Set of constraints that must be satisfied for the job if/because it's in the RESTRICTED
194 * bucket.
195 */
196 private int mDynamicConstraints = 0;
Dianne Hackborn1a30bd92016-01-11 11:05:00 -0800197
Dianne Hackborn7ab40252016-06-15 17:30:24 -0700198 // Set to true if doze constraint was satisfied due to app being whitelisted.
199 public boolean dozeWhitelisted;
200
Christopher Tate20afddd2018-02-28 15:19:19 -0800201 // Set to true when the app is "active" per AppStateTracker
202 public boolean uidActive;
203
Dianne Hackbornf9bac162017-04-20 17:17:48 -0700204 /**
205 * Flag for {@link #trackingControllers}: the battery controller is currently tracking this job.
206 */
207 public static final int TRACKING_BATTERY = 1<<0;
208 /**
209 * Flag for {@link #trackingControllers}: the network connectivity controller is currently
210 * tracking this job.
211 */
212 public static final int TRACKING_CONNECTIVITY = 1<<1;
213 /**
214 * Flag for {@link #trackingControllers}: the content observer controller is currently
215 * tracking this job.
216 */
217 public static final int TRACKING_CONTENT = 1<<2;
218 /**
219 * Flag for {@link #trackingControllers}: the idle controller is currently tracking this job.
220 */
221 public static final int TRACKING_IDLE = 1<<3;
222 /**
223 * Flag for {@link #trackingControllers}: the storage controller is currently tracking this job.
224 */
225 public static final int TRACKING_STORAGE = 1<<4;
226 /**
227 * Flag for {@link #trackingControllers}: the time controller is currently tracking this job.
228 */
229 public static final int TRACKING_TIME = 1<<5;
Kweku Adams4836f9d2018-11-12 17:04:17 -0800230 /**
231 * Flag for {@link #trackingControllers}: the quota controller is currently tracking this job.
232 */
233 public static final int TRACKING_QUOTA = 1 << 6;
Dianne Hackbornf9bac162017-04-20 17:17:48 -0700234
235 /**
236 * Bit mask of controllers that are currently tracking the job.
237 */
238 private int trackingControllers;
239
Makoto Onuki15407842018-01-19 14:23:11 -0800240 /**
241 * Flag for {@link #mInternalFlags}: this job was scheduled when the app that owns the job
242 * service (not necessarily the caller) was in the foreground and the job has no time
243 * constraints, which makes it exempted from the battery saver job restriction.
244 *
245 * @hide
246 */
247 public static final int INTERNAL_FLAG_HAS_FOREGROUND_EXEMPTION = 1 << 0;
248
249 /**
250 * Versatile, persistable flags for a job that's updated within the system server,
251 * as opposed to {@link JobInfo#flags} that's set by callers.
252 */
253 private int mInternalFlags;
254
Dianne Hackborn1a30bd92016-01-11 11:05:00 -0800255 // These are filled in by controllers when preparing for execution.
256 public ArraySet<Uri> changedUris;
257 public ArraySet<String> changedAuthorities;
Jeff Sharkey76a02412017-10-24 16:55:04 -0600258 public Network network;
Dianne Hackborn1a30bd92016-01-11 11:05:00 -0800259
Dianne Hackborn1085ff62016-02-23 17:04:58 -0800260 public int lastEvaluatedPriority;
261
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700262 // If non-null, this is work that has been enqueued for the job.
263 public ArrayList<JobWorkItem> pendingWork;
264
265 // If non-null, this is work that is currently being executed.
266 public ArrayList<JobWorkItem> executingWork;
267
268 public int nextPendingWorkId = 1;
269
Christopher Tate5d346052016-03-08 12:56:08 -0800270 // Used by shell commands
271 public int overrideState = 0;
272
Dianne Hackbornbfc23312017-05-11 11:53:24 -0700273 // When this job was enqueued, for ordering. (in elapsedRealtimeMillis)
274 public long enqueueTime;
275
276 // Metrics about queue latency. (in uptimeMillis)
Christopher Tate7234fc62017-04-03 17:36:07 -0700277 public long madePending;
278 public long madeActive;
279
Dianne Hackborn1a30bd92016-01-11 11:05:00 -0800280 /**
Makoto Onukiab8a67f2017-06-20 12:20:34 -0700281 * Last time a job finished successfully for a periodic job, in the currentTimeMillis time,
282 * for dumpsys.
283 */
284 private long mLastSuccessfulRunTime;
285
286 /**
287 * Last time a job finished unsuccessfully, in the currentTimeMillis time, for dumpsys.
288 */
289 private long mLastFailedRunTime;
290
291 /**
Christopher Tate616541d2017-07-26 14:27:38 -0700292 * Transient: when a job is inflated from disk before we have a reliable RTC clock time,
293 * we retain the canonical (delay, deadline) scheduling tuple read out of the persistent
294 * store in UTC so that we can fix up the job's scheduling criteria once we get a good
295 * wall-clock time. If we have to persist the job again before the clock has been updated,
296 * we record these times again rather than calculating based on the earliest/latest elapsed
297 * time base figures.
298 *
299 * 'first' is the earliest/delay time, and 'second' is the latest/deadline time.
300 */
301 private Pair<Long, Long> mPersistedUtcTimes;
302
303 /**
Dianne Hackborn1a30bd92016-01-11 11:05:00 -0800304 * For use only by ContentObserverController: state it is maintaining about content URIs
305 * being observed.
306 */
307 ContentObserverController.JobInstance contentObserverJobInstance;
Christopher Tate7060b042014-06-09 19:50:00 -0700308
Kweku Adams316b1412019-06-14 11:34:37 -0700309 private long mTotalNetworkDownloadBytes = JobInfo.NETWORK_BYTES_UNKNOWN;
310 private long mTotalNetworkUploadBytes = JobInfo.NETWORK_BYTES_UNKNOWN;
Jeff Sharkeycaa3f8d2017-10-25 16:12:00 -0600311
Kweku Adamsd14ce4d2018-10-19 11:40:55 -0700312 /////// Booleans that track if a job is ready to run. They should be updated whenever dependent
313 /////// states change.
314
315 /**
316 * The deadline for the job has passed. This is only good for non-periodic jobs. A periodic job
317 * should only run if its constraints are satisfied.
318 * Computed as: NOT periodic AND has deadline constraint AND deadline constraint satisfied.
319 */
320 private boolean mReadyDeadlineSatisfied;
321
322 /**
323 * The device isn't Dozing or this job will be in the foreground. This implicit constraint must
324 * be satisfied.
325 */
326 private boolean mReadyNotDozing;
327
328 /**
329 * The job is not restricted from running in the background (due to Battery Saver). This
330 * implicit constraint must be satisfied.
331 */
332 private boolean mReadyNotRestrictedInBg;
333
Kweku Adams4836f9d2018-11-12 17:04:17 -0800334 /** The job is within its quota based on its standby bucket. */
335 private boolean mReadyWithinQuota;
336
Kweku Adamsae9d6be2020-01-14 16:10:14 -0800337 /** The job's dynamic requirements have been satisfied. */
338 private boolean mReadyDynamicSatisfied;
339
Christopher Tate7060b042014-06-09 19:50:00 -0700340 /** Provide a handle to the service that this job will be run on. */
341 public int getServiceToken() {
Shreyas Basarge8e64e2e2016-02-12 15:49:31 +0000342 return callingUid;
Christopher Tate7060b042014-06-09 19:50:00 -0700343 }
344
Christopher Tate72a0b2a2018-03-19 17:24:32 -0700345 /**
346 * Core constructor for JobStatus instances. All other ctors funnel down to this one.
347 *
348 * @param job The actual requested parameters for the job
349 * @param callingUid Identity of the app that is scheduling the job. This may not be the
350 * app in which the job is implemented; such as with sync jobs.
Christopher Tate72a0b2a2018-03-19 17:24:32 -0700351 * @param sourcePackageName The package name of the app in which the job will run.
352 * @param sourceUserId The user in which the job will run
353 * @param standbyBucket The standby bucket that the source package is currently assigned to,
354 * cached here for speed of handling during runnability evaluations (and updated when bucket
355 * assignments are changed)
Christopher Tate72a0b2a2018-03-19 17:24:32 -0700356 * @param tag A string associated with the job for debugging/logging purposes.
357 * @param numFailures Count of how many times this job has requested a reschedule because
358 * its work was not yet finished.
359 * @param earliestRunTimeElapsedMillis Milestone: earliest point in time at which the job
360 * is to be considered runnable
361 * @param latestRunTimeElapsedMillis Milestone: point in time at which the job will be
362 * considered overdue
363 * @param lastSuccessfulRunTime When did we last run this job to completion?
364 * @param lastFailedRunTime When did we last run this job only to have it stop incomplete?
365 * @param internalFlags Non-API property flags about this job
366 */
Kweku Adams8843a2812019-07-02 15:30:23 -0700367 private JobStatus(JobInfo job, int callingUid, String sourcePackageName,
Kweku Adams6c5dc642019-07-25 10:29:38 -0700368 int sourceUserId, int standbyBucket, String tag, int numFailures,
Christopher Tatea732f012017-10-26 17:26:53 -0700369 long earliestRunTimeElapsedMillis, long latestRunTimeElapsedMillis,
Makoto Onuki15407842018-01-19 14:23:11 -0800370 long lastSuccessfulRunTime, long lastFailedRunTime, int internalFlags) {
Christopher Tate7060b042014-06-09 19:50:00 -0700371 this.job = job;
Shreyas Basarge8e64e2e2016-02-12 15:49:31 +0000372 this.callingUid = callingUid;
Christopher Tatea732f012017-10-26 17:26:53 -0700373 this.standbyBucket = standbyBucket;
Shreyas Basarge8e64e2e2016-02-12 15:49:31 +0000374
375 int tempSourceUid = -1;
376 if (sourceUserId != -1 && sourcePackageName != null) {
377 try {
378 tempSourceUid = AppGlobals.getPackageManager().getPackageUid(sourcePackageName, 0,
379 sourceUserId);
380 } catch (RemoteException ex) {
381 // Can't happen, PackageManager runs in the same process.
382 }
383 }
384 if (tempSourceUid == -1) {
385 this.sourceUid = callingUid;
386 this.sourceUserId = UserHandle.getUserId(callingUid);
387 this.sourcePackageName = job.getService().getPackageName();
Dianne Hackborn1085ff62016-02-23 17:04:58 -0800388 this.sourceTag = null;
Shreyas Basarge8e64e2e2016-02-12 15:49:31 +0000389 } else {
390 this.sourceUid = tempSourceUid;
391 this.sourceUserId = sourceUserId;
392 this.sourcePackageName = sourcePackageName;
Dianne Hackborn1085ff62016-02-23 17:04:58 -0800393 this.sourceTag = tag;
Shreyas Basarge8e64e2e2016-02-12 15:49:31 +0000394 }
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800395
Shreyas Basargeeda34e42016-04-26 00:14:02 +0100396 this.batteryName = this.sourceTag != null
397 ? this.sourceTag + ":" + job.getService().getPackageName()
398 : job.getService().flattenToShortString();
Dianne Hackborn1085ff62016-02-23 17:04:58 -0800399 this.tag = "*job*/" + this.batteryName;
400
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800401 this.earliestRunTimeElapsedMillis = earliestRunTimeElapsedMillis;
402 this.latestRunTimeElapsedMillis = latestRunTimeElapsedMillis;
Kweku Adamsc9793092019-04-30 11:54:48 -0700403 this.mOriginalLatestRunTimeElapsedMillis = latestRunTimeElapsedMillis;
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800404 this.numFailures = numFailures;
405
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800406 int requiredConstraints = job.getConstraintFlags();
Jeff Sharkeyd0fff2e2017-11-07 16:55:06 -0700407 if (job.getRequiredNetwork() != null) {
408 requiredConstraints |= CONSTRAINT_CONNECTIVITY;
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800409 }
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800410 if (earliestRunTimeElapsedMillis != NO_EARLIEST_RUNTIME) {
411 requiredConstraints |= CONSTRAINT_TIMING_DELAY;
412 }
413 if (latestRunTimeElapsedMillis != NO_LATEST_RUNTIME) {
414 requiredConstraints |= CONSTRAINT_DEADLINE;
415 }
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800416 if (job.getTriggerContentUris() != null) {
417 requiredConstraints |= CONSTRAINT_CONTENT_TRIGGER;
418 }
419 this.requiredConstraints = requiredConstraints;
Kweku Adamsd14ce4d2018-10-19 11:40:55 -0700420 mRequiredConstraintsOfInterest = requiredConstraints & CONSTRAINTS_OF_INTEREST;
421 mReadyNotDozing = (job.getFlags() & JobInfo.FLAG_WILL_BE_FOREGROUND) != 0;
Kweku Adamsae9d6be2020-01-14 16:10:14 -0800422 mReadyDynamicSatisfied = true;
Makoto Onukiab8a67f2017-06-20 12:20:34 -0700423
424 mLastSuccessfulRunTime = lastSuccessfulRunTime;
425 mLastFailedRunTime = lastFailedRunTime;
Jeff Sharkeycaa3f8d2017-10-25 16:12:00 -0600426
Makoto Onuki15407842018-01-19 14:23:11 -0800427 mInternalFlags = internalFlags;
428
Jeff Sharkeycaa3f8d2017-10-25 16:12:00 -0600429 updateEstimatedNetworkBytesLocked();
Jeff Sharkeyfb0f9712018-02-21 20:27:48 -0700430
431 if (job.getRequiredNetwork() != null) {
432 // Later, when we check if a given network satisfies the required
433 // network, we need to know the UID that is requesting it, so push
434 // our source UID into place.
435 job.getRequiredNetwork().networkCapabilities.setSingleUid(this.sourceUid);
436 }
Christopher Tate7060b042014-06-09 19:50:00 -0700437 }
438
Christopher Tate616541d2017-07-26 14:27:38 -0700439 /** Copy constructor: used specifically when cloning JobStatus objects for persistence,
440 * so we preserve RTC window bounds if the source object has them. */
Matthew Williams0cc76542015-10-16 21:04:51 -0700441 public JobStatus(JobStatus jobStatus) {
Kweku Adams8843a2812019-07-02 15:30:23 -0700442 this(jobStatus.getJob(), jobStatus.getUid(),
Dianne Hackbornd506b2b2016-02-16 10:30:33 -0800443 jobStatus.getSourcePackageName(), jobStatus.getSourceUserId(),
Kweku Adams6c5dc642019-07-25 10:29:38 -0700444 jobStatus.getStandbyBucket(),
Dianne Hackborn1085ff62016-02-23 17:04:58 -0800445 jobStatus.getSourceTag(), jobStatus.getNumFailures(),
Makoto Onukiab8a67f2017-06-20 12:20:34 -0700446 jobStatus.getEarliestRunTime(), jobStatus.getLatestRunTimeElapsed(),
Makoto Onuki15407842018-01-19 14:23:11 -0800447 jobStatus.getLastSuccessfulRunTime(), jobStatus.getLastFailedRunTime(),
448 jobStatus.getInternalFlags());
Christopher Tate616541d2017-07-26 14:27:38 -0700449 mPersistedUtcTimes = jobStatus.mPersistedUtcTimes;
450 if (jobStatus.mPersistedUtcTimes != null) {
451 if (DEBUG) {
452 Slog.i(TAG, "Cloning job with persisted run times", new RuntimeException("here"));
453 }
454 }
Christopher Tate7060b042014-06-09 19:50:00 -0700455 }
456
457 /**
458 * Create a new JobStatus that was loaded from disk. We ignore the provided
459 * {@link android.app.job.JobInfo} time criteria because we can load a persisted periodic job
460 * from the {@link com.android.server.job.JobStore} and still want to respect its
461 * wallclock runtime rather than resetting it on every boot.
Christopher Tatea732f012017-10-26 17:26:53 -0700462 * We consider a freshly loaded job to no longer be in back-off, and the associated
463 * standby bucket is whatever the OS thinks it should be at this moment.
Christopher Tate7060b042014-06-09 19:50:00 -0700464 */
Christopher Tatea732f012017-10-26 17:26:53 -0700465 public JobStatus(JobInfo job, int callingUid, String sourcePkgName, int sourceUserId,
Kweku Adams6c5dc642019-07-25 10:29:38 -0700466 int standbyBucket, String sourceTag,
Christopher Tatea732f012017-10-26 17:26:53 -0700467 long earliestRunTimeElapsedMillis, long latestRunTimeElapsedMillis,
Christopher Tate616541d2017-07-26 14:27:38 -0700468 long lastSuccessfulRunTime, long lastFailedRunTime,
Makoto Onuki15407842018-01-19 14:23:11 -0800469 Pair<Long, Long> persistedExecutionTimesUTC,
470 int innerFlags) {
Kweku Adams8843a2812019-07-02 15:30:23 -0700471 this(job, callingUid, sourcePkgName, sourceUserId,
Kweku Adams6c5dc642019-07-25 10:29:38 -0700472 standbyBucket,
Christopher Tatea732f012017-10-26 17:26:53 -0700473 sourceTag, 0,
Makoto Onukiab8a67f2017-06-20 12:20:34 -0700474 earliestRunTimeElapsedMillis, latestRunTimeElapsedMillis,
Makoto Onuki15407842018-01-19 14:23:11 -0800475 lastSuccessfulRunTime, lastFailedRunTime, innerFlags);
Christopher Tate616541d2017-07-26 14:27:38 -0700476
477 // Only during initial inflation do we record the UTC-timebase execution bounds
478 // read from the persistent store. If we ever have to recreate the JobStatus on
479 // the fly, it means we're rescheduling the job; and this means that the calculated
480 // elapsed timebase bounds intrinsically become correct.
481 this.mPersistedUtcTimes = persistedExecutionTimesUTC;
482 if (persistedExecutionTimesUTC != null) {
483 if (DEBUG) {
484 Slog.i(TAG, "+ restored job with RTC times because of bad boot clock");
485 }
486 }
Christopher Tate7060b042014-06-09 19:50:00 -0700487 }
488
489 /** Create a new job to be rescheduled with the provided parameters. */
Kweku Adams6c5dc642019-07-25 10:29:38 -0700490 public JobStatus(JobStatus rescheduling,
Christopher Tatea732f012017-10-26 17:26:53 -0700491 long newEarliestRuntimeElapsedMillis,
Makoto Onukiab8a67f2017-06-20 12:20:34 -0700492 long newLatestRuntimeElapsedMillis, int backoffAttempt,
493 long lastSuccessfulRunTime, long lastFailedRunTime) {
Kweku Adams8843a2812019-07-02 15:30:23 -0700494 this(rescheduling.job, rescheduling.getUid(),
Dianne Hackborn1085ff62016-02-23 17:04:58 -0800495 rescheduling.getSourcePackageName(), rescheduling.getSourceUserId(),
Kweku Adams6c5dc642019-07-25 10:29:38 -0700496 rescheduling.getStandbyBucket(),
Dianne Hackborn1085ff62016-02-23 17:04:58 -0800497 rescheduling.getSourceTag(), backoffAttempt, newEarliestRuntimeElapsedMillis,
Makoto Onukiab8a67f2017-06-20 12:20:34 -0700498 newLatestRuntimeElapsedMillis,
Makoto Onuki15407842018-01-19 14:23:11 -0800499 lastSuccessfulRunTime, lastFailedRunTime, rescheduling.getInternalFlags());
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800500 }
Christopher Tate7060b042014-06-09 19:50:00 -0700501
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800502 /**
503 * Create a newly scheduled job.
504 * @param callingUid Uid of the package that scheduled this job.
Christopher Tate72a0b2a2018-03-19 17:24:32 -0700505 * @param sourcePkg Package name of the app that will actually run the job. Null indicates
506 * that the calling package is the source.
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800507 * @param sourceUserId User id for whom this job is scheduled. -1 indicates this is same as the
Christopher Tatea732f012017-10-26 17:26:53 -0700508 * caller.
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800509 */
Christopher Tatea732f012017-10-26 17:26:53 -0700510 public static JobStatus createFromJobInfo(JobInfo job, int callingUid, String sourcePkg,
Dianne Hackborn1085ff62016-02-23 17:04:58 -0800511 int sourceUserId, String tag) {
Jeff Sharkeyd0fff2e2017-11-07 16:55:06 -0700512 final long elapsedNow = sElapsedRealtimeClock.millis();
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800513 final long earliestRunTimeElapsedMillis, latestRunTimeElapsedMillis;
514 if (job.isPeriodic()) {
Kweku Adams966e72b2019-05-30 14:45:39 -0700515 // Make sure period is in the interval [min_possible_period, max_possible_period].
516 final long period = Math.max(JobInfo.getMinPeriodMillis(),
517 Math.min(JobSchedulerService.MAX_ALLOWED_PERIOD_MS, job.getIntervalMillis()));
518 latestRunTimeElapsedMillis = elapsedNow + period;
519 earliestRunTimeElapsedMillis = latestRunTimeElapsedMillis
520 // Make sure flex is in the interval [min_possible_flex, period].
521 - Math.max(JobInfo.getMinFlexMillis(), Math.min(period, job.getFlexMillis()));
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800522 } else {
523 earliestRunTimeElapsedMillis = job.hasEarlyConstraint() ?
524 elapsedNow + job.getMinLatencyMillis() : NO_EARLIEST_RUNTIME;
525 latestRunTimeElapsedMillis = job.hasLateConstraint() ?
526 elapsedNow + job.getMaxExecutionDelayMillis() : NO_LATEST_RUNTIME;
527 }
Christopher Tatea732f012017-10-26 17:26:53 -0700528 String jobPackage = (sourcePkg != null) ? sourcePkg : job.getService().getPackageName();
529
530 int standbyBucket = JobSchedulerService.standbyBucketForPackage(jobPackage,
531 sourceUserId, elapsedNow);
532 JobSchedulerInternal js = LocalServices.getService(JobSchedulerInternal.class);
Kweku Adams8843a2812019-07-02 15:30:23 -0700533 return new JobStatus(job, callingUid, sourcePkg, sourceUserId,
Kweku Adams6c5dc642019-07-25 10:29:38 -0700534 standbyBucket, tag, 0,
Makoto Onukiab8a67f2017-06-20 12:20:34 -0700535 earliestRunTimeElapsedMillis, latestRunTimeElapsedMillis,
Makoto Onuki15407842018-01-19 14:23:11 -0800536 0 /* lastSuccessfulRunTime */, 0 /* lastFailedRunTime */,
537 /*innerFlags=*/ 0);
Christopher Tate7060b042014-06-09 19:50:00 -0700538 }
539
Christopher Tate1b1fca82019-08-14 16:59:33 -0700540 public void enqueueWorkLocked(JobWorkItem work) {
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700541 if (pendingWork == null) {
542 pendingWork = new ArrayList<>();
543 }
544 work.setWorkId(nextPendingWorkId);
545 nextPendingWorkId++;
Dianne Hackborn342e6032017-04-13 18:04:31 -0700546 if (work.getIntent() != null
547 && GrantedUriPermissions.checkGrantFlags(work.getIntent().getFlags())) {
Christopher Tate1b1fca82019-08-14 16:59:33 -0700548 work.setGrants(GrantedUriPermissions.createFromIntent(work.getIntent(), sourceUid,
Dianne Hackborn342e6032017-04-13 18:04:31 -0700549 sourcePackageName, sourceUserId, toShortString()));
550 }
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700551 pendingWork.add(work);
Jeff Sharkeycaa3f8d2017-10-25 16:12:00 -0600552 updateEstimatedNetworkBytesLocked();
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700553 }
554
555 public JobWorkItem dequeueWorkLocked() {
556 if (pendingWork != null && pendingWork.size() > 0) {
557 JobWorkItem work = pendingWork.remove(0);
558 if (work != null) {
559 if (executingWork == null) {
560 executingWork = new ArrayList<>();
561 }
562 executingWork.add(work);
Dianne Hackborn28d1b662017-04-21 14:17:23 -0700563 work.bumpDeliveryCount();
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700564 }
Jeff Sharkeycaa3f8d2017-10-25 16:12:00 -0600565 updateEstimatedNetworkBytesLocked();
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700566 return work;
567 }
568 return null;
569 }
570
Dianne Hackbornbfc23312017-05-11 11:53:24 -0700571 public boolean hasWorkLocked() {
572 return (pendingWork != null && pendingWork.size() > 0) || hasExecutingWorkLocked();
573 }
574
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700575 public boolean hasExecutingWorkLocked() {
576 return executingWork != null && executingWork.size() > 0;
577 }
578
Christopher Tate1b1fca82019-08-14 16:59:33 -0700579 private static void ungrantWorkItem(JobWorkItem work) {
Dianne Hackborn342e6032017-04-13 18:04:31 -0700580 if (work.getGrants() != null) {
Christopher Tate1b1fca82019-08-14 16:59:33 -0700581 ((GrantedUriPermissions)work.getGrants()).revoke();
Dianne Hackborn342e6032017-04-13 18:04:31 -0700582 }
583 }
584
Christopher Tate1b1fca82019-08-14 16:59:33 -0700585 public boolean completeWorkLocked(int workId) {
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700586 if (executingWork != null) {
587 final int N = executingWork.size();
588 for (int i = 0; i < N; i++) {
Dianne Hackborn342e6032017-04-13 18:04:31 -0700589 JobWorkItem work = executingWork.get(i);
590 if (work.getWorkId() == workId) {
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700591 executingWork.remove(i);
Christopher Tate1b1fca82019-08-14 16:59:33 -0700592 ungrantWorkItem(work);
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700593 return true;
594 }
595 }
596 }
597 return false;
598 }
599
Christopher Tate1b1fca82019-08-14 16:59:33 -0700600 private static void ungrantWorkList(ArrayList<JobWorkItem> list) {
Dianne Hackborn342e6032017-04-13 18:04:31 -0700601 if (list != null) {
602 final int N = list.size();
603 for (int i = 0; i < N; i++) {
Christopher Tate1b1fca82019-08-14 16:59:33 -0700604 ungrantWorkItem(list.get(i));
Dianne Hackborn342e6032017-04-13 18:04:31 -0700605 }
606 }
607 }
608
Christopher Tate1b1fca82019-08-14 16:59:33 -0700609 public void stopTrackingJobLocked(JobStatus incomingJob) {
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700610 if (incomingJob != null) {
Dianne Hackborn342e6032017-04-13 18:04:31 -0700611 // We are replacing with a new job -- transfer the work! We do any executing
612 // work first, since that was originally at the front of the pending work.
613 if (executingWork != null && executingWork.size() > 0) {
614 incomingJob.pendingWork = executingWork;
615 }
616 if (incomingJob.pendingWork == null) {
617 incomingJob.pendingWork = pendingWork;
618 } else if (pendingWork != null && pendingWork.size() > 0) {
619 incomingJob.pendingWork.addAll(pendingWork);
620 }
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700621 pendingWork = null;
Dianne Hackborn342e6032017-04-13 18:04:31 -0700622 executingWork = null;
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700623 incomingJob.nextPendingWorkId = nextPendingWorkId;
Jeff Sharkeycaa3f8d2017-10-25 16:12:00 -0600624 incomingJob.updateEstimatedNetworkBytesLocked();
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700625 } else {
626 // We are completely stopping the job... need to clean up work.
Christopher Tate1b1fca82019-08-14 16:59:33 -0700627 ungrantWorkList(pendingWork);
Dianne Hackborn342e6032017-04-13 18:04:31 -0700628 pendingWork = null;
Christopher Tate1b1fca82019-08-14 16:59:33 -0700629 ungrantWorkList(executingWork);
Dianne Hackborn342e6032017-04-13 18:04:31 -0700630 executingWork = null;
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700631 }
Jeff Sharkeycaa3f8d2017-10-25 16:12:00 -0600632 updateEstimatedNetworkBytesLocked();
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700633 }
634
Christopher Tate1b1fca82019-08-14 16:59:33 -0700635 public void prepareLocked() {
Dianne Hackborna47223f2017-03-30 13:49:13 -0700636 if (prepared) {
637 Slog.wtf(TAG, "Already prepared: " + this);
638 return;
639 }
640 prepared = true;
Christopher Tateb164f012017-04-26 15:41:37 -0700641 if (DEBUG_PREPARE) {
642 unpreparedPoint = null;
643 }
Dianne Hackborna47223f2017-03-30 13:49:13 -0700644 final ClipData clip = job.getClipData();
645 if (clip != null) {
Christopher Tate1b1fca82019-08-14 16:59:33 -0700646 uriPerms = GrantedUriPermissions.createFromClip(clip, sourceUid, sourcePackageName,
Dianne Hackborn342e6032017-04-13 18:04:31 -0700647 sourceUserId, job.getClipGrantFlags(), toShortString());
Dianne Hackborna47223f2017-03-30 13:49:13 -0700648 }
649 }
650
Christopher Tate1b1fca82019-08-14 16:59:33 -0700651 public void unprepareLocked() {
Dianne Hackborna47223f2017-03-30 13:49:13 -0700652 if (!prepared) {
653 Slog.wtf(TAG, "Hasn't been prepared: " + this);
Christopher Tateb164f012017-04-26 15:41:37 -0700654 if (DEBUG_PREPARE && unpreparedPoint != null) {
655 Slog.e(TAG, "Was already unprepared at ", unpreparedPoint);
656 }
Dianne Hackborna47223f2017-03-30 13:49:13 -0700657 return;
658 }
659 prepared = false;
Christopher Tateb164f012017-04-26 15:41:37 -0700660 if (DEBUG_PREPARE) {
661 unpreparedPoint = new Throwable().fillInStackTrace();
662 }
Dianne Hackborn342e6032017-04-13 18:04:31 -0700663 if (uriPerms != null) {
Christopher Tate1b1fca82019-08-14 16:59:33 -0700664 uriPerms.revoke();
Dianne Hackborn342e6032017-04-13 18:04:31 -0700665 uriPerms = null;
Dianne Hackborna47223f2017-03-30 13:49:13 -0700666 }
667 }
668
Dianne Hackborn7da13d72017-04-04 17:17:35 -0700669 public boolean isPreparedLocked() {
Dianne Hackborna47223f2017-03-30 13:49:13 -0700670 return prepared;
671 }
672
Christopher Tate7060b042014-06-09 19:50:00 -0700673 public JobInfo getJob() {
674 return job;
675 }
676
677 public int getJobId() {
678 return job.getId();
679 }
680
Dianne Hackborne9a988c2016-05-27 17:59:40 -0700681 public void printUniqueId(PrintWriter pw) {
682 UserHandle.formatUid(pw, callingUid);
683 pw.print("/");
684 pw.print(job.getId());
685 }
686
Christopher Tate7060b042014-06-09 19:50:00 -0700687 public int getNumFailures() {
688 return numFailures;
689 }
690
691 public ComponentName getServiceComponent() {
692 return job.getService();
693 }
694
Shreyas Basarge968ac752016-01-11 23:09:26 +0000695 public String getSourcePackageName() {
Shreyas Basarge8e64e2e2016-02-12 15:49:31 +0000696 return sourcePackageName;
Shreyas Basarge968ac752016-01-11 23:09:26 +0000697 }
698
699 public int getSourceUid() {
700 return sourceUid;
701 }
702
703 public int getSourceUserId() {
Shreyas Basarge968ac752016-01-11 23:09:26 +0000704 return sourceUserId;
705 }
706
Christopher Tate7060b042014-06-09 19:50:00 -0700707 public int getUserId() {
Shreyas Basarge8e64e2e2016-02-12 15:49:31 +0000708 return UserHandle.getUserId(callingUid);
Christopher Tate7060b042014-06-09 19:50:00 -0700709 }
710
Kweku Adamsbbb04592019-08-05 15:17:23 -0700711 /**
712 * Returns an appropriate standby bucket for the job, taking into account any standby
713 * exemptions.
714 */
715 public int getEffectiveStandbyBucket() {
716 if (uidActive || getJob().isExemptedFromAppStandby()) {
717 // Treat these cases as if they're in the ACTIVE bucket so that they get throttled
718 // like other ACTIVE apps.
719 return ACTIVE_INDEX;
720 }
721 return getStandbyBucket();
722 }
723
724 /** Returns the real standby bucket of the job. */
Christopher Tatea732f012017-10-26 17:26:53 -0700725 public int getStandbyBucket() {
726 return standbyBucket;
727 }
728
Christopher Tatea732f012017-10-26 17:26:53 -0700729 public void setStandbyBucket(int newBucket) {
730 standbyBucket = newBucket;
731 }
732
733 // Called only by the standby monitoring code
734 public long getWhenStandbyDeferred() {
735 return whenStandbyDeferred;
736 }
737
738 // Called only by the standby monitoring code
739 public void setWhenStandbyDeferred(long now) {
740 whenStandbyDeferred = now;
741 }
742
Kweku Adams41cb3322019-06-26 14:42:26 -0700743 /**
744 * Returns the first time this job was force batched, in the elapsed realtime timebase. Will be
745 * 0 if this job was never force batched.
746 */
747 public long getFirstForceBatchedTimeElapsed() {
748 return mFirstForceBatchedTimeElapsed;
749 }
750
751 public void setFirstForceBatchedTimeElapsed(long now) {
752 mFirstForceBatchedTimeElapsed = now;
753 }
754
Dianne Hackborn1085ff62016-02-23 17:04:58 -0800755 public String getSourceTag() {
756 return sourceTag;
757 }
758
Christopher Tate7060b042014-06-09 19:50:00 -0700759 public int getUid() {
Shreyas Basarge8e64e2e2016-02-12 15:49:31 +0000760 return callingUid;
Christopher Tate7060b042014-06-09 19:50:00 -0700761 }
762
Dianne Hackborn1085ff62016-02-23 17:04:58 -0800763 public String getBatteryName() {
764 return batteryName;
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700765 }
766
767 public String getTag() {
768 return tag;
769 }
770
Shreyas Basarge5db09082016-01-07 13:38:29 +0000771 public int getPriority() {
772 return job.getPriority();
773 }
Christopher Tate7060b042014-06-09 19:50:00 -0700774
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600775 public int getFlags() {
776 return job.getFlags();
777 }
778
Makoto Onuki15407842018-01-19 14:23:11 -0800779 public int getInternalFlags() {
780 return mInternalFlags;
781 }
782
783 public void addInternalFlags(int flags) {
784 mInternalFlags |= flags;
785 }
786
Serik Beketayev75915d12018-08-01 16:56:59 -0700787 public int getSatisfiedConstraintFlags() {
788 return satisfiedConstraints;
789 }
790
Makoto Onuki15407842018-01-19 14:23:11 -0800791 public void maybeAddForegroundExemption(Predicate<Integer> uidForegroundChecker) {
792 // Jobs with time constraints shouldn't be exempted.
793 if (job.hasEarlyConstraint() || job.hasLateConstraint()) {
794 return;
795 }
796 // Already exempted, skip the foreground check.
797 if ((mInternalFlags & INTERNAL_FLAG_HAS_FOREGROUND_EXEMPTION) != 0) {
798 return;
799 }
800 if (uidForegroundChecker.test(getSourceUid())) {
801 addInternalFlags(INTERNAL_FLAG_HAS_FOREGROUND_EXEMPTION);
802 }
803 }
804
Jeff Sharkeycaa3f8d2017-10-25 16:12:00 -0600805 private void updateEstimatedNetworkBytesLocked() {
Kweku Adams316b1412019-06-14 11:34:37 -0700806 mTotalNetworkDownloadBytes = job.getEstimatedNetworkDownloadBytes();
807 mTotalNetworkUploadBytes = job.getEstimatedNetworkUploadBytes();
Jeff Sharkeycaa3f8d2017-10-25 16:12:00 -0600808
Jeff Sharkeycaa3f8d2017-10-25 16:12:00 -0600809 if (pendingWork != null) {
810 for (int i = 0; i < pendingWork.size(); i++) {
Kweku Adams316b1412019-06-14 11:34:37 -0700811 if (mTotalNetworkDownloadBytes != JobInfo.NETWORK_BYTES_UNKNOWN) {
812 // If any component of the job has unknown usage, we don't have a
813 // complete picture of what data will be used, and we have to treat the
814 // entire up/download as unknown.
815 long downloadBytes = pendingWork.get(i).getEstimatedNetworkDownloadBytes();
816 if (downloadBytes != JobInfo.NETWORK_BYTES_UNKNOWN) {
817 mTotalNetworkDownloadBytes += downloadBytes;
818 }
819 }
820 if (mTotalNetworkUploadBytes != JobInfo.NETWORK_BYTES_UNKNOWN) {
821 // If any component of the job has unknown usage, we don't have a
822 // complete picture of what data will be used, and we have to treat the
823 // entire up/download as unknown.
824 long uploadBytes = pendingWork.get(i).getEstimatedNetworkUploadBytes();
825 if (uploadBytes != JobInfo.NETWORK_BYTES_UNKNOWN) {
826 mTotalNetworkUploadBytes += uploadBytes;
827 }
Jeff Sharkeycaa3f8d2017-10-25 16:12:00 -0600828 }
829 }
830 }
Jeff Sharkeycaa3f8d2017-10-25 16:12:00 -0600831 }
832
Kweku Adams316b1412019-06-14 11:34:37 -0700833 public long getEstimatedNetworkDownloadBytes() {
834 return mTotalNetworkDownloadBytes;
835 }
836
837 public long getEstimatedNetworkUploadBytes() {
838 return mTotalNetworkUploadBytes;
Jeff Sharkeycaa3f8d2017-10-25 16:12:00 -0600839 }
840
Christopher Tate60977f42017-04-13 13:48:46 -0700841 /** Does this job have any sort of networking constraint? */
Christopher Tate7060b042014-06-09 19:50:00 -0700842 public boolean hasConnectivityConstraint() {
Kweku Adamsae9d6be2020-01-14 16:10:14 -0800843 // No need to check mDynamicConstraints since connectivity will only be in that list if
844 // it's already in the requiredConstraints list.
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800845 return (requiredConstraints&CONSTRAINT_CONNECTIVITY) != 0;
Christopher Tate7060b042014-06-09 19:50:00 -0700846 }
847
Christopher Tate7060b042014-06-09 19:50:00 -0700848 public boolean hasChargingConstraint() {
Kweku Adamsae9d6be2020-01-14 16:10:14 -0800849 return hasConstraint(CONSTRAINT_CHARGING);
Christopher Tate7060b042014-06-09 19:50:00 -0700850 }
851
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800852 public boolean hasBatteryNotLowConstraint() {
Kweku Adamsae9d6be2020-01-14 16:10:14 -0800853 return hasConstraint(CONSTRAINT_BATTERY_NOT_LOW);
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800854 }
855
Kweku Adamsae9d6be2020-01-14 16:10:14 -0800856 /** Returns true if the job requires charging OR battery not low. */
857 boolean hasPowerConstraint() {
858 return hasConstraint(CONSTRAINT_CHARGING | CONSTRAINT_BATTERY_NOT_LOW);
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800859 }
860
Dianne Hackborn532ea262017-03-17 17:50:55 -0700861 public boolean hasStorageNotLowConstraint() {
Kweku Adamsae9d6be2020-01-14 16:10:14 -0800862 return hasConstraint(CONSTRAINT_STORAGE_NOT_LOW);
Dianne Hackborn532ea262017-03-17 17:50:55 -0700863 }
864
Christopher Tate7060b042014-06-09 19:50:00 -0700865 public boolean hasTimingDelayConstraint() {
Kweku Adamsae9d6be2020-01-14 16:10:14 -0800866 return hasConstraint(CONSTRAINT_TIMING_DELAY);
Christopher Tate7060b042014-06-09 19:50:00 -0700867 }
868
869 public boolean hasDeadlineConstraint() {
Kweku Adamsae9d6be2020-01-14 16:10:14 -0800870 return hasConstraint(CONSTRAINT_DEADLINE);
Christopher Tate7060b042014-06-09 19:50:00 -0700871 }
872
873 public boolean hasIdleConstraint() {
Kweku Adamsae9d6be2020-01-14 16:10:14 -0800874 return hasConstraint(CONSTRAINT_IDLE);
Christopher Tate7060b042014-06-09 19:50:00 -0700875 }
876
Dianne Hackborn1a30bd92016-01-11 11:05:00 -0800877 public boolean hasContentTriggerConstraint() {
Kweku Adamsae9d6be2020-01-14 16:10:14 -0800878 // No need to check mDynamicConstraints since content trigger will only be in that list if
879 // it's already in the requiredConstraints list.
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800880 return (requiredConstraints&CONSTRAINT_CONTENT_TRIGGER) != 0;
Dianne Hackborn1a30bd92016-01-11 11:05:00 -0800881 }
882
Kweku Adamsae9d6be2020-01-14 16:10:14 -0800883 /**
884 * Checks both {@link #requiredConstraints} and {@link #mDynamicConstraints} to see if this job
885 * requires the specified constraint.
886 */
887 private boolean hasConstraint(int constraint) {
888 return (requiredConstraints & constraint) != 0 || (mDynamicConstraints & constraint) != 0;
889 }
890
Dianne Hackborn8db0fc12016-04-12 13:48:25 -0700891 public long getTriggerContentUpdateDelay() {
892 long time = job.getTriggerContentUpdateDelay();
893 if (time < 0) {
894 return DEFAULT_TRIGGER_UPDATE_DELAY;
895 }
896 return Math.max(time, MIN_TRIGGER_UPDATE_DELAY);
897 }
898
899 public long getTriggerContentMaxDelay() {
900 long time = job.getTriggerContentMaxDelay();
901 if (time < 0) {
902 return DEFAULT_TRIGGER_MAX_DELAY;
903 }
904 return Math.max(time, MIN_TRIGGER_MAX_DELAY);
905 }
906
Matthew Williams900c67f2014-07-09 12:46:53 -0700907 public boolean isPersisted() {
908 return job.isPersisted();
909 }
910
Christopher Tate7060b042014-06-09 19:50:00 -0700911 public long getEarliestRunTime() {
912 return earliestRunTimeElapsedMillis;
913 }
914
915 public long getLatestRunTimeElapsed() {
916 return latestRunTimeElapsedMillis;
917 }
918
Kweku Adamsc9793092019-04-30 11:54:48 -0700919 public long getOriginalLatestRunTimeElapsed() {
920 return mOriginalLatestRunTimeElapsedMillis;
921 }
922
923 public void setOriginalLatestRunTimeElapsed(long latestRunTimeElapsed) {
924 mOriginalLatestRunTimeElapsedMillis = latestRunTimeElapsed;
925 }
926
Jeff Sharkey9252b342018-01-19 07:58:35 +0900927 /**
928 * Return the fractional position of "now" within the "run time" window of
929 * this job.
930 * <p>
931 * For example, if the earliest run time was 10 minutes ago, and the latest
932 * run time is 30 minutes from now, this would return 0.25.
933 * <p>
934 * If the job has no window defined, returns 1. When only an earliest or
935 * latest time is defined, it's treated as an infinitely small window at
936 * that time.
937 */
938 public float getFractionRunTime() {
939 final long now = JobSchedulerService.sElapsedRealtimeClock.millis();
940 if (earliestRunTimeElapsedMillis == 0 && latestRunTimeElapsedMillis == Long.MAX_VALUE) {
941 return 1;
942 } else if (earliestRunTimeElapsedMillis == 0) {
943 return now >= latestRunTimeElapsedMillis ? 1 : 0;
944 } else if (latestRunTimeElapsedMillis == Long.MAX_VALUE) {
945 return now >= earliestRunTimeElapsedMillis ? 1 : 0;
946 } else {
947 if (now <= earliestRunTimeElapsedMillis) {
948 return 0;
949 } else if (now >= latestRunTimeElapsedMillis) {
950 return 1;
951 } else {
952 return (float) (now - earliestRunTimeElapsedMillis)
953 / (float) (latestRunTimeElapsedMillis - earliestRunTimeElapsedMillis);
954 }
955 }
956 }
957
Christopher Tate616541d2017-07-26 14:27:38 -0700958 public Pair<Long, Long> getPersistedUtcTimes() {
959 return mPersistedUtcTimes;
960 }
961
962 public void clearPersistedUtcTimes() {
963 mPersistedUtcTimes = null;
964 }
965
Kweku Adams4836f9d2018-11-12 17:04:17 -0800966 /** @return true if the constraint was changed, false otherwise. */
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800967 boolean setChargingConstraintSatisfied(boolean state) {
968 return setConstraintSatisfied(CONSTRAINT_CHARGING, state);
969 }
970
Kweku Adams4836f9d2018-11-12 17:04:17 -0800971 /** @return true if the constraint was changed, false otherwise. */
Dianne Hackborna06ec6a2017-02-13 10:08:42 -0800972 boolean setBatteryNotLowConstraintSatisfied(boolean state) {
973 return setConstraintSatisfied(CONSTRAINT_BATTERY_NOT_LOW, state);
974 }
975
Kweku Adams4836f9d2018-11-12 17:04:17 -0800976 /** @return true if the constraint was changed, false otherwise. */
Dianne Hackborn532ea262017-03-17 17:50:55 -0700977 boolean setStorageNotLowConstraintSatisfied(boolean state) {
978 return setConstraintSatisfied(CONSTRAINT_STORAGE_NOT_LOW, state);
979 }
980
Kweku Adams4836f9d2018-11-12 17:04:17 -0800981 /** @return true if the constraint was changed, false otherwise. */
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800982 boolean setTimingDelayConstraintSatisfied(boolean state) {
983 return setConstraintSatisfied(CONSTRAINT_TIMING_DELAY, state);
984 }
985
Kweku Adams4836f9d2018-11-12 17:04:17 -0800986 /** @return true if the constraint was changed, false otherwise. */
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800987 boolean setDeadlineConstraintSatisfied(boolean state) {
Kweku Adamsd14ce4d2018-10-19 11:40:55 -0700988 if (setConstraintSatisfied(CONSTRAINT_DEADLINE, state)) {
989 // The constraint was changed. Update the ready flag.
990 mReadyDeadlineSatisfied = !job.isPeriodic() && hasDeadlineConstraint() && state;
991 return true;
992 }
993 return false;
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800994 }
995
Kweku Adams4836f9d2018-11-12 17:04:17 -0800996 /** @return true if the constraint was changed, false otherwise. */
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800997 boolean setIdleConstraintSatisfied(boolean state) {
998 return setConstraintSatisfied(CONSTRAINT_IDLE, state);
999 }
1000
Kweku Adams4836f9d2018-11-12 17:04:17 -08001001 /** @return true if the constraint was changed, false otherwise. */
Jeff Sharkeyf07c7b92016-04-22 09:50:16 -06001002 boolean setConnectivityConstraintSatisfied(boolean state) {
1003 return setConstraintSatisfied(CONSTRAINT_CONNECTIVITY, state);
1004 }
1005
Kweku Adams4836f9d2018-11-12 17:04:17 -08001006 /** @return true if the constraint was changed, false otherwise. */
Dianne Hackbornb0001f62016-02-16 10:30:33 -08001007 boolean setContentTriggerConstraintSatisfied(boolean state) {
1008 return setConstraintSatisfied(CONSTRAINT_CONTENT_TRIGGER, state);
1009 }
1010
Kweku Adams4836f9d2018-11-12 17:04:17 -08001011 /** @return true if the constraint was changed, false otherwise. */
Dianne Hackborn7ab40252016-06-15 17:30:24 -07001012 boolean setDeviceNotDozingConstraintSatisfied(boolean state, boolean whitelisted) {
1013 dozeWhitelisted = whitelisted;
Kweku Adamsd14ce4d2018-10-19 11:40:55 -07001014 if (setConstraintSatisfied(CONSTRAINT_DEVICE_NOT_DOZING, state)) {
1015 // The constraint was changed. Update the ready flag.
1016 mReadyNotDozing = state || (job.getFlags() & JobInfo.FLAG_WILL_BE_FOREGROUND) != 0;
1017 return true;
1018 }
1019 return false;
Amith Yamasanicb926fc2016-03-14 17:15:20 -07001020 }
1021
Kweku Adams4836f9d2018-11-12 17:04:17 -08001022 /** @return true if the constraint was changed, false otherwise. */
Suprabh Shukla3ac1daa2017-07-14 12:15:27 -07001023 boolean setBackgroundNotRestrictedConstraintSatisfied(boolean state) {
Kweku Adamsd14ce4d2018-10-19 11:40:55 -07001024 if (setConstraintSatisfied(CONSTRAINT_BACKGROUND_NOT_RESTRICTED, state)) {
1025 // The constraint was changed. Update the ready flag.
1026 mReadyNotRestrictedInBg = state;
1027 return true;
1028 }
1029 return false;
Suprabh Shukla3ac1daa2017-07-14 12:15:27 -07001030 }
1031
Kweku Adams4836f9d2018-11-12 17:04:17 -08001032 /** @return true if the constraint was changed, false otherwise. */
1033 boolean setQuotaConstraintSatisfied(boolean state) {
1034 if (setConstraintSatisfied(CONSTRAINT_WITHIN_QUOTA, state)) {
1035 // The constraint was changed. Update the ready flag.
1036 mReadyWithinQuota = state;
1037 return true;
1038 }
1039 return false;
1040 }
1041
1042 /** @return true if the state was changed, false otherwise. */
Christopher Tate20afddd2018-02-28 15:19:19 -08001043 boolean setUidActive(final boolean newActiveState) {
1044 if (newActiveState != uidActive) {
1045 uidActive = newActiveState;
1046 return true;
1047 }
1048 return false; /* unchanged */
1049 }
1050
Kweku Adams4836f9d2018-11-12 17:04:17 -08001051 /** @return true if the constraint was changed, false otherwise. */
Dianne Hackbornb0001f62016-02-16 10:30:33 -08001052 boolean setConstraintSatisfied(int constraint, boolean state) {
1053 boolean old = (satisfiedConstraints&constraint) != 0;
1054 if (old == state) {
1055 return false;
1056 }
1057 satisfiedConstraints = (satisfiedConstraints&~constraint) | (state ? constraint : 0);
Kweku Adamsd14ce4d2018-10-19 11:40:55 -07001058 mSatisfiedConstraintsOfInterest = satisfiedConstraints & CONSTRAINTS_OF_INTEREST;
Kweku Adamsae9d6be2020-01-14 16:10:14 -08001059 mReadyDynamicSatisfied =
1060 mDynamicConstraints == (satisfiedConstraints & mDynamicConstraints);
Yao Chen26217e32019-04-04 16:34:24 -07001061 if (STATS_LOG_ENABLED && (STATSD_CONSTRAINTS_TO_LOG & constraint) != 0) {
Kweku Adams8845d012018-12-11 20:06:45 -08001062 StatsLog.write_non_chained(StatsLog.SCHEDULED_JOB_CONSTRAINT_CHANGED,
1063 sourceUid, null, getBatteryName(), getProtoConstraint(constraint),
1064 state ? StatsLog.SCHEDULED_JOB_CONSTRAINT_CHANGED__STATE__SATISFIED
1065 : StatsLog.SCHEDULED_JOB_CONSTRAINT_CHANGED__STATE__UNSATISFIED);
1066 }
Dianne Hackbornb0001f62016-02-16 10:30:33 -08001067 return true;
1068 }
1069
Dianne Hackborne9a988c2016-05-27 17:59:40 -07001070 boolean isConstraintSatisfied(int constraint) {
1071 return (satisfiedConstraints&constraint) != 0;
1072 }
1073
Dianne Hackbornf9bac162017-04-20 17:17:48 -07001074 boolean clearTrackingController(int which) {
1075 if ((trackingControllers&which) != 0) {
1076 trackingControllers &= ~which;
1077 return true;
1078 }
1079 return false;
1080 }
1081
1082 void setTrackingController(int which) {
1083 trackingControllers |= which;
1084 }
1085
Kweku Adamsae9d6be2020-01-14 16:10:14 -08001086 /**
1087 * Indicates that this job cannot run without the specified constraint. This is evaluated
1088 * separately from the job's explicitly requested constraints and MUST be satisfied before
1089 * the job can run if the app doesn't have quota.
1090 *
1091 */
1092 public void addDynamicConstraint(int constraint) {
1093 if (constraint == CONSTRAINT_WITHIN_QUOTA) {
1094 Slog.wtf(TAG, "Tried to set quota as a dynamic constraint");
1095 return;
1096 }
1097
1098 // Connectivity and content trigger are special since they're only valid to add if the
1099 // job has requested network or specific content URIs. Adding these constraints to jobs
1100 // that don't need them doesn't make sense.
1101 if ((constraint == CONSTRAINT_CONNECTIVITY && !hasConnectivityConstraint())
1102 || (constraint == CONSTRAINT_CONTENT_TRIGGER && !hasContentTriggerConstraint())) {
1103 return;
1104 }
1105
1106 mDynamicConstraints |= constraint;
1107 mReadyDynamicSatisfied =
1108 mDynamicConstraints == (satisfiedConstraints & mDynamicConstraints);
1109 }
1110
1111 /**
1112 * Removes a dynamic constraint from a job, meaning that the requirement is not required for
1113 * the job to run (if the job itself hasn't requested the constraint. This is separate from
1114 * the job's explicitly requested constraints and does not remove those requested constraints.
1115 *
1116 */
1117 public void removeDynamicConstraint(int constraint) {
1118 mDynamicConstraints &= ~constraint;
1119 mReadyDynamicSatisfied =
1120 mDynamicConstraints == (satisfiedConstraints & mDynamicConstraints);
1121 }
1122
Makoto Onukiab8a67f2017-06-20 12:20:34 -07001123 public long getLastSuccessfulRunTime() {
1124 return mLastSuccessfulRunTime;
1125 }
1126
1127 public long getLastFailedRunTime() {
1128 return mLastFailedRunTime;
1129 }
1130
Christopher Tate7060b042014-06-09 19:50:00 -07001131 /**
Kweku Adams32f44762018-11-02 16:58:32 -07001132 * @return Whether or not this job is ready to run, based on its requirements.
Christopher Tate7060b042014-06-09 19:50:00 -07001133 */
Dianne Hackbornd506b2b2016-02-16 10:30:33 -08001134 public boolean isReady() {
Kweku Adamscdbfcb92018-12-06 17:05:15 -08001135 return isReady(mSatisfiedConstraintsOfInterest);
1136 }
1137
1138 /**
1139 * @return Whether or not this job would be ready to run if it had the specified constraint
1140 * granted, based on its requirements.
1141 */
Kweku Adamsab8a9012019-01-28 15:42:09 -08001142 boolean wouldBeReadyWithConstraint(int constraint) {
1143 boolean oldValue = false;
1144 int satisfied = mSatisfiedConstraintsOfInterest;
1145 switch (constraint) {
1146 case CONSTRAINT_BACKGROUND_NOT_RESTRICTED:
1147 oldValue = mReadyNotRestrictedInBg;
1148 mReadyNotRestrictedInBg = true;
1149 break;
1150 case CONSTRAINT_DEADLINE:
1151 oldValue = mReadyDeadlineSatisfied;
1152 mReadyDeadlineSatisfied = true;
1153 break;
1154 case CONSTRAINT_DEVICE_NOT_DOZING:
1155 oldValue = mReadyNotDozing;
1156 mReadyNotDozing = true;
1157 break;
1158 case CONSTRAINT_WITHIN_QUOTA:
1159 oldValue = mReadyWithinQuota;
1160 mReadyWithinQuota = true;
1161 break;
1162 default:
1163 satisfied |= constraint;
Kweku Adamsae9d6be2020-01-14 16:10:14 -08001164 mReadyDynamicSatisfied =
1165 mDynamicConstraints == (satisfied & mDynamicConstraints);
Kweku Adamsab8a9012019-01-28 15:42:09 -08001166 break;
1167 }
1168
1169 boolean toReturn = isReady(satisfied);
1170
1171 switch (constraint) {
1172 case CONSTRAINT_BACKGROUND_NOT_RESTRICTED:
1173 mReadyNotRestrictedInBg = oldValue;
1174 break;
1175 case CONSTRAINT_DEADLINE:
1176 mReadyDeadlineSatisfied = oldValue;
1177 break;
1178 case CONSTRAINT_DEVICE_NOT_DOZING:
1179 mReadyNotDozing = oldValue;
1180 break;
1181 case CONSTRAINT_WITHIN_QUOTA:
1182 mReadyWithinQuota = oldValue;
1183 break;
Kweku Adamsae9d6be2020-01-14 16:10:14 -08001184 default:
1185 mReadyDynamicSatisfied =
1186 mDynamicConstraints == (satisfiedConstraints & mDynamicConstraints);
1187 break;
Kweku Adamsab8a9012019-01-28 15:42:09 -08001188 }
1189 return toReturn;
Kweku Adamscdbfcb92018-12-06 17:05:15 -08001190 }
1191
1192 private boolean isReady(int satisfiedConstraints) {
Kweku Adamsae9d6be2020-01-14 16:10:14 -08001193 // Quota and dynamic constraints trump all other constraints.
1194 if (!mReadyWithinQuota && !mReadyDynamicSatisfied) {
Kweku Adams4836f9d2018-11-12 17:04:17 -08001195 return false;
1196 }
Kweku Adamsae9d6be2020-01-14 16:10:14 -08001197 // Deadline constraint trumps other constraints besides quota and dynamic (except for
1198 // periodic jobs where deadline is an implementation detail. A periodic job should only
1199 // run if its constraints are satisfied).
Amith Yamasanicb926fc2016-03-14 17:15:20 -07001200 // DeviceNotDozing implicit constraint must be satisfied
Suprabh Shukla3ac1daa2017-07-14 12:15:27 -07001201 // NotRestrictedInBackground implicit constraint must be satisfied
Kweku Adamsd14ce4d2018-10-19 11:40:55 -07001202 return mReadyNotDozing && mReadyNotRestrictedInBg && (mReadyDeadlineSatisfied
Kweku Adamscdbfcb92018-12-06 17:05:15 -08001203 || isConstraintsSatisfied(satisfiedConstraints));
Matthew Williams03a4da62014-09-10 17:32:18 -07001204 }
1205
Kweku Adamsae9d6be2020-01-14 16:10:14 -08001206 /** All constraints besides implicit and deadline. */
Jeff Sharkeyd0fff2e2017-11-07 16:55:06 -07001207 static final int CONSTRAINTS_OF_INTEREST = CONSTRAINT_CHARGING | CONSTRAINT_BATTERY_NOT_LOW
1208 | CONSTRAINT_STORAGE_NOT_LOW | CONSTRAINT_TIMING_DELAY | CONSTRAINT_CONNECTIVITY
1209 | CONSTRAINT_IDLE | CONSTRAINT_CONTENT_TRIGGER;
Dianne Hackbornb0001f62016-02-16 10:30:33 -08001210
Christopher Tate5d346052016-03-08 12:56:08 -08001211 // Soft override covers all non-"functional" constraints
1212 static final int SOFT_OVERRIDE_CONSTRAINTS =
Dianne Hackborn532ea262017-03-17 17:50:55 -07001213 CONSTRAINT_CHARGING | CONSTRAINT_BATTERY_NOT_LOW | CONSTRAINT_STORAGE_NOT_LOW
Dianne Hackborna06ec6a2017-02-13 10:08:42 -08001214 | CONSTRAINT_TIMING_DELAY | CONSTRAINT_IDLE;
Christopher Tate5d346052016-03-08 12:56:08 -08001215
Matthew Williams03a4da62014-09-10 17:32:18 -07001216 /**
1217 * @return Whether the constraints set on this job are satisfied.
1218 */
Dianne Hackbornd506b2b2016-02-16 10:30:33 -08001219 public boolean isConstraintsSatisfied() {
Kweku Adamscdbfcb92018-12-06 17:05:15 -08001220 return isConstraintsSatisfied(mSatisfiedConstraintsOfInterest);
1221 }
1222
1223 private boolean isConstraintsSatisfied(int satisfiedConstraints) {
Christopher Tate5d346052016-03-08 12:56:08 -08001224 if (overrideState == OVERRIDE_FULL) {
1225 // force override: the job is always runnable
1226 return true;
1227 }
1228
Kweku Adamscdbfcb92018-12-06 17:05:15 -08001229 int sat = satisfiedConstraints;
Christopher Tate5d346052016-03-08 12:56:08 -08001230 if (overrideState == OVERRIDE_SOFT) {
1231 // override: pretend all 'soft' requirements are satisfied
1232 sat |= (requiredConstraints & SOFT_OVERRIDE_CONSTRAINTS);
1233 }
1234
Kweku Adamsd14ce4d2018-10-19 11:40:55 -07001235 return (sat & mRequiredConstraintsOfInterest) == mRequiredConstraintsOfInterest;
Christopher Tate7060b042014-06-09 19:50:00 -07001236 }
1237
1238 public boolean matches(int uid, int jobId) {
Shreyas Basarge8e64e2e2016-02-12 15:49:31 +00001239 return this.job.getId() == jobId && this.callingUid == uid;
Christopher Tate7060b042014-06-09 19:50:00 -07001240 }
1241
1242 @Override
1243 public String toString() {
Dianne Hackborna47223f2017-03-30 13:49:13 -07001244 StringBuilder sb = new StringBuilder(128);
1245 sb.append("JobStatus{");
1246 sb.append(Integer.toHexString(System.identityHashCode(this)));
1247 sb.append(" #");
1248 UserHandle.formatUid(sb, callingUid);
1249 sb.append("/");
1250 sb.append(job.getId());
1251 sb.append(' ');
1252 sb.append(batteryName);
1253 sb.append(" u=");
1254 sb.append(getUserId());
1255 sb.append(" s=");
1256 sb.append(getSourceUid());
1257 if (earliestRunTimeElapsedMillis != NO_EARLIEST_RUNTIME
1258 || latestRunTimeElapsedMillis != NO_LATEST_RUNTIME) {
Jeff Sharkeyd0fff2e2017-11-07 16:55:06 -07001259 long now = sElapsedRealtimeClock.millis();
Dianne Hackborna47223f2017-03-30 13:49:13 -07001260 sb.append(" TIME=");
Dianne Hackbornbfc23312017-05-11 11:53:24 -07001261 formatRunTime(sb, earliestRunTimeElapsedMillis, NO_EARLIEST_RUNTIME, now);
1262 sb.append(":");
1263 formatRunTime(sb, latestRunTimeElapsedMillis, NO_LATEST_RUNTIME, now);
Dianne Hackborna47223f2017-03-30 13:49:13 -07001264 }
Jeff Sharkeyd0fff2e2017-11-07 16:55:06 -07001265 if (job.getRequiredNetwork() != null) {
1266 sb.append(" NET");
Dianne Hackborna47223f2017-03-30 13:49:13 -07001267 }
1268 if (job.isRequireCharging()) {
1269 sb.append(" CHARGING");
1270 }
1271 if (job.isRequireBatteryNotLow()) {
1272 sb.append(" BATNOTLOW");
1273 }
1274 if (job.isRequireStorageNotLow()) {
1275 sb.append(" STORENOTLOW");
1276 }
1277 if (job.isRequireDeviceIdle()) {
1278 sb.append(" IDLE");
1279 }
Christopher Tate616541d2017-07-26 14:27:38 -07001280 if (job.isPeriodic()) {
1281 sb.append(" PERIODIC");
1282 }
Dianne Hackborna47223f2017-03-30 13:49:13 -07001283 if (job.isPersisted()) {
1284 sb.append(" PERSISTED");
1285 }
Dianne Hackborna47223f2017-03-30 13:49:13 -07001286 if ((satisfiedConstraints&CONSTRAINT_DEVICE_NOT_DOZING) == 0) {
1287 sb.append(" WAIT:DEV_NOT_DOZING");
1288 }
1289 if (job.getTriggerContentUris() != null) {
1290 sb.append(" URIS=");
1291 sb.append(Arrays.toString(job.getTriggerContentUris()));
1292 }
1293 if (numFailures != 0) {
1294 sb.append(" failures=");
1295 sb.append(numFailures);
1296 }
1297 if (isReady()) {
1298 sb.append(" READY");
1299 }
1300 sb.append("}");
1301 return sb.toString();
Christopher Tate7060b042014-06-09 19:50:00 -07001302 }
Matthew Williams9ae3dbe2014-08-21 13:47:47 -07001303
Dianne Hackbornbfc23312017-05-11 11:53:24 -07001304 private void formatRunTime(PrintWriter pw, long runtime, long defaultValue, long now) {
Matthew Williams9ae3dbe2014-08-21 13:47:47 -07001305 if (runtime == defaultValue) {
Dianne Hackbornbfc23312017-05-11 11:53:24 -07001306 pw.print("none");
Matthew Williams9ae3dbe2014-08-21 13:47:47 -07001307 } else {
Dianne Hackbornbfc23312017-05-11 11:53:24 -07001308 TimeUtils.formatDuration(runtime - now, pw);
1309 }
1310 }
1311
1312 private void formatRunTime(StringBuilder sb, long runtime, long defaultValue, long now) {
1313 if (runtime == defaultValue) {
1314 sb.append("none");
1315 } else {
1316 TimeUtils.formatDuration(runtime - now, sb);
Matthew Williams9ae3dbe2014-08-21 13:47:47 -07001317 }
1318 }
1319
1320 /**
1321 * Convenience function to identify a job uniquely without pulling all the data that
1322 * {@link #toString()} returns.
1323 */
1324 public String toShortString() {
Dianne Hackborn1a30bd92016-01-11 11:05:00 -08001325 StringBuilder sb = new StringBuilder();
1326 sb.append(Integer.toHexString(System.identityHashCode(this)));
Dianne Hackborne9a988c2016-05-27 17:59:40 -07001327 sb.append(" #");
1328 UserHandle.formatUid(sb, callingUid);
1329 sb.append("/");
Dianne Hackborn1a30bd92016-01-11 11:05:00 -08001330 sb.append(job.getId());
Dianne Hackborn970510b2016-02-24 16:56:42 -08001331 sb.append(' ');
Dianne Hackborne9a988c2016-05-27 17:59:40 -07001332 sb.append(batteryName);
1333 return sb.toString();
1334 }
1335
1336 /**
1337 * Convenience function to identify a job uniquely without pulling all the data that
1338 * {@link #toString()} returns.
1339 */
1340 public String toShortStringExceptUniqueId() {
1341 StringBuilder sb = new StringBuilder();
1342 sb.append(Integer.toHexString(System.identityHashCode(this)));
Dianne Hackborn1a30bd92016-01-11 11:05:00 -08001343 sb.append(' ');
Dianne Hackborn970510b2016-02-24 16:56:42 -08001344 sb.append(batteryName);
Dianne Hackborn1a30bd92016-01-11 11:05:00 -08001345 return sb.toString();
Matthew Williams9ae3dbe2014-08-21 13:47:47 -07001346 }
1347
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001348 /**
1349 * Convenience function to dump data that identifies a job uniquely to proto. This is intended
1350 * to mimic {@link #toShortString}.
1351 */
1352 public void writeToShortProto(ProtoOutputStream proto, long fieldId) {
1353 final long token = proto.start(fieldId);
1354
1355 proto.write(JobStatusShortInfoProto.CALLING_UID, callingUid);
1356 proto.write(JobStatusShortInfoProto.JOB_ID, job.getId());
1357 proto.write(JobStatusShortInfoProto.BATTERY_NAME, batteryName);
1358
1359 proto.end(token);
1360 }
1361
Dianne Hackbornb0001f62016-02-16 10:30:33 -08001362 void dumpConstraints(PrintWriter pw, int constraints) {
1363 if ((constraints&CONSTRAINT_CHARGING) != 0) {
1364 pw.print(" CHARGING");
1365 }
Dianne Hackborna06ec6a2017-02-13 10:08:42 -08001366 if ((constraints& CONSTRAINT_BATTERY_NOT_LOW) != 0) {
1367 pw.print(" BATTERY_NOT_LOW");
1368 }
Dianne Hackborn532ea262017-03-17 17:50:55 -07001369 if ((constraints& CONSTRAINT_STORAGE_NOT_LOW) != 0) {
1370 pw.print(" STORAGE_NOT_LOW");
1371 }
Dianne Hackbornb0001f62016-02-16 10:30:33 -08001372 if ((constraints&CONSTRAINT_TIMING_DELAY) != 0) {
1373 pw.print(" TIMING_DELAY");
1374 }
1375 if ((constraints&CONSTRAINT_DEADLINE) != 0) {
1376 pw.print(" DEADLINE");
1377 }
1378 if ((constraints&CONSTRAINT_IDLE) != 0) {
1379 pw.print(" IDLE");
1380 }
Jeff Sharkeyf07c7b92016-04-22 09:50:16 -06001381 if ((constraints&CONSTRAINT_CONNECTIVITY) != 0) {
1382 pw.print(" CONNECTIVITY");
1383 }
Dianne Hackbornb0001f62016-02-16 10:30:33 -08001384 if ((constraints&CONSTRAINT_CONTENT_TRIGGER) != 0) {
1385 pw.print(" CONTENT_TRIGGER");
1386 }
Amith Yamasanicb926fc2016-03-14 17:15:20 -07001387 if ((constraints&CONSTRAINT_DEVICE_NOT_DOZING) != 0) {
1388 pw.print(" DEVICE_NOT_DOZING");
1389 }
Makoto Onukidfede0a2018-01-22 15:11:34 -08001390 if ((constraints&CONSTRAINT_BACKGROUND_NOT_RESTRICTED) != 0) {
1391 pw.print(" BACKGROUND_NOT_RESTRICTED");
1392 }
Kweku Adams4836f9d2018-11-12 17:04:17 -08001393 if ((constraints & CONSTRAINT_WITHIN_QUOTA) != 0) {
1394 pw.print(" WITHIN_QUOTA");
1395 }
Makoto Onukidfede0a2018-01-22 15:11:34 -08001396 if (constraints != 0) {
1397 pw.print(" [0x");
1398 pw.print(Integer.toHexString(constraints));
1399 pw.print("]");
1400 }
Dianne Hackbornb0001f62016-02-16 10:30:33 -08001401 }
1402
Kweku Adams8845d012018-12-11 20:06:45 -08001403 /** Returns a {@link JobServerProtoEnums.Constraint} enum value for the given constraint. */
1404 private int getProtoConstraint(int constraint) {
1405 switch (constraint) {
1406 case CONSTRAINT_BACKGROUND_NOT_RESTRICTED:
1407 return JobServerProtoEnums.CONSTRAINT_BACKGROUND_NOT_RESTRICTED;
1408 case CONSTRAINT_BATTERY_NOT_LOW:
1409 return JobServerProtoEnums.CONSTRAINT_BATTERY_NOT_LOW;
1410 case CONSTRAINT_CHARGING:
1411 return JobServerProtoEnums.CONSTRAINT_CHARGING;
1412 case CONSTRAINT_CONNECTIVITY:
1413 return JobServerProtoEnums.CONSTRAINT_CONNECTIVITY;
1414 case CONSTRAINT_CONTENT_TRIGGER:
1415 return JobServerProtoEnums.CONSTRAINT_CONTENT_TRIGGER;
1416 case CONSTRAINT_DEADLINE:
1417 return JobServerProtoEnums.CONSTRAINT_DEADLINE;
1418 case CONSTRAINT_DEVICE_NOT_DOZING:
1419 return JobServerProtoEnums.CONSTRAINT_DEVICE_NOT_DOZING;
1420 case CONSTRAINT_IDLE:
1421 return JobServerProtoEnums.CONSTRAINT_IDLE;
1422 case CONSTRAINT_STORAGE_NOT_LOW:
1423 return JobServerProtoEnums.CONSTRAINT_STORAGE_NOT_LOW;
1424 case CONSTRAINT_TIMING_DELAY:
1425 return JobServerProtoEnums.CONSTRAINT_TIMING_DELAY;
1426 case CONSTRAINT_WITHIN_QUOTA:
1427 return JobServerProtoEnums.CONSTRAINT_WITHIN_QUOTA;
1428 default:
1429 return JobServerProtoEnums.CONSTRAINT_UNKNOWN;
1430 }
1431 }
1432
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001433 /** Writes constraints to the given repeating proto field. */
1434 void dumpConstraints(ProtoOutputStream proto, long fieldId, int constraints) {
1435 if ((constraints & CONSTRAINT_CHARGING) != 0) {
Kweku Adams8845d012018-12-11 20:06:45 -08001436 proto.write(fieldId, JobServerProtoEnums.CONSTRAINT_CHARGING);
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001437 }
1438 if ((constraints & CONSTRAINT_BATTERY_NOT_LOW) != 0) {
Kweku Adams8845d012018-12-11 20:06:45 -08001439 proto.write(fieldId, JobServerProtoEnums.CONSTRAINT_BATTERY_NOT_LOW);
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001440 }
1441 if ((constraints & CONSTRAINT_STORAGE_NOT_LOW) != 0) {
Kweku Adams8845d012018-12-11 20:06:45 -08001442 proto.write(fieldId, JobServerProtoEnums.CONSTRAINT_STORAGE_NOT_LOW);
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001443 }
1444 if ((constraints & CONSTRAINT_TIMING_DELAY) != 0) {
Kweku Adams8845d012018-12-11 20:06:45 -08001445 proto.write(fieldId, JobServerProtoEnums.CONSTRAINT_TIMING_DELAY);
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001446 }
1447 if ((constraints & CONSTRAINT_DEADLINE) != 0) {
Kweku Adams8845d012018-12-11 20:06:45 -08001448 proto.write(fieldId, JobServerProtoEnums.CONSTRAINT_DEADLINE);
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001449 }
1450 if ((constraints & CONSTRAINT_IDLE) != 0) {
Kweku Adams8845d012018-12-11 20:06:45 -08001451 proto.write(fieldId, JobServerProtoEnums.CONSTRAINT_IDLE);
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001452 }
1453 if ((constraints & CONSTRAINT_CONNECTIVITY) != 0) {
Kweku Adams8845d012018-12-11 20:06:45 -08001454 proto.write(fieldId, JobServerProtoEnums.CONSTRAINT_CONNECTIVITY);
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001455 }
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001456 if ((constraints & CONSTRAINT_CONTENT_TRIGGER) != 0) {
Kweku Adams8845d012018-12-11 20:06:45 -08001457 proto.write(fieldId, JobServerProtoEnums.CONSTRAINT_CONTENT_TRIGGER);
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001458 }
1459 if ((constraints & CONSTRAINT_DEVICE_NOT_DOZING) != 0) {
Kweku Adams8845d012018-12-11 20:06:45 -08001460 proto.write(fieldId, JobServerProtoEnums.CONSTRAINT_DEVICE_NOT_DOZING);
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001461 }
Kweku Adams4836f9d2018-11-12 17:04:17 -08001462 if ((constraints & CONSTRAINT_WITHIN_QUOTA) != 0) {
Kweku Adams8845d012018-12-11 20:06:45 -08001463 proto.write(fieldId, JobServerProtoEnums.CONSTRAINT_WITHIN_QUOTA);
1464 }
1465 if ((constraints & CONSTRAINT_BACKGROUND_NOT_RESTRICTED) != 0) {
1466 proto.write(fieldId, JobServerProtoEnums.CONSTRAINT_BACKGROUND_NOT_RESTRICTED);
Kweku Adams4836f9d2018-11-12 17:04:17 -08001467 }
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001468 }
1469
Dianne Hackborn342e6032017-04-13 18:04:31 -07001470 private void dumpJobWorkItem(PrintWriter pw, String prefix, JobWorkItem work, int index) {
1471 pw.print(prefix); pw.print(" #"); pw.print(index); pw.print(": #");
Dianne Hackborn28d1b662017-04-21 14:17:23 -07001472 pw.print(work.getWorkId()); pw.print(" "); pw.print(work.getDeliveryCount());
1473 pw.print("x "); pw.println(work.getIntent());
Dianne Hackborn342e6032017-04-13 18:04:31 -07001474 if (work.getGrants() != null) {
1475 pw.print(prefix); pw.println(" URI grants:");
1476 ((GrantedUriPermissions)work.getGrants()).dump(pw, prefix + " ");
1477 }
1478 }
1479
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001480 private void dumpJobWorkItem(ProtoOutputStream proto, long fieldId, JobWorkItem work) {
1481 final long token = proto.start(fieldId);
1482
1483 proto.write(JobStatusDumpProto.JobWorkItem.WORK_ID, work.getWorkId());
1484 proto.write(JobStatusDumpProto.JobWorkItem.DELIVERY_COUNT, work.getDeliveryCount());
1485 if (work.getIntent() != null) {
Jeffrey Huangcb782852019-12-05 11:28:11 -08001486 work.getIntent().dumpDebug(proto, JobStatusDumpProto.JobWorkItem.INTENT);
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001487 }
1488 Object grants = work.getGrants();
1489 if (grants != null) {
1490 ((GrantedUriPermissions) grants).dump(proto, JobStatusDumpProto.JobWorkItem.URI_GRANTS);
1491 }
1492
1493 proto.end(token);
1494 }
1495
Kweku Adams32f44762018-11-02 16:58:32 -07001496 /**
1497 * Returns a bucket name based on the normalized bucket indices, not the AppStandby constants.
1498 */
1499 String getBucketName() {
Kweku Adams4836f9d2018-11-12 17:04:17 -08001500 return bucketName(standbyBucket);
1501 }
1502
1503 /**
1504 * Returns a bucket name based on the normalized bucket indices, not the AppStandby constants.
1505 */
1506 static String bucketName(int standbyBucket) {
Kweku Adams32f44762018-11-02 16:58:32 -07001507 switch (standbyBucket) {
Christopher Tatea732f012017-10-26 17:26:53 -07001508 case 0: return "ACTIVE";
1509 case 1: return "WORKING_SET";
1510 case 2: return "FREQUENT";
1511 case 3: return "RARE";
1512 case 4: return "NEVER";
Kweku Adamsae9d6be2020-01-14 16:10:14 -08001513 case 5:
1514 return "RESTRICTED";
Christopher Tatea732f012017-10-26 17:26:53 -07001515 default:
Kweku Adams32f44762018-11-02 16:58:32 -07001516 return "Unknown: " + standbyBucket;
Christopher Tatea732f012017-10-26 17:26:53 -07001517 }
1518 }
1519
Christopher Tate7060b042014-06-09 19:50:00 -07001520 // Dumpsys infrastructure
Dianne Hackbornbfc23312017-05-11 11:53:24 -07001521 public void dump(PrintWriter pw, String prefix, boolean full, long elapsedRealtimeMillis) {
Shreyas Basarge8e64e2e2016-02-12 15:49:31 +00001522 pw.print(prefix); UserHandle.formatUid(pw, callingUid);
Dianne Hackborn1a30bd92016-01-11 11:05:00 -08001523 pw.print(" tag="); pw.println(tag);
Christopher Tatef973a7b2014-08-29 12:54:08 -07001524 pw.print(prefix);
Shreyas Basarged8bf6b92016-02-02 23:45:14 +00001525 pw.print("Source: uid="); UserHandle.formatUid(pw, getSourceUid());
1526 pw.print(" user="); pw.print(getSourceUserId());
1527 pw.print(" pkg="); pw.println(getSourcePackageName());
Dianne Hackborn970510b2016-02-24 16:56:42 -08001528 if (full) {
Dianne Hackborna47223f2017-03-30 13:49:13 -07001529 pw.print(prefix); pw.println("JobInfo:");
1530 pw.print(prefix); pw.print(" Service: ");
1531 pw.println(job.getService().flattenToShortString());
Dianne Hackborn970510b2016-02-24 16:56:42 -08001532 if (job.isPeriodic()) {
1533 pw.print(prefix); pw.print(" PERIODIC: interval=");
1534 TimeUtils.formatDuration(job.getIntervalMillis(), pw);
1535 pw.print(" flex="); TimeUtils.formatDuration(job.getFlexMillis(), pw);
1536 pw.println();
Dianne Hackborn1a30bd92016-01-11 11:05:00 -08001537 }
Dianne Hackborn970510b2016-02-24 16:56:42 -08001538 if (job.isPersisted()) {
1539 pw.print(prefix); pw.println(" PERSISTED");
1540 }
1541 if (job.getPriority() != 0) {
Makoto Onukiec8b14d2018-12-05 13:22:24 -08001542 pw.print(prefix); pw.print(" Priority: ");
1543 pw.println(JobInfo.getPriorityString(job.getPriority()));
Dianne Hackborn970510b2016-02-24 16:56:42 -08001544 }
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001545 if (job.getFlags() != 0) {
1546 pw.print(prefix); pw.print(" Flags: ");
1547 pw.println(Integer.toHexString(job.getFlags()));
1548 }
Makoto Onuki15407842018-01-19 14:23:11 -08001549 if (getInternalFlags() != 0) {
1550 pw.print(prefix); pw.print(" Internal flags: ");
1551 pw.print(Integer.toHexString(getInternalFlags()));
1552
1553 if ((getInternalFlags()&INTERNAL_FLAG_HAS_FOREGROUND_EXEMPTION) != 0) {
1554 pw.print(" HAS_FOREGROUND_EXEMPTION");
1555 }
1556 pw.println();
1557 }
Dianne Hackborn970510b2016-02-24 16:56:42 -08001558 pw.print(prefix); pw.print(" Requires: charging=");
Dianne Hackborna06ec6a2017-02-13 10:08:42 -08001559 pw.print(job.isRequireCharging()); pw.print(" batteryNotLow=");
1560 pw.print(job.isRequireBatteryNotLow()); pw.print(" deviceIdle=");
Dianne Hackborn970510b2016-02-24 16:56:42 -08001561 pw.println(job.isRequireDeviceIdle());
1562 if (job.getTriggerContentUris() != null) {
1563 pw.print(prefix); pw.println(" Trigger content URIs:");
1564 for (int i = 0; i < job.getTriggerContentUris().length; i++) {
1565 JobInfo.TriggerContentUri trig = job.getTriggerContentUris()[i];
1566 pw.print(prefix); pw.print(" ");
1567 pw.print(Integer.toHexString(trig.getFlags()));
1568 pw.print(' '); pw.println(trig.getUri());
1569 }
Dianne Hackborn8db0fc12016-04-12 13:48:25 -07001570 if (job.getTriggerContentUpdateDelay() >= 0) {
1571 pw.print(prefix); pw.print(" Trigger update delay: ");
1572 TimeUtils.formatDuration(job.getTriggerContentUpdateDelay(), pw);
1573 pw.println();
1574 }
1575 if (job.getTriggerContentMaxDelay() >= 0) {
1576 pw.print(prefix); pw.print(" Trigger max delay: ");
1577 TimeUtils.formatDuration(job.getTriggerContentMaxDelay(), pw);
1578 pw.println();
1579 }
Dianne Hackborn970510b2016-02-24 16:56:42 -08001580 }
Dianne Hackborna47223f2017-03-30 13:49:13 -07001581 if (job.getExtras() != null && !job.getExtras().maybeIsEmpty()) {
1582 pw.print(prefix); pw.print(" Extras: ");
1583 pw.println(job.getExtras().toShortString());
1584 }
1585 if (job.getTransientExtras() != null && !job.getTransientExtras().maybeIsEmpty()) {
1586 pw.print(prefix); pw.print(" Transient extras: ");
1587 pw.println(job.getTransientExtras().toShortString());
1588 }
1589 if (job.getClipData() != null) {
1590 pw.print(prefix); pw.print(" Clip data: ");
1591 StringBuilder b = new StringBuilder(128);
Makoto Onuki0e1ce972019-10-09 12:51:55 -07001592 b.append(job.getClipData());
Dianne Hackborna47223f2017-03-30 13:49:13 -07001593 pw.println(b);
1594 }
Dianne Hackborn342e6032017-04-13 18:04:31 -07001595 if (uriPerms != null) {
1596 pw.print(prefix); pw.println(" Granted URI permissions:");
1597 uriPerms.dump(pw, prefix + " ");
1598 }
Jeff Sharkeyd0fff2e2017-11-07 16:55:06 -07001599 if (job.getRequiredNetwork() != null) {
1600 pw.print(prefix); pw.print(" Network type: ");
1601 pw.println(job.getRequiredNetwork());
Dianne Hackborn970510b2016-02-24 16:56:42 -08001602 }
Kweku Adams316b1412019-06-14 11:34:37 -07001603 if (mTotalNetworkDownloadBytes != JobInfo.NETWORK_BYTES_UNKNOWN) {
1604 pw.print(prefix); pw.print(" Network download bytes: ");
1605 pw.println(mTotalNetworkDownloadBytes);
1606 }
1607 if (mTotalNetworkUploadBytes != JobInfo.NETWORK_BYTES_UNKNOWN) {
1608 pw.print(prefix); pw.print(" Network upload bytes: ");
1609 pw.println(mTotalNetworkUploadBytes);
Jeff Sharkeycaa3f8d2017-10-25 16:12:00 -06001610 }
Dianne Hackborn970510b2016-02-24 16:56:42 -08001611 if (job.getMinLatencyMillis() != 0) {
1612 pw.print(prefix); pw.print(" Minimum latency: ");
1613 TimeUtils.formatDuration(job.getMinLatencyMillis(), pw);
1614 pw.println();
1615 }
1616 if (job.getMaxExecutionDelayMillis() != 0) {
1617 pw.print(prefix); pw.print(" Max execution delay: ");
1618 TimeUtils.formatDuration(job.getMaxExecutionDelayMillis(), pw);
1619 pw.println();
1620 }
1621 pw.print(prefix); pw.print(" Backoff: policy="); pw.print(job.getBackoffPolicy());
1622 pw.print(" initial="); TimeUtils.formatDuration(job.getInitialBackoffMillis(), pw);
Dianne Hackborn1a30bd92016-01-11 11:05:00 -08001623 pw.println();
Dianne Hackborn970510b2016-02-24 16:56:42 -08001624 if (job.hasEarlyConstraint()) {
1625 pw.print(prefix); pw.println(" Has early constraint");
1626 }
1627 if (job.hasLateConstraint()) {
1628 pw.print(prefix); pw.println(" Has late constraint");
1629 }
Dianne Hackborn1a30bd92016-01-11 11:05:00 -08001630 }
Dianne Hackbornb0001f62016-02-16 10:30:33 -08001631 pw.print(prefix); pw.print("Required constraints:");
1632 dumpConstraints(pw, requiredConstraints);
1633 pw.println();
Kweku Adamsae9d6be2020-01-14 16:10:14 -08001634 pw.print(prefix);
1635 pw.print("Dynamic constraints:");
1636 dumpConstraints(pw, mDynamicConstraints);
1637 pw.println();
Dianne Hackborn970510b2016-02-24 16:56:42 -08001638 if (full) {
1639 pw.print(prefix); pw.print("Satisfied constraints:");
1640 dumpConstraints(pw, satisfiedConstraints);
1641 pw.println();
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001642 pw.print(prefix); pw.print("Unsatisfied constraints:");
Kweku Adams4836f9d2018-11-12 17:04:17 -08001643 dumpConstraints(pw,
1644 ((requiredConstraints | CONSTRAINT_WITHIN_QUOTA) & ~satisfiedConstraints));
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001645 pw.println();
Dianne Hackborn7ab40252016-06-15 17:30:24 -07001646 if (dozeWhitelisted) {
1647 pw.print(prefix); pw.println("Doze whitelisted: true");
1648 }
Christopher Tate20afddd2018-02-28 15:19:19 -08001649 if (uidActive) {
1650 pw.print(prefix); pw.println("Uid: active");
1651 }
Kweku Adams4836f9d2018-11-12 17:04:17 -08001652 if (job.isExemptedFromAppStandby()) {
1653 pw.print(prefix); pw.println("Is exempted from app standby");
1654 }
Dianne Hackborn970510b2016-02-24 16:56:42 -08001655 }
Dianne Hackbornf9bac162017-04-20 17:17:48 -07001656 if (trackingControllers != 0) {
1657 pw.print(prefix); pw.print("Tracking:");
1658 if ((trackingControllers&TRACKING_BATTERY) != 0) pw.print(" BATTERY");
1659 if ((trackingControllers&TRACKING_CONNECTIVITY) != 0) pw.print(" CONNECTIVITY");
1660 if ((trackingControllers&TRACKING_CONTENT) != 0) pw.print(" CONTENT");
1661 if ((trackingControllers&TRACKING_IDLE) != 0) pw.print(" IDLE");
1662 if ((trackingControllers&TRACKING_STORAGE) != 0) pw.print(" STORAGE");
1663 if ((trackingControllers&TRACKING_TIME) != 0) pw.print(" TIME");
Kweku Adams4836f9d2018-11-12 17:04:17 -08001664 if ((trackingControllers & TRACKING_QUOTA) != 0) pw.print(" QUOTA");
Dianne Hackbornf9bac162017-04-20 17:17:48 -07001665 pw.println();
1666 }
Kweku Adams32f44762018-11-02 16:58:32 -07001667
1668 pw.print(prefix); pw.println("Implicit constraints:");
1669 pw.print(prefix); pw.print(" readyNotDozing: ");
1670 pw.println(mReadyNotDozing);
1671 pw.print(prefix); pw.print(" readyNotRestrictedInBg: ");
1672 pw.println(mReadyNotRestrictedInBg);
1673 if (!job.isPeriodic() && hasDeadlineConstraint()) {
1674 pw.print(prefix); pw.print(" readyDeadlineSatisfied: ");
1675 pw.println(mReadyDeadlineSatisfied);
1676 }
Kweku Adamsae9d6be2020-01-14 16:10:14 -08001677 pw.print(prefix);
1678 pw.print(" readyDynamicSatisfied: ");
1679 pw.println(mReadyDynamicSatisfied);
Kweku Adams32f44762018-11-02 16:58:32 -07001680
Dianne Hackborn1a30bd92016-01-11 11:05:00 -08001681 if (changedAuthorities != null) {
1682 pw.print(prefix); pw.println("Changed authorities:");
1683 for (int i=0; i<changedAuthorities.size(); i++) {
1684 pw.print(prefix); pw.print(" "); pw.println(changedAuthorities.valueAt(i));
1685 }
Kweku Adamsc3f9ba62019-08-01 12:14:55 -07001686 }
1687 if (changedUris != null) {
1688 pw.print(prefix);
1689 pw.println("Changed URIs:");
1690 for (int i = 0; i < changedUris.size(); i++) {
1691 pw.print(prefix);
1692 pw.print(" ");
1693 pw.println(changedUris.valueAt(i));
Dianne Hackborn1a30bd92016-01-11 11:05:00 -08001694 }
1695 }
Jeff Sharkey76a02412017-10-24 16:55:04 -06001696 if (network != null) {
1697 pw.print(prefix); pw.print("Network: "); pw.println(network);
1698 }
Dianne Hackborn7da13d72017-04-04 17:17:35 -07001699 if (pendingWork != null && pendingWork.size() > 0) {
1700 pw.print(prefix); pw.println("Pending work:");
1701 for (int i = 0; i < pendingWork.size(); i++) {
Dianne Hackborn342e6032017-04-13 18:04:31 -07001702 dumpJobWorkItem(pw, prefix, pendingWork.get(i), i);
Dianne Hackborn7da13d72017-04-04 17:17:35 -07001703 }
1704 }
1705 if (executingWork != null && executingWork.size() > 0) {
1706 pw.print(prefix); pw.println("Executing work:");
1707 for (int i = 0; i < executingWork.size(); i++) {
Dianne Hackborn342e6032017-04-13 18:04:31 -07001708 dumpJobWorkItem(pw, prefix, executingWork.get(i), i);
Dianne Hackborn7da13d72017-04-04 17:17:35 -07001709 }
1710 }
Christopher Tatea732f012017-10-26 17:26:53 -07001711 pw.print(prefix); pw.print("Standby bucket: ");
Kweku Adams32f44762018-11-02 16:58:32 -07001712 pw.println(getBucketName());
Christopher Tated1aebb32018-01-31 13:24:14 -08001713 if (whenStandbyDeferred != 0) {
1714 pw.print(prefix); pw.print(" Deferred since: ");
1715 TimeUtils.formatDuration(whenStandbyDeferred, elapsedRealtimeMillis, pw);
1716 pw.println();
1717 }
Kweku Adams41cb3322019-06-26 14:42:26 -07001718 if (mFirstForceBatchedTimeElapsed != 0) {
1719 pw.print(prefix);
1720 pw.print(" Time since first force batch attempt: ");
1721 TimeUtils.formatDuration(mFirstForceBatchedTimeElapsed, elapsedRealtimeMillis, pw);
1722 pw.println();
1723 }
Dianne Hackbornbfc23312017-05-11 11:53:24 -07001724 pw.print(prefix); pw.print("Enqueue time: ");
1725 TimeUtils.formatDuration(enqueueTime, elapsedRealtimeMillis, pw);
1726 pw.println();
1727 pw.print(prefix); pw.print("Run time: earliest=");
1728 formatRunTime(pw, earliestRunTimeElapsedMillis, NO_EARLIEST_RUNTIME, elapsedRealtimeMillis);
1729 pw.print(", latest=");
1730 formatRunTime(pw, latestRunTimeElapsedMillis, NO_LATEST_RUNTIME, elapsedRealtimeMillis);
Kweku Adams966e72b2019-05-30 14:45:39 -07001731 pw.print(", original latest=");
1732 formatRunTime(pw, mOriginalLatestRunTimeElapsedMillis,
1733 NO_LATEST_RUNTIME, elapsedRealtimeMillis);
Dianne Hackbornbfc23312017-05-11 11:53:24 -07001734 pw.println();
Dianne Hackborn1a30bd92016-01-11 11:05:00 -08001735 if (numFailures != 0) {
1736 pw.print(prefix); pw.print("Num failures: "); pw.println(numFailures);
1737 }
Makoto Onukiab8a67f2017-06-20 12:20:34 -07001738 if (mLastSuccessfulRunTime != 0) {
1739 pw.print(prefix); pw.print("Last successful run: ");
Makoto Onuki0e1ce972019-10-09 12:51:55 -07001740 pw.println(formatTime(mLastSuccessfulRunTime));
Makoto Onukiab8a67f2017-06-20 12:20:34 -07001741 }
1742 if (mLastFailedRunTime != 0) {
1743 pw.print(prefix); pw.print("Last failed run: ");
Makoto Onuki0e1ce972019-10-09 12:51:55 -07001744 pw.println(formatTime(mLastFailedRunTime));
Makoto Onukiab8a67f2017-06-20 12:20:34 -07001745 }
Christopher Tate7060b042014-06-09 19:50:00 -07001746 }
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001747
Makoto Onuki0e1ce972019-10-09 12:51:55 -07001748 private static CharSequence formatTime(long time) {
1749 return DateFormat.format("yyyy-MM-dd HH:mm:ss", time);
1750 }
1751
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001752 public void dump(ProtoOutputStream proto, long fieldId, boolean full, long elapsedRealtimeMillis) {
1753 final long token = proto.start(fieldId);
1754
1755 proto.write(JobStatusDumpProto.CALLING_UID, callingUid);
1756 proto.write(JobStatusDumpProto.TAG, tag);
1757 proto.write(JobStatusDumpProto.SOURCE_UID, getSourceUid());
1758 proto.write(JobStatusDumpProto.SOURCE_USER_ID, getSourceUserId());
1759 proto.write(JobStatusDumpProto.SOURCE_PACKAGE_NAME, getSourcePackageName());
1760
1761 if (full) {
1762 final long jiToken = proto.start(JobStatusDumpProto.JOB_INFO);
1763
Jeffrey Huangcb782852019-12-05 11:28:11 -08001764 job.getService().dumpDebug(proto, JobStatusDumpProto.JobInfo.SERVICE);
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001765
1766 proto.write(JobStatusDumpProto.JobInfo.IS_PERIODIC, job.isPeriodic());
1767 proto.write(JobStatusDumpProto.JobInfo.PERIOD_INTERVAL_MS, job.getIntervalMillis());
1768 proto.write(JobStatusDumpProto.JobInfo.PERIOD_FLEX_MS, job.getFlexMillis());
1769
1770 proto.write(JobStatusDumpProto.JobInfo.IS_PERSISTED, job.isPersisted());
1771 proto.write(JobStatusDumpProto.JobInfo.PRIORITY, job.getPriority());
1772 proto.write(JobStatusDumpProto.JobInfo.FLAGS, job.getFlags());
Kweku Adamsc3f9ba62019-08-01 12:14:55 -07001773 proto.write(JobStatusDumpProto.INTERNAL_FLAGS, getInternalFlags());
1774 // Foreground exemption can be determined from internal flags value.
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001775
1776 proto.write(JobStatusDumpProto.JobInfo.REQUIRES_CHARGING, job.isRequireCharging());
1777 proto.write(JobStatusDumpProto.JobInfo.REQUIRES_BATTERY_NOT_LOW, job.isRequireBatteryNotLow());
1778 proto.write(JobStatusDumpProto.JobInfo.REQUIRES_DEVICE_IDLE, job.isRequireDeviceIdle());
1779
1780 if (job.getTriggerContentUris() != null) {
1781 for (int i = 0; i < job.getTriggerContentUris().length; i++) {
1782 final long tcuToken = proto.start(JobStatusDumpProto.JobInfo.TRIGGER_CONTENT_URIS);
1783 JobInfo.TriggerContentUri trig = job.getTriggerContentUris()[i];
1784
1785 proto.write(JobStatusDumpProto.JobInfo.TriggerContentUri.FLAGS, trig.getFlags());
1786 Uri u = trig.getUri();
1787 if (u != null) {
1788 proto.write(JobStatusDumpProto.JobInfo.TriggerContentUri.URI, u.toString());
1789 }
1790
1791 proto.end(tcuToken);
1792 }
1793 if (job.getTriggerContentUpdateDelay() >= 0) {
1794 proto.write(JobStatusDumpProto.JobInfo.TRIGGER_CONTENT_UPDATE_DELAY_MS,
1795 job.getTriggerContentUpdateDelay());
1796 }
1797 if (job.getTriggerContentMaxDelay() >= 0) {
1798 proto.write(JobStatusDumpProto.JobInfo.TRIGGER_CONTENT_MAX_DELAY_MS,
1799 job.getTriggerContentMaxDelay());
1800 }
1801 }
1802 if (job.getExtras() != null && !job.getExtras().maybeIsEmpty()) {
Jeffrey Huangcb782852019-12-05 11:28:11 -08001803 job.getExtras().dumpDebug(proto, JobStatusDumpProto.JobInfo.EXTRAS);
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001804 }
1805 if (job.getTransientExtras() != null && !job.getTransientExtras().maybeIsEmpty()) {
Jeffrey Huangcb782852019-12-05 11:28:11 -08001806 job.getTransientExtras().dumpDebug(proto, JobStatusDumpProto.JobInfo.TRANSIENT_EXTRAS);
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001807 }
1808 if (job.getClipData() != null) {
Jeffrey Huangcb782852019-12-05 11:28:11 -08001809 job.getClipData().dumpDebug(proto, JobStatusDumpProto.JobInfo.CLIP_DATA);
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001810 }
1811 if (uriPerms != null) {
1812 uriPerms.dump(proto, JobStatusDumpProto.JobInfo.GRANTED_URI_PERMISSIONS);
1813 }
1814 if (job.getRequiredNetwork() != null) {
Jeffrey Huangcb782852019-12-05 11:28:11 -08001815 job.getRequiredNetwork().dumpDebug(proto, JobStatusDumpProto.JobInfo.REQUIRED_NETWORK);
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001816 }
Kweku Adams316b1412019-06-14 11:34:37 -07001817 if (mTotalNetworkDownloadBytes != JobInfo.NETWORK_BYTES_UNKNOWN) {
1818 proto.write(JobStatusDumpProto.JobInfo.TOTAL_NETWORK_DOWNLOAD_BYTES,
1819 mTotalNetworkDownloadBytes);
1820 }
1821 if (mTotalNetworkUploadBytes != JobInfo.NETWORK_BYTES_UNKNOWN) {
1822 proto.write(JobStatusDumpProto.JobInfo.TOTAL_NETWORK_UPLOAD_BYTES,
1823 mTotalNetworkUploadBytes);
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001824 }
1825 proto.write(JobStatusDumpProto.JobInfo.MIN_LATENCY_MS, job.getMinLatencyMillis());
1826 proto.write(JobStatusDumpProto.JobInfo.MAX_EXECUTION_DELAY_MS, job.getMaxExecutionDelayMillis());
1827
1828 final long bpToken = proto.start(JobStatusDumpProto.JobInfo.BACKOFF_POLICY);
1829 proto.write(JobStatusDumpProto.JobInfo.Backoff.POLICY, job.getBackoffPolicy());
1830 proto.write(JobStatusDumpProto.JobInfo.Backoff.INITIAL_BACKOFF_MS,
1831 job.getInitialBackoffMillis());
1832 proto.end(bpToken);
1833
1834 proto.write(JobStatusDumpProto.JobInfo.HAS_EARLY_CONSTRAINT, job.hasEarlyConstraint());
1835 proto.write(JobStatusDumpProto.JobInfo.HAS_LATE_CONSTRAINT, job.hasLateConstraint());
1836
1837 proto.end(jiToken);
1838 }
1839
1840 dumpConstraints(proto, JobStatusDumpProto.REQUIRED_CONSTRAINTS, requiredConstraints);
Kweku Adamsae9d6be2020-01-14 16:10:14 -08001841 dumpConstraints(proto, JobStatusDumpProto.DYNAMIC_CONSTRAINTS, mDynamicConstraints);
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001842 if (full) {
1843 dumpConstraints(proto, JobStatusDumpProto.SATISFIED_CONSTRAINTS, satisfiedConstraints);
1844 dumpConstraints(proto, JobStatusDumpProto.UNSATISFIED_CONSTRAINTS,
Kweku Adams4836f9d2018-11-12 17:04:17 -08001845 ((requiredConstraints | CONSTRAINT_WITHIN_QUOTA) & ~satisfiedConstraints));
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001846 proto.write(JobStatusDumpProto.IS_DOZE_WHITELISTED, dozeWhitelisted);
Kweku Adams4836f9d2018-11-12 17:04:17 -08001847 proto.write(JobStatusDumpProto.IS_UID_ACTIVE, uidActive);
1848 proto.write(JobStatusDumpProto.IS_EXEMPTED_FROM_APP_STANDBY,
1849 job.isExemptedFromAppStandby());
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001850 }
1851
1852 // Tracking controllers
1853 if ((trackingControllers&TRACKING_BATTERY) != 0) {
1854 proto.write(JobStatusDumpProto.TRACKING_CONTROLLERS,
1855 JobStatusDumpProto.TRACKING_BATTERY);
1856 }
1857 if ((trackingControllers&TRACKING_CONNECTIVITY) != 0) {
1858 proto.write(JobStatusDumpProto.TRACKING_CONTROLLERS,
1859 JobStatusDumpProto.TRACKING_CONNECTIVITY);
1860 }
1861 if ((trackingControllers&TRACKING_CONTENT) != 0) {
1862 proto.write(JobStatusDumpProto.TRACKING_CONTROLLERS,
1863 JobStatusDumpProto.TRACKING_CONTENT);
1864 }
1865 if ((trackingControllers&TRACKING_IDLE) != 0) {
1866 proto.write(JobStatusDumpProto.TRACKING_CONTROLLERS,
1867 JobStatusDumpProto.TRACKING_IDLE);
1868 }
1869 if ((trackingControllers&TRACKING_STORAGE) != 0) {
1870 proto.write(JobStatusDumpProto.TRACKING_CONTROLLERS,
1871 JobStatusDumpProto.TRACKING_STORAGE);
1872 }
1873 if ((trackingControllers&TRACKING_TIME) != 0) {
1874 proto.write(JobStatusDumpProto.TRACKING_CONTROLLERS,
1875 JobStatusDumpProto.TRACKING_TIME);
1876 }
Kweku Adams4836f9d2018-11-12 17:04:17 -08001877 if ((trackingControllers & TRACKING_QUOTA) != 0) {
1878 proto.write(JobStatusDumpProto.TRACKING_CONTROLLERS,
1879 JobStatusDumpProto.TRACKING_QUOTA);
1880 }
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001881
Kweku Adams32f44762018-11-02 16:58:32 -07001882 // Implicit constraints
1883 final long icToken = proto.start(JobStatusDumpProto.IMPLICIT_CONSTRAINTS);
1884 proto.write(JobStatusDumpProto.ImplicitConstraints.IS_NOT_DOZING, mReadyNotDozing);
1885 proto.write(JobStatusDumpProto.ImplicitConstraints.IS_NOT_RESTRICTED_IN_BG,
1886 mReadyNotRestrictedInBg);
Kweku Adamsc3f9ba62019-08-01 12:14:55 -07001887 // mReadyDeadlineSatisfied isn't an implicit constraint...and can be determined from other
1888 // field values.
Kweku Adamsae9d6be2020-01-14 16:10:14 -08001889 proto.write(JobStatusDumpProto.ImplicitConstraints.IS_DYNAMIC_SATISFIED,
1890 mReadyDynamicSatisfied);
Kweku Adams32f44762018-11-02 16:58:32 -07001891 proto.end(icToken);
1892
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001893 if (changedAuthorities != null) {
1894 for (int k = 0; k < changedAuthorities.size(); k++) {
1895 proto.write(JobStatusDumpProto.CHANGED_AUTHORITIES, changedAuthorities.valueAt(k));
1896 }
1897 }
1898 if (changedUris != null) {
1899 for (int i = 0; i < changedUris.size(); i++) {
1900 Uri u = changedUris.valueAt(i);
1901 proto.write(JobStatusDumpProto.CHANGED_URIS, u.toString());
1902 }
1903 }
1904
1905 if (network != null) {
Jeffrey Huangcb782852019-12-05 11:28:11 -08001906 network.dumpDebug(proto, JobStatusDumpProto.NETWORK);
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001907 }
1908
Kweku Adamsc3f9ba62019-08-01 12:14:55 -07001909 if (pendingWork != null) {
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001910 for (int i = 0; i < pendingWork.size(); i++) {
1911 dumpJobWorkItem(proto, JobStatusDumpProto.PENDING_WORK, pendingWork.get(i));
1912 }
1913 }
Kweku Adamsc3f9ba62019-08-01 12:14:55 -07001914 if (executingWork != null) {
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001915 for (int i = 0; i < executingWork.size(); i++) {
1916 dumpJobWorkItem(proto, JobStatusDumpProto.EXECUTING_WORK, executingWork.get(i));
1917 }
1918 }
1919
1920 proto.write(JobStatusDumpProto.STANDBY_BUCKET, standbyBucket);
1921 proto.write(JobStatusDumpProto.ENQUEUE_DURATION_MS, elapsedRealtimeMillis - enqueueTime);
Kweku Adams41cb3322019-06-26 14:42:26 -07001922 proto.write(JobStatusDumpProto.TIME_SINCE_FIRST_DEFERRAL_MS,
1923 whenStandbyDeferred == 0 ? 0 : elapsedRealtimeMillis - whenStandbyDeferred);
1924 proto.write(JobStatusDumpProto.TIME_SINCE_FIRST_FORCE_BATCH_ATTEMPT_MS,
1925 mFirstForceBatchedTimeElapsed == 0
1926 ? 0 : elapsedRealtimeMillis - mFirstForceBatchedTimeElapsed);
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001927 if (earliestRunTimeElapsedMillis == NO_EARLIEST_RUNTIME) {
1928 proto.write(JobStatusDumpProto.TIME_UNTIL_EARLIEST_RUNTIME_MS, 0);
1929 } else {
1930 proto.write(JobStatusDumpProto.TIME_UNTIL_EARLIEST_RUNTIME_MS,
1931 earliestRunTimeElapsedMillis - elapsedRealtimeMillis);
1932 }
1933 if (latestRunTimeElapsedMillis == NO_LATEST_RUNTIME) {
1934 proto.write(JobStatusDumpProto.TIME_UNTIL_LATEST_RUNTIME_MS, 0);
1935 } else {
1936 proto.write(JobStatusDumpProto.TIME_UNTIL_LATEST_RUNTIME_MS,
1937 latestRunTimeElapsedMillis - elapsedRealtimeMillis);
1938 }
Kweku Adamsc3f9ba62019-08-01 12:14:55 -07001939 proto.write(JobStatusDumpProto.ORIGINAL_LATEST_RUNTIME_ELAPSED,
1940 mOriginalLatestRunTimeElapsedMillis);
Kweku Adams85f2fbc2017-12-18 12:04:12 -08001941
1942 proto.write(JobStatusDumpProto.NUM_FAILURES, numFailures);
1943 proto.write(JobStatusDumpProto.LAST_SUCCESSFUL_RUN_TIME, mLastSuccessfulRunTime);
1944 proto.write(JobStatusDumpProto.LAST_FAILED_RUN_TIME, mLastFailedRunTime);
1945
1946 proto.end(token);
1947 }
Christopher Tate7060b042014-06-09 19:50:00 -07001948}