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