blob: 759199ce58e61471ad7e0fae6fdf354b6910dec6 [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
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070025import android.app.ActivityManager;
Christopher Tate5568f542014-06-18 13:53:31 -070026import android.app.AppGlobals;
Christopher Tate7060b042014-06-09 19:50:00 -070027import android.app.job.JobInfo;
28import android.app.job.JobScheduler;
29import android.app.job.JobService;
30import android.app.job.IJobScheduler;
31import android.content.BroadcastReceiver;
32import android.content.ComponentName;
33import android.content.Context;
34import android.content.Intent;
35import android.content.IntentFilter;
Christopher Tate5568f542014-06-18 13:53:31 -070036import android.content.pm.IPackageManager;
Christopher Tate7060b042014-06-09 19:50:00 -070037import android.content.pm.PackageManager;
Christopher Tate7060b042014-06-09 19:50:00 -070038import android.content.pm.ServiceInfo;
Dianne Hackbornfdb19562014-07-11 16:03:36 -070039import android.os.BatteryStats;
Christopher Tate7060b042014-06-09 19:50:00 -070040import android.os.Binder;
41import android.os.Handler;
42import android.os.Looper;
43import android.os.Message;
Dianne Hackborn88e98df2015-03-23 13:29:14 -070044import android.os.PowerManager;
Christopher Tate7060b042014-06-09 19:50:00 -070045import android.os.RemoteException;
Dianne Hackbornfdb19562014-07-11 16:03:36 -070046import android.os.ServiceManager;
Christopher Tate7060b042014-06-09 19:50:00 -070047import android.os.SystemClock;
48import android.os.UserHandle;
Dianne Hackbornfdb19562014-07-11 16:03:36 -070049import android.util.ArraySet;
Christopher Tate7060b042014-06-09 19:50:00 -070050import android.util.Slog;
51import android.util.SparseArray;
52
Dianne Hackbornfdb19562014-07-11 16:03:36 -070053import com.android.internal.app.IBatteryStats;
Dianne Hackborn627dfa12015-11-11 18:10:30 -080054import com.android.server.DeviceIdleController;
55import com.android.server.LocalServices;
Amith Yamasanib0ff3222015-03-04 09:56:14 -080056import com.android.server.job.controllers.AppIdleController;
Christopher Tate7060b042014-06-09 19:50:00 -070057import com.android.server.job.controllers.BatteryController;
58import com.android.server.job.controllers.ConnectivityController;
59import com.android.server.job.controllers.IdleController;
60import com.android.server.job.controllers.JobStatus;
61import com.android.server.job.controllers.StateController;
62import com.android.server.job.controllers.TimeController;
63
Christopher Tate7060b042014-06-09 19:50:00 -070064/**
65 * Responsible for taking jobs representing work to be performed by a client app, and determining
66 * based on the criteria specified when that job should be run against the client application's
67 * endpoint.
68 * Implements logic for scheduling, and rescheduling jobs. The JobSchedulerService knows nothing
69 * about constraints, or the state of active jobs. It receives callbacks from the various
70 * controllers and completed jobs and operates accordingly.
71 *
72 * Note on locking: Any operations that manipulate {@link #mJobs} need to lock on that object.
73 * Any function with the suffix 'Locked' also needs to lock on {@link #mJobs}.
74 * @hide
75 */
76public class JobSchedulerService extends com.android.server.SystemService
Matthew Williams01ac45b2014-07-22 20:44:12 -070077 implements StateChangedListener, JobCompletedListener {
Matthew Williamsaa984312015-10-15 16:08:05 -070078 public static final boolean DEBUG = false;
Christopher Tate7060b042014-06-09 19:50:00 -070079 /** The number of concurrent jobs we run at one time. */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070080 private static final int MAX_JOB_CONTEXTS_COUNT
81 = ActivityManager.isLowRamDeviceStatic() ? 1 : 3;
Matthew Williamsbe0c4172014-08-06 18:14:16 -070082 static final String TAG = "JobSchedulerService";
Christopher Tate7060b042014-06-09 19:50:00 -070083 /** Master list of jobs. */
Dianne Hackbornfdb19562014-07-11 16:03:36 -070084 final JobStore mJobs;
Christopher Tate7060b042014-06-09 19:50:00 -070085
86 static final int MSG_JOB_EXPIRED = 0;
87 static final int MSG_CHECK_JOB = 1;
88
89 // Policy constants
90 /**
91 * Minimum # of idle jobs that must be ready in order to force the JMS to schedule things
92 * early.
93 */
Dianne Hackbornfdb19562014-07-11 16:03:36 -070094 static final int MIN_IDLE_COUNT = 1;
Christopher Tate7060b042014-06-09 19:50:00 -070095 /**
Matthew Williamsbe0c4172014-08-06 18:14:16 -070096 * Minimum # of charging jobs that must be ready in order to force the JMS to schedule things
97 * early.
98 */
99 static final int MIN_CHARGING_COUNT = 1;
100 /**
Christopher Tate7060b042014-06-09 19:50:00 -0700101 * Minimum # of connectivity jobs that must be ready in order to force the JMS to schedule
102 * things early.
103 */
Matthew Williamsaa984312015-10-15 16:08:05 -0700104 static final int MIN_CONNECTIVITY_COUNT = 1; // Run connectivity jobs as soon as ready.
Christopher Tate7060b042014-06-09 19:50:00 -0700105 /**
106 * Minimum # of jobs (with no particular constraints) for which the JMS will be happy running
107 * some work early.
Matthew Williamsbe0c4172014-08-06 18:14:16 -0700108 * This is correlated with the amount of batching we'll be able to do.
Christopher Tate7060b042014-06-09 19:50:00 -0700109 */
Matthew Williamsbe0c4172014-08-06 18:14:16 -0700110 static final int MIN_READY_JOBS_COUNT = 2;
Christopher Tate7060b042014-06-09 19:50:00 -0700111
112 /**
113 * Track Services that have currently active or pending jobs. The index is provided by
114 * {@link JobStatus#getServiceToken()}
115 */
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700116 final List<JobServiceContext> mActiveServices = new ArrayList<>();
Christopher Tate7060b042014-06-09 19:50:00 -0700117 /** List of controllers that will notify this service of updates to jobs. */
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700118 List<StateController> mControllers;
Christopher Tate7060b042014-06-09 19:50:00 -0700119 /**
120 * Queue of pending jobs. The JobServiceContext class will receive jobs from this list
121 * when ready to execute them.
122 */
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700123 final ArrayList<JobStatus> mPendingJobs = new ArrayList<>();
Christopher Tate7060b042014-06-09 19:50:00 -0700124
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700125 final ArrayList<Integer> mStartedUsers = new ArrayList<>();
Matthew Williams9ae3dbe2014-08-21 13:47:47 -0700126
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700127 final JobHandler mHandler;
128 final JobSchedulerStub mJobSchedulerStub;
129
130 IBatteryStats mBatteryStats;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700131 PowerManager mPowerManager;
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800132 DeviceIdleController.LocalService mLocalDeviceIdleController;
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700133
134 /**
135 * Set to true once we are allowed to run third party apps.
136 */
137 boolean mReadyToRock;
138
Christopher Tate7060b042014-06-09 19:50:00 -0700139 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700140 * True when in device idle mode, so we don't want to schedule any jobs.
141 */
142 boolean mDeviceIdleMode;
143
144 /**
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800145 * What we last reported to DeviceIdleController about wheter we are active.
146 */
147 boolean mReportedActive;
148
149 /**
Christopher Tate7060b042014-06-09 19:50:00 -0700150 * Cleans up outstanding jobs when a package is removed. Even if it's being replaced later we
151 * still clean up. On reinstall the package will have a new uid.
152 */
153 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
154 @Override
155 public void onReceive(Context context, Intent intent) {
156 Slog.d(TAG, "Receieved: " + intent.getAction());
157 if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) {
Christopher Tateaad67a32014-10-20 16:29:20 -0700158 // If this is an outright uninstall rather than the first half of an
159 // app update sequence, cancel the jobs associated with the app.
160 if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
161 int uidRemoved = intent.getIntExtra(Intent.EXTRA_UID, -1);
162 if (DEBUG) {
163 Slog.d(TAG, "Removing jobs for uid: " + uidRemoved);
164 }
165 cancelJobsForUid(uidRemoved);
Christopher Tate7060b042014-06-09 19:50:00 -0700166 }
Christopher Tate7060b042014-06-09 19:50:00 -0700167 } else if (Intent.ACTION_USER_REMOVED.equals(intent.getAction())) {
168 final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0);
169 if (DEBUG) {
170 Slog.d(TAG, "Removing jobs for user: " + userId);
171 }
172 cancelJobsForUser(userId);
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700173 } else if (PowerManager.ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED.equals(intent.getAction())
174 || PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED.equals(intent.getAction())) {
175 updateIdleMode(mPowerManager != null
176 ? (mPowerManager.isDeviceIdleMode()
177 || mPowerManager.isLightDeviceIdleMode())
178 : false);
Christopher Tate7060b042014-06-09 19:50:00 -0700179 }
180 }
181 };
182
Matthew Williams9ae3dbe2014-08-21 13:47:47 -0700183 @Override
184 public void onStartUser(int userHandle) {
185 mStartedUsers.add(userHandle);
186 // Let's kick any outstanding jobs for this user.
187 mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
188 }
189
190 @Override
191 public void onStopUser(int userHandle) {
192 mStartedUsers.remove(Integer.valueOf(userHandle));
193 }
194
Christopher Tate7060b042014-06-09 19:50:00 -0700195 /**
196 * Entry point from client to schedule the provided job.
197 * This cancels the job if it's already been scheduled, and replaces it with the one provided.
198 * @param job JobInfo object containing execution parameters
199 * @param uId The package identifier of the application this job is for.
Christopher Tate7060b042014-06-09 19:50:00 -0700200 * @return Result of this operation. See <code>JobScheduler#RESULT_*</code> return codes.
201 */
Matthew Williams900c67f2014-07-09 12:46:53 -0700202 public int schedule(JobInfo job, int uId) {
203 JobStatus jobStatus = new JobStatus(job, uId);
Christopher Tate7060b042014-06-09 19:50:00 -0700204 cancelJob(uId, job.getId());
205 startTrackingJob(jobStatus);
Matthew Williamsbafeeb92014-08-08 11:51:06 -0700206 mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
Christopher Tate7060b042014-06-09 19:50:00 -0700207 return JobScheduler.RESULT_SUCCESS;
208 }
209
210 public List<JobInfo> getPendingJobs(int uid) {
211 ArrayList<JobInfo> outList = new ArrayList<JobInfo>();
212 synchronized (mJobs) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700213 ArraySet<JobStatus> jobs = mJobs.getJobs();
214 for (int i=0; i<jobs.size(); i++) {
215 JobStatus job = jobs.valueAt(i);
Christopher Tate7060b042014-06-09 19:50:00 -0700216 if (job.getUid() == uid) {
217 outList.add(job.getJob());
218 }
219 }
220 }
221 return outList;
222 }
223
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700224 void cancelJobsForUser(int userHandle) {
Matthew Williams48a30db2014-09-23 13:39:36 -0700225 List<JobStatus> jobsForUser;
Christopher Tate7060b042014-06-09 19:50:00 -0700226 synchronized (mJobs) {
Matthew Williams48a30db2014-09-23 13:39:36 -0700227 jobsForUser = mJobs.getJobsByUser(userHandle);
228 }
229 for (int i=0; i<jobsForUser.size(); i++) {
230 JobStatus toRemove = jobsForUser.get(i);
231 cancelJobImpl(toRemove);
Christopher Tate7060b042014-06-09 19:50:00 -0700232 }
233 }
234
235 /**
236 * Entry point from client to cancel all jobs originating from their uid.
237 * This will remove the job from the master list, and cancel the job if it was staged for
238 * execution or being executed.
Matthew Williams48a30db2014-09-23 13:39:36 -0700239 * @param uid Uid to check against for removal of a job.
Christopher Tate7060b042014-06-09 19:50:00 -0700240 */
241 public void cancelJobsForUid(int uid) {
Matthew Williams48a30db2014-09-23 13:39:36 -0700242 List<JobStatus> jobsForUid;
Christopher Tate7060b042014-06-09 19:50:00 -0700243 synchronized (mJobs) {
Matthew Williams48a30db2014-09-23 13:39:36 -0700244 jobsForUid = mJobs.getJobsByUid(uid);
245 }
246 for (int i=0; i<jobsForUid.size(); i++) {
247 JobStatus toRemove = jobsForUid.get(i);
248 cancelJobImpl(toRemove);
Christopher Tate7060b042014-06-09 19:50:00 -0700249 }
250 }
251
252 /**
253 * Entry point from client to cancel the job corresponding to the jobId provided.
254 * This will remove the job from the master list, and cancel the job if it was staged for
255 * execution or being executed.
256 * @param uid Uid of the calling client.
257 * @param jobId Id of the job, provided at schedule-time.
258 */
259 public void cancelJob(int uid, int jobId) {
260 JobStatus toCancel;
261 synchronized (mJobs) {
262 toCancel = mJobs.getJobByUidAndJobId(uid, jobId);
Matthew Williams48a30db2014-09-23 13:39:36 -0700263 }
264 if (toCancel != null) {
265 cancelJobImpl(toCancel);
Christopher Tate7060b042014-06-09 19:50:00 -0700266 }
267 }
268
Matthew Williams48a30db2014-09-23 13:39:36 -0700269 private void cancelJobImpl(JobStatus cancelled) {
Matthew Williamsee410da2014-07-25 11:30:40 -0700270 if (DEBUG) {
271 Slog.d(TAG, "Cancelling: " + cancelled);
272 }
Christopher Tate7060b042014-06-09 19:50:00 -0700273 stopTrackingJob(cancelled);
Matthew Williams48a30db2014-09-23 13:39:36 -0700274 synchronized (mJobs) {
275 // Remove from pending queue.
276 mPendingJobs.remove(cancelled);
277 // Cancel if running.
278 stopJobOnServiceContextLocked(cancelled);
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800279 reportActive();
Matthew Williams48a30db2014-09-23 13:39:36 -0700280 }
Christopher Tate7060b042014-06-09 19:50:00 -0700281 }
282
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700283 void updateIdleMode(boolean enabled) {
284 boolean changed = false;
285 boolean rocking;
286 synchronized (mJobs) {
287 if (mDeviceIdleMode != enabled) {
288 changed = true;
289 }
290 rocking = mReadyToRock;
291 }
292 if (changed) {
293 if (rocking) {
294 for (int i=0; i<mControllers.size(); i++) {
295 mControllers.get(i).deviceIdleModeChanged(enabled);
296 }
297 }
298 synchronized (mJobs) {
299 mDeviceIdleMode = enabled;
300 if (enabled) {
301 // When becoming idle, make sure no jobs are actively running.
302 for (int i=0; i<mActiveServices.size(); i++) {
303 JobServiceContext jsc = mActiveServices.get(i);
304 final JobStatus executing = jsc.getRunningJob();
305 if (executing != null) {
306 jsc.cancelExecutingJob();
307 }
308 }
309 } else {
310 // When coming out of idle, allow thing to start back up.
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800311 if (rocking) {
312 if (mLocalDeviceIdleController != null) {
313 if (!mReportedActive) {
314 mReportedActive = true;
315 mLocalDeviceIdleController.setJobsActive(true);
316 }
317 }
318 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700319 mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
320 }
321 }
322 }
323 }
324
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800325 void reportActive() {
326 boolean active = false;
327 if (mPendingJobs.size() <= 0) {
328 for (int i=0; i<mActiveServices.size(); i++) {
329 JobServiceContext jsc = mActiveServices.get(i);
330 if (!jsc.isAvailable()) {
331 active = true;
332 break;
333 }
334 }
335 }
336 if (mLocalDeviceIdleController != null) {
337 if (mReportedActive != active) {
338 mReportedActive = active;
339 mLocalDeviceIdleController.setJobsActive(active);
340 }
341 }
342 }
343
Christopher Tate7060b042014-06-09 19:50:00 -0700344 /**
345 * Initializes the system service.
346 * <p>
347 * Subclasses must define a single argument constructor that accepts the context
348 * and passes it to super.
349 * </p>
350 *
351 * @param context The system server context.
352 */
353 public JobSchedulerService(Context context) {
354 super(context);
355 // Create the controllers.
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700356 mControllers = new ArrayList<StateController>();
Christopher Tate7060b042014-06-09 19:50:00 -0700357 mControllers.add(ConnectivityController.get(this));
358 mControllers.add(TimeController.get(this));
359 mControllers.add(IdleController.get(this));
360 mControllers.add(BatteryController.get(this));
Amith Yamasanib0ff3222015-03-04 09:56:14 -0800361 mControllers.add(AppIdleController.get(this));
Christopher Tate7060b042014-06-09 19:50:00 -0700362
363 mHandler = new JobHandler(context.getMainLooper());
364 mJobSchedulerStub = new JobSchedulerStub();
Christopher Tate7060b042014-06-09 19:50:00 -0700365 mJobs = JobStore.initAndGet(this);
366 }
367
368 @Override
369 public void onStart() {
370 publishBinderService(Context.JOB_SCHEDULER_SERVICE, mJobSchedulerStub);
371 }
372
373 @Override
374 public void onBootPhase(int phase) {
375 if (PHASE_SYSTEM_SERVICES_READY == phase) {
376 // Register br for package removals and user removals.
377 final IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_REMOVED);
378 filter.addDataScheme("package");
379 getContext().registerReceiverAsUser(
380 mBroadcastReceiver, UserHandle.ALL, filter, null, null);
381 final IntentFilter userFilter = new IntentFilter(Intent.ACTION_USER_REMOVED);
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700382 userFilter.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700383 userFilter.addAction(PowerManager.ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED);
Christopher Tate7060b042014-06-09 19:50:00 -0700384 getContext().registerReceiverAsUser(
385 mBroadcastReceiver, UserHandle.ALL, userFilter, null, null);
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700386 mPowerManager = (PowerManager)getContext().getSystemService(Context.POWER_SERVICE);
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700387 } else if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {
388 synchronized (mJobs) {
389 // Let's go!
390 mReadyToRock = true;
391 mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService(
392 BatteryStats.SERVICE_NAME));
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800393 mLocalDeviceIdleController
394 = LocalServices.getService(DeviceIdleController.LocalService.class);
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700395 // Create the "runners".
396 for (int i = 0; i < MAX_JOB_CONTEXTS_COUNT; i++) {
397 mActiveServices.add(
398 new JobServiceContext(this, mBatteryStats,
399 getContext().getMainLooper()));
400 }
401 // Attach jobs to their controllers.
402 ArraySet<JobStatus> jobs = mJobs.getJobs();
403 for (int i=0; i<jobs.size(); i++) {
404 JobStatus job = jobs.valueAt(i);
Christopher Tate4a79dae2014-07-18 17:01:40 -0700405 for (int controller=0; controller<mControllers.size(); controller++) {
Dianne Hackborn2bf51f42015-03-24 14:17:12 -0700406 mControllers.get(controller).deviceIdleModeChanged(mDeviceIdleMode);
Christopher Tate4a79dae2014-07-18 17:01:40 -0700407 mControllers.get(controller).maybeStartTrackingJob(job);
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700408 }
409 }
410 // GO GO GO!
411 mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
412 }
Christopher Tate7060b042014-06-09 19:50:00 -0700413 }
414 }
415
416 /**
417 * Called when we have a job status object that we need to insert in our
418 * {@link com.android.server.job.JobStore}, and make sure all the relevant controllers know
419 * about.
420 */
421 private void startTrackingJob(JobStatus jobStatus) {
422 boolean update;
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700423 boolean rocking;
Christopher Tate7060b042014-06-09 19:50:00 -0700424 synchronized (mJobs) {
425 update = mJobs.add(jobStatus);
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700426 rocking = mReadyToRock;
Christopher Tate7060b042014-06-09 19:50:00 -0700427 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700428 if (rocking) {
429 for (int i=0; i<mControllers.size(); i++) {
430 StateController controller = mControllers.get(i);
431 if (update) {
432 controller.maybeStopTrackingJob(jobStatus);
433 }
434 controller.maybeStartTrackingJob(jobStatus);
Christopher Tate7060b042014-06-09 19:50:00 -0700435 }
Christopher Tate7060b042014-06-09 19:50:00 -0700436 }
437 }
438
439 /**
440 * Called when we want to remove a JobStatus object that we've finished executing. Returns the
441 * object removed.
442 */
443 private boolean stopTrackingJob(JobStatus jobStatus) {
444 boolean removed;
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700445 boolean rocking;
Christopher Tate7060b042014-06-09 19:50:00 -0700446 synchronized (mJobs) {
447 // Remove from store as well as controllers.
448 removed = mJobs.remove(jobStatus);
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700449 rocking = mReadyToRock;
Christopher Tate7060b042014-06-09 19:50:00 -0700450 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700451 if (removed && rocking) {
452 for (int i=0; i<mControllers.size(); i++) {
453 StateController controller = mControllers.get(i);
Christopher Tate7060b042014-06-09 19:50:00 -0700454 controller.maybeStopTrackingJob(jobStatus);
455 }
456 }
457 return removed;
458 }
459
460 private boolean stopJobOnServiceContextLocked(JobStatus job) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700461 for (int i=0; i<mActiveServices.size(); i++) {
462 JobServiceContext jsc = mActiveServices.get(i);
Christopher Tate7060b042014-06-09 19:50:00 -0700463 final JobStatus executing = jsc.getRunningJob();
464 if (executing != null && executing.matches(job.getUid(), job.getJobId())) {
465 jsc.cancelExecutingJob();
466 return true;
467 }
468 }
469 return false;
470 }
471
472 /**
473 * @param job JobStatus we are querying against.
474 * @return Whether or not the job represented by the status object is currently being run or
475 * is pending.
476 */
477 private boolean isCurrentlyActiveLocked(JobStatus job) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700478 for (int i=0; i<mActiveServices.size(); i++) {
479 JobServiceContext serviceContext = mActiveServices.get(i);
Christopher Tate7060b042014-06-09 19:50:00 -0700480 final JobStatus running = serviceContext.getRunningJob();
481 if (running != null && running.matches(job.getUid(), job.getJobId())) {
482 return true;
483 }
484 }
485 return false;
486 }
487
488 /**
Matthew Williams1bde39a2015-10-07 14:29:30 -0700489 * Reschedules the given job based on the job's backoff policy. It doesn't make sense to
490 * specify an override deadline on a failed job (the failed job will run even though it's not
491 * ready), so we reschedule it with {@link JobStatus#NO_LATEST_RUNTIME}, but specify that any
492 * ready job with {@link JobStatus#numFailures} > 0 will be executed.
493 *
Christopher Tate7060b042014-06-09 19:50:00 -0700494 * @param failureToReschedule Provided job status that we will reschedule.
495 * @return A newly instantiated JobStatus with the same constraints as the last job except
496 * with adjusted timing constraints.
Matthew Williams1bde39a2015-10-07 14:29:30 -0700497 *
498 * @see JobHandler#maybeQueueReadyJobsForExecutionLockedH
Christopher Tate7060b042014-06-09 19:50:00 -0700499 */
500 private JobStatus getRescheduleJobForFailure(JobStatus failureToReschedule) {
501 final long elapsedNowMillis = SystemClock.elapsedRealtime();
502 final JobInfo job = failureToReschedule.getJob();
503
504 final long initialBackoffMillis = job.getInitialBackoffMillis();
Matthew Williamsd1c06752014-08-22 14:15:28 -0700505 final int backoffAttempts = failureToReschedule.getNumFailures() + 1;
506 long delayMillis;
Christopher Tate7060b042014-06-09 19:50:00 -0700507
508 switch (job.getBackoffPolicy()) {
Matthew Williamsd1c06752014-08-22 14:15:28 -0700509 case JobInfo.BACKOFF_POLICY_LINEAR:
510 delayMillis = initialBackoffMillis * backoffAttempts;
Christopher Tate7060b042014-06-09 19:50:00 -0700511 break;
512 default:
513 if (DEBUG) {
514 Slog.v(TAG, "Unrecognised back-off policy, defaulting to exponential.");
515 }
Matthew Williamsd1c06752014-08-22 14:15:28 -0700516 case JobInfo.BACKOFF_POLICY_EXPONENTIAL:
517 delayMillis =
518 (long) Math.scalb(initialBackoffMillis, backoffAttempts - 1);
Christopher Tate7060b042014-06-09 19:50:00 -0700519 break;
520 }
Matthew Williamsd1c06752014-08-22 14:15:28 -0700521 delayMillis =
522 Math.min(delayMillis, JobInfo.MAX_BACKOFF_DELAY_MILLIS);
523 return new JobStatus(failureToReschedule, elapsedNowMillis + delayMillis,
524 JobStatus.NO_LATEST_RUNTIME, backoffAttempts);
Christopher Tate7060b042014-06-09 19:50:00 -0700525 }
526
527 /**
Matthew Williams1bde39a2015-10-07 14:29:30 -0700528 * Called after a periodic has executed so we can reschedule it. We take the last execution
529 * time of the job to be the time of completion (i.e. the time at which this function is
530 * called).
Christopher Tate7060b042014-06-09 19:50:00 -0700531 * This could be inaccurate b/c the job can run for as long as
532 * {@link com.android.server.job.JobServiceContext#EXECUTING_TIMESLICE_MILLIS}, but will lead
533 * to underscheduling at least, rather than if we had taken the last execution time to be the
534 * start of the execution.
535 * @return A new job representing the execution criteria for this instantiation of the
536 * recurring job.
537 */
538 private JobStatus getRescheduleJobForPeriodic(JobStatus periodicToReschedule) {
539 final long elapsedNow = SystemClock.elapsedRealtime();
540 // Compute how much of the period is remaining.
Matthew Williams1bde39a2015-10-07 14:29:30 -0700541 long runEarly = 0L;
542
543 // If this periodic was rescheduled it won't have a deadline.
544 if (periodicToReschedule.hasDeadlineConstraint()) {
545 runEarly = Math.max(periodicToReschedule.getLatestRunTimeElapsed() - elapsedNow, 0L);
546 }
Christopher Tate7060b042014-06-09 19:50:00 -0700547 long newEarliestRunTimeElapsed = elapsedNow + runEarly;
548 long period = periodicToReschedule.getJob().getIntervalMillis();
549 long newLatestRuntimeElapsed = newEarliestRunTimeElapsed + period;
550
551 if (DEBUG) {
552 Slog.v(TAG, "Rescheduling executed periodic. New execution window [" +
553 newEarliestRunTimeElapsed/1000 + ", " + newLatestRuntimeElapsed/1000 + "]s");
554 }
555 return new JobStatus(periodicToReschedule, newEarliestRunTimeElapsed,
556 newLatestRuntimeElapsed, 0 /* backoffAttempt */);
557 }
558
559 // JobCompletedListener implementations.
560
561 /**
562 * A job just finished executing. We fetch the
563 * {@link com.android.server.job.controllers.JobStatus} from the store and depending on
564 * whether we want to reschedule we readd it to the controllers.
565 * @param jobStatus Completed job.
566 * @param needsReschedule Whether the implementing class should reschedule this job.
567 */
568 @Override
569 public void onJobCompleted(JobStatus jobStatus, boolean needsReschedule) {
570 if (DEBUG) {
571 Slog.d(TAG, "Completed " + jobStatus + ", reschedule=" + needsReschedule);
572 }
573 if (!stopTrackingJob(jobStatus)) {
574 if (DEBUG) {
Matthew Williamsee410da2014-07-25 11:30:40 -0700575 Slog.d(TAG, "Could not find job to remove. Was job removed while executing?");
Christopher Tate7060b042014-06-09 19:50:00 -0700576 }
577 return;
578 }
579 if (needsReschedule) {
580 JobStatus rescheduled = getRescheduleJobForFailure(jobStatus);
581 startTrackingJob(rescheduled);
582 } else if (jobStatus.getJob().isPeriodic()) {
583 JobStatus rescheduledPeriodic = getRescheduleJobForPeriodic(jobStatus);
584 startTrackingJob(rescheduledPeriodic);
585 }
586 mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
587 }
588
589 // StateChangedListener implementations.
590
591 /**
Matthew Williams48a30db2014-09-23 13:39:36 -0700592 * Posts a message to the {@link com.android.server.job.JobSchedulerService.JobHandler} that
593 * some controller's state has changed, so as to run through the list of jobs and start/stop
594 * any that are eligible.
Christopher Tate7060b042014-06-09 19:50:00 -0700595 */
596 @Override
597 public void onControllerStateChanged() {
Matthew Williams48a30db2014-09-23 13:39:36 -0700598 mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
Christopher Tate7060b042014-06-09 19:50:00 -0700599 }
600
601 @Override
602 public void onRunJobNow(JobStatus jobStatus) {
603 mHandler.obtainMessage(MSG_JOB_EXPIRED, jobStatus).sendToTarget();
604 }
605
Christopher Tate7060b042014-06-09 19:50:00 -0700606 private class JobHandler extends Handler {
607
608 public JobHandler(Looper looper) {
609 super(looper);
610 }
611
612 @Override
613 public void handleMessage(Message message) {
Matthew Williams48a30db2014-09-23 13:39:36 -0700614 synchronized (mJobs) {
615 if (!mReadyToRock) {
616 return;
617 }
618 }
Christopher Tate7060b042014-06-09 19:50:00 -0700619 switch (message.what) {
620 case MSG_JOB_EXPIRED:
621 synchronized (mJobs) {
622 JobStatus runNow = (JobStatus) message.obj;
Matthew Williamsbafeeb92014-08-08 11:51:06 -0700623 // runNow can be null, which is a controller's way of indicating that its
624 // state is such that all ready jobs should be run immediately.
Matthew Williams48a30db2014-09-23 13:39:36 -0700625 if (runNow != null && !mPendingJobs.contains(runNow)
626 && mJobs.containsJob(runNow)) {
Christopher Tate7060b042014-06-09 19:50:00 -0700627 mPendingJobs.add(runNow);
628 }
Matthew Williams48a30db2014-09-23 13:39:36 -0700629 queueReadyJobsForExecutionLockedH();
Christopher Tate7060b042014-06-09 19:50:00 -0700630 }
Christopher Tate7060b042014-06-09 19:50:00 -0700631 break;
632 case MSG_CHECK_JOB:
Matthew Williams48a30db2014-09-23 13:39:36 -0700633 synchronized (mJobs) {
634 // Check the list of jobs and run some of them if we feel inclined.
635 maybeQueueReadyJobsForExecutionLockedH();
636 }
Christopher Tate7060b042014-06-09 19:50:00 -0700637 break;
638 }
639 maybeRunPendingJobsH();
640 // Don't remove JOB_EXPIRED in case one came along while processing the queue.
641 removeMessages(MSG_CHECK_JOB);
642 }
643
644 /**
645 * Run through list of jobs and execute all possible - at least one is expired so we do
646 * as many as we can.
647 */
Matthew Williams48a30db2014-09-23 13:39:36 -0700648 private void queueReadyJobsForExecutionLockedH() {
649 ArraySet<JobStatus> jobs = mJobs.getJobs();
650 if (DEBUG) {
651 Slog.d(TAG, "queuing all ready jobs for execution:");
652 }
653 for (int i=0; i<jobs.size(); i++) {
654 JobStatus job = jobs.valueAt(i);
655 if (isReadyToBeExecutedLocked(job)) {
656 if (DEBUG) {
657 Slog.d(TAG, " queued " + job.toShortString());
Christopher Tate7060b042014-06-09 19:50:00 -0700658 }
Matthew Williams48a30db2014-09-23 13:39:36 -0700659 mPendingJobs.add(job);
660 } else if (isReadyToBeCancelledLocked(job)) {
661 stopJobOnServiceContextLocked(job);
Christopher Tate7060b042014-06-09 19:50:00 -0700662 }
Matthew Williams48a30db2014-09-23 13:39:36 -0700663 }
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800664 reportActive();
Matthew Williams48a30db2014-09-23 13:39:36 -0700665 if (DEBUG) {
666 final int queuedJobs = mPendingJobs.size();
667 if (queuedJobs == 0) {
668 Slog.d(TAG, "No jobs pending.");
669 } else {
670 Slog.d(TAG, queuedJobs + " jobs queued.");
Matthew Williams75fc5252014-09-02 16:17:53 -0700671 }
Christopher Tate7060b042014-06-09 19:50:00 -0700672 }
673 }
674
675 /**
676 * The state of at least one job has changed. Here is where we could enforce various
677 * policies on when we want to execute jobs.
678 * Right now the policy is such:
679 * If >1 of the ready jobs is idle mode we send all of them off
680 * if more than 2 network connectivity jobs are ready we send them all off.
681 * If more than 4 jobs total are ready we send them all off.
682 * TODO: It would be nice to consolidate these sort of high-level policies somewhere.
683 */
Matthew Williams48a30db2014-09-23 13:39:36 -0700684 private void maybeQueueReadyJobsForExecutionLockedH() {
685 int chargingCount = 0;
686 int idleCount = 0;
687 int backoffCount = 0;
688 int connectivityCount = 0;
689 List<JobStatus> runnableJobs = new ArrayList<JobStatus>();
690 ArraySet<JobStatus> jobs = mJobs.getJobs();
691 for (int i=0; i<jobs.size(); i++) {
692 JobStatus job = jobs.valueAt(i);
693 if (isReadyToBeExecutedLocked(job)) {
694 if (job.getNumFailures() > 0) {
695 backoffCount++;
Christopher Tate7060b042014-06-09 19:50:00 -0700696 }
Matthew Williams48a30db2014-09-23 13:39:36 -0700697 if (job.hasIdleConstraint()) {
698 idleCount++;
699 }
700 if (job.hasConnectivityConstraint() || job.hasUnmeteredConstraint()) {
701 connectivityCount++;
702 }
703 if (job.hasChargingConstraint()) {
704 chargingCount++;
705 }
706 runnableJobs.add(job);
707 } else if (isReadyToBeCancelledLocked(job)) {
708 stopJobOnServiceContextLocked(job);
Christopher Tate7060b042014-06-09 19:50:00 -0700709 }
Matthew Williams48a30db2014-09-23 13:39:36 -0700710 }
711 if (backoffCount > 0 ||
712 idleCount >= MIN_IDLE_COUNT ||
713 connectivityCount >= MIN_CONNECTIVITY_COUNT ||
714 chargingCount >= MIN_CHARGING_COUNT ||
715 runnableJobs.size() >= MIN_READY_JOBS_COUNT) {
Matthew Williamsbe0c4172014-08-06 18:14:16 -0700716 if (DEBUG) {
Matthew Williams48a30db2014-09-23 13:39:36 -0700717 Slog.d(TAG, "maybeQueueReadyJobsForExecutionLockedH: Running jobs.");
Christopher Tate7060b042014-06-09 19:50:00 -0700718 }
Matthew Williams48a30db2014-09-23 13:39:36 -0700719 for (int i=0; i<runnableJobs.size(); i++) {
720 mPendingJobs.add(runnableJobs.get(i));
721 }
722 } else {
723 if (DEBUG) {
724 Slog.d(TAG, "maybeQueueReadyJobsForExecutionLockedH: Not running anything.");
725 }
726 }
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800727 reportActive();
Matthew Williams48a30db2014-09-23 13:39:36 -0700728 if (DEBUG) {
729 Slog.d(TAG, "idle=" + idleCount + " connectivity=" +
730 connectivityCount + " charging=" + chargingCount + " tot=" +
731 runnableJobs.size());
Christopher Tate7060b042014-06-09 19:50:00 -0700732 }
733 }
734
735 /**
736 * Criteria for moving a job into the pending queue:
737 * - It's ready.
738 * - It's not pending.
739 * - It's not already running on a JSC.
Matthew Williams9ae3dbe2014-08-21 13:47:47 -0700740 * - The user that requested the job is running.
Christopher Tate7060b042014-06-09 19:50:00 -0700741 */
742 private boolean isReadyToBeExecutedLocked(JobStatus job) {
Matthew Williams9ae3dbe2014-08-21 13:47:47 -0700743 final boolean jobReady = job.isReady();
744 final boolean jobPending = mPendingJobs.contains(job);
745 final boolean jobActive = isCurrentlyActiveLocked(job);
746 final boolean userRunning = mStartedUsers.contains(job.getUserId());
Matthew Williams9ae3dbe2014-08-21 13:47:47 -0700747 if (DEBUG) {
748 Slog.v(TAG, "isReadyToBeExecutedLocked: " + job.toShortString()
749 + " ready=" + jobReady + " pending=" + jobPending
750 + " active=" + jobActive + " userRunning=" + userRunning);
751 }
752 return userRunning && jobReady && !jobPending && !jobActive;
Christopher Tate7060b042014-06-09 19:50:00 -0700753 }
754
755 /**
756 * Criteria for cancelling an active job:
757 * - It's not ready
758 * - It's running on a JSC.
759 */
760 private boolean isReadyToBeCancelledLocked(JobStatus job) {
761 return !job.isReady() && isCurrentlyActiveLocked(job);
762 }
763
764 /**
765 * Reconcile jobs in the pending queue against available execution contexts.
766 * A controller can force a job into the pending queue even if it's already running, but
767 * here is where we decide whether to actually execute it.
768 */
769 private void maybeRunPendingJobsH() {
770 synchronized (mJobs) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700771 if (mDeviceIdleMode) {
772 // If device is idle, we will not schedule jobs to run.
773 return;
774 }
Christopher Tate7060b042014-06-09 19:50:00 -0700775 Iterator<JobStatus> it = mPendingJobs.iterator();
Matthew Williams75fc5252014-09-02 16:17:53 -0700776 if (DEBUG) {
777 Slog.d(TAG, "pending queue: " + mPendingJobs.size() + " jobs.");
778 }
Christopher Tate7060b042014-06-09 19:50:00 -0700779 while (it.hasNext()) {
780 JobStatus nextPending = it.next();
781 JobServiceContext availableContext = null;
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700782 for (int i=0; i<mActiveServices.size(); i++) {
783 JobServiceContext jsc = mActiveServices.get(i);
Christopher Tate7060b042014-06-09 19:50:00 -0700784 final JobStatus running = jsc.getRunningJob();
785 if (running != null && running.matches(nextPending.getUid(),
786 nextPending.getJobId())) {
Matthew Williamsee410da2014-07-25 11:30:40 -0700787 // Already running this job for this uId, skip.
Christopher Tate7060b042014-06-09 19:50:00 -0700788 availableContext = null;
789 break;
790 }
791 if (jsc.isAvailable()) {
792 availableContext = jsc;
793 }
794 }
795 if (availableContext != null) {
Amith Yamasanib0ff3222015-03-04 09:56:14 -0800796 if (DEBUG) {
797 Slog.d(TAG, "About to run job "
798 + nextPending.getJob().getService().toString());
799 }
Christopher Tate7060b042014-06-09 19:50:00 -0700800 if (!availableContext.executeRunnableJob(nextPending)) {
801 if (DEBUG) {
802 Slog.d(TAG, "Error executing " + nextPending);
803 }
804 mJobs.remove(nextPending);
805 }
806 it.remove();
807 }
808 }
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800809 reportActive();
Christopher Tate7060b042014-06-09 19:50:00 -0700810 }
811 }
812 }
813
814 /**
815 * Binder stub trampoline implementation
816 */
817 final class JobSchedulerStub extends IJobScheduler.Stub {
818 /** Cache determination of whether a given app can persist jobs
819 * key is uid of the calling app; value is undetermined/true/false
820 */
821 private final SparseArray<Boolean> mPersistCache = new SparseArray<Boolean>();
822
823 // Enforce that only the app itself (or shared uid participant) can schedule a
824 // job that runs one of the app's services, as well as verifying that the
825 // named service properly requires the BIND_JOB_SERVICE permission
826 private void enforceValidJobRequest(int uid, JobInfo job) {
Christopher Tate5568f542014-06-18 13:53:31 -0700827 final IPackageManager pm = AppGlobals.getPackageManager();
Christopher Tate7060b042014-06-09 19:50:00 -0700828 final ComponentName service = job.getService();
829 try {
Christopher Tate5568f542014-06-18 13:53:31 -0700830 ServiceInfo si = pm.getServiceInfo(service, 0, UserHandle.getUserId(uid));
831 if (si == null) {
832 throw new IllegalArgumentException("No such service " + service);
833 }
Christopher Tate7060b042014-06-09 19:50:00 -0700834 if (si.applicationInfo.uid != uid) {
835 throw new IllegalArgumentException("uid " + uid +
836 " cannot schedule job in " + service.getPackageName());
837 }
838 if (!JobService.PERMISSION_BIND.equals(si.permission)) {
839 throw new IllegalArgumentException("Scheduled service " + service
840 + " does not require android.permission.BIND_JOB_SERVICE permission");
841 }
Christopher Tate5568f542014-06-18 13:53:31 -0700842 } catch (RemoteException e) {
843 // Can't happen; the Package Manager is in this same process
Christopher Tate7060b042014-06-09 19:50:00 -0700844 }
845 }
846
847 private boolean canPersistJobs(int pid, int uid) {
848 // If we get this far we're good to go; all we need to do now is check
849 // whether the app is allowed to persist its scheduled work.
850 final boolean canPersist;
851 synchronized (mPersistCache) {
852 Boolean cached = mPersistCache.get(uid);
853 if (cached != null) {
854 canPersist = cached.booleanValue();
855 } else {
856 // Persisting jobs is tantamount to running at boot, so we permit
857 // it when the app has declared that it uses the RECEIVE_BOOT_COMPLETED
858 // permission
859 int result = getContext().checkPermission(
860 android.Manifest.permission.RECEIVE_BOOT_COMPLETED, pid, uid);
861 canPersist = (result == PackageManager.PERMISSION_GRANTED);
862 mPersistCache.put(uid, canPersist);
863 }
864 }
865 return canPersist;
866 }
867
868 // IJobScheduler implementation
869 @Override
870 public int schedule(JobInfo job) throws RemoteException {
871 if (DEBUG) {
Matthew Williamsee410da2014-07-25 11:30:40 -0700872 Slog.d(TAG, "Scheduling job: " + job.toString());
Christopher Tate7060b042014-06-09 19:50:00 -0700873 }
874 final int pid = Binder.getCallingPid();
875 final int uid = Binder.getCallingUid();
876
877 enforceValidJobRequest(uid, job);
Matthew Williams900c67f2014-07-09 12:46:53 -0700878 if (job.isPersisted()) {
879 if (!canPersistJobs(pid, uid)) {
880 throw new IllegalArgumentException("Error: requested job be persisted without"
881 + " holding RECEIVE_BOOT_COMPLETED permission.");
882 }
883 }
Christopher Tate7060b042014-06-09 19:50:00 -0700884
885 long ident = Binder.clearCallingIdentity();
886 try {
Matthew Williams900c67f2014-07-09 12:46:53 -0700887 return JobSchedulerService.this.schedule(job, uid);
Christopher Tate7060b042014-06-09 19:50:00 -0700888 } finally {
889 Binder.restoreCallingIdentity(ident);
890 }
891 }
892
893 @Override
894 public List<JobInfo> getAllPendingJobs() throws RemoteException {
895 final int uid = Binder.getCallingUid();
896
897 long ident = Binder.clearCallingIdentity();
898 try {
899 return JobSchedulerService.this.getPendingJobs(uid);
900 } finally {
901 Binder.restoreCallingIdentity(ident);
902 }
903 }
904
905 @Override
906 public void cancelAll() throws RemoteException {
907 final int uid = Binder.getCallingUid();
908
909 long ident = Binder.clearCallingIdentity();
910 try {
911 JobSchedulerService.this.cancelJobsForUid(uid);
912 } finally {
913 Binder.restoreCallingIdentity(ident);
914 }
915 }
916
917 @Override
918 public void cancel(int jobId) throws RemoteException {
919 final int uid = Binder.getCallingUid();
920
921 long ident = Binder.clearCallingIdentity();
922 try {
923 JobSchedulerService.this.cancelJob(uid, jobId);
924 } finally {
925 Binder.restoreCallingIdentity(ident);
926 }
927 }
928
929 /**
930 * "dumpsys" infrastructure
931 */
932 @Override
933 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
934 getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
935
936 long identityToken = Binder.clearCallingIdentity();
937 try {
938 JobSchedulerService.this.dumpInternal(pw);
939 } finally {
940 Binder.restoreCallingIdentity(identityToken);
941 }
942 }
943 };
944
945 void dumpInternal(PrintWriter pw) {
Christopher Tatef973a7b2014-08-29 12:54:08 -0700946 final long now = SystemClock.elapsedRealtime();
Christopher Tate7060b042014-06-09 19:50:00 -0700947 synchronized (mJobs) {
Matthew Williams9ae3dbe2014-08-21 13:47:47 -0700948 pw.print("Started users: ");
949 for (int i=0; i<mStartedUsers.size(); i++) {
950 pw.print("u" + mStartedUsers.get(i) + " ");
951 }
952 pw.println();
Christopher Tate7060b042014-06-09 19:50:00 -0700953 pw.println("Registered jobs:");
954 if (mJobs.size() > 0) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700955 ArraySet<JobStatus> jobs = mJobs.getJobs();
956 for (int i=0; i<jobs.size(); i++) {
957 JobStatus job = jobs.valueAt(i);
Christopher Tate7060b042014-06-09 19:50:00 -0700958 job.dump(pw, " ");
959 }
960 } else {
Christopher Tatef973a7b2014-08-29 12:54:08 -0700961 pw.println(" None.");
Christopher Tate7060b042014-06-09 19:50:00 -0700962 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700963 for (int i=0; i<mControllers.size(); i++) {
Christopher Tate7060b042014-06-09 19:50:00 -0700964 pw.println();
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700965 mControllers.get(i).dumpControllerState(pw);
Christopher Tate7060b042014-06-09 19:50:00 -0700966 }
967 pw.println();
Christopher Tatef973a7b2014-08-29 12:54:08 -0700968 pw.println("Pending:");
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700969 for (int i=0; i<mPendingJobs.size(); i++) {
970 pw.println(mPendingJobs.get(i).hashCode());
Christopher Tate7060b042014-06-09 19:50:00 -0700971 }
972 pw.println();
973 pw.println("Active jobs:");
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700974 for (int i=0; i<mActiveServices.size(); i++) {
975 JobServiceContext jsc = mActiveServices.get(i);
Christopher Tate7060b042014-06-09 19:50:00 -0700976 if (jsc.isAvailable()) {
977 continue;
978 } else {
Christopher Tatef973a7b2014-08-29 12:54:08 -0700979 final long timeout = jsc.getTimeoutElapsed();
980 pw.print("Running for: ");
981 pw.print((now - jsc.getExecutionStartTimeElapsed())/1000);
982 pw.print("s timeout=");
983 pw.print(timeout);
984 pw.print(" fromnow=");
985 pw.println(timeout-now);
986 jsc.getRunningJob().dump(pw, " ");
Christopher Tate7060b042014-06-09 19:50:00 -0700987 }
988 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700989 pw.println();
990 pw.print("mReadyToRock="); pw.println(mReadyToRock);
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700991 pw.print("mDeviceIdleMode="); pw.println(mDeviceIdleMode);
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800992 pw.print("mReportedActive="); pw.println(mReportedActive);
Christopher Tate7060b042014-06-09 19:50:00 -0700993 }
994 pw.println();
995 }
996}