Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.job.controllers; |
| 18 | |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 19 | import static com.android.server.job.JobSchedulerService.sElapsedRealtimeClock; |
| 20 | |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 21 | import android.app.AppGlobals; |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 22 | import android.app.IActivityManager; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 23 | import android.app.job.JobInfo; |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 24 | import android.app.job.JobWorkItem; |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 25 | import android.content.ClipData; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 26 | import android.content.ComponentName; |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 27 | import android.content.pm.PackageManagerInternal; |
Jeff Sharkey | 76a0241 | 2017-10-24 16:55:04 -0600 | [diff] [blame] | 28 | import android.net.Network; |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 29 | import android.net.Uri; |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 30 | import android.os.RemoteException; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 31 | import android.os.UserHandle; |
Makoto Onuki | ab8a67f | 2017-06-20 12:20:34 -0700 | [diff] [blame] | 32 | import android.text.format.Time; |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 33 | import android.util.ArraySet; |
Christopher Tate | 616541d | 2017-07-26 14:27:38 -0700 | [diff] [blame] | 34 | import android.util.Pair; |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 35 | import android.util.Slog; |
Kweku Adams | 8845d01 | 2018-12-11 20:06:45 -0800 | [diff] [blame] | 36 | import android.util.StatsLog; |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 37 | import android.util.TimeUtils; |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 38 | import android.util.proto.ProtoOutputStream; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 39 | |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 40 | import com.android.server.LocalServices; |
Dianne Hackborn | 342e603 | 2017-04-13 18:04:31 -0700 | [diff] [blame] | 41 | import com.android.server.job.GrantedUriPermissions; |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 42 | import com.android.server.job.JobSchedulerInternal; |
Christopher Tate | 616541d | 2017-07-26 14:27:38 -0700 | [diff] [blame] | 43 | import com.android.server.job.JobSchedulerService; |
Kweku Adams | 8845d01 | 2018-12-11 20:06:45 -0800 | [diff] [blame] | 44 | import com.android.server.job.JobServerProtoEnums; |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 45 | import com.android.server.job.JobStatusDumpProto; |
| 46 | import com.android.server.job.JobStatusShortInfoProto; |
Dianne Hackborn | 342e603 | 2017-04-13 18:04:31 -0700 | [diff] [blame] | 47 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 48 | import java.io.PrintWriter; |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 49 | import java.util.ArrayList; |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 50 | import java.util.Arrays; |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 51 | import java.util.function.Predicate; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * Uniquely identifies a job internally. |
| 55 | * Created from the public {@link android.app.job.JobInfo} object when it lands on the scheduler. |
| 56 | * Contains current state of the requirements of the job, as well as a function to evaluate |
| 57 | * whether it's ready to run. |
| 58 | * This object is shared among the various controllers - hence why the different fields are atomic. |
| 59 | * This isn't strictly necessary because each controller is only interested in a specific field, |
| 60 | * and the receivers that are listening for global state change will all run on the main looper, |
| 61 | * but we don't enforce that so this is safer. |
Kweku Adams | d14ce4d | 2018-10-19 11:40:55 -0700 | [diff] [blame] | 62 | * |
| 63 | * Test: atest com.android.server.job.controllers.JobStatusTest |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 64 | * @hide |
| 65 | */ |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 66 | public final class JobStatus { |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 67 | static final String TAG = "JobSchedulerService"; |
Christopher Tate | 616541d | 2017-07-26 14:27:38 -0700 | [diff] [blame] | 68 | static final boolean DEBUG = JobSchedulerService.DEBUG; |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 69 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 70 | public static final long NO_LATEST_RUNTIME = Long.MAX_VALUE; |
| 71 | public static final long NO_EARLIEST_RUNTIME = 0L; |
| 72 | |
Kweku Adams | 32f4476 | 2018-11-02 16:58:32 -0700 | [diff] [blame] | 73 | static final int CONSTRAINT_CHARGING = JobInfo.CONSTRAINT_FLAG_CHARGING; // 1 < 0 |
| 74 | static final int CONSTRAINT_IDLE = JobInfo.CONSTRAINT_FLAG_DEVICE_IDLE; // 1 << 2 |
| 75 | static final int CONSTRAINT_BATTERY_NOT_LOW = JobInfo.CONSTRAINT_FLAG_BATTERY_NOT_LOW; // 1 << 1 |
| 76 | static final int CONSTRAINT_STORAGE_NOT_LOW = JobInfo.CONSTRAINT_FLAG_STORAGE_NOT_LOW; // 1 << 3 |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 77 | static final int CONSTRAINT_TIMING_DELAY = 1<<31; |
| 78 | static final int CONSTRAINT_DEADLINE = 1<<30; |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 79 | static final int CONSTRAINT_CONNECTIVITY = 1<<28; |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 80 | static final int CONSTRAINT_CONTENT_TRIGGER = 1<<26; |
| 81 | static final int CONSTRAINT_DEVICE_NOT_DOZING = 1<<25; |
Kweku Adams | 4836f9d | 2018-11-12 17:04:17 -0800 | [diff] [blame] | 82 | static final int CONSTRAINT_WITHIN_QUOTA = 1 << 24; |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 83 | static final int CONSTRAINT_BACKGROUND_NOT_RESTRICTED = 1<<22; |
Christopher Tate | 60977f4 | 2017-04-13 13:48:46 -0700 | [diff] [blame] | 84 | |
Kweku Adams | 8845d01 | 2018-12-11 20:06:45 -0800 | [diff] [blame] | 85 | /** |
| 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 | |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 107 | // Soft override: ignore constraints like time that don't affect API availability |
| 108 | public static final int OVERRIDE_SOFT = 1; |
| 109 | // Full override: ignore all constraints including API-affecting like connectivity |
| 110 | public static final int OVERRIDE_FULL = 2; |
| 111 | |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 112 | /** If not specified, trigger update delay is 10 seconds. */ |
| 113 | public static final long DEFAULT_TRIGGER_UPDATE_DELAY = 10*1000; |
| 114 | |
| 115 | /** The minimum possible update delay is 1/2 second. */ |
| 116 | public static final long MIN_TRIGGER_UPDATE_DELAY = 500; |
| 117 | |
| 118 | /** If not specified, trigger maxumum delay is 2 minutes. */ |
| 119 | public static final long DEFAULT_TRIGGER_MAX_DELAY = 2*60*1000; |
| 120 | |
| 121 | /** The minimum possible update delay is 1 second. */ |
| 122 | public static final long MIN_TRIGGER_MAX_DELAY = 1000; |
| 123 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 124 | final JobInfo job; |
Christopher Tate | 72a0b2a | 2018-03-19 17:24:32 -0700 | [diff] [blame] | 125 | /** |
| 126 | * Uid of the package requesting this job. This can differ from the "source" |
| 127 | * uid when the job was scheduled on the app's behalf, such as with the jobs |
| 128 | * that underly Sync Manager operation. |
| 129 | */ |
Shreyas Basarge | 8e64e2e | 2016-02-12 15:49:31 +0000 | [diff] [blame] | 130 | final int callingUid; |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 131 | final int targetSdkVersion; |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 132 | final String batteryName; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 133 | |
Christopher Tate | 72a0b2a | 2018-03-19 17:24:32 -0700 | [diff] [blame] | 134 | /** |
| 135 | * Identity of the app in which the job is hosted. |
| 136 | */ |
Shreyas Basarge | 8e64e2e | 2016-02-12 15:49:31 +0000 | [diff] [blame] | 137 | final String sourcePackageName; |
| 138 | final int sourceUserId; |
| 139 | final int sourceUid; |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 140 | final String sourceTag; |
| 141 | |
| 142 | final String tag; |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 143 | |
Dianne Hackborn | 342e603 | 2017-04-13 18:04:31 -0700 | [diff] [blame] | 144 | private GrantedUriPermissions uriPerms; |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 145 | private boolean prepared; |
| 146 | |
Christopher Tate | b164f01 | 2017-04-26 15:41:37 -0700 | [diff] [blame] | 147 | static final boolean DEBUG_PREPARE = true; |
| 148 | private Throwable unpreparedPoint = null; |
| 149 | |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 150 | /** |
| 151 | * Earliest point in the future at which this job will be eligible to run. A value of 0 |
| 152 | * indicates there is no delay constraint. See {@link #hasTimingDelayConstraint()}. |
| 153 | */ |
| 154 | private final long earliestRunTimeElapsedMillis; |
| 155 | /** |
| 156 | * Latest point in the future at which this job must be run. A value of {@link Long#MAX_VALUE} |
| 157 | * indicates there is no deadline constraint. See {@link #hasDeadlineConstraint()}. |
| 158 | */ |
| 159 | private final long latestRunTimeElapsedMillis; |
| 160 | |
| 161 | /** How many times this job has failed, used to compute back-off. */ |
| 162 | private final int numFailures; |
| 163 | |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 164 | /** |
| 165 | * Current standby heartbeat when this job was scheduled or last ran. Used to |
| 166 | * pin the runnability check regardless of the job's app moving between buckets. |
| 167 | */ |
| 168 | private final long baseHeartbeat; |
| 169 | |
| 170 | /** |
| 171 | * Which app standby bucket this job's app is in. Updated when the app is moved to a |
| 172 | * different bucket. |
| 173 | */ |
| 174 | private int standbyBucket; |
| 175 | |
| 176 | /** |
| 177 | * Debugging: timestamp if we ever defer this job based on standby bucketing, this |
| 178 | * is when we did so. |
| 179 | */ |
| 180 | private long whenStandbyDeferred; |
| 181 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 182 | // Constraints. |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 183 | final int requiredConstraints; |
Kweku Adams | d14ce4d | 2018-10-19 11:40:55 -0700 | [diff] [blame] | 184 | private final int mRequiredConstraintsOfInterest; |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 185 | int satisfiedConstraints = 0; |
Kweku Adams | d14ce4d | 2018-10-19 11:40:55 -0700 | [diff] [blame] | 186 | private int mSatisfiedConstraintsOfInterest = 0; |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 187 | |
Dianne Hackborn | 7ab4025 | 2016-06-15 17:30:24 -0700 | [diff] [blame] | 188 | // Set to true if doze constraint was satisfied due to app being whitelisted. |
| 189 | public boolean dozeWhitelisted; |
| 190 | |
Christopher Tate | 20afddd | 2018-02-28 15:19:19 -0800 | [diff] [blame] | 191 | // Set to true when the app is "active" per AppStateTracker |
| 192 | public boolean uidActive; |
| 193 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 194 | /** |
| 195 | * Flag for {@link #trackingControllers}: the battery controller is currently tracking this job. |
| 196 | */ |
| 197 | public static final int TRACKING_BATTERY = 1<<0; |
| 198 | /** |
| 199 | * Flag for {@link #trackingControllers}: the network connectivity controller is currently |
| 200 | * tracking this job. |
| 201 | */ |
| 202 | public static final int TRACKING_CONNECTIVITY = 1<<1; |
| 203 | /** |
| 204 | * Flag for {@link #trackingControllers}: the content observer controller is currently |
| 205 | * tracking this job. |
| 206 | */ |
| 207 | public static final int TRACKING_CONTENT = 1<<2; |
| 208 | /** |
| 209 | * Flag for {@link #trackingControllers}: the idle controller is currently tracking this job. |
| 210 | */ |
| 211 | public static final int TRACKING_IDLE = 1<<3; |
| 212 | /** |
| 213 | * Flag for {@link #trackingControllers}: the storage controller is currently tracking this job. |
| 214 | */ |
| 215 | public static final int TRACKING_STORAGE = 1<<4; |
| 216 | /** |
| 217 | * Flag for {@link #trackingControllers}: the time controller is currently tracking this job. |
| 218 | */ |
| 219 | public static final int TRACKING_TIME = 1<<5; |
Kweku Adams | 4836f9d | 2018-11-12 17:04:17 -0800 | [diff] [blame] | 220 | /** |
| 221 | * Flag for {@link #trackingControllers}: the quota controller is currently tracking this job. |
| 222 | */ |
| 223 | public static final int TRACKING_QUOTA = 1 << 6; |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 224 | |
| 225 | /** |
| 226 | * Bit mask of controllers that are currently tracking the job. |
| 227 | */ |
| 228 | private int trackingControllers; |
| 229 | |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 230 | /** |
| 231 | * Flag for {@link #mInternalFlags}: this job was scheduled when the app that owns the job |
| 232 | * service (not necessarily the caller) was in the foreground and the job has no time |
| 233 | * constraints, which makes it exempted from the battery saver job restriction. |
| 234 | * |
| 235 | * @hide |
| 236 | */ |
| 237 | public static final int INTERNAL_FLAG_HAS_FOREGROUND_EXEMPTION = 1 << 0; |
| 238 | |
| 239 | /** |
| 240 | * Versatile, persistable flags for a job that's updated within the system server, |
| 241 | * as opposed to {@link JobInfo#flags} that's set by callers. |
| 242 | */ |
| 243 | private int mInternalFlags; |
| 244 | |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 245 | // These are filled in by controllers when preparing for execution. |
| 246 | public ArraySet<Uri> changedUris; |
| 247 | public ArraySet<String> changedAuthorities; |
Jeff Sharkey | 76a0241 | 2017-10-24 16:55:04 -0600 | [diff] [blame] | 248 | public Network network; |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 249 | |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 250 | public int lastEvaluatedPriority; |
| 251 | |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 252 | // If non-null, this is work that has been enqueued for the job. |
| 253 | public ArrayList<JobWorkItem> pendingWork; |
| 254 | |
| 255 | // If non-null, this is work that is currently being executed. |
| 256 | public ArrayList<JobWorkItem> executingWork; |
| 257 | |
| 258 | public int nextPendingWorkId = 1; |
| 259 | |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 260 | // Used by shell commands |
| 261 | public int overrideState = 0; |
| 262 | |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 263 | // When this job was enqueued, for ordering. (in elapsedRealtimeMillis) |
| 264 | public long enqueueTime; |
| 265 | |
| 266 | // Metrics about queue latency. (in uptimeMillis) |
Christopher Tate | 7234fc6 | 2017-04-03 17:36:07 -0700 | [diff] [blame] | 267 | public long madePending; |
| 268 | public long madeActive; |
| 269 | |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 270 | /** |
Makoto Onuki | ab8a67f | 2017-06-20 12:20:34 -0700 | [diff] [blame] | 271 | * Last time a job finished successfully for a periodic job, in the currentTimeMillis time, |
| 272 | * for dumpsys. |
| 273 | */ |
| 274 | private long mLastSuccessfulRunTime; |
| 275 | |
| 276 | /** |
| 277 | * Last time a job finished unsuccessfully, in the currentTimeMillis time, for dumpsys. |
| 278 | */ |
| 279 | private long mLastFailedRunTime; |
| 280 | |
| 281 | /** |
Christopher Tate | 616541d | 2017-07-26 14:27:38 -0700 | [diff] [blame] | 282 | * Transient: when a job is inflated from disk before we have a reliable RTC clock time, |
| 283 | * we retain the canonical (delay, deadline) scheduling tuple read out of the persistent |
| 284 | * store in UTC so that we can fix up the job's scheduling criteria once we get a good |
| 285 | * wall-clock time. If we have to persist the job again before the clock has been updated, |
| 286 | * we record these times again rather than calculating based on the earliest/latest elapsed |
| 287 | * time base figures. |
| 288 | * |
| 289 | * 'first' is the earliest/delay time, and 'second' is the latest/deadline time. |
| 290 | */ |
| 291 | private Pair<Long, Long> mPersistedUtcTimes; |
| 292 | |
| 293 | /** |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 294 | * For use only by ContentObserverController: state it is maintaining about content URIs |
| 295 | * being observed. |
| 296 | */ |
| 297 | ContentObserverController.JobInstance contentObserverJobInstance; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 298 | |
Jeff Sharkey | caa3f8d | 2017-10-25 16:12:00 -0600 | [diff] [blame] | 299 | private long totalNetworkBytes = JobInfo.NETWORK_BYTES_UNKNOWN; |
| 300 | |
Kweku Adams | d14ce4d | 2018-10-19 11:40:55 -0700 | [diff] [blame] | 301 | /////// Booleans that track if a job is ready to run. They should be updated whenever dependent |
| 302 | /////// states change. |
| 303 | |
| 304 | /** |
| 305 | * The deadline for the job has passed. This is only good for non-periodic jobs. A periodic job |
| 306 | * should only run if its constraints are satisfied. |
| 307 | * Computed as: NOT periodic AND has deadline constraint AND deadline constraint satisfied. |
| 308 | */ |
| 309 | private boolean mReadyDeadlineSatisfied; |
| 310 | |
| 311 | /** |
| 312 | * The device isn't Dozing or this job will be in the foreground. This implicit constraint must |
| 313 | * be satisfied. |
| 314 | */ |
| 315 | private boolean mReadyNotDozing; |
| 316 | |
| 317 | /** |
| 318 | * The job is not restricted from running in the background (due to Battery Saver). This |
| 319 | * implicit constraint must be satisfied. |
| 320 | */ |
| 321 | private boolean mReadyNotRestrictedInBg; |
| 322 | |
Kweku Adams | 4836f9d | 2018-11-12 17:04:17 -0800 | [diff] [blame] | 323 | /** The job is within its quota based on its standby bucket. */ |
| 324 | private boolean mReadyWithinQuota; |
| 325 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 326 | /** Provide a handle to the service that this job will be run on. */ |
| 327 | public int getServiceToken() { |
Shreyas Basarge | 8e64e2e | 2016-02-12 15:49:31 +0000 | [diff] [blame] | 328 | return callingUid; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Christopher Tate | 72a0b2a | 2018-03-19 17:24:32 -0700 | [diff] [blame] | 331 | /** |
| 332 | * Core constructor for JobStatus instances. All other ctors funnel down to this one. |
| 333 | * |
| 334 | * @param job The actual requested parameters for the job |
| 335 | * @param callingUid Identity of the app that is scheduling the job. This may not be the |
| 336 | * app in which the job is implemented; such as with sync jobs. |
| 337 | * @param targetSdkVersion The targetSdkVersion of the app in which the job will run. |
| 338 | * @param sourcePackageName The package name of the app in which the job will run. |
| 339 | * @param sourceUserId The user in which the job will run |
| 340 | * @param standbyBucket The standby bucket that the source package is currently assigned to, |
| 341 | * cached here for speed of handling during runnability evaluations (and updated when bucket |
| 342 | * assignments are changed) |
| 343 | * @param heartbeat Timestamp of when the job was created, in the standby-related |
| 344 | * timebase. |
| 345 | * @param tag A string associated with the job for debugging/logging purposes. |
| 346 | * @param numFailures Count of how many times this job has requested a reschedule because |
| 347 | * its work was not yet finished. |
| 348 | * @param earliestRunTimeElapsedMillis Milestone: earliest point in time at which the job |
| 349 | * is to be considered runnable |
| 350 | * @param latestRunTimeElapsedMillis Milestone: point in time at which the job will be |
| 351 | * considered overdue |
| 352 | * @param lastSuccessfulRunTime When did we last run this job to completion? |
| 353 | * @param lastFailedRunTime When did we last run this job only to have it stop incomplete? |
| 354 | * @param internalFlags Non-API property flags about this job |
| 355 | */ |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 356 | private JobStatus(JobInfo job, int callingUid, int targetSdkVersion, String sourcePackageName, |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 357 | int sourceUserId, int standbyBucket, long heartbeat, String tag, int numFailures, |
| 358 | long earliestRunTimeElapsedMillis, long latestRunTimeElapsedMillis, |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 359 | long lastSuccessfulRunTime, long lastFailedRunTime, int internalFlags) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 360 | this.job = job; |
Shreyas Basarge | 8e64e2e | 2016-02-12 15:49:31 +0000 | [diff] [blame] | 361 | this.callingUid = callingUid; |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 362 | this.targetSdkVersion = targetSdkVersion; |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 363 | this.standbyBucket = standbyBucket; |
| 364 | this.baseHeartbeat = heartbeat; |
Shreyas Basarge | 8e64e2e | 2016-02-12 15:49:31 +0000 | [diff] [blame] | 365 | |
| 366 | int tempSourceUid = -1; |
| 367 | if (sourceUserId != -1 && sourcePackageName != null) { |
| 368 | try { |
| 369 | tempSourceUid = AppGlobals.getPackageManager().getPackageUid(sourcePackageName, 0, |
| 370 | sourceUserId); |
| 371 | } catch (RemoteException ex) { |
| 372 | // Can't happen, PackageManager runs in the same process. |
| 373 | } |
| 374 | } |
| 375 | if (tempSourceUid == -1) { |
| 376 | this.sourceUid = callingUid; |
| 377 | this.sourceUserId = UserHandle.getUserId(callingUid); |
| 378 | this.sourcePackageName = job.getService().getPackageName(); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 379 | this.sourceTag = null; |
Shreyas Basarge | 8e64e2e | 2016-02-12 15:49:31 +0000 | [diff] [blame] | 380 | } else { |
| 381 | this.sourceUid = tempSourceUid; |
| 382 | this.sourceUserId = sourceUserId; |
| 383 | this.sourcePackageName = sourcePackageName; |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 384 | this.sourceTag = tag; |
Shreyas Basarge | 8e64e2e | 2016-02-12 15:49:31 +0000 | [diff] [blame] | 385 | } |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 386 | |
Shreyas Basarge | eda34e4 | 2016-04-26 00:14:02 +0100 | [diff] [blame] | 387 | this.batteryName = this.sourceTag != null |
| 388 | ? this.sourceTag + ":" + job.getService().getPackageName() |
| 389 | : job.getService().flattenToShortString(); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 390 | this.tag = "*job*/" + this.batteryName; |
| 391 | |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 392 | this.earliestRunTimeElapsedMillis = earliestRunTimeElapsedMillis; |
| 393 | this.latestRunTimeElapsedMillis = latestRunTimeElapsedMillis; |
| 394 | this.numFailures = numFailures; |
| 395 | |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 396 | int requiredConstraints = job.getConstraintFlags(); |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 397 | if (job.getRequiredNetwork() != null) { |
| 398 | requiredConstraints |= CONSTRAINT_CONNECTIVITY; |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 399 | } |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 400 | if (earliestRunTimeElapsedMillis != NO_EARLIEST_RUNTIME) { |
| 401 | requiredConstraints |= CONSTRAINT_TIMING_DELAY; |
| 402 | } |
| 403 | if (latestRunTimeElapsedMillis != NO_LATEST_RUNTIME) { |
| 404 | requiredConstraints |= CONSTRAINT_DEADLINE; |
| 405 | } |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 406 | if (job.getTriggerContentUris() != null) { |
| 407 | requiredConstraints |= CONSTRAINT_CONTENT_TRIGGER; |
| 408 | } |
| 409 | this.requiredConstraints = requiredConstraints; |
Kweku Adams | d14ce4d | 2018-10-19 11:40:55 -0700 | [diff] [blame] | 410 | mRequiredConstraintsOfInterest = requiredConstraints & CONSTRAINTS_OF_INTEREST; |
| 411 | mReadyNotDozing = (job.getFlags() & JobInfo.FLAG_WILL_BE_FOREGROUND) != 0; |
Makoto Onuki | ab8a67f | 2017-06-20 12:20:34 -0700 | [diff] [blame] | 412 | |
| 413 | mLastSuccessfulRunTime = lastSuccessfulRunTime; |
| 414 | mLastFailedRunTime = lastFailedRunTime; |
Jeff Sharkey | caa3f8d | 2017-10-25 16:12:00 -0600 | [diff] [blame] | 415 | |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 416 | mInternalFlags = internalFlags; |
| 417 | |
Jeff Sharkey | caa3f8d | 2017-10-25 16:12:00 -0600 | [diff] [blame] | 418 | updateEstimatedNetworkBytesLocked(); |
Jeff Sharkey | fb0f971 | 2018-02-21 20:27:48 -0700 | [diff] [blame] | 419 | |
| 420 | if (job.getRequiredNetwork() != null) { |
| 421 | // Later, when we check if a given network satisfies the required |
| 422 | // network, we need to know the UID that is requesting it, so push |
| 423 | // our source UID into place. |
| 424 | job.getRequiredNetwork().networkCapabilities.setSingleUid(this.sourceUid); |
| 425 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Christopher Tate | 616541d | 2017-07-26 14:27:38 -0700 | [diff] [blame] | 428 | /** Copy constructor: used specifically when cloning JobStatus objects for persistence, |
| 429 | * so we preserve RTC window bounds if the source object has them. */ |
Matthew Williams | 0cc7654 | 2015-10-16 21:04:51 -0700 | [diff] [blame] | 430 | public JobStatus(JobStatus jobStatus) { |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 431 | this(jobStatus.getJob(), jobStatus.getUid(), jobStatus.targetSdkVersion, |
Dianne Hackborn | d506b2b | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 432 | jobStatus.getSourcePackageName(), jobStatus.getSourceUserId(), |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 433 | jobStatus.getStandbyBucket(), jobStatus.getBaseHeartbeat(), |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 434 | jobStatus.getSourceTag(), jobStatus.getNumFailures(), |
Makoto Onuki | ab8a67f | 2017-06-20 12:20:34 -0700 | [diff] [blame] | 435 | jobStatus.getEarliestRunTime(), jobStatus.getLatestRunTimeElapsed(), |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 436 | jobStatus.getLastSuccessfulRunTime(), jobStatus.getLastFailedRunTime(), |
| 437 | jobStatus.getInternalFlags()); |
Christopher Tate | 616541d | 2017-07-26 14:27:38 -0700 | [diff] [blame] | 438 | mPersistedUtcTimes = jobStatus.mPersistedUtcTimes; |
| 439 | if (jobStatus.mPersistedUtcTimes != null) { |
| 440 | if (DEBUG) { |
| 441 | Slog.i(TAG, "Cloning job with persisted run times", new RuntimeException("here")); |
| 442 | } |
| 443 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /** |
| 447 | * Create a new JobStatus that was loaded from disk. We ignore the provided |
| 448 | * {@link android.app.job.JobInfo} time criteria because we can load a persisted periodic job |
| 449 | * from the {@link com.android.server.job.JobStore} and still want to respect its |
| 450 | * wallclock runtime rather than resetting it on every boot. |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 451 | * We consider a freshly loaded job to no longer be in back-off, and the associated |
| 452 | * standby bucket is whatever the OS thinks it should be at this moment. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 453 | */ |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 454 | public JobStatus(JobInfo job, int callingUid, String sourcePkgName, int sourceUserId, |
| 455 | int standbyBucket, long baseHeartbeat, String sourceTag, |
| 456 | long earliestRunTimeElapsedMillis, long latestRunTimeElapsedMillis, |
Christopher Tate | 616541d | 2017-07-26 14:27:38 -0700 | [diff] [blame] | 457 | long lastSuccessfulRunTime, long lastFailedRunTime, |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 458 | Pair<Long, Long> persistedExecutionTimesUTC, |
| 459 | int innerFlags) { |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 460 | this(job, callingUid, resolveTargetSdkVersion(job), sourcePkgName, sourceUserId, |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 461 | standbyBucket, baseHeartbeat, |
| 462 | sourceTag, 0, |
Makoto Onuki | ab8a67f | 2017-06-20 12:20:34 -0700 | [diff] [blame] | 463 | earliestRunTimeElapsedMillis, latestRunTimeElapsedMillis, |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 464 | lastSuccessfulRunTime, lastFailedRunTime, innerFlags); |
Christopher Tate | 616541d | 2017-07-26 14:27:38 -0700 | [diff] [blame] | 465 | |
| 466 | // Only during initial inflation do we record the UTC-timebase execution bounds |
| 467 | // read from the persistent store. If we ever have to recreate the JobStatus on |
| 468 | // the fly, it means we're rescheduling the job; and this means that the calculated |
| 469 | // elapsed timebase bounds intrinsically become correct. |
| 470 | this.mPersistedUtcTimes = persistedExecutionTimesUTC; |
| 471 | if (persistedExecutionTimesUTC != null) { |
| 472 | if (DEBUG) { |
| 473 | Slog.i(TAG, "+ restored job with RTC times because of bad boot clock"); |
| 474 | } |
| 475 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | /** Create a new job to be rescheduled with the provided parameters. */ |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 479 | public JobStatus(JobStatus rescheduling, long newBaseHeartbeat, |
| 480 | long newEarliestRuntimeElapsedMillis, |
Makoto Onuki | ab8a67f | 2017-06-20 12:20:34 -0700 | [diff] [blame] | 481 | long newLatestRuntimeElapsedMillis, int backoffAttempt, |
| 482 | long lastSuccessfulRunTime, long lastFailedRunTime) { |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 483 | this(rescheduling.job, rescheduling.getUid(), resolveTargetSdkVersion(rescheduling.job), |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 484 | rescheduling.getSourcePackageName(), rescheduling.getSourceUserId(), |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 485 | rescheduling.getStandbyBucket(), newBaseHeartbeat, |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 486 | rescheduling.getSourceTag(), backoffAttempt, newEarliestRuntimeElapsedMillis, |
Makoto Onuki | ab8a67f | 2017-06-20 12:20:34 -0700 | [diff] [blame] | 487 | newLatestRuntimeElapsedMillis, |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 488 | lastSuccessfulRunTime, lastFailedRunTime, rescheduling.getInternalFlags()); |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 489 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 490 | |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 491 | /** |
| 492 | * Create a newly scheduled job. |
| 493 | * @param callingUid Uid of the package that scheduled this job. |
Christopher Tate | 72a0b2a | 2018-03-19 17:24:32 -0700 | [diff] [blame] | 494 | * @param sourcePkg Package name of the app that will actually run the job. Null indicates |
| 495 | * that the calling package is the source. |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 496 | * @param sourceUserId User id for whom this job is scheduled. -1 indicates this is same as the |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 497 | * caller. |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 498 | */ |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 499 | public static JobStatus createFromJobInfo(JobInfo job, int callingUid, String sourcePkg, |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 500 | int sourceUserId, String tag) { |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 501 | final long elapsedNow = sElapsedRealtimeClock.millis(); |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 502 | final long earliestRunTimeElapsedMillis, latestRunTimeElapsedMillis; |
| 503 | if (job.isPeriodic()) { |
| 504 | latestRunTimeElapsedMillis = elapsedNow + job.getIntervalMillis(); |
| 505 | earliestRunTimeElapsedMillis = latestRunTimeElapsedMillis - job.getFlexMillis(); |
| 506 | } else { |
| 507 | earliestRunTimeElapsedMillis = job.hasEarlyConstraint() ? |
| 508 | elapsedNow + job.getMinLatencyMillis() : NO_EARLIEST_RUNTIME; |
| 509 | latestRunTimeElapsedMillis = job.hasLateConstraint() ? |
| 510 | elapsedNow + job.getMaxExecutionDelayMillis() : NO_LATEST_RUNTIME; |
| 511 | } |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 512 | String jobPackage = (sourcePkg != null) ? sourcePkg : job.getService().getPackageName(); |
| 513 | |
| 514 | int standbyBucket = JobSchedulerService.standbyBucketForPackage(jobPackage, |
| 515 | sourceUserId, elapsedNow); |
| 516 | JobSchedulerInternal js = LocalServices.getService(JobSchedulerInternal.class); |
Christopher Tate | 435c2f4 | 2018-01-18 12:59:15 -0800 | [diff] [blame] | 517 | long currentHeartbeat = js != null |
| 518 | ? js.baseHeartbeatForApp(jobPackage, sourceUserId, standbyBucket) |
| 519 | : 0; |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 520 | return new JobStatus(job, callingUid, resolveTargetSdkVersion(job), sourcePkg, sourceUserId, |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 521 | standbyBucket, currentHeartbeat, tag, 0, |
Makoto Onuki | ab8a67f | 2017-06-20 12:20:34 -0700 | [diff] [blame] | 522 | earliestRunTimeElapsedMillis, latestRunTimeElapsedMillis, |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 523 | 0 /* lastSuccessfulRunTime */, 0 /* lastFailedRunTime */, |
| 524 | /*innerFlags=*/ 0); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Dianne Hackborn | 342e603 | 2017-04-13 18:04:31 -0700 | [diff] [blame] | 527 | public void enqueueWorkLocked(IActivityManager am, JobWorkItem work) { |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 528 | if (pendingWork == null) { |
| 529 | pendingWork = new ArrayList<>(); |
| 530 | } |
| 531 | work.setWorkId(nextPendingWorkId); |
| 532 | nextPendingWorkId++; |
Dianne Hackborn | 342e603 | 2017-04-13 18:04:31 -0700 | [diff] [blame] | 533 | if (work.getIntent() != null |
| 534 | && GrantedUriPermissions.checkGrantFlags(work.getIntent().getFlags())) { |
| 535 | work.setGrants(GrantedUriPermissions.createFromIntent(am, work.getIntent(), sourceUid, |
| 536 | sourcePackageName, sourceUserId, toShortString())); |
| 537 | } |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 538 | pendingWork.add(work); |
Jeff Sharkey | caa3f8d | 2017-10-25 16:12:00 -0600 | [diff] [blame] | 539 | updateEstimatedNetworkBytesLocked(); |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | public JobWorkItem dequeueWorkLocked() { |
| 543 | if (pendingWork != null && pendingWork.size() > 0) { |
| 544 | JobWorkItem work = pendingWork.remove(0); |
| 545 | if (work != null) { |
| 546 | if (executingWork == null) { |
| 547 | executingWork = new ArrayList<>(); |
| 548 | } |
| 549 | executingWork.add(work); |
Dianne Hackborn | 28d1b66 | 2017-04-21 14:17:23 -0700 | [diff] [blame] | 550 | work.bumpDeliveryCount(); |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 551 | } |
Jeff Sharkey | caa3f8d | 2017-10-25 16:12:00 -0600 | [diff] [blame] | 552 | updateEstimatedNetworkBytesLocked(); |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 553 | return work; |
| 554 | } |
| 555 | return null; |
| 556 | } |
| 557 | |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 558 | public boolean hasWorkLocked() { |
| 559 | return (pendingWork != null && pendingWork.size() > 0) || hasExecutingWorkLocked(); |
| 560 | } |
| 561 | |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 562 | public boolean hasExecutingWorkLocked() { |
| 563 | return executingWork != null && executingWork.size() > 0; |
| 564 | } |
| 565 | |
Dianne Hackborn | 342e603 | 2017-04-13 18:04:31 -0700 | [diff] [blame] | 566 | private static void ungrantWorkItem(IActivityManager am, JobWorkItem work) { |
| 567 | if (work.getGrants() != null) { |
| 568 | ((GrantedUriPermissions)work.getGrants()).revoke(am); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | public boolean completeWorkLocked(IActivityManager am, int workId) { |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 573 | if (executingWork != null) { |
| 574 | final int N = executingWork.size(); |
| 575 | for (int i = 0; i < N; i++) { |
Dianne Hackborn | 342e603 | 2017-04-13 18:04:31 -0700 | [diff] [blame] | 576 | JobWorkItem work = executingWork.get(i); |
| 577 | if (work.getWorkId() == workId) { |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 578 | executingWork.remove(i); |
Dianne Hackborn | 342e603 | 2017-04-13 18:04:31 -0700 | [diff] [blame] | 579 | ungrantWorkItem(am, work); |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 580 | return true; |
| 581 | } |
| 582 | } |
| 583 | } |
| 584 | return false; |
| 585 | } |
| 586 | |
Dianne Hackborn | 342e603 | 2017-04-13 18:04:31 -0700 | [diff] [blame] | 587 | private static void ungrantWorkList(IActivityManager am, ArrayList<JobWorkItem> list) { |
| 588 | if (list != null) { |
| 589 | final int N = list.size(); |
| 590 | for (int i = 0; i < N; i++) { |
| 591 | ungrantWorkItem(am, list.get(i)); |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | public void stopTrackingJobLocked(IActivityManager am, JobStatus incomingJob) { |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 597 | if (incomingJob != null) { |
Dianne Hackborn | 342e603 | 2017-04-13 18:04:31 -0700 | [diff] [blame] | 598 | // We are replacing with a new job -- transfer the work! We do any executing |
| 599 | // work first, since that was originally at the front of the pending work. |
| 600 | if (executingWork != null && executingWork.size() > 0) { |
| 601 | incomingJob.pendingWork = executingWork; |
| 602 | } |
| 603 | if (incomingJob.pendingWork == null) { |
| 604 | incomingJob.pendingWork = pendingWork; |
| 605 | } else if (pendingWork != null && pendingWork.size() > 0) { |
| 606 | incomingJob.pendingWork.addAll(pendingWork); |
| 607 | } |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 608 | pendingWork = null; |
Dianne Hackborn | 342e603 | 2017-04-13 18:04:31 -0700 | [diff] [blame] | 609 | executingWork = null; |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 610 | incomingJob.nextPendingWorkId = nextPendingWorkId; |
Jeff Sharkey | caa3f8d | 2017-10-25 16:12:00 -0600 | [diff] [blame] | 611 | incomingJob.updateEstimatedNetworkBytesLocked(); |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 612 | } else { |
| 613 | // We are completely stopping the job... need to clean up work. |
Dianne Hackborn | 342e603 | 2017-04-13 18:04:31 -0700 | [diff] [blame] | 614 | ungrantWorkList(am, pendingWork); |
| 615 | pendingWork = null; |
| 616 | ungrantWorkList(am, executingWork); |
| 617 | executingWork = null; |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 618 | } |
Jeff Sharkey | caa3f8d | 2017-10-25 16:12:00 -0600 | [diff] [blame] | 619 | updateEstimatedNetworkBytesLocked(); |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | public void prepareLocked(IActivityManager am) { |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 623 | if (prepared) { |
| 624 | Slog.wtf(TAG, "Already prepared: " + this); |
| 625 | return; |
| 626 | } |
| 627 | prepared = true; |
Christopher Tate | b164f01 | 2017-04-26 15:41:37 -0700 | [diff] [blame] | 628 | if (DEBUG_PREPARE) { |
| 629 | unpreparedPoint = null; |
| 630 | } |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 631 | final ClipData clip = job.getClipData(); |
| 632 | if (clip != null) { |
Dianne Hackborn | 342e603 | 2017-04-13 18:04:31 -0700 | [diff] [blame] | 633 | uriPerms = GrantedUriPermissions.createFromClip(am, clip, sourceUid, sourcePackageName, |
| 634 | sourceUserId, job.getClipGrantFlags(), toShortString()); |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 638 | public void unprepareLocked(IActivityManager am) { |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 639 | if (!prepared) { |
| 640 | Slog.wtf(TAG, "Hasn't been prepared: " + this); |
Christopher Tate | b164f01 | 2017-04-26 15:41:37 -0700 | [diff] [blame] | 641 | if (DEBUG_PREPARE && unpreparedPoint != null) { |
| 642 | Slog.e(TAG, "Was already unprepared at ", unpreparedPoint); |
| 643 | } |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 644 | return; |
| 645 | } |
| 646 | prepared = false; |
Christopher Tate | b164f01 | 2017-04-26 15:41:37 -0700 | [diff] [blame] | 647 | if (DEBUG_PREPARE) { |
| 648 | unpreparedPoint = new Throwable().fillInStackTrace(); |
| 649 | } |
Dianne Hackborn | 342e603 | 2017-04-13 18:04:31 -0700 | [diff] [blame] | 650 | if (uriPerms != null) { |
| 651 | uriPerms.revoke(am); |
| 652 | uriPerms = null; |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 653 | } |
| 654 | } |
| 655 | |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 656 | public boolean isPreparedLocked() { |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 657 | return prepared; |
| 658 | } |
| 659 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 660 | public JobInfo getJob() { |
| 661 | return job; |
| 662 | } |
| 663 | |
| 664 | public int getJobId() { |
| 665 | return job.getId(); |
| 666 | } |
| 667 | |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 668 | public int getTargetSdkVersion() { |
| 669 | return targetSdkVersion; |
| 670 | } |
| 671 | |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 672 | public void printUniqueId(PrintWriter pw) { |
| 673 | UserHandle.formatUid(pw, callingUid); |
| 674 | pw.print("/"); |
| 675 | pw.print(job.getId()); |
| 676 | } |
| 677 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 678 | public int getNumFailures() { |
| 679 | return numFailures; |
| 680 | } |
| 681 | |
| 682 | public ComponentName getServiceComponent() { |
| 683 | return job.getService(); |
| 684 | } |
| 685 | |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 686 | public String getSourcePackageName() { |
Shreyas Basarge | 8e64e2e | 2016-02-12 15:49:31 +0000 | [diff] [blame] | 687 | return sourcePackageName; |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 688 | } |
| 689 | |
| 690 | public int getSourceUid() { |
| 691 | return sourceUid; |
| 692 | } |
| 693 | |
| 694 | public int getSourceUserId() { |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 695 | return sourceUserId; |
| 696 | } |
| 697 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 698 | public int getUserId() { |
Shreyas Basarge | 8e64e2e | 2016-02-12 15:49:31 +0000 | [diff] [blame] | 699 | return UserHandle.getUserId(callingUid); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 700 | } |
| 701 | |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 702 | public int getStandbyBucket() { |
| 703 | return standbyBucket; |
| 704 | } |
| 705 | |
| 706 | public long getBaseHeartbeat() { |
| 707 | return baseHeartbeat; |
| 708 | } |
| 709 | |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 710 | public void setStandbyBucket(int newBucket) { |
| 711 | standbyBucket = newBucket; |
| 712 | } |
| 713 | |
| 714 | // Called only by the standby monitoring code |
| 715 | public long getWhenStandbyDeferred() { |
| 716 | return whenStandbyDeferred; |
| 717 | } |
| 718 | |
| 719 | // Called only by the standby monitoring code |
| 720 | public void setWhenStandbyDeferred(long now) { |
| 721 | whenStandbyDeferred = now; |
| 722 | } |
| 723 | |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 724 | public String getSourceTag() { |
| 725 | return sourceTag; |
| 726 | } |
| 727 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 728 | public int getUid() { |
Shreyas Basarge | 8e64e2e | 2016-02-12 15:49:31 +0000 | [diff] [blame] | 729 | return callingUid; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 730 | } |
| 731 | |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 732 | public String getBatteryName() { |
| 733 | return batteryName; |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | public String getTag() { |
| 737 | return tag; |
| 738 | } |
| 739 | |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 740 | public int getPriority() { |
| 741 | return job.getPriority(); |
| 742 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 743 | |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 744 | public int getFlags() { |
| 745 | return job.getFlags(); |
| 746 | } |
| 747 | |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 748 | public int getInternalFlags() { |
| 749 | return mInternalFlags; |
| 750 | } |
| 751 | |
| 752 | public void addInternalFlags(int flags) { |
| 753 | mInternalFlags |= flags; |
| 754 | } |
| 755 | |
Serik Beketayev | 75915d1 | 2018-08-01 16:56:59 -0700 | [diff] [blame] | 756 | public int getSatisfiedConstraintFlags() { |
| 757 | return satisfiedConstraints; |
| 758 | } |
| 759 | |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 760 | public void maybeAddForegroundExemption(Predicate<Integer> uidForegroundChecker) { |
| 761 | // Jobs with time constraints shouldn't be exempted. |
| 762 | if (job.hasEarlyConstraint() || job.hasLateConstraint()) { |
| 763 | return; |
| 764 | } |
| 765 | // Already exempted, skip the foreground check. |
| 766 | if ((mInternalFlags & INTERNAL_FLAG_HAS_FOREGROUND_EXEMPTION) != 0) { |
| 767 | return; |
| 768 | } |
| 769 | if (uidForegroundChecker.test(getSourceUid())) { |
| 770 | addInternalFlags(INTERNAL_FLAG_HAS_FOREGROUND_EXEMPTION); |
| 771 | } |
| 772 | } |
| 773 | |
Jeff Sharkey | caa3f8d | 2017-10-25 16:12:00 -0600 | [diff] [blame] | 774 | private void updateEstimatedNetworkBytesLocked() { |
| 775 | totalNetworkBytes = computeEstimatedNetworkBytesLocked(); |
| 776 | } |
| 777 | |
| 778 | private long computeEstimatedNetworkBytesLocked() { |
| 779 | // If any component of the job has unknown usage, we don't have a |
| 780 | // complete picture of what data will be used, and we have to treat the |
| 781 | // entire job as unknown. |
| 782 | long totalNetworkBytes = 0; |
| 783 | long networkBytes = job.getEstimatedNetworkBytes(); |
| 784 | if (networkBytes == JobInfo.NETWORK_BYTES_UNKNOWN) { |
| 785 | return JobInfo.NETWORK_BYTES_UNKNOWN; |
| 786 | } else { |
| 787 | totalNetworkBytes += networkBytes; |
| 788 | } |
| 789 | if (pendingWork != null) { |
| 790 | for (int i = 0; i < pendingWork.size(); i++) { |
| 791 | networkBytes = pendingWork.get(i).getEstimatedNetworkBytes(); |
| 792 | if (networkBytes == JobInfo.NETWORK_BYTES_UNKNOWN) { |
| 793 | return JobInfo.NETWORK_BYTES_UNKNOWN; |
| 794 | } else { |
| 795 | totalNetworkBytes += networkBytes; |
| 796 | } |
| 797 | } |
| 798 | } |
| 799 | return totalNetworkBytes; |
| 800 | } |
| 801 | |
| 802 | public long getEstimatedNetworkBytes() { |
| 803 | return totalNetworkBytes; |
| 804 | } |
| 805 | |
Christopher Tate | 60977f4 | 2017-04-13 13:48:46 -0700 | [diff] [blame] | 806 | /** Does this job have any sort of networking constraint? */ |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 807 | public boolean hasConnectivityConstraint() { |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 808 | return (requiredConstraints&CONSTRAINT_CONNECTIVITY) != 0; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 809 | } |
| 810 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 811 | public boolean hasChargingConstraint() { |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 812 | return (requiredConstraints&CONSTRAINT_CHARGING) != 0; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 813 | } |
| 814 | |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 815 | public boolean hasBatteryNotLowConstraint() { |
| 816 | return (requiredConstraints&CONSTRAINT_BATTERY_NOT_LOW) != 0; |
| 817 | } |
| 818 | |
| 819 | public boolean hasPowerConstraint() { |
| 820 | return (requiredConstraints&(CONSTRAINT_CHARGING|CONSTRAINT_BATTERY_NOT_LOW)) != 0; |
| 821 | } |
| 822 | |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 823 | public boolean hasStorageNotLowConstraint() { |
| 824 | return (requiredConstraints&CONSTRAINT_STORAGE_NOT_LOW) != 0; |
| 825 | } |
| 826 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 827 | public boolean hasTimingDelayConstraint() { |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 828 | return (requiredConstraints&CONSTRAINT_TIMING_DELAY) != 0; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | public boolean hasDeadlineConstraint() { |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 832 | return (requiredConstraints&CONSTRAINT_DEADLINE) != 0; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | public boolean hasIdleConstraint() { |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 836 | return (requiredConstraints&CONSTRAINT_IDLE) != 0; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 837 | } |
| 838 | |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 839 | public boolean hasContentTriggerConstraint() { |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 840 | return (requiredConstraints&CONSTRAINT_CONTENT_TRIGGER) != 0; |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 841 | } |
| 842 | |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 843 | public long getTriggerContentUpdateDelay() { |
| 844 | long time = job.getTriggerContentUpdateDelay(); |
| 845 | if (time < 0) { |
| 846 | return DEFAULT_TRIGGER_UPDATE_DELAY; |
| 847 | } |
| 848 | return Math.max(time, MIN_TRIGGER_UPDATE_DELAY); |
| 849 | } |
| 850 | |
| 851 | public long getTriggerContentMaxDelay() { |
| 852 | long time = job.getTriggerContentMaxDelay(); |
| 853 | if (time < 0) { |
| 854 | return DEFAULT_TRIGGER_MAX_DELAY; |
| 855 | } |
| 856 | return Math.max(time, MIN_TRIGGER_MAX_DELAY); |
| 857 | } |
| 858 | |
Matthew Williams | 900c67f | 2014-07-09 12:46:53 -0700 | [diff] [blame] | 859 | public boolean isPersisted() { |
| 860 | return job.isPersisted(); |
| 861 | } |
| 862 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 863 | public long getEarliestRunTime() { |
| 864 | return earliestRunTimeElapsedMillis; |
| 865 | } |
| 866 | |
| 867 | public long getLatestRunTimeElapsed() { |
| 868 | return latestRunTimeElapsedMillis; |
| 869 | } |
| 870 | |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 871 | /** |
| 872 | * Return the fractional position of "now" within the "run time" window of |
| 873 | * this job. |
| 874 | * <p> |
| 875 | * For example, if the earliest run time was 10 minutes ago, and the latest |
| 876 | * run time is 30 minutes from now, this would return 0.25. |
| 877 | * <p> |
| 878 | * If the job has no window defined, returns 1. When only an earliest or |
| 879 | * latest time is defined, it's treated as an infinitely small window at |
| 880 | * that time. |
| 881 | */ |
| 882 | public float getFractionRunTime() { |
| 883 | final long now = JobSchedulerService.sElapsedRealtimeClock.millis(); |
| 884 | if (earliestRunTimeElapsedMillis == 0 && latestRunTimeElapsedMillis == Long.MAX_VALUE) { |
| 885 | return 1; |
| 886 | } else if (earliestRunTimeElapsedMillis == 0) { |
| 887 | return now >= latestRunTimeElapsedMillis ? 1 : 0; |
| 888 | } else if (latestRunTimeElapsedMillis == Long.MAX_VALUE) { |
| 889 | return now >= earliestRunTimeElapsedMillis ? 1 : 0; |
| 890 | } else { |
| 891 | if (now <= earliestRunTimeElapsedMillis) { |
| 892 | return 0; |
| 893 | } else if (now >= latestRunTimeElapsedMillis) { |
| 894 | return 1; |
| 895 | } else { |
| 896 | return (float) (now - earliestRunTimeElapsedMillis) |
| 897 | / (float) (latestRunTimeElapsedMillis - earliestRunTimeElapsedMillis); |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | |
Christopher Tate | 616541d | 2017-07-26 14:27:38 -0700 | [diff] [blame] | 902 | public Pair<Long, Long> getPersistedUtcTimes() { |
| 903 | return mPersistedUtcTimes; |
| 904 | } |
| 905 | |
| 906 | public void clearPersistedUtcTimes() { |
| 907 | mPersistedUtcTimes = null; |
| 908 | } |
| 909 | |
Kweku Adams | 4836f9d | 2018-11-12 17:04:17 -0800 | [diff] [blame] | 910 | /** @return true if the constraint was changed, false otherwise. */ |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 911 | boolean setChargingConstraintSatisfied(boolean state) { |
| 912 | return setConstraintSatisfied(CONSTRAINT_CHARGING, state); |
| 913 | } |
| 914 | |
Kweku Adams | 4836f9d | 2018-11-12 17:04:17 -0800 | [diff] [blame] | 915 | /** @return true if the constraint was changed, false otherwise. */ |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 916 | boolean setBatteryNotLowConstraintSatisfied(boolean state) { |
| 917 | return setConstraintSatisfied(CONSTRAINT_BATTERY_NOT_LOW, state); |
| 918 | } |
| 919 | |
Kweku Adams | 4836f9d | 2018-11-12 17:04:17 -0800 | [diff] [blame] | 920 | /** @return true if the constraint was changed, false otherwise. */ |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 921 | boolean setStorageNotLowConstraintSatisfied(boolean state) { |
| 922 | return setConstraintSatisfied(CONSTRAINT_STORAGE_NOT_LOW, state); |
| 923 | } |
| 924 | |
Kweku Adams | 4836f9d | 2018-11-12 17:04:17 -0800 | [diff] [blame] | 925 | /** @return true if the constraint was changed, false otherwise. */ |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 926 | boolean setTimingDelayConstraintSatisfied(boolean state) { |
| 927 | return setConstraintSatisfied(CONSTRAINT_TIMING_DELAY, state); |
| 928 | } |
| 929 | |
Kweku Adams | 4836f9d | 2018-11-12 17:04:17 -0800 | [diff] [blame] | 930 | /** @return true if the constraint was changed, false otherwise. */ |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 931 | boolean setDeadlineConstraintSatisfied(boolean state) { |
Kweku Adams | d14ce4d | 2018-10-19 11:40:55 -0700 | [diff] [blame] | 932 | if (setConstraintSatisfied(CONSTRAINT_DEADLINE, state)) { |
| 933 | // The constraint was changed. Update the ready flag. |
| 934 | mReadyDeadlineSatisfied = !job.isPeriodic() && hasDeadlineConstraint() && state; |
| 935 | return true; |
| 936 | } |
| 937 | return false; |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 938 | } |
| 939 | |
Kweku Adams | 4836f9d | 2018-11-12 17:04:17 -0800 | [diff] [blame] | 940 | /** @return true if the constraint was changed, false otherwise. */ |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 941 | boolean setIdleConstraintSatisfied(boolean state) { |
| 942 | return setConstraintSatisfied(CONSTRAINT_IDLE, state); |
| 943 | } |
| 944 | |
Kweku Adams | 4836f9d | 2018-11-12 17:04:17 -0800 | [diff] [blame] | 945 | /** @return true if the constraint was changed, false otherwise. */ |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 946 | boolean setConnectivityConstraintSatisfied(boolean state) { |
| 947 | return setConstraintSatisfied(CONSTRAINT_CONNECTIVITY, state); |
| 948 | } |
| 949 | |
Kweku Adams | 4836f9d | 2018-11-12 17:04:17 -0800 | [diff] [blame] | 950 | /** @return true if the constraint was changed, false otherwise. */ |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 951 | boolean setContentTriggerConstraintSatisfied(boolean state) { |
| 952 | return setConstraintSatisfied(CONSTRAINT_CONTENT_TRIGGER, state); |
| 953 | } |
| 954 | |
Kweku Adams | 4836f9d | 2018-11-12 17:04:17 -0800 | [diff] [blame] | 955 | /** @return true if the constraint was changed, false otherwise. */ |
Dianne Hackborn | 7ab4025 | 2016-06-15 17:30:24 -0700 | [diff] [blame] | 956 | boolean setDeviceNotDozingConstraintSatisfied(boolean state, boolean whitelisted) { |
| 957 | dozeWhitelisted = whitelisted; |
Kweku Adams | d14ce4d | 2018-10-19 11:40:55 -0700 | [diff] [blame] | 958 | if (setConstraintSatisfied(CONSTRAINT_DEVICE_NOT_DOZING, state)) { |
| 959 | // The constraint was changed. Update the ready flag. |
| 960 | mReadyNotDozing = state || (job.getFlags() & JobInfo.FLAG_WILL_BE_FOREGROUND) != 0; |
| 961 | return true; |
| 962 | } |
| 963 | return false; |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 964 | } |
| 965 | |
Kweku Adams | 4836f9d | 2018-11-12 17:04:17 -0800 | [diff] [blame] | 966 | /** @return true if the constraint was changed, false otherwise. */ |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 967 | boolean setBackgroundNotRestrictedConstraintSatisfied(boolean state) { |
Kweku Adams | d14ce4d | 2018-10-19 11:40:55 -0700 | [diff] [blame] | 968 | if (setConstraintSatisfied(CONSTRAINT_BACKGROUND_NOT_RESTRICTED, state)) { |
| 969 | // The constraint was changed. Update the ready flag. |
| 970 | mReadyNotRestrictedInBg = state; |
| 971 | return true; |
| 972 | } |
| 973 | return false; |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 974 | } |
| 975 | |
Kweku Adams | 4836f9d | 2018-11-12 17:04:17 -0800 | [diff] [blame] | 976 | /** @return true if the constraint was changed, false otherwise. */ |
| 977 | boolean setQuotaConstraintSatisfied(boolean state) { |
| 978 | if (setConstraintSatisfied(CONSTRAINT_WITHIN_QUOTA, state)) { |
| 979 | // The constraint was changed. Update the ready flag. |
| 980 | mReadyWithinQuota = state; |
| 981 | return true; |
| 982 | } |
| 983 | return false; |
| 984 | } |
| 985 | |
| 986 | /** @return true if the state was changed, false otherwise. */ |
Christopher Tate | 20afddd | 2018-02-28 15:19:19 -0800 | [diff] [blame] | 987 | boolean setUidActive(final boolean newActiveState) { |
| 988 | if (newActiveState != uidActive) { |
| 989 | uidActive = newActiveState; |
| 990 | return true; |
| 991 | } |
| 992 | return false; /* unchanged */ |
| 993 | } |
| 994 | |
Kweku Adams | 4836f9d | 2018-11-12 17:04:17 -0800 | [diff] [blame] | 995 | /** @return true if the constraint was changed, false otherwise. */ |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 996 | boolean setConstraintSatisfied(int constraint, boolean state) { |
| 997 | boolean old = (satisfiedConstraints&constraint) != 0; |
| 998 | if (old == state) { |
| 999 | return false; |
| 1000 | } |
| 1001 | satisfiedConstraints = (satisfiedConstraints&~constraint) | (state ? constraint : 0); |
Kweku Adams | d14ce4d | 2018-10-19 11:40:55 -0700 | [diff] [blame] | 1002 | mSatisfiedConstraintsOfInterest = satisfiedConstraints & CONSTRAINTS_OF_INTEREST; |
Kweku Adams | 8845d01 | 2018-12-11 20:06:45 -0800 | [diff] [blame] | 1003 | if ((STATSD_CONSTRAINTS_TO_LOG & constraint) != 0) { |
| 1004 | StatsLog.write_non_chained(StatsLog.SCHEDULED_JOB_CONSTRAINT_CHANGED, |
| 1005 | sourceUid, null, getBatteryName(), getProtoConstraint(constraint), |
| 1006 | state ? StatsLog.SCHEDULED_JOB_CONSTRAINT_CHANGED__STATE__SATISFIED |
| 1007 | : StatsLog.SCHEDULED_JOB_CONSTRAINT_CHANGED__STATE__UNSATISFIED); |
| 1008 | } |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 1009 | return true; |
| 1010 | } |
| 1011 | |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1012 | boolean isConstraintSatisfied(int constraint) { |
| 1013 | return (satisfiedConstraints&constraint) != 0; |
| 1014 | } |
| 1015 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1016 | boolean clearTrackingController(int which) { |
| 1017 | if ((trackingControllers&which) != 0) { |
| 1018 | trackingControllers &= ~which; |
| 1019 | return true; |
| 1020 | } |
| 1021 | return false; |
| 1022 | } |
| 1023 | |
| 1024 | void setTrackingController(int which) { |
| 1025 | trackingControllers |= which; |
| 1026 | } |
| 1027 | |
Makoto Onuki | ab8a67f | 2017-06-20 12:20:34 -0700 | [diff] [blame] | 1028 | public long getLastSuccessfulRunTime() { |
| 1029 | return mLastSuccessfulRunTime; |
| 1030 | } |
| 1031 | |
| 1032 | public long getLastFailedRunTime() { |
| 1033 | return mLastFailedRunTime; |
| 1034 | } |
| 1035 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1036 | /** |
Kweku Adams | 32f4476 | 2018-11-02 16:58:32 -0700 | [diff] [blame] | 1037 | * @return Whether or not this job is ready to run, based on its requirements. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1038 | */ |
Dianne Hackborn | d506b2b | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 1039 | public boolean isReady() { |
Kweku Adams | cdbfcb9 | 2018-12-06 17:05:15 -0800 | [diff] [blame] | 1040 | return isReady(mSatisfiedConstraintsOfInterest); |
| 1041 | } |
| 1042 | |
| 1043 | /** |
| 1044 | * @return Whether or not this job would be ready to run if it had the specified constraint |
| 1045 | * granted, based on its requirements. |
| 1046 | */ |
| 1047 | public boolean wouldBeReadyWithConstraint(int constraint) { |
| 1048 | return isReady(mSatisfiedConstraintsOfInterest | constraint); |
| 1049 | } |
| 1050 | |
| 1051 | private boolean isReady(int satisfiedConstraints) { |
Kweku Adams | 4836f9d | 2018-11-12 17:04:17 -0800 | [diff] [blame] | 1052 | // Quota constraints trumps all other constraints. |
| 1053 | if (!mReadyWithinQuota) { |
| 1054 | return false; |
| 1055 | } |
| 1056 | // Deadline constraint trumps other constraints besides quota (except for periodic jobs |
| 1057 | // where deadline is an implementation detail. A periodic job should only run if its |
| 1058 | // constraints are satisfied). |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 1059 | // DeviceNotDozing implicit constraint must be satisfied |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 1060 | // NotRestrictedInBackground implicit constraint must be satisfied |
Kweku Adams | d14ce4d | 2018-10-19 11:40:55 -0700 | [diff] [blame] | 1061 | return mReadyNotDozing && mReadyNotRestrictedInBg && (mReadyDeadlineSatisfied |
Kweku Adams | cdbfcb9 | 2018-12-06 17:05:15 -0800 | [diff] [blame] | 1062 | || isConstraintsSatisfied(satisfiedConstraints)); |
Matthew Williams | 03a4da6 | 2014-09-10 17:32:18 -0700 | [diff] [blame] | 1063 | } |
| 1064 | |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 1065 | static final int CONSTRAINTS_OF_INTEREST = CONSTRAINT_CHARGING | CONSTRAINT_BATTERY_NOT_LOW |
| 1066 | | CONSTRAINT_STORAGE_NOT_LOW | CONSTRAINT_TIMING_DELAY | CONSTRAINT_CONNECTIVITY |
| 1067 | | CONSTRAINT_IDLE | CONSTRAINT_CONTENT_TRIGGER; |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 1068 | |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 1069 | // Soft override covers all non-"functional" constraints |
| 1070 | static final int SOFT_OVERRIDE_CONSTRAINTS = |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 1071 | CONSTRAINT_CHARGING | CONSTRAINT_BATTERY_NOT_LOW | CONSTRAINT_STORAGE_NOT_LOW |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 1072 | | CONSTRAINT_TIMING_DELAY | CONSTRAINT_IDLE; |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 1073 | |
Matthew Williams | 03a4da6 | 2014-09-10 17:32:18 -0700 | [diff] [blame] | 1074 | /** |
| 1075 | * @return Whether the constraints set on this job are satisfied. |
| 1076 | */ |
Dianne Hackborn | d506b2b | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 1077 | public boolean isConstraintsSatisfied() { |
Kweku Adams | cdbfcb9 | 2018-12-06 17:05:15 -0800 | [diff] [blame] | 1078 | return isConstraintsSatisfied(mSatisfiedConstraintsOfInterest); |
| 1079 | } |
| 1080 | |
| 1081 | private boolean isConstraintsSatisfied(int satisfiedConstraints) { |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 1082 | if (overrideState == OVERRIDE_FULL) { |
| 1083 | // force override: the job is always runnable |
| 1084 | return true; |
| 1085 | } |
| 1086 | |
Kweku Adams | cdbfcb9 | 2018-12-06 17:05:15 -0800 | [diff] [blame] | 1087 | int sat = satisfiedConstraints; |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 1088 | if (overrideState == OVERRIDE_SOFT) { |
| 1089 | // override: pretend all 'soft' requirements are satisfied |
| 1090 | sat |= (requiredConstraints & SOFT_OVERRIDE_CONSTRAINTS); |
| 1091 | } |
| 1092 | |
Kweku Adams | d14ce4d | 2018-10-19 11:40:55 -0700 | [diff] [blame] | 1093 | return (sat & mRequiredConstraintsOfInterest) == mRequiredConstraintsOfInterest; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | public boolean matches(int uid, int jobId) { |
Shreyas Basarge | 8e64e2e | 2016-02-12 15:49:31 +0000 | [diff] [blame] | 1097 | return this.job.getId() == jobId && this.callingUid == uid; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | @Override |
| 1101 | public String toString() { |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 1102 | StringBuilder sb = new StringBuilder(128); |
| 1103 | sb.append("JobStatus{"); |
| 1104 | sb.append(Integer.toHexString(System.identityHashCode(this))); |
| 1105 | sb.append(" #"); |
| 1106 | UserHandle.formatUid(sb, callingUid); |
| 1107 | sb.append("/"); |
| 1108 | sb.append(job.getId()); |
| 1109 | sb.append(' '); |
| 1110 | sb.append(batteryName); |
| 1111 | sb.append(" u="); |
| 1112 | sb.append(getUserId()); |
| 1113 | sb.append(" s="); |
| 1114 | sb.append(getSourceUid()); |
| 1115 | if (earliestRunTimeElapsedMillis != NO_EARLIEST_RUNTIME |
| 1116 | || latestRunTimeElapsedMillis != NO_LATEST_RUNTIME) { |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 1117 | long now = sElapsedRealtimeClock.millis(); |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 1118 | sb.append(" TIME="); |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 1119 | formatRunTime(sb, earliestRunTimeElapsedMillis, NO_EARLIEST_RUNTIME, now); |
| 1120 | sb.append(":"); |
| 1121 | formatRunTime(sb, latestRunTimeElapsedMillis, NO_LATEST_RUNTIME, now); |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 1122 | } |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 1123 | if (job.getRequiredNetwork() != null) { |
| 1124 | sb.append(" NET"); |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 1125 | } |
| 1126 | if (job.isRequireCharging()) { |
| 1127 | sb.append(" CHARGING"); |
| 1128 | } |
| 1129 | if (job.isRequireBatteryNotLow()) { |
| 1130 | sb.append(" BATNOTLOW"); |
| 1131 | } |
| 1132 | if (job.isRequireStorageNotLow()) { |
| 1133 | sb.append(" STORENOTLOW"); |
| 1134 | } |
| 1135 | if (job.isRequireDeviceIdle()) { |
| 1136 | sb.append(" IDLE"); |
| 1137 | } |
Christopher Tate | 616541d | 2017-07-26 14:27:38 -0700 | [diff] [blame] | 1138 | if (job.isPeriodic()) { |
| 1139 | sb.append(" PERIODIC"); |
| 1140 | } |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 1141 | if (job.isPersisted()) { |
| 1142 | sb.append(" PERSISTED"); |
| 1143 | } |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 1144 | if ((satisfiedConstraints&CONSTRAINT_DEVICE_NOT_DOZING) == 0) { |
| 1145 | sb.append(" WAIT:DEV_NOT_DOZING"); |
| 1146 | } |
| 1147 | if (job.getTriggerContentUris() != null) { |
| 1148 | sb.append(" URIS="); |
| 1149 | sb.append(Arrays.toString(job.getTriggerContentUris())); |
| 1150 | } |
| 1151 | if (numFailures != 0) { |
| 1152 | sb.append(" failures="); |
| 1153 | sb.append(numFailures); |
| 1154 | } |
| 1155 | if (isReady()) { |
| 1156 | sb.append(" READY"); |
| 1157 | } |
| 1158 | sb.append("}"); |
| 1159 | return sb.toString(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1160 | } |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 1161 | |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 1162 | private void formatRunTime(PrintWriter pw, long runtime, long defaultValue, long now) { |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 1163 | if (runtime == defaultValue) { |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 1164 | pw.print("none"); |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 1165 | } else { |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 1166 | TimeUtils.formatDuration(runtime - now, pw); |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | private void formatRunTime(StringBuilder sb, long runtime, long defaultValue, long now) { |
| 1171 | if (runtime == defaultValue) { |
| 1172 | sb.append("none"); |
| 1173 | } else { |
| 1174 | TimeUtils.formatDuration(runtime - now, sb); |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 1175 | } |
| 1176 | } |
| 1177 | |
| 1178 | /** |
| 1179 | * Convenience function to identify a job uniquely without pulling all the data that |
| 1180 | * {@link #toString()} returns. |
| 1181 | */ |
| 1182 | public String toShortString() { |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 1183 | StringBuilder sb = new StringBuilder(); |
| 1184 | sb.append(Integer.toHexString(System.identityHashCode(this))); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1185 | sb.append(" #"); |
| 1186 | UserHandle.formatUid(sb, callingUid); |
| 1187 | sb.append("/"); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 1188 | sb.append(job.getId()); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1189 | sb.append(' '); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1190 | sb.append(batteryName); |
| 1191 | return sb.toString(); |
| 1192 | } |
| 1193 | |
| 1194 | /** |
| 1195 | * Convenience function to identify a job uniquely without pulling all the data that |
| 1196 | * {@link #toString()} returns. |
| 1197 | */ |
| 1198 | public String toShortStringExceptUniqueId() { |
| 1199 | StringBuilder sb = new StringBuilder(); |
| 1200 | sb.append(Integer.toHexString(System.identityHashCode(this))); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 1201 | sb.append(' '); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1202 | sb.append(batteryName); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 1203 | return sb.toString(); |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 1204 | } |
| 1205 | |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 1206 | /** |
| 1207 | * Convenience function to dump data that identifies a job uniquely to proto. This is intended |
| 1208 | * to mimic {@link #toShortString}. |
| 1209 | */ |
| 1210 | public void writeToShortProto(ProtoOutputStream proto, long fieldId) { |
| 1211 | final long token = proto.start(fieldId); |
| 1212 | |
| 1213 | proto.write(JobStatusShortInfoProto.CALLING_UID, callingUid); |
| 1214 | proto.write(JobStatusShortInfoProto.JOB_ID, job.getId()); |
| 1215 | proto.write(JobStatusShortInfoProto.BATTERY_NAME, batteryName); |
| 1216 | |
| 1217 | proto.end(token); |
| 1218 | } |
| 1219 | |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 1220 | void dumpConstraints(PrintWriter pw, int constraints) { |
| 1221 | if ((constraints&CONSTRAINT_CHARGING) != 0) { |
| 1222 | pw.print(" CHARGING"); |
| 1223 | } |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 1224 | if ((constraints& CONSTRAINT_BATTERY_NOT_LOW) != 0) { |
| 1225 | pw.print(" BATTERY_NOT_LOW"); |
| 1226 | } |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 1227 | if ((constraints& CONSTRAINT_STORAGE_NOT_LOW) != 0) { |
| 1228 | pw.print(" STORAGE_NOT_LOW"); |
| 1229 | } |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 1230 | if ((constraints&CONSTRAINT_TIMING_DELAY) != 0) { |
| 1231 | pw.print(" TIMING_DELAY"); |
| 1232 | } |
| 1233 | if ((constraints&CONSTRAINT_DEADLINE) != 0) { |
| 1234 | pw.print(" DEADLINE"); |
| 1235 | } |
| 1236 | if ((constraints&CONSTRAINT_IDLE) != 0) { |
| 1237 | pw.print(" IDLE"); |
| 1238 | } |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1239 | if ((constraints&CONSTRAINT_CONNECTIVITY) != 0) { |
| 1240 | pw.print(" CONNECTIVITY"); |
| 1241 | } |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 1242 | if ((constraints&CONSTRAINT_CONTENT_TRIGGER) != 0) { |
| 1243 | pw.print(" CONTENT_TRIGGER"); |
| 1244 | } |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 1245 | if ((constraints&CONSTRAINT_DEVICE_NOT_DOZING) != 0) { |
| 1246 | pw.print(" DEVICE_NOT_DOZING"); |
| 1247 | } |
Makoto Onuki | dfede0a | 2018-01-22 15:11:34 -0800 | [diff] [blame] | 1248 | if ((constraints&CONSTRAINT_BACKGROUND_NOT_RESTRICTED) != 0) { |
| 1249 | pw.print(" BACKGROUND_NOT_RESTRICTED"); |
| 1250 | } |
Kweku Adams | 4836f9d | 2018-11-12 17:04:17 -0800 | [diff] [blame] | 1251 | if ((constraints & CONSTRAINT_WITHIN_QUOTA) != 0) { |
| 1252 | pw.print(" WITHIN_QUOTA"); |
| 1253 | } |
Makoto Onuki | dfede0a | 2018-01-22 15:11:34 -0800 | [diff] [blame] | 1254 | if (constraints != 0) { |
| 1255 | pw.print(" [0x"); |
| 1256 | pw.print(Integer.toHexString(constraints)); |
| 1257 | pw.print("]"); |
| 1258 | } |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 1259 | } |
| 1260 | |
Kweku Adams | 8845d01 | 2018-12-11 20:06:45 -0800 | [diff] [blame] | 1261 | /** Returns a {@link JobServerProtoEnums.Constraint} enum value for the given constraint. */ |
| 1262 | private int getProtoConstraint(int constraint) { |
| 1263 | switch (constraint) { |
| 1264 | case CONSTRAINT_BACKGROUND_NOT_RESTRICTED: |
| 1265 | return JobServerProtoEnums.CONSTRAINT_BACKGROUND_NOT_RESTRICTED; |
| 1266 | case CONSTRAINT_BATTERY_NOT_LOW: |
| 1267 | return JobServerProtoEnums.CONSTRAINT_BATTERY_NOT_LOW; |
| 1268 | case CONSTRAINT_CHARGING: |
| 1269 | return JobServerProtoEnums.CONSTRAINT_CHARGING; |
| 1270 | case CONSTRAINT_CONNECTIVITY: |
| 1271 | return JobServerProtoEnums.CONSTRAINT_CONNECTIVITY; |
| 1272 | case CONSTRAINT_CONTENT_TRIGGER: |
| 1273 | return JobServerProtoEnums.CONSTRAINT_CONTENT_TRIGGER; |
| 1274 | case CONSTRAINT_DEADLINE: |
| 1275 | return JobServerProtoEnums.CONSTRAINT_DEADLINE; |
| 1276 | case CONSTRAINT_DEVICE_NOT_DOZING: |
| 1277 | return JobServerProtoEnums.CONSTRAINT_DEVICE_NOT_DOZING; |
| 1278 | case CONSTRAINT_IDLE: |
| 1279 | return JobServerProtoEnums.CONSTRAINT_IDLE; |
| 1280 | case CONSTRAINT_STORAGE_NOT_LOW: |
| 1281 | return JobServerProtoEnums.CONSTRAINT_STORAGE_NOT_LOW; |
| 1282 | case CONSTRAINT_TIMING_DELAY: |
| 1283 | return JobServerProtoEnums.CONSTRAINT_TIMING_DELAY; |
| 1284 | case CONSTRAINT_WITHIN_QUOTA: |
| 1285 | return JobServerProtoEnums.CONSTRAINT_WITHIN_QUOTA; |
| 1286 | default: |
| 1287 | return JobServerProtoEnums.CONSTRAINT_UNKNOWN; |
| 1288 | } |
| 1289 | } |
| 1290 | |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 1291 | /** Writes constraints to the given repeating proto field. */ |
| 1292 | void dumpConstraints(ProtoOutputStream proto, long fieldId, int constraints) { |
| 1293 | if ((constraints & CONSTRAINT_CHARGING) != 0) { |
Kweku Adams | 8845d01 | 2018-12-11 20:06:45 -0800 | [diff] [blame] | 1294 | proto.write(fieldId, JobServerProtoEnums.CONSTRAINT_CHARGING); |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 1295 | } |
| 1296 | if ((constraints & CONSTRAINT_BATTERY_NOT_LOW) != 0) { |
Kweku Adams | 8845d01 | 2018-12-11 20:06:45 -0800 | [diff] [blame] | 1297 | proto.write(fieldId, JobServerProtoEnums.CONSTRAINT_BATTERY_NOT_LOW); |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 1298 | } |
| 1299 | if ((constraints & CONSTRAINT_STORAGE_NOT_LOW) != 0) { |
Kweku Adams | 8845d01 | 2018-12-11 20:06:45 -0800 | [diff] [blame] | 1300 | proto.write(fieldId, JobServerProtoEnums.CONSTRAINT_STORAGE_NOT_LOW); |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 1301 | } |
| 1302 | if ((constraints & CONSTRAINT_TIMING_DELAY) != 0) { |
Kweku Adams | 8845d01 | 2018-12-11 20:06:45 -0800 | [diff] [blame] | 1303 | proto.write(fieldId, JobServerProtoEnums.CONSTRAINT_TIMING_DELAY); |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 1304 | } |
| 1305 | if ((constraints & CONSTRAINT_DEADLINE) != 0) { |
Kweku Adams | 8845d01 | 2018-12-11 20:06:45 -0800 | [diff] [blame] | 1306 | proto.write(fieldId, JobServerProtoEnums.CONSTRAINT_DEADLINE); |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 1307 | } |
| 1308 | if ((constraints & CONSTRAINT_IDLE) != 0) { |
Kweku Adams | 8845d01 | 2018-12-11 20:06:45 -0800 | [diff] [blame] | 1309 | proto.write(fieldId, JobServerProtoEnums.CONSTRAINT_IDLE); |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 1310 | } |
| 1311 | if ((constraints & CONSTRAINT_CONNECTIVITY) != 0) { |
Kweku Adams | 8845d01 | 2018-12-11 20:06:45 -0800 | [diff] [blame] | 1312 | proto.write(fieldId, JobServerProtoEnums.CONSTRAINT_CONNECTIVITY); |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 1313 | } |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 1314 | if ((constraints & CONSTRAINT_CONTENT_TRIGGER) != 0) { |
Kweku Adams | 8845d01 | 2018-12-11 20:06:45 -0800 | [diff] [blame] | 1315 | proto.write(fieldId, JobServerProtoEnums.CONSTRAINT_CONTENT_TRIGGER); |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 1316 | } |
| 1317 | if ((constraints & CONSTRAINT_DEVICE_NOT_DOZING) != 0) { |
Kweku Adams | 8845d01 | 2018-12-11 20:06:45 -0800 | [diff] [blame] | 1318 | proto.write(fieldId, JobServerProtoEnums.CONSTRAINT_DEVICE_NOT_DOZING); |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 1319 | } |
Kweku Adams | 4836f9d | 2018-11-12 17:04:17 -0800 | [diff] [blame] | 1320 | if ((constraints & CONSTRAINT_WITHIN_QUOTA) != 0) { |
Kweku Adams | 8845d01 | 2018-12-11 20:06:45 -0800 | [diff] [blame] | 1321 | proto.write(fieldId, JobServerProtoEnums.CONSTRAINT_WITHIN_QUOTA); |
| 1322 | } |
| 1323 | if ((constraints & CONSTRAINT_BACKGROUND_NOT_RESTRICTED) != 0) { |
| 1324 | proto.write(fieldId, JobServerProtoEnums.CONSTRAINT_BACKGROUND_NOT_RESTRICTED); |
Kweku Adams | 4836f9d | 2018-11-12 17:04:17 -0800 | [diff] [blame] | 1325 | } |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 1326 | } |
| 1327 | |
Dianne Hackborn | 342e603 | 2017-04-13 18:04:31 -0700 | [diff] [blame] | 1328 | private void dumpJobWorkItem(PrintWriter pw, String prefix, JobWorkItem work, int index) { |
| 1329 | pw.print(prefix); pw.print(" #"); pw.print(index); pw.print(": #"); |
Dianne Hackborn | 28d1b66 | 2017-04-21 14:17:23 -0700 | [diff] [blame] | 1330 | pw.print(work.getWorkId()); pw.print(" "); pw.print(work.getDeliveryCount()); |
| 1331 | pw.print("x "); pw.println(work.getIntent()); |
Dianne Hackborn | 342e603 | 2017-04-13 18:04:31 -0700 | [diff] [blame] | 1332 | if (work.getGrants() != null) { |
| 1333 | pw.print(prefix); pw.println(" URI grants:"); |
| 1334 | ((GrantedUriPermissions)work.getGrants()).dump(pw, prefix + " "); |
| 1335 | } |
| 1336 | } |
| 1337 | |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 1338 | private void dumpJobWorkItem(ProtoOutputStream proto, long fieldId, JobWorkItem work) { |
| 1339 | final long token = proto.start(fieldId); |
| 1340 | |
| 1341 | proto.write(JobStatusDumpProto.JobWorkItem.WORK_ID, work.getWorkId()); |
| 1342 | proto.write(JobStatusDumpProto.JobWorkItem.DELIVERY_COUNT, work.getDeliveryCount()); |
| 1343 | if (work.getIntent() != null) { |
| 1344 | work.getIntent().writeToProto(proto, JobStatusDumpProto.JobWorkItem.INTENT); |
| 1345 | } |
| 1346 | Object grants = work.getGrants(); |
| 1347 | if (grants != null) { |
| 1348 | ((GrantedUriPermissions) grants).dump(proto, JobStatusDumpProto.JobWorkItem.URI_GRANTS); |
| 1349 | } |
| 1350 | |
| 1351 | proto.end(token); |
| 1352 | } |
| 1353 | |
Kweku Adams | 32f4476 | 2018-11-02 16:58:32 -0700 | [diff] [blame] | 1354 | /** |
| 1355 | * Returns a bucket name based on the normalized bucket indices, not the AppStandby constants. |
| 1356 | */ |
| 1357 | String getBucketName() { |
Kweku Adams | 4836f9d | 2018-11-12 17:04:17 -0800 | [diff] [blame] | 1358 | return bucketName(standbyBucket); |
| 1359 | } |
| 1360 | |
| 1361 | /** |
| 1362 | * Returns a bucket name based on the normalized bucket indices, not the AppStandby constants. |
| 1363 | */ |
| 1364 | static String bucketName(int standbyBucket) { |
Kweku Adams | 32f4476 | 2018-11-02 16:58:32 -0700 | [diff] [blame] | 1365 | switch (standbyBucket) { |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1366 | case 0: return "ACTIVE"; |
| 1367 | case 1: return "WORKING_SET"; |
| 1368 | case 2: return "FREQUENT"; |
| 1369 | case 3: return "RARE"; |
| 1370 | case 4: return "NEVER"; |
| 1371 | default: |
Kweku Adams | 32f4476 | 2018-11-02 16:58:32 -0700 | [diff] [blame] | 1372 | return "Unknown: " + standbyBucket; |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1373 | } |
| 1374 | } |
| 1375 | |
Jeff Sharkey | 9252b34 | 2018-01-19 07:58:35 +0900 | [diff] [blame] | 1376 | private static int resolveTargetSdkVersion(JobInfo job) { |
| 1377 | return LocalServices.getService(PackageManagerInternal.class) |
| 1378 | .getPackageTargetSdkVersion(job.getService().getPackageName()); |
| 1379 | } |
| 1380 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1381 | // Dumpsys infrastructure |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 1382 | public void dump(PrintWriter pw, String prefix, boolean full, long elapsedRealtimeMillis) { |
Shreyas Basarge | 8e64e2e | 2016-02-12 15:49:31 +0000 | [diff] [blame] | 1383 | pw.print(prefix); UserHandle.formatUid(pw, callingUid); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 1384 | pw.print(" tag="); pw.println(tag); |
Christopher Tate | f973a7b | 2014-08-29 12:54:08 -0700 | [diff] [blame] | 1385 | pw.print(prefix); |
Shreyas Basarge | d8bf6b9 | 2016-02-02 23:45:14 +0000 | [diff] [blame] | 1386 | pw.print("Source: uid="); UserHandle.formatUid(pw, getSourceUid()); |
| 1387 | pw.print(" user="); pw.print(getSourceUserId()); |
| 1388 | pw.print(" pkg="); pw.println(getSourcePackageName()); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1389 | if (full) { |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 1390 | pw.print(prefix); pw.println("JobInfo:"); |
| 1391 | pw.print(prefix); pw.print(" Service: "); |
| 1392 | pw.println(job.getService().flattenToShortString()); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1393 | if (job.isPeriodic()) { |
| 1394 | pw.print(prefix); pw.print(" PERIODIC: interval="); |
| 1395 | TimeUtils.formatDuration(job.getIntervalMillis(), pw); |
| 1396 | pw.print(" flex="); TimeUtils.formatDuration(job.getFlexMillis(), pw); |
| 1397 | pw.println(); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 1398 | } |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1399 | if (job.isPersisted()) { |
| 1400 | pw.print(prefix); pw.println(" PERSISTED"); |
| 1401 | } |
| 1402 | if (job.getPriority() != 0) { |
Makoto Onuki | ec8b14d | 2018-12-05 13:22:24 -0800 | [diff] [blame] | 1403 | pw.print(prefix); pw.print(" Priority: "); |
| 1404 | pw.println(JobInfo.getPriorityString(job.getPriority())); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1405 | } |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1406 | if (job.getFlags() != 0) { |
| 1407 | pw.print(prefix); pw.print(" Flags: "); |
| 1408 | pw.println(Integer.toHexString(job.getFlags())); |
| 1409 | } |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 1410 | if (getInternalFlags() != 0) { |
| 1411 | pw.print(prefix); pw.print(" Internal flags: "); |
| 1412 | pw.print(Integer.toHexString(getInternalFlags())); |
| 1413 | |
| 1414 | if ((getInternalFlags()&INTERNAL_FLAG_HAS_FOREGROUND_EXEMPTION) != 0) { |
| 1415 | pw.print(" HAS_FOREGROUND_EXEMPTION"); |
| 1416 | } |
| 1417 | pw.println(); |
| 1418 | } |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1419 | pw.print(prefix); pw.print(" Requires: charging="); |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 1420 | pw.print(job.isRequireCharging()); pw.print(" batteryNotLow="); |
| 1421 | pw.print(job.isRequireBatteryNotLow()); pw.print(" deviceIdle="); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1422 | pw.println(job.isRequireDeviceIdle()); |
| 1423 | if (job.getTriggerContentUris() != null) { |
| 1424 | pw.print(prefix); pw.println(" Trigger content URIs:"); |
| 1425 | for (int i = 0; i < job.getTriggerContentUris().length; i++) { |
| 1426 | JobInfo.TriggerContentUri trig = job.getTriggerContentUris()[i]; |
| 1427 | pw.print(prefix); pw.print(" "); |
| 1428 | pw.print(Integer.toHexString(trig.getFlags())); |
| 1429 | pw.print(' '); pw.println(trig.getUri()); |
| 1430 | } |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 1431 | if (job.getTriggerContentUpdateDelay() >= 0) { |
| 1432 | pw.print(prefix); pw.print(" Trigger update delay: "); |
| 1433 | TimeUtils.formatDuration(job.getTriggerContentUpdateDelay(), pw); |
| 1434 | pw.println(); |
| 1435 | } |
| 1436 | if (job.getTriggerContentMaxDelay() >= 0) { |
| 1437 | pw.print(prefix); pw.print(" Trigger max delay: "); |
| 1438 | TimeUtils.formatDuration(job.getTriggerContentMaxDelay(), pw); |
| 1439 | pw.println(); |
| 1440 | } |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1441 | } |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 1442 | if (job.getExtras() != null && !job.getExtras().maybeIsEmpty()) { |
| 1443 | pw.print(prefix); pw.print(" Extras: "); |
| 1444 | pw.println(job.getExtras().toShortString()); |
| 1445 | } |
| 1446 | if (job.getTransientExtras() != null && !job.getTransientExtras().maybeIsEmpty()) { |
| 1447 | pw.print(prefix); pw.print(" Transient extras: "); |
| 1448 | pw.println(job.getTransientExtras().toShortString()); |
| 1449 | } |
| 1450 | if (job.getClipData() != null) { |
| 1451 | pw.print(prefix); pw.print(" Clip data: "); |
| 1452 | StringBuilder b = new StringBuilder(128); |
| 1453 | job.getClipData().toShortString(b); |
| 1454 | pw.println(b); |
| 1455 | } |
Dianne Hackborn | 342e603 | 2017-04-13 18:04:31 -0700 | [diff] [blame] | 1456 | if (uriPerms != null) { |
| 1457 | pw.print(prefix); pw.println(" Granted URI permissions:"); |
| 1458 | uriPerms.dump(pw, prefix + " "); |
| 1459 | } |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 1460 | if (job.getRequiredNetwork() != null) { |
| 1461 | pw.print(prefix); pw.print(" Network type: "); |
| 1462 | pw.println(job.getRequiredNetwork()); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1463 | } |
Jeff Sharkey | caa3f8d | 2017-10-25 16:12:00 -0600 | [diff] [blame] | 1464 | if (totalNetworkBytes != JobInfo.NETWORK_BYTES_UNKNOWN) { |
Jeff Sharkey | d0fff2e | 2017-11-07 16:55:06 -0700 | [diff] [blame] | 1465 | pw.print(prefix); pw.print(" Network bytes: "); |
| 1466 | pw.println(totalNetworkBytes); |
Jeff Sharkey | caa3f8d | 2017-10-25 16:12:00 -0600 | [diff] [blame] | 1467 | } |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1468 | if (job.getMinLatencyMillis() != 0) { |
| 1469 | pw.print(prefix); pw.print(" Minimum latency: "); |
| 1470 | TimeUtils.formatDuration(job.getMinLatencyMillis(), pw); |
| 1471 | pw.println(); |
| 1472 | } |
| 1473 | if (job.getMaxExecutionDelayMillis() != 0) { |
| 1474 | pw.print(prefix); pw.print(" Max execution delay: "); |
| 1475 | TimeUtils.formatDuration(job.getMaxExecutionDelayMillis(), pw); |
| 1476 | pw.println(); |
| 1477 | } |
| 1478 | pw.print(prefix); pw.print(" Backoff: policy="); pw.print(job.getBackoffPolicy()); |
| 1479 | pw.print(" initial="); TimeUtils.formatDuration(job.getInitialBackoffMillis(), pw); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 1480 | pw.println(); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1481 | if (job.hasEarlyConstraint()) { |
| 1482 | pw.print(prefix); pw.println(" Has early constraint"); |
| 1483 | } |
| 1484 | if (job.hasLateConstraint()) { |
| 1485 | pw.print(prefix); pw.println(" Has late constraint"); |
| 1486 | } |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 1487 | } |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 1488 | pw.print(prefix); pw.print("Required constraints:"); |
| 1489 | dumpConstraints(pw, requiredConstraints); |
| 1490 | pw.println(); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1491 | if (full) { |
| 1492 | pw.print(prefix); pw.print("Satisfied constraints:"); |
| 1493 | dumpConstraints(pw, satisfiedConstraints); |
| 1494 | pw.println(); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1495 | pw.print(prefix); pw.print("Unsatisfied constraints:"); |
Kweku Adams | 4836f9d | 2018-11-12 17:04:17 -0800 | [diff] [blame] | 1496 | dumpConstraints(pw, |
| 1497 | ((requiredConstraints | CONSTRAINT_WITHIN_QUOTA) & ~satisfiedConstraints)); |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1498 | pw.println(); |
Dianne Hackborn | 7ab4025 | 2016-06-15 17:30:24 -0700 | [diff] [blame] | 1499 | if (dozeWhitelisted) { |
| 1500 | pw.print(prefix); pw.println("Doze whitelisted: true"); |
| 1501 | } |
Christopher Tate | 20afddd | 2018-02-28 15:19:19 -0800 | [diff] [blame] | 1502 | if (uidActive) { |
| 1503 | pw.print(prefix); pw.println("Uid: active"); |
| 1504 | } |
Kweku Adams | 4836f9d | 2018-11-12 17:04:17 -0800 | [diff] [blame] | 1505 | if (job.isExemptedFromAppStandby()) { |
| 1506 | pw.print(prefix); pw.println("Is exempted from app standby"); |
| 1507 | } |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1508 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1509 | if (trackingControllers != 0) { |
| 1510 | pw.print(prefix); pw.print("Tracking:"); |
| 1511 | if ((trackingControllers&TRACKING_BATTERY) != 0) pw.print(" BATTERY"); |
| 1512 | if ((trackingControllers&TRACKING_CONNECTIVITY) != 0) pw.print(" CONNECTIVITY"); |
| 1513 | if ((trackingControllers&TRACKING_CONTENT) != 0) pw.print(" CONTENT"); |
| 1514 | if ((trackingControllers&TRACKING_IDLE) != 0) pw.print(" IDLE"); |
| 1515 | if ((trackingControllers&TRACKING_STORAGE) != 0) pw.print(" STORAGE"); |
| 1516 | if ((trackingControllers&TRACKING_TIME) != 0) pw.print(" TIME"); |
Kweku Adams | 4836f9d | 2018-11-12 17:04:17 -0800 | [diff] [blame] | 1517 | if ((trackingControllers & TRACKING_QUOTA) != 0) pw.print(" QUOTA"); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 1518 | pw.println(); |
| 1519 | } |
Kweku Adams | 32f4476 | 2018-11-02 16:58:32 -0700 | [diff] [blame] | 1520 | |
| 1521 | pw.print(prefix); pw.println("Implicit constraints:"); |
| 1522 | pw.print(prefix); pw.print(" readyNotDozing: "); |
| 1523 | pw.println(mReadyNotDozing); |
| 1524 | pw.print(prefix); pw.print(" readyNotRestrictedInBg: "); |
| 1525 | pw.println(mReadyNotRestrictedInBg); |
| 1526 | if (!job.isPeriodic() && hasDeadlineConstraint()) { |
| 1527 | pw.print(prefix); pw.print(" readyDeadlineSatisfied: "); |
| 1528 | pw.println(mReadyDeadlineSatisfied); |
| 1529 | } |
| 1530 | |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 1531 | if (changedAuthorities != null) { |
| 1532 | pw.print(prefix); pw.println("Changed authorities:"); |
| 1533 | for (int i=0; i<changedAuthorities.size(); i++) { |
| 1534 | pw.print(prefix); pw.print(" "); pw.println(changedAuthorities.valueAt(i)); |
| 1535 | } |
| 1536 | if (changedUris != null) { |
| 1537 | pw.print(prefix); pw.println("Changed URIs:"); |
| 1538 | for (int i=0; i<changedUris.size(); i++) { |
| 1539 | pw.print(prefix); pw.print(" "); pw.println(changedUris.valueAt(i)); |
| 1540 | } |
| 1541 | } |
| 1542 | } |
Jeff Sharkey | 76a0241 | 2017-10-24 16:55:04 -0600 | [diff] [blame] | 1543 | if (network != null) { |
| 1544 | pw.print(prefix); pw.print("Network: "); pw.println(network); |
| 1545 | } |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1546 | if (pendingWork != null && pendingWork.size() > 0) { |
| 1547 | pw.print(prefix); pw.println("Pending work:"); |
| 1548 | for (int i = 0; i < pendingWork.size(); i++) { |
Dianne Hackborn | 342e603 | 2017-04-13 18:04:31 -0700 | [diff] [blame] | 1549 | dumpJobWorkItem(pw, prefix, pendingWork.get(i), i); |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1550 | } |
| 1551 | } |
| 1552 | if (executingWork != null && executingWork.size() > 0) { |
| 1553 | pw.print(prefix); pw.println("Executing work:"); |
| 1554 | for (int i = 0; i < executingWork.size(); i++) { |
Dianne Hackborn | 342e603 | 2017-04-13 18:04:31 -0700 | [diff] [blame] | 1555 | dumpJobWorkItem(pw, prefix, executingWork.get(i), i); |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1556 | } |
| 1557 | } |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 1558 | pw.print(prefix); pw.print("Standby bucket: "); |
Kweku Adams | 32f4476 | 2018-11-02 16:58:32 -0700 | [diff] [blame] | 1559 | pw.println(getBucketName()); |
Christopher Tate | d1aebb3 | 2018-01-31 13:24:14 -0800 | [diff] [blame] | 1560 | if (standbyBucket > 0) { |
| 1561 | pw.print(prefix); pw.print("Base heartbeat: "); |
| 1562 | pw.println(baseHeartbeat); |
| 1563 | } |
| 1564 | if (whenStandbyDeferred != 0) { |
| 1565 | pw.print(prefix); pw.print(" Deferred since: "); |
| 1566 | TimeUtils.formatDuration(whenStandbyDeferred, elapsedRealtimeMillis, pw); |
| 1567 | pw.println(); |
| 1568 | } |
Dianne Hackborn | bfc2331 | 2017-05-11 11:53:24 -0700 | [diff] [blame] | 1569 | pw.print(prefix); pw.print("Enqueue time: "); |
| 1570 | TimeUtils.formatDuration(enqueueTime, elapsedRealtimeMillis, pw); |
| 1571 | pw.println(); |
| 1572 | pw.print(prefix); pw.print("Run time: earliest="); |
| 1573 | formatRunTime(pw, earliestRunTimeElapsedMillis, NO_EARLIEST_RUNTIME, elapsedRealtimeMillis); |
| 1574 | pw.print(", latest="); |
| 1575 | formatRunTime(pw, latestRunTimeElapsedMillis, NO_LATEST_RUNTIME, elapsedRealtimeMillis); |
| 1576 | pw.println(); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 1577 | if (numFailures != 0) { |
| 1578 | pw.print(prefix); pw.print("Num failures: "); pw.println(numFailures); |
| 1579 | } |
Makoto Onuki | ab8a67f | 2017-06-20 12:20:34 -0700 | [diff] [blame] | 1580 | final Time t = new Time(); |
| 1581 | final String format = "%Y-%m-%d %H:%M:%S"; |
| 1582 | if (mLastSuccessfulRunTime != 0) { |
| 1583 | pw.print(prefix); pw.print("Last successful run: "); |
| 1584 | t.set(mLastSuccessfulRunTime); |
| 1585 | pw.println(t.format(format)); |
| 1586 | } |
| 1587 | if (mLastFailedRunTime != 0) { |
| 1588 | pw.print(prefix); pw.print("Last failed run: "); |
| 1589 | t.set(mLastFailedRunTime); |
| 1590 | pw.println(t.format(format)); |
| 1591 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1592 | } |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 1593 | |
| 1594 | public void dump(ProtoOutputStream proto, long fieldId, boolean full, long elapsedRealtimeMillis) { |
| 1595 | final long token = proto.start(fieldId); |
| 1596 | |
| 1597 | proto.write(JobStatusDumpProto.CALLING_UID, callingUid); |
| 1598 | proto.write(JobStatusDumpProto.TAG, tag); |
| 1599 | proto.write(JobStatusDumpProto.SOURCE_UID, getSourceUid()); |
| 1600 | proto.write(JobStatusDumpProto.SOURCE_USER_ID, getSourceUserId()); |
| 1601 | proto.write(JobStatusDumpProto.SOURCE_PACKAGE_NAME, getSourcePackageName()); |
Makoto Onuki | 1540784 | 2018-01-19 14:23:11 -0800 | [diff] [blame] | 1602 | proto.write(JobStatusDumpProto.INTERNAL_FLAGS, getInternalFlags()); |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 1603 | |
| 1604 | if (full) { |
| 1605 | final long jiToken = proto.start(JobStatusDumpProto.JOB_INFO); |
| 1606 | |
| 1607 | job.getService().writeToProto(proto, JobStatusDumpProto.JobInfo.SERVICE); |
| 1608 | |
| 1609 | proto.write(JobStatusDumpProto.JobInfo.IS_PERIODIC, job.isPeriodic()); |
| 1610 | proto.write(JobStatusDumpProto.JobInfo.PERIOD_INTERVAL_MS, job.getIntervalMillis()); |
| 1611 | proto.write(JobStatusDumpProto.JobInfo.PERIOD_FLEX_MS, job.getFlexMillis()); |
| 1612 | |
| 1613 | proto.write(JobStatusDumpProto.JobInfo.IS_PERSISTED, job.isPersisted()); |
| 1614 | proto.write(JobStatusDumpProto.JobInfo.PRIORITY, job.getPriority()); |
| 1615 | proto.write(JobStatusDumpProto.JobInfo.FLAGS, job.getFlags()); |
| 1616 | |
| 1617 | proto.write(JobStatusDumpProto.JobInfo.REQUIRES_CHARGING, job.isRequireCharging()); |
| 1618 | proto.write(JobStatusDumpProto.JobInfo.REQUIRES_BATTERY_NOT_LOW, job.isRequireBatteryNotLow()); |
| 1619 | proto.write(JobStatusDumpProto.JobInfo.REQUIRES_DEVICE_IDLE, job.isRequireDeviceIdle()); |
| 1620 | |
| 1621 | if (job.getTriggerContentUris() != null) { |
| 1622 | for (int i = 0; i < job.getTriggerContentUris().length; i++) { |
| 1623 | final long tcuToken = proto.start(JobStatusDumpProto.JobInfo.TRIGGER_CONTENT_URIS); |
| 1624 | JobInfo.TriggerContentUri trig = job.getTriggerContentUris()[i]; |
| 1625 | |
| 1626 | proto.write(JobStatusDumpProto.JobInfo.TriggerContentUri.FLAGS, trig.getFlags()); |
| 1627 | Uri u = trig.getUri(); |
| 1628 | if (u != null) { |
| 1629 | proto.write(JobStatusDumpProto.JobInfo.TriggerContentUri.URI, u.toString()); |
| 1630 | } |
| 1631 | |
| 1632 | proto.end(tcuToken); |
| 1633 | } |
| 1634 | if (job.getTriggerContentUpdateDelay() >= 0) { |
| 1635 | proto.write(JobStatusDumpProto.JobInfo.TRIGGER_CONTENT_UPDATE_DELAY_MS, |
| 1636 | job.getTriggerContentUpdateDelay()); |
| 1637 | } |
| 1638 | if (job.getTriggerContentMaxDelay() >= 0) { |
| 1639 | proto.write(JobStatusDumpProto.JobInfo.TRIGGER_CONTENT_MAX_DELAY_MS, |
| 1640 | job.getTriggerContentMaxDelay()); |
| 1641 | } |
| 1642 | } |
| 1643 | if (job.getExtras() != null && !job.getExtras().maybeIsEmpty()) { |
| 1644 | job.getExtras().writeToProto(proto, JobStatusDumpProto.JobInfo.EXTRAS); |
| 1645 | } |
| 1646 | if (job.getTransientExtras() != null && !job.getTransientExtras().maybeIsEmpty()) { |
| 1647 | job.getTransientExtras().writeToProto(proto, JobStatusDumpProto.JobInfo.TRANSIENT_EXTRAS); |
| 1648 | } |
| 1649 | if (job.getClipData() != null) { |
| 1650 | job.getClipData().writeToProto(proto, JobStatusDumpProto.JobInfo.CLIP_DATA); |
| 1651 | } |
| 1652 | if (uriPerms != null) { |
| 1653 | uriPerms.dump(proto, JobStatusDumpProto.JobInfo.GRANTED_URI_PERMISSIONS); |
| 1654 | } |
| 1655 | if (job.getRequiredNetwork() != null) { |
| 1656 | job.getRequiredNetwork().writeToProto(proto, JobStatusDumpProto.JobInfo.REQUIRED_NETWORK); |
| 1657 | } |
| 1658 | if (totalNetworkBytes != JobInfo.NETWORK_BYTES_UNKNOWN) { |
| 1659 | proto.write(JobStatusDumpProto.JobInfo.TOTAL_NETWORK_BYTES, totalNetworkBytes); |
| 1660 | } |
| 1661 | proto.write(JobStatusDumpProto.JobInfo.MIN_LATENCY_MS, job.getMinLatencyMillis()); |
| 1662 | proto.write(JobStatusDumpProto.JobInfo.MAX_EXECUTION_DELAY_MS, job.getMaxExecutionDelayMillis()); |
| 1663 | |
| 1664 | final long bpToken = proto.start(JobStatusDumpProto.JobInfo.BACKOFF_POLICY); |
| 1665 | proto.write(JobStatusDumpProto.JobInfo.Backoff.POLICY, job.getBackoffPolicy()); |
| 1666 | proto.write(JobStatusDumpProto.JobInfo.Backoff.INITIAL_BACKOFF_MS, |
| 1667 | job.getInitialBackoffMillis()); |
| 1668 | proto.end(bpToken); |
| 1669 | |
| 1670 | proto.write(JobStatusDumpProto.JobInfo.HAS_EARLY_CONSTRAINT, job.hasEarlyConstraint()); |
| 1671 | proto.write(JobStatusDumpProto.JobInfo.HAS_LATE_CONSTRAINT, job.hasLateConstraint()); |
| 1672 | |
| 1673 | proto.end(jiToken); |
| 1674 | } |
| 1675 | |
| 1676 | dumpConstraints(proto, JobStatusDumpProto.REQUIRED_CONSTRAINTS, requiredConstraints); |
| 1677 | if (full) { |
| 1678 | dumpConstraints(proto, JobStatusDumpProto.SATISFIED_CONSTRAINTS, satisfiedConstraints); |
| 1679 | dumpConstraints(proto, JobStatusDumpProto.UNSATISFIED_CONSTRAINTS, |
Kweku Adams | 4836f9d | 2018-11-12 17:04:17 -0800 | [diff] [blame] | 1680 | ((requiredConstraints | CONSTRAINT_WITHIN_QUOTA) & ~satisfiedConstraints)); |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 1681 | proto.write(JobStatusDumpProto.IS_DOZE_WHITELISTED, dozeWhitelisted); |
Kweku Adams | 4836f9d | 2018-11-12 17:04:17 -0800 | [diff] [blame] | 1682 | proto.write(JobStatusDumpProto.IS_UID_ACTIVE, uidActive); |
| 1683 | proto.write(JobStatusDumpProto.IS_EXEMPTED_FROM_APP_STANDBY, |
| 1684 | job.isExemptedFromAppStandby()); |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | // Tracking controllers |
| 1688 | if ((trackingControllers&TRACKING_BATTERY) != 0) { |
| 1689 | proto.write(JobStatusDumpProto.TRACKING_CONTROLLERS, |
| 1690 | JobStatusDumpProto.TRACKING_BATTERY); |
| 1691 | } |
| 1692 | if ((trackingControllers&TRACKING_CONNECTIVITY) != 0) { |
| 1693 | proto.write(JobStatusDumpProto.TRACKING_CONTROLLERS, |
| 1694 | JobStatusDumpProto.TRACKING_CONNECTIVITY); |
| 1695 | } |
| 1696 | if ((trackingControllers&TRACKING_CONTENT) != 0) { |
| 1697 | proto.write(JobStatusDumpProto.TRACKING_CONTROLLERS, |
| 1698 | JobStatusDumpProto.TRACKING_CONTENT); |
| 1699 | } |
| 1700 | if ((trackingControllers&TRACKING_IDLE) != 0) { |
| 1701 | proto.write(JobStatusDumpProto.TRACKING_CONTROLLERS, |
| 1702 | JobStatusDumpProto.TRACKING_IDLE); |
| 1703 | } |
| 1704 | if ((trackingControllers&TRACKING_STORAGE) != 0) { |
| 1705 | proto.write(JobStatusDumpProto.TRACKING_CONTROLLERS, |
| 1706 | JobStatusDumpProto.TRACKING_STORAGE); |
| 1707 | } |
| 1708 | if ((trackingControllers&TRACKING_TIME) != 0) { |
| 1709 | proto.write(JobStatusDumpProto.TRACKING_CONTROLLERS, |
| 1710 | JobStatusDumpProto.TRACKING_TIME); |
| 1711 | } |
Kweku Adams | 4836f9d | 2018-11-12 17:04:17 -0800 | [diff] [blame] | 1712 | if ((trackingControllers & TRACKING_QUOTA) != 0) { |
| 1713 | proto.write(JobStatusDumpProto.TRACKING_CONTROLLERS, |
| 1714 | JobStatusDumpProto.TRACKING_QUOTA); |
| 1715 | } |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 1716 | |
Kweku Adams | 32f4476 | 2018-11-02 16:58:32 -0700 | [diff] [blame] | 1717 | // Implicit constraints |
| 1718 | final long icToken = proto.start(JobStatusDumpProto.IMPLICIT_CONSTRAINTS); |
| 1719 | proto.write(JobStatusDumpProto.ImplicitConstraints.IS_NOT_DOZING, mReadyNotDozing); |
| 1720 | proto.write(JobStatusDumpProto.ImplicitConstraints.IS_NOT_RESTRICTED_IN_BG, |
| 1721 | mReadyNotRestrictedInBg); |
| 1722 | proto.end(icToken); |
| 1723 | |
Kweku Adams | 85f2fbc | 2017-12-18 12:04:12 -0800 | [diff] [blame] | 1724 | if (changedAuthorities != null) { |
| 1725 | for (int k = 0; k < changedAuthorities.size(); k++) { |
| 1726 | proto.write(JobStatusDumpProto.CHANGED_AUTHORITIES, changedAuthorities.valueAt(k)); |
| 1727 | } |
| 1728 | } |
| 1729 | if (changedUris != null) { |
| 1730 | for (int i = 0; i < changedUris.size(); i++) { |
| 1731 | Uri u = changedUris.valueAt(i); |
| 1732 | proto.write(JobStatusDumpProto.CHANGED_URIS, u.toString()); |
| 1733 | } |
| 1734 | } |
| 1735 | |
| 1736 | if (network != null) { |
| 1737 | network.writeToProto(proto, JobStatusDumpProto.NETWORK); |
| 1738 | } |
| 1739 | |
| 1740 | if (pendingWork != null && pendingWork.size() > 0) { |
| 1741 | for (int i = 0; i < pendingWork.size(); i++) { |
| 1742 | dumpJobWorkItem(proto, JobStatusDumpProto.PENDING_WORK, pendingWork.get(i)); |
| 1743 | } |
| 1744 | } |
| 1745 | if (executingWork != null && executingWork.size() > 0) { |
| 1746 | for (int i = 0; i < executingWork.size(); i++) { |
| 1747 | dumpJobWorkItem(proto, JobStatusDumpProto.EXECUTING_WORK, executingWork.get(i)); |
| 1748 | } |
| 1749 | } |
| 1750 | |
| 1751 | proto.write(JobStatusDumpProto.STANDBY_BUCKET, standbyBucket); |
| 1752 | proto.write(JobStatusDumpProto.ENQUEUE_DURATION_MS, elapsedRealtimeMillis - enqueueTime); |
| 1753 | if (earliestRunTimeElapsedMillis == NO_EARLIEST_RUNTIME) { |
| 1754 | proto.write(JobStatusDumpProto.TIME_UNTIL_EARLIEST_RUNTIME_MS, 0); |
| 1755 | } else { |
| 1756 | proto.write(JobStatusDumpProto.TIME_UNTIL_EARLIEST_RUNTIME_MS, |
| 1757 | earliestRunTimeElapsedMillis - elapsedRealtimeMillis); |
| 1758 | } |
| 1759 | if (latestRunTimeElapsedMillis == NO_LATEST_RUNTIME) { |
| 1760 | proto.write(JobStatusDumpProto.TIME_UNTIL_LATEST_RUNTIME_MS, 0); |
| 1761 | } else { |
| 1762 | proto.write(JobStatusDumpProto.TIME_UNTIL_LATEST_RUNTIME_MS, |
| 1763 | latestRunTimeElapsedMillis - elapsedRealtimeMillis); |
| 1764 | } |
| 1765 | |
| 1766 | proto.write(JobStatusDumpProto.NUM_FAILURES, numFailures); |
| 1767 | proto.write(JobStatusDumpProto.LAST_SUCCESSFUL_RUN_TIME, mLastSuccessfulRunTime); |
| 1768 | proto.write(JobStatusDumpProto.LAST_FAILED_RUN_TIME, mLastFailedRunTime); |
| 1769 | |
| 1770 | proto.end(token); |
| 1771 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1772 | } |