blob: e5a32648ea7541cf7acf2d14107a97f2c44a2d85 [file] [log] [blame]
Christopher Tate7060b042014-06-09 19:50:00 -07001/*
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
17package com.android.server.job;
18
Shreyas Basarge5db09082016-01-07 13:38:29 +000019import java.io.FileDescriptor;
20import java.io.PrintWriter;
21import java.util.ArrayList;
22import java.util.Iterator;
23import java.util.List;
24
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070025import android.app.ActivityManager;
Dianne Hackbornbef28fe2015-10-29 17:57:11 -070026import android.app.ActivityManagerNative;
Christopher Tate5568f542014-06-18 13:53:31 -070027import android.app.AppGlobals;
Dianne Hackbornbef28fe2015-10-29 17:57:11 -070028import android.app.IUidObserver;
Christopher Tate7060b042014-06-09 19:50:00 -070029import android.app.job.JobInfo;
Shreyas Basarge5db09082016-01-07 13:38:29 +000030import android.app.job.JobParameters;
Christopher Tate7060b042014-06-09 19:50:00 -070031import android.app.job.JobScheduler;
32import android.app.job.JobService;
Shreyas Basarge5db09082016-01-07 13:38:29 +000033import android.app.job.IJobScheduler;
Christopher Tate7060b042014-06-09 19:50:00 -070034import android.content.BroadcastReceiver;
35import android.content.ComponentName;
36import android.content.Context;
37import android.content.Intent;
38import android.content.IntentFilter;
Christopher Tate5568f542014-06-18 13:53:31 -070039import android.content.pm.IPackageManager;
Christopher Tate7060b042014-06-09 19:50:00 -070040import android.content.pm.PackageManager;
Christopher Tate7060b042014-06-09 19:50:00 -070041import android.content.pm.ServiceInfo;
Dianne Hackbornfdb19562014-07-11 16:03:36 -070042import android.os.BatteryStats;
Christopher Tate7060b042014-06-09 19:50:00 -070043import android.os.Binder;
44import android.os.Handler;
45import android.os.Looper;
46import android.os.Message;
Dianne Hackborn88e98df2015-03-23 13:29:14 -070047import android.os.PowerManager;
Christopher Tate7060b042014-06-09 19:50:00 -070048import android.os.RemoteException;
Dianne Hackbornfdb19562014-07-11 16:03:36 -070049import android.os.ServiceManager;
Christopher Tate7060b042014-06-09 19:50:00 -070050import android.os.SystemClock;
51import android.os.UserHandle;
Dianne Hackbornfdb19562014-07-11 16:03:36 -070052import android.util.ArraySet;
Christopher Tate7060b042014-06-09 19:50:00 -070053import android.util.Slog;
54import android.util.SparseArray;
55
Dianne Hackbornfdb19562014-07-11 16:03:36 -070056import com.android.internal.app.IBatteryStats;
Dianne Hackborn627dfa12015-11-11 18:10:30 -080057import com.android.server.DeviceIdleController;
58import com.android.server.LocalServices;
Amith Yamasanib0ff3222015-03-04 09:56:14 -080059import com.android.server.job.controllers.AppIdleController;
Christopher Tate7060b042014-06-09 19:50:00 -070060import com.android.server.job.controllers.BatteryController;
61import com.android.server.job.controllers.ConnectivityController;
62import com.android.server.job.controllers.IdleController;
63import com.android.server.job.controllers.JobStatus;
64import com.android.server.job.controllers.StateController;
65import com.android.server.job.controllers.TimeController;
66
Christopher Tate7060b042014-06-09 19:50:00 -070067/**
68 * Responsible for taking jobs representing work to be performed by a client app, and determining
69 * based on the criteria specified when that job should be run against the client application's
70 * endpoint.
71 * Implements logic for scheduling, and rescheduling jobs. The JobSchedulerService knows nothing
72 * about constraints, or the state of active jobs. It receives callbacks from the various
73 * controllers and completed jobs and operates accordingly.
74 *
75 * Note on locking: Any operations that manipulate {@link #mJobs} need to lock on that object.
76 * Any function with the suffix 'Locked' also needs to lock on {@link #mJobs}.
77 * @hide
78 */
79public class JobSchedulerService extends com.android.server.SystemService
Matthew Williams01ac45b2014-07-22 20:44:12 -070080 implements StateChangedListener, JobCompletedListener {
Matthew Williamsaa984312015-10-15 16:08:05 -070081 public static final boolean DEBUG = false;
Christopher Tate7060b042014-06-09 19:50:00 -070082 /** The number of concurrent jobs we run at one time. */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070083 private static final int MAX_JOB_CONTEXTS_COUNT
84 = ActivityManager.isLowRamDeviceStatic() ? 1 : 3;
Matthew Williamsbe0c4172014-08-06 18:14:16 -070085 static final String TAG = "JobSchedulerService";
Christopher Tate7060b042014-06-09 19:50:00 -070086 /** Master list of jobs. */
Dianne Hackbornfdb19562014-07-11 16:03:36 -070087 final JobStore mJobs;
Christopher Tate7060b042014-06-09 19:50:00 -070088
89 static final int MSG_JOB_EXPIRED = 0;
90 static final int MSG_CHECK_JOB = 1;
Dianne Hackbornbef28fe2015-10-29 17:57:11 -070091 static final int MSG_STOP_JOB = 2;
Shreyas Basarge4cff8ac2015-12-10 21:32:52 +000092 static final int MSG_CHECK_JOB_GREEDY = 3;
Christopher Tate7060b042014-06-09 19:50:00 -070093
94 // Policy constants
95 /**
96 * Minimum # of idle jobs that must be ready in order to force the JMS to schedule things
97 * early.
98 */
Dianne Hackbornfdb19562014-07-11 16:03:36 -070099 static final int MIN_IDLE_COUNT = 1;
Christopher Tate7060b042014-06-09 19:50:00 -0700100 /**
Matthew Williamsbe0c4172014-08-06 18:14:16 -0700101 * Minimum # of charging jobs that must be ready in order to force the JMS to schedule things
102 * early.
103 */
104 static final int MIN_CHARGING_COUNT = 1;
105 /**
Christopher Tate7060b042014-06-09 19:50:00 -0700106 * Minimum # of connectivity jobs that must be ready in order to force the JMS to schedule
107 * things early.
108 */
Matthew Williamsaa984312015-10-15 16:08:05 -0700109 static final int MIN_CONNECTIVITY_COUNT = 1; // Run connectivity jobs as soon as ready.
Christopher Tate7060b042014-06-09 19:50:00 -0700110 /**
111 * Minimum # of jobs (with no particular constraints) for which the JMS will be happy running
112 * some work early.
Matthew Williamsbe0c4172014-08-06 18:14:16 -0700113 * This is correlated with the amount of batching we'll be able to do.
Christopher Tate7060b042014-06-09 19:50:00 -0700114 */
Matthew Williamsbe0c4172014-08-06 18:14:16 -0700115 static final int MIN_READY_JOBS_COUNT = 2;
Christopher Tate7060b042014-06-09 19:50:00 -0700116
117 /**
118 * Track Services that have currently active or pending jobs. The index is provided by
119 * {@link JobStatus#getServiceToken()}
120 */
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700121 final List<JobServiceContext> mActiveServices = new ArrayList<>();
Christopher Tate7060b042014-06-09 19:50:00 -0700122 /** List of controllers that will notify this service of updates to jobs. */
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700123 List<StateController> mControllers;
Christopher Tate7060b042014-06-09 19:50:00 -0700124 /**
125 * Queue of pending jobs. The JobServiceContext class will receive jobs from this list
126 * when ready to execute them.
127 */
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700128 final ArrayList<JobStatus> mPendingJobs = new ArrayList<>();
Christopher Tate7060b042014-06-09 19:50:00 -0700129
Shreyas Basarge5db09082016-01-07 13:38:29 +0000130 final ArrayList<Integer> mStartedUsers = new ArrayList<>();
Matthew Williams9ae3dbe2014-08-21 13:47:47 -0700131
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700132 final JobHandler mHandler;
133 final JobSchedulerStub mJobSchedulerStub;
134
135 IBatteryStats mBatteryStats;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700136 PowerManager mPowerManager;
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800137 DeviceIdleController.LocalService mLocalDeviceIdleController;
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700138
139 /**
140 * Set to true once we are allowed to run third party apps.
141 */
142 boolean mReadyToRock;
143
Christopher Tate7060b042014-06-09 19:50:00 -0700144 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700145 * True when in device idle mode, so we don't want to schedule any jobs.
146 */
147 boolean mDeviceIdleMode;
148
149 /**
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800150 * What we last reported to DeviceIdleController about wheter we are active.
151 */
152 boolean mReportedActive;
153
154 /**
Christopher Tate7060b042014-06-09 19:50:00 -0700155 * Cleans up outstanding jobs when a package is removed. Even if it's being replaced later we
156 * still clean up. On reinstall the package will have a new uid.
157 */
158 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
159 @Override
160 public void onReceive(Context context, Intent intent) {
Shreyas Basarge5db09082016-01-07 13:38:29 +0000161 Slog.d(TAG, "Receieved: " + intent.getAction());
162 if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) {
Christopher Tateaad67a32014-10-20 16:29:20 -0700163 // If this is an outright uninstall rather than the first half of an
164 // app update sequence, cancel the jobs associated with the app.
165 if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
166 int uidRemoved = intent.getIntExtra(Intent.EXTRA_UID, -1);
167 if (DEBUG) {
168 Slog.d(TAG, "Removing jobs for uid: " + uidRemoved);
169 }
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700170 cancelJobsForUid(uidRemoved, true);
Christopher Tate7060b042014-06-09 19:50:00 -0700171 }
Shreyas Basarge5db09082016-01-07 13:38:29 +0000172 } else if (Intent.ACTION_USER_REMOVED.equals(intent.getAction())) {
Christopher Tate7060b042014-06-09 19:50:00 -0700173 final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0);
174 if (DEBUG) {
175 Slog.d(TAG, "Removing jobs for user: " + userId);
176 }
177 cancelJobsForUser(userId);
Shreyas Basarge5db09082016-01-07 13:38:29 +0000178 } else if (PowerManager.ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED.equals(intent.getAction())
179 || PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED.equals(intent.getAction())) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700180 updateIdleMode(mPowerManager != null
181 ? (mPowerManager.isDeviceIdleMode()
Shreyas Basarge5db09082016-01-07 13:38:29 +0000182 || mPowerManager.isLightDeviceIdleMode())
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700183 : false);
Christopher Tate7060b042014-06-09 19:50:00 -0700184 }
185 }
186 };
187
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700188 final private IUidObserver mUidObserver = new IUidObserver.Stub() {
189 @Override public void onUidStateChanged(int uid, int procState) throws RemoteException {
190 }
191
192 @Override public void onUidGone(int uid) throws RemoteException {
193 }
194
195 @Override public void onUidActive(int uid) throws RemoteException {
196 }
197
198 @Override public void onUidIdle(int uid) throws RemoteException {
199 cancelJobsForUid(uid, false);
200 }
201 };
202
Matthew Williams9ae3dbe2014-08-21 13:47:47 -0700203 @Override
204 public void onStartUser(int userHandle) {
Shreyas Basarge5db09082016-01-07 13:38:29 +0000205 mStartedUsers.add(userHandle);
Matthew Williams9ae3dbe2014-08-21 13:47:47 -0700206 // Let's kick any outstanding jobs for this user.
207 mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
208 }
209
210 @Override
211 public void onStopUser(int userHandle) {
Shreyas Basarge5db09082016-01-07 13:38:29 +0000212 mStartedUsers.remove(Integer.valueOf(userHandle));
Matthew Williams9ae3dbe2014-08-21 13:47:47 -0700213 }
214
Christopher Tate7060b042014-06-09 19:50:00 -0700215 /**
216 * Entry point from client to schedule the provided job.
217 * This cancels the job if it's already been scheduled, and replaces it with the one provided.
218 * @param job JobInfo object containing execution parameters
219 * @param uId The package identifier of the application this job is for.
Christopher Tate7060b042014-06-09 19:50:00 -0700220 * @return Result of this operation. See <code>JobScheduler#RESULT_*</code> return codes.
221 */
Matthew Williams900c67f2014-07-09 12:46:53 -0700222 public int schedule(JobInfo job, int uId) {
223 JobStatus jobStatus = new JobStatus(job, uId);
Christopher Tate7060b042014-06-09 19:50:00 -0700224 cancelJob(uId, job.getId());
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700225 try {
226 if (ActivityManagerNative.getDefault().getAppStartMode(uId,
227 job.getService().getPackageName()) == ActivityManager.APP_START_MODE_DISABLED) {
228 Slog.w(TAG, "Not scheduling job " + uId + ":" + job.toString()
229 + " -- package not allowed to start");
230 return JobScheduler.RESULT_FAILURE;
231 }
232 } catch (RemoteException e) {
233 }
Christopher Tate7060b042014-06-09 19:50:00 -0700234 startTrackingJob(jobStatus);
Matthew Williamsbafeeb92014-08-08 11:51:06 -0700235 mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
Christopher Tate7060b042014-06-09 19:50:00 -0700236 return JobScheduler.RESULT_SUCCESS;
237 }
238
239 public List<JobInfo> getPendingJobs(int uid) {
240 ArrayList<JobInfo> outList = new ArrayList<JobInfo>();
241 synchronized (mJobs) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700242 ArraySet<JobStatus> jobs = mJobs.getJobs();
243 for (int i=0; i<jobs.size(); i++) {
244 JobStatus job = jobs.valueAt(i);
Christopher Tate7060b042014-06-09 19:50:00 -0700245 if (job.getUid() == uid) {
246 outList.add(job.getJob());
247 }
248 }
249 }
250 return outList;
251 }
252
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700253 void cancelJobsForUser(int userHandle) {
Matthew Williams48a30db2014-09-23 13:39:36 -0700254 List<JobStatus> jobsForUser;
Christopher Tate7060b042014-06-09 19:50:00 -0700255 synchronized (mJobs) {
Matthew Williams48a30db2014-09-23 13:39:36 -0700256 jobsForUser = mJobs.getJobsByUser(userHandle);
257 }
258 for (int i=0; i<jobsForUser.size(); i++) {
259 JobStatus toRemove = jobsForUser.get(i);
260 cancelJobImpl(toRemove);
Christopher Tate7060b042014-06-09 19:50:00 -0700261 }
262 }
263
264 /**
265 * Entry point from client to cancel all jobs originating from their uid.
266 * This will remove the job from the master list, and cancel the job if it was staged for
267 * execution or being executed.
Matthew Williams48a30db2014-09-23 13:39:36 -0700268 * @param uid Uid to check against for removal of a job.
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700269 * @param forceAll If true, all jobs for the uid will be canceled; if false, only those
270 * whose apps are stopped.
Christopher Tate7060b042014-06-09 19:50:00 -0700271 */
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700272 public void cancelJobsForUid(int uid, boolean forceAll) {
Matthew Williams48a30db2014-09-23 13:39:36 -0700273 List<JobStatus> jobsForUid;
Christopher Tate7060b042014-06-09 19:50:00 -0700274 synchronized (mJobs) {
Matthew Williams48a30db2014-09-23 13:39:36 -0700275 jobsForUid = mJobs.getJobsByUid(uid);
276 }
277 for (int i=0; i<jobsForUid.size(); i++) {
278 JobStatus toRemove = jobsForUid.get(i);
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700279 if (!forceAll) {
280 String packageName = toRemove.getServiceComponent().getPackageName();
281 try {
282 if (ActivityManagerNative.getDefault().getAppStartMode(uid, packageName)
283 != ActivityManager.APP_START_MODE_DISABLED) {
284 continue;
285 }
286 } catch (RemoteException e) {
287 }
288 }
Matthew Williams48a30db2014-09-23 13:39:36 -0700289 cancelJobImpl(toRemove);
Christopher Tate7060b042014-06-09 19:50:00 -0700290 }
291 }
292
293 /**
294 * Entry point from client to cancel the job corresponding to the jobId provided.
295 * This will remove the job from the master list, and cancel the job if it was staged for
296 * execution or being executed.
297 * @param uid Uid of the calling client.
298 * @param jobId Id of the job, provided at schedule-time.
299 */
300 public void cancelJob(int uid, int jobId) {
301 JobStatus toCancel;
302 synchronized (mJobs) {
303 toCancel = mJobs.getJobByUidAndJobId(uid, jobId);
Matthew Williams48a30db2014-09-23 13:39:36 -0700304 }
305 if (toCancel != null) {
306 cancelJobImpl(toCancel);
Christopher Tate7060b042014-06-09 19:50:00 -0700307 }
308 }
309
Matthew Williams48a30db2014-09-23 13:39:36 -0700310 private void cancelJobImpl(JobStatus cancelled) {
Matthew Williamsee410da2014-07-25 11:30:40 -0700311 if (DEBUG) {
312 Slog.d(TAG, "Cancelling: " + cancelled);
313 }
Christopher Tate7060b042014-06-09 19:50:00 -0700314 stopTrackingJob(cancelled);
Matthew Williams48a30db2014-09-23 13:39:36 -0700315 synchronized (mJobs) {
316 // Remove from pending queue.
317 mPendingJobs.remove(cancelled);
318 // Cancel if running.
Shreyas Basarge5db09082016-01-07 13:38:29 +0000319 stopJobOnServiceContextLocked(cancelled, JobParameters.REASON_CANCELED);
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800320 reportActive();
Matthew Williams48a30db2014-09-23 13:39:36 -0700321 }
Christopher Tate7060b042014-06-09 19:50:00 -0700322 }
323
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700324 void updateIdleMode(boolean enabled) {
325 boolean changed = false;
326 boolean rocking;
327 synchronized (mJobs) {
328 if (mDeviceIdleMode != enabled) {
329 changed = true;
330 }
331 rocking = mReadyToRock;
332 }
333 if (changed) {
334 if (rocking) {
335 for (int i=0; i<mControllers.size(); i++) {
336 mControllers.get(i).deviceIdleModeChanged(enabled);
337 }
338 }
339 synchronized (mJobs) {
340 mDeviceIdleMode = enabled;
341 if (enabled) {
342 // When becoming idle, make sure no jobs are actively running.
343 for (int i=0; i<mActiveServices.size(); i++) {
344 JobServiceContext jsc = mActiveServices.get(i);
345 final JobStatus executing = jsc.getRunningJob();
346 if (executing != null) {
Shreyas Basarge5db09082016-01-07 13:38:29 +0000347 jsc.cancelExecutingJob(JobParameters.REASON_DEVICE_IDLE);
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700348 }
349 }
350 } else {
351 // When coming out of idle, allow thing to start back up.
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800352 if (rocking) {
353 if (mLocalDeviceIdleController != null) {
354 if (!mReportedActive) {
355 mReportedActive = true;
356 mLocalDeviceIdleController.setJobsActive(true);
357 }
358 }
359 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700360 mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
361 }
362 }
363 }
364 }
365
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800366 void reportActive() {
Shreyas Basarge4cff8ac2015-12-10 21:32:52 +0000367 // active is true if pending queue contains jobs OR some job is running.
368 boolean active = mPendingJobs.size() > 0;
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800369 if (mPendingJobs.size() <= 0) {
370 for (int i=0; i<mActiveServices.size(); i++) {
371 JobServiceContext jsc = mActiveServices.get(i);
Shreyas Basarge5db09082016-01-07 13:38:29 +0000372 if (jsc.getRunningJob() != null) {
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800373 active = true;
374 break;
375 }
376 }
377 }
Shreyas Basarge4cff8ac2015-12-10 21:32:52 +0000378
379 if (mReportedActive != active) {
380 mReportedActive = active;
381 if (mLocalDeviceIdleController != null) {
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800382 mLocalDeviceIdleController.setJobsActive(active);
383 }
384 }
385 }
386
Christopher Tate7060b042014-06-09 19:50:00 -0700387 /**
388 * Initializes the system service.
389 * <p>
390 * Subclasses must define a single argument constructor that accepts the context
391 * and passes it to super.
392 * </p>
393 *
394 * @param context The system server context.
395 */
396 public JobSchedulerService(Context context) {
397 super(context);
398 // Create the controllers.
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700399 mControllers = new ArrayList<StateController>();
Christopher Tate7060b042014-06-09 19:50:00 -0700400 mControllers.add(ConnectivityController.get(this));
401 mControllers.add(TimeController.get(this));
402 mControllers.add(IdleController.get(this));
403 mControllers.add(BatteryController.get(this));
Amith Yamasanib0ff3222015-03-04 09:56:14 -0800404 mControllers.add(AppIdleController.get(this));
Christopher Tate7060b042014-06-09 19:50:00 -0700405
406 mHandler = new JobHandler(context.getMainLooper());
407 mJobSchedulerStub = new JobSchedulerStub();
Christopher Tate7060b042014-06-09 19:50:00 -0700408 mJobs = JobStore.initAndGet(this);
409 }
410
411 @Override
412 public void onStart() {
413 publishBinderService(Context.JOB_SCHEDULER_SERVICE, mJobSchedulerStub);
414 }
415
416 @Override
417 public void onBootPhase(int phase) {
418 if (PHASE_SYSTEM_SERVICES_READY == phase) {
Shreyas Basarge5db09082016-01-07 13:38:29 +0000419 // Register br for package removals and user removals.
Christopher Tate7060b042014-06-09 19:50:00 -0700420 final IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_REMOVED);
421 filter.addDataScheme("package");
422 getContext().registerReceiverAsUser(
423 mBroadcastReceiver, UserHandle.ALL, filter, null, null);
424 final IntentFilter userFilter = new IntentFilter(Intent.ACTION_USER_REMOVED);
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700425 userFilter.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700426 userFilter.addAction(PowerManager.ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED);
Christopher Tate7060b042014-06-09 19:50:00 -0700427 getContext().registerReceiverAsUser(
428 mBroadcastReceiver, UserHandle.ALL, userFilter, null, null);
Shreyas Basarge5db09082016-01-07 13:38:29 +0000429 mPowerManager = (PowerManager)getContext().getSystemService(Context.POWER_SERVICE);
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700430 try {
431 ActivityManagerNative.getDefault().registerUidObserver(mUidObserver,
432 ActivityManager.UID_OBSERVER_IDLE);
433 } catch (RemoteException e) {
434 // ignored; both services live in system_server
435 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700436 } else if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {
437 synchronized (mJobs) {
438 // Let's go!
439 mReadyToRock = true;
440 mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService(
441 BatteryStats.SERVICE_NAME));
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800442 mLocalDeviceIdleController
443 = LocalServices.getService(DeviceIdleController.LocalService.class);
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700444 // Create the "runners".
445 for (int i = 0; i < MAX_JOB_CONTEXTS_COUNT; i++) {
446 mActiveServices.add(
447 new JobServiceContext(this, mBatteryStats,
448 getContext().getMainLooper()));
449 }
450 // Attach jobs to their controllers.
451 ArraySet<JobStatus> jobs = mJobs.getJobs();
452 for (int i=0; i<jobs.size(); i++) {
453 JobStatus job = jobs.valueAt(i);
Christopher Tate4a79dae2014-07-18 17:01:40 -0700454 for (int controller=0; controller<mControllers.size(); controller++) {
Dianne Hackborn2bf51f42015-03-24 14:17:12 -0700455 mControllers.get(controller).deviceIdleModeChanged(mDeviceIdleMode);
Christopher Tate4a79dae2014-07-18 17:01:40 -0700456 mControllers.get(controller).maybeStartTrackingJob(job);
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700457 }
458 }
459 // GO GO GO!
460 mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
461 }
Christopher Tate7060b042014-06-09 19:50:00 -0700462 }
463 }
464
465 /**
466 * Called when we have a job status object that we need to insert in our
467 * {@link com.android.server.job.JobStore}, and make sure all the relevant controllers know
468 * about.
469 */
470 private void startTrackingJob(JobStatus jobStatus) {
471 boolean update;
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700472 boolean rocking;
Christopher Tate7060b042014-06-09 19:50:00 -0700473 synchronized (mJobs) {
474 update = mJobs.add(jobStatus);
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700475 rocking = mReadyToRock;
Christopher Tate7060b042014-06-09 19:50:00 -0700476 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700477 if (rocking) {
478 for (int i=0; i<mControllers.size(); i++) {
479 StateController controller = mControllers.get(i);
480 if (update) {
481 controller.maybeStopTrackingJob(jobStatus);
482 }
483 controller.maybeStartTrackingJob(jobStatus);
Christopher Tate7060b042014-06-09 19:50:00 -0700484 }
Christopher Tate7060b042014-06-09 19:50:00 -0700485 }
486 }
487
488 /**
489 * Called when we want to remove a JobStatus object that we've finished executing. Returns the
490 * object removed.
491 */
492 private boolean stopTrackingJob(JobStatus jobStatus) {
493 boolean removed;
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700494 boolean rocking;
Christopher Tate7060b042014-06-09 19:50:00 -0700495 synchronized (mJobs) {
496 // Remove from store as well as controllers.
497 removed = mJobs.remove(jobStatus);
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700498 rocking = mReadyToRock;
Christopher Tate7060b042014-06-09 19:50:00 -0700499 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700500 if (removed && rocking) {
501 for (int i=0; i<mControllers.size(); i++) {
502 StateController controller = mControllers.get(i);
Christopher Tate7060b042014-06-09 19:50:00 -0700503 controller.maybeStopTrackingJob(jobStatus);
504 }
505 }
506 return removed;
507 }
508
Shreyas Basarge5db09082016-01-07 13:38:29 +0000509 private boolean stopJobOnServiceContextLocked(JobStatus job, int reason) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700510 for (int i=0; i<mActiveServices.size(); i++) {
511 JobServiceContext jsc = mActiveServices.get(i);
Christopher Tate7060b042014-06-09 19:50:00 -0700512 final JobStatus executing = jsc.getRunningJob();
513 if (executing != null && executing.matches(job.getUid(), job.getJobId())) {
Shreyas Basarge5db09082016-01-07 13:38:29 +0000514 jsc.cancelExecutingJob(reason);
Christopher Tate7060b042014-06-09 19:50:00 -0700515 return true;
516 }
517 }
518 return false;
519 }
520
521 /**
522 * @param job JobStatus we are querying against.
523 * @return Whether or not the job represented by the status object is currently being run or
524 * is pending.
525 */
526 private boolean isCurrentlyActiveLocked(JobStatus job) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700527 for (int i=0; i<mActiveServices.size(); i++) {
528 JobServiceContext serviceContext = mActiveServices.get(i);
Christopher Tate7060b042014-06-09 19:50:00 -0700529 final JobStatus running = serviceContext.getRunningJob();
530 if (running != null && running.matches(job.getUid(), job.getJobId())) {
531 return true;
532 }
533 }
534 return false;
535 }
536
537 /**
Matthew Williams1bde39a2015-10-07 14:29:30 -0700538 * Reschedules the given job based on the job's backoff policy. It doesn't make sense to
539 * specify an override deadline on a failed job (the failed job will run even though it's not
540 * ready), so we reschedule it with {@link JobStatus#NO_LATEST_RUNTIME}, but specify that any
541 * ready job with {@link JobStatus#numFailures} > 0 will be executed.
542 *
Christopher Tate7060b042014-06-09 19:50:00 -0700543 * @param failureToReschedule Provided job status that we will reschedule.
544 * @return A newly instantiated JobStatus with the same constraints as the last job except
545 * with adjusted timing constraints.
Matthew Williams1bde39a2015-10-07 14:29:30 -0700546 *
547 * @see JobHandler#maybeQueueReadyJobsForExecutionLockedH
Christopher Tate7060b042014-06-09 19:50:00 -0700548 */
549 private JobStatus getRescheduleJobForFailure(JobStatus failureToReschedule) {
550 final long elapsedNowMillis = SystemClock.elapsedRealtime();
551 final JobInfo job = failureToReschedule.getJob();
552
553 final long initialBackoffMillis = job.getInitialBackoffMillis();
Matthew Williamsd1c06752014-08-22 14:15:28 -0700554 final int backoffAttempts = failureToReschedule.getNumFailures() + 1;
555 long delayMillis;
Christopher Tate7060b042014-06-09 19:50:00 -0700556
557 switch (job.getBackoffPolicy()) {
Matthew Williamsd1c06752014-08-22 14:15:28 -0700558 case JobInfo.BACKOFF_POLICY_LINEAR:
559 delayMillis = initialBackoffMillis * backoffAttempts;
Christopher Tate7060b042014-06-09 19:50:00 -0700560 break;
561 default:
562 if (DEBUG) {
563 Slog.v(TAG, "Unrecognised back-off policy, defaulting to exponential.");
564 }
Matthew Williamsd1c06752014-08-22 14:15:28 -0700565 case JobInfo.BACKOFF_POLICY_EXPONENTIAL:
566 delayMillis =
567 (long) Math.scalb(initialBackoffMillis, backoffAttempts - 1);
Christopher Tate7060b042014-06-09 19:50:00 -0700568 break;
569 }
Matthew Williamsd1c06752014-08-22 14:15:28 -0700570 delayMillis =
571 Math.min(delayMillis, JobInfo.MAX_BACKOFF_DELAY_MILLIS);
572 return new JobStatus(failureToReschedule, elapsedNowMillis + delayMillis,
573 JobStatus.NO_LATEST_RUNTIME, backoffAttempts);
Christopher Tate7060b042014-06-09 19:50:00 -0700574 }
575
576 /**
Matthew Williams1bde39a2015-10-07 14:29:30 -0700577 * Called after a periodic has executed so we can reschedule it. We take the last execution
578 * time of the job to be the time of completion (i.e. the time at which this function is
579 * called).
Christopher Tate7060b042014-06-09 19:50:00 -0700580 * This could be inaccurate b/c the job can run for as long as
581 * {@link com.android.server.job.JobServiceContext#EXECUTING_TIMESLICE_MILLIS}, but will lead
582 * to underscheduling at least, rather than if we had taken the last execution time to be the
583 * start of the execution.
584 * @return A new job representing the execution criteria for this instantiation of the
585 * recurring job.
586 */
587 private JobStatus getRescheduleJobForPeriodic(JobStatus periodicToReschedule) {
588 final long elapsedNow = SystemClock.elapsedRealtime();
589 // Compute how much of the period is remaining.
Matthew Williams1bde39a2015-10-07 14:29:30 -0700590 long runEarly = 0L;
591
592 // If this periodic was rescheduled it won't have a deadline.
593 if (periodicToReschedule.hasDeadlineConstraint()) {
594 runEarly = Math.max(periodicToReschedule.getLatestRunTimeElapsed() - elapsedNow, 0L);
595 }
Shreyas Basarge89ee6182015-12-17 15:16:36 +0000596 long flex = periodicToReschedule.getJob().getFlexMillis();
Christopher Tate7060b042014-06-09 19:50:00 -0700597 long period = periodicToReschedule.getJob().getIntervalMillis();
Shreyas Basarge89ee6182015-12-17 15:16:36 +0000598 long newLatestRuntimeElapsed = elapsedNow + runEarly + period;
599 long newEarliestRunTimeElapsed = newLatestRuntimeElapsed - flex;
Christopher Tate7060b042014-06-09 19:50:00 -0700600
601 if (DEBUG) {
602 Slog.v(TAG, "Rescheduling executed periodic. New execution window [" +
603 newEarliestRunTimeElapsed/1000 + ", " + newLatestRuntimeElapsed/1000 + "]s");
604 }
605 return new JobStatus(periodicToReschedule, newEarliestRunTimeElapsed,
606 newLatestRuntimeElapsed, 0 /* backoffAttempt */);
607 }
608
609 // JobCompletedListener implementations.
610
611 /**
612 * A job just finished executing. We fetch the
613 * {@link com.android.server.job.controllers.JobStatus} from the store and depending on
614 * whether we want to reschedule we readd it to the controllers.
615 * @param jobStatus Completed job.
616 * @param needsReschedule Whether the implementing class should reschedule this job.
617 */
618 @Override
619 public void onJobCompleted(JobStatus jobStatus, boolean needsReschedule) {
620 if (DEBUG) {
621 Slog.d(TAG, "Completed " + jobStatus + ", reschedule=" + needsReschedule);
622 }
623 if (!stopTrackingJob(jobStatus)) {
624 if (DEBUG) {
Matthew Williamsee410da2014-07-25 11:30:40 -0700625 Slog.d(TAG, "Could not find job to remove. Was job removed while executing?");
Christopher Tate7060b042014-06-09 19:50:00 -0700626 }
627 return;
628 }
629 if (needsReschedule) {
630 JobStatus rescheduled = getRescheduleJobForFailure(jobStatus);
631 startTrackingJob(rescheduled);
632 } else if (jobStatus.getJob().isPeriodic()) {
633 JobStatus rescheduledPeriodic = getRescheduleJobForPeriodic(jobStatus);
634 startTrackingJob(rescheduledPeriodic);
635 }
Shreyas Basarge4cff8ac2015-12-10 21:32:52 +0000636 reportActive();
637 mHandler.obtainMessage(MSG_CHECK_JOB_GREEDY).sendToTarget();
Christopher Tate7060b042014-06-09 19:50:00 -0700638 }
639
640 // StateChangedListener implementations.
641
642 /**
Matthew Williams48a30db2014-09-23 13:39:36 -0700643 * Posts a message to the {@link com.android.server.job.JobSchedulerService.JobHandler} that
644 * some controller's state has changed, so as to run through the list of jobs and start/stop
645 * any that are eligible.
Christopher Tate7060b042014-06-09 19:50:00 -0700646 */
647 @Override
648 public void onControllerStateChanged() {
Matthew Williams48a30db2014-09-23 13:39:36 -0700649 mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
Christopher Tate7060b042014-06-09 19:50:00 -0700650 }
651
652 @Override
653 public void onRunJobNow(JobStatus jobStatus) {
654 mHandler.obtainMessage(MSG_JOB_EXPIRED, jobStatus).sendToTarget();
655 }
656
Christopher Tate7060b042014-06-09 19:50:00 -0700657 private class JobHandler extends Handler {
658
659 public JobHandler(Looper looper) {
660 super(looper);
661 }
662
663 @Override
664 public void handleMessage(Message message) {
Matthew Williams48a30db2014-09-23 13:39:36 -0700665 synchronized (mJobs) {
666 if (!mReadyToRock) {
667 return;
668 }
669 }
Christopher Tate7060b042014-06-09 19:50:00 -0700670 switch (message.what) {
671 case MSG_JOB_EXPIRED:
672 synchronized (mJobs) {
673 JobStatus runNow = (JobStatus) message.obj;
Matthew Williamsbafeeb92014-08-08 11:51:06 -0700674 // runNow can be null, which is a controller's way of indicating that its
675 // state is such that all ready jobs should be run immediately.
Matthew Williams48a30db2014-09-23 13:39:36 -0700676 if (runNow != null && !mPendingJobs.contains(runNow)
677 && mJobs.containsJob(runNow)) {
Christopher Tate7060b042014-06-09 19:50:00 -0700678 mPendingJobs.add(runNow);
679 }
Matthew Williams48a30db2014-09-23 13:39:36 -0700680 queueReadyJobsForExecutionLockedH();
Christopher Tate7060b042014-06-09 19:50:00 -0700681 }
Christopher Tate7060b042014-06-09 19:50:00 -0700682 break;
683 case MSG_CHECK_JOB:
Matthew Williams48a30db2014-09-23 13:39:36 -0700684 synchronized (mJobs) {
Shreyas Basarge4cff8ac2015-12-10 21:32:52 +0000685 if (mReportedActive) {
686 // if jobs are currently being run, queue all ready jobs for execution.
687 queueReadyJobsForExecutionLockedH();
688 } else {
689 // Check the list of jobs and run some of them if we feel inclined.
690 maybeQueueReadyJobsForExecutionLockedH();
691 }
692 }
693 break;
694 case MSG_CHECK_JOB_GREEDY:
695 synchronized (mJobs) {
696 queueReadyJobsForExecutionLockedH();
Matthew Williams48a30db2014-09-23 13:39:36 -0700697 }
Christopher Tate7060b042014-06-09 19:50:00 -0700698 break;
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700699 case MSG_STOP_JOB:
700 cancelJobImpl((JobStatus)message.obj);
701 break;
Christopher Tate7060b042014-06-09 19:50:00 -0700702 }
703 maybeRunPendingJobsH();
704 // Don't remove JOB_EXPIRED in case one came along while processing the queue.
705 removeMessages(MSG_CHECK_JOB);
706 }
707
708 /**
709 * Run through list of jobs and execute all possible - at least one is expired so we do
710 * as many as we can.
711 */
Matthew Williams48a30db2014-09-23 13:39:36 -0700712 private void queueReadyJobsForExecutionLockedH() {
713 ArraySet<JobStatus> jobs = mJobs.getJobs();
Shreyas Basarge5db09082016-01-07 13:38:29 +0000714 mPendingJobs.clear();
Matthew Williams48a30db2014-09-23 13:39:36 -0700715 if (DEBUG) {
716 Slog.d(TAG, "queuing all ready jobs for execution:");
717 }
718 for (int i=0; i<jobs.size(); i++) {
719 JobStatus job = jobs.valueAt(i);
720 if (isReadyToBeExecutedLocked(job)) {
721 if (DEBUG) {
722 Slog.d(TAG, " queued " + job.toShortString());
Christopher Tate7060b042014-06-09 19:50:00 -0700723 }
Matthew Williams48a30db2014-09-23 13:39:36 -0700724 mPendingJobs.add(job);
Shreyas Basarge5db09082016-01-07 13:38:29 +0000725 } else if (areJobConstraintsNotSatisfied(job)) {
726 stopJobOnServiceContextLocked(job,
727 JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED);
Christopher Tate7060b042014-06-09 19:50:00 -0700728 }
Matthew Williams48a30db2014-09-23 13:39:36 -0700729 }
730 if (DEBUG) {
731 final int queuedJobs = mPendingJobs.size();
732 if (queuedJobs == 0) {
733 Slog.d(TAG, "No jobs pending.");
734 } else {
735 Slog.d(TAG, queuedJobs + " jobs queued.");
Matthew Williams75fc5252014-09-02 16:17:53 -0700736 }
Christopher Tate7060b042014-06-09 19:50:00 -0700737 }
738 }
739
740 /**
741 * The state of at least one job has changed. Here is where we could enforce various
742 * policies on when we want to execute jobs.
743 * Right now the policy is such:
744 * If >1 of the ready jobs is idle mode we send all of them off
745 * if more than 2 network connectivity jobs are ready we send them all off.
746 * If more than 4 jobs total are ready we send them all off.
747 * TODO: It would be nice to consolidate these sort of high-level policies somewhere.
748 */
Matthew Williams48a30db2014-09-23 13:39:36 -0700749 private void maybeQueueReadyJobsForExecutionLockedH() {
Shreyas Basarge5db09082016-01-07 13:38:29 +0000750 mPendingJobs.clear();
Matthew Williams48a30db2014-09-23 13:39:36 -0700751 int chargingCount = 0;
Shreyas Basarge5db09082016-01-07 13:38:29 +0000752 int idleCount = 0;
Matthew Williams48a30db2014-09-23 13:39:36 -0700753 int backoffCount = 0;
754 int connectivityCount = 0;
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700755 List<JobStatus> runnableJobs = null;
Matthew Williams48a30db2014-09-23 13:39:36 -0700756 ArraySet<JobStatus> jobs = mJobs.getJobs();
757 for (int i=0; i<jobs.size(); i++) {
758 JobStatus job = jobs.valueAt(i);
759 if (isReadyToBeExecutedLocked(job)) {
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700760 try {
761 if (ActivityManagerNative.getDefault().getAppStartMode(job.getUid(),
762 job.getJob().getService().getPackageName())
763 == ActivityManager.APP_START_MODE_DISABLED) {
764 Slog.w(TAG, "Aborting job " + job.getUid() + ":"
765 + job.getJob().toString() + " -- package not allowed to start");
766 mHandler.obtainMessage(MSG_STOP_JOB, job).sendToTarget();
767 continue;
768 }
769 } catch (RemoteException e) {
770 }
Matthew Williams48a30db2014-09-23 13:39:36 -0700771 if (job.getNumFailures() > 0) {
772 backoffCount++;
Christopher Tate7060b042014-06-09 19:50:00 -0700773 }
Matthew Williams48a30db2014-09-23 13:39:36 -0700774 if (job.hasIdleConstraint()) {
775 idleCount++;
776 }
777 if (job.hasConnectivityConstraint() || job.hasUnmeteredConstraint()) {
778 connectivityCount++;
779 }
780 if (job.hasChargingConstraint()) {
781 chargingCount++;
782 }
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700783 if (runnableJobs == null) {
784 runnableJobs = new ArrayList<>();
785 }
Matthew Williams48a30db2014-09-23 13:39:36 -0700786 runnableJobs.add(job);
Shreyas Basarge5db09082016-01-07 13:38:29 +0000787 } else if (areJobConstraintsNotSatisfied(job)) {
788 stopJobOnServiceContextLocked(job,
789 JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED);
Christopher Tate7060b042014-06-09 19:50:00 -0700790 }
Matthew Williams48a30db2014-09-23 13:39:36 -0700791 }
792 if (backoffCount > 0 ||
793 idleCount >= MIN_IDLE_COUNT ||
794 connectivityCount >= MIN_CONNECTIVITY_COUNT ||
795 chargingCount >= MIN_CHARGING_COUNT ||
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700796 (runnableJobs != null && runnableJobs.size() >= MIN_READY_JOBS_COUNT)) {
Matthew Williamsbe0c4172014-08-06 18:14:16 -0700797 if (DEBUG) {
Matthew Williams48a30db2014-09-23 13:39:36 -0700798 Slog.d(TAG, "maybeQueueReadyJobsForExecutionLockedH: Running jobs.");
Christopher Tate7060b042014-06-09 19:50:00 -0700799 }
Matthew Williams48a30db2014-09-23 13:39:36 -0700800 for (int i=0; i<runnableJobs.size(); i++) {
801 mPendingJobs.add(runnableJobs.get(i));
802 }
803 } else {
804 if (DEBUG) {
805 Slog.d(TAG, "maybeQueueReadyJobsForExecutionLockedH: Not running anything.");
806 }
807 }
Christopher Tate7060b042014-06-09 19:50:00 -0700808 }
809
810 /**
811 * Criteria for moving a job into the pending queue:
812 * - It's ready.
813 * - It's not pending.
814 * - It's not already running on a JSC.
Matthew Williams9ae3dbe2014-08-21 13:47:47 -0700815 * - The user that requested the job is running.
Christopher Tate7060b042014-06-09 19:50:00 -0700816 */
817 private boolean isReadyToBeExecutedLocked(JobStatus job) {
Matthew Williams9ae3dbe2014-08-21 13:47:47 -0700818 final boolean jobReady = job.isReady();
819 final boolean jobPending = mPendingJobs.contains(job);
820 final boolean jobActive = isCurrentlyActiveLocked(job);
Shreyas Basarge5db09082016-01-07 13:38:29 +0000821 final boolean userRunning = mStartedUsers.contains(job.getUserId());
Matthew Williams9ae3dbe2014-08-21 13:47:47 -0700822 if (DEBUG) {
823 Slog.v(TAG, "isReadyToBeExecutedLocked: " + job.toShortString()
824 + " ready=" + jobReady + " pending=" + jobPending
Shreyas Basarge5db09082016-01-07 13:38:29 +0000825 + " active=" + jobActive + " userRunning=" + userRunning);
Matthew Williams9ae3dbe2014-08-21 13:47:47 -0700826 }
Shreyas Basarge5db09082016-01-07 13:38:29 +0000827 return userRunning && jobReady && !jobPending && !jobActive;
Christopher Tate7060b042014-06-09 19:50:00 -0700828 }
829
830 /**
831 * Criteria for cancelling an active job:
832 * - It's not ready
833 * - It's running on a JSC.
834 */
Shreyas Basarge5db09082016-01-07 13:38:29 +0000835 private boolean areJobConstraintsNotSatisfied(JobStatus job) {
Christopher Tate7060b042014-06-09 19:50:00 -0700836 return !job.isReady() && isCurrentlyActiveLocked(job);
837 }
838
839 /**
840 * Reconcile jobs in the pending queue against available execution contexts.
841 * A controller can force a job into the pending queue even if it's already running, but
842 * here is where we decide whether to actually execute it.
843 */
844 private void maybeRunPendingJobsH() {
845 synchronized (mJobs) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700846 if (mDeviceIdleMode) {
847 // If device is idle, we will not schedule jobs to run.
848 return;
849 }
Matthew Williams75fc5252014-09-02 16:17:53 -0700850 if (DEBUG) {
851 Slog.d(TAG, "pending queue: " + mPendingJobs.size() + " jobs.");
852 }
Shreyas Basarge5db09082016-01-07 13:38:29 +0000853 assignJobsToContextsH();
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800854 reportActive();
Christopher Tate7060b042014-06-09 19:50:00 -0700855 }
856 }
857 }
858
859 /**
Shreyas Basarge5db09082016-01-07 13:38:29 +0000860 * Takes jobs from pending queue and runs them on available contexts.
861 * If no contexts are available, preempts lower priority jobs to
862 * run higher priority ones.
863 * Lock on mJobs before calling this function.
864 */
865 private void assignJobsToContextsH() {
866 if (DEBUG) {
867 Slog.d(TAG, printPendingQueue());
868 }
869
870 // This array essentially stores the state of mActiveServices array.
871 // ith index stores the job present on the ith JobServiceContext.
872 // We manipulate this array until we arrive at what jobs should be running on
873 // what JobServiceContext.
874 JobStatus[] contextIdToJobMap = new JobStatus[MAX_JOB_CONTEXTS_COUNT];
875 // Indicates whether we need to act on this jobContext id
876 boolean[] act = new boolean[MAX_JOB_CONTEXTS_COUNT];
877 int[] preferredUidForContext = new int[MAX_JOB_CONTEXTS_COUNT];
878 for (int i=0; i<mActiveServices.size(); i++) {
879 contextIdToJobMap[i] = mActiveServices.get(i).getRunningJob();
880 preferredUidForContext[i] = mActiveServices.get(i).getPreferredUid();
881 }
882 if (DEBUG) {
883 Slog.d(TAG, printContextIdToJobMap(contextIdToJobMap, "running jobs initial"));
884 }
885 Iterator<JobStatus> it = mPendingJobs.iterator();
886 while (it.hasNext()) {
887 JobStatus nextPending = it.next();
888
889 // If job is already running, go to next job.
890 int jobRunningContext = findJobContextIdFromMap(nextPending, contextIdToJobMap);
891 if (jobRunningContext != -1) {
892 continue;
893 }
894
895 // Find a context for nextPending. The context should be available OR
896 // it should have lowest priority among all running jobs
897 // (sharing the same Uid as nextPending)
898 int minPriority = Integer.MAX_VALUE;
899 int minPriorityContextId = -1;
900 for (int i=0; i<mActiveServices.size(); i++) {
901 JobStatus job = contextIdToJobMap[i];
902 int preferredUid = preferredUidForContext[i];
903 if (job == null && (preferredUid == nextPending.getUid() ||
904 preferredUid == JobServiceContext.NO_PREFERRED_UID) ) {
905 minPriorityContextId = i;
906 break;
907 }
Shreyas Basarge347c2782016-01-15 18:24:36 +0000908 if (job == null) {
909 // No job on this context, but nextPending can't run here because
910 // the context has a preferred Uid.
911 continue;
912 }
Shreyas Basarge5db09082016-01-07 13:38:29 +0000913 if (job.getUid() != nextPending.getUid()) {
914 continue;
915 }
916 if (job.getPriority() >= nextPending.getPriority()) {
917 continue;
918 }
919 if (minPriority > nextPending.getPriority()) {
920 minPriority = nextPending.getPriority();
921 minPriorityContextId = i;
922 }
923 }
924 if (minPriorityContextId != -1) {
925 contextIdToJobMap[minPriorityContextId] = nextPending;
926 act[minPriorityContextId] = true;
927 }
928 }
929 if (DEBUG) {
930 Slog.d(TAG, printContextIdToJobMap(contextIdToJobMap, "running jobs final"));
931 }
932 for (int i=0; i<mActiveServices.size(); i++) {
933 boolean preservePreferredUid = false;
934 if (act[i]) {
935 JobStatus js = mActiveServices.get(i).getRunningJob();
936 if (js != null) {
937 if (DEBUG) {
938 Slog.d(TAG, "preempting job: " + mActiveServices.get(i).getRunningJob());
939 }
940 // preferredUid will be set to uid of currently running job.
941 mActiveServices.get(i).preemptExecutingJob();
942 preservePreferredUid = true;
943 } else {
944 if (DEBUG) {
945 Slog.d(TAG, "About to run job on context "
946 + String.valueOf(i) + ", job: " + contextIdToJobMap[i]);
947 }
948 if (!mActiveServices.get(i).executeRunnableJob(contextIdToJobMap[i])) {
949 Slog.d(TAG, "Error executing " + contextIdToJobMap[i]);
950 }
951 mPendingJobs.remove(contextIdToJobMap[i]);
952 }
953 }
954 if (!preservePreferredUid) {
955 mActiveServices.get(i).clearPreferredUid();
956 }
957 }
958 }
959
960 int findJobContextIdFromMap(JobStatus jobStatus, JobStatus[] map) {
961 for (int i=0; i<map.length; i++) {
962 if (map[i] != null && map[i].matches(jobStatus.getUid(), jobStatus.getJobId())) {
963 return i;
964 }
965 }
966 return -1;
967 }
968
969 /**
Christopher Tate7060b042014-06-09 19:50:00 -0700970 * Binder stub trampoline implementation
971 */
972 final class JobSchedulerStub extends IJobScheduler.Stub {
973 /** Cache determination of whether a given app can persist jobs
974 * key is uid of the calling app; value is undetermined/true/false
975 */
976 private final SparseArray<Boolean> mPersistCache = new SparseArray<Boolean>();
977
978 // Enforce that only the app itself (or shared uid participant) can schedule a
979 // job that runs one of the app's services, as well as verifying that the
980 // named service properly requires the BIND_JOB_SERVICE permission
981 private void enforceValidJobRequest(int uid, JobInfo job) {
Christopher Tate5568f542014-06-18 13:53:31 -0700982 final IPackageManager pm = AppGlobals.getPackageManager();
Christopher Tate7060b042014-06-09 19:50:00 -0700983 final ComponentName service = job.getService();
984 try {
Shreyas Basarge5db09082016-01-07 13:38:29 +0000985 ServiceInfo si = pm.getServiceInfo(service, 0, UserHandle.getUserId(uid));
Christopher Tate5568f542014-06-18 13:53:31 -0700986 if (si == null) {
987 throw new IllegalArgumentException("No such service " + service);
988 }
Christopher Tate7060b042014-06-09 19:50:00 -0700989 if (si.applicationInfo.uid != uid) {
990 throw new IllegalArgumentException("uid " + uid +
991 " cannot schedule job in " + service.getPackageName());
992 }
993 if (!JobService.PERMISSION_BIND.equals(si.permission)) {
994 throw new IllegalArgumentException("Scheduled service " + service
995 + " does not require android.permission.BIND_JOB_SERVICE permission");
996 }
Christopher Tate5568f542014-06-18 13:53:31 -0700997 } catch (RemoteException e) {
998 // Can't happen; the Package Manager is in this same process
Christopher Tate7060b042014-06-09 19:50:00 -0700999 }
1000 }
1001
1002 private boolean canPersistJobs(int pid, int uid) {
1003 // If we get this far we're good to go; all we need to do now is check
1004 // whether the app is allowed to persist its scheduled work.
1005 final boolean canPersist;
1006 synchronized (mPersistCache) {
1007 Boolean cached = mPersistCache.get(uid);
1008 if (cached != null) {
1009 canPersist = cached.booleanValue();
1010 } else {
1011 // Persisting jobs is tantamount to running at boot, so we permit
1012 // it when the app has declared that it uses the RECEIVE_BOOT_COMPLETED
1013 // permission
1014 int result = getContext().checkPermission(
1015 android.Manifest.permission.RECEIVE_BOOT_COMPLETED, pid, uid);
1016 canPersist = (result == PackageManager.PERMISSION_GRANTED);
1017 mPersistCache.put(uid, canPersist);
1018 }
1019 }
1020 return canPersist;
1021 }
1022
1023 // IJobScheduler implementation
1024 @Override
1025 public int schedule(JobInfo job) throws RemoteException {
1026 if (DEBUG) {
Matthew Williamsee410da2014-07-25 11:30:40 -07001027 Slog.d(TAG, "Scheduling job: " + job.toString());
Christopher Tate7060b042014-06-09 19:50:00 -07001028 }
1029 final int pid = Binder.getCallingPid();
1030 final int uid = Binder.getCallingUid();
1031
1032 enforceValidJobRequest(uid, job);
Matthew Williams900c67f2014-07-09 12:46:53 -07001033 if (job.isPersisted()) {
1034 if (!canPersistJobs(pid, uid)) {
1035 throw new IllegalArgumentException("Error: requested job be persisted without"
1036 + " holding RECEIVE_BOOT_COMPLETED permission.");
1037 }
1038 }
Christopher Tate7060b042014-06-09 19:50:00 -07001039
1040 long ident = Binder.clearCallingIdentity();
1041 try {
Matthew Williams900c67f2014-07-09 12:46:53 -07001042 return JobSchedulerService.this.schedule(job, uid);
Christopher Tate7060b042014-06-09 19:50:00 -07001043 } finally {
1044 Binder.restoreCallingIdentity(ident);
1045 }
1046 }
1047
1048 @Override
1049 public List<JobInfo> getAllPendingJobs() throws RemoteException {
1050 final int uid = Binder.getCallingUid();
1051
1052 long ident = Binder.clearCallingIdentity();
1053 try {
1054 return JobSchedulerService.this.getPendingJobs(uid);
1055 } finally {
1056 Binder.restoreCallingIdentity(ident);
1057 }
1058 }
1059
1060 @Override
1061 public void cancelAll() throws RemoteException {
1062 final int uid = Binder.getCallingUid();
1063
1064 long ident = Binder.clearCallingIdentity();
1065 try {
Dianne Hackbornbef28fe2015-10-29 17:57:11 -07001066 JobSchedulerService.this.cancelJobsForUid(uid, true);
Christopher Tate7060b042014-06-09 19:50:00 -07001067 } finally {
1068 Binder.restoreCallingIdentity(ident);
1069 }
1070 }
1071
1072 @Override
1073 public void cancel(int jobId) throws RemoteException {
1074 final int uid = Binder.getCallingUid();
1075
1076 long ident = Binder.clearCallingIdentity();
1077 try {
1078 JobSchedulerService.this.cancelJob(uid, jobId);
1079 } finally {
1080 Binder.restoreCallingIdentity(ident);
1081 }
1082 }
1083
1084 /**
1085 * "dumpsys" infrastructure
1086 */
1087 @Override
1088 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1089 getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
1090
1091 long identityToken = Binder.clearCallingIdentity();
1092 try {
1093 JobSchedulerService.this.dumpInternal(pw);
1094 } finally {
1095 Binder.restoreCallingIdentity(identityToken);
1096 }
1097 }
Shreyas Basarge5db09082016-01-07 13:38:29 +00001098 };
1099
1100 private String printContextIdToJobMap(JobStatus[] map, String initial) {
1101 StringBuilder s = new StringBuilder(initial + ": ");
1102 for (int i=0; i<map.length; i++) {
1103 s.append("(")
1104 .append(map[i] == null? -1: map[i].getJobId())
1105 .append(map[i] == null? -1: map[i].getUid())
1106 .append(")" );
1107 }
1108 return s.toString();
1109 }
1110
1111 private String printPendingQueue() {
1112 StringBuilder s = new StringBuilder("Pending queue: ");
1113 Iterator<JobStatus> it = mPendingJobs.iterator();
1114 while (it.hasNext()) {
1115 JobStatus js = it.next();
1116 s.append("(")
1117 .append(js.getJob().getId())
1118 .append(", ")
1119 .append(js.getUid())
1120 .append(") ");
1121 }
1122 return s.toString();
Jeff Sharkey5217cac2015-12-20 15:34:01 -07001123 }
Christopher Tate7060b042014-06-09 19:50:00 -07001124
1125 void dumpInternal(PrintWriter pw) {
Christopher Tatef973a7b2014-08-29 12:54:08 -07001126 final long now = SystemClock.elapsedRealtime();
Christopher Tate7060b042014-06-09 19:50:00 -07001127 synchronized (mJobs) {
Shreyas Basarge5db09082016-01-07 13:38:29 +00001128 pw.print("Started users: ");
1129 for (int i=0; i<mStartedUsers.size(); i++) {
1130 pw.print("u" + mStartedUsers.get(i) + " ");
1131 }
1132 pw.println();
Christopher Tate7060b042014-06-09 19:50:00 -07001133 pw.println("Registered jobs:");
1134 if (mJobs.size() > 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001135 ArraySet<JobStatus> jobs = mJobs.getJobs();
1136 for (int i=0; i<jobs.size(); i++) {
1137 JobStatus job = jobs.valueAt(i);
Christopher Tate7060b042014-06-09 19:50:00 -07001138 job.dump(pw, " ");
1139 }
1140 } else {
Christopher Tatef973a7b2014-08-29 12:54:08 -07001141 pw.println(" None.");
Christopher Tate7060b042014-06-09 19:50:00 -07001142 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001143 for (int i=0; i<mControllers.size(); i++) {
Christopher Tate7060b042014-06-09 19:50:00 -07001144 pw.println();
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001145 mControllers.get(i).dumpControllerState(pw);
Christopher Tate7060b042014-06-09 19:50:00 -07001146 }
1147 pw.println();
Shreyas Basarge5db09082016-01-07 13:38:29 +00001148 pw.println(printPendingQueue());
Christopher Tate7060b042014-06-09 19:50:00 -07001149 pw.println();
1150 pw.println("Active jobs:");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001151 for (int i=0; i<mActiveServices.size(); i++) {
1152 JobServiceContext jsc = mActiveServices.get(i);
Shreyas Basarge5db09082016-01-07 13:38:29 +00001153 if (jsc.getRunningJob() == null) {
Christopher Tate7060b042014-06-09 19:50:00 -07001154 continue;
1155 } else {
Christopher Tatef973a7b2014-08-29 12:54:08 -07001156 final long timeout = jsc.getTimeoutElapsed();
1157 pw.print("Running for: ");
1158 pw.print((now - jsc.getExecutionStartTimeElapsed())/1000);
1159 pw.print("s timeout=");
1160 pw.print(timeout);
1161 pw.print(" fromnow=");
1162 pw.println(timeout-now);
1163 jsc.getRunningJob().dump(pw, " ");
Christopher Tate7060b042014-06-09 19:50:00 -07001164 }
1165 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001166 pw.println();
1167 pw.print("mReadyToRock="); pw.println(mReadyToRock);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001168 pw.print("mDeviceIdleMode="); pw.println(mDeviceIdleMode);
Dianne Hackborn627dfa12015-11-11 18:10:30 -08001169 pw.print("mReportedActive="); pw.println(mReportedActive);
Christopher Tate7060b042014-06-09 19:50:00 -07001170 }
1171 pw.println();
1172 }
1173}