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