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 | |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 19 | import java.io.FileDescriptor; |
| 20 | import java.io.PrintWriter; |
| 21 | import java.util.ArrayList; |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 22 | import java.util.Arrays; |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 23 | import java.util.Iterator; |
| 24 | import java.util.List; |
| 25 | |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 26 | import android.app.ActivityManager; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 27 | import android.app.ActivityManagerNative; |
Christopher Tate | 5568f54 | 2014-06-18 13:53:31 -0700 | [diff] [blame] | 28 | import android.app.AppGlobals; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 29 | import android.app.IUidObserver; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 30 | import android.app.job.JobInfo; |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 31 | import android.app.job.JobParameters; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 32 | import android.app.job.JobScheduler; |
| 33 | import android.app.job.JobService; |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 34 | import android.app.job.IJobScheduler; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 35 | import android.content.BroadcastReceiver; |
| 36 | import android.content.ComponentName; |
| 37 | import android.content.Context; |
| 38 | import android.content.Intent; |
| 39 | import android.content.IntentFilter; |
Christopher Tate | 5568f54 | 2014-06-18 13:53:31 -0700 | [diff] [blame] | 40 | import android.content.pm.IPackageManager; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 41 | import android.content.pm.PackageManager; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 42 | import android.content.pm.ServiceInfo; |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 43 | import android.os.BatteryStats; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 44 | import android.os.Binder; |
| 45 | import android.os.Handler; |
| 46 | import android.os.Looper; |
| 47 | import android.os.Message; |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 48 | import android.os.Process; |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 49 | import android.os.PowerManager; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 50 | import android.os.RemoteException; |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 51 | import android.os.ResultReceiver; |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 52 | import android.os.ServiceManager; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 53 | import android.os.SystemClock; |
| 54 | import android.os.UserHandle; |
| 55 | import android.util.Slog; |
| 56 | import android.util.SparseArray; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 57 | import android.util.SparseIntArray; |
| 58 | import android.util.TimeUtils; |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 59 | |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 60 | import com.android.internal.app.IBatteryStats; |
Joe Onorato | 4eb64fd | 2016-03-21 15:30:09 -0700 | [diff] [blame] | 61 | import com.android.internal.app.procstats.ProcessStats; |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 62 | import com.android.internal.util.ArrayUtils; |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 63 | import com.android.server.DeviceIdleController; |
| 64 | import com.android.server.LocalServices; |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 65 | import com.android.server.job.JobStore.JobStatusFunctor; |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 66 | import com.android.server.job.controllers.AppIdleController; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 67 | import com.android.server.job.controllers.BatteryController; |
| 68 | import com.android.server.job.controllers.ConnectivityController; |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 69 | import com.android.server.job.controllers.ContentObserverController; |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 70 | import com.android.server.job.controllers.DeviceIdleJobsController; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 71 | import com.android.server.job.controllers.IdleController; |
| 72 | import com.android.server.job.controllers.JobStatus; |
| 73 | import com.android.server.job.controllers.StateController; |
| 74 | import com.android.server.job.controllers.TimeController; |
| 75 | |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 76 | import libcore.util.EmptyArray; |
| 77 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 78 | /** |
| 79 | * Responsible for taking jobs representing work to be performed by a client app, and determining |
| 80 | * based on the criteria specified when that job should be run against the client application's |
| 81 | * endpoint. |
| 82 | * Implements logic for scheduling, and rescheduling jobs. The JobSchedulerService knows nothing |
| 83 | * about constraints, or the state of active jobs. It receives callbacks from the various |
| 84 | * controllers and completed jobs and operates accordingly. |
| 85 | * |
| 86 | * Note on locking: Any operations that manipulate {@link #mJobs} need to lock on that object. |
| 87 | * Any function with the suffix 'Locked' also needs to lock on {@link #mJobs}. |
| 88 | * @hide |
| 89 | */ |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 90 | public final class JobSchedulerService extends com.android.server.SystemService |
Matthew Williams | 01ac45b | 2014-07-22 20:44:12 -0700 | [diff] [blame] | 91 | implements StateChangedListener, JobCompletedListener { |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 92 | static final String TAG = "JobSchedulerService"; |
Matthew Williams | aa98431 | 2015-10-15 16:08:05 -0700 | [diff] [blame] | 93 | public static final boolean DEBUG = false; |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 94 | |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 95 | /** The maximum number of concurrent jobs we run at one time. */ |
| 96 | private static final int MAX_JOB_CONTEXTS_COUNT = 8; |
Christopher Tate | dabdf6f | 2016-02-24 12:30:22 -0800 | [diff] [blame] | 97 | /** Enforce a per-app limit on scheduled jobs? */ |
Christopher Tate | 0213ace0 | 2016-02-24 14:18:35 -0800 | [diff] [blame] | 98 | private static final boolean ENFORCE_MAX_JOBS = true; |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 99 | /** The maximum number of jobs that we allow an unprivileged app to schedule */ |
| 100 | private static final int MAX_JOBS_PER_APP = 100; |
| 101 | |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 102 | /** Global local for all job scheduler state. */ |
| 103 | final Object mLock = new Object(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 104 | /** Master list of jobs. */ |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 105 | final JobStore mJobs; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 106 | |
| 107 | static final int MSG_JOB_EXPIRED = 0; |
| 108 | static final int MSG_CHECK_JOB = 1; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 109 | static final int MSG_STOP_JOB = 2; |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 110 | static final int MSG_CHECK_JOB_GREEDY = 3; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 111 | |
| 112 | // Policy constants |
| 113 | /** |
| 114 | * Minimum # of idle jobs that must be ready in order to force the JMS to schedule things |
| 115 | * early. |
| 116 | */ |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 117 | static final int MIN_IDLE_COUNT = 1; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 118 | /** |
Matthew Williams | be0c417 | 2014-08-06 18:14:16 -0700 | [diff] [blame] | 119 | * Minimum # of charging jobs that must be ready in order to force the JMS to schedule things |
| 120 | * early. |
| 121 | */ |
| 122 | static final int MIN_CHARGING_COUNT = 1; |
| 123 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 124 | * Minimum # of connectivity jobs that must be ready in order to force the JMS to schedule |
| 125 | * things early. |
| 126 | */ |
Matthew Williams | aa98431 | 2015-10-15 16:08:05 -0700 | [diff] [blame] | 127 | static final int MIN_CONNECTIVITY_COUNT = 1; // Run connectivity jobs as soon as ready. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 128 | /** |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 129 | * Minimum # of content trigger jobs that must be ready in order to force the JMS to schedule |
| 130 | * things early. |
| 131 | */ |
| 132 | static final int MIN_CONTENT_COUNT = 1; |
| 133 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 134 | * Minimum # of jobs (with no particular constraints) for which the JMS will be happy running |
| 135 | * some work early. |
Matthew Williams | be0c417 | 2014-08-06 18:14:16 -0700 | [diff] [blame] | 136 | * This is correlated with the amount of batching we'll be able to do. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 137 | */ |
Matthew Williams | be0c417 | 2014-08-06 18:14:16 -0700 | [diff] [blame] | 138 | static final int MIN_READY_JOBS_COUNT = 2; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 139 | |
| 140 | /** |
| 141 | * Track Services that have currently active or pending jobs. The index is provided by |
| 142 | * {@link JobStatus#getServiceToken()} |
| 143 | */ |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 144 | final List<JobServiceContext> mActiveServices = new ArrayList<>(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 145 | /** List of controllers that will notify this service of updates to jobs. */ |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 146 | List<StateController> mControllers; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 147 | /** |
| 148 | * Queue of pending jobs. The JobServiceContext class will receive jobs from this list |
| 149 | * when ready to execute them. |
| 150 | */ |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 151 | final ArrayList<JobStatus> mPendingJobs = new ArrayList<>(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 152 | |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 153 | int[] mStartedUsers = EmptyArray.INT; |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 154 | |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 155 | final JobHandler mHandler; |
| 156 | final JobSchedulerStub mJobSchedulerStub; |
| 157 | |
| 158 | IBatteryStats mBatteryStats; |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 159 | PowerManager mPowerManager; |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 160 | DeviceIdleController.LocalService mLocalDeviceIdleController; |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 161 | |
| 162 | /** |
| 163 | * Set to true once we are allowed to run third party apps. |
| 164 | */ |
| 165 | boolean mReadyToRock; |
| 166 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 167 | /** |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 168 | * What we last reported to DeviceIdleController about whether we are active. |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 169 | */ |
| 170 | boolean mReportedActive; |
| 171 | |
| 172 | /** |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 173 | * Current limit on the number of concurrent JobServiceContext entries we want to |
| 174 | * keep actively running a job. |
| 175 | */ |
| 176 | int mMaxActiveJobs = MAX_JOB_CONTEXTS_COUNT - 2; |
| 177 | |
| 178 | /** |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 179 | * Which uids are currently in the foreground. |
| 180 | */ |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 181 | final SparseIntArray mUidPriorityOverride = new SparseIntArray(); |
| 182 | |
| 183 | // -- Pre-allocated temporaries only for use in assignJobsToContextsLocked -- |
| 184 | |
| 185 | /** |
| 186 | * This array essentially stores the state of mActiveServices array. |
| 187 | * The ith index stores the job present on the ith JobServiceContext. |
| 188 | * We manipulate this array until we arrive at what jobs should be running on |
| 189 | * what JobServiceContext. |
| 190 | */ |
| 191 | JobStatus[] mTmpAssignContextIdToJobMap = new JobStatus[MAX_JOB_CONTEXTS_COUNT]; |
| 192 | /** |
| 193 | * Indicates whether we need to act on this jobContext id |
| 194 | */ |
| 195 | boolean[] mTmpAssignAct = new boolean[MAX_JOB_CONTEXTS_COUNT]; |
| 196 | /** |
| 197 | * The uid whose jobs we would like to assign to a context. |
| 198 | */ |
| 199 | int[] mTmpAssignPreferredUidForContext = new int[MAX_JOB_CONTEXTS_COUNT]; |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 200 | |
| 201 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 202 | * Cleans up outstanding jobs when a package is removed. Even if it's being replaced later we |
| 203 | * still clean up. On reinstall the package will have a new uid. |
| 204 | */ |
| 205 | private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 206 | @Override |
| 207 | public void onReceive(Context context, Intent intent) { |
Dianne Hackborn | 2fefbcf | 2016-03-18 15:34:54 -0700 | [diff] [blame] | 208 | if (DEBUG) { |
| 209 | Slog.d(TAG, "Receieved: " + intent.getAction()); |
| 210 | } |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 211 | if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) { |
Christopher Tate | aad67a3 | 2014-10-20 16:29:20 -0700 | [diff] [blame] | 212 | // If this is an outright uninstall rather than the first half of an |
| 213 | // app update sequence, cancel the jobs associated with the app. |
| 214 | if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
| 215 | int uidRemoved = intent.getIntExtra(Intent.EXTRA_UID, -1); |
| 216 | if (DEBUG) { |
| 217 | Slog.d(TAG, "Removing jobs for uid: " + uidRemoved); |
| 218 | } |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 219 | cancelJobsForUid(uidRemoved, true); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 220 | } |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 221 | } else if (Intent.ACTION_USER_REMOVED.equals(intent.getAction())) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 222 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0); |
| 223 | if (DEBUG) { |
| 224 | Slog.d(TAG, "Removing jobs for user: " + userId); |
| 225 | } |
| 226 | cancelJobsForUser(userId); |
| 227 | } |
| 228 | } |
| 229 | }; |
| 230 | |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 231 | final private IUidObserver mUidObserver = new IUidObserver.Stub() { |
| 232 | @Override public void onUidStateChanged(int uid, int procState) throws RemoteException { |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 233 | updateUidState(uid, procState); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | @Override public void onUidGone(int uid) throws RemoteException { |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 237 | updateUidState(uid, ActivityManager.PROCESS_STATE_CACHED_EMPTY); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | @Override public void onUidActive(int uid) throws RemoteException { |
| 241 | } |
| 242 | |
| 243 | @Override public void onUidIdle(int uid) throws RemoteException { |
| 244 | cancelJobsForUid(uid, false); |
| 245 | } |
| 246 | }; |
| 247 | |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 248 | public Object getLock() { |
| 249 | return mLock; |
| 250 | } |
| 251 | |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 252 | @Override |
| 253 | public void onStartUser(int userHandle) { |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 254 | mStartedUsers = ArrayUtils.appendInt(mStartedUsers, userHandle); |
| 255 | // Let's kick any outstanding jobs for this user. |
| 256 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
| 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public void onUnlockUser(int userHandle) { |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 261 | // Let's kick any outstanding jobs for this user. |
| 262 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
| 263 | } |
| 264 | |
| 265 | @Override |
| 266 | public void onStopUser(int userHandle) { |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 267 | mStartedUsers = ArrayUtils.removeInt(mStartedUsers, userHandle); |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 270 | /** |
| 271 | * Entry point from client to schedule the provided job. |
| 272 | * This cancels the job if it's already been scheduled, and replaces it with the one provided. |
| 273 | * @param job JobInfo object containing execution parameters |
| 274 | * @param uId The package identifier of the application this job is for. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 275 | * @return Result of this operation. See <code>JobScheduler#RESULT_*</code> return codes. |
| 276 | */ |
Matthew Williams | 900c67f | 2014-07-09 12:46:53 -0700 | [diff] [blame] | 277 | public int schedule(JobInfo job, int uId) { |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 278 | return scheduleAsPackage(job, uId, null, -1, null); |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 279 | } |
| 280 | |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 281 | public int scheduleAsPackage(JobInfo job, int uId, String packageName, int userId, |
| 282 | String tag) { |
| 283 | JobStatus jobStatus = JobStatus.createFromJobInfo(job, uId, packageName, userId, tag); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 284 | try { |
| 285 | if (ActivityManagerNative.getDefault().getAppStartMode(uId, |
| 286 | job.getService().getPackageName()) == ActivityManager.APP_START_MODE_DISABLED) { |
| 287 | Slog.w(TAG, "Not scheduling job " + uId + ":" + job.toString() |
| 288 | + " -- package not allowed to start"); |
| 289 | return JobScheduler.RESULT_FAILURE; |
| 290 | } |
| 291 | } catch (RemoteException e) { |
| 292 | } |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 293 | if (DEBUG) Slog.d(TAG, "SCHEDULE: " + jobStatus.toShortString()); |
| 294 | JobStatus toCancel; |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 295 | synchronized (mLock) { |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 296 | // 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] | 297 | if (ENFORCE_MAX_JOBS && packageName == null) { |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 298 | if (mJobs.countJobsForUid(uId) > MAX_JOBS_PER_APP) { |
| 299 | Slog.w(TAG, "Too many jobs for uid " + uId); |
| 300 | throw new IllegalStateException("Apps may not schedule more than " |
| 301 | + MAX_JOBS_PER_APP + " distinct jobs"); |
| 302 | } |
| 303 | } |
| 304 | |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 305 | toCancel = mJobs.getJobByUidAndJobId(uId, job.getId()); |
Christopher Tate | b1c1f9a | 2016-03-17 13:29:25 -0700 | [diff] [blame] | 306 | if (toCancel != null) { |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame^] | 307 | cancelJobImpl(toCancel, jobStatus); |
Christopher Tate | b1c1f9a | 2016-03-17 13:29:25 -0700 | [diff] [blame] | 308 | } |
| 309 | startTrackingJob(jobStatus, toCancel); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 310 | } |
Matthew Williams | bafeeb9 | 2014-08-08 11:51:06 -0700 | [diff] [blame] | 311 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 312 | return JobScheduler.RESULT_SUCCESS; |
| 313 | } |
| 314 | |
| 315 | public List<JobInfo> getPendingJobs(int uid) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 316 | synchronized (mLock) { |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 317 | List<JobStatus> jobs = mJobs.getJobsByUid(uid); |
| 318 | ArrayList<JobInfo> outList = new ArrayList<JobInfo>(jobs.size()); |
| 319 | for (int i = jobs.size() - 1; i >= 0; i--) { |
| 320 | JobStatus job = jobs.get(i); |
| 321 | outList.add(job.getJob()); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 322 | } |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 323 | return outList; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 324 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 327 | void cancelJobsForUser(int userHandle) { |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 328 | List<JobStatus> jobsForUser; |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 329 | synchronized (mLock) { |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 330 | jobsForUser = mJobs.getJobsByUser(userHandle); |
| 331 | } |
| 332 | for (int i=0; i<jobsForUser.size(); i++) { |
| 333 | JobStatus toRemove = jobsForUser.get(i); |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame^] | 334 | cancelJobImpl(toRemove, null); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Entry point from client to cancel all jobs originating from their uid. |
| 340 | * This will remove the job from the master list, and cancel the job if it was staged for |
| 341 | * execution or being executed. |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 342 | * @param uid Uid to check against for removal of a job. |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 343 | * @param forceAll If true, all jobs for the uid will be canceled; if false, only those |
| 344 | * whose apps are stopped. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 345 | */ |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 346 | public void cancelJobsForUid(int uid, boolean forceAll) { |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 347 | List<JobStatus> jobsForUid; |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 348 | synchronized (mLock) { |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 349 | jobsForUid = mJobs.getJobsByUid(uid); |
| 350 | } |
| 351 | for (int i=0; i<jobsForUid.size(); i++) { |
| 352 | JobStatus toRemove = jobsForUid.get(i); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 353 | if (!forceAll) { |
| 354 | String packageName = toRemove.getServiceComponent().getPackageName(); |
| 355 | try { |
| 356 | if (ActivityManagerNative.getDefault().getAppStartMode(uid, packageName) |
| 357 | != ActivityManager.APP_START_MODE_DISABLED) { |
| 358 | continue; |
| 359 | } |
| 360 | } catch (RemoteException e) { |
| 361 | } |
| 362 | } |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame^] | 363 | cancelJobImpl(toRemove, null); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 364 | } |
| 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Entry point from client to cancel the job corresponding to the jobId provided. |
| 369 | * This will remove the job from the master list, and cancel the job if it was staged for |
| 370 | * execution or being executed. |
| 371 | * @param uid Uid of the calling client. |
| 372 | * @param jobId Id of the job, provided at schedule-time. |
| 373 | */ |
| 374 | public void cancelJob(int uid, int jobId) { |
| 375 | JobStatus toCancel; |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 376 | synchronized (mLock) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 377 | toCancel = mJobs.getJobByUidAndJobId(uid, jobId); |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 378 | } |
| 379 | if (toCancel != null) { |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame^] | 380 | cancelJobImpl(toCancel, null); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 381 | } |
| 382 | } |
| 383 | |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame^] | 384 | private void cancelJobImpl(JobStatus cancelled, JobStatus incomingJob) { |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 385 | if (DEBUG) Slog.d(TAG, "CANCEL: " + cancelled.toShortString()); |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame^] | 386 | stopTrackingJob(cancelled, incomingJob, true /* writeBack */); |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 387 | synchronized (mLock) { |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 388 | // Remove from pending queue. |
| 389 | mPendingJobs.remove(cancelled); |
| 390 | // Cancel if running. |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 391 | stopJobOnServiceContextLocked(cancelled, JobParameters.REASON_CANCELED); |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 392 | reportActive(); |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 393 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 396 | void updateUidState(int uid, int procState) { |
| 397 | synchronized (mLock) { |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 398 | if (procState == ActivityManager.PROCESS_STATE_TOP) { |
| 399 | // Only use this if we are exactly the top app. All others can live |
| 400 | // with just the foreground priority. This means that persistent processes |
| 401 | // can never be the top app priority... that is fine. |
| 402 | mUidPriorityOverride.put(uid, JobInfo.PRIORITY_TOP_APP); |
| 403 | } else if (procState <= ActivityManager.PROCESS_STATE_FOREGROUND_SERVICE) { |
| 404 | mUidPriorityOverride.put(uid, JobInfo.PRIORITY_FOREGROUND_APP); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 405 | } else { |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 406 | mUidPriorityOverride.delete(uid); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 407 | } |
| 408 | } |
| 409 | } |
| 410 | |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 411 | @Override |
| 412 | public void onDeviceIdleStateChanged(boolean deviceIdle) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 413 | synchronized (mLock) { |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 414 | if (deviceIdle) { |
| 415 | // When becoming idle, make sure no jobs are actively running. |
| 416 | for (int i=0; i<mActiveServices.size(); i++) { |
| 417 | JobServiceContext jsc = mActiveServices.get(i); |
| 418 | final JobStatus executing = jsc.getRunningJob(); |
| 419 | if (executing != null) { |
| 420 | jsc.cancelExecutingJob(JobParameters.REASON_DEVICE_IDLE); |
| 421 | } |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 422 | } |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 423 | } else { |
| 424 | // When coming out of idle, allow thing to start back up. |
| 425 | if (mReadyToRock) { |
| 426 | if (mLocalDeviceIdleController != null) { |
| 427 | if (!mReportedActive) { |
| 428 | mReportedActive = true; |
| 429 | mLocalDeviceIdleController.setJobsActive(true); |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 430 | } |
| 431 | } |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 432 | } |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 433 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | } |
| 437 | |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 438 | void reportActive() { |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 439 | // active is true if pending queue contains jobs OR some job is running. |
| 440 | boolean active = mPendingJobs.size() > 0; |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 441 | if (mPendingJobs.size() <= 0) { |
| 442 | for (int i=0; i<mActiveServices.size(); i++) { |
| 443 | JobServiceContext jsc = mActiveServices.get(i); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 444 | if (jsc.getRunningJob() != null) { |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 445 | active = true; |
| 446 | break; |
| 447 | } |
| 448 | } |
| 449 | } |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 450 | |
| 451 | if (mReportedActive != active) { |
| 452 | mReportedActive = active; |
| 453 | if (mLocalDeviceIdleController != null) { |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 454 | mLocalDeviceIdleController.setJobsActive(active); |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 459 | /** |
| 460 | * Initializes the system service. |
| 461 | * <p> |
| 462 | * Subclasses must define a single argument constructor that accepts the context |
| 463 | * and passes it to super. |
| 464 | * </p> |
| 465 | * |
| 466 | * @param context The system server context. |
| 467 | */ |
| 468 | public JobSchedulerService(Context context) { |
| 469 | super(context); |
| 470 | // Create the controllers. |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 471 | mControllers = new ArrayList<StateController>(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 472 | mControllers.add(ConnectivityController.get(this)); |
| 473 | mControllers.add(TimeController.get(this)); |
| 474 | mControllers.add(IdleController.get(this)); |
| 475 | mControllers.add(BatteryController.get(this)); |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 476 | mControllers.add(AppIdleController.get(this)); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 477 | mControllers.add(ContentObserverController.get(this)); |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 478 | mControllers.add(DeviceIdleJobsController.get(this)); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 479 | |
| 480 | mHandler = new JobHandler(context.getMainLooper()); |
| 481 | mJobSchedulerStub = new JobSchedulerStub(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 482 | mJobs = JobStore.initAndGet(this); |
| 483 | } |
| 484 | |
| 485 | @Override |
| 486 | public void onStart() { |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 487 | publishLocalService(JobSchedulerInternal.class, new LocalService()); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 488 | publishBinderService(Context.JOB_SCHEDULER_SERVICE, mJobSchedulerStub); |
| 489 | } |
| 490 | |
| 491 | @Override |
| 492 | public void onBootPhase(int phase) { |
| 493 | if (PHASE_SYSTEM_SERVICES_READY == phase) { |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 494 | // Register br for package removals and user removals. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 495 | final IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_REMOVED); |
| 496 | filter.addDataScheme("package"); |
| 497 | getContext().registerReceiverAsUser( |
| 498 | mBroadcastReceiver, UserHandle.ALL, filter, null, null); |
| 499 | final IntentFilter userFilter = new IntentFilter(Intent.ACTION_USER_REMOVED); |
| 500 | getContext().registerReceiverAsUser( |
| 501 | mBroadcastReceiver, UserHandle.ALL, userFilter, null, null); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 502 | mPowerManager = (PowerManager)getContext().getSystemService(Context.POWER_SERVICE); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 503 | try { |
| 504 | ActivityManagerNative.getDefault().registerUidObserver(mUidObserver, |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 505 | ActivityManager.UID_OBSERVER_PROCSTATE | ActivityManager.UID_OBSERVER_GONE |
| 506 | | ActivityManager.UID_OBSERVER_IDLE); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 507 | } catch (RemoteException e) { |
| 508 | // ignored; both services live in system_server |
| 509 | } |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 510 | } else if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 511 | synchronized (mLock) { |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 512 | // Let's go! |
| 513 | mReadyToRock = true; |
| 514 | mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService( |
| 515 | BatteryStats.SERVICE_NAME)); |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 516 | mLocalDeviceIdleController |
| 517 | = LocalServices.getService(DeviceIdleController.LocalService.class); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 518 | // Create the "runners". |
| 519 | for (int i = 0; i < MAX_JOB_CONTEXTS_COUNT; i++) { |
| 520 | mActiveServices.add( |
| 521 | new JobServiceContext(this, mBatteryStats, |
| 522 | getContext().getMainLooper())); |
| 523 | } |
| 524 | // Attach jobs to their controllers. |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 525 | mJobs.forEachJob(new JobStatusFunctor() { |
| 526 | @Override |
| 527 | public void process(JobStatus job) { |
| 528 | for (int controller = 0; controller < mControllers.size(); controller++) { |
| 529 | final StateController sc = mControllers.get(controller); |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 530 | sc.maybeStartTrackingJobLocked(job, null); |
| 531 | } |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 532 | } |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 533 | }); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 534 | // GO GO GO! |
| 535 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
| 536 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 537 | } |
| 538 | } |
| 539 | |
| 540 | /** |
| 541 | * Called when we have a job status object that we need to insert in our |
| 542 | * {@link com.android.server.job.JobStore}, and make sure all the relevant controllers know |
| 543 | * about. |
| 544 | */ |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 545 | private void startTrackingJob(JobStatus jobStatus, JobStatus lastJob) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 546 | synchronized (mLock) { |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 547 | final boolean update = mJobs.add(jobStatus); |
| 548 | if (mReadyToRock) { |
| 549 | for (int i = 0; i < mControllers.size(); i++) { |
| 550 | StateController controller = mControllers.get(i); |
| 551 | if (update) { |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame^] | 552 | controller.maybeStopTrackingJobLocked(jobStatus, null, true); |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 553 | } |
| 554 | controller.maybeStartTrackingJobLocked(jobStatus, lastJob); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 555 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 556 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 557 | } |
| 558 | } |
| 559 | |
| 560 | /** |
| 561 | * Called when we want to remove a JobStatus object that we've finished executing. Returns the |
| 562 | * object removed. |
| 563 | */ |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame^] | 564 | private boolean stopTrackingJob(JobStatus jobStatus, JobStatus incomingJob, |
| 565 | boolean writeBack) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 566 | synchronized (mLock) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 567 | // Remove from store as well as controllers. |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 568 | final boolean removed = mJobs.remove(jobStatus, writeBack); |
| 569 | if (removed && mReadyToRock) { |
| 570 | for (int i=0; i<mControllers.size(); i++) { |
| 571 | StateController controller = mControllers.get(i); |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame^] | 572 | controller.maybeStopTrackingJobLocked(jobStatus, incomingJob, false); |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 573 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 574 | } |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 575 | return removed; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 576 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 577 | } |
| 578 | |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 579 | private boolean stopJobOnServiceContextLocked(JobStatus job, int reason) { |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 580 | for (int i=0; i<mActiveServices.size(); i++) { |
| 581 | JobServiceContext jsc = mActiveServices.get(i); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 582 | final JobStatus executing = jsc.getRunningJob(); |
| 583 | if (executing != null && executing.matches(job.getUid(), job.getJobId())) { |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 584 | jsc.cancelExecutingJob(reason); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 585 | return true; |
| 586 | } |
| 587 | } |
| 588 | return false; |
| 589 | } |
| 590 | |
| 591 | /** |
| 592 | * @param job JobStatus we are querying against. |
| 593 | * @return Whether or not the job represented by the status object is currently being run or |
| 594 | * is pending. |
| 595 | */ |
| 596 | private boolean isCurrentlyActiveLocked(JobStatus job) { |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 597 | for (int i=0; i<mActiveServices.size(); i++) { |
| 598 | JobServiceContext serviceContext = mActiveServices.get(i); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 599 | final JobStatus running = serviceContext.getRunningJob(); |
| 600 | if (running != null && running.matches(job.getUid(), job.getJobId())) { |
| 601 | return true; |
| 602 | } |
| 603 | } |
| 604 | return false; |
| 605 | } |
| 606 | |
| 607 | /** |
Matthew Williams | 1bde39a | 2015-10-07 14:29:30 -0700 | [diff] [blame] | 608 | * Reschedules the given job based on the job's backoff policy. It doesn't make sense to |
| 609 | * specify an override deadline on a failed job (the failed job will run even though it's not |
| 610 | * ready), so we reschedule it with {@link JobStatus#NO_LATEST_RUNTIME}, but specify that any |
| 611 | * ready job with {@link JobStatus#numFailures} > 0 will be executed. |
| 612 | * |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 613 | * @param failureToReschedule Provided job status that we will reschedule. |
| 614 | * @return A newly instantiated JobStatus with the same constraints as the last job except |
| 615 | * with adjusted timing constraints. |
Matthew Williams | 1bde39a | 2015-10-07 14:29:30 -0700 | [diff] [blame] | 616 | * |
| 617 | * @see JobHandler#maybeQueueReadyJobsForExecutionLockedH |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 618 | */ |
| 619 | private JobStatus getRescheduleJobForFailure(JobStatus failureToReschedule) { |
| 620 | final long elapsedNowMillis = SystemClock.elapsedRealtime(); |
| 621 | final JobInfo job = failureToReschedule.getJob(); |
| 622 | |
| 623 | final long initialBackoffMillis = job.getInitialBackoffMillis(); |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 624 | final int backoffAttempts = failureToReschedule.getNumFailures() + 1; |
| 625 | long delayMillis; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 626 | |
| 627 | switch (job.getBackoffPolicy()) { |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 628 | case JobInfo.BACKOFF_POLICY_LINEAR: |
| 629 | delayMillis = initialBackoffMillis * backoffAttempts; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 630 | break; |
| 631 | default: |
| 632 | if (DEBUG) { |
| 633 | Slog.v(TAG, "Unrecognised back-off policy, defaulting to exponential."); |
| 634 | } |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 635 | case JobInfo.BACKOFF_POLICY_EXPONENTIAL: |
| 636 | delayMillis = |
| 637 | (long) Math.scalb(initialBackoffMillis, backoffAttempts - 1); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 638 | break; |
| 639 | } |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 640 | delayMillis = |
| 641 | Math.min(delayMillis, JobInfo.MAX_BACKOFF_DELAY_MILLIS); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 642 | JobStatus newJob = new JobStatus(failureToReschedule, elapsedNowMillis + delayMillis, |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 643 | JobStatus.NO_LATEST_RUNTIME, backoffAttempts); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 644 | for (int ic=0; ic<mControllers.size(); ic++) { |
| 645 | StateController controller = mControllers.get(ic); |
| 646 | controller.rescheduleForFailure(newJob, failureToReschedule); |
| 647 | } |
| 648 | return newJob; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 649 | } |
| 650 | |
| 651 | /** |
Matthew Williams | 1bde39a | 2015-10-07 14:29:30 -0700 | [diff] [blame] | 652 | * Called after a periodic has executed so we can reschedule it. We take the last execution |
| 653 | * time of the job to be the time of completion (i.e. the time at which this function is |
| 654 | * called). |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 655 | * This could be inaccurate b/c the job can run for as long as |
| 656 | * {@link com.android.server.job.JobServiceContext#EXECUTING_TIMESLICE_MILLIS}, but will lead |
| 657 | * to underscheduling at least, rather than if we had taken the last execution time to be the |
| 658 | * start of the execution. |
| 659 | * @return A new job representing the execution criteria for this instantiation of the |
| 660 | * recurring job. |
| 661 | */ |
| 662 | private JobStatus getRescheduleJobForPeriodic(JobStatus periodicToReschedule) { |
| 663 | final long elapsedNow = SystemClock.elapsedRealtime(); |
| 664 | // Compute how much of the period is remaining. |
Matthew Williams | 1bde39a | 2015-10-07 14:29:30 -0700 | [diff] [blame] | 665 | long runEarly = 0L; |
| 666 | |
| 667 | // If this periodic was rescheduled it won't have a deadline. |
| 668 | if (periodicToReschedule.hasDeadlineConstraint()) { |
| 669 | runEarly = Math.max(periodicToReschedule.getLatestRunTimeElapsed() - elapsedNow, 0L); |
| 670 | } |
Shreyas Basarge | 89ee618 | 2015-12-17 15:16:36 +0000 | [diff] [blame] | 671 | long flex = periodicToReschedule.getJob().getFlexMillis(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 672 | long period = periodicToReschedule.getJob().getIntervalMillis(); |
Shreyas Basarge | 89ee618 | 2015-12-17 15:16:36 +0000 | [diff] [blame] | 673 | long newLatestRuntimeElapsed = elapsedNow + runEarly + period; |
| 674 | long newEarliestRunTimeElapsed = newLatestRuntimeElapsed - flex; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 675 | |
| 676 | if (DEBUG) { |
| 677 | Slog.v(TAG, "Rescheduling executed periodic. New execution window [" + |
| 678 | newEarliestRunTimeElapsed/1000 + ", " + newLatestRuntimeElapsed/1000 + "]s"); |
| 679 | } |
| 680 | return new JobStatus(periodicToReschedule, newEarliestRunTimeElapsed, |
| 681 | newLatestRuntimeElapsed, 0 /* backoffAttempt */); |
| 682 | } |
| 683 | |
| 684 | // JobCompletedListener implementations. |
| 685 | |
| 686 | /** |
| 687 | * A job just finished executing. We fetch the |
| 688 | * {@link com.android.server.job.controllers.JobStatus} from the store and depending on |
| 689 | * whether we want to reschedule we readd it to the controllers. |
| 690 | * @param jobStatus Completed job. |
| 691 | * @param needsReschedule Whether the implementing class should reschedule this job. |
| 692 | */ |
| 693 | @Override |
| 694 | public void onJobCompleted(JobStatus jobStatus, boolean needsReschedule) { |
| 695 | if (DEBUG) { |
| 696 | Slog.d(TAG, "Completed " + jobStatus + ", reschedule=" + needsReschedule); |
| 697 | } |
Shreyas Basarge | 73f1025 | 2016-02-11 17:06:13 +0000 | [diff] [blame] | 698 | // Do not write back immediately if this is a periodic job. The job may get lost if system |
| 699 | // shuts down before it is added back. |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame^] | 700 | if (!stopTrackingJob(jobStatus, null, !jobStatus.getJob().isPeriodic())) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 701 | if (DEBUG) { |
Matthew Williams | ee410da | 2014-07-25 11:30:40 -0700 | [diff] [blame] | 702 | 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] | 703 | } |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 704 | // We still want to check for jobs to execute, because this job may have |
| 705 | // scheduled a new job under the same job id, and now we can run it. |
| 706 | mHandler.obtainMessage(MSG_CHECK_JOB_GREEDY).sendToTarget(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 707 | return; |
| 708 | } |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 709 | // Note: there is a small window of time in here where, when rescheduling a job, |
| 710 | // we will stop monitoring its content providers. This should be fixed by stopping |
| 711 | // the old job after scheduling the new one, but since we have no lock held here |
| 712 | // 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] | 713 | if (needsReschedule) { |
| 714 | JobStatus rescheduled = getRescheduleJobForFailure(jobStatus); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 715 | startTrackingJob(rescheduled, jobStatus); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 716 | } else if (jobStatus.getJob().isPeriodic()) { |
| 717 | JobStatus rescheduledPeriodic = getRescheduleJobForPeriodic(jobStatus); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 718 | startTrackingJob(rescheduledPeriodic, jobStatus); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 719 | } |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 720 | reportActive(); |
| 721 | mHandler.obtainMessage(MSG_CHECK_JOB_GREEDY).sendToTarget(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 722 | } |
| 723 | |
| 724 | // StateChangedListener implementations. |
| 725 | |
| 726 | /** |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 727 | * Posts a message to the {@link com.android.server.job.JobSchedulerService.JobHandler} that |
| 728 | * some controller's state has changed, so as to run through the list of jobs and start/stop |
| 729 | * any that are eligible. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 730 | */ |
| 731 | @Override |
| 732 | public void onControllerStateChanged() { |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 733 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | @Override |
| 737 | public void onRunJobNow(JobStatus jobStatus) { |
| 738 | mHandler.obtainMessage(MSG_JOB_EXPIRED, jobStatus).sendToTarget(); |
| 739 | } |
| 740 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 741 | private class JobHandler extends Handler { |
| 742 | |
| 743 | public JobHandler(Looper looper) { |
| 744 | super(looper); |
| 745 | } |
| 746 | |
| 747 | @Override |
| 748 | public void handleMessage(Message message) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 749 | synchronized (mLock) { |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 750 | if (!mReadyToRock) { |
| 751 | return; |
| 752 | } |
| 753 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 754 | switch (message.what) { |
| 755 | case MSG_JOB_EXPIRED: |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 756 | synchronized (mLock) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 757 | JobStatus runNow = (JobStatus) message.obj; |
Matthew Williams | bafeeb9 | 2014-08-08 11:51:06 -0700 | [diff] [blame] | 758 | // runNow can be null, which is a controller's way of indicating that its |
| 759 | // state is such that all ready jobs should be run immediately. |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 760 | if (runNow != null && !mPendingJobs.contains(runNow) |
| 761 | && mJobs.containsJob(runNow)) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 762 | mPendingJobs.add(runNow); |
| 763 | } |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 764 | queueReadyJobsForExecutionLockedH(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 765 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 766 | break; |
| 767 | case MSG_CHECK_JOB: |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 768 | synchronized (mLock) { |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 769 | if (mReportedActive) { |
| 770 | // if jobs are currently being run, queue all ready jobs for execution. |
| 771 | queueReadyJobsForExecutionLockedH(); |
| 772 | } else { |
| 773 | // Check the list of jobs and run some of them if we feel inclined. |
| 774 | maybeQueueReadyJobsForExecutionLockedH(); |
| 775 | } |
| 776 | } |
| 777 | break; |
| 778 | case MSG_CHECK_JOB_GREEDY: |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 779 | synchronized (mLock) { |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 780 | queueReadyJobsForExecutionLockedH(); |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 781 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 782 | break; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 783 | case MSG_STOP_JOB: |
Dianne Hackborn | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame^] | 784 | cancelJobImpl((JobStatus)message.obj, null); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 785 | break; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 786 | } |
| 787 | maybeRunPendingJobsH(); |
| 788 | // Don't remove JOB_EXPIRED in case one came along while processing the queue. |
| 789 | removeMessages(MSG_CHECK_JOB); |
| 790 | } |
| 791 | |
| 792 | /** |
| 793 | * Run through list of jobs and execute all possible - at least one is expired so we do |
| 794 | * as many as we can. |
| 795 | */ |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 796 | private void queueReadyJobsForExecutionLockedH() { |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 797 | if (DEBUG) { |
| 798 | Slog.d(TAG, "queuing all ready jobs for execution:"); |
| 799 | } |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 800 | mPendingJobs.clear(); |
| 801 | mJobs.forEachJob(mReadyQueueFunctor); |
| 802 | mReadyQueueFunctor.postProcess(); |
| 803 | |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 804 | if (DEBUG) { |
| 805 | final int queuedJobs = mPendingJobs.size(); |
| 806 | if (queuedJobs == 0) { |
| 807 | Slog.d(TAG, "No jobs pending."); |
| 808 | } else { |
| 809 | Slog.d(TAG, queuedJobs + " jobs queued."); |
Matthew Williams | 75fc525 | 2014-09-02 16:17:53 -0700 | [diff] [blame] | 810 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 811 | } |
| 812 | } |
| 813 | |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 814 | class ReadyJobQueueFunctor implements JobStatusFunctor { |
| 815 | ArrayList<JobStatus> newReadyJobs; |
| 816 | |
| 817 | @Override |
| 818 | public void process(JobStatus job) { |
| 819 | if (isReadyToBeExecutedLocked(job)) { |
| 820 | if (DEBUG) { |
| 821 | Slog.d(TAG, " queued " + job.toShortString()); |
| 822 | } |
| 823 | if (newReadyJobs == null) { |
| 824 | newReadyJobs = new ArrayList<JobStatus>(); |
| 825 | } |
| 826 | newReadyJobs.add(job); |
| 827 | } else if (areJobConstraintsNotSatisfiedLocked(job)) { |
| 828 | stopJobOnServiceContextLocked(job, |
| 829 | JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED); |
| 830 | } |
| 831 | } |
| 832 | |
| 833 | public void postProcess() { |
| 834 | if (newReadyJobs != null) { |
| 835 | mPendingJobs.addAll(newReadyJobs); |
| 836 | } |
| 837 | newReadyJobs = null; |
| 838 | } |
| 839 | } |
| 840 | private final ReadyJobQueueFunctor mReadyQueueFunctor = new ReadyJobQueueFunctor(); |
| 841 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 842 | /** |
| 843 | * The state of at least one job has changed. Here is where we could enforce various |
| 844 | * policies on when we want to execute jobs. |
| 845 | * Right now the policy is such: |
| 846 | * If >1 of the ready jobs is idle mode we send all of them off |
| 847 | * if more than 2 network connectivity jobs are ready we send them all off. |
| 848 | * If more than 4 jobs total are ready we send them all off. |
| 849 | * TODO: It would be nice to consolidate these sort of high-level policies somewhere. |
| 850 | */ |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 851 | class MaybeReadyJobQueueFunctor implements JobStatusFunctor { |
| 852 | int chargingCount; |
| 853 | int idleCount; |
| 854 | int backoffCount; |
| 855 | int connectivityCount; |
| 856 | int contentCount; |
| 857 | List<JobStatus> runnableJobs; |
| 858 | |
| 859 | public MaybeReadyJobQueueFunctor() { |
| 860 | reset(); |
| 861 | } |
| 862 | |
| 863 | // Functor method invoked for each job via JobStore.forEachJob() |
| 864 | @Override |
| 865 | public void process(JobStatus job) { |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 866 | if (isReadyToBeExecutedLocked(job)) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 867 | try { |
| 868 | if (ActivityManagerNative.getDefault().getAppStartMode(job.getUid(), |
| 869 | job.getJob().getService().getPackageName()) |
| 870 | == ActivityManager.APP_START_MODE_DISABLED) { |
| 871 | Slog.w(TAG, "Aborting job " + job.getUid() + ":" |
| 872 | + job.getJob().toString() + " -- package not allowed to start"); |
| 873 | mHandler.obtainMessage(MSG_STOP_JOB, job).sendToTarget(); |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 874 | return; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 875 | } |
| 876 | } catch (RemoteException e) { |
| 877 | } |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 878 | if (job.getNumFailures() > 0) { |
| 879 | backoffCount++; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 880 | } |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 881 | if (job.hasIdleConstraint()) { |
| 882 | idleCount++; |
| 883 | } |
| 884 | if (job.hasConnectivityConstraint() || job.hasUnmeteredConstraint()) { |
| 885 | connectivityCount++; |
| 886 | } |
| 887 | if (job.hasChargingConstraint()) { |
| 888 | chargingCount++; |
| 889 | } |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 890 | if (job.hasContentTriggerConstraint()) { |
| 891 | contentCount++; |
| 892 | } |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 893 | if (runnableJobs == null) { |
| 894 | runnableJobs = new ArrayList<>(); |
| 895 | } |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 896 | runnableJobs.add(job); |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 897 | } else if (areJobConstraintsNotSatisfiedLocked(job)) { |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 898 | stopJobOnServiceContextLocked(job, |
| 899 | JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 900 | } |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 901 | } |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 902 | |
| 903 | public void postProcess() { |
| 904 | if (backoffCount > 0 || |
| 905 | idleCount >= MIN_IDLE_COUNT || |
| 906 | connectivityCount >= MIN_CONNECTIVITY_COUNT || |
| 907 | chargingCount >= MIN_CHARGING_COUNT || |
| 908 | contentCount >= MIN_CONTENT_COUNT || |
| 909 | (runnableJobs != null && runnableJobs.size() >= MIN_READY_JOBS_COUNT)) { |
| 910 | if (DEBUG) { |
| 911 | Slog.d(TAG, "maybeQueueReadyJobsForExecutionLockedH: Running jobs."); |
| 912 | } |
| 913 | mPendingJobs.addAll(runnableJobs); |
| 914 | } else { |
| 915 | if (DEBUG) { |
| 916 | Slog.d(TAG, "maybeQueueReadyJobsForExecutionLockedH: Not running anything."); |
| 917 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 918 | } |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 919 | |
| 920 | // Be ready for next time |
| 921 | reset(); |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 922 | } |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 923 | |
| 924 | private void reset() { |
| 925 | chargingCount = 0; |
| 926 | idleCount = 0; |
| 927 | backoffCount = 0; |
| 928 | connectivityCount = 0; |
| 929 | contentCount = 0; |
| 930 | runnableJobs = null; |
| 931 | } |
| 932 | } |
| 933 | private final MaybeReadyJobQueueFunctor mMaybeQueueFunctor = new MaybeReadyJobQueueFunctor(); |
| 934 | |
| 935 | private void maybeQueueReadyJobsForExecutionLockedH() { |
| 936 | if (DEBUG) Slog.d(TAG, "Maybe queuing ready jobs..."); |
| 937 | |
| 938 | mPendingJobs.clear(); |
| 939 | mJobs.forEachJob(mMaybeQueueFunctor); |
| 940 | mMaybeQueueFunctor.postProcess(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 941 | } |
| 942 | |
| 943 | /** |
| 944 | * Criteria for moving a job into the pending queue: |
| 945 | * - It's ready. |
| 946 | * - It's not pending. |
| 947 | * - It's not already running on a JSC. |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 948 | * - The user that requested the job is running. |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 949 | * - The component is enabled and runnable. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 950 | */ |
| 951 | private boolean isReadyToBeExecutedLocked(JobStatus job) { |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 952 | final boolean jobReady = job.isReady(); |
| 953 | final boolean jobPending = mPendingJobs.contains(job); |
| 954 | final boolean jobActive = isCurrentlyActiveLocked(job); |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 955 | |
| 956 | final int userId = job.getUserId(); |
| 957 | final boolean userStarted = ArrayUtils.contains(mStartedUsers, userId); |
| 958 | final boolean componentPresent; |
| 959 | try { |
| 960 | componentPresent = (AppGlobals.getPackageManager().getServiceInfo( |
| 961 | job.getServiceComponent(), PackageManager.MATCH_DEBUG_TRIAGED_MISSING, |
| 962 | userId) != null); |
| 963 | } catch (RemoteException e) { |
| 964 | throw e.rethrowAsRuntimeException(); |
| 965 | } |
| 966 | |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 967 | if (DEBUG) { |
| 968 | Slog.v(TAG, "isReadyToBeExecutedLocked: " + job.toShortString() |
| 969 | + " ready=" + jobReady + " pending=" + jobPending |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 970 | + " active=" + jobActive + " userStarted=" + userStarted |
| 971 | + " componentPresent=" + componentPresent); |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 972 | } |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 973 | return userStarted && componentPresent && jobReady && !jobPending && !jobActive; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 974 | } |
| 975 | |
| 976 | /** |
| 977 | * Criteria for cancelling an active job: |
| 978 | * - It's not ready |
| 979 | * - It's running on a JSC. |
| 980 | */ |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 981 | private boolean areJobConstraintsNotSatisfiedLocked(JobStatus job) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 982 | return !job.isReady() && isCurrentlyActiveLocked(job); |
| 983 | } |
| 984 | |
| 985 | /** |
| 986 | * Reconcile jobs in the pending queue against available execution contexts. |
| 987 | * A controller can force a job into the pending queue even if it's already running, but |
| 988 | * here is where we decide whether to actually execute it. |
| 989 | */ |
| 990 | private void maybeRunPendingJobsH() { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 991 | synchronized (mLock) { |
Matthew Williams | 75fc525 | 2014-09-02 16:17:53 -0700 | [diff] [blame] | 992 | if (DEBUG) { |
| 993 | Slog.d(TAG, "pending queue: " + mPendingJobs.size() + " jobs."); |
| 994 | } |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 995 | assignJobsToContextsLocked(); |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 996 | reportActive(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 997 | } |
| 998 | } |
| 999 | } |
| 1000 | |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1001 | private int evaluateJobPriorityLocked(JobStatus job) { |
| 1002 | int priority = job.getPriority(); |
| 1003 | if (priority >= JobInfo.PRIORITY_FOREGROUND_APP) { |
| 1004 | return priority; |
| 1005 | } |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1006 | int override = mUidPriorityOverride.get(job.getSourceUid(), 0); |
| 1007 | if (override != 0) { |
| 1008 | return override; |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1009 | } |
| 1010 | return priority; |
| 1011 | } |
| 1012 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1013 | /** |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1014 | * Takes jobs from pending queue and runs them on available contexts. |
| 1015 | * If no contexts are available, preempts lower priority jobs to |
| 1016 | * run higher priority ones. |
| 1017 | * Lock on mJobs before calling this function. |
| 1018 | */ |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 1019 | private void assignJobsToContextsLocked() { |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1020 | if (DEBUG) { |
| 1021 | Slog.d(TAG, printPendingQueue()); |
| 1022 | } |
| 1023 | |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1024 | int memLevel; |
| 1025 | try { |
| 1026 | memLevel = ActivityManagerNative.getDefault().getMemoryTrimLevel(); |
| 1027 | } catch (RemoteException e) { |
| 1028 | memLevel = ProcessStats.ADJ_MEM_FACTOR_NORMAL; |
| 1029 | } |
| 1030 | switch (memLevel) { |
| 1031 | case ProcessStats.ADJ_MEM_FACTOR_MODERATE: |
| 1032 | mMaxActiveJobs = ((MAX_JOB_CONTEXTS_COUNT - 2) * 2) / 3; |
| 1033 | break; |
| 1034 | case ProcessStats.ADJ_MEM_FACTOR_LOW: |
| 1035 | mMaxActiveJobs = (MAX_JOB_CONTEXTS_COUNT - 2) / 3; |
| 1036 | break; |
| 1037 | case ProcessStats.ADJ_MEM_FACTOR_CRITICAL: |
| 1038 | mMaxActiveJobs = 1; |
| 1039 | break; |
| 1040 | default: |
| 1041 | mMaxActiveJobs = MAX_JOB_CONTEXTS_COUNT - 2; |
| 1042 | break; |
| 1043 | } |
| 1044 | |
| 1045 | JobStatus[] contextIdToJobMap = mTmpAssignContextIdToJobMap; |
| 1046 | boolean[] act = mTmpAssignAct; |
| 1047 | int[] preferredUidForContext = mTmpAssignPreferredUidForContext; |
| 1048 | int numActive = 0; |
| 1049 | for (int i=0; i<MAX_JOB_CONTEXTS_COUNT; i++) { |
| 1050 | final JobServiceContext js = mActiveServices.get(i); |
| 1051 | if ((contextIdToJobMap[i] = js.getRunningJob()) != null) { |
| 1052 | numActive++; |
| 1053 | } |
| 1054 | act[i] = false; |
| 1055 | preferredUidForContext[i] = js.getPreferredUid(); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1056 | } |
| 1057 | if (DEBUG) { |
| 1058 | Slog.d(TAG, printContextIdToJobMap(contextIdToJobMap, "running jobs initial")); |
| 1059 | } |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1060 | for (int i=0; i<mPendingJobs.size(); i++) { |
| 1061 | JobStatus nextPending = mPendingJobs.get(i); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1062 | |
| 1063 | // If job is already running, go to next job. |
| 1064 | int jobRunningContext = findJobContextIdFromMap(nextPending, contextIdToJobMap); |
| 1065 | if (jobRunningContext != -1) { |
| 1066 | continue; |
| 1067 | } |
| 1068 | |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1069 | final int priority = evaluateJobPriorityLocked(nextPending); |
| 1070 | nextPending.lastEvaluatedPriority = priority; |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1071 | |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1072 | // Find a context for nextPending. The context should be available OR |
| 1073 | // it should have lowest priority among all running jobs |
| 1074 | // (sharing the same Uid as nextPending) |
| 1075 | int minPriority = Integer.MAX_VALUE; |
| 1076 | int minPriorityContextId = -1; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1077 | for (int j=0; j<MAX_JOB_CONTEXTS_COUNT; j++) { |
| 1078 | JobStatus job = contextIdToJobMap[j]; |
| 1079 | int preferredUid = preferredUidForContext[j]; |
Shreyas Basarge | 347c278 | 2016-01-15 18:24:36 +0000 | [diff] [blame] | 1080 | if (job == null) { |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1081 | if ((numActive < mMaxActiveJobs || priority >= JobInfo.PRIORITY_TOP_APP) && |
| 1082 | (preferredUid == nextPending.getUid() || |
| 1083 | preferredUid == JobServiceContext.NO_PREFERRED_UID)) { |
| 1084 | // This slot is free, and we haven't yet hit the limit on |
| 1085 | // concurrent jobs... we can just throw the job in to here. |
| 1086 | minPriorityContextId = j; |
| 1087 | numActive++; |
| 1088 | break; |
| 1089 | } |
Shreyas Basarge | 347c278 | 2016-01-15 18:24:36 +0000 | [diff] [blame] | 1090 | // No job on this context, but nextPending can't run here because |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1091 | // the context has a preferred Uid or we have reached the limit on |
| 1092 | // concurrent jobs. |
Shreyas Basarge | 347c278 | 2016-01-15 18:24:36 +0000 | [diff] [blame] | 1093 | continue; |
| 1094 | } |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1095 | if (job.getUid() != nextPending.getUid()) { |
| 1096 | continue; |
| 1097 | } |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1098 | if (evaluateJobPriorityLocked(job) >= nextPending.lastEvaluatedPriority) { |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1099 | continue; |
| 1100 | } |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1101 | if (minPriority > nextPending.lastEvaluatedPriority) { |
| 1102 | minPriority = nextPending.lastEvaluatedPriority; |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1103 | minPriorityContextId = j; |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1104 | } |
| 1105 | } |
| 1106 | if (minPriorityContextId != -1) { |
| 1107 | contextIdToJobMap[minPriorityContextId] = nextPending; |
| 1108 | act[minPriorityContextId] = true; |
| 1109 | } |
| 1110 | } |
| 1111 | if (DEBUG) { |
| 1112 | Slog.d(TAG, printContextIdToJobMap(contextIdToJobMap, "running jobs final")); |
| 1113 | } |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1114 | for (int i=0; i<MAX_JOB_CONTEXTS_COUNT; i++) { |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1115 | boolean preservePreferredUid = false; |
| 1116 | if (act[i]) { |
| 1117 | JobStatus js = mActiveServices.get(i).getRunningJob(); |
| 1118 | if (js != null) { |
| 1119 | if (DEBUG) { |
| 1120 | Slog.d(TAG, "preempting job: " + mActiveServices.get(i).getRunningJob()); |
| 1121 | } |
| 1122 | // preferredUid will be set to uid of currently running job. |
| 1123 | mActiveServices.get(i).preemptExecutingJob(); |
| 1124 | preservePreferredUid = true; |
| 1125 | } else { |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1126 | final JobStatus pendingJob = contextIdToJobMap[i]; |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1127 | if (DEBUG) { |
| 1128 | Slog.d(TAG, "About to run job on context " |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1129 | + String.valueOf(i) + ", job: " + pendingJob); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1130 | } |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 1131 | for (int ic=0; ic<mControllers.size(); ic++) { |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1132 | mControllers.get(ic).prepareForExecutionLocked(pendingJob); |
Dianne Hackborn | 1a30bd9 | 2016-01-11 11:05:00 -0800 | [diff] [blame] | 1133 | } |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1134 | if (!mActiveServices.get(i).executeRunnableJob(pendingJob)) { |
| 1135 | Slog.d(TAG, "Error executing " + pendingJob); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1136 | } |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1137 | mPendingJobs.remove(pendingJob); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1138 | } |
| 1139 | } |
| 1140 | if (!preservePreferredUid) { |
| 1141 | mActiveServices.get(i).clearPreferredUid(); |
| 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | int findJobContextIdFromMap(JobStatus jobStatus, JobStatus[] map) { |
| 1147 | for (int i=0; i<map.length; i++) { |
| 1148 | if (map[i] != null && map[i].matches(jobStatus.getUid(), jobStatus.getJobId())) { |
| 1149 | return i; |
| 1150 | } |
| 1151 | } |
| 1152 | return -1; |
| 1153 | } |
| 1154 | |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 1155 | final class LocalService implements JobSchedulerInternal { |
| 1156 | |
| 1157 | /** |
| 1158 | * Returns a list of all pending jobs. A running job is not considered pending. Periodic |
| 1159 | * jobs are always considered pending. |
| 1160 | */ |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 1161 | @Override |
Shreyas Basarge | cbf5ae9 | 2016-03-08 16:13:06 +0000 | [diff] [blame] | 1162 | public List<JobInfo> getSystemScheduledPendingJobs() { |
| 1163 | synchronized (mLock) { |
| 1164 | final List<JobInfo> pendingJobs = new ArrayList<JobInfo>(); |
| 1165 | mJobs.forEachJob(Process.SYSTEM_UID, new JobStatusFunctor() { |
| 1166 | @Override |
| 1167 | public void process(JobStatus job) { |
| 1168 | if (job.getJob().isPeriodic() || !isCurrentlyActiveLocked(job)) { |
| 1169 | pendingJobs.add(job.getJob()); |
| 1170 | } |
| 1171 | } |
| 1172 | }); |
| 1173 | return pendingJobs; |
| 1174 | } |
| 1175 | } |
| 1176 | } |
| 1177 | |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1178 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1179 | * Binder stub trampoline implementation |
| 1180 | */ |
| 1181 | final class JobSchedulerStub extends IJobScheduler.Stub { |
| 1182 | /** Cache determination of whether a given app can persist jobs |
| 1183 | * key is uid of the calling app; value is undetermined/true/false |
| 1184 | */ |
| 1185 | private final SparseArray<Boolean> mPersistCache = new SparseArray<Boolean>(); |
| 1186 | |
| 1187 | // Enforce that only the app itself (or shared uid participant) can schedule a |
| 1188 | // job that runs one of the app's services, as well as verifying that the |
| 1189 | // named service properly requires the BIND_JOB_SERVICE permission |
| 1190 | private void enforceValidJobRequest(int uid, JobInfo job) { |
Christopher Tate | 5568f54 | 2014-06-18 13:53:31 -0700 | [diff] [blame] | 1191 | final IPackageManager pm = AppGlobals.getPackageManager(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1192 | final ComponentName service = job.getService(); |
| 1193 | try { |
Jeff Sharkey | c7bacab | 2016-02-09 15:56:11 -0700 | [diff] [blame] | 1194 | ServiceInfo si = pm.getServiceInfo(service, |
Jeff Sharkey | 8a372a0 | 2016-03-16 16:25:45 -0600 | [diff] [blame] | 1195 | PackageManager.MATCH_DIRECT_BOOT_AWARE |
| 1196 | | PackageManager.MATCH_DIRECT_BOOT_UNAWARE, |
Jeff Sharkey | 12c0da4 | 2016-02-25 17:10:50 -0700 | [diff] [blame] | 1197 | UserHandle.getUserId(uid)); |
Christopher Tate | 5568f54 | 2014-06-18 13:53:31 -0700 | [diff] [blame] | 1198 | if (si == null) { |
| 1199 | throw new IllegalArgumentException("No such service " + service); |
| 1200 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1201 | if (si.applicationInfo.uid != uid) { |
| 1202 | throw new IllegalArgumentException("uid " + uid + |
| 1203 | " cannot schedule job in " + service.getPackageName()); |
| 1204 | } |
| 1205 | if (!JobService.PERMISSION_BIND.equals(si.permission)) { |
| 1206 | throw new IllegalArgumentException("Scheduled service " + service |
| 1207 | + " does not require android.permission.BIND_JOB_SERVICE permission"); |
| 1208 | } |
Christopher Tate | 5568f54 | 2014-06-18 13:53:31 -0700 | [diff] [blame] | 1209 | } catch (RemoteException e) { |
| 1210 | // Can't happen; the Package Manager is in this same process |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1211 | } |
| 1212 | } |
| 1213 | |
| 1214 | private boolean canPersistJobs(int pid, int uid) { |
| 1215 | // If we get this far we're good to go; all we need to do now is check |
| 1216 | // whether the app is allowed to persist its scheduled work. |
| 1217 | final boolean canPersist; |
| 1218 | synchronized (mPersistCache) { |
| 1219 | Boolean cached = mPersistCache.get(uid); |
| 1220 | if (cached != null) { |
| 1221 | canPersist = cached.booleanValue(); |
| 1222 | } else { |
| 1223 | // Persisting jobs is tantamount to running at boot, so we permit |
| 1224 | // it when the app has declared that it uses the RECEIVE_BOOT_COMPLETED |
| 1225 | // permission |
| 1226 | int result = getContext().checkPermission( |
| 1227 | android.Manifest.permission.RECEIVE_BOOT_COMPLETED, pid, uid); |
| 1228 | canPersist = (result == PackageManager.PERMISSION_GRANTED); |
| 1229 | mPersistCache.put(uid, canPersist); |
| 1230 | } |
| 1231 | } |
| 1232 | return canPersist; |
| 1233 | } |
| 1234 | |
| 1235 | // IJobScheduler implementation |
| 1236 | @Override |
| 1237 | public int schedule(JobInfo job) throws RemoteException { |
| 1238 | if (DEBUG) { |
Matthew Williams | ee410da | 2014-07-25 11:30:40 -0700 | [diff] [blame] | 1239 | Slog.d(TAG, "Scheduling job: " + job.toString()); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1240 | } |
| 1241 | final int pid = Binder.getCallingPid(); |
| 1242 | final int uid = Binder.getCallingUid(); |
| 1243 | |
| 1244 | enforceValidJobRequest(uid, job); |
Matthew Williams | 900c67f | 2014-07-09 12:46:53 -0700 | [diff] [blame] | 1245 | if (job.isPersisted()) { |
| 1246 | if (!canPersistJobs(pid, uid)) { |
| 1247 | throw new IllegalArgumentException("Error: requested job be persisted without" |
| 1248 | + " holding RECEIVE_BOOT_COMPLETED permission."); |
| 1249 | } |
| 1250 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1251 | |
| 1252 | long ident = Binder.clearCallingIdentity(); |
| 1253 | try { |
Matthew Williams | 900c67f | 2014-07-09 12:46:53 -0700 | [diff] [blame] | 1254 | return JobSchedulerService.this.schedule(job, uid); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1255 | } finally { |
| 1256 | Binder.restoreCallingIdentity(ident); |
| 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | @Override |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1261 | public int scheduleAsPackage(JobInfo job, String packageName, int userId, String tag) |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 1262 | throws RemoteException { |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1263 | final int callerUid = Binder.getCallingUid(); |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 1264 | if (DEBUG) { |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1265 | Slog.d(TAG, "Caller uid " + callerUid + " scheduling job: " + job.toString() |
| 1266 | + " on behalf of " + packageName); |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 1267 | } |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1268 | |
| 1269 | if (packageName == null) { |
| 1270 | throw new NullPointerException("Must specify a package for scheduleAsPackage()"); |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 1271 | } |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1272 | |
| 1273 | int mayScheduleForOthers = getContext().checkCallingOrSelfPermission( |
| 1274 | android.Manifest.permission.UPDATE_DEVICE_STATS); |
| 1275 | if (mayScheduleForOthers != PackageManager.PERMISSION_GRANTED) { |
| 1276 | throw new SecurityException("Caller uid " + callerUid |
| 1277 | + " not permitted to schedule jobs for other apps"); |
| 1278 | } |
| 1279 | |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 1280 | long ident = Binder.clearCallingIdentity(); |
| 1281 | try { |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1282 | return JobSchedulerService.this.scheduleAsPackage(job, callerUid, |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1283 | packageName, userId, tag); |
Shreyas Basarge | 968ac75 | 2016-01-11 23:09:26 +0000 | [diff] [blame] | 1284 | } finally { |
| 1285 | Binder.restoreCallingIdentity(ident); |
| 1286 | } |
| 1287 | } |
| 1288 | |
| 1289 | @Override |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1290 | public List<JobInfo> getAllPendingJobs() throws RemoteException { |
| 1291 | final int uid = Binder.getCallingUid(); |
| 1292 | |
| 1293 | long ident = Binder.clearCallingIdentity(); |
| 1294 | try { |
| 1295 | return JobSchedulerService.this.getPendingJobs(uid); |
| 1296 | } finally { |
| 1297 | Binder.restoreCallingIdentity(ident); |
| 1298 | } |
| 1299 | } |
| 1300 | |
| 1301 | @Override |
| 1302 | public void cancelAll() throws RemoteException { |
| 1303 | final int uid = Binder.getCallingUid(); |
| 1304 | |
| 1305 | long ident = Binder.clearCallingIdentity(); |
| 1306 | try { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1307 | JobSchedulerService.this.cancelJobsForUid(uid, true); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1308 | } finally { |
| 1309 | Binder.restoreCallingIdentity(ident); |
| 1310 | } |
| 1311 | } |
| 1312 | |
| 1313 | @Override |
| 1314 | public void cancel(int jobId) throws RemoteException { |
| 1315 | final int uid = Binder.getCallingUid(); |
| 1316 | |
| 1317 | long ident = Binder.clearCallingIdentity(); |
| 1318 | try { |
| 1319 | JobSchedulerService.this.cancelJob(uid, jobId); |
| 1320 | } finally { |
| 1321 | Binder.restoreCallingIdentity(ident); |
| 1322 | } |
| 1323 | } |
| 1324 | |
| 1325 | /** |
| 1326 | * "dumpsys" infrastructure |
| 1327 | */ |
| 1328 | @Override |
| 1329 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1330 | getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG); |
| 1331 | |
| 1332 | long identityToken = Binder.clearCallingIdentity(); |
| 1333 | try { |
| 1334 | JobSchedulerService.this.dumpInternal(pw); |
| 1335 | } finally { |
| 1336 | Binder.restoreCallingIdentity(identityToken); |
| 1337 | } |
| 1338 | } |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 1339 | |
| 1340 | @Override |
| 1341 | public void onShellCommand(FileDescriptor in, FileDescriptor out, FileDescriptor err, |
| 1342 | String[] args, ResultReceiver resultReceiver) throws RemoteException { |
| 1343 | (new JobSchedulerShellCommand(JobSchedulerService.this)).exec( |
| 1344 | this, in, out, err, args, resultReceiver); |
| 1345 | } |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1346 | }; |
| 1347 | |
Christopher Tate | 5d34605 | 2016-03-08 12:56:08 -0800 | [diff] [blame] | 1348 | // Shell command infrastructure: run the given job immediately |
| 1349 | int executeRunCommand(String pkgName, int userId, int jobId, boolean force) { |
| 1350 | if (DEBUG) { |
| 1351 | Slog.v(TAG, "executeRunCommand(): " + pkgName + "/" + userId |
| 1352 | + " " + jobId + " f=" + force); |
| 1353 | } |
| 1354 | |
| 1355 | try { |
| 1356 | final int uid = AppGlobals.getPackageManager().getPackageUid(pkgName, 0, userId); |
| 1357 | if (uid < 0) { |
| 1358 | return JobSchedulerShellCommand.CMD_ERR_NO_PACKAGE; |
| 1359 | } |
| 1360 | |
| 1361 | synchronized (mLock) { |
| 1362 | final JobStatus js = mJobs.getJobByUidAndJobId(uid, jobId); |
| 1363 | if (js == null) { |
| 1364 | return JobSchedulerShellCommand.CMD_ERR_NO_JOB; |
| 1365 | } |
| 1366 | |
| 1367 | js.overrideState = (force) ? JobStatus.OVERRIDE_FULL : JobStatus.OVERRIDE_SOFT; |
| 1368 | if (!js.isConstraintsSatisfied()) { |
| 1369 | js.overrideState = 0; |
| 1370 | return JobSchedulerShellCommand.CMD_ERR_CONSTRAINTS; |
| 1371 | } |
| 1372 | |
| 1373 | mHandler.obtainMessage(MSG_CHECK_JOB_GREEDY).sendToTarget(); |
| 1374 | } |
| 1375 | } catch (RemoteException e) { |
| 1376 | // can't happen |
| 1377 | } |
| 1378 | return 0; |
| 1379 | } |
| 1380 | |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1381 | private String printContextIdToJobMap(JobStatus[] map, String initial) { |
| 1382 | StringBuilder s = new StringBuilder(initial + ": "); |
| 1383 | for (int i=0; i<map.length; i++) { |
| 1384 | s.append("(") |
| 1385 | .append(map[i] == null? -1: map[i].getJobId()) |
| 1386 | .append(map[i] == null? -1: map[i].getUid()) |
| 1387 | .append(")" ); |
| 1388 | } |
| 1389 | return s.toString(); |
| 1390 | } |
| 1391 | |
| 1392 | private String printPendingQueue() { |
| 1393 | StringBuilder s = new StringBuilder("Pending queue: "); |
| 1394 | Iterator<JobStatus> it = mPendingJobs.iterator(); |
| 1395 | while (it.hasNext()) { |
| 1396 | JobStatus js = it.next(); |
| 1397 | s.append("(") |
| 1398 | .append(js.getJob().getId()) |
| 1399 | .append(", ") |
| 1400 | .append(js.getUid()) |
| 1401 | .append(") "); |
| 1402 | } |
| 1403 | return s.toString(); |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 1404 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1405 | |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1406 | void dumpInternal(final PrintWriter pw) { |
Christopher Tate | f973a7b | 2014-08-29 12:54:08 -0700 | [diff] [blame] | 1407 | final long now = SystemClock.elapsedRealtime(); |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 1408 | synchronized (mLock) { |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 1409 | pw.println("Started users: " + Arrays.toString(mStartedUsers)); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1410 | pw.println("Registered jobs:"); |
| 1411 | if (mJobs.size() > 0) { |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1412 | mJobs.forEachJob(new JobStatusFunctor() { |
| 1413 | private int index = 0; |
| 1414 | |
| 1415 | @Override |
| 1416 | public void process(JobStatus job) { |
| 1417 | pw.print(" Job #"); pw.print(index++); pw.print(": "); |
| 1418 | pw.println(job.toShortString()); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1419 | job.dump(pw, " ", true); |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1420 | pw.print(" Ready: "); |
| 1421 | pw.print(mHandler.isReadyToBeExecutedLocked(job)); |
| 1422 | pw.print(" (job="); |
| 1423 | pw.print(job.isReady()); |
| 1424 | pw.print(" pending="); |
| 1425 | pw.print(mPendingJobs.contains(job)); |
| 1426 | pw.print(" active="); |
| 1427 | pw.print(isCurrentlyActiveLocked(job)); |
| 1428 | pw.print(" user="); |
Jeff Sharkey | 822cbd1 | 2016-02-25 11:09:55 -0700 | [diff] [blame] | 1429 | pw.print(ArrayUtils.contains(mStartedUsers, job.getUserId())); |
Christopher Tate | 2f36fd6 | 2016-02-18 18:36:08 -0800 | [diff] [blame] | 1430 | pw.println(")"); |
| 1431 | } |
| 1432 | }); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1433 | } else { |
Christopher Tate | f973a7b | 2014-08-29 12:54:08 -0700 | [diff] [blame] | 1434 | pw.println(" None."); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1435 | } |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1436 | for (int i=0; i<mControllers.size(); i++) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1437 | pw.println(); |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 1438 | mControllers.get(i).dumpControllerStateLocked(pw); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1439 | } |
| 1440 | pw.println(); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1441 | pw.println("Uid priority overrides:"); |
| 1442 | for (int i=0; i< mUidPriorityOverride.size(); i++) { |
| 1443 | pw.print(" "); pw.print(UserHandle.formatUid(mUidPriorityOverride.keyAt(i))); |
| 1444 | pw.print(": "); pw.println(mUidPriorityOverride.valueAt(i)); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1445 | } |
| 1446 | pw.println(); |
| 1447 | pw.println("Pending queue:"); |
| 1448 | for (int i=0; i<mPendingJobs.size(); i++) { |
| 1449 | JobStatus job = mPendingJobs.get(i); |
| 1450 | pw.print(" Pending #"); pw.print(i); pw.print(": "); |
| 1451 | pw.println(job.toShortString()); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1452 | job.dump(pw, " ", false); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1453 | int priority = evaluateJobPriorityLocked(job); |
| 1454 | if (priority != JobInfo.PRIORITY_DEFAULT) { |
| 1455 | pw.print(" Evaluated priority: "); pw.println(priority); |
| 1456 | } |
| 1457 | pw.print(" Tag: "); pw.println(job.getTag()); |
| 1458 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1459 | pw.println(); |
| 1460 | pw.println("Active jobs:"); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1461 | for (int i=0; i<mActiveServices.size(); i++) { |
| 1462 | JobServiceContext jsc = mActiveServices.get(i); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1463 | pw.print(" Slot #"); pw.print(i); pw.print(": "); |
Shreyas Basarge | 5db0908 | 2016-01-07 13:38:29 +0000 | [diff] [blame] | 1464 | if (jsc.getRunningJob() == null) { |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1465 | pw.println("inactive"); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1466 | continue; |
| 1467 | } else { |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1468 | pw.println(jsc.getRunningJob().toShortString()); |
| 1469 | pw.print(" Running for: "); |
| 1470 | TimeUtils.formatDuration(now - jsc.getExecutionStartTimeElapsed(), pw); |
| 1471 | pw.print(", timeout at: "); |
| 1472 | TimeUtils.formatDuration(jsc.getTimeoutElapsed() - now, pw); |
| 1473 | pw.println(); |
| 1474 | jsc.getRunningJob().dump(pw, " ", false); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1475 | int priority = evaluateJobPriorityLocked(jsc.getRunningJob()); |
| 1476 | if (priority != JobInfo.PRIORITY_DEFAULT) { |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1477 | pw.print(" Evaluated priority: "); pw.println(priority); |
Dianne Hackborn | 1085ff6 | 2016-02-23 17:04:58 -0800 | [diff] [blame] | 1478 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1479 | } |
| 1480 | } |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1481 | pw.println(); |
| 1482 | pw.print("mReadyToRock="); pw.println(mReadyToRock); |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 1483 | pw.print("mReportedActive="); pw.println(mReportedActive); |
Dianne Hackborn | 970510b | 2016-02-24 16:56:42 -0800 | [diff] [blame] | 1484 | pw.print("mMaxActiveJobs="); pw.println(mMaxActiveJobs); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1485 | } |
| 1486 | pw.println(); |
| 1487 | } |
| 1488 | } |