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; |
| 18 | |
Christopher Tate | b5c0788 | 2016-05-26 17:11:09 -0700 | [diff] [blame] | 19 | import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED; |
| 20 | import static android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED_USER; |
| 21 | |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 22 | import java.io.FileDescriptor; |
| 23 | import java.io.PrintWriter; |
| 24 | import java.util.ArrayList; |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 25 | import java.util.Arrays; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 26 | import java.util.Collections; |
| 27 | import java.util.Comparator; |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 28 | import java.util.Iterator; |
| 29 | import java.util.List; |
| 30 | |
Christopher Tate | ee7805b | 2016-07-15 16:56:56 -0700 | [diff] [blame] | 31 | import android.app.Activity; |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 32 | import android.app.ActivityManager; |
Christopher Tate | 5568f54 | 2014-06-18 13:53:31 -0700 | [diff] [blame] | 33 | import android.app.AppGlobals; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 34 | import android.app.IUidObserver; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 35 | import android.app.job.JobInfo; |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 36 | import android.app.job.JobParameters; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 37 | import android.app.job.JobScheduler; |
| 38 | import android.app.job.JobService; |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 39 | import android.app.job.IJobScheduler; |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 40 | import android.app.job.JobWorkItem; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 41 | import android.content.BroadcastReceiver; |
| 42 | import android.content.ComponentName; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 43 | import android.content.ContentResolver; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 44 | import android.content.Context; |
| 45 | import android.content.Intent; |
| 46 | import android.content.IntentFilter; |
Christopher Tate | 5568f54 | 2014-06-18 13:53:31 -0700 | [diff] [blame] | 47 | import android.content.pm.IPackageManager; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 48 | import android.content.pm.PackageManager; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 49 | import android.content.pm.ServiceInfo; |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 50 | import android.content.pm.PackageManager.NameNotFoundException; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 51 | import android.database.ContentObserver; |
Christopher Tate | b5c0788 | 2016-05-26 17:11:09 -0700 | [diff] [blame] | 52 | import android.net.Uri; |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 53 | import android.os.BatteryStats; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 54 | import android.os.Binder; |
| 55 | import android.os.Handler; |
| 56 | import android.os.Looper; |
| 57 | import android.os.Message; |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 58 | import android.os.Process; |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 59 | import android.os.PowerManager; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 60 | import android.os.RemoteException; |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 61 | import android.os.ResultReceiver; |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 62 | import android.os.ServiceManager; |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 63 | import android.os.ShellCallback; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 64 | import android.os.SystemClock; |
| 65 | import android.os.UserHandle; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 66 | import android.provider.Settings; |
| 67 | import android.util.KeyValueListParser; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 68 | import android.util.Slog; |
| 69 | import android.util.SparseArray; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 70 | import android.util.SparseIntArray; |
| 71 | import android.util.TimeUtils; |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 72 | |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 73 | import com.android.internal.app.IBatteryStats; |
Joe Onorato | 4eb64fd | 2016-03-21 15:30:09 -0700 | [diff] [blame] | 74 | import com.android.internal.app.procstats.ProcessStats; |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 75 | import com.android.internal.util.ArrayUtils; |
Jeff Sharkey | fe9a53b | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 76 | import com.android.internal.util.DumpUtils; |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 77 | import com.android.server.DeviceIdleController; |
| 78 | import com.android.server.LocalServices; |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 79 | import com.android.server.job.JobStore.JobStatusFunctor; |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 80 | import com.android.server.job.controllers.AppIdleController; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 81 | import com.android.server.job.controllers.BatteryController; |
| 82 | import com.android.server.job.controllers.ConnectivityController; |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 83 | import com.android.server.job.controllers.ContentObserverController; |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 84 | import com.android.server.job.controllers.DeviceIdleJobsController; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 85 | import com.android.server.job.controllers.IdleController; |
| 86 | import com.android.server.job.controllers.JobStatus; |
| 87 | import com.android.server.job.controllers.StateController; |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 88 | import com.android.server.job.controllers.StorageController; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 89 | import com.android.server.job.controllers.TimeController; |
| 90 | |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 91 | import libcore.util.EmptyArray; |
| 92 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 93 | /** |
| 94 | * Responsible for taking jobs representing work to be performed by a client app, and determining |
| 95 | * based on the criteria specified when that job should be run against the client application's |
| 96 | * endpoint. |
| 97 | * Implements logic for scheduling, and rescheduling jobs. The JobSchedulerService knows nothing |
| 98 | * about constraints, or the state of active jobs. It receives callbacks from the various |
| 99 | * controllers and completed jobs and operates accordingly. |
| 100 | * |
| 101 | * Note on locking: Any operations that manipulate {@link #mJobs} need to lock on that object. |
| 102 | * Any function with the suffix 'Locked' also needs to lock on {@link #mJobs}. |
| 103 | * @hide |
| 104 | */ |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 105 | public final class JobSchedulerService extends com.android.server.SystemService |
Matthew Williams | 01ac45b | 2014-07-22 20:44:12 -0700 | [diff] [blame] | 106 | implements StateChangedListener, JobCompletedListener { |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 107 | static final String TAG = "JobSchedulerService"; |
Matthew Williams | aa98431 | 2015-10-15 16:08:05 -0700 | [diff] [blame] | 108 | public static final boolean DEBUG = false; |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 109 | |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 110 | /** The maximum number of concurrent jobs we run at one time. */ |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 111 | private static final int MAX_JOB_CONTEXTS_COUNT = 16; |
Christopher Tate | dabdf6f | 2016-02-24 12:30:22 -0800 | [diff] [blame] | 112 | /** Enforce a per-app limit on scheduled jobs? */ |
Christopher Tate | 0213ace0 | 2016-02-24 14:18:35 -0800 | [diff] [blame] | 113 | private static final boolean ENFORCE_MAX_JOBS = true; |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 114 | /** The maximum number of jobs that we allow an unprivileged app to schedule */ |
| 115 | private static final int MAX_JOBS_PER_APP = 100; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 116 | |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 117 | |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 118 | /** Global local for all job scheduler state. */ |
| 119 | final Object mLock = new Object(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 120 | /** Master list of jobs. */ |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 121 | final JobStore mJobs; |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 122 | /** Tracking amount of time each package runs for. */ |
| 123 | final JobPackageTracker mJobPackageTracker = new JobPackageTracker(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 124 | |
| 125 | static final int MSG_JOB_EXPIRED = 0; |
| 126 | static final int MSG_CHECK_JOB = 1; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 127 | static final int MSG_STOP_JOB = 2; |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 128 | static final int MSG_CHECK_JOB_GREEDY = 3; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 129 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 130 | /** |
| 131 | * Track Services that have currently active or pending jobs. The index is provided by |
| 132 | * {@link JobStatus#getServiceToken()} |
| 133 | */ |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 134 | final List<JobServiceContext> mActiveServices = new ArrayList<>(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 135 | /** List of controllers that will notify this service of updates to jobs. */ |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 136 | List<StateController> mControllers; |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 137 | /** Need direct access to this for testing. */ |
| 138 | BatteryController mBatteryController; |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 139 | /** Need direct access to this for testing. */ |
| 140 | StorageController mStorageController; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 141 | /** |
| 142 | * Queue of pending jobs. The JobServiceContext class will receive jobs from this list |
| 143 | * when ready to execute them. |
| 144 | */ |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 145 | final ArrayList<JobStatus> mPendingJobs = new ArrayList<>(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 146 | |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 147 | int[] mStartedUsers = EmptyArray.INT; |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 148 | |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 149 | final JobHandler mHandler; |
| 150 | final JobSchedulerStub mJobSchedulerStub; |
| 151 | |
| 152 | IBatteryStats mBatteryStats; |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 153 | PowerManager mPowerManager; |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 154 | DeviceIdleController.LocalService mLocalDeviceIdleController; |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 155 | |
| 156 | /** |
| 157 | * Set to true once we are allowed to run third party apps. |
| 158 | */ |
| 159 | boolean mReadyToRock; |
| 160 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 161 | /** |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 162 | * What we last reported to DeviceIdleController about whether we are active. |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 163 | */ |
| 164 | boolean mReportedActive; |
| 165 | |
| 166 | /** |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 167 | * Current limit on the number of concurrent JobServiceContext entries we want to |
| 168 | * keep actively running a job. |
| 169 | */ |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 170 | int mMaxActiveJobs = 1; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 171 | |
| 172 | /** |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 173 | * Which uids are currently in the foreground. |
| 174 | */ |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 175 | final SparseIntArray mUidPriorityOverride = new SparseIntArray(); |
| 176 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame^] | 177 | /** |
| 178 | * Which uids are currently performing backups, so we shouldn't allow their jobs to run. |
| 179 | */ |
| 180 | final SparseIntArray mBackingUpUids = new SparseIntArray(); |
| 181 | |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 182 | // -- Pre-allocated temporaries only for use in assignJobsToContextsLocked -- |
| 183 | |
| 184 | /** |
| 185 | * This array essentially stores the state of mActiveServices array. |
| 186 | * The ith index stores the job present on the ith JobServiceContext. |
| 187 | * We manipulate this array until we arrive at what jobs should be running on |
| 188 | * what JobServiceContext. |
| 189 | */ |
| 190 | JobStatus[] mTmpAssignContextIdToJobMap = new JobStatus[MAX_JOB_CONTEXTS_COUNT]; |
| 191 | /** |
| 192 | * Indicates whether we need to act on this jobContext id |
| 193 | */ |
| 194 | boolean[] mTmpAssignAct = new boolean[MAX_JOB_CONTEXTS_COUNT]; |
| 195 | /** |
| 196 | * The uid whose jobs we would like to assign to a context. |
| 197 | */ |
| 198 | int[] mTmpAssignPreferredUidForContext = new int[MAX_JOB_CONTEXTS_COUNT]; |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 199 | |
| 200 | /** |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 201 | * All times are in milliseconds. These constants are kept synchronized with the system |
| 202 | * global Settings. Any access to this class or its fields should be done while |
| 203 | * holding the JobSchedulerService.mLock lock. |
| 204 | */ |
| 205 | private final class Constants extends ContentObserver { |
| 206 | // Key names stored in the settings value. |
| 207 | private static final String KEY_MIN_IDLE_COUNT = "min_idle_count"; |
| 208 | private static final String KEY_MIN_CHARGING_COUNT = "min_charging_count"; |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 209 | private static final String KEY_MIN_BATTERY_NOT_LOW_COUNT = "min_battery_not_low_count"; |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 210 | private static final String KEY_MIN_STORAGE_NOT_LOW_COUNT = "min_storage_not_low_count"; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 211 | private static final String KEY_MIN_CONNECTIVITY_COUNT = "min_connectivity_count"; |
| 212 | private static final String KEY_MIN_CONTENT_COUNT = "min_content_count"; |
| 213 | private static final String KEY_MIN_READY_JOBS_COUNT = "min_ready_jobs_count"; |
| 214 | private static final String KEY_HEAVY_USE_FACTOR = "heavy_use_factor"; |
| 215 | private static final String KEY_MODERATE_USE_FACTOR = "moderate_use_factor"; |
| 216 | private static final String KEY_FG_JOB_COUNT = "fg_job_count"; |
| 217 | private static final String KEY_BG_NORMAL_JOB_COUNT = "bg_normal_job_count"; |
| 218 | private static final String KEY_BG_MODERATE_JOB_COUNT = "bg_moderate_job_count"; |
| 219 | private static final String KEY_BG_LOW_JOB_COUNT = "bg_low_job_count"; |
| 220 | private static final String KEY_BG_CRITICAL_JOB_COUNT = "bg_critical_job_count"; |
| 221 | |
| 222 | private static final int DEFAULT_MIN_IDLE_COUNT = 1; |
| 223 | private static final int DEFAULT_MIN_CHARGING_COUNT = 1; |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 224 | private static final int DEFAULT_MIN_BATTERY_NOT_LOW_COUNT = 1; |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 225 | private static final int DEFAULT_MIN_STORAGE_NOT_LOW_COUNT = 1; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 226 | private static final int DEFAULT_MIN_CONNECTIVITY_COUNT = 1; |
| 227 | private static final int DEFAULT_MIN_CONTENT_COUNT = 1; |
| 228 | private static final int DEFAULT_MIN_READY_JOBS_COUNT = 1; |
| 229 | private static final float DEFAULT_HEAVY_USE_FACTOR = .9f; |
| 230 | private static final float DEFAULT_MODERATE_USE_FACTOR = .5f; |
| 231 | private static final int DEFAULT_FG_JOB_COUNT = 4; |
| 232 | private static final int DEFAULT_BG_NORMAL_JOB_COUNT = 6; |
| 233 | private static final int DEFAULT_BG_MODERATE_JOB_COUNT = 4; |
Nancy Zheng | e39a8a4 | 2016-10-05 16:27:14 -0700 | [diff] [blame] | 234 | private static final int DEFAULT_BG_LOW_JOB_COUNT = 1; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 235 | private static final int DEFAULT_BG_CRITICAL_JOB_COUNT = 1; |
| 236 | |
| 237 | /** |
| 238 | * Minimum # of idle jobs that must be ready in order to force the JMS to schedule things |
| 239 | * early. |
| 240 | */ |
| 241 | int MIN_IDLE_COUNT = DEFAULT_MIN_IDLE_COUNT; |
| 242 | /** |
| 243 | * Minimum # of charging jobs that must be ready in order to force the JMS to schedule |
| 244 | * things early. |
| 245 | */ |
| 246 | int MIN_CHARGING_COUNT = DEFAULT_MIN_CHARGING_COUNT; |
| 247 | /** |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 248 | * Minimum # of "battery not low" jobs that must be ready in order to force the JMS to |
| 249 | * schedule things early. |
| 250 | */ |
| 251 | int MIN_BATTERY_NOT_LOW_COUNT = DEFAULT_MIN_BATTERY_NOT_LOW_COUNT; |
| 252 | /** |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 253 | * Minimum # of "storage not low" jobs that must be ready in order to force the JMS to |
| 254 | * schedule things early. |
| 255 | */ |
| 256 | int MIN_STORAGE_NOT_LOW_COUNT = DEFAULT_MIN_STORAGE_NOT_LOW_COUNT; |
| 257 | /** |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 258 | * Minimum # of connectivity jobs that must be ready in order to force the JMS to schedule |
| 259 | * things early. 1 == Run connectivity jobs as soon as ready. |
| 260 | */ |
| 261 | int MIN_CONNECTIVITY_COUNT = DEFAULT_MIN_CONNECTIVITY_COUNT; |
| 262 | /** |
| 263 | * Minimum # of content trigger jobs that must be ready in order to force the JMS to |
| 264 | * schedule things early. |
| 265 | */ |
| 266 | int MIN_CONTENT_COUNT = DEFAULT_MIN_CONTENT_COUNT; |
| 267 | /** |
| 268 | * Minimum # of jobs (with no particular constraints) for which the JMS will be happy |
| 269 | * running some work early. This (and thus the other min counts) is now set to 1, to |
| 270 | * prevent any batching at this level. Since we now do batching through doze, that is |
| 271 | * a much better mechanism. |
| 272 | */ |
| 273 | int MIN_READY_JOBS_COUNT = DEFAULT_MIN_READY_JOBS_COUNT; |
| 274 | /** |
| 275 | * This is the job execution factor that is considered to be heavy use of the system. |
| 276 | */ |
| 277 | float HEAVY_USE_FACTOR = DEFAULT_HEAVY_USE_FACTOR; |
| 278 | /** |
| 279 | * This is the job execution factor that is considered to be moderate use of the system. |
| 280 | */ |
| 281 | float MODERATE_USE_FACTOR = DEFAULT_MODERATE_USE_FACTOR; |
| 282 | /** |
| 283 | * The number of MAX_JOB_CONTEXTS_COUNT we reserve for the foreground app. |
| 284 | */ |
| 285 | int FG_JOB_COUNT = DEFAULT_FG_JOB_COUNT; |
| 286 | /** |
| 287 | * The maximum number of background jobs we allow when the system is in a normal |
| 288 | * memory state. |
| 289 | */ |
| 290 | int BG_NORMAL_JOB_COUNT = DEFAULT_BG_NORMAL_JOB_COUNT; |
| 291 | /** |
| 292 | * The maximum number of background jobs we allow when the system is in a moderate |
| 293 | * memory state. |
| 294 | */ |
| 295 | int BG_MODERATE_JOB_COUNT = DEFAULT_BG_MODERATE_JOB_COUNT; |
| 296 | /** |
| 297 | * The maximum number of background jobs we allow when the system is in a low |
| 298 | * memory state. |
| 299 | */ |
| 300 | int BG_LOW_JOB_COUNT = DEFAULT_BG_LOW_JOB_COUNT; |
| 301 | /** |
| 302 | * The maximum number of background jobs we allow when the system is in a critical |
| 303 | * memory state. |
| 304 | */ |
| 305 | int BG_CRITICAL_JOB_COUNT = DEFAULT_BG_CRITICAL_JOB_COUNT; |
| 306 | |
| 307 | private ContentResolver mResolver; |
| 308 | private final KeyValueListParser mParser = new KeyValueListParser(','); |
| 309 | |
| 310 | public Constants(Handler handler) { |
| 311 | super(handler); |
| 312 | } |
| 313 | |
| 314 | public void start(ContentResolver resolver) { |
| 315 | mResolver = resolver; |
| 316 | mResolver.registerContentObserver(Settings.Global.getUriFor( |
| 317 | Settings.Global.JOB_SCHEDULER_CONSTANTS), false, this); |
| 318 | updateConstants(); |
| 319 | } |
| 320 | |
| 321 | @Override |
| 322 | public void onChange(boolean selfChange, Uri uri) { |
| 323 | updateConstants(); |
| 324 | } |
| 325 | |
| 326 | private void updateConstants() { |
| 327 | synchronized (mLock) { |
| 328 | try { |
| 329 | mParser.setString(Settings.Global.getString(mResolver, |
| 330 | Settings.Global.ALARM_MANAGER_CONSTANTS)); |
| 331 | } catch (IllegalArgumentException e) { |
| 332 | // Failed to parse the settings string, log this and move on |
| 333 | // with defaults. |
| 334 | Slog.e(TAG, "Bad device idle settings", e); |
| 335 | } |
| 336 | |
| 337 | MIN_IDLE_COUNT = mParser.getInt(KEY_MIN_IDLE_COUNT, |
| 338 | DEFAULT_MIN_IDLE_COUNT); |
| 339 | MIN_CHARGING_COUNT = mParser.getInt(KEY_MIN_CHARGING_COUNT, |
| 340 | DEFAULT_MIN_CHARGING_COUNT); |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 341 | MIN_BATTERY_NOT_LOW_COUNT = mParser.getInt(KEY_MIN_BATTERY_NOT_LOW_COUNT, |
| 342 | DEFAULT_MIN_BATTERY_NOT_LOW_COUNT); |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 343 | MIN_STORAGE_NOT_LOW_COUNT = mParser.getInt(KEY_MIN_STORAGE_NOT_LOW_COUNT, |
| 344 | DEFAULT_MIN_STORAGE_NOT_LOW_COUNT); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 345 | MIN_CONNECTIVITY_COUNT = mParser.getInt(KEY_MIN_CONNECTIVITY_COUNT, |
| 346 | DEFAULT_MIN_CONNECTIVITY_COUNT); |
| 347 | MIN_CONTENT_COUNT = mParser.getInt(KEY_MIN_CONTENT_COUNT, |
| 348 | DEFAULT_MIN_CONTENT_COUNT); |
| 349 | MIN_READY_JOBS_COUNT = mParser.getInt(KEY_MIN_READY_JOBS_COUNT, |
| 350 | DEFAULT_MIN_READY_JOBS_COUNT); |
| 351 | HEAVY_USE_FACTOR = mParser.getFloat(KEY_HEAVY_USE_FACTOR, |
| 352 | DEFAULT_HEAVY_USE_FACTOR); |
| 353 | MODERATE_USE_FACTOR = mParser.getFloat(KEY_MODERATE_USE_FACTOR, |
| 354 | DEFAULT_MODERATE_USE_FACTOR); |
| 355 | FG_JOB_COUNT = mParser.getInt(KEY_FG_JOB_COUNT, |
| 356 | DEFAULT_FG_JOB_COUNT); |
| 357 | BG_NORMAL_JOB_COUNT = mParser.getInt(KEY_BG_NORMAL_JOB_COUNT, |
| 358 | DEFAULT_BG_NORMAL_JOB_COUNT); |
| 359 | if ((FG_JOB_COUNT+BG_NORMAL_JOB_COUNT) > MAX_JOB_CONTEXTS_COUNT) { |
| 360 | BG_NORMAL_JOB_COUNT = MAX_JOB_CONTEXTS_COUNT - FG_JOB_COUNT; |
| 361 | } |
| 362 | BG_MODERATE_JOB_COUNT = mParser.getInt(KEY_BG_MODERATE_JOB_COUNT, |
| 363 | DEFAULT_BG_MODERATE_JOB_COUNT); |
| 364 | if ((FG_JOB_COUNT+BG_MODERATE_JOB_COUNT) > MAX_JOB_CONTEXTS_COUNT) { |
| 365 | BG_MODERATE_JOB_COUNT = MAX_JOB_CONTEXTS_COUNT - FG_JOB_COUNT; |
| 366 | } |
| 367 | BG_LOW_JOB_COUNT = mParser.getInt(KEY_BG_LOW_JOB_COUNT, |
| 368 | DEFAULT_BG_LOW_JOB_COUNT); |
| 369 | if ((FG_JOB_COUNT+BG_LOW_JOB_COUNT) > MAX_JOB_CONTEXTS_COUNT) { |
| 370 | BG_LOW_JOB_COUNT = MAX_JOB_CONTEXTS_COUNT - FG_JOB_COUNT; |
| 371 | } |
| 372 | BG_CRITICAL_JOB_COUNT = mParser.getInt(KEY_BG_CRITICAL_JOB_COUNT, |
| 373 | DEFAULT_BG_CRITICAL_JOB_COUNT); |
| 374 | if ((FG_JOB_COUNT+BG_CRITICAL_JOB_COUNT) > MAX_JOB_CONTEXTS_COUNT) { |
| 375 | BG_CRITICAL_JOB_COUNT = MAX_JOB_CONTEXTS_COUNT - FG_JOB_COUNT; |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | void dump(PrintWriter pw) { |
| 381 | pw.println(" Settings:"); |
| 382 | |
| 383 | pw.print(" "); pw.print(KEY_MIN_IDLE_COUNT); pw.print("="); |
| 384 | pw.print(MIN_IDLE_COUNT); pw.println(); |
| 385 | |
| 386 | pw.print(" "); pw.print(KEY_MIN_CHARGING_COUNT); pw.print("="); |
| 387 | pw.print(MIN_CHARGING_COUNT); pw.println(); |
| 388 | |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 389 | pw.print(" "); pw.print(KEY_MIN_BATTERY_NOT_LOW_COUNT); pw.print("="); |
| 390 | pw.print(MIN_BATTERY_NOT_LOW_COUNT); pw.println(); |
| 391 | |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 392 | pw.print(" "); pw.print(KEY_MIN_STORAGE_NOT_LOW_COUNT); pw.print("="); |
| 393 | pw.print(MIN_STORAGE_NOT_LOW_COUNT); pw.println(); |
| 394 | |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 395 | pw.print(" "); pw.print(KEY_MIN_CONNECTIVITY_COUNT); pw.print("="); |
| 396 | pw.print(MIN_CONNECTIVITY_COUNT); pw.println(); |
| 397 | |
| 398 | pw.print(" "); pw.print(KEY_MIN_CONTENT_COUNT); pw.print("="); |
| 399 | pw.print(MIN_CONTENT_COUNT); pw.println(); |
| 400 | |
| 401 | pw.print(" "); pw.print(KEY_MIN_READY_JOBS_COUNT); pw.print("="); |
| 402 | pw.print(MIN_READY_JOBS_COUNT); pw.println(); |
| 403 | |
| 404 | pw.print(" "); pw.print(KEY_HEAVY_USE_FACTOR); pw.print("="); |
| 405 | pw.print(HEAVY_USE_FACTOR); pw.println(); |
| 406 | |
| 407 | pw.print(" "); pw.print(KEY_MODERATE_USE_FACTOR); pw.print("="); |
| 408 | pw.print(MODERATE_USE_FACTOR); pw.println(); |
| 409 | |
| 410 | pw.print(" "); pw.print(KEY_FG_JOB_COUNT); pw.print("="); |
| 411 | pw.print(FG_JOB_COUNT); pw.println(); |
| 412 | |
| 413 | pw.print(" "); pw.print(KEY_BG_NORMAL_JOB_COUNT); pw.print("="); |
| 414 | pw.print(BG_NORMAL_JOB_COUNT); pw.println(); |
| 415 | |
| 416 | pw.print(" "); pw.print(KEY_BG_MODERATE_JOB_COUNT); pw.print("="); |
| 417 | pw.print(BG_MODERATE_JOB_COUNT); pw.println(); |
| 418 | |
| 419 | pw.print(" "); pw.print(KEY_BG_LOW_JOB_COUNT); pw.print("="); |
| 420 | pw.print(BG_LOW_JOB_COUNT); pw.println(); |
| 421 | |
| 422 | pw.print(" "); pw.print(KEY_BG_CRITICAL_JOB_COUNT); pw.print("="); |
| 423 | pw.print(BG_CRITICAL_JOB_COUNT); pw.println(); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | final Constants mConstants; |
| 428 | |
| 429 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 430 | * Cleans up outstanding jobs when a package is removed. Even if it's being replaced later we |
| 431 | * still clean up. On reinstall the package will have a new uid. |
| 432 | */ |
| 433 | private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 434 | @Override |
| 435 | public void onReceive(Context context, Intent intent) { |
Christopher Tate | ee7805b | 2016-07-15 16:56:56 -0700 | [diff] [blame] | 436 | final String action = intent.getAction(); |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 437 | if (DEBUG) { |
Christopher Tate | ee7805b | 2016-07-15 16:56:56 -0700 | [diff] [blame] | 438 | Slog.d(TAG, "Receieved: " + action); |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 439 | } |
Christopher Tate | ee7805b | 2016-07-15 16:56:56 -0700 | [diff] [blame] | 440 | if (Intent.ACTION_PACKAGE_CHANGED.equals(action)) { |
Christopher Tate | b5c0788 | 2016-05-26 17:11:09 -0700 | [diff] [blame] | 441 | // Purge the app's jobs if the whole package was just disabled. When this is |
| 442 | // the case the component name will be a bare package name. |
| 443 | final String pkgName = getPackageName(intent); |
| 444 | final int pkgUid = intent.getIntExtra(Intent.EXTRA_UID, -1); |
| 445 | if (pkgName != null && pkgUid != -1) { |
| 446 | final String[] changedComponents = intent.getStringArrayExtra( |
| 447 | Intent.EXTRA_CHANGED_COMPONENT_NAME_LIST); |
| 448 | if (changedComponents != null) { |
| 449 | for (String component : changedComponents) { |
| 450 | if (component.equals(pkgName)) { |
| 451 | if (DEBUG) { |
| 452 | Slog.d(TAG, "Package state change: " + pkgName); |
| 453 | } |
| 454 | try { |
| 455 | final int userId = UserHandle.getUserId(pkgUid); |
| 456 | IPackageManager pm = AppGlobals.getPackageManager(); |
| 457 | final int state = pm.getApplicationEnabledSetting(pkgName, userId); |
| 458 | if (state == COMPONENT_ENABLED_STATE_DISABLED |
| 459 | || state == COMPONENT_ENABLED_STATE_DISABLED_USER) { |
| 460 | if (DEBUG) { |
| 461 | Slog.d(TAG, "Removing jobs for package " + pkgName |
| 462 | + " in user " + userId); |
| 463 | } |
Dianne Hackborn | e07641d | 2016-11-09 15:07:23 -0800 | [diff] [blame] | 464 | cancelJobsForUid(pkgUid); |
Christopher Tate | b5c0788 | 2016-05-26 17:11:09 -0700 | [diff] [blame] | 465 | } |
Christopher Tate | 652c5ad | 2016-10-05 14:45:46 -0700 | [diff] [blame] | 466 | } catch (RemoteException|IllegalArgumentException e) { |
| 467 | /* |
| 468 | * IllegalArgumentException means that the package doesn't exist. |
| 469 | * This arises when PACKAGE_CHANGED broadcast delivery has lagged |
| 470 | * behind outright uninstall, so by the time we try to act it's gone. |
| 471 | * We don't need to act on this PACKAGE_CHANGED when this happens; |
| 472 | * we'll get a PACKAGE_REMOVED later and clean up then. |
| 473 | * |
| 474 | * RemoteException can't actually happen; the package manager is |
| 475 | * running in this same process. |
| 476 | */ |
| 477 | } |
Christopher Tate | b5c0788 | 2016-05-26 17:11:09 -0700 | [diff] [blame] | 478 | break; |
| 479 | } |
| 480 | } |
| 481 | } |
| 482 | } else { |
| 483 | Slog.w(TAG, "PACKAGE_CHANGED for " + pkgName + " / uid " + pkgUid); |
| 484 | } |
Christopher Tate | ee7805b | 2016-07-15 16:56:56 -0700 | [diff] [blame] | 485 | } else if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) { |
Christopher Tate | aad67a3 | 2014-10-20 16:29:20 -0700 | [diff] [blame] | 486 | // If this is an outright uninstall rather than the first half of an |
| 487 | // app update sequence, cancel the jobs associated with the app. |
| 488 | if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
| 489 | int uidRemoved = intent.getIntExtra(Intent.EXTRA_UID, -1); |
| 490 | if (DEBUG) { |
| 491 | Slog.d(TAG, "Removing jobs for uid: " + uidRemoved); |
| 492 | } |
Dianne Hackborn | e07641d | 2016-11-09 15:07:23 -0800 | [diff] [blame] | 493 | cancelJobsForUid(uidRemoved); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 494 | } |
Christopher Tate | ee7805b | 2016-07-15 16:56:56 -0700 | [diff] [blame] | 495 | } else if (Intent.ACTION_USER_REMOVED.equals(action)) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 496 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0); |
| 497 | if (DEBUG) { |
| 498 | Slog.d(TAG, "Removing jobs for user: " + userId); |
| 499 | } |
| 500 | cancelJobsForUser(userId); |
Christopher Tate | ee7805b | 2016-07-15 16:56:56 -0700 | [diff] [blame] | 501 | } else if (Intent.ACTION_QUERY_PACKAGE_RESTART.equals(action)) { |
| 502 | // Has this package scheduled any jobs, such that we will take action |
| 503 | // if it were to be force-stopped? |
| 504 | final int pkgUid = intent.getIntExtra(Intent.EXTRA_UID, -1); |
| 505 | final String pkgName = intent.getData().getSchemeSpecificPart(); |
| 506 | if (pkgUid != -1) { |
| 507 | List<JobStatus> jobsForUid; |
| 508 | synchronized (mLock) { |
| 509 | jobsForUid = mJobs.getJobsByUid(pkgUid); |
| 510 | } |
| 511 | for (int i = jobsForUid.size() - 1; i >= 0; i--) { |
| 512 | if (jobsForUid.get(i).getSourcePackageName().equals(pkgName)) { |
| 513 | if (DEBUG) { |
| 514 | Slog.d(TAG, "Restart query: package " + pkgName + " at uid " |
| 515 | + pkgUid + " has jobs"); |
| 516 | } |
| 517 | setResultCode(Activity.RESULT_OK); |
| 518 | break; |
| 519 | } |
| 520 | } |
| 521 | } |
| 522 | } else if (Intent.ACTION_PACKAGE_RESTARTED.equals(action)) { |
| 523 | // possible force-stop |
| 524 | final int pkgUid = intent.getIntExtra(Intent.EXTRA_UID, -1); |
| 525 | final String pkgName = intent.getData().getSchemeSpecificPart(); |
| 526 | if (pkgUid != -1) { |
| 527 | if (DEBUG) { |
| 528 | Slog.d(TAG, "Removing jobs for pkg " + pkgName + " at uid " + pkgUid); |
| 529 | } |
| 530 | cancelJobsForPackageAndUid(pkgName, pkgUid); |
| 531 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | }; |
| 535 | |
Christopher Tate | b5c0788 | 2016-05-26 17:11:09 -0700 | [diff] [blame] | 536 | private String getPackageName(Intent intent) { |
| 537 | Uri uri = intent.getData(); |
| 538 | String pkg = uri != null ? uri.getSchemeSpecificPart() : null; |
| 539 | return pkg; |
| 540 | } |
| 541 | |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 542 | final private IUidObserver mUidObserver = new IUidObserver.Stub() { |
Sudheer Shanka | 8025580 | 2017-03-04 14:48:53 -0800 | [diff] [blame] | 543 | @Override public void onUidStateChanged(int uid, int procState, |
| 544 | long procStateSeq) throws RemoteException { |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 545 | updateUidState(uid, procState); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 546 | } |
| 547 | |
Dianne Hackborn | e07641d | 2016-11-09 15:07:23 -0800 | [diff] [blame] | 548 | @Override public void onUidGone(int uid, boolean disabled) throws RemoteException { |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 549 | updateUidState(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
Dianne Hackborn | e07641d | 2016-11-09 15:07:23 -0800 | [diff] [blame] | 550 | if (disabled) { |
| 551 | cancelJobsForUid(uid); |
| 552 | } |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | @Override public void onUidActive(int uid) throws RemoteException { |
| 556 | } |
| 557 | |
Dianne Hackborn | e07641d | 2016-11-09 15:07:23 -0800 | [diff] [blame] | 558 | @Override public void onUidIdle(int uid, boolean disabled) throws RemoteException { |
| 559 | if (disabled) { |
| 560 | cancelJobsForUid(uid); |
| 561 | } |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 562 | } |
| 563 | }; |
| 564 | |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 565 | public Object getLock() { |
| 566 | return mLock; |
| 567 | } |
| 568 | |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 569 | public JobStore getJobStore() { |
| 570 | return mJobs; |
| 571 | } |
| 572 | |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 573 | @Override |
| 574 | public void onStartUser(int userHandle) { |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 575 | mStartedUsers = ArrayUtils.appendInt(mStartedUsers, userHandle); |
| 576 | // Let's kick any outstanding jobs for this user. |
| 577 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
| 578 | } |
| 579 | |
| 580 | @Override |
| 581 | public void onUnlockUser(int userHandle) { |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 582 | // Let's kick any outstanding jobs for this user. |
| 583 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
| 584 | } |
| 585 | |
| 586 | @Override |
| 587 | public void onStopUser(int userHandle) { |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 588 | mStartedUsers = ArrayUtils.removeInt(mStartedUsers, userHandle); |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 589 | } |
| 590 | |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 591 | public int scheduleAsPackage(JobInfo job, JobWorkItem work, int uId, String packageName, |
| 592 | int userId, String tag) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 593 | try { |
Dianne Hackborn | c3af19a | 2017-01-20 17:00:44 -0800 | [diff] [blame] | 594 | if (ActivityManager.getService().isAppStartModeDisabled(uId, |
| 595 | job.getService().getPackageName())) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 596 | Slog.w(TAG, "Not scheduling job " + uId + ":" + job.toString() |
| 597 | + " -- package not allowed to start"); |
| 598 | return JobScheduler.RESULT_FAILURE; |
| 599 | } |
| 600 | } catch (RemoteException e) { |
| 601 | } |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 602 | synchronized (mLock) { |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 603 | final JobStatus toCancel = mJobs.getJobByUidAndJobId(uId, job.getId()); |
| 604 | |
| 605 | if (work != null && toCancel != null) { |
| 606 | // Fast path: we are adding work to an existing job, and the JobInfo is not |
| 607 | // changing. We can just directly enqueue this work in to the job. |
| 608 | if (toCancel.getJob().equals(job)) { |
Dianne Hackborn | 342e603 | 2017-04-13 18:04:31 -0700 | [diff] [blame] | 609 | toCancel.enqueueWorkLocked(ActivityManager.getService(), work); |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 610 | return JobScheduler.RESULT_SUCCESS; |
| 611 | } |
| 612 | } |
| 613 | |
| 614 | JobStatus jobStatus = JobStatus.createFromJobInfo(job, uId, packageName, userId, tag); |
| 615 | if (DEBUG) Slog.d(TAG, "SCHEDULE: " + jobStatus.toShortString()); |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 616 | // Jobs on behalf of others don't apply to the per-app job cap |
Christopher Tate | dabdf6f | 2016-02-24 12:30:22 -0800 | [diff] [blame] | 617 | if (ENFORCE_MAX_JOBS && packageName == null) { |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 618 | if (mJobs.countJobsForUid(uId) > MAX_JOBS_PER_APP) { |
| 619 | Slog.w(TAG, "Too many jobs for uid " + uId); |
| 620 | throw new IllegalStateException("Apps may not schedule more than " |
| 621 | + MAX_JOBS_PER_APP + " distinct jobs"); |
| 622 | } |
| 623 | } |
| 624 | |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 625 | // This may throw a SecurityException. |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 626 | jobStatus.prepareLocked(ActivityManager.getService()); |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 627 | |
Christopher Tate | b1c1f9a | 2016-03-17 13:29:25 -0700 | [diff] [blame] | 628 | if (toCancel != null) { |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame^] | 629 | cancelJobImplLocked(toCancel, jobStatus); |
Christopher Tate | b1c1f9a | 2016-03-17 13:29:25 -0700 | [diff] [blame] | 630 | } |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 631 | if (work != null) { |
| 632 | // If work has been supplied, enqueue it into the new job. |
Dianne Hackborn | 342e603 | 2017-04-13 18:04:31 -0700 | [diff] [blame] | 633 | jobStatus.enqueueWorkLocked(ActivityManager.getService(), work); |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 634 | } |
| 635 | startTrackingJobLocked(jobStatus, toCancel); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame^] | 636 | |
| 637 | // If the job is immediately ready to run, then we can just immediately |
| 638 | // put it in the pending list and try to schedule it. This is especially |
| 639 | // important for jobs with a 0 deadline constraint, since they will happen a fair |
| 640 | // amount, we want to handle them as quickly as possible, and semantically we want to |
| 641 | // make sure we have started holding the wake lock for the job before returning to |
| 642 | // the caller. |
| 643 | // If the job is not yet ready to run, there is nothing more to do -- we are |
| 644 | // now just waiting for one of its controllers to change state and schedule |
| 645 | // the job appropriately. |
| 646 | if (isReadyToBeExecutedLocked(jobStatus)) { |
| 647 | // This is a new job, we can just immediately put it on the pending |
| 648 | // list and try to run it. |
| 649 | mJobPackageTracker.notePending(jobStatus); |
| 650 | mPendingJobs.add(jobStatus); |
| 651 | maybeRunPendingJobsLocked(); |
| 652 | } |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 653 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 654 | return JobScheduler.RESULT_SUCCESS; |
| 655 | } |
| 656 | |
| 657 | public List<JobInfo> getPendingJobs(int uid) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 658 | synchronized (mLock) { |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 659 | List<JobStatus> jobs = mJobs.getJobsByUid(uid); |
| 660 | ArrayList<JobInfo> outList = new ArrayList<JobInfo>(jobs.size()); |
| 661 | for (int i = jobs.size() - 1; i >= 0; i--) { |
| 662 | JobStatus job = jobs.get(i); |
| 663 | outList.add(job.getJob()); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 664 | } |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 665 | return outList; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 666 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 667 | } |
| 668 | |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 669 | public JobInfo getPendingJob(int uid, int jobId) { |
| 670 | synchronized (mLock) { |
| 671 | List<JobStatus> jobs = mJobs.getJobsByUid(uid); |
| 672 | for (int i = jobs.size() - 1; i >= 0; i--) { |
| 673 | JobStatus job = jobs.get(i); |
| 674 | if (job.getJobId() == jobId) { |
| 675 | return job.getJob(); |
| 676 | } |
| 677 | } |
| 678 | return null; |
| 679 | } |
| 680 | } |
| 681 | |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 682 | void cancelJobsForUser(int userHandle) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 683 | synchronized (mLock) { |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame^] | 684 | final List<JobStatus> jobsForUser = mJobs.getJobsByUser(userHandle); |
| 685 | for (int i=0; i<jobsForUser.size(); i++) { |
| 686 | JobStatus toRemove = jobsForUser.get(i); |
| 687 | cancelJobImplLocked(toRemove, null); |
| 688 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | |
Christopher Tate | ee7805b | 2016-07-15 16:56:56 -0700 | [diff] [blame] | 692 | void cancelJobsForPackageAndUid(String pkgName, int uid) { |
Christopher Tate | ee7805b | 2016-07-15 16:56:56 -0700 | [diff] [blame] | 693 | synchronized (mLock) { |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame^] | 694 | final List<JobStatus> jobsForUid = mJobs.getJobsByUid(uid); |
| 695 | for (int i = jobsForUid.size() - 1; i >= 0; i--) { |
| 696 | final JobStatus job = jobsForUid.get(i); |
| 697 | if (job.getSourcePackageName().equals(pkgName)) { |
| 698 | cancelJobImplLocked(job, null); |
| 699 | } |
Christopher Tate | ee7805b | 2016-07-15 16:56:56 -0700 | [diff] [blame] | 700 | } |
| 701 | } |
| 702 | } |
| 703 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 704 | /** |
| 705 | * Entry point from client to cancel all jobs originating from their uid. |
| 706 | * This will remove the job from the master list, and cancel the job if it was staged for |
| 707 | * execution or being executed. |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 708 | * @param uid Uid to check against for removal of a job. |
Dianne Hackborn | e07641d | 2016-11-09 15:07:23 -0800 | [diff] [blame] | 709 | * |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 710 | */ |
Dianne Hackborn | e07641d | 2016-11-09 15:07:23 -0800 | [diff] [blame] | 711 | public void cancelJobsForUid(int uid) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 712 | synchronized (mLock) { |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame^] | 713 | final List<JobStatus> jobsForUid = mJobs.getJobsByUid(uid); |
| 714 | for (int i=0; i<jobsForUid.size(); i++) { |
| 715 | JobStatus toRemove = jobsForUid.get(i); |
| 716 | cancelJobImplLocked(toRemove, null); |
| 717 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 718 | } |
| 719 | } |
| 720 | |
| 721 | /** |
| 722 | * Entry point from client to cancel the job corresponding to the jobId provided. |
| 723 | * This will remove the job from the master list, and cancel the job if it was staged for |
| 724 | * execution or being executed. |
| 725 | * @param uid Uid of the calling client. |
| 726 | * @param jobId Id of the job, provided at schedule-time. |
| 727 | */ |
| 728 | public void cancelJob(int uid, int jobId) { |
| 729 | JobStatus toCancel; |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 730 | synchronized (mLock) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 731 | toCancel = mJobs.getJobByUidAndJobId(uid, jobId); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame^] | 732 | if (toCancel != null) { |
| 733 | cancelJobImplLocked(toCancel, null); |
| 734 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 735 | } |
| 736 | } |
| 737 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame^] | 738 | private void cancelJobImplLocked(JobStatus cancelled, JobStatus incomingJob) { |
| 739 | if (DEBUG) Slog.d(TAG, "CANCEL: " + cancelled.toShortString()); |
| 740 | cancelled.unprepareLocked(ActivityManager.getService()); |
| 741 | stopTrackingJobLocked(cancelled, incomingJob, true /* writeBack */); |
| 742 | // Remove from pending queue. |
| 743 | if (mPendingJobs.remove(cancelled)) { |
| 744 | mJobPackageTracker.noteNonpending(cancelled); |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 745 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame^] | 746 | // Cancel if running. |
| 747 | stopJobOnServiceContextLocked(cancelled, JobParameters.REASON_CANCELED); |
| 748 | reportActiveLocked(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 749 | } |
| 750 | |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 751 | void updateUidState(int uid, int procState) { |
| 752 | synchronized (mLock) { |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 753 | if (procState == ActivityManager.PROCESS_STATE_TOP) { |
| 754 | // Only use this if we are exactly the top app. All others can live |
| 755 | // with just the foreground priority. This means that persistent processes |
| 756 | // can never be the top app priority... that is fine. |
| 757 | mUidPriorityOverride.put(uid, JobInfo.PRIORITY_TOP_APP); |
| 758 | } else if (procState <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) { |
| 759 | mUidPriorityOverride.put(uid, JobInfo.PRIORITY_FOREGROUND_APP); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 760 | } else { |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 761 | mUidPriorityOverride.delete(uid); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 762 | } |
| 763 | } |
| 764 | } |
| 765 | |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 766 | @Override |
| 767 | public void onDeviceIdleStateChanged(boolean deviceIdle) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 768 | synchronized (mLock) { |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 769 | if (deviceIdle) { |
Jeff Sharkey | 34618b5 | 2016-06-01 15:51:19 -0600 | [diff] [blame] | 770 | // When becoming idle, make sure no jobs are actively running, |
| 771 | // except those using the idle exemption flag. |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 772 | for (int i=0; i<mActiveServices.size(); i++) { |
| 773 | JobServiceContext jsc = mActiveServices.get(i); |
| 774 | final JobStatus executing = jsc.getRunningJob(); |
Jeff Sharkey | 34618b5 | 2016-06-01 15:51:19 -0600 | [diff] [blame] | 775 | if (executing != null |
| 776 | && (executing.getFlags() & JobInfo.FLAG_WILL_BE_FOREGROUND) == 0) { |
Dianne Hackborn | 342e603 | 2017-04-13 18:04:31 -0700 | [diff] [blame] | 777 | jsc.cancelExecutingJobLocked(JobParameters.REASON_DEVICE_IDLE); |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 778 | } |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 779 | } |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 780 | } else { |
| 781 | // When coming out of idle, allow thing to start back up. |
| 782 | if (mReadyToRock) { |
| 783 | if (mLocalDeviceIdleController != null) { |
| 784 | if (!mReportedActive) { |
| 785 | mReportedActive = true; |
| 786 | mLocalDeviceIdleController.setJobsActive(true); |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 787 | } |
| 788 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame^] | 789 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 790 | } |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 795 | void reportActiveLocked() { |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 796 | // active is true if pending queue contains jobs OR some job is running. |
| 797 | boolean active = mPendingJobs.size() > 0; |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 798 | if (mPendingJobs.size() <= 0) { |
| 799 | for (int i=0; i<mActiveServices.size(); i++) { |
Dianne Hackborn | 7ab4025 | 2016-06-15 17:30:24 -0700 | [diff] [blame] | 800 | final JobServiceContext jsc = mActiveServices.get(i); |
| 801 | final JobStatus job = jsc.getRunningJob(); |
| 802 | if (job != null |
| 803 | && (job.getJob().getFlags() & JobInfo.FLAG_WILL_BE_FOREGROUND) == 0 |
| 804 | && !job.dozeWhitelisted) { |
| 805 | // We will report active if we have a job running and it is not an exception |
| 806 | // due to being in the foreground or whitelisted. |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 807 | active = true; |
| 808 | break; |
| 809 | } |
| 810 | } |
| 811 | } |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 812 | |
| 813 | if (mReportedActive != active) { |
| 814 | mReportedActive = active; |
| 815 | if (mLocalDeviceIdleController != null) { |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 816 | mLocalDeviceIdleController.setJobsActive(active); |
| 817 | } |
| 818 | } |
| 819 | } |
| 820 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 821 | /** |
| 822 | * Initializes the system service. |
| 823 | * <p> |
| 824 | * Subclasses must define a single argument constructor that accepts the context |
| 825 | * and passes it to super. |
| 826 | * </p> |
| 827 | * |
| 828 | * @param context The system server context. |
| 829 | */ |
| 830 | public JobSchedulerService(Context context) { |
| 831 | super(context); |
Dianne Hackborn | 970e3f4 | 2016-06-01 10:55:13 -0700 | [diff] [blame] | 832 | mHandler = new JobHandler(context.getMainLooper()); |
| 833 | mConstants = new Constants(mHandler); |
| 834 | mJobSchedulerStub = new JobSchedulerStub(); |
| 835 | mJobs = JobStore.initAndGet(this); |
| 836 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 837 | // Create the controllers. |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 838 | mControllers = new ArrayList<StateController>(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 839 | mControllers.add(ConnectivityController.get(this)); |
| 840 | mControllers.add(TimeController.get(this)); |
| 841 | mControllers.add(IdleController.get(this)); |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 842 | mBatteryController = BatteryController.get(this); |
| 843 | mControllers.add(mBatteryController); |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 844 | mStorageController = StorageController.get(this); |
| 845 | mControllers.add(mStorageController); |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 846 | mControllers.add(AppIdleController.get(this)); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 847 | mControllers.add(ContentObserverController.get(this)); |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 848 | mControllers.add(DeviceIdleJobsController.get(this)); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | @Override |
| 852 | public void onStart() { |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 853 | publishLocalService(JobSchedulerInternal.class, new LocalService()); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 854 | publishBinderService(Context.JOB_SCHEDULER_SERVICE, mJobSchedulerStub); |
| 855 | } |
| 856 | |
| 857 | @Override |
| 858 | public void onBootPhase(int phase) { |
| 859 | if (PHASE_SYSTEM_SERVICES_READY == phase) { |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 860 | mConstants.start(getContext().getContentResolver()); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 861 | // Register br for package removals and user removals. |
Christopher Tate | b5c0788 | 2016-05-26 17:11:09 -0700 | [diff] [blame] | 862 | final IntentFilter filter = new IntentFilter(); |
| 863 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 864 | filter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
Christopher Tate | ee7805b | 2016-07-15 16:56:56 -0700 | [diff] [blame] | 865 | filter.addAction(Intent.ACTION_PACKAGE_RESTARTED); |
| 866 | filter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 867 | filter.addDataScheme("package"); |
| 868 | getContext().registerReceiverAsUser( |
| 869 | mBroadcastReceiver, UserHandle.ALL, filter, null, null); |
| 870 | final IntentFilter userFilter = new IntentFilter(Intent.ACTION_USER_REMOVED); |
| 871 | getContext().registerReceiverAsUser( |
| 872 | mBroadcastReceiver, UserHandle.ALL, userFilter, null, null); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 873 | mPowerManager = (PowerManager)getContext().getSystemService(Context.POWER_SERVICE); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 874 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 875 | ActivityManager.getService().registerUidObserver(mUidObserver, |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 876 | ActivityManager.UID_OBSERVER_PROCSTATE | ActivityManager.UID_OBSERVER_GONE |
Dianne Hackborn | 5614bf5 | 2016-11-07 17:26:41 -0800 | [diff] [blame] | 877 | | ActivityManager.UID_OBSERVER_IDLE, ActivityManager.PROCESS_STATE_UNKNOWN, |
| 878 | null); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 879 | } catch (RemoteException e) { |
| 880 | // ignored; both services live in system_server |
| 881 | } |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 882 | } else if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 883 | synchronized (mLock) { |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 884 | // Let's go! |
| 885 | mReadyToRock = true; |
| 886 | mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService( |
| 887 | BatteryStats.SERVICE_NAME)); |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 888 | mLocalDeviceIdleController |
| 889 | = LocalServices.getService(DeviceIdleController.LocalService.class); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 890 | // Create the "runners". |
| 891 | for (int i = 0; i < MAX_JOB_CONTEXTS_COUNT; i++) { |
| 892 | mActiveServices.add( |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 893 | new JobServiceContext(this, mBatteryStats, mJobPackageTracker, |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 894 | getContext().getMainLooper())); |
| 895 | } |
| 896 | // Attach jobs to their controllers. |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 897 | mJobs.forEachJob(new JobStatusFunctor() { |
| 898 | @Override |
| 899 | public void process(JobStatus job) { |
| 900 | for (int controller = 0; controller < mControllers.size(); controller++) { |
| 901 | final StateController sc = mControllers.get(controller); |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 902 | sc.maybeStartTrackingJobLocked(job, null); |
| 903 | } |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 904 | } |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 905 | }); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 906 | // GO GO GO! |
| 907 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
| 908 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 909 | } |
| 910 | } |
| 911 | |
| 912 | /** |
| 913 | * Called when we have a job status object that we need to insert in our |
| 914 | * {@link com.android.server.job.JobStore}, and make sure all the relevant controllers know |
| 915 | * about. |
| 916 | */ |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 917 | private void startTrackingJobLocked(JobStatus jobStatus, JobStatus lastJob) { |
| 918 | if (!jobStatus.isPreparedLocked()) { |
| 919 | Slog.wtf(TAG, "Not yet prepared when started tracking: " + jobStatus); |
| 920 | } |
| 921 | final boolean update = mJobs.add(jobStatus); |
| 922 | if (mReadyToRock) { |
| 923 | for (int i = 0; i < mControllers.size(); i++) { |
| 924 | StateController controller = mControllers.get(i); |
| 925 | if (update) { |
| 926 | controller.maybeStopTrackingJobLocked(jobStatus, null, true); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 927 | } |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 928 | controller.maybeStartTrackingJobLocked(jobStatus, lastJob); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 929 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 930 | } |
| 931 | } |
| 932 | |
| 933 | /** |
| 934 | * Called when we want to remove a JobStatus object that we've finished executing. Returns the |
| 935 | * object removed. |
| 936 | */ |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 937 | private boolean stopTrackingJobLocked(JobStatus jobStatus, JobStatus incomingJob, |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 938 | boolean writeBack) { |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 939 | // Deal with any remaining work items in the old job. |
Dianne Hackborn | 342e603 | 2017-04-13 18:04:31 -0700 | [diff] [blame] | 940 | jobStatus.stopTrackingJobLocked(ActivityManager.getService(), incomingJob); |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 941 | |
| 942 | // Remove from store as well as controllers. |
| 943 | final boolean removed = mJobs.remove(jobStatus, writeBack); |
| 944 | if (removed && mReadyToRock) { |
| 945 | for (int i=0; i<mControllers.size(); i++) { |
| 946 | StateController controller = mControllers.get(i); |
| 947 | controller.maybeStopTrackingJobLocked(jobStatus, incomingJob, false); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 948 | } |
| 949 | } |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 950 | return removed; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 951 | } |
| 952 | |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 953 | private boolean stopJobOnServiceContextLocked(JobStatus job, int reason) { |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 954 | for (int i=0; i<mActiveServices.size(); i++) { |
| 955 | JobServiceContext jsc = mActiveServices.get(i); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 956 | final JobStatus executing = jsc.getRunningJob(); |
| 957 | if (executing != null && executing.matches(job.getUid(), job.getJobId())) { |
Dianne Hackborn | 342e603 | 2017-04-13 18:04:31 -0700 | [diff] [blame] | 958 | jsc.cancelExecutingJobLocked(reason); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 959 | return true; |
| 960 | } |
| 961 | } |
| 962 | return false; |
| 963 | } |
| 964 | |
| 965 | /** |
| 966 | * @param job JobStatus we are querying against. |
| 967 | * @return Whether or not the job represented by the status object is currently being run or |
| 968 | * is pending. |
| 969 | */ |
| 970 | private boolean isCurrentlyActiveLocked(JobStatus job) { |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 971 | for (int i=0; i<mActiveServices.size(); i++) { |
| 972 | JobServiceContext serviceContext = mActiveServices.get(i); |
Christopher Tate | eafb535 | 2016-10-04 16:34:48 -0700 | [diff] [blame] | 973 | // The 'unsafe' direct-internal-reference running-job inspector is okay to |
| 974 | // use here because we are already holding the necessary lock *and* we |
| 975 | // immediately discard the returned object reference, if any; we return |
| 976 | // only a boolean state indicator to the caller. |
| 977 | final JobStatus running = serviceContext.getRunningJobUnsafeLocked(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 978 | if (running != null && running.matches(job.getUid(), job.getJobId())) { |
| 979 | return true; |
| 980 | } |
| 981 | } |
| 982 | return false; |
| 983 | } |
| 984 | |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 985 | void noteJobsPending(List<JobStatus> jobs) { |
| 986 | for (int i = jobs.size() - 1; i >= 0; i--) { |
| 987 | JobStatus job = jobs.get(i); |
| 988 | mJobPackageTracker.notePending(job); |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | void noteJobsNonpending(List<JobStatus> jobs) { |
| 993 | for (int i = jobs.size() - 1; i >= 0; i--) { |
| 994 | JobStatus job = jobs.get(i); |
| 995 | mJobPackageTracker.noteNonpending(job); |
| 996 | } |
| 997 | } |
| 998 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 999 | /** |
Matthew Williams | 1bde39a | 2015-10-07 14:29:30 -0700 | [diff] [blame] | 1000 | * Reschedules the given job based on the job's backoff policy. It doesn't make sense to |
| 1001 | * specify an override deadline on a failed job (the failed job will run even though it's not |
| 1002 | * ready), so we reschedule it with {@link JobStatus#NO_LATEST_RUNTIME}, but specify that any |
| 1003 | * ready job with {@link JobStatus#numFailures} > 0 will be executed. |
| 1004 | * |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1005 | * @param failureToReschedule Provided job status that we will reschedule. |
| 1006 | * @return A newly instantiated JobStatus with the same constraints as the last job except |
| 1007 | * with adjusted timing constraints. |
Matthew Williams | 1bde39a | 2015-10-07 14:29:30 -0700 | [diff] [blame] | 1008 | * |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame^] | 1009 | * @see #maybeQueueReadyJobsForExecutionLocked |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1010 | */ |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1011 | private JobStatus getRescheduleJobForFailureLocked(JobStatus failureToReschedule) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1012 | final long elapsedNowMillis = SystemClock.elapsedRealtime(); |
| 1013 | final JobInfo job = failureToReschedule.getJob(); |
| 1014 | |
| 1015 | final long initialBackoffMillis = job.getInitialBackoffMillis(); |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 1016 | final int backoffAttempts = failureToReschedule.getNumFailures() + 1; |
| 1017 | long delayMillis; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1018 | |
| 1019 | switch (job.getBackoffPolicy()) { |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 1020 | case JobInfo.BACKOFF_POLICY_LINEAR: |
| 1021 | delayMillis = initialBackoffMillis * backoffAttempts; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1022 | break; |
| 1023 | default: |
| 1024 | if (DEBUG) { |
| 1025 | Slog.v(TAG, "Unrecognised back-off policy, defaulting to exponential."); |
| 1026 | } |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 1027 | case JobInfo.BACKOFF_POLICY_EXPONENTIAL: |
| 1028 | delayMillis = |
| 1029 | (long) Math.scalb(initialBackoffMillis, backoffAttempts - 1); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1030 | break; |
| 1031 | } |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 1032 | delayMillis = |
| 1033 | Math.min(delayMillis, JobInfo.MAX_BACKOFF_DELAY_MILLIS); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 1034 | JobStatus newJob = new JobStatus(failureToReschedule, elapsedNowMillis + delayMillis, |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 1035 | JobStatus.NO_LATEST_RUNTIME, backoffAttempts); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 1036 | for (int ic=0; ic<mControllers.size(); ic++) { |
| 1037 | StateController controller = mControllers.get(ic); |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1038 | controller.rescheduleForFailureLocked(newJob, failureToReschedule); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 1039 | } |
| 1040 | return newJob; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1041 | } |
| 1042 | |
| 1043 | /** |
Matthew Williams | 1bde39a | 2015-10-07 14:29:30 -0700 | [diff] [blame] | 1044 | * Called after a periodic has executed so we can reschedule it. We take the last execution |
| 1045 | * time of the job to be the time of completion (i.e. the time at which this function is |
| 1046 | * called). |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1047 | * This could be inaccurate b/c the job can run for as long as |
| 1048 | * {@link com.android.server.job.JobServiceContext#EXECUTING_TIMESLICE_MILLIS}, but will lead |
| 1049 | * to underscheduling at least, rather than if we had taken the last execution time to be the |
| 1050 | * start of the execution. |
| 1051 | * @return A new job representing the execution criteria for this instantiation of the |
| 1052 | * recurring job. |
| 1053 | */ |
| 1054 | private JobStatus getRescheduleJobForPeriodic(JobStatus periodicToReschedule) { |
| 1055 | final long elapsedNow = SystemClock.elapsedRealtime(); |
| 1056 | // Compute how much of the period is remaining. |
Matthew Williams | 1bde39a | 2015-10-07 14:29:30 -0700 | [diff] [blame] | 1057 | long runEarly = 0L; |
| 1058 | |
| 1059 | // If this periodic was rescheduled it won't have a deadline. |
| 1060 | if (periodicToReschedule.hasDeadlineConstraint()) { |
| 1061 | runEarly = Math.max(periodicToReschedule.getLatestRunTimeElapsed() - elapsedNow, 0L); |
| 1062 | } |
Shreyas Basarge | 89ee618 | 2015-12-17 15:16:36 +0000 | [diff] [blame] | 1063 | long flex = periodicToReschedule.getJob().getFlexMillis(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1064 | long period = periodicToReschedule.getJob().getIntervalMillis(); |
Shreyas Basarge | 89ee618 | 2015-12-17 15:16:36 +0000 | [diff] [blame] | 1065 | long newLatestRuntimeElapsed = elapsedNow + runEarly + period; |
| 1066 | long newEarliestRunTimeElapsed = newLatestRuntimeElapsed - flex; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1067 | |
| 1068 | if (DEBUG) { |
| 1069 | Slog.v(TAG, "Rescheduling executed periodic. New execution window [" + |
| 1070 | newEarliestRunTimeElapsed/1000 + ", " + newLatestRuntimeElapsed/1000 + "]s"); |
| 1071 | } |
| 1072 | return new JobStatus(periodicToReschedule, newEarliestRunTimeElapsed, |
| 1073 | newLatestRuntimeElapsed, 0 /* backoffAttempt */); |
| 1074 | } |
| 1075 | |
| 1076 | // JobCompletedListener implementations. |
| 1077 | |
| 1078 | /** |
| 1079 | * A job just finished executing. We fetch the |
| 1080 | * {@link com.android.server.job.controllers.JobStatus} from the store and depending on |
| 1081 | * whether we want to reschedule we readd it to the controllers. |
| 1082 | * @param jobStatus Completed job. |
| 1083 | * @param needsReschedule Whether the implementing class should reschedule this job. |
| 1084 | */ |
| 1085 | @Override |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1086 | public void onJobCompletedLocked(JobStatus jobStatus, boolean needsReschedule) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1087 | if (DEBUG) { |
| 1088 | Slog.d(TAG, "Completed " + jobStatus + ", reschedule=" + needsReschedule); |
| 1089 | } |
Dianne Hackborn | fd8807a | 2017-04-17 13:34:51 -0700 | [diff] [blame] | 1090 | |
| 1091 | // If the job wants to be rescheduled, we first need to make the next upcoming |
| 1092 | // job so we can transfer any appropriate state over from the previous job when |
| 1093 | // we stop it. |
| 1094 | final JobStatus rescheduledJob = needsReschedule |
| 1095 | ? getRescheduleJobForFailureLocked(jobStatus) : null; |
| 1096 | |
Shreyas Basarge | 73f1025 | 2016-02-11 17:06:13 +0000 | [diff] [blame] | 1097 | // Do not write back immediately if this is a periodic job. The job may get lost if system |
| 1098 | // shuts down before it is added back. |
Dianne Hackborn | fd8807a | 2017-04-17 13:34:51 -0700 | [diff] [blame] | 1099 | if (!stopTrackingJobLocked(jobStatus, rescheduledJob, !jobStatus.getJob().isPeriodic())) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1100 | if (DEBUG) { |
Matthew Williams | ee410da | 2014-07-25 11:30:40 -0700 | [diff] [blame] | 1101 | Slog.d(TAG, "Could not find job to remove. Was job removed while executing?"); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1102 | } |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 1103 | // We still want to check for jobs to execute, because this job may have |
| 1104 | // scheduled a new job under the same job id, and now we can run it. |
| 1105 | mHandler.obtainMessage(MSG_CHECK_JOB_GREEDY).sendToTarget(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1106 | return; |
| 1107 | } |
Dianne Hackborn | fd8807a | 2017-04-17 13:34:51 -0700 | [diff] [blame] | 1108 | |
| 1109 | if (rescheduledJob != null) { |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 1110 | try { |
Dianne Hackborn | fd8807a | 2017-04-17 13:34:51 -0700 | [diff] [blame] | 1111 | rescheduledJob.prepareLocked(ActivityManager.getService()); |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 1112 | } catch (SecurityException e) { |
Dianne Hackborn | fd8807a | 2017-04-17 13:34:51 -0700 | [diff] [blame] | 1113 | Slog.w(TAG, "Unable to regrant job permissions for " + rescheduledJob); |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 1114 | } |
Dianne Hackborn | fd8807a | 2017-04-17 13:34:51 -0700 | [diff] [blame] | 1115 | startTrackingJobLocked(rescheduledJob, jobStatus); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1116 | } else if (jobStatus.getJob().isPeriodic()) { |
| 1117 | JobStatus rescheduledPeriodic = getRescheduleJobForPeriodic(jobStatus); |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 1118 | try { |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1119 | rescheduledPeriodic.prepareLocked(ActivityManager.getService()); |
Dianne Hackborn | a47223f | 2017-03-30 13:49:13 -0700 | [diff] [blame] | 1120 | } catch (SecurityException e) { |
| 1121 | Slog.w(TAG, "Unable to regrant job permissions for " + rescheduledPeriodic); |
| 1122 | } |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1123 | startTrackingJobLocked(rescheduledPeriodic, jobStatus); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1124 | } |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1125 | jobStatus.unprepareLocked(ActivityManager.getService()); |
| 1126 | reportActiveLocked(); |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 1127 | mHandler.obtainMessage(MSG_CHECK_JOB_GREEDY).sendToTarget(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | // StateChangedListener implementations. |
| 1131 | |
| 1132 | /** |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 1133 | * Posts a message to the {@link com.android.server.job.JobSchedulerService.JobHandler} that |
| 1134 | * some controller's state has changed, so as to run through the list of jobs and start/stop |
| 1135 | * any that are eligible. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1136 | */ |
| 1137 | @Override |
| 1138 | public void onControllerStateChanged() { |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 1139 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1140 | } |
| 1141 | |
| 1142 | @Override |
| 1143 | public void onRunJobNow(JobStatus jobStatus) { |
| 1144 | mHandler.obtainMessage(MSG_JOB_EXPIRED, jobStatus).sendToTarget(); |
| 1145 | } |
| 1146 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame^] | 1147 | final private class JobHandler extends Handler { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1148 | |
| 1149 | public JobHandler(Looper looper) { |
| 1150 | super(looper); |
| 1151 | } |
| 1152 | |
| 1153 | @Override |
| 1154 | public void handleMessage(Message message) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 1155 | synchronized (mLock) { |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 1156 | if (!mReadyToRock) { |
| 1157 | return; |
| 1158 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame^] | 1159 | switch (message.what) { |
| 1160 | case MSG_JOB_EXPIRED: { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1161 | JobStatus runNow = (JobStatus) message.obj; |
Matthew Williams | bafeeb9 | 2014-08-08 11:51:06 -0700 | [diff] [blame] | 1162 | // runNow can be null, which is a controller's way of indicating that its |
| 1163 | // state is such that all ready jobs should be run immediately. |
Christopher Tate | b1d6448 | 2017-03-15 12:01:22 -0700 | [diff] [blame] | 1164 | if (runNow != null && isReadyToBeExecutedLocked(runNow)) { |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1165 | mJobPackageTracker.notePending(runNow); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1166 | mPendingJobs.add(runNow); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame^] | 1167 | } else { |
| 1168 | queueReadyJobsForExecutionLocked(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1169 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame^] | 1170 | } break; |
| 1171 | case MSG_CHECK_JOB: |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 1172 | if (mReportedActive) { |
| 1173 | // if jobs are currently being run, queue all ready jobs for execution. |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame^] | 1174 | queueReadyJobsForExecutionLocked(); |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 1175 | } else { |
| 1176 | // Check the list of jobs and run some of them if we feel inclined. |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame^] | 1177 | maybeQueueReadyJobsForExecutionLocked(); |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 1178 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame^] | 1179 | break; |
| 1180 | case MSG_CHECK_JOB_GREEDY: |
| 1181 | queueReadyJobsForExecutionLocked(); |
| 1182 | break; |
| 1183 | case MSG_STOP_JOB: |
| 1184 | cancelJobImplLocked((JobStatus) message.obj, null); |
| 1185 | break; |
Matthew Williams | 75fc525 | 2014-09-02 16:17:53 -0700 | [diff] [blame] | 1186 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame^] | 1187 | maybeRunPendingJobsLocked(); |
| 1188 | // Don't remove JOB_EXPIRED in case one came along while processing the queue. |
| 1189 | removeMessages(MSG_CHECK_JOB); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1190 | } |
| 1191 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame^] | 1192 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1193 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame^] | 1194 | /** |
| 1195 | * Run through list of jobs and execute all possible - at least one is expired so we do |
| 1196 | * as many as we can. |
| 1197 | */ |
| 1198 | private void queueReadyJobsForExecutionLocked() { |
| 1199 | if (DEBUG) { |
| 1200 | Slog.d(TAG, "queuing all ready jobs for execution:"); |
| 1201 | } |
| 1202 | noteJobsNonpending(mPendingJobs); |
| 1203 | mPendingJobs.clear(); |
| 1204 | mJobs.forEachJob(mReadyQueueFunctor); |
| 1205 | mReadyQueueFunctor.postProcess(); |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1206 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame^] | 1207 | if (DEBUG) { |
| 1208 | final int queuedJobs = mPendingJobs.size(); |
| 1209 | if (queuedJobs == 0) { |
| 1210 | Slog.d(TAG, "No jobs pending."); |
| 1211 | } else { |
| 1212 | Slog.d(TAG, queuedJobs + " jobs queued."); |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1213 | } |
| 1214 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame^] | 1215 | } |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1216 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame^] | 1217 | final class ReadyJobQueueFunctor implements JobStatusFunctor { |
| 1218 | ArrayList<JobStatus> newReadyJobs; |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1219 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame^] | 1220 | @Override |
| 1221 | public void process(JobStatus job) { |
| 1222 | if (isReadyToBeExecutedLocked(job)) { |
Matthew Williams | 75fc525 | 2014-09-02 16:17:53 -0700 | [diff] [blame] | 1223 | if (DEBUG) { |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame^] | 1224 | Slog.d(TAG, " queued " + job.toShortString()); |
Matthew Williams | 75fc525 | 2014-09-02 16:17:53 -0700 | [diff] [blame] | 1225 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame^] | 1226 | if (newReadyJobs == null) { |
| 1227 | newReadyJobs = new ArrayList<JobStatus>(); |
| 1228 | } |
| 1229 | newReadyJobs.add(job); |
| 1230 | } else if (areJobConstraintsNotSatisfiedLocked(job)) { |
| 1231 | stopJobOnServiceContextLocked(job, |
| 1232 | JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1233 | } |
| 1234 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame^] | 1235 | |
| 1236 | public void postProcess() { |
| 1237 | if (newReadyJobs != null) { |
| 1238 | noteJobsPending(newReadyJobs); |
| 1239 | mPendingJobs.addAll(newReadyJobs); |
| 1240 | } |
| 1241 | newReadyJobs = null; |
| 1242 | } |
| 1243 | } |
| 1244 | private final ReadyJobQueueFunctor mReadyQueueFunctor = new ReadyJobQueueFunctor(); |
| 1245 | |
| 1246 | /** |
| 1247 | * The state of at least one job has changed. Here is where we could enforce various |
| 1248 | * policies on when we want to execute jobs. |
| 1249 | * Right now the policy is such: |
| 1250 | * If >1 of the ready jobs is idle mode we send all of them off |
| 1251 | * if more than 2 network connectivity jobs are ready we send them all off. |
| 1252 | * If more than 4 jobs total are ready we send them all off. |
| 1253 | * TODO: It would be nice to consolidate these sort of high-level policies somewhere. |
| 1254 | */ |
| 1255 | final class MaybeReadyJobQueueFunctor implements JobStatusFunctor { |
| 1256 | int chargingCount; |
| 1257 | int batteryNotLowCount; |
| 1258 | int storageNotLowCount; |
| 1259 | int idleCount; |
| 1260 | int backoffCount; |
| 1261 | int connectivityCount; |
| 1262 | int contentCount; |
| 1263 | List<JobStatus> runnableJobs; |
| 1264 | |
| 1265 | public MaybeReadyJobQueueFunctor() { |
| 1266 | reset(); |
| 1267 | } |
| 1268 | |
| 1269 | // Functor method invoked for each job via JobStore.forEachJob() |
| 1270 | @Override |
| 1271 | public void process(JobStatus job) { |
| 1272 | if (isReadyToBeExecutedLocked(job)) { |
| 1273 | try { |
| 1274 | if (ActivityManager.getService().isAppStartModeDisabled(job.getUid(), |
| 1275 | job.getJob().getService().getPackageName())) { |
| 1276 | Slog.w(TAG, "Aborting job " + job.getUid() + ":" |
| 1277 | + job.getJob().toString() + " -- package not allowed to start"); |
| 1278 | mHandler.obtainMessage(MSG_STOP_JOB, job).sendToTarget(); |
| 1279 | return; |
| 1280 | } |
| 1281 | } catch (RemoteException e) { |
| 1282 | } |
| 1283 | if (job.getNumFailures() > 0) { |
| 1284 | backoffCount++; |
| 1285 | } |
| 1286 | if (job.hasIdleConstraint()) { |
| 1287 | idleCount++; |
| 1288 | } |
| 1289 | if (job.hasConnectivityConstraint()) { |
| 1290 | connectivityCount++; |
| 1291 | } |
| 1292 | if (job.hasChargingConstraint()) { |
| 1293 | chargingCount++; |
| 1294 | } |
| 1295 | if (job.hasBatteryNotLowConstraint()) { |
| 1296 | batteryNotLowCount++; |
| 1297 | } |
| 1298 | if (job.hasStorageNotLowConstraint()) { |
| 1299 | storageNotLowCount++; |
| 1300 | } |
| 1301 | if (job.hasContentTriggerConstraint()) { |
| 1302 | contentCount++; |
| 1303 | } |
| 1304 | if (runnableJobs == null) { |
| 1305 | runnableJobs = new ArrayList<>(); |
| 1306 | } |
| 1307 | runnableJobs.add(job); |
| 1308 | } else if (areJobConstraintsNotSatisfiedLocked(job)) { |
| 1309 | stopJobOnServiceContextLocked(job, |
| 1310 | JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED); |
| 1311 | } |
| 1312 | } |
| 1313 | |
| 1314 | public void postProcess() { |
| 1315 | if (backoffCount > 0 || |
| 1316 | idleCount >= mConstants.MIN_IDLE_COUNT || |
| 1317 | connectivityCount >= mConstants.MIN_CONNECTIVITY_COUNT || |
| 1318 | chargingCount >= mConstants.MIN_CHARGING_COUNT || |
| 1319 | batteryNotLowCount >= mConstants.MIN_BATTERY_NOT_LOW_COUNT || |
| 1320 | storageNotLowCount >= mConstants.MIN_STORAGE_NOT_LOW_COUNT || |
| 1321 | contentCount >= mConstants.MIN_CONTENT_COUNT || |
| 1322 | (runnableJobs != null |
| 1323 | && runnableJobs.size() >= mConstants.MIN_READY_JOBS_COUNT)) { |
| 1324 | if (DEBUG) { |
| 1325 | Slog.d(TAG, "maybeQueueReadyJobsForExecutionLocked: Running jobs."); |
| 1326 | } |
| 1327 | noteJobsPending(runnableJobs); |
| 1328 | mPendingJobs.addAll(runnableJobs); |
| 1329 | } else { |
| 1330 | if (DEBUG) { |
| 1331 | Slog.d(TAG, "maybeQueueReadyJobsForExecutionLocked: Not running anything."); |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | // Be ready for next time |
| 1336 | reset(); |
| 1337 | } |
| 1338 | |
| 1339 | private void reset() { |
| 1340 | chargingCount = 0; |
| 1341 | idleCount = 0; |
| 1342 | backoffCount = 0; |
| 1343 | connectivityCount = 0; |
| 1344 | batteryNotLowCount = 0; |
| 1345 | storageNotLowCount = 0; |
| 1346 | contentCount = 0; |
| 1347 | runnableJobs = null; |
| 1348 | } |
| 1349 | } |
| 1350 | private final MaybeReadyJobQueueFunctor mMaybeQueueFunctor = new MaybeReadyJobQueueFunctor(); |
| 1351 | |
| 1352 | private void maybeQueueReadyJobsForExecutionLocked() { |
| 1353 | if (DEBUG) Slog.d(TAG, "Maybe queuing ready jobs..."); |
| 1354 | |
| 1355 | noteJobsNonpending(mPendingJobs); |
| 1356 | mPendingJobs.clear(); |
| 1357 | mJobs.forEachJob(mMaybeQueueFunctor); |
| 1358 | mMaybeQueueFunctor.postProcess(); |
| 1359 | } |
| 1360 | |
| 1361 | /** |
| 1362 | * Criteria for moving a job into the pending queue: |
| 1363 | * - It's ready. |
| 1364 | * - It's not pending. |
| 1365 | * - It's not already running on a JSC. |
| 1366 | * - The user that requested the job is running. |
| 1367 | * - The component is enabled and runnable. |
| 1368 | */ |
| 1369 | private boolean isReadyToBeExecutedLocked(JobStatus job) { |
| 1370 | final boolean jobExists = mJobs.containsJob(job); |
| 1371 | final boolean jobReady = job.isReady(); |
| 1372 | final boolean jobPending = mPendingJobs.contains(job); |
| 1373 | final boolean jobActive = isCurrentlyActiveLocked(job); |
| 1374 | final boolean jobBackingUp = mBackingUpUids.indexOfKey(job.getSourceUid()) >= 0; |
| 1375 | |
| 1376 | final int userId = job.getUserId(); |
| 1377 | final boolean userStarted = ArrayUtils.contains(mStartedUsers, userId); |
| 1378 | |
| 1379 | if (DEBUG) { |
| 1380 | Slog.v(TAG, "isReadyToBeExecutedLocked: " + job.toShortString() |
| 1381 | + " exists=" + jobExists |
| 1382 | + " ready=" + jobReady + " pending=" + jobPending |
| 1383 | + " active=" + jobActive + " backingup=" + jobBackingUp |
| 1384 | + " userStarted=" + userStarted); |
| 1385 | } |
| 1386 | |
| 1387 | // Short circuit: don't do the expensive PM check unless we really think |
| 1388 | // we might need to run this job now. |
| 1389 | if (!jobExists || !userStarted || !jobReady || jobPending || jobActive || jobBackingUp) { |
| 1390 | return false; |
| 1391 | } |
| 1392 | |
| 1393 | final boolean componentPresent; |
| 1394 | try { |
| 1395 | componentPresent = (AppGlobals.getPackageManager().getServiceInfo( |
| 1396 | job.getServiceComponent(), PackageManager.MATCH_DEBUG_TRIAGED_MISSING, |
| 1397 | userId) != null); |
| 1398 | } catch (RemoteException e) { |
| 1399 | throw e.rethrowAsRuntimeException(); |
| 1400 | } |
| 1401 | |
| 1402 | if (DEBUG) { |
| 1403 | Slog.v(TAG, "isReadyToBeExecutedLocked: " + job.toShortString() |
| 1404 | + " componentPresent=" + componentPresent); |
| 1405 | } |
| 1406 | |
| 1407 | // Everything else checked out so far, so this is the final yes/no check |
| 1408 | return componentPresent; |
| 1409 | } |
| 1410 | |
| 1411 | /** |
| 1412 | * Criteria for cancelling an active job: |
| 1413 | * - It's not ready |
| 1414 | * - It's running on a JSC. |
| 1415 | */ |
| 1416 | private boolean areJobConstraintsNotSatisfiedLocked(JobStatus job) { |
| 1417 | return !job.isReady() && isCurrentlyActiveLocked(job); |
| 1418 | } |
| 1419 | |
| 1420 | /** |
| 1421 | * Reconcile jobs in the pending queue against available execution contexts. |
| 1422 | * A controller can force a job into the pending queue even if it's already running, but |
| 1423 | * here is where we decide whether to actually execute it. |
| 1424 | */ |
| 1425 | private void maybeRunPendingJobsLocked() { |
| 1426 | if (DEBUG) { |
| 1427 | Slog.d(TAG, "pending queue: " + mPendingJobs.size() + " jobs."); |
| 1428 | } |
| 1429 | assignJobsToContextsLocked(); |
| 1430 | reportActiveLocked(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1431 | } |
| 1432 | |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1433 | private int adjustJobPriority(int curPriority, JobStatus job) { |
| 1434 | if (curPriority < JobInfo.PRIORITY_TOP_APP) { |
| 1435 | float factor = mJobPackageTracker.getLoadFactor(job); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1436 | if (factor >= mConstants.HEAVY_USE_FACTOR) { |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1437 | curPriority += JobInfo.PRIORITY_ADJ_ALWAYS_RUNNING; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1438 | } else if (factor >= mConstants.MODERATE_USE_FACTOR) { |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1439 | curPriority += JobInfo.PRIORITY_ADJ_OFTEN_RUNNING; |
| 1440 | } |
| 1441 | } |
| 1442 | return curPriority; |
| 1443 | } |
| 1444 | |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1445 | private int evaluateJobPriorityLocked(JobStatus job) { |
| 1446 | int priority = job.getPriority(); |
| 1447 | if (priority >= JobInfo.PRIORITY_FOREGROUND_APP) { |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1448 | return adjustJobPriority(priority, job); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1449 | } |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1450 | int override = mUidPriorityOverride.get(job.getSourceUid(), 0); |
| 1451 | if (override != 0) { |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1452 | return adjustJobPriority(override, job); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1453 | } |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1454 | return adjustJobPriority(priority, job); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1455 | } |
| 1456 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1457 | /** |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1458 | * Takes jobs from pending queue and runs them on available contexts. |
| 1459 | * If no contexts are available, preempts lower priority jobs to |
| 1460 | * run higher priority ones. |
| 1461 | * Lock on mJobs before calling this function. |
| 1462 | */ |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 1463 | private void assignJobsToContextsLocked() { |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1464 | if (DEBUG) { |
| 1465 | Slog.d(TAG, printPendingQueue()); |
| 1466 | } |
| 1467 | |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1468 | int memLevel; |
| 1469 | try { |
Sudheer Shanka | dc589ac | 2016-11-10 15:30:17 -0800 | [diff] [blame] | 1470 | memLevel = ActivityManager.getService().getMemoryTrimLevel(); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1471 | } catch (RemoteException e) { |
| 1472 | memLevel = ProcessStats.ADJ_MEM_FACTOR_NORMAL; |
| 1473 | } |
| 1474 | switch (memLevel) { |
| 1475 | case ProcessStats.ADJ_MEM_FACTOR_MODERATE: |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1476 | mMaxActiveJobs = mConstants.BG_MODERATE_JOB_COUNT; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1477 | break; |
| 1478 | case ProcessStats.ADJ_MEM_FACTOR_LOW: |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1479 | mMaxActiveJobs = mConstants.BG_LOW_JOB_COUNT; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1480 | break; |
| 1481 | case ProcessStats.ADJ_MEM_FACTOR_CRITICAL: |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1482 | mMaxActiveJobs = mConstants.BG_CRITICAL_JOB_COUNT; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1483 | break; |
| 1484 | default: |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1485 | mMaxActiveJobs = mConstants.BG_NORMAL_JOB_COUNT; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1486 | break; |
| 1487 | } |
| 1488 | |
| 1489 | JobStatus[] contextIdToJobMap = mTmpAssignContextIdToJobMap; |
| 1490 | boolean[] act = mTmpAssignAct; |
| 1491 | int[] preferredUidForContext = mTmpAssignPreferredUidForContext; |
| 1492 | int numActive = 0; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1493 | int numForeground = 0; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1494 | for (int i=0; i<MAX_JOB_CONTEXTS_COUNT; i++) { |
| 1495 | final JobServiceContext js = mActiveServices.get(i); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1496 | final JobStatus status = js.getRunningJob(); |
| 1497 | if ((contextIdToJobMap[i] = status) != null) { |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1498 | numActive++; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1499 | if (status.lastEvaluatedPriority >= JobInfo.PRIORITY_TOP_APP) { |
| 1500 | numForeground++; |
| 1501 | } |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1502 | } |
| 1503 | act[i] = false; |
| 1504 | preferredUidForContext[i] = js.getPreferredUid(); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1505 | } |
| 1506 | if (DEBUG) { |
| 1507 | Slog.d(TAG, printContextIdToJobMap(contextIdToJobMap, "running jobs initial")); |
| 1508 | } |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1509 | for (int i=0; i<mPendingJobs.size(); i++) { |
| 1510 | JobStatus nextPending = mPendingJobs.get(i); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1511 | |
| 1512 | // If job is already running, go to next job. |
| 1513 | int jobRunningContext = findJobContextIdFromMap(nextPending, contextIdToJobMap); |
| 1514 | if (jobRunningContext != -1) { |
| 1515 | continue; |
| 1516 | } |
| 1517 | |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1518 | final int priority = evaluateJobPriorityLocked(nextPending); |
| 1519 | nextPending.lastEvaluatedPriority = priority; |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1520 | |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1521 | // Find a context for nextPending. The context should be available OR |
| 1522 | // it should have lowest priority among all running jobs |
| 1523 | // (sharing the same Uid as nextPending) |
| 1524 | int minPriority = Integer.MAX_VALUE; |
| 1525 | int minPriorityContextId = -1; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1526 | for (int j=0; j<MAX_JOB_CONTEXTS_COUNT; j++) { |
| 1527 | JobStatus job = contextIdToJobMap[j]; |
| 1528 | int preferredUid = preferredUidForContext[j]; |
Shreyas Basarge | 347c278 | 2016-01-15 18:24:36 +0000 | [diff] [blame] | 1529 | if (job == null) { |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1530 | if ((numActive < mMaxActiveJobs || |
| 1531 | (priority >= JobInfo.PRIORITY_TOP_APP && |
| 1532 | numForeground < mConstants.FG_JOB_COUNT)) && |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1533 | (preferredUid == nextPending.getUid() || |
| 1534 | preferredUid == JobServiceContext.NO_PREFERRED_UID)) { |
| 1535 | // This slot is free, and we haven't yet hit the limit on |
| 1536 | // concurrent jobs... we can just throw the job in to here. |
| 1537 | minPriorityContextId = j; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1538 | break; |
| 1539 | } |
Shreyas Basarge | 347c278 | 2016-01-15 18:24:36 +0000 | [diff] [blame] | 1540 | // No job on this context, but nextPending can't run here because |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1541 | // the context has a preferred Uid or we have reached the limit on |
| 1542 | // concurrent jobs. |
Shreyas Basarge | 347c278 | 2016-01-15 18:24:36 +0000 | [diff] [blame] | 1543 | continue; |
| 1544 | } |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1545 | if (job.getUid() != nextPending.getUid()) { |
| 1546 | continue; |
| 1547 | } |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1548 | if (evaluateJobPriorityLocked(job) >= nextPending.lastEvaluatedPriority) { |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1549 | continue; |
| 1550 | } |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1551 | if (minPriority > nextPending.lastEvaluatedPriority) { |
| 1552 | minPriority = nextPending.lastEvaluatedPriority; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1553 | minPriorityContextId = j; |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1554 | } |
| 1555 | } |
| 1556 | if (minPriorityContextId != -1) { |
| 1557 | contextIdToJobMap[minPriorityContextId] = nextPending; |
| 1558 | act[minPriorityContextId] = true; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1559 | numActive++; |
| 1560 | if (priority >= JobInfo.PRIORITY_TOP_APP) { |
| 1561 | numForeground++; |
| 1562 | } |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1563 | } |
| 1564 | } |
| 1565 | if (DEBUG) { |
| 1566 | Slog.d(TAG, printContextIdToJobMap(contextIdToJobMap, "running jobs final")); |
| 1567 | } |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 1568 | mJobPackageTracker.noteConcurrency(numActive, numForeground); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1569 | for (int i=0; i<MAX_JOB_CONTEXTS_COUNT; i++) { |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1570 | boolean preservePreferredUid = false; |
| 1571 | if (act[i]) { |
| 1572 | JobStatus js = mActiveServices.get(i).getRunningJob(); |
| 1573 | if (js != null) { |
| 1574 | if (DEBUG) { |
| 1575 | Slog.d(TAG, "preempting job: " + mActiveServices.get(i).getRunningJob()); |
| 1576 | } |
| 1577 | // preferredUid will be set to uid of currently running job. |
Dianne Hackborn | 342e603 | 2017-04-13 18:04:31 -0700 | [diff] [blame] | 1578 | mActiveServices.get(i).preemptExecutingJobLocked(); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1579 | preservePreferredUid = true; |
| 1580 | } else { |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1581 | final JobStatus pendingJob = contextIdToJobMap[i]; |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1582 | if (DEBUG) { |
| 1583 | Slog.d(TAG, "About to run job on context " |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1584 | + String.valueOf(i) + ", job: " + pendingJob); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1585 | } |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 1586 | for (int ic=0; ic<mControllers.size(); ic++) { |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1587 | mControllers.get(ic).prepareForExecutionLocked(pendingJob); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 1588 | } |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1589 | if (!mActiveServices.get(i).executeRunnableJob(pendingJob)) { |
| 1590 | Slog.d(TAG, "Error executing " + pendingJob); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1591 | } |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 1592 | if (mPendingJobs.remove(pendingJob)) { |
| 1593 | mJobPackageTracker.noteNonpending(pendingJob); |
| 1594 | } |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1595 | } |
| 1596 | } |
| 1597 | if (!preservePreferredUid) { |
| 1598 | mActiveServices.get(i).clearPreferredUid(); |
| 1599 | } |
| 1600 | } |
| 1601 | } |
| 1602 | |
| 1603 | int findJobContextIdFromMap(JobStatus jobStatus, JobStatus[] map) { |
| 1604 | for (int i=0; i<map.length; i++) { |
| 1605 | if (map[i] != null && map[i].matches(jobStatus.getUid(), jobStatus.getJobId())) { |
| 1606 | return i; |
| 1607 | } |
| 1608 | } |
| 1609 | return -1; |
| 1610 | } |
| 1611 | |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 1612 | final class LocalService implements JobSchedulerInternal { |
| 1613 | |
| 1614 | /** |
| 1615 | * Returns a list of all pending jobs. A running job is not considered pending. Periodic |
| 1616 | * jobs are always considered pending. |
| 1617 | */ |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 1618 | @Override |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 1619 | public List<JobInfo> getSystemScheduledPendingJobs() { |
| 1620 | synchronized (mLock) { |
| 1621 | final List<JobInfo> pendingJobs = new ArrayList<JobInfo>(); |
| 1622 | mJobs.forEachJob(Process.SYSTEM_UID, new JobStatusFunctor() { |
| 1623 | @Override |
| 1624 | public void process(JobStatus job) { |
| 1625 | if (job.getJob().isPeriodic() || !isCurrentlyActiveLocked(job)) { |
| 1626 | pendingJobs.add(job.getJob()); |
| 1627 | } |
| 1628 | } |
| 1629 | }); |
| 1630 | return pendingJobs; |
| 1631 | } |
| 1632 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame^] | 1633 | |
| 1634 | @Override |
| 1635 | public void addBackingUpUid(int uid) { |
| 1636 | synchronized (mLock) { |
| 1637 | // No need to actually do anything here, since for a full backup the |
| 1638 | // activity manager will kill the process which will kill the job (and |
| 1639 | // cause it to restart, but now it can't run). |
| 1640 | mBackingUpUids.put(uid, uid); |
| 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | @Override |
| 1645 | public void removeBackingUpUid(int uid) { |
| 1646 | synchronized (mLock) { |
| 1647 | mBackingUpUids.delete(uid); |
| 1648 | // If there are any jobs for this uid, we need to rebuild the pending list |
| 1649 | // in case they are now ready to run. |
| 1650 | if (mJobs.countJobsForUid(uid) > 0) { |
| 1651 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
| 1652 | } |
| 1653 | } |
| 1654 | } |
| 1655 | |
| 1656 | @Override |
| 1657 | public void clearAllBackingUpUids() { |
| 1658 | synchronized (mLock) { |
| 1659 | if (mBackingUpUids.size() > 0) { |
| 1660 | mBackingUpUids.clear(); |
| 1661 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
| 1662 | } |
| 1663 | } |
| 1664 | } |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 1665 | } |
| 1666 | |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1667 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1668 | * Binder stub trampoline implementation |
| 1669 | */ |
| 1670 | final class JobSchedulerStub extends IJobScheduler.Stub { |
| 1671 | /** Cache determination of whether a given app can persist jobs |
| 1672 | * key is uid of the calling app; value is undetermined/true/false |
| 1673 | */ |
| 1674 | private final SparseArray<Boolean> mPersistCache = new SparseArray<Boolean>(); |
| 1675 | |
| 1676 | // Enforce that only the app itself (or shared uid participant) can schedule a |
| 1677 | // job that runs one of the app's services, as well as verifying that the |
| 1678 | // named service properly requires the BIND_JOB_SERVICE permission |
| 1679 | private void enforceValidJobRequest(int uid, JobInfo job) { |
Christopher Tate | 5568f54 | 2014-06-18 13:53:31 -0700 | [diff] [blame] | 1680 | final IPackageManager pm = AppGlobals.getPackageManager(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1681 | final ComponentName service = job.getService(); |
| 1682 | try { |
Jeff Sharkey | c7bacab | 2016-02-09 15:56:11 -0700 | [diff] [blame] | 1683 | ServiceInfo si = pm.getServiceInfo(service, |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 1684 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 1685 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, |
Jeff Sharkey | 12c0da4 | 2016-02-25 17:10:50 -0700 | [diff] [blame] | 1686 | UserHandle.getUserId(uid)); |
Christopher Tate | 5568f54 | 2014-06-18 13:53:31 -0700 | [diff] [blame] | 1687 | if (si == null) { |
| 1688 | throw new IllegalArgumentException("No such service " + service); |
| 1689 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1690 | if (si.applicationInfo.uid != uid) { |
| 1691 | throw new IllegalArgumentException("uid " + uid + |
| 1692 | " cannot schedule job in " + service.getPackageName()); |
| 1693 | } |
| 1694 | if (!JobService.PERMISSION_BIND.equals(si.permission)) { |
| 1695 | throw new IllegalArgumentException("Scheduled service " + service |
| 1696 | + " does not require android.permission.BIND_JOB_SERVICE permission"); |
| 1697 | } |
Christopher Tate | 5568f54 | 2014-06-18 13:53:31 -0700 | [diff] [blame] | 1698 | } catch (RemoteException e) { |
| 1699 | // Can't happen; the Package Manager is in this same process |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1700 | } |
| 1701 | } |
| 1702 | |
| 1703 | private boolean canPersistJobs(int pid, int uid) { |
| 1704 | // If we get this far we're good to go; all we need to do now is check |
| 1705 | // whether the app is allowed to persist its scheduled work. |
| 1706 | final boolean canPersist; |
| 1707 | synchronized (mPersistCache) { |
| 1708 | Boolean cached = mPersistCache.get(uid); |
| 1709 | if (cached != null) { |
| 1710 | canPersist = cached.booleanValue(); |
| 1711 | } else { |
| 1712 | // Persisting jobs is tantamount to running at boot, so we permit |
| 1713 | // it when the app has declared that it uses the RECEIVE_BOOT_COMPLETED |
| 1714 | // permission |
| 1715 | int result = getContext().checkPermission( |
| 1716 | android.Manifest.permission.RECEIVE_BOOT_COMPLETED, pid, uid); |
| 1717 | canPersist = (result == PackageManager.PERMISSION_GRANTED); |
| 1718 | mPersistCache.put(uid, canPersist); |
| 1719 | } |
| 1720 | } |
| 1721 | return canPersist; |
| 1722 | } |
| 1723 | |
| 1724 | // IJobScheduler implementation |
| 1725 | @Override |
| 1726 | public int schedule(JobInfo job) throws RemoteException { |
| 1727 | if (DEBUG) { |
Matthew Williams | ee410da | 2014-07-25 11:30:40 -0700 | [diff] [blame] | 1728 | Slog.d(TAG, "Scheduling job: " + job.toString()); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1729 | } |
| 1730 | final int pid = Binder.getCallingPid(); |
| 1731 | final int uid = Binder.getCallingUid(); |
| 1732 | |
| 1733 | enforceValidJobRequest(uid, job); |
Matthew Williams | 900c67f | 2014-07-09 12:46:53 -0700 | [diff] [blame] | 1734 | if (job.isPersisted()) { |
| 1735 | if (!canPersistJobs(pid, uid)) { |
| 1736 | throw new IllegalArgumentException("Error: requested job be persisted without" |
| 1737 | + " holding RECEIVE_BOOT_COMPLETED permission."); |
| 1738 | } |
| 1739 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1740 | |
Jeff Sharkey | 785f494 | 2016-07-14 10:31:15 -0600 | [diff] [blame] | 1741 | if ((job.getFlags() & JobInfo.FLAG_WILL_BE_FOREGROUND) != 0) { |
| 1742 | getContext().enforceCallingOrSelfPermission( |
| 1743 | android.Manifest.permission.CONNECTIVITY_INTERNAL, TAG); |
| 1744 | } |
| 1745 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1746 | long ident = Binder.clearCallingIdentity(); |
| 1747 | try { |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1748 | return JobSchedulerService.this.scheduleAsPackage(job, null, uid, null, -1, null); |
| 1749 | } finally { |
| 1750 | Binder.restoreCallingIdentity(ident); |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | // IJobScheduler implementation |
| 1755 | @Override |
| 1756 | public int enqueue(JobInfo job, JobWorkItem work) throws RemoteException { |
| 1757 | if (DEBUG) { |
| 1758 | Slog.d(TAG, "Enqueueing job: " + job.toString() + " work: " + work); |
| 1759 | } |
| 1760 | final int pid = Binder.getCallingPid(); |
| 1761 | final int uid = Binder.getCallingUid(); |
| 1762 | |
| 1763 | enforceValidJobRequest(uid, job); |
| 1764 | if (job.isPersisted()) { |
| 1765 | throw new IllegalArgumentException("Can't enqueue work for persisted jobs"); |
| 1766 | } |
| 1767 | if (work == null) { |
| 1768 | throw new NullPointerException("work is null"); |
| 1769 | } |
| 1770 | |
| 1771 | if ((job.getFlags() & JobInfo.FLAG_WILL_BE_FOREGROUND) != 0) { |
| 1772 | getContext().enforceCallingOrSelfPermission( |
| 1773 | android.Manifest.permission.CONNECTIVITY_INTERNAL, TAG); |
| 1774 | } |
| 1775 | |
| 1776 | long ident = Binder.clearCallingIdentity(); |
| 1777 | try { |
| 1778 | return JobSchedulerService.this.scheduleAsPackage(job, work, uid, null, -1, null); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1779 | } finally { |
| 1780 | Binder.restoreCallingIdentity(ident); |
| 1781 | } |
| 1782 | } |
| 1783 | |
| 1784 | @Override |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1785 | public int scheduleAsPackage(JobInfo job, String packageName, int userId, String tag) |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 1786 | throws RemoteException { |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1787 | final int callerUid = Binder.getCallingUid(); |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 1788 | if (DEBUG) { |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1789 | Slog.d(TAG, "Caller uid " + callerUid + " scheduling job: " + job.toString() |
| 1790 | + " on behalf of " + packageName); |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 1791 | } |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1792 | |
| 1793 | if (packageName == null) { |
| 1794 | throw new NullPointerException("Must specify a package for scheduleAsPackage()"); |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 1795 | } |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1796 | |
| 1797 | int mayScheduleForOthers = getContext().checkCallingOrSelfPermission( |
| 1798 | android.Manifest.permission.UPDATE_DEVICE_STATS); |
| 1799 | if (mayScheduleForOthers != PackageManager.PERMISSION_GRANTED) { |
| 1800 | throw new SecurityException("Caller uid " + callerUid |
| 1801 | + " not permitted to schedule jobs for other apps"); |
| 1802 | } |
| 1803 | |
Jeff Sharkey | 4f10040 | 2016-05-03 17:44:23 -0600 | [diff] [blame] | 1804 | if ((job.getFlags() & JobInfo.FLAG_WILL_BE_FOREGROUND) != 0) { |
| 1805 | getContext().enforceCallingOrSelfPermission( |
| 1806 | android.Manifest.permission.CONNECTIVITY_INTERNAL, TAG); |
| 1807 | } |
| 1808 | |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 1809 | long ident = Binder.clearCallingIdentity(); |
| 1810 | try { |
Dianne Hackborn | 7da13d7 | 2017-04-04 17:17:35 -0700 | [diff] [blame] | 1811 | return JobSchedulerService.this.scheduleAsPackage(job, null, callerUid, |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1812 | packageName, userId, tag); |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 1813 | } finally { |
| 1814 | Binder.restoreCallingIdentity(ident); |
| 1815 | } |
| 1816 | } |
| 1817 | |
| 1818 | @Override |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1819 | public List<JobInfo> getAllPendingJobs() throws RemoteException { |
| 1820 | final int uid = Binder.getCallingUid(); |
| 1821 | |
| 1822 | long ident = Binder.clearCallingIdentity(); |
| 1823 | try { |
| 1824 | return JobSchedulerService.this.getPendingJobs(uid); |
| 1825 | } finally { |
| 1826 | Binder.restoreCallingIdentity(ident); |
| 1827 | } |
| 1828 | } |
| 1829 | |
| 1830 | @Override |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1831 | public JobInfo getPendingJob(int jobId) throws RemoteException { |
| 1832 | final int uid = Binder.getCallingUid(); |
| 1833 | |
| 1834 | long ident = Binder.clearCallingIdentity(); |
| 1835 | try { |
| 1836 | return JobSchedulerService.this.getPendingJob(uid, jobId); |
| 1837 | } finally { |
| 1838 | Binder.restoreCallingIdentity(ident); |
| 1839 | } |
| 1840 | } |
| 1841 | |
| 1842 | @Override |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1843 | public void cancelAll() throws RemoteException { |
| 1844 | final int uid = Binder.getCallingUid(); |
| 1845 | |
| 1846 | long ident = Binder.clearCallingIdentity(); |
| 1847 | try { |
Dianne Hackborn | e07641d | 2016-11-09 15:07:23 -0800 | [diff] [blame] | 1848 | JobSchedulerService.this.cancelJobsForUid(uid); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1849 | } finally { |
| 1850 | Binder.restoreCallingIdentity(ident); |
| 1851 | } |
| 1852 | } |
| 1853 | |
| 1854 | @Override |
| 1855 | public void cancel(int jobId) throws RemoteException { |
| 1856 | final int uid = Binder.getCallingUid(); |
| 1857 | |
| 1858 | long ident = Binder.clearCallingIdentity(); |
| 1859 | try { |
| 1860 | JobSchedulerService.this.cancelJob(uid, jobId); |
| 1861 | } finally { |
| 1862 | Binder.restoreCallingIdentity(ident); |
| 1863 | } |
| 1864 | } |
| 1865 | |
| 1866 | /** |
| 1867 | * "dumpsys" infrastructure |
| 1868 | */ |
| 1869 | @Override |
| 1870 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
Jeff Sharkey | 6df866a | 2017-03-31 14:08:23 -0600 | [diff] [blame] | 1871 | if (!DumpUtils.checkDumpAndUsageStatsPermission(getContext(), TAG, pw)) return; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1872 | |
| 1873 | long identityToken = Binder.clearCallingIdentity(); |
| 1874 | try { |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1875 | JobSchedulerService.this.dumpInternal(pw, args); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1876 | } finally { |
| 1877 | Binder.restoreCallingIdentity(identityToken); |
| 1878 | } |
| 1879 | } |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 1880 | |
| 1881 | @Override |
| 1882 | public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err, |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 1883 | String[] args, ShellCallback callback, ResultReceiver resultReceiver) { |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 1884 | (new JobSchedulerShellCommand(JobSchedulerService.this)).exec( |
Dianne Hackborn | 354736e | 2016-08-22 17:00:05 -0700 | [diff] [blame] | 1885 | this, in, out, err, args, callback, resultReceiver); |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 1886 | } |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1887 | }; |
| 1888 | |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 1889 | // Shell command infrastructure: run the given job immediately |
| 1890 | int executeRunCommand(String pkgName, int userId, int jobId, boolean force) { |
| 1891 | if (DEBUG) { |
| 1892 | Slog.v(TAG, "executeRunCommand(): " + pkgName + "/" + userId |
| 1893 | + " " + jobId + " f=" + force); |
| 1894 | } |
| 1895 | |
| 1896 | try { |
| 1897 | final int uid = AppGlobals.getPackageManager().getPackageUid(pkgName, 0, userId); |
| 1898 | if (uid < 0) { |
| 1899 | return JobSchedulerShellCommand.CMD_ERR_NO_PACKAGE; |
| 1900 | } |
| 1901 | |
| 1902 | synchronized (mLock) { |
| 1903 | final JobStatus js = mJobs.getJobByUidAndJobId(uid, jobId); |
| 1904 | if (js == null) { |
| 1905 | return JobSchedulerShellCommand.CMD_ERR_NO_JOB; |
| 1906 | } |
| 1907 | |
| 1908 | js.overrideState = (force) ? JobStatus.OVERRIDE_FULL : JobStatus.OVERRIDE_SOFT; |
| 1909 | if (!js.isConstraintsSatisfied()) { |
| 1910 | js.overrideState = 0; |
| 1911 | return JobSchedulerShellCommand.CMD_ERR_CONSTRAINTS; |
| 1912 | } |
| 1913 | |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame^] | 1914 | queueReadyJobsForExecutionLocked(); |
| 1915 | maybeRunPendingJobsLocked(); |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 1916 | } |
| 1917 | } catch (RemoteException e) { |
| 1918 | // can't happen |
| 1919 | } |
| 1920 | return 0; |
| 1921 | } |
| 1922 | |
Dianne Hackborn | a06ec6a | 2017-02-13 10:08:42 -0800 | [diff] [blame] | 1923 | void setMonitorBattery(boolean enabled) { |
| 1924 | synchronized (mLock) { |
| 1925 | if (mBatteryController != null) { |
| 1926 | mBatteryController.getTracker().setMonitorBatteryLocked(enabled); |
| 1927 | } |
| 1928 | } |
| 1929 | } |
| 1930 | |
| 1931 | int getBatterySeq() { |
| 1932 | synchronized (mLock) { |
| 1933 | return mBatteryController != null ? mBatteryController.getTracker().getSeq() : -1; |
| 1934 | } |
| 1935 | } |
| 1936 | |
| 1937 | boolean getBatteryCharging() { |
| 1938 | synchronized (mLock) { |
| 1939 | return mBatteryController != null |
| 1940 | ? mBatteryController.getTracker().isOnStablePower() : false; |
| 1941 | } |
| 1942 | } |
| 1943 | |
| 1944 | boolean getBatteryNotLow() { |
| 1945 | synchronized (mLock) { |
| 1946 | return mBatteryController != null |
| 1947 | ? mBatteryController.getTracker().isBatteryNotLow() : false; |
| 1948 | } |
| 1949 | } |
| 1950 | |
Dianne Hackborn | 532ea26 | 2017-03-17 17:50:55 -0700 | [diff] [blame] | 1951 | int getStorageSeq() { |
| 1952 | synchronized (mLock) { |
| 1953 | return mStorageController != null ? mStorageController.getTracker().getSeq() : -1; |
| 1954 | } |
| 1955 | } |
| 1956 | |
| 1957 | boolean getStorageNotLow() { |
| 1958 | synchronized (mLock) { |
| 1959 | return mStorageController != null |
| 1960 | ? mStorageController.getTracker().isStorageNotLow() : false; |
| 1961 | } |
| 1962 | } |
| 1963 | |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1964 | private String printContextIdToJobMap(JobStatus[] map, String initial) { |
| 1965 | StringBuilder s = new StringBuilder(initial + ": "); |
| 1966 | for (int i=0; i<map.length; i++) { |
| 1967 | s.append("(") |
| 1968 | .append(map[i] == null? -1: map[i].getJobId()) |
| 1969 | .append(map[i] == null? -1: map[i].getUid()) |
| 1970 | .append(")" ); |
| 1971 | } |
| 1972 | return s.toString(); |
| 1973 | } |
| 1974 | |
| 1975 | private String printPendingQueue() { |
| 1976 | StringBuilder s = new StringBuilder("Pending queue: "); |
| 1977 | Iterator<JobStatus> it = mPendingJobs.iterator(); |
| 1978 | while (it.hasNext()) { |
| 1979 | JobStatus js = it.next(); |
| 1980 | s.append("(") |
| 1981 | .append(js.getJob().getId()) |
| 1982 | .append(", ") |
| 1983 | .append(js.getUid()) |
| 1984 | .append(") "); |
| 1985 | } |
| 1986 | return s.toString(); |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 1987 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1988 | |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 1989 | static void dumpHelp(PrintWriter pw) { |
| 1990 | pw.println("Job Scheduler (jobscheduler) dump options:"); |
| 1991 | pw.println(" [-h] [package] ..."); |
| 1992 | pw.println(" -h: print this help"); |
| 1993 | pw.println(" [package] is an optional package name to limit the output to."); |
| 1994 | } |
| 1995 | |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 1996 | void dumpInternal(final PrintWriter pw, String[] args) { |
| 1997 | int filterUid = -1; |
| 1998 | if (!ArrayUtils.isEmpty(args)) { |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 1999 | int opti = 0; |
| 2000 | while (opti < args.length) { |
| 2001 | String arg = args[opti]; |
| 2002 | if ("-h".equals(arg)) { |
| 2003 | dumpHelp(pw); |
| 2004 | return; |
| 2005 | } else if ("-a".equals(arg)) { |
| 2006 | // Ignore, we always dump all. |
| 2007 | } else if (arg.length() > 0 && arg.charAt(0) == '-') { |
| 2008 | pw.println("Unknown option: " + arg); |
| 2009 | return; |
| 2010 | } else { |
| 2011 | break; |
| 2012 | } |
| 2013 | opti++; |
| 2014 | } |
| 2015 | if (opti < args.length) { |
| 2016 | String pkg = args[opti]; |
| 2017 | try { |
| 2018 | filterUid = getContext().getPackageManager().getPackageUid(pkg, |
Amith Yamasani | 0d1fd8d | 2016-10-12 14:21:51 -0700 | [diff] [blame] | 2019 | PackageManager.MATCH_ANY_USER); |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 2020 | } catch (NameNotFoundException ignored) { |
| 2021 | pw.println("Invalid package: " + pkg); |
| 2022 | return; |
| 2023 | } |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 2024 | } |
| 2025 | } |
| 2026 | |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 2027 | final int filterUidFinal = UserHandle.getAppId(filterUid); |
Christopher Tate | f973a7b | 2014-08-29 12:54:08 -0700 | [diff] [blame] | 2028 | final long now = SystemClock.elapsedRealtime(); |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 2029 | synchronized (mLock) { |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 2030 | mConstants.dump(pw); |
| 2031 | pw.println(); |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 2032 | pw.println("Started users: " + Arrays.toString(mStartedUsers)); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 2033 | pw.print("Registered "); |
| 2034 | pw.print(mJobs.size()); |
| 2035 | pw.println(" jobs:"); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2036 | if (mJobs.size() > 0) { |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 2037 | final List<JobStatus> jobs = mJobs.mJobSet.getAllJobs(); |
| 2038 | Collections.sort(jobs, new Comparator<JobStatus>() { |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 2039 | @Override |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 2040 | public int compare(JobStatus o1, JobStatus o2) { |
| 2041 | int uid1 = o1.getUid(); |
| 2042 | int uid2 = o2.getUid(); |
| 2043 | int id1 = o1.getJobId(); |
| 2044 | int id2 = o2.getJobId(); |
| 2045 | if (uid1 != uid2) { |
| 2046 | return uid1 < uid2 ? -1 : 1; |
Jeff Sharkey | f07c7b9 | 2016-04-22 09:50:16 -0600 | [diff] [blame] | 2047 | } |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 2048 | return id1 < id2 ? -1 : (id1 > id2 ? 1 : 0); |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 2049 | } |
| 2050 | }); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 2051 | for (JobStatus job : jobs) { |
| 2052 | pw.print(" JOB #"); job.printUniqueId(pw); pw.print(": "); |
| 2053 | pw.println(job.toShortStringExceptUniqueId()); |
| 2054 | |
| 2055 | // Skip printing details if the caller requested a filter |
| 2056 | if (!job.shouldDump(filterUidFinal)) { |
| 2057 | continue; |
| 2058 | } |
| 2059 | |
| 2060 | job.dump(pw, " ", true); |
| 2061 | pw.print(" Ready: "); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame^] | 2062 | pw.print(isReadyToBeExecutedLocked(job)); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 2063 | pw.print(" (job="); |
| 2064 | pw.print(job.isReady()); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 2065 | pw.print(" user="); |
| 2066 | pw.print(ArrayUtils.contains(mStartedUsers, job.getUserId())); |
Dianne Hackborn | 0aa4313 | 2017-03-22 11:42:03 -0700 | [diff] [blame] | 2067 | pw.print(" !pending="); |
| 2068 | pw.print(!mPendingJobs.contains(job)); |
| 2069 | pw.print(" !active="); |
| 2070 | pw.print(!isCurrentlyActiveLocked(job)); |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame^] | 2071 | pw.print(" !backingup="); |
| 2072 | pw.print(!(mBackingUpUids.indexOfKey(job.getSourceUid()) >= 0)); |
Dianne Hackborn | 0aa4313 | 2017-03-22 11:42:03 -0700 | [diff] [blame] | 2073 | pw.print(" comp="); |
| 2074 | boolean componentPresent = false; |
| 2075 | try { |
| 2076 | componentPresent = (AppGlobals.getPackageManager().getServiceInfo( |
| 2077 | job.getServiceComponent(), |
| 2078 | PackageManager.MATCH_DEBUG_TRIAGED_MISSING, |
| 2079 | job.getUserId()) != null); |
| 2080 | } catch (RemoteException e) { |
| 2081 | } |
| 2082 | pw.print(componentPresent); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 2083 | pw.println(")"); |
| 2084 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2085 | } else { |
Christopher Tate | f973a7b | 2014-08-29 12:54:08 -0700 | [diff] [blame] | 2086 | pw.println(" None."); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2087 | } |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 2088 | for (int i=0; i<mControllers.size(); i++) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2089 | pw.println(); |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 2090 | mControllers.get(i).dumpControllerStateLocked(pw, filterUidFinal); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2091 | } |
| 2092 | pw.println(); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 2093 | pw.println("Uid priority overrides:"); |
| 2094 | for (int i=0; i< mUidPriorityOverride.size(); i++) { |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 2095 | int uid = mUidPriorityOverride.keyAt(i); |
| 2096 | if (filterUidFinal == -1 || filterUidFinal == UserHandle.getAppId(uid)) { |
| 2097 | pw.print(" "); pw.print(UserHandle.formatUid(uid)); |
| 2098 | pw.print(": "); pw.println(mUidPriorityOverride.valueAt(i)); |
| 2099 | } |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 2100 | } |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame^] | 2101 | if (mBackingUpUids.size() > 0) { |
| 2102 | pw.println(); |
| 2103 | pw.println("Backing up uids:"); |
| 2104 | boolean first = true; |
| 2105 | for (int i = 0; i < mBackingUpUids.size(); i++) { |
| 2106 | int uid = mBackingUpUids.keyAt(i); |
| 2107 | if (filterUidFinal == -1 || filterUidFinal == UserHandle.getAppId(uid)) { |
| 2108 | if (first) { |
| 2109 | pw.print(" "); |
| 2110 | first = false; |
| 2111 | } else { |
| 2112 | pw.print(", "); |
| 2113 | } |
| 2114 | pw.print(UserHandle.formatUid(uid)); |
| 2115 | } |
| 2116 | } |
| 2117 | pw.println(); |
| 2118 | } |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 2119 | pw.println(); |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 2120 | mJobPackageTracker.dump(pw, "", filterUidFinal); |
Dianne Hackborn | 807de78 | 2016-04-07 17:54:41 -0700 | [diff] [blame] | 2121 | pw.println(); |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 2122 | if (mJobPackageTracker.dumpHistory(pw, "", filterUidFinal)) { |
| 2123 | pw.println(); |
| 2124 | } |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 2125 | pw.println("Pending queue:"); |
| 2126 | for (int i=0; i<mPendingJobs.size(); i++) { |
| 2127 | JobStatus job = mPendingJobs.get(i); |
| 2128 | pw.print(" Pending #"); pw.print(i); pw.print(": "); |
| 2129 | pw.println(job.toShortString()); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 2130 | job.dump(pw, " ", false); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 2131 | int priority = evaluateJobPriorityLocked(job); |
| 2132 | if (priority != JobInfo.PRIORITY_DEFAULT) { |
| 2133 | pw.print(" Evaluated priority: "); pw.println(priority); |
| 2134 | } |
| 2135 | pw.print(" Tag: "); pw.println(job.getTag()); |
Christopher Tate | 7234fc6 | 2017-04-03 17:36:07 -0700 | [diff] [blame] | 2136 | pw.print(" Enq: "); |
| 2137 | TimeUtils.formatDuration(now - job.madePending, pw); |
| 2138 | pw.println(" ago"); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 2139 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2140 | pw.println(); |
| 2141 | pw.println("Active jobs:"); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 2142 | for (int i=0; i<mActiveServices.size(); i++) { |
| 2143 | JobServiceContext jsc = mActiveServices.get(i); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 2144 | pw.print(" Slot #"); pw.print(i); pw.print(": "); |
Christopher Tate | 7234fc6 | 2017-04-03 17:36:07 -0700 | [diff] [blame] | 2145 | final JobStatus job = jsc.getRunningJob(); |
| 2146 | if (job == null) { |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 2147 | pw.println("inactive"); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2148 | continue; |
| 2149 | } else { |
Christopher Tate | 7234fc6 | 2017-04-03 17:36:07 -0700 | [diff] [blame] | 2150 | pw.println(job.toShortString()); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 2151 | pw.print(" Running for: "); |
| 2152 | TimeUtils.formatDuration(now - jsc.getExecutionStartTimeElapsed(), pw); |
| 2153 | pw.print(", timeout at: "); |
| 2154 | TimeUtils.formatDuration(jsc.getTimeoutElapsed() - now, pw); |
| 2155 | pw.println(); |
Christopher Tate | 7234fc6 | 2017-04-03 17:36:07 -0700 | [diff] [blame] | 2156 | job.dump(pw, " ", false); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 2157 | int priority = evaluateJobPriorityLocked(jsc.getRunningJob()); |
| 2158 | if (priority != JobInfo.PRIORITY_DEFAULT) { |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 2159 | pw.print(" Evaluated priority: "); pw.println(priority); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 2160 | } |
Christopher Tate | 7234fc6 | 2017-04-03 17:36:07 -0700 | [diff] [blame] | 2161 | pw.print(" Active at "); pw.println(job.madeActive); |
| 2162 | pw.print(" Pending for "); |
| 2163 | TimeUtils.formatDuration(job.madeActive - job.madePending, pw); |
| 2164 | pw.println(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2165 | } |
| 2166 | } |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 2167 | if (filterUid == -1) { |
| 2168 | pw.println(); |
| 2169 | pw.print("mReadyToRock="); pw.println(mReadyToRock); |
| 2170 | pw.print("mReportedActive="); pw.println(mReportedActive); |
| 2171 | pw.print("mMaxActiveJobs="); pw.println(mMaxActiveJobs); |
| 2172 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 2173 | } |
| 2174 | pw.println(); |
| 2175 | } |
| 2176 | } |