blob: 83d6986a410f7b7e58ca71d9f9806a0a5c83d50d [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
19import java.io.FileDescriptor;
20import java.io.PrintWriter;
21import java.util.ArrayList;
22import java.util.Iterator;
23import java.util.List;
24
Christopher Tate5568f542014-06-18 13:53:31 -070025import android.app.AppGlobals;
Christopher Tate7060b042014-06-09 19:50:00 -070026import android.app.job.JobInfo;
27import android.app.job.JobScheduler;
28import android.app.job.JobService;
29import android.app.job.IJobScheduler;
30import android.content.BroadcastReceiver;
31import android.content.ComponentName;
32import android.content.Context;
33import android.content.Intent;
34import android.content.IntentFilter;
Christopher Tate5568f542014-06-18 13:53:31 -070035import android.content.pm.IPackageManager;
Christopher Tate7060b042014-06-09 19:50:00 -070036import android.content.pm.PackageManager;
Christopher Tate7060b042014-06-09 19:50:00 -070037import android.content.pm.ServiceInfo;
Dianne Hackbornfdb19562014-07-11 16:03:36 -070038import android.os.BatteryStats;
Christopher Tate7060b042014-06-09 19:50:00 -070039import android.os.Binder;
40import android.os.Handler;
41import android.os.Looper;
42import android.os.Message;
43import android.os.RemoteException;
Dianne Hackbornfdb19562014-07-11 16:03:36 -070044import android.os.ServiceManager;
Christopher Tate7060b042014-06-09 19:50:00 -070045import android.os.SystemClock;
46import android.os.UserHandle;
Dianne Hackbornfdb19562014-07-11 16:03:36 -070047import android.util.ArraySet;
Christopher Tate7060b042014-06-09 19:50:00 -070048import android.util.Slog;
49import android.util.SparseArray;
50
Dianne Hackbornfdb19562014-07-11 16:03:36 -070051import com.android.internal.app.IBatteryStats;
Christopher Tate7060b042014-06-09 19:50:00 -070052import com.android.server.job.controllers.BatteryController;
53import com.android.server.job.controllers.ConnectivityController;
54import com.android.server.job.controllers.IdleController;
55import com.android.server.job.controllers.JobStatus;
56import com.android.server.job.controllers.StateController;
57import com.android.server.job.controllers.TimeController;
58
Christopher Tate7060b042014-06-09 19:50:00 -070059/**
60 * Responsible for taking jobs representing work to be performed by a client app, and determining
61 * based on the criteria specified when that job should be run against the client application's
62 * endpoint.
63 * Implements logic for scheduling, and rescheduling jobs. The JobSchedulerService knows nothing
64 * about constraints, or the state of active jobs. It receives callbacks from the various
65 * controllers and completed jobs and operates accordingly.
66 *
67 * Note on locking: Any operations that manipulate {@link #mJobs} need to lock on that object.
68 * Any function with the suffix 'Locked' also needs to lock on {@link #mJobs}.
69 * @hide
70 */
71public class JobSchedulerService extends com.android.server.SystemService
Matthew Williams01ac45b2014-07-22 20:44:12 -070072 implements StateChangedListener, JobCompletedListener {
Georgi Nikolov326f2302014-10-08 11:50:50 -070073 static final boolean DEBUG = false;
Christopher Tate7060b042014-06-09 19:50:00 -070074 /** The number of concurrent jobs we run at one time. */
75 private static final int MAX_JOB_CONTEXTS_COUNT = 3;
Matthew Williamsbe0c4172014-08-06 18:14:16 -070076 static final String TAG = "JobSchedulerService";
Christopher Tate7060b042014-06-09 19:50:00 -070077 /** Master list of jobs. */
Dianne Hackbornfdb19562014-07-11 16:03:36 -070078 final JobStore mJobs;
Christopher Tate7060b042014-06-09 19:50:00 -070079
80 static final int MSG_JOB_EXPIRED = 0;
81 static final int MSG_CHECK_JOB = 1;
82
83 // Policy constants
84 /**
85 * Minimum # of idle jobs that must be ready in order to force the JMS to schedule things
86 * early.
87 */
Dianne Hackbornfdb19562014-07-11 16:03:36 -070088 static final int MIN_IDLE_COUNT = 1;
Christopher Tate7060b042014-06-09 19:50:00 -070089 /**
Matthew Williamsbe0c4172014-08-06 18:14:16 -070090 * Minimum # of charging jobs that must be ready in order to force the JMS to schedule things
91 * early.
92 */
93 static final int MIN_CHARGING_COUNT = 1;
94 /**
Christopher Tate7060b042014-06-09 19:50:00 -070095 * Minimum # of connectivity jobs that must be ready in order to force the JMS to schedule
96 * things early.
97 */
Dianne Hackbornfdb19562014-07-11 16:03:36 -070098 static final int MIN_CONNECTIVITY_COUNT = 2;
Christopher Tate7060b042014-06-09 19:50:00 -070099 /**
100 * Minimum # of jobs (with no particular constraints) for which the JMS will be happy running
101 * some work early.
Matthew Williamsbe0c4172014-08-06 18:14:16 -0700102 * This is correlated with the amount of batching we'll be able to do.
Christopher Tate7060b042014-06-09 19:50:00 -0700103 */
Matthew Williamsbe0c4172014-08-06 18:14:16 -0700104 static final int MIN_READY_JOBS_COUNT = 2;
Christopher Tate7060b042014-06-09 19:50:00 -0700105
106 /**
107 * Track Services that have currently active or pending jobs. The index is provided by
108 * {@link JobStatus#getServiceToken()}
109 */
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700110 final List<JobServiceContext> mActiveServices = new ArrayList<JobServiceContext>();
Christopher Tate7060b042014-06-09 19:50:00 -0700111 /** List of controllers that will notify this service of updates to jobs. */
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700112 List<StateController> mControllers;
Christopher Tate7060b042014-06-09 19:50:00 -0700113 /**
114 * Queue of pending jobs. The JobServiceContext class will receive jobs from this list
115 * when ready to execute them.
116 */
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700117 final ArrayList<JobStatus> mPendingJobs = new ArrayList<JobStatus>();
Christopher Tate7060b042014-06-09 19:50:00 -0700118
Matthew Williams9ae3dbe2014-08-21 13:47:47 -0700119 final ArrayList<Integer> mStartedUsers = new ArrayList();
120
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700121 final JobHandler mHandler;
122 final JobSchedulerStub mJobSchedulerStub;
123
124 IBatteryStats mBatteryStats;
125
126 /**
127 * Set to true once we are allowed to run third party apps.
128 */
129 boolean mReadyToRock;
130
Christopher Tate7060b042014-06-09 19:50:00 -0700131 /**
132 * Cleans up outstanding jobs when a package is removed. Even if it's being replaced later we
133 * still clean up. On reinstall the package will have a new uid.
134 */
135 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
136 @Override
137 public void onReceive(Context context, Intent intent) {
138 Slog.d(TAG, "Receieved: " + intent.getAction());
139 if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) {
Christopher Tateaad67a32014-10-20 16:29:20 -0700140 // If this is an outright uninstall rather than the first half of an
141 // app update sequence, cancel the jobs associated with the app.
142 if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
143 int uidRemoved = intent.getIntExtra(Intent.EXTRA_UID, -1);
144 if (DEBUG) {
145 Slog.d(TAG, "Removing jobs for uid: " + uidRemoved);
146 }
147 cancelJobsForUid(uidRemoved);
Christopher Tate7060b042014-06-09 19:50:00 -0700148 }
Christopher Tate7060b042014-06-09 19:50:00 -0700149 } else if (Intent.ACTION_USER_REMOVED.equals(intent.getAction())) {
150 final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0);
151 if (DEBUG) {
152 Slog.d(TAG, "Removing jobs for user: " + userId);
153 }
154 cancelJobsForUser(userId);
155 }
156 }
157 };
158
Matthew Williams9ae3dbe2014-08-21 13:47:47 -0700159 @Override
160 public void onStartUser(int userHandle) {
161 mStartedUsers.add(userHandle);
162 // Let's kick any outstanding jobs for this user.
163 mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
164 }
165
166 @Override
167 public void onStopUser(int userHandle) {
168 mStartedUsers.remove(Integer.valueOf(userHandle));
169 }
170
Christopher Tate7060b042014-06-09 19:50:00 -0700171 /**
172 * Entry point from client to schedule the provided job.
173 * This cancels the job if it's already been scheduled, and replaces it with the one provided.
174 * @param job JobInfo object containing execution parameters
175 * @param uId The package identifier of the application this job is for.
Christopher Tate7060b042014-06-09 19:50:00 -0700176 * @return Result of this operation. See <code>JobScheduler#RESULT_*</code> return codes.
177 */
Matthew Williams900c67f2014-07-09 12:46:53 -0700178 public int schedule(JobInfo job, int uId) {
179 JobStatus jobStatus = new JobStatus(job, uId);
Christopher Tate7060b042014-06-09 19:50:00 -0700180 cancelJob(uId, job.getId());
181 startTrackingJob(jobStatus);
Matthew Williamsbafeeb92014-08-08 11:51:06 -0700182 mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
Christopher Tate7060b042014-06-09 19:50:00 -0700183 return JobScheduler.RESULT_SUCCESS;
184 }
185
186 public List<JobInfo> getPendingJobs(int uid) {
187 ArrayList<JobInfo> outList = new ArrayList<JobInfo>();
188 synchronized (mJobs) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700189 ArraySet<JobStatus> jobs = mJobs.getJobs();
190 for (int i=0; i<jobs.size(); i++) {
191 JobStatus job = jobs.valueAt(i);
Christopher Tate7060b042014-06-09 19:50:00 -0700192 if (job.getUid() == uid) {
193 outList.add(job.getJob());
194 }
195 }
196 }
197 return outList;
198 }
199
200 private void cancelJobsForUser(int userHandle) {
Matthew Williams48a30db2014-09-23 13:39:36 -0700201 List<JobStatus> jobsForUser;
Christopher Tate7060b042014-06-09 19:50:00 -0700202 synchronized (mJobs) {
Matthew Williams48a30db2014-09-23 13:39:36 -0700203 jobsForUser = mJobs.getJobsByUser(userHandle);
204 }
205 for (int i=0; i<jobsForUser.size(); i++) {
206 JobStatus toRemove = jobsForUser.get(i);
207 cancelJobImpl(toRemove);
Christopher Tate7060b042014-06-09 19:50:00 -0700208 }
209 }
210
211 /**
212 * Entry point from client to cancel all jobs originating from their uid.
213 * This will remove the job from the master list, and cancel the job if it was staged for
214 * execution or being executed.
Matthew Williams48a30db2014-09-23 13:39:36 -0700215 * @param uid Uid to check against for removal of a job.
Christopher Tate7060b042014-06-09 19:50:00 -0700216 */
217 public void cancelJobsForUid(int uid) {
Matthew Williams48a30db2014-09-23 13:39:36 -0700218 List<JobStatus> jobsForUid;
Christopher Tate7060b042014-06-09 19:50:00 -0700219 synchronized (mJobs) {
Matthew Williams48a30db2014-09-23 13:39:36 -0700220 jobsForUid = mJobs.getJobsByUid(uid);
221 }
222 for (int i=0; i<jobsForUid.size(); i++) {
223 JobStatus toRemove = jobsForUid.get(i);
224 cancelJobImpl(toRemove);
Christopher Tate7060b042014-06-09 19:50:00 -0700225 }
226 }
227
228 /**
229 * Entry point from client to cancel the job corresponding to the jobId provided.
230 * This will remove the job from the master list, and cancel the job if it was staged for
231 * execution or being executed.
232 * @param uid Uid of the calling client.
233 * @param jobId Id of the job, provided at schedule-time.
234 */
235 public void cancelJob(int uid, int jobId) {
236 JobStatus toCancel;
237 synchronized (mJobs) {
238 toCancel = mJobs.getJobByUidAndJobId(uid, jobId);
Matthew Williams48a30db2014-09-23 13:39:36 -0700239 }
240 if (toCancel != null) {
241 cancelJobImpl(toCancel);
Christopher Tate7060b042014-06-09 19:50:00 -0700242 }
243 }
244
Matthew Williams48a30db2014-09-23 13:39:36 -0700245 private void cancelJobImpl(JobStatus cancelled) {
Matthew Williamsee410da2014-07-25 11:30:40 -0700246 if (DEBUG) {
247 Slog.d(TAG, "Cancelling: " + cancelled);
248 }
Christopher Tate7060b042014-06-09 19:50:00 -0700249 stopTrackingJob(cancelled);
Matthew Williams48a30db2014-09-23 13:39:36 -0700250 synchronized (mJobs) {
251 // Remove from pending queue.
252 mPendingJobs.remove(cancelled);
253 // Cancel if running.
254 stopJobOnServiceContextLocked(cancelled);
255 }
Christopher Tate7060b042014-06-09 19:50:00 -0700256 }
257
258 /**
259 * Initializes the system service.
260 * <p>
261 * Subclasses must define a single argument constructor that accepts the context
262 * and passes it to super.
263 * </p>
264 *
265 * @param context The system server context.
266 */
267 public JobSchedulerService(Context context) {
268 super(context);
269 // Create the controllers.
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700270 mControllers = new ArrayList<StateController>();
Christopher Tate7060b042014-06-09 19:50:00 -0700271 mControllers.add(ConnectivityController.get(this));
272 mControllers.add(TimeController.get(this));
273 mControllers.add(IdleController.get(this));
274 mControllers.add(BatteryController.get(this));
275
276 mHandler = new JobHandler(context.getMainLooper());
277 mJobSchedulerStub = new JobSchedulerStub();
Christopher Tate7060b042014-06-09 19:50:00 -0700278 mJobs = JobStore.initAndGet(this);
279 }
280
281 @Override
282 public void onStart() {
283 publishBinderService(Context.JOB_SCHEDULER_SERVICE, mJobSchedulerStub);
284 }
285
286 @Override
287 public void onBootPhase(int phase) {
288 if (PHASE_SYSTEM_SERVICES_READY == phase) {
289 // Register br for package removals and user removals.
290 final IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_REMOVED);
291 filter.addDataScheme("package");
292 getContext().registerReceiverAsUser(
293 mBroadcastReceiver, UserHandle.ALL, filter, null, null);
294 final IntentFilter userFilter = new IntentFilter(Intent.ACTION_USER_REMOVED);
295 getContext().registerReceiverAsUser(
296 mBroadcastReceiver, UserHandle.ALL, userFilter, null, null);
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700297 } else if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {
298 synchronized (mJobs) {
299 // Let's go!
300 mReadyToRock = true;
301 mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService(
302 BatteryStats.SERVICE_NAME));
303 // Create the "runners".
304 for (int i = 0; i < MAX_JOB_CONTEXTS_COUNT; i++) {
305 mActiveServices.add(
306 new JobServiceContext(this, mBatteryStats,
307 getContext().getMainLooper()));
308 }
309 // Attach jobs to their controllers.
310 ArraySet<JobStatus> jobs = mJobs.getJobs();
311 for (int i=0; i<jobs.size(); i++) {
312 JobStatus job = jobs.valueAt(i);
Christopher Tate4a79dae2014-07-18 17:01:40 -0700313 for (int controller=0; controller<mControllers.size(); controller++) {
314 mControllers.get(controller).maybeStartTrackingJob(job);
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700315 }
316 }
317 // GO GO GO!
318 mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
319 }
Christopher Tate7060b042014-06-09 19:50:00 -0700320 }
321 }
322
323 /**
324 * Called when we have a job status object that we need to insert in our
325 * {@link com.android.server.job.JobStore}, and make sure all the relevant controllers know
326 * about.
327 */
328 private void startTrackingJob(JobStatus jobStatus) {
329 boolean update;
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700330 boolean rocking;
Christopher Tate7060b042014-06-09 19:50:00 -0700331 synchronized (mJobs) {
332 update = mJobs.add(jobStatus);
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700333 rocking = mReadyToRock;
Christopher Tate7060b042014-06-09 19:50:00 -0700334 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700335 if (rocking) {
336 for (int i=0; i<mControllers.size(); i++) {
337 StateController controller = mControllers.get(i);
338 if (update) {
339 controller.maybeStopTrackingJob(jobStatus);
340 }
341 controller.maybeStartTrackingJob(jobStatus);
Christopher Tate7060b042014-06-09 19:50:00 -0700342 }
Christopher Tate7060b042014-06-09 19:50:00 -0700343 }
344 }
345
346 /**
347 * Called when we want to remove a JobStatus object that we've finished executing. Returns the
348 * object removed.
349 */
350 private boolean stopTrackingJob(JobStatus jobStatus) {
351 boolean removed;
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700352 boolean rocking;
Christopher Tate7060b042014-06-09 19:50:00 -0700353 synchronized (mJobs) {
354 // Remove from store as well as controllers.
355 removed = mJobs.remove(jobStatus);
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700356 rocking = mReadyToRock;
Christopher Tate7060b042014-06-09 19:50:00 -0700357 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700358 if (removed && rocking) {
359 for (int i=0; i<mControllers.size(); i++) {
360 StateController controller = mControllers.get(i);
Christopher Tate7060b042014-06-09 19:50:00 -0700361 controller.maybeStopTrackingJob(jobStatus);
362 }
363 }
364 return removed;
365 }
366
367 private boolean stopJobOnServiceContextLocked(JobStatus job) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700368 for (int i=0; i<mActiveServices.size(); i++) {
369 JobServiceContext jsc = mActiveServices.get(i);
Christopher Tate7060b042014-06-09 19:50:00 -0700370 final JobStatus executing = jsc.getRunningJob();
371 if (executing != null && executing.matches(job.getUid(), job.getJobId())) {
372 jsc.cancelExecutingJob();
373 return true;
374 }
375 }
376 return false;
377 }
378
379 /**
380 * @param job JobStatus we are querying against.
381 * @return Whether or not the job represented by the status object is currently being run or
382 * is pending.
383 */
384 private boolean isCurrentlyActiveLocked(JobStatus job) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700385 for (int i=0; i<mActiveServices.size(); i++) {
386 JobServiceContext serviceContext = mActiveServices.get(i);
Christopher Tate7060b042014-06-09 19:50:00 -0700387 final JobStatus running = serviceContext.getRunningJob();
388 if (running != null && running.matches(job.getUid(), job.getJobId())) {
389 return true;
390 }
391 }
392 return false;
393 }
394
395 /**
396 * A job is rescheduled with exponential back-off if the client requests this from their
397 * execution logic.
398 * A caveat is for idle-mode jobs, for which the idle-mode constraint will usurp the
399 * timeliness of the reschedule. For an idle-mode job, no deadline is given.
400 * @param failureToReschedule Provided job status that we will reschedule.
401 * @return A newly instantiated JobStatus with the same constraints as the last job except
402 * with adjusted timing constraints.
403 */
404 private JobStatus getRescheduleJobForFailure(JobStatus failureToReschedule) {
405 final long elapsedNowMillis = SystemClock.elapsedRealtime();
406 final JobInfo job = failureToReschedule.getJob();
407
408 final long initialBackoffMillis = job.getInitialBackoffMillis();
Matthew Williamsd1c06752014-08-22 14:15:28 -0700409 final int backoffAttempts = failureToReschedule.getNumFailures() + 1;
410 long delayMillis;
Christopher Tate7060b042014-06-09 19:50:00 -0700411
412 switch (job.getBackoffPolicy()) {
Matthew Williamsd1c06752014-08-22 14:15:28 -0700413 case JobInfo.BACKOFF_POLICY_LINEAR:
414 delayMillis = initialBackoffMillis * backoffAttempts;
Christopher Tate7060b042014-06-09 19:50:00 -0700415 break;
416 default:
417 if (DEBUG) {
418 Slog.v(TAG, "Unrecognised back-off policy, defaulting to exponential.");
419 }
Matthew Williamsd1c06752014-08-22 14:15:28 -0700420 case JobInfo.BACKOFF_POLICY_EXPONENTIAL:
421 delayMillis =
422 (long) Math.scalb(initialBackoffMillis, backoffAttempts - 1);
Christopher Tate7060b042014-06-09 19:50:00 -0700423 break;
424 }
Matthew Williamsd1c06752014-08-22 14:15:28 -0700425 delayMillis =
426 Math.min(delayMillis, JobInfo.MAX_BACKOFF_DELAY_MILLIS);
427 return new JobStatus(failureToReschedule, elapsedNowMillis + delayMillis,
428 JobStatus.NO_LATEST_RUNTIME, backoffAttempts);
Christopher Tate7060b042014-06-09 19:50:00 -0700429 }
430
431 /**
432 * Called after a periodic has executed so we can to re-add it. We take the last execution time
433 * of the job to be the time of completion (i.e. the time at which this function is called).
434 * This could be inaccurate b/c the job can run for as long as
435 * {@link com.android.server.job.JobServiceContext#EXECUTING_TIMESLICE_MILLIS}, but will lead
436 * to underscheduling at least, rather than if we had taken the last execution time to be the
437 * start of the execution.
438 * @return A new job representing the execution criteria for this instantiation of the
439 * recurring job.
440 */
441 private JobStatus getRescheduleJobForPeriodic(JobStatus periodicToReschedule) {
442 final long elapsedNow = SystemClock.elapsedRealtime();
443 // Compute how much of the period is remaining.
444 long runEarly = Math.max(periodicToReschedule.getLatestRunTimeElapsed() - elapsedNow, 0);
445 long newEarliestRunTimeElapsed = elapsedNow + runEarly;
446 long period = periodicToReschedule.getJob().getIntervalMillis();
447 long newLatestRuntimeElapsed = newEarliestRunTimeElapsed + period;
448
449 if (DEBUG) {
450 Slog.v(TAG, "Rescheduling executed periodic. New execution window [" +
451 newEarliestRunTimeElapsed/1000 + ", " + newLatestRuntimeElapsed/1000 + "]s");
452 }
453 return new JobStatus(periodicToReschedule, newEarliestRunTimeElapsed,
454 newLatestRuntimeElapsed, 0 /* backoffAttempt */);
455 }
456
457 // JobCompletedListener implementations.
458
459 /**
460 * A job just finished executing. We fetch the
461 * {@link com.android.server.job.controllers.JobStatus} from the store and depending on
462 * whether we want to reschedule we readd it to the controllers.
463 * @param jobStatus Completed job.
464 * @param needsReschedule Whether the implementing class should reschedule this job.
465 */
466 @Override
467 public void onJobCompleted(JobStatus jobStatus, boolean needsReschedule) {
468 if (DEBUG) {
469 Slog.d(TAG, "Completed " + jobStatus + ", reschedule=" + needsReschedule);
470 }
471 if (!stopTrackingJob(jobStatus)) {
472 if (DEBUG) {
Matthew Williamsee410da2014-07-25 11:30:40 -0700473 Slog.d(TAG, "Could not find job to remove. Was job removed while executing?");
Christopher Tate7060b042014-06-09 19:50:00 -0700474 }
475 return;
476 }
477 if (needsReschedule) {
478 JobStatus rescheduled = getRescheduleJobForFailure(jobStatus);
479 startTrackingJob(rescheduled);
480 } else if (jobStatus.getJob().isPeriodic()) {
481 JobStatus rescheduledPeriodic = getRescheduleJobForPeriodic(jobStatus);
482 startTrackingJob(rescheduledPeriodic);
483 }
484 mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
485 }
486
487 // StateChangedListener implementations.
488
489 /**
Matthew Williams48a30db2014-09-23 13:39:36 -0700490 * Posts a message to the {@link com.android.server.job.JobSchedulerService.JobHandler} that
491 * some controller's state has changed, so as to run through the list of jobs and start/stop
492 * any that are eligible.
Christopher Tate7060b042014-06-09 19:50:00 -0700493 */
494 @Override
495 public void onControllerStateChanged() {
Matthew Williams48a30db2014-09-23 13:39:36 -0700496 mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
Christopher Tate7060b042014-06-09 19:50:00 -0700497 }
498
499 @Override
500 public void onRunJobNow(JobStatus jobStatus) {
501 mHandler.obtainMessage(MSG_JOB_EXPIRED, jobStatus).sendToTarget();
502 }
503
Christopher Tate7060b042014-06-09 19:50:00 -0700504 private class JobHandler extends Handler {
505
506 public JobHandler(Looper looper) {
507 super(looper);
508 }
509
510 @Override
511 public void handleMessage(Message message) {
Matthew Williams48a30db2014-09-23 13:39:36 -0700512 synchronized (mJobs) {
513 if (!mReadyToRock) {
514 return;
515 }
516 }
Christopher Tate7060b042014-06-09 19:50:00 -0700517 switch (message.what) {
518 case MSG_JOB_EXPIRED:
519 synchronized (mJobs) {
520 JobStatus runNow = (JobStatus) message.obj;
Matthew Williamsbafeeb92014-08-08 11:51:06 -0700521 // runNow can be null, which is a controller's way of indicating that its
522 // state is such that all ready jobs should be run immediately.
Matthew Williams48a30db2014-09-23 13:39:36 -0700523 if (runNow != null && !mPendingJobs.contains(runNow)
524 && mJobs.containsJob(runNow)) {
Christopher Tate7060b042014-06-09 19:50:00 -0700525 mPendingJobs.add(runNow);
526 }
Matthew Williams48a30db2014-09-23 13:39:36 -0700527 queueReadyJobsForExecutionLockedH();
Christopher Tate7060b042014-06-09 19:50:00 -0700528 }
Christopher Tate7060b042014-06-09 19:50:00 -0700529 break;
530 case MSG_CHECK_JOB:
Matthew Williams48a30db2014-09-23 13:39:36 -0700531 synchronized (mJobs) {
532 // Check the list of jobs and run some of them if we feel inclined.
533 maybeQueueReadyJobsForExecutionLockedH();
534 }
Christopher Tate7060b042014-06-09 19:50:00 -0700535 break;
536 }
537 maybeRunPendingJobsH();
538 // Don't remove JOB_EXPIRED in case one came along while processing the queue.
539 removeMessages(MSG_CHECK_JOB);
540 }
541
542 /**
543 * Run through list of jobs and execute all possible - at least one is expired so we do
544 * as many as we can.
545 */
Matthew Williams48a30db2014-09-23 13:39:36 -0700546 private void queueReadyJobsForExecutionLockedH() {
547 ArraySet<JobStatus> jobs = mJobs.getJobs();
548 if (DEBUG) {
549 Slog.d(TAG, "queuing all ready jobs for execution:");
550 }
551 for (int i=0; i<jobs.size(); i++) {
552 JobStatus job = jobs.valueAt(i);
553 if (isReadyToBeExecutedLocked(job)) {
554 if (DEBUG) {
555 Slog.d(TAG, " queued " + job.toShortString());
Christopher Tate7060b042014-06-09 19:50:00 -0700556 }
Matthew Williams48a30db2014-09-23 13:39:36 -0700557 mPendingJobs.add(job);
558 } else if (isReadyToBeCancelledLocked(job)) {
559 stopJobOnServiceContextLocked(job);
Christopher Tate7060b042014-06-09 19:50:00 -0700560 }
Matthew Williams48a30db2014-09-23 13:39:36 -0700561 }
562 if (DEBUG) {
563 final int queuedJobs = mPendingJobs.size();
564 if (queuedJobs == 0) {
565 Slog.d(TAG, "No jobs pending.");
566 } else {
567 Slog.d(TAG, queuedJobs + " jobs queued.");
Matthew Williams75fc5252014-09-02 16:17:53 -0700568 }
Christopher Tate7060b042014-06-09 19:50:00 -0700569 }
570 }
571
572 /**
573 * The state of at least one job has changed. Here is where we could enforce various
574 * policies on when we want to execute jobs.
575 * Right now the policy is such:
576 * If >1 of the ready jobs is idle mode we send all of them off
577 * if more than 2 network connectivity jobs are ready we send them all off.
578 * If more than 4 jobs total are ready we send them all off.
579 * TODO: It would be nice to consolidate these sort of high-level policies somewhere.
580 */
Matthew Williams48a30db2014-09-23 13:39:36 -0700581 private void maybeQueueReadyJobsForExecutionLockedH() {
582 int chargingCount = 0;
583 int idleCount = 0;
584 int backoffCount = 0;
585 int connectivityCount = 0;
586 List<JobStatus> runnableJobs = new ArrayList<JobStatus>();
587 ArraySet<JobStatus> jobs = mJobs.getJobs();
588 for (int i=0; i<jobs.size(); i++) {
589 JobStatus job = jobs.valueAt(i);
590 if (isReadyToBeExecutedLocked(job)) {
591 if (job.getNumFailures() > 0) {
592 backoffCount++;
Christopher Tate7060b042014-06-09 19:50:00 -0700593 }
Matthew Williams48a30db2014-09-23 13:39:36 -0700594 if (job.hasIdleConstraint()) {
595 idleCount++;
596 }
597 if (job.hasConnectivityConstraint() || job.hasUnmeteredConstraint()) {
598 connectivityCount++;
599 }
600 if (job.hasChargingConstraint()) {
601 chargingCount++;
602 }
603 runnableJobs.add(job);
604 } else if (isReadyToBeCancelledLocked(job)) {
605 stopJobOnServiceContextLocked(job);
Christopher Tate7060b042014-06-09 19:50:00 -0700606 }
Matthew Williams48a30db2014-09-23 13:39:36 -0700607 }
608 if (backoffCount > 0 ||
609 idleCount >= MIN_IDLE_COUNT ||
610 connectivityCount >= MIN_CONNECTIVITY_COUNT ||
611 chargingCount >= MIN_CHARGING_COUNT ||
612 runnableJobs.size() >= MIN_READY_JOBS_COUNT) {
Matthew Williamsbe0c4172014-08-06 18:14:16 -0700613 if (DEBUG) {
Matthew Williams48a30db2014-09-23 13:39:36 -0700614 Slog.d(TAG, "maybeQueueReadyJobsForExecutionLockedH: Running jobs.");
Christopher Tate7060b042014-06-09 19:50:00 -0700615 }
Matthew Williams48a30db2014-09-23 13:39:36 -0700616 for (int i=0; i<runnableJobs.size(); i++) {
617 mPendingJobs.add(runnableJobs.get(i));
618 }
619 } else {
620 if (DEBUG) {
621 Slog.d(TAG, "maybeQueueReadyJobsForExecutionLockedH: Not running anything.");
622 }
623 }
624 if (DEBUG) {
625 Slog.d(TAG, "idle=" + idleCount + " connectivity=" +
626 connectivityCount + " charging=" + chargingCount + " tot=" +
627 runnableJobs.size());
Christopher Tate7060b042014-06-09 19:50:00 -0700628 }
629 }
630
631 /**
632 * Criteria for moving a job into the pending queue:
633 * - It's ready.
634 * - It's not pending.
635 * - It's not already running on a JSC.
Matthew Williams9ae3dbe2014-08-21 13:47:47 -0700636 * - The user that requested the job is running.
Christopher Tate7060b042014-06-09 19:50:00 -0700637 */
638 private boolean isReadyToBeExecutedLocked(JobStatus job) {
Matthew Williams9ae3dbe2014-08-21 13:47:47 -0700639 final boolean jobReady = job.isReady();
640 final boolean jobPending = mPendingJobs.contains(job);
641 final boolean jobActive = isCurrentlyActiveLocked(job);
642 final boolean userRunning = mStartedUsers.contains(job.getUserId());
643
644 if (DEBUG) {
645 Slog.v(TAG, "isReadyToBeExecutedLocked: " + job.toShortString()
646 + " ready=" + jobReady + " pending=" + jobPending
647 + " active=" + jobActive + " userRunning=" + userRunning);
648 }
649 return userRunning && jobReady && !jobPending && !jobActive;
Christopher Tate7060b042014-06-09 19:50:00 -0700650 }
651
652 /**
653 * Criteria for cancelling an active job:
654 * - It's not ready
655 * - It's running on a JSC.
656 */
657 private boolean isReadyToBeCancelledLocked(JobStatus job) {
658 return !job.isReady() && isCurrentlyActiveLocked(job);
659 }
660
661 /**
662 * Reconcile jobs in the pending queue against available execution contexts.
663 * A controller can force a job into the pending queue even if it's already running, but
664 * here is where we decide whether to actually execute it.
665 */
666 private void maybeRunPendingJobsH() {
667 synchronized (mJobs) {
668 Iterator<JobStatus> it = mPendingJobs.iterator();
Matthew Williams75fc5252014-09-02 16:17:53 -0700669 if (DEBUG) {
670 Slog.d(TAG, "pending queue: " + mPendingJobs.size() + " jobs.");
671 }
Christopher Tate7060b042014-06-09 19:50:00 -0700672 while (it.hasNext()) {
673 JobStatus nextPending = it.next();
674 JobServiceContext availableContext = null;
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700675 for (int i=0; i<mActiveServices.size(); i++) {
676 JobServiceContext jsc = mActiveServices.get(i);
Christopher Tate7060b042014-06-09 19:50:00 -0700677 final JobStatus running = jsc.getRunningJob();
678 if (running != null && running.matches(nextPending.getUid(),
679 nextPending.getJobId())) {
Matthew Williamsee410da2014-07-25 11:30:40 -0700680 // Already running this job for this uId, skip.
Christopher Tate7060b042014-06-09 19:50:00 -0700681 availableContext = null;
682 break;
683 }
684 if (jsc.isAvailable()) {
685 availableContext = jsc;
686 }
687 }
688 if (availableContext != null) {
689 if (!availableContext.executeRunnableJob(nextPending)) {
690 if (DEBUG) {
691 Slog.d(TAG, "Error executing " + nextPending);
692 }
693 mJobs.remove(nextPending);
694 }
695 it.remove();
696 }
697 }
698 }
699 }
700 }
701
702 /**
703 * Binder stub trampoline implementation
704 */
705 final class JobSchedulerStub extends IJobScheduler.Stub {
706 /** Cache determination of whether a given app can persist jobs
707 * key is uid of the calling app; value is undetermined/true/false
708 */
709 private final SparseArray<Boolean> mPersistCache = new SparseArray<Boolean>();
710
711 // Enforce that only the app itself (or shared uid participant) can schedule a
712 // job that runs one of the app's services, as well as verifying that the
713 // named service properly requires the BIND_JOB_SERVICE permission
714 private void enforceValidJobRequest(int uid, JobInfo job) {
Christopher Tate5568f542014-06-18 13:53:31 -0700715 final IPackageManager pm = AppGlobals.getPackageManager();
Christopher Tate7060b042014-06-09 19:50:00 -0700716 final ComponentName service = job.getService();
717 try {
Christopher Tate5568f542014-06-18 13:53:31 -0700718 ServiceInfo si = pm.getServiceInfo(service, 0, UserHandle.getUserId(uid));
719 if (si == null) {
720 throw new IllegalArgumentException("No such service " + service);
721 }
Christopher Tate7060b042014-06-09 19:50:00 -0700722 if (si.applicationInfo.uid != uid) {
723 throw new IllegalArgumentException("uid " + uid +
724 " cannot schedule job in " + service.getPackageName());
725 }
726 if (!JobService.PERMISSION_BIND.equals(si.permission)) {
727 throw new IllegalArgumentException("Scheduled service " + service
728 + " does not require android.permission.BIND_JOB_SERVICE permission");
729 }
Christopher Tate5568f542014-06-18 13:53:31 -0700730 } catch (RemoteException e) {
731 // Can't happen; the Package Manager is in this same process
Christopher Tate7060b042014-06-09 19:50:00 -0700732 }
733 }
734
735 private boolean canPersistJobs(int pid, int uid) {
736 // If we get this far we're good to go; all we need to do now is check
737 // whether the app is allowed to persist its scheduled work.
738 final boolean canPersist;
739 synchronized (mPersistCache) {
740 Boolean cached = mPersistCache.get(uid);
741 if (cached != null) {
742 canPersist = cached.booleanValue();
743 } else {
744 // Persisting jobs is tantamount to running at boot, so we permit
745 // it when the app has declared that it uses the RECEIVE_BOOT_COMPLETED
746 // permission
747 int result = getContext().checkPermission(
748 android.Manifest.permission.RECEIVE_BOOT_COMPLETED, pid, uid);
749 canPersist = (result == PackageManager.PERMISSION_GRANTED);
750 mPersistCache.put(uid, canPersist);
751 }
752 }
753 return canPersist;
754 }
755
756 // IJobScheduler implementation
757 @Override
758 public int schedule(JobInfo job) throws RemoteException {
759 if (DEBUG) {
Matthew Williamsee410da2014-07-25 11:30:40 -0700760 Slog.d(TAG, "Scheduling job: " + job.toString());
Christopher Tate7060b042014-06-09 19:50:00 -0700761 }
762 final int pid = Binder.getCallingPid();
763 final int uid = Binder.getCallingUid();
764
765 enforceValidJobRequest(uid, job);
Matthew Williams900c67f2014-07-09 12:46:53 -0700766 if (job.isPersisted()) {
767 if (!canPersistJobs(pid, uid)) {
768 throw new IllegalArgumentException("Error: requested job be persisted without"
769 + " holding RECEIVE_BOOT_COMPLETED permission.");
770 }
771 }
Christopher Tate7060b042014-06-09 19:50:00 -0700772
773 long ident = Binder.clearCallingIdentity();
774 try {
Matthew Williams900c67f2014-07-09 12:46:53 -0700775 return JobSchedulerService.this.schedule(job, uid);
Christopher Tate7060b042014-06-09 19:50:00 -0700776 } finally {
777 Binder.restoreCallingIdentity(ident);
778 }
779 }
780
781 @Override
782 public List<JobInfo> getAllPendingJobs() throws RemoteException {
783 final int uid = Binder.getCallingUid();
784
785 long ident = Binder.clearCallingIdentity();
786 try {
787 return JobSchedulerService.this.getPendingJobs(uid);
788 } finally {
789 Binder.restoreCallingIdentity(ident);
790 }
791 }
792
793 @Override
794 public void cancelAll() throws RemoteException {
795 final int uid = Binder.getCallingUid();
796
797 long ident = Binder.clearCallingIdentity();
798 try {
799 JobSchedulerService.this.cancelJobsForUid(uid);
800 } finally {
801 Binder.restoreCallingIdentity(ident);
802 }
803 }
804
805 @Override
806 public void cancel(int jobId) throws RemoteException {
807 final int uid = Binder.getCallingUid();
808
809 long ident = Binder.clearCallingIdentity();
810 try {
811 JobSchedulerService.this.cancelJob(uid, jobId);
812 } finally {
813 Binder.restoreCallingIdentity(ident);
814 }
815 }
816
817 /**
818 * "dumpsys" infrastructure
819 */
820 @Override
821 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
822 getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
823
824 long identityToken = Binder.clearCallingIdentity();
825 try {
826 JobSchedulerService.this.dumpInternal(pw);
827 } finally {
828 Binder.restoreCallingIdentity(identityToken);
829 }
830 }
831 };
832
833 void dumpInternal(PrintWriter pw) {
Christopher Tatef973a7b2014-08-29 12:54:08 -0700834 final long now = SystemClock.elapsedRealtime();
Christopher Tate7060b042014-06-09 19:50:00 -0700835 synchronized (mJobs) {
Matthew Williams9ae3dbe2014-08-21 13:47:47 -0700836 pw.print("Started users: ");
837 for (int i=0; i<mStartedUsers.size(); i++) {
838 pw.print("u" + mStartedUsers.get(i) + " ");
839 }
840 pw.println();
Christopher Tate7060b042014-06-09 19:50:00 -0700841 pw.println("Registered jobs:");
842 if (mJobs.size() > 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700843 ArraySet<JobStatus> jobs = mJobs.getJobs();
844 for (int i=0; i<jobs.size(); i++) {
845 JobStatus job = jobs.valueAt(i);
Christopher Tate7060b042014-06-09 19:50:00 -0700846 job.dump(pw, " ");
847 }
848 } else {
Christopher Tatef973a7b2014-08-29 12:54:08 -0700849 pw.println(" None.");
Christopher Tate7060b042014-06-09 19:50:00 -0700850 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700851 for (int i=0; i<mControllers.size(); i++) {
Christopher Tate7060b042014-06-09 19:50:00 -0700852 pw.println();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700853 mControllers.get(i).dumpControllerState(pw);
Christopher Tate7060b042014-06-09 19:50:00 -0700854 }
855 pw.println();
Christopher Tatef973a7b2014-08-29 12:54:08 -0700856 pw.println("Pending:");
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700857 for (int i=0; i<mPendingJobs.size(); i++) {
858 pw.println(mPendingJobs.get(i).hashCode());
Christopher Tate7060b042014-06-09 19:50:00 -0700859 }
860 pw.println();
861 pw.println("Active jobs:");
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700862 for (int i=0; i<mActiveServices.size(); i++) {
863 JobServiceContext jsc = mActiveServices.get(i);
Christopher Tate7060b042014-06-09 19:50:00 -0700864 if (jsc.isAvailable()) {
865 continue;
866 } else {
Christopher Tatef973a7b2014-08-29 12:54:08 -0700867 final long timeout = jsc.getTimeoutElapsed();
868 pw.print("Running for: ");
869 pw.print((now - jsc.getExecutionStartTimeElapsed())/1000);
870 pw.print("s timeout=");
871 pw.print(timeout);
872 pw.print(" fromnow=");
873 pw.println(timeout-now);
874 jsc.getRunningJob().dump(pw, " ");
Christopher Tate7060b042014-06-09 19:50:00 -0700875 }
876 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700877 pw.println();
878 pw.print("mReadyToRock="); pw.println(mReadyToRock);
Christopher Tate7060b042014-06-09 19:50:00 -0700879 }
880 pw.println();
881 }
882}