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 | |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 19 | import android.app.ActivityManager; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 20 | import android.app.ActivityManagerNative; |
Christopher Tate | 5568f54 | 2014-06-18 13:53:31 -0700 | [diff] [blame] | 21 | import android.app.AppGlobals; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 22 | import android.app.IUidObserver; |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 23 | import android.app.job.IJobScheduler; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 24 | import android.app.job.JobInfo; |
| 25 | import android.app.job.JobScheduler; |
| 26 | import android.app.job.JobService; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 27 | import android.content.BroadcastReceiver; |
| 28 | import android.content.ComponentName; |
| 29 | import android.content.Context; |
| 30 | import android.content.Intent; |
| 31 | import android.content.IntentFilter; |
Christopher Tate | 5568f54 | 2014-06-18 13:53:31 -0700 | [diff] [blame] | 32 | import android.content.pm.IPackageManager; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 33 | import android.content.pm.PackageManager; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 34 | import android.content.pm.ServiceInfo; |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 35 | import android.os.BatteryStats; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 36 | import android.os.Binder; |
| 37 | import android.os.Handler; |
| 38 | import android.os.Looper; |
| 39 | import android.os.Message; |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 40 | import android.os.PowerManager; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 41 | import android.os.RemoteException; |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 42 | import android.os.ServiceManager; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 43 | import android.os.SystemClock; |
| 44 | import android.os.UserHandle; |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 45 | import android.util.ArraySet; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 46 | import android.util.Slog; |
| 47 | import android.util.SparseArray; |
| 48 | |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 49 | import com.android.internal.app.IBatteryStats; |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 50 | import com.android.internal.util.ArrayUtils; |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 51 | import com.android.server.DeviceIdleController; |
| 52 | import com.android.server.LocalServices; |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 53 | import com.android.server.job.controllers.AppIdleController; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 54 | import com.android.server.job.controllers.BatteryController; |
| 55 | import com.android.server.job.controllers.ConnectivityController; |
| 56 | import com.android.server.job.controllers.IdleController; |
| 57 | import com.android.server.job.controllers.JobStatus; |
| 58 | import com.android.server.job.controllers.StateController; |
| 59 | import com.android.server.job.controllers.TimeController; |
| 60 | |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 61 | import libcore.util.EmptyArray; |
| 62 | |
| 63 | import java.io.FileDescriptor; |
| 64 | import java.io.PrintWriter; |
| 65 | import java.util.ArrayList; |
| 66 | import java.util.Arrays; |
| 67 | import java.util.Iterator; |
| 68 | import java.util.List; |
| 69 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 70 | /** |
| 71 | * Responsible for taking jobs representing work to be performed by a client app, and determining |
| 72 | * based on the criteria specified when that job should be run against the client application's |
| 73 | * endpoint. |
| 74 | * Implements logic for scheduling, and rescheduling jobs. The JobSchedulerService knows nothing |
| 75 | * about constraints, or the state of active jobs. It receives callbacks from the various |
| 76 | * controllers and completed jobs and operates accordingly. |
| 77 | * |
| 78 | * Note on locking: Any operations that manipulate {@link #mJobs} need to lock on that object. |
| 79 | * Any function with the suffix 'Locked' also needs to lock on {@link #mJobs}. |
| 80 | * @hide |
| 81 | */ |
| 82 | public class JobSchedulerService extends com.android.server.SystemService |
Matthew Williams | 01ac45b | 2014-07-22 20:44:12 -0700 | [diff] [blame] | 83 | implements StateChangedListener, JobCompletedListener { |
Matthew Williams | aa98431 | 2015-10-15 16:08:05 -0700 | [diff] [blame] | 84 | public static final boolean DEBUG = false; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 85 | /** The number of concurrent jobs we run at one time. */ |
Dianne Hackborn | 8ad2af7 | 2015-03-17 17:00:24 -0700 | [diff] [blame] | 86 | private static final int MAX_JOB_CONTEXTS_COUNT |
| 87 | = ActivityManager.isLowRamDeviceStatic() ? 1 : 3; |
Matthew Williams | be0c417 | 2014-08-06 18:14:16 -0700 | [diff] [blame] | 88 | static final String TAG = "JobSchedulerService"; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 89 | /** Master list of jobs. */ |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 90 | final JobStore mJobs; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 91 | |
| 92 | static final int MSG_JOB_EXPIRED = 0; |
| 93 | static final int MSG_CHECK_JOB = 1; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 94 | static final int MSG_STOP_JOB = 2; |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 95 | static final int MSG_CHECK_JOB_GREEDY = 3; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 96 | |
| 97 | // Policy constants |
| 98 | /** |
| 99 | * Minimum # of idle jobs that must be ready in order to force the JMS to schedule things |
| 100 | * early. |
| 101 | */ |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 102 | static final int MIN_IDLE_COUNT = 1; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 103 | /** |
Matthew Williams | be0c417 | 2014-08-06 18:14:16 -0700 | [diff] [blame] | 104 | * Minimum # of charging jobs that must be ready in order to force the JMS to schedule things |
| 105 | * early. |
| 106 | */ |
| 107 | static final int MIN_CHARGING_COUNT = 1; |
| 108 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 109 | * Minimum # of connectivity jobs that must be ready in order to force the JMS to schedule |
| 110 | * things early. |
| 111 | */ |
Matthew Williams | aa98431 | 2015-10-15 16:08:05 -0700 | [diff] [blame] | 112 | 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] | 113 | /** |
| 114 | * Minimum # of jobs (with no particular constraints) for which the JMS will be happy running |
| 115 | * some work early. |
Matthew Williams | be0c417 | 2014-08-06 18:14:16 -0700 | [diff] [blame] | 116 | * 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] | 117 | */ |
Matthew Williams | be0c417 | 2014-08-06 18:14:16 -0700 | [diff] [blame] | 118 | static final int MIN_READY_JOBS_COUNT = 2; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 119 | |
| 120 | /** |
| 121 | * Track Services that have currently active or pending jobs. The index is provided by |
| 122 | * {@link JobStatus#getServiceToken()} |
| 123 | */ |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 124 | final List<JobServiceContext> mActiveServices = new ArrayList<>(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 125 | /** List of controllers that will notify this service of updates to jobs. */ |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 126 | List<StateController> mControllers; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 127 | /** |
| 128 | * Queue of pending jobs. The JobServiceContext class will receive jobs from this list |
| 129 | * when ready to execute them. |
| 130 | */ |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 131 | final ArrayList<JobStatus> mPendingJobs = new ArrayList<>(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 132 | |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 133 | int[] mStartedUsers = EmptyArray.INT; |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 134 | |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 135 | final JobHandler mHandler; |
| 136 | final JobSchedulerStub mJobSchedulerStub; |
| 137 | |
| 138 | IBatteryStats mBatteryStats; |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 139 | PowerManager mPowerManager; |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 140 | DeviceIdleController.LocalService mLocalDeviceIdleController; |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 141 | |
| 142 | /** |
| 143 | * Set to true once we are allowed to run third party apps. |
| 144 | */ |
| 145 | boolean mReadyToRock; |
| 146 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 147 | /** |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 148 | * True when in device idle mode, so we don't want to schedule any jobs. |
| 149 | */ |
| 150 | boolean mDeviceIdleMode; |
| 151 | |
| 152 | /** |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 153 | * What we last reported to DeviceIdleController about wheter we are active. |
| 154 | */ |
| 155 | boolean mReportedActive; |
| 156 | |
| 157 | /** |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 158 | * Cleans up outstanding jobs when a package is removed. Even if it's being replaced later we |
| 159 | * still clean up. On reinstall the package will have a new uid. |
| 160 | */ |
| 161 | private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 162 | @Override |
| 163 | public void onReceive(Context context, Intent intent) { |
Jeff Sharkey | 2f3e353 | 2015-12-21 14:16:43 -0700 | [diff] [blame] | 164 | final String action = intent.getAction(); |
| 165 | Slog.d(TAG, "Receieved: " + action); |
| 166 | if (Intent.ACTION_PACKAGE_REMOVED.equals(action)) { |
Christopher Tate | aad67a3 | 2014-10-20 16:29:20 -0700 | [diff] [blame] | 167 | // If this is an outright uninstall rather than the first half of an |
| 168 | // app update sequence, cancel the jobs associated with the app. |
| 169 | if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { |
| 170 | int uidRemoved = intent.getIntExtra(Intent.EXTRA_UID, -1); |
| 171 | if (DEBUG) { |
| 172 | Slog.d(TAG, "Removing jobs for uid: " + uidRemoved); |
| 173 | } |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 174 | cancelJobsForUid(uidRemoved, true); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 175 | } |
Jeff Sharkey | 2f3e353 | 2015-12-21 14:16:43 -0700 | [diff] [blame] | 176 | } else if (Intent.ACTION_USER_REMOVED.equals(action)) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 177 | final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0); |
| 178 | if (DEBUG) { |
| 179 | Slog.d(TAG, "Removing jobs for user: " + userId); |
| 180 | } |
| 181 | cancelJobsForUser(userId); |
Jeff Sharkey | 2f3e353 | 2015-12-21 14:16:43 -0700 | [diff] [blame] | 182 | } else if (PowerManager.ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED.equals(action) |
| 183 | || PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED.equals(action)) { |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 184 | updateIdleMode(mPowerManager != null |
| 185 | ? (mPowerManager.isDeviceIdleMode() |
| 186 | || mPowerManager.isLightDeviceIdleMode()) |
| 187 | : false); |
Jeff Sharkey | 2f3e353 | 2015-12-21 14:16:43 -0700 | [diff] [blame] | 188 | } else if (Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE.equals(action)) { |
| 189 | // Kick off pending jobs for any apps that re-appeared |
| 190 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | }; |
| 194 | |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 195 | final private IUidObserver mUidObserver = new IUidObserver.Stub() { |
| 196 | @Override public void onUidStateChanged(int uid, int procState) throws RemoteException { |
| 197 | } |
| 198 | |
| 199 | @Override public void onUidGone(int uid) throws RemoteException { |
| 200 | } |
| 201 | |
| 202 | @Override public void onUidActive(int uid) throws RemoteException { |
| 203 | } |
| 204 | |
| 205 | @Override public void onUidIdle(int uid) throws RemoteException { |
| 206 | cancelJobsForUid(uid, false); |
| 207 | } |
| 208 | }; |
| 209 | |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 210 | @Override |
| 211 | public void onStartUser(int userHandle) { |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 212 | mStartedUsers = ArrayUtils.appendInt(mStartedUsers, userHandle); |
| 213 | // Let's kick any outstanding jobs for this user. |
| 214 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | public void onUnlockUser(int userHandle) { |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 219 | // Let's kick any outstanding jobs for this user. |
| 220 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public void onStopUser(int userHandle) { |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 225 | mStartedUsers = ArrayUtils.removeInt(mStartedUsers, userHandle); |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 228 | /** |
| 229 | * Entry point from client to schedule the provided job. |
| 230 | * This cancels the job if it's already been scheduled, and replaces it with the one provided. |
| 231 | * @param job JobInfo object containing execution parameters |
| 232 | * @param uId The package identifier of the application this job is for. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 233 | * @return Result of this operation. See <code>JobScheduler#RESULT_*</code> return codes. |
| 234 | */ |
Matthew Williams | 900c67f | 2014-07-09 12:46:53 -0700 | [diff] [blame] | 235 | public int schedule(JobInfo job, int uId) { |
| 236 | JobStatus jobStatus = new JobStatus(job, uId); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 237 | cancelJob(uId, job.getId()); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 238 | try { |
| 239 | if (ActivityManagerNative.getDefault().getAppStartMode(uId, |
| 240 | job.getService().getPackageName()) == ActivityManager.APP_START_MODE_DISABLED) { |
| 241 | Slog.w(TAG, "Not scheduling job " + uId + ":" + job.toString() |
| 242 | + " -- package not allowed to start"); |
| 243 | return JobScheduler.RESULT_FAILURE; |
| 244 | } |
| 245 | } catch (RemoteException e) { |
| 246 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 247 | startTrackingJob(jobStatus); |
Matthew Williams | bafeeb9 | 2014-08-08 11:51:06 -0700 | [diff] [blame] | 248 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 249 | return JobScheduler.RESULT_SUCCESS; |
| 250 | } |
| 251 | |
| 252 | public List<JobInfo> getPendingJobs(int uid) { |
| 253 | ArrayList<JobInfo> outList = new ArrayList<JobInfo>(); |
| 254 | synchronized (mJobs) { |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 255 | ArraySet<JobStatus> jobs = mJobs.getJobs(); |
| 256 | for (int i=0; i<jobs.size(); i++) { |
| 257 | JobStatus job = jobs.valueAt(i); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 258 | if (job.getUid() == uid) { |
| 259 | outList.add(job.getJob()); |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | return outList; |
| 264 | } |
| 265 | |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 266 | void cancelJobsForUser(int userHandle) { |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 267 | List<JobStatus> jobsForUser; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 268 | synchronized (mJobs) { |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 269 | jobsForUser = mJobs.getJobsByUser(userHandle); |
| 270 | } |
| 271 | for (int i=0; i<jobsForUser.size(); i++) { |
| 272 | JobStatus toRemove = jobsForUser.get(i); |
| 273 | cancelJobImpl(toRemove); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Entry point from client to cancel all jobs originating from their uid. |
| 279 | * This will remove the job from the master list, and cancel the job if it was staged for |
| 280 | * execution or being executed. |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 281 | * @param uid Uid to check against for removal of a job. |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 282 | * @param forceAll If true, all jobs for the uid will be canceled; if false, only those |
| 283 | * whose apps are stopped. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 284 | */ |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 285 | public void cancelJobsForUid(int uid, boolean forceAll) { |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 286 | List<JobStatus> jobsForUid; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 287 | synchronized (mJobs) { |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 288 | jobsForUid = mJobs.getJobsByUid(uid); |
| 289 | } |
| 290 | for (int i=0; i<jobsForUid.size(); i++) { |
| 291 | JobStatus toRemove = jobsForUid.get(i); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 292 | if (!forceAll) { |
| 293 | String packageName = toRemove.getServiceComponent().getPackageName(); |
| 294 | try { |
| 295 | if (ActivityManagerNative.getDefault().getAppStartMode(uid, packageName) |
| 296 | != ActivityManager.APP_START_MODE_DISABLED) { |
| 297 | continue; |
| 298 | } |
| 299 | } catch (RemoteException e) { |
| 300 | } |
| 301 | } |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 302 | cancelJobImpl(toRemove); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Entry point from client to cancel the job corresponding to the jobId provided. |
| 308 | * This will remove the job from the master list, and cancel the job if it was staged for |
| 309 | * execution or being executed. |
| 310 | * @param uid Uid of the calling client. |
| 311 | * @param jobId Id of the job, provided at schedule-time. |
| 312 | */ |
| 313 | public void cancelJob(int uid, int jobId) { |
| 314 | JobStatus toCancel; |
| 315 | synchronized (mJobs) { |
| 316 | toCancel = mJobs.getJobByUidAndJobId(uid, jobId); |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 317 | } |
| 318 | if (toCancel != null) { |
| 319 | cancelJobImpl(toCancel); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 323 | private void cancelJobImpl(JobStatus cancelled) { |
Matthew Williams | ee410da | 2014-07-25 11:30:40 -0700 | [diff] [blame] | 324 | if (DEBUG) { |
| 325 | Slog.d(TAG, "Cancelling: " + cancelled); |
| 326 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 327 | stopTrackingJob(cancelled); |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 328 | synchronized (mJobs) { |
| 329 | // Remove from pending queue. |
| 330 | mPendingJobs.remove(cancelled); |
| 331 | // Cancel if running. |
| 332 | stopJobOnServiceContextLocked(cancelled); |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 333 | reportActive(); |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 334 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 335 | } |
| 336 | |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 337 | void updateIdleMode(boolean enabled) { |
| 338 | boolean changed = false; |
| 339 | boolean rocking; |
| 340 | synchronized (mJobs) { |
| 341 | if (mDeviceIdleMode != enabled) { |
| 342 | changed = true; |
| 343 | } |
| 344 | rocking = mReadyToRock; |
| 345 | } |
| 346 | if (changed) { |
| 347 | if (rocking) { |
| 348 | for (int i=0; i<mControllers.size(); i++) { |
| 349 | mControllers.get(i).deviceIdleModeChanged(enabled); |
| 350 | } |
| 351 | } |
| 352 | synchronized (mJobs) { |
| 353 | mDeviceIdleMode = enabled; |
| 354 | if (enabled) { |
| 355 | // When becoming idle, make sure no jobs are actively running. |
| 356 | for (int i=0; i<mActiveServices.size(); i++) { |
| 357 | JobServiceContext jsc = mActiveServices.get(i); |
| 358 | final JobStatus executing = jsc.getRunningJob(); |
| 359 | if (executing != null) { |
| 360 | jsc.cancelExecutingJob(); |
| 361 | } |
| 362 | } |
| 363 | } else { |
| 364 | // When coming out of idle, allow thing to start back up. |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 365 | if (rocking) { |
| 366 | if (mLocalDeviceIdleController != null) { |
| 367 | if (!mReportedActive) { |
| 368 | mReportedActive = true; |
| 369 | mLocalDeviceIdleController.setJobsActive(true); |
| 370 | } |
| 371 | } |
| 372 | } |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 373 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
| 374 | } |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 379 | void reportActive() { |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 380 | // active is true if pending queue contains jobs OR some job is running. |
| 381 | boolean active = mPendingJobs.size() > 0; |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 382 | if (mPendingJobs.size() <= 0) { |
| 383 | for (int i=0; i<mActiveServices.size(); i++) { |
| 384 | JobServiceContext jsc = mActiveServices.get(i); |
| 385 | if (!jsc.isAvailable()) { |
| 386 | active = true; |
| 387 | break; |
| 388 | } |
| 389 | } |
| 390 | } |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 391 | |
| 392 | if (mReportedActive != active) { |
| 393 | mReportedActive = active; |
| 394 | if (mLocalDeviceIdleController != null) { |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 395 | mLocalDeviceIdleController.setJobsActive(active); |
| 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 400 | /** |
| 401 | * Initializes the system service. |
| 402 | * <p> |
| 403 | * Subclasses must define a single argument constructor that accepts the context |
| 404 | * and passes it to super. |
| 405 | * </p> |
| 406 | * |
| 407 | * @param context The system server context. |
| 408 | */ |
| 409 | public JobSchedulerService(Context context) { |
| 410 | super(context); |
| 411 | // Create the controllers. |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 412 | mControllers = new ArrayList<StateController>(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 413 | mControllers.add(ConnectivityController.get(this)); |
| 414 | mControllers.add(TimeController.get(this)); |
| 415 | mControllers.add(IdleController.get(this)); |
| 416 | mControllers.add(BatteryController.get(this)); |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 417 | mControllers.add(AppIdleController.get(this)); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 418 | |
| 419 | mHandler = new JobHandler(context.getMainLooper()); |
| 420 | mJobSchedulerStub = new JobSchedulerStub(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 421 | mJobs = JobStore.initAndGet(this); |
| 422 | } |
| 423 | |
| 424 | @Override |
| 425 | public void onStart() { |
| 426 | publishBinderService(Context.JOB_SCHEDULER_SERVICE, mJobSchedulerStub); |
| 427 | } |
| 428 | |
| 429 | @Override |
| 430 | public void onBootPhase(int phase) { |
| 431 | if (PHASE_SYSTEM_SERVICES_READY == phase) { |
Jeff Sharkey | 2f3e353 | 2015-12-21 14:16:43 -0700 | [diff] [blame] | 432 | // Register for package removals and user removals. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 433 | final IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_REMOVED); |
| 434 | filter.addDataScheme("package"); |
| 435 | getContext().registerReceiverAsUser( |
| 436 | mBroadcastReceiver, UserHandle.ALL, filter, null, null); |
Jeff Sharkey | 2f3e353 | 2015-12-21 14:16:43 -0700 | [diff] [blame] | 437 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 438 | final IntentFilter userFilter = new IntentFilter(Intent.ACTION_USER_REMOVED); |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 439 | userFilter.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED); |
Dianne Hackborn | 08c47a5 | 2015-10-15 12:38:14 -0700 | [diff] [blame] | 440 | userFilter.addAction(PowerManager.ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 441 | getContext().registerReceiverAsUser( |
| 442 | mBroadcastReceiver, UserHandle.ALL, userFilter, null, null); |
Jeff Sharkey | 2f3e353 | 2015-12-21 14:16:43 -0700 | [diff] [blame] | 443 | |
| 444 | final IntentFilter storageFilter = new IntentFilter(); |
| 445 | storageFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE); |
| 446 | getContext().registerReceiverAsUser( |
| 447 | mBroadcastReceiver, UserHandle.ALL, storageFilter, null, null); |
| 448 | |
| 449 | mPowerManager = getContext().getSystemService(PowerManager.class); |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 450 | try { |
| 451 | ActivityManagerNative.getDefault().registerUidObserver(mUidObserver, |
| 452 | ActivityManager.UID_OBSERVER_IDLE); |
| 453 | } catch (RemoteException e) { |
| 454 | // ignored; both services live in system_server |
| 455 | } |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 456 | } else if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) { |
| 457 | synchronized (mJobs) { |
| 458 | // Let's go! |
| 459 | mReadyToRock = true; |
| 460 | mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService( |
| 461 | BatteryStats.SERVICE_NAME)); |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 462 | mLocalDeviceIdleController |
| 463 | = LocalServices.getService(DeviceIdleController.LocalService.class); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 464 | // Create the "runners". |
| 465 | for (int i = 0; i < MAX_JOB_CONTEXTS_COUNT; i++) { |
| 466 | mActiveServices.add( |
| 467 | new JobServiceContext(this, mBatteryStats, |
| 468 | getContext().getMainLooper())); |
| 469 | } |
| 470 | // Attach jobs to their controllers. |
| 471 | ArraySet<JobStatus> jobs = mJobs.getJobs(); |
| 472 | for (int i=0; i<jobs.size(); i++) { |
| 473 | JobStatus job = jobs.valueAt(i); |
Christopher Tate | 4a79dae | 2014-07-18 17:01:40 -0700 | [diff] [blame] | 474 | for (int controller=0; controller<mControllers.size(); controller++) { |
Dianne Hackborn | 2bf51f4 | 2015-03-24 14:17:12 -0700 | [diff] [blame] | 475 | mControllers.get(controller).deviceIdleModeChanged(mDeviceIdleMode); |
Christopher Tate | 4a79dae | 2014-07-18 17:01:40 -0700 | [diff] [blame] | 476 | mControllers.get(controller).maybeStartTrackingJob(job); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | // GO GO GO! |
| 480 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
| 481 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | |
| 485 | /** |
| 486 | * Called when we have a job status object that we need to insert in our |
| 487 | * {@link com.android.server.job.JobStore}, and make sure all the relevant controllers know |
| 488 | * about. |
| 489 | */ |
| 490 | private void startTrackingJob(JobStatus jobStatus) { |
| 491 | boolean update; |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 492 | boolean rocking; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 493 | synchronized (mJobs) { |
| 494 | update = mJobs.add(jobStatus); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 495 | rocking = mReadyToRock; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 496 | } |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 497 | if (rocking) { |
| 498 | for (int i=0; i<mControllers.size(); i++) { |
| 499 | StateController controller = mControllers.get(i); |
| 500 | if (update) { |
| 501 | controller.maybeStopTrackingJob(jobStatus); |
| 502 | } |
| 503 | controller.maybeStartTrackingJob(jobStatus); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 504 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 505 | } |
| 506 | } |
| 507 | |
| 508 | /** |
| 509 | * Called when we want to remove a JobStatus object that we've finished executing. Returns the |
| 510 | * object removed. |
| 511 | */ |
| 512 | private boolean stopTrackingJob(JobStatus jobStatus) { |
| 513 | boolean removed; |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 514 | boolean rocking; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 515 | synchronized (mJobs) { |
| 516 | // Remove from store as well as controllers. |
| 517 | removed = mJobs.remove(jobStatus); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 518 | rocking = mReadyToRock; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 519 | } |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 520 | if (removed && rocking) { |
| 521 | for (int i=0; i<mControllers.size(); i++) { |
| 522 | StateController controller = mControllers.get(i); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 523 | controller.maybeStopTrackingJob(jobStatus); |
| 524 | } |
| 525 | } |
| 526 | return removed; |
| 527 | } |
| 528 | |
| 529 | private boolean stopJobOnServiceContextLocked(JobStatus job) { |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 530 | for (int i=0; i<mActiveServices.size(); i++) { |
| 531 | JobServiceContext jsc = mActiveServices.get(i); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 532 | final JobStatus executing = jsc.getRunningJob(); |
| 533 | if (executing != null && executing.matches(job.getUid(), job.getJobId())) { |
| 534 | jsc.cancelExecutingJob(); |
| 535 | return true; |
| 536 | } |
| 537 | } |
| 538 | return false; |
| 539 | } |
| 540 | |
| 541 | /** |
| 542 | * @param job JobStatus we are querying against. |
| 543 | * @return Whether or not the job represented by the status object is currently being run or |
| 544 | * is pending. |
| 545 | */ |
| 546 | private boolean isCurrentlyActiveLocked(JobStatus job) { |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 547 | for (int i=0; i<mActiveServices.size(); i++) { |
| 548 | JobServiceContext serviceContext = mActiveServices.get(i); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 549 | final JobStatus running = serviceContext.getRunningJob(); |
| 550 | if (running != null && running.matches(job.getUid(), job.getJobId())) { |
| 551 | return true; |
| 552 | } |
| 553 | } |
| 554 | return false; |
| 555 | } |
| 556 | |
| 557 | /** |
Matthew Williams | 1bde39a | 2015-10-07 14:29:30 -0700 | [diff] [blame] | 558 | * Reschedules the given job based on the job's backoff policy. It doesn't make sense to |
| 559 | * specify an override deadline on a failed job (the failed job will run even though it's not |
| 560 | * ready), so we reschedule it with {@link JobStatus#NO_LATEST_RUNTIME}, but specify that any |
| 561 | * ready job with {@link JobStatus#numFailures} > 0 will be executed. |
| 562 | * |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 563 | * @param failureToReschedule Provided job status that we will reschedule. |
| 564 | * @return A newly instantiated JobStatus with the same constraints as the last job except |
| 565 | * with adjusted timing constraints. |
Matthew Williams | 1bde39a | 2015-10-07 14:29:30 -0700 | [diff] [blame] | 566 | * |
| 567 | * @see JobHandler#maybeQueueReadyJobsForExecutionLockedH |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 568 | */ |
| 569 | private JobStatus getRescheduleJobForFailure(JobStatus failureToReschedule) { |
| 570 | final long elapsedNowMillis = SystemClock.elapsedRealtime(); |
| 571 | final JobInfo job = failureToReschedule.getJob(); |
| 572 | |
| 573 | final long initialBackoffMillis = job.getInitialBackoffMillis(); |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 574 | final int backoffAttempts = failureToReschedule.getNumFailures() + 1; |
| 575 | long delayMillis; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 576 | |
| 577 | switch (job.getBackoffPolicy()) { |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 578 | case JobInfo.BACKOFF_POLICY_LINEAR: |
| 579 | delayMillis = initialBackoffMillis * backoffAttempts; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 580 | break; |
| 581 | default: |
| 582 | if (DEBUG) { |
| 583 | Slog.v(TAG, "Unrecognised back-off policy, defaulting to exponential."); |
| 584 | } |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 585 | case JobInfo.BACKOFF_POLICY_EXPONENTIAL: |
| 586 | delayMillis = |
| 587 | (long) Math.scalb(initialBackoffMillis, backoffAttempts - 1); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 588 | break; |
| 589 | } |
Matthew Williams | d1c0675 | 2014-08-22 14:15:28 -0700 | [diff] [blame] | 590 | delayMillis = |
| 591 | Math.min(delayMillis, JobInfo.MAX_BACKOFF_DELAY_MILLIS); |
| 592 | return new JobStatus(failureToReschedule, elapsedNowMillis + delayMillis, |
| 593 | JobStatus.NO_LATEST_RUNTIME, backoffAttempts); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | /** |
Matthew Williams | 1bde39a | 2015-10-07 14:29:30 -0700 | [diff] [blame] | 597 | * Called after a periodic has executed so we can reschedule it. We take the last execution |
| 598 | * time of the job to be the time of completion (i.e. the time at which this function is |
| 599 | * called). |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 600 | * This could be inaccurate b/c the job can run for as long as |
| 601 | * {@link com.android.server.job.JobServiceContext#EXECUTING_TIMESLICE_MILLIS}, but will lead |
| 602 | * to underscheduling at least, rather than if we had taken the last execution time to be the |
| 603 | * start of the execution. |
| 604 | * @return A new job representing the execution criteria for this instantiation of the |
| 605 | * recurring job. |
| 606 | */ |
| 607 | private JobStatus getRescheduleJobForPeriodic(JobStatus periodicToReschedule) { |
| 608 | final long elapsedNow = SystemClock.elapsedRealtime(); |
| 609 | // Compute how much of the period is remaining. |
Matthew Williams | 1bde39a | 2015-10-07 14:29:30 -0700 | [diff] [blame] | 610 | long runEarly = 0L; |
| 611 | |
| 612 | // If this periodic was rescheduled it won't have a deadline. |
| 613 | if (periodicToReschedule.hasDeadlineConstraint()) { |
| 614 | runEarly = Math.max(periodicToReschedule.getLatestRunTimeElapsed() - elapsedNow, 0L); |
| 615 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 616 | long newEarliestRunTimeElapsed = elapsedNow + runEarly; |
| 617 | long period = periodicToReschedule.getJob().getIntervalMillis(); |
| 618 | long newLatestRuntimeElapsed = newEarliestRunTimeElapsed + period; |
| 619 | |
| 620 | if (DEBUG) { |
| 621 | Slog.v(TAG, "Rescheduling executed periodic. New execution window [" + |
| 622 | newEarliestRunTimeElapsed/1000 + ", " + newLatestRuntimeElapsed/1000 + "]s"); |
| 623 | } |
| 624 | return new JobStatus(periodicToReschedule, newEarliestRunTimeElapsed, |
| 625 | newLatestRuntimeElapsed, 0 /* backoffAttempt */); |
| 626 | } |
| 627 | |
| 628 | // JobCompletedListener implementations. |
| 629 | |
| 630 | /** |
| 631 | * A job just finished executing. We fetch the |
| 632 | * {@link com.android.server.job.controllers.JobStatus} from the store and depending on |
| 633 | * whether we want to reschedule we readd it to the controllers. |
| 634 | * @param jobStatus Completed job. |
| 635 | * @param needsReschedule Whether the implementing class should reschedule this job. |
| 636 | */ |
| 637 | @Override |
| 638 | public void onJobCompleted(JobStatus jobStatus, boolean needsReschedule) { |
| 639 | if (DEBUG) { |
| 640 | Slog.d(TAG, "Completed " + jobStatus + ", reschedule=" + needsReschedule); |
| 641 | } |
| 642 | if (!stopTrackingJob(jobStatus)) { |
| 643 | if (DEBUG) { |
Matthew Williams | ee410da | 2014-07-25 11:30:40 -0700 | [diff] [blame] | 644 | 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] | 645 | } |
| 646 | return; |
| 647 | } |
| 648 | if (needsReschedule) { |
| 649 | JobStatus rescheduled = getRescheduleJobForFailure(jobStatus); |
| 650 | startTrackingJob(rescheduled); |
| 651 | } else if (jobStatus.getJob().isPeriodic()) { |
| 652 | JobStatus rescheduledPeriodic = getRescheduleJobForPeriodic(jobStatus); |
| 653 | startTrackingJob(rescheduledPeriodic); |
| 654 | } |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 655 | reportActive(); |
| 656 | mHandler.obtainMessage(MSG_CHECK_JOB_GREEDY).sendToTarget(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | // StateChangedListener implementations. |
| 660 | |
| 661 | /** |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 662 | * Posts a message to the {@link com.android.server.job.JobSchedulerService.JobHandler} that |
| 663 | * some controller's state has changed, so as to run through the list of jobs and start/stop |
| 664 | * any that are eligible. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 665 | */ |
| 666 | @Override |
| 667 | public void onControllerStateChanged() { |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 668 | mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | @Override |
| 672 | public void onRunJobNow(JobStatus jobStatus) { |
| 673 | mHandler.obtainMessage(MSG_JOB_EXPIRED, jobStatus).sendToTarget(); |
| 674 | } |
| 675 | |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 676 | private class JobHandler extends Handler { |
| 677 | |
| 678 | public JobHandler(Looper looper) { |
| 679 | super(looper); |
| 680 | } |
| 681 | |
| 682 | @Override |
| 683 | public void handleMessage(Message message) { |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 684 | synchronized (mJobs) { |
| 685 | if (!mReadyToRock) { |
| 686 | return; |
| 687 | } |
| 688 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 689 | switch (message.what) { |
| 690 | case MSG_JOB_EXPIRED: |
| 691 | synchronized (mJobs) { |
| 692 | JobStatus runNow = (JobStatus) message.obj; |
Matthew Williams | bafeeb9 | 2014-08-08 11:51:06 -0700 | [diff] [blame] | 693 | // runNow can be null, which is a controller's way of indicating that its |
| 694 | // state is such that all ready jobs should be run immediately. |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 695 | if (runNow != null && !mPendingJobs.contains(runNow) |
| 696 | && mJobs.containsJob(runNow)) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 697 | mPendingJobs.add(runNow); |
| 698 | } |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 699 | queueReadyJobsForExecutionLockedH(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 700 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 701 | break; |
| 702 | case MSG_CHECK_JOB: |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 703 | synchronized (mJobs) { |
Shreyas Basarge | 4cff8ac | 2015-12-10 21:32:52 +0000 | [diff] [blame] | 704 | if (mReportedActive) { |
| 705 | // if jobs are currently being run, queue all ready jobs for execution. |
| 706 | queueReadyJobsForExecutionLockedH(); |
| 707 | } else { |
| 708 | // Check the list of jobs and run some of them if we feel inclined. |
| 709 | maybeQueueReadyJobsForExecutionLockedH(); |
| 710 | } |
| 711 | } |
| 712 | break; |
| 713 | case MSG_CHECK_JOB_GREEDY: |
| 714 | synchronized (mJobs) { |
| 715 | queueReadyJobsForExecutionLockedH(); |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 716 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 717 | break; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 718 | case MSG_STOP_JOB: |
| 719 | cancelJobImpl((JobStatus)message.obj); |
| 720 | break; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 721 | } |
| 722 | maybeRunPendingJobsH(); |
| 723 | // Don't remove JOB_EXPIRED in case one came along while processing the queue. |
| 724 | removeMessages(MSG_CHECK_JOB); |
| 725 | } |
| 726 | |
| 727 | /** |
| 728 | * Run through list of jobs and execute all possible - at least one is expired so we do |
| 729 | * as many as we can. |
| 730 | */ |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 731 | private void queueReadyJobsForExecutionLockedH() { |
| 732 | ArraySet<JobStatus> jobs = mJobs.getJobs(); |
| 733 | if (DEBUG) { |
| 734 | Slog.d(TAG, "queuing all ready jobs for execution:"); |
| 735 | } |
| 736 | for (int i=0; i<jobs.size(); i++) { |
| 737 | JobStatus job = jobs.valueAt(i); |
| 738 | if (isReadyToBeExecutedLocked(job)) { |
| 739 | if (DEBUG) { |
| 740 | Slog.d(TAG, " queued " + job.toShortString()); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 741 | } |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 742 | mPendingJobs.add(job); |
| 743 | } else if (isReadyToBeCancelledLocked(job)) { |
| 744 | stopJobOnServiceContextLocked(job); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 745 | } |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 746 | } |
| 747 | if (DEBUG) { |
| 748 | final int queuedJobs = mPendingJobs.size(); |
| 749 | if (queuedJobs == 0) { |
| 750 | Slog.d(TAG, "No jobs pending."); |
| 751 | } else { |
| 752 | Slog.d(TAG, queuedJobs + " jobs queued."); |
Matthew Williams | 75fc525 | 2014-09-02 16:17:53 -0700 | [diff] [blame] | 753 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 754 | } |
| 755 | } |
| 756 | |
| 757 | /** |
| 758 | * The state of at least one job has changed. Here is where we could enforce various |
| 759 | * policies on when we want to execute jobs. |
| 760 | * Right now the policy is such: |
| 761 | * If >1 of the ready jobs is idle mode we send all of them off |
| 762 | * if more than 2 network connectivity jobs are ready we send them all off. |
| 763 | * If more than 4 jobs total are ready we send them all off. |
| 764 | * TODO: It would be nice to consolidate these sort of high-level policies somewhere. |
| 765 | */ |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 766 | private void maybeQueueReadyJobsForExecutionLockedH() { |
| 767 | int chargingCount = 0; |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 768 | int idleCount = 0; |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 769 | int backoffCount = 0; |
| 770 | int connectivityCount = 0; |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 771 | List<JobStatus> runnableJobs = null; |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 772 | ArraySet<JobStatus> jobs = mJobs.getJobs(); |
| 773 | for (int i=0; i<jobs.size(); i++) { |
| 774 | JobStatus job = jobs.valueAt(i); |
| 775 | if (isReadyToBeExecutedLocked(job)) { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 776 | try { |
| 777 | if (ActivityManagerNative.getDefault().getAppStartMode(job.getUid(), |
| 778 | job.getJob().getService().getPackageName()) |
| 779 | == ActivityManager.APP_START_MODE_DISABLED) { |
| 780 | Slog.w(TAG, "Aborting job " + job.getUid() + ":" |
| 781 | + job.getJob().toString() + " -- package not allowed to start"); |
| 782 | mHandler.obtainMessage(MSG_STOP_JOB, job).sendToTarget(); |
| 783 | continue; |
| 784 | } |
| 785 | } catch (RemoteException e) { |
| 786 | } |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 787 | if (job.getNumFailures() > 0) { |
| 788 | backoffCount++; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 789 | } |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 790 | if (job.hasIdleConstraint()) { |
| 791 | idleCount++; |
| 792 | } |
| 793 | if (job.hasConnectivityConstraint() || job.hasUnmeteredConstraint()) { |
| 794 | connectivityCount++; |
| 795 | } |
| 796 | if (job.hasChargingConstraint()) { |
| 797 | chargingCount++; |
| 798 | } |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 799 | if (runnableJobs == null) { |
| 800 | runnableJobs = new ArrayList<>(); |
| 801 | } |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 802 | runnableJobs.add(job); |
| 803 | } else if (isReadyToBeCancelledLocked(job)) { |
| 804 | stopJobOnServiceContextLocked(job); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 805 | } |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 806 | } |
| 807 | if (backoffCount > 0 || |
| 808 | idleCount >= MIN_IDLE_COUNT || |
| 809 | connectivityCount >= MIN_CONNECTIVITY_COUNT || |
| 810 | chargingCount >= MIN_CHARGING_COUNT || |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 811 | (runnableJobs != null && runnableJobs.size() >= MIN_READY_JOBS_COUNT)) { |
Matthew Williams | be0c417 | 2014-08-06 18:14:16 -0700 | [diff] [blame] | 812 | if (DEBUG) { |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 813 | Slog.d(TAG, "maybeQueueReadyJobsForExecutionLockedH: Running jobs."); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 814 | } |
Matthew Williams | 48a30db | 2014-09-23 13:39:36 -0700 | [diff] [blame] | 815 | for (int i=0; i<runnableJobs.size(); i++) { |
| 816 | mPendingJobs.add(runnableJobs.get(i)); |
| 817 | } |
| 818 | } else { |
| 819 | if (DEBUG) { |
| 820 | Slog.d(TAG, "maybeQueueReadyJobsForExecutionLockedH: Not running anything."); |
| 821 | } |
| 822 | } |
| 823 | if (DEBUG) { |
| 824 | Slog.d(TAG, "idle=" + idleCount + " connectivity=" + |
| 825 | connectivityCount + " charging=" + chargingCount + " tot=" + |
| 826 | runnableJobs.size()); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 827 | } |
| 828 | } |
| 829 | |
| 830 | /** |
| 831 | * Criteria for moving a job into the pending queue: |
| 832 | * - It's ready. |
| 833 | * - It's not pending. |
| 834 | * - It's not already running on a JSC. |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 835 | * - The user that requested the job is running. |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 836 | * - The component is enabled and runnable. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 837 | */ |
| 838 | private boolean isReadyToBeExecutedLocked(JobStatus job) { |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 839 | final boolean jobReady = job.isReady(); |
| 840 | final boolean jobPending = mPendingJobs.contains(job); |
| 841 | final boolean jobActive = isCurrentlyActiveLocked(job); |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 842 | |
| 843 | final int userId = job.getUserId(); |
| 844 | final boolean userStarted = ArrayUtils.contains(mStartedUsers, userId); |
| 845 | final boolean componentPresent; |
| 846 | try { |
| 847 | componentPresent = (AppGlobals.getPackageManager().getServiceInfo( |
Jeff Sharkey | 2f3e353 | 2015-12-21 14:16:43 -0700 | [diff] [blame] | 848 | job.getServiceComponent(), PackageManager.MATCH_DEBUG_TRIAGED_MISSING, |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 849 | userId) != null); |
| 850 | } catch (RemoteException e) { |
| 851 | throw e.rethrowAsRuntimeException(); |
| 852 | } |
| 853 | |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 854 | if (DEBUG) { |
| 855 | Slog.v(TAG, "isReadyToBeExecutedLocked: " + job.toShortString() |
| 856 | + " ready=" + jobReady + " pending=" + jobPending |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 857 | + " active=" + jobActive + " userStarted=" + userStarted |
| 858 | + " componentPresent=" + componentPresent); |
Matthew Williams | 9ae3dbe | 2014-08-21 13:47:47 -0700 | [diff] [blame] | 859 | } |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 860 | return userStarted && componentPresent && jobReady && !jobPending && !jobActive; |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | /** |
| 864 | * Criteria for cancelling an active job: |
| 865 | * - It's not ready |
| 866 | * - It's running on a JSC. |
| 867 | */ |
| 868 | private boolean isReadyToBeCancelledLocked(JobStatus job) { |
| 869 | return !job.isReady() && isCurrentlyActiveLocked(job); |
| 870 | } |
| 871 | |
| 872 | /** |
| 873 | * Reconcile jobs in the pending queue against available execution contexts. |
| 874 | * A controller can force a job into the pending queue even if it's already running, but |
| 875 | * here is where we decide whether to actually execute it. |
| 876 | */ |
| 877 | private void maybeRunPendingJobsH() { |
| 878 | synchronized (mJobs) { |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 879 | if (mDeviceIdleMode) { |
| 880 | // If device is idle, we will not schedule jobs to run. |
| 881 | return; |
| 882 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 883 | Iterator<JobStatus> it = mPendingJobs.iterator(); |
Matthew Williams | 75fc525 | 2014-09-02 16:17:53 -0700 | [diff] [blame] | 884 | if (DEBUG) { |
| 885 | Slog.d(TAG, "pending queue: " + mPendingJobs.size() + " jobs."); |
| 886 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 887 | while (it.hasNext()) { |
| 888 | JobStatus nextPending = it.next(); |
| 889 | JobServiceContext availableContext = null; |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 890 | for (int i=0; i<mActiveServices.size(); i++) { |
| 891 | JobServiceContext jsc = mActiveServices.get(i); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 892 | final JobStatus running = jsc.getRunningJob(); |
| 893 | if (running != null && running.matches(nextPending.getUid(), |
| 894 | nextPending.getJobId())) { |
Matthew Williams | ee410da | 2014-07-25 11:30:40 -0700 | [diff] [blame] | 895 | // Already running this job for this uId, skip. |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 896 | availableContext = null; |
| 897 | break; |
| 898 | } |
| 899 | if (jsc.isAvailable()) { |
| 900 | availableContext = jsc; |
| 901 | } |
| 902 | } |
| 903 | if (availableContext != null) { |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 904 | if (DEBUG) { |
| 905 | Slog.d(TAG, "About to run job " |
| 906 | + nextPending.getJob().getService().toString()); |
| 907 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 908 | if (!availableContext.executeRunnableJob(nextPending)) { |
| 909 | if (DEBUG) { |
| 910 | Slog.d(TAG, "Error executing " + nextPending); |
| 911 | } |
| 912 | mJobs.remove(nextPending); |
| 913 | } |
| 914 | it.remove(); |
| 915 | } |
| 916 | } |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 917 | reportActive(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 918 | } |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | /** |
| 923 | * Binder stub trampoline implementation |
| 924 | */ |
| 925 | final class JobSchedulerStub extends IJobScheduler.Stub { |
| 926 | /** Cache determination of whether a given app can persist jobs |
| 927 | * key is uid of the calling app; value is undetermined/true/false |
| 928 | */ |
| 929 | private final SparseArray<Boolean> mPersistCache = new SparseArray<Boolean>(); |
| 930 | |
| 931 | // Enforce that only the app itself (or shared uid participant) can schedule a |
| 932 | // job that runs one of the app's services, as well as verifying that the |
| 933 | // named service properly requires the BIND_JOB_SERVICE permission |
| 934 | private void enforceValidJobRequest(int uid, JobInfo job) { |
Christopher Tate | 5568f54 | 2014-06-18 13:53:31 -0700 | [diff] [blame] | 935 | final IPackageManager pm = AppGlobals.getPackageManager(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 936 | final ComponentName service = job.getService(); |
| 937 | try { |
Jeff Sharkey | 2f3e353 | 2015-12-21 14:16:43 -0700 | [diff] [blame] | 938 | ServiceInfo si = pm.getServiceInfo(service, |
| 939 | PackageManager.MATCH_DEBUG_TRIAGED_MISSING, UserHandle.getUserId(uid)); |
Christopher Tate | 5568f54 | 2014-06-18 13:53:31 -0700 | [diff] [blame] | 940 | if (si == null) { |
| 941 | throw new IllegalArgumentException("No such service " + service); |
| 942 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 943 | if (si.applicationInfo.uid != uid) { |
| 944 | throw new IllegalArgumentException("uid " + uid + |
| 945 | " cannot schedule job in " + service.getPackageName()); |
| 946 | } |
| 947 | if (!JobService.PERMISSION_BIND.equals(si.permission)) { |
| 948 | throw new IllegalArgumentException("Scheduled service " + service |
| 949 | + " does not require android.permission.BIND_JOB_SERVICE permission"); |
| 950 | } |
Christopher Tate | 5568f54 | 2014-06-18 13:53:31 -0700 | [diff] [blame] | 951 | } catch (RemoteException e) { |
| 952 | // Can't happen; the Package Manager is in this same process |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 953 | } |
| 954 | } |
| 955 | |
| 956 | private boolean canPersistJobs(int pid, int uid) { |
| 957 | // If we get this far we're good to go; all we need to do now is check |
| 958 | // whether the app is allowed to persist its scheduled work. |
| 959 | final boolean canPersist; |
| 960 | synchronized (mPersistCache) { |
| 961 | Boolean cached = mPersistCache.get(uid); |
| 962 | if (cached != null) { |
| 963 | canPersist = cached.booleanValue(); |
| 964 | } else { |
| 965 | // Persisting jobs is tantamount to running at boot, so we permit |
| 966 | // it when the app has declared that it uses the RECEIVE_BOOT_COMPLETED |
| 967 | // permission |
| 968 | int result = getContext().checkPermission( |
| 969 | android.Manifest.permission.RECEIVE_BOOT_COMPLETED, pid, uid); |
| 970 | canPersist = (result == PackageManager.PERMISSION_GRANTED); |
| 971 | mPersistCache.put(uid, canPersist); |
| 972 | } |
| 973 | } |
| 974 | return canPersist; |
| 975 | } |
| 976 | |
| 977 | // IJobScheduler implementation |
| 978 | @Override |
| 979 | public int schedule(JobInfo job) throws RemoteException { |
| 980 | if (DEBUG) { |
Matthew Williams | ee410da | 2014-07-25 11:30:40 -0700 | [diff] [blame] | 981 | Slog.d(TAG, "Scheduling job: " + job.toString()); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 982 | } |
| 983 | final int pid = Binder.getCallingPid(); |
| 984 | final int uid = Binder.getCallingUid(); |
| 985 | |
| 986 | enforceValidJobRequest(uid, job); |
Matthew Williams | 900c67f | 2014-07-09 12:46:53 -0700 | [diff] [blame] | 987 | if (job.isPersisted()) { |
| 988 | if (!canPersistJobs(pid, uid)) { |
| 989 | throw new IllegalArgumentException("Error: requested job be persisted without" |
| 990 | + " holding RECEIVE_BOOT_COMPLETED permission."); |
| 991 | } |
| 992 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 993 | |
| 994 | long ident = Binder.clearCallingIdentity(); |
| 995 | try { |
Matthew Williams | 900c67f | 2014-07-09 12:46:53 -0700 | [diff] [blame] | 996 | return JobSchedulerService.this.schedule(job, uid); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 997 | } finally { |
| 998 | Binder.restoreCallingIdentity(ident); |
| 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | @Override |
| 1003 | public List<JobInfo> getAllPendingJobs() throws RemoteException { |
| 1004 | final int uid = Binder.getCallingUid(); |
| 1005 | |
| 1006 | long ident = Binder.clearCallingIdentity(); |
| 1007 | try { |
| 1008 | return JobSchedulerService.this.getPendingJobs(uid); |
| 1009 | } finally { |
| 1010 | Binder.restoreCallingIdentity(ident); |
| 1011 | } |
| 1012 | } |
| 1013 | |
| 1014 | @Override |
| 1015 | public void cancelAll() throws RemoteException { |
| 1016 | final int uid = Binder.getCallingUid(); |
| 1017 | |
| 1018 | long ident = Binder.clearCallingIdentity(); |
| 1019 | try { |
Dianne Hackborn | bef28fe | 2015-10-29 17:57:11 -0700 | [diff] [blame] | 1020 | JobSchedulerService.this.cancelJobsForUid(uid, true); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1021 | } finally { |
| 1022 | Binder.restoreCallingIdentity(ident); |
| 1023 | } |
| 1024 | } |
| 1025 | |
| 1026 | @Override |
| 1027 | public void cancel(int jobId) throws RemoteException { |
| 1028 | final int uid = Binder.getCallingUid(); |
| 1029 | |
| 1030 | long ident = Binder.clearCallingIdentity(); |
| 1031 | try { |
| 1032 | JobSchedulerService.this.cancelJob(uid, jobId); |
| 1033 | } finally { |
| 1034 | Binder.restoreCallingIdentity(ident); |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | /** |
| 1039 | * "dumpsys" infrastructure |
| 1040 | */ |
| 1041 | @Override |
| 1042 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1043 | getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG); |
| 1044 | |
| 1045 | long identityToken = Binder.clearCallingIdentity(); |
| 1046 | try { |
| 1047 | JobSchedulerService.this.dumpInternal(pw); |
| 1048 | } finally { |
| 1049 | Binder.restoreCallingIdentity(identityToken); |
| 1050 | } |
| 1051 | } |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 1052 | } |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1053 | |
| 1054 | void dumpInternal(PrintWriter pw) { |
Christopher Tate | f973a7b | 2014-08-29 12:54:08 -0700 | [diff] [blame] | 1055 | final long now = SystemClock.elapsedRealtime(); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1056 | synchronized (mJobs) { |
Jeff Sharkey | 5217cac | 2015-12-20 15:34:01 -0700 | [diff] [blame] | 1057 | pw.println("Started users: " + Arrays.toString(mStartedUsers)); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1058 | pw.println("Registered jobs:"); |
| 1059 | if (mJobs.size() > 0) { |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1060 | ArraySet<JobStatus> jobs = mJobs.getJobs(); |
| 1061 | for (int i=0; i<jobs.size(); i++) { |
| 1062 | JobStatus job = jobs.valueAt(i); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1063 | job.dump(pw, " "); |
| 1064 | } |
| 1065 | } else { |
Christopher Tate | f973a7b | 2014-08-29 12:54:08 -0700 | [diff] [blame] | 1066 | pw.println(" None."); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1067 | } |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1068 | for (int i=0; i<mControllers.size(); i++) { |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1069 | pw.println(); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1070 | mControllers.get(i).dumpControllerState(pw); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1071 | } |
| 1072 | pw.println(); |
Christopher Tate | f973a7b | 2014-08-29 12:54:08 -0700 | [diff] [blame] | 1073 | pw.println("Pending:"); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1074 | for (int i=0; i<mPendingJobs.size(); i++) { |
| 1075 | pw.println(mPendingJobs.get(i).hashCode()); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1076 | } |
| 1077 | pw.println(); |
| 1078 | pw.println("Active jobs:"); |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1079 | for (int i=0; i<mActiveServices.size(); i++) { |
| 1080 | JobServiceContext jsc = mActiveServices.get(i); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1081 | if (jsc.isAvailable()) { |
| 1082 | continue; |
| 1083 | } else { |
Christopher Tate | f973a7b | 2014-08-29 12:54:08 -0700 | [diff] [blame] | 1084 | final long timeout = jsc.getTimeoutElapsed(); |
| 1085 | pw.print("Running for: "); |
| 1086 | pw.print((now - jsc.getExecutionStartTimeElapsed())/1000); |
| 1087 | pw.print("s timeout="); |
| 1088 | pw.print(timeout); |
| 1089 | pw.print(" fromnow="); |
| 1090 | pw.println(timeout-now); |
| 1091 | jsc.getRunningJob().dump(pw, " "); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1092 | } |
| 1093 | } |
Dianne Hackborn | fdb1956 | 2014-07-11 16:03:36 -0700 | [diff] [blame] | 1094 | pw.println(); |
| 1095 | pw.print("mReadyToRock="); pw.println(mReadyToRock); |
Dianne Hackborn | 88e98df | 2015-03-23 13:29:14 -0700 | [diff] [blame] | 1096 | pw.print("mDeviceIdleMode="); pw.println(mDeviceIdleMode); |
Dianne Hackborn | 627dfa1 | 2015-11-11 18:10:30 -0800 | [diff] [blame] | 1097 | pw.print("mReportedActive="); pw.println(mReportedActive); |
Christopher Tate | 7060b04 | 2014-06-09 19:50:00 -0700 | [diff] [blame] | 1098 | } |
| 1099 | pw.println(); |
| 1100 | } |
| 1101 | } |