blob: 42ecc06605ff32e9c638b568a86d61a6cb1f1729 [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;
52import android.util.Slog;
53import android.util.SparseArray;
54
Dianne Hackbornfdb19562014-07-11 16:03:36 -070055import com.android.internal.app.IBatteryStats;
Dianne Hackborn627dfa12015-11-11 18:10:30 -080056import com.android.server.DeviceIdleController;
57import com.android.server.LocalServices;
Christopher Tate2f36fd62016-02-18 18:36:08 -080058import com.android.server.job.JobStore.JobStatusFunctor;
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;
Dianne Hackborn1a30bd92016-01-11 11:05:00 -080062import com.android.server.job.controllers.ContentObserverController;
Christopher Tate7060b042014-06-09 19:50:00 -070063import com.android.server.job.controllers.IdleController;
64import com.android.server.job.controllers.JobStatus;
65import com.android.server.job.controllers.StateController;
66import com.android.server.job.controllers.TimeController;
67
Christopher Tate7060b042014-06-09 19:50:00 -070068/**
69 * Responsible for taking jobs representing work to be performed by a client app, and determining
70 * based on the criteria specified when that job should be run against the client application's
71 * endpoint.
72 * Implements logic for scheduling, and rescheduling jobs. The JobSchedulerService knows nothing
73 * about constraints, or the state of active jobs. It receives callbacks from the various
74 * controllers and completed jobs and operates accordingly.
75 *
76 * Note on locking: Any operations that manipulate {@link #mJobs} need to lock on that object.
77 * Any function with the suffix 'Locked' also needs to lock on {@link #mJobs}.
78 * @hide
79 */
Dianne Hackborn33d31c52016-02-16 10:30:33 -080080public final class JobSchedulerService extends com.android.server.SystemService
Matthew Williams01ac45b2014-07-22 20:44:12 -070081 implements StateChangedListener, JobCompletedListener {
Christopher Tate2f36fd62016-02-18 18:36:08 -080082 static final String TAG = "JobSchedulerService";
Matthew Williamsaa984312015-10-15 16:08:05 -070083 public static final boolean DEBUG = false;
Christopher Tate2f36fd62016-02-18 18:36:08 -080084
Christopher Tate7060b042014-06-09 19:50:00 -070085 /** The number of concurrent jobs we run at one time. */
Dianne Hackborn8ad2af72015-03-17 17:00:24 -070086 private static final int MAX_JOB_CONTEXTS_COUNT
Shreyas Basarge8c834c02016-01-07 13:53:16 +000087 = ActivityManager.isLowRamDeviceStatic() ? 3 : 6;
Christopher Tate2f36fd62016-02-18 18:36:08 -080088 /** The maximum number of jobs that we allow an unprivileged app to schedule */
89 private static final int MAX_JOBS_PER_APP = 100;
90
Dianne Hackborn33d31c52016-02-16 10:30:33 -080091 /** Global local for all job scheduler state. */
92 final Object mLock = new Object();
Christopher Tate7060b042014-06-09 19:50:00 -070093 /** Master list of jobs. */
Dianne Hackbornfdb19562014-07-11 16:03:36 -070094 final JobStore mJobs;
Christopher Tate7060b042014-06-09 19:50:00 -070095
96 static final int MSG_JOB_EXPIRED = 0;
97 static final int MSG_CHECK_JOB = 1;
Dianne Hackbornbef28fe2015-10-29 17:57:11 -070098 static final int MSG_STOP_JOB = 2;
Shreyas Basarge4cff8ac2015-12-10 21:32:52 +000099 static final int MSG_CHECK_JOB_GREEDY = 3;
Christopher Tate7060b042014-06-09 19:50:00 -0700100
101 // Policy constants
102 /**
103 * Minimum # of idle jobs that must be ready in order to force the JMS to schedule things
104 * early.
105 */
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700106 static final int MIN_IDLE_COUNT = 1;
Christopher Tate7060b042014-06-09 19:50:00 -0700107 /**
Matthew Williamsbe0c4172014-08-06 18:14:16 -0700108 * Minimum # of charging jobs that must be ready in order to force the JMS to schedule things
109 * early.
110 */
111 static final int MIN_CHARGING_COUNT = 1;
112 /**
Christopher Tate7060b042014-06-09 19:50:00 -0700113 * Minimum # of connectivity jobs that must be ready in order to force the JMS to schedule
114 * things early.
115 */
Matthew Williamsaa984312015-10-15 16:08:05 -0700116 static final int MIN_CONNECTIVITY_COUNT = 1; // Run connectivity jobs as soon as ready.
Christopher Tate7060b042014-06-09 19:50:00 -0700117 /**
Dianne Hackborn1a30bd92016-01-11 11:05:00 -0800118 * Minimum # of content trigger jobs that must be ready in order to force the JMS to schedule
119 * things early.
120 */
121 static final int MIN_CONTENT_COUNT = 1;
122 /**
Christopher Tate7060b042014-06-09 19:50:00 -0700123 * Minimum # of jobs (with no particular constraints) for which the JMS will be happy running
124 * some work early.
Matthew Williamsbe0c4172014-08-06 18:14:16 -0700125 * This is correlated with the amount of batching we'll be able to do.
Christopher Tate7060b042014-06-09 19:50:00 -0700126 */
Matthew Williamsbe0c4172014-08-06 18:14:16 -0700127 static final int MIN_READY_JOBS_COUNT = 2;
Christopher Tate7060b042014-06-09 19:50:00 -0700128
129 /**
130 * Track Services that have currently active or pending jobs. The index is provided by
131 * {@link JobStatus#getServiceToken()}
132 */
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700133 final List<JobServiceContext> mActiveServices = new ArrayList<>();
Christopher Tate7060b042014-06-09 19:50:00 -0700134 /** List of controllers that will notify this service of updates to jobs. */
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700135 List<StateController> mControllers;
Christopher Tate7060b042014-06-09 19:50:00 -0700136 /**
137 * Queue of pending jobs. The JobServiceContext class will receive jobs from this list
138 * when ready to execute them.
139 */
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700140 final ArrayList<JobStatus> mPendingJobs = new ArrayList<>();
Christopher Tate7060b042014-06-09 19:50:00 -0700141
Shreyas Basarge5db09082016-01-07 13:38:29 +0000142 final ArrayList<Integer> mStartedUsers = new ArrayList<>();
Matthew Williams9ae3dbe2014-08-21 13:47:47 -0700143
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700144 final JobHandler mHandler;
145 final JobSchedulerStub mJobSchedulerStub;
146
147 IBatteryStats mBatteryStats;
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700148 PowerManager mPowerManager;
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800149 DeviceIdleController.LocalService mLocalDeviceIdleController;
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700150
151 /**
152 * Set to true once we are allowed to run third party apps.
153 */
154 boolean mReadyToRock;
155
Christopher Tate7060b042014-06-09 19:50:00 -0700156 /**
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700157 * True when in device idle mode, so we don't want to schedule any jobs.
158 */
159 boolean mDeviceIdleMode;
160
161 /**
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800162 * What we last reported to DeviceIdleController about wheter we are active.
163 */
164 boolean mReportedActive;
165
166 /**
Christopher Tate7060b042014-06-09 19:50:00 -0700167 * Cleans up outstanding jobs when a package is removed. Even if it's being replaced later we
168 * still clean up. On reinstall the package will have a new uid.
169 */
170 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
171 @Override
172 public void onReceive(Context context, Intent intent) {
Shreyas Basarge5db09082016-01-07 13:38:29 +0000173 Slog.d(TAG, "Receieved: " + intent.getAction());
174 if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) {
Christopher Tateaad67a32014-10-20 16:29:20 -0700175 // If this is an outright uninstall rather than the first half of an
176 // app update sequence, cancel the jobs associated with the app.
177 if (!intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
178 int uidRemoved = intent.getIntExtra(Intent.EXTRA_UID, -1);
179 if (DEBUG) {
180 Slog.d(TAG, "Removing jobs for uid: " + uidRemoved);
181 }
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700182 cancelJobsForUid(uidRemoved, true);
Christopher Tate7060b042014-06-09 19:50:00 -0700183 }
Shreyas Basarge5db09082016-01-07 13:38:29 +0000184 } else if (Intent.ACTION_USER_REMOVED.equals(intent.getAction())) {
Christopher Tate7060b042014-06-09 19:50:00 -0700185 final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, 0);
186 if (DEBUG) {
187 Slog.d(TAG, "Removing jobs for user: " + userId);
188 }
189 cancelJobsForUser(userId);
Shreyas Basarge5db09082016-01-07 13:38:29 +0000190 } else if (PowerManager.ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED.equals(intent.getAction())
191 || PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED.equals(intent.getAction())) {
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700192 updateIdleMode(mPowerManager != null
193 ? (mPowerManager.isDeviceIdleMode()
Shreyas Basarge5db09082016-01-07 13:38:29 +0000194 || mPowerManager.isLightDeviceIdleMode())
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700195 : false);
Christopher Tate7060b042014-06-09 19:50:00 -0700196 }
197 }
198 };
199
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700200 final private IUidObserver mUidObserver = new IUidObserver.Stub() {
201 @Override public void onUidStateChanged(int uid, int procState) throws RemoteException {
202 }
203
204 @Override public void onUidGone(int uid) throws RemoteException {
205 }
206
207 @Override public void onUidActive(int uid) throws RemoteException {
208 }
209
210 @Override public void onUidIdle(int uid) throws RemoteException {
211 cancelJobsForUid(uid, false);
212 }
213 };
214
Dianne Hackborn33d31c52016-02-16 10:30:33 -0800215 public Object getLock() {
216 return mLock;
217 }
218
Matthew Williams9ae3dbe2014-08-21 13:47:47 -0700219 @Override
220 public void onStartUser(int userHandle) {
Shreyas Basarge5db09082016-01-07 13:38:29 +0000221 mStartedUsers.add(userHandle);
Matthew Williams9ae3dbe2014-08-21 13:47:47 -0700222 // Let's kick any outstanding jobs for this user.
223 mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
224 }
225
226 @Override
227 public void onStopUser(int userHandle) {
Shreyas Basarge5db09082016-01-07 13:38:29 +0000228 mStartedUsers.remove(Integer.valueOf(userHandle));
Matthew Williams9ae3dbe2014-08-21 13:47:47 -0700229 }
230
Christopher Tate7060b042014-06-09 19:50:00 -0700231 /**
232 * Entry point from client to schedule the provided job.
233 * This cancels the job if it's already been scheduled, and replaces it with the one provided.
234 * @param job JobInfo object containing execution parameters
235 * @param uId The package identifier of the application this job is for.
Christopher Tate7060b042014-06-09 19:50:00 -0700236 * @return Result of this operation. See <code>JobScheduler#RESULT_*</code> return codes.
237 */
Matthew Williams900c67f2014-07-09 12:46:53 -0700238 public int schedule(JobInfo job, int uId) {
Shreyas Basarge968ac752016-01-11 23:09:26 +0000239 return scheduleAsPackage(job, uId, null, -1);
240 }
241
242 public int scheduleAsPackage(JobInfo job, int uId, String packageName, int userId) {
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800243 JobStatus jobStatus = JobStatus.createFromJobInfo(job, uId, packageName, userId);
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700244 try {
245 if (ActivityManagerNative.getDefault().getAppStartMode(uId,
246 job.getService().getPackageName()) == ActivityManager.APP_START_MODE_DISABLED) {
247 Slog.w(TAG, "Not scheduling job " + uId + ":" + job.toString()
248 + " -- package not allowed to start");
249 return JobScheduler.RESULT_FAILURE;
250 }
251 } catch (RemoteException e) {
252 }
Dianne Hackborn1a30bd92016-01-11 11:05:00 -0800253 if (DEBUG) Slog.d(TAG, "SCHEDULE: " + jobStatus.toShortString());
254 JobStatus toCancel;
Dianne Hackborn33d31c52016-02-16 10:30:33 -0800255 synchronized (mLock) {
Christopher Tate2f36fd62016-02-18 18:36:08 -0800256 // Jobs on behalf of others don't apply to the per-app job cap
257 if (packageName == null) {
258 if (mJobs.countJobsForUid(uId) > MAX_JOBS_PER_APP) {
259 Slog.w(TAG, "Too many jobs for uid " + uId);
260 throw new IllegalStateException("Apps may not schedule more than "
261 + MAX_JOBS_PER_APP + " distinct jobs");
262 }
263 }
264
Dianne Hackborn1a30bd92016-01-11 11:05:00 -0800265 toCancel = mJobs.getJobByUidAndJobId(uId, job.getId());
266 }
267 startTrackingJob(jobStatus, toCancel);
268 if (toCancel != null) {
269 cancelJobImpl(toCancel);
270 }
Matthew Williamsbafeeb92014-08-08 11:51:06 -0700271 mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
Christopher Tate7060b042014-06-09 19:50:00 -0700272 return JobScheduler.RESULT_SUCCESS;
273 }
274
275 public List<JobInfo> getPendingJobs(int uid) {
Dianne Hackborn33d31c52016-02-16 10:30:33 -0800276 synchronized (mLock) {
Christopher Tate2f36fd62016-02-18 18:36:08 -0800277 List<JobStatus> jobs = mJobs.getJobsByUid(uid);
278 ArrayList<JobInfo> outList = new ArrayList<JobInfo>(jobs.size());
279 for (int i = jobs.size() - 1; i >= 0; i--) {
280 JobStatus job = jobs.get(i);
281 outList.add(job.getJob());
Christopher Tate7060b042014-06-09 19:50:00 -0700282 }
Christopher Tate2f36fd62016-02-18 18:36:08 -0800283 return outList;
Christopher Tate7060b042014-06-09 19:50:00 -0700284 }
Christopher Tate7060b042014-06-09 19:50:00 -0700285 }
286
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700287 void cancelJobsForUser(int userHandle) {
Matthew Williams48a30db2014-09-23 13:39:36 -0700288 List<JobStatus> jobsForUser;
Dianne Hackborn33d31c52016-02-16 10:30:33 -0800289 synchronized (mLock) {
Matthew Williams48a30db2014-09-23 13:39:36 -0700290 jobsForUser = mJobs.getJobsByUser(userHandle);
291 }
292 for (int i=0; i<jobsForUser.size(); i++) {
293 JobStatus toRemove = jobsForUser.get(i);
294 cancelJobImpl(toRemove);
Christopher Tate7060b042014-06-09 19:50:00 -0700295 }
296 }
297
298 /**
299 * Entry point from client to cancel all jobs originating from their uid.
300 * This will remove the job from the master list, and cancel the job if it was staged for
301 * execution or being executed.
Matthew Williams48a30db2014-09-23 13:39:36 -0700302 * @param uid Uid to check against for removal of a job.
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700303 * @param forceAll If true, all jobs for the uid will be canceled; if false, only those
304 * whose apps are stopped.
Christopher Tate7060b042014-06-09 19:50:00 -0700305 */
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700306 public void cancelJobsForUid(int uid, boolean forceAll) {
Matthew Williams48a30db2014-09-23 13:39:36 -0700307 List<JobStatus> jobsForUid;
Dianne Hackborn33d31c52016-02-16 10:30:33 -0800308 synchronized (mLock) {
Matthew Williams48a30db2014-09-23 13:39:36 -0700309 jobsForUid = mJobs.getJobsByUid(uid);
310 }
311 for (int i=0; i<jobsForUid.size(); i++) {
312 JobStatus toRemove = jobsForUid.get(i);
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700313 if (!forceAll) {
314 String packageName = toRemove.getServiceComponent().getPackageName();
315 try {
316 if (ActivityManagerNative.getDefault().getAppStartMode(uid, packageName)
317 != ActivityManager.APP_START_MODE_DISABLED) {
318 continue;
319 }
320 } catch (RemoteException e) {
321 }
322 }
Matthew Williams48a30db2014-09-23 13:39:36 -0700323 cancelJobImpl(toRemove);
Christopher Tate7060b042014-06-09 19:50:00 -0700324 }
325 }
326
327 /**
328 * Entry point from client to cancel the job corresponding to the jobId provided.
329 * This will remove the job from the master list, and cancel the job if it was staged for
330 * execution or being executed.
331 * @param uid Uid of the calling client.
332 * @param jobId Id of the job, provided at schedule-time.
333 */
334 public void cancelJob(int uid, int jobId) {
335 JobStatus toCancel;
Dianne Hackborn33d31c52016-02-16 10:30:33 -0800336 synchronized (mLock) {
Christopher Tate7060b042014-06-09 19:50:00 -0700337 toCancel = mJobs.getJobByUidAndJobId(uid, jobId);
Matthew Williams48a30db2014-09-23 13:39:36 -0700338 }
339 if (toCancel != null) {
340 cancelJobImpl(toCancel);
Christopher Tate7060b042014-06-09 19:50:00 -0700341 }
342 }
343
Matthew Williams48a30db2014-09-23 13:39:36 -0700344 private void cancelJobImpl(JobStatus cancelled) {
Dianne Hackborn1a30bd92016-01-11 11:05:00 -0800345 if (DEBUG) Slog.d(TAG, "CANCEL: " + cancelled.toShortString());
Shreyas Basarge73f10252016-02-11 17:06:13 +0000346 stopTrackingJob(cancelled, true /* writeBack */);
Dianne Hackborn33d31c52016-02-16 10:30:33 -0800347 synchronized (mLock) {
Matthew Williams48a30db2014-09-23 13:39:36 -0700348 // Remove from pending queue.
349 mPendingJobs.remove(cancelled);
350 // Cancel if running.
Shreyas Basarge5db09082016-01-07 13:38:29 +0000351 stopJobOnServiceContextLocked(cancelled, JobParameters.REASON_CANCELED);
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800352 reportActive();
Matthew Williams48a30db2014-09-23 13:39:36 -0700353 }
Christopher Tate7060b042014-06-09 19:50:00 -0700354 }
355
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700356 void updateIdleMode(boolean enabled) {
357 boolean changed = false;
358 boolean rocking;
Dianne Hackborn33d31c52016-02-16 10:30:33 -0800359 synchronized (mLock) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700360 if (mDeviceIdleMode != enabled) {
361 changed = true;
362 }
363 rocking = mReadyToRock;
364 }
365 if (changed) {
366 if (rocking) {
367 for (int i=0; i<mControllers.size(); i++) {
368 mControllers.get(i).deviceIdleModeChanged(enabled);
369 }
370 }
Dianne Hackborn33d31c52016-02-16 10:30:33 -0800371 synchronized (mLock) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700372 mDeviceIdleMode = enabled;
373 if (enabled) {
374 // When becoming idle, make sure no jobs are actively running.
375 for (int i=0; i<mActiveServices.size(); i++) {
376 JobServiceContext jsc = mActiveServices.get(i);
377 final JobStatus executing = jsc.getRunningJob();
378 if (executing != null) {
Shreyas Basarge5db09082016-01-07 13:38:29 +0000379 jsc.cancelExecutingJob(JobParameters.REASON_DEVICE_IDLE);
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700380 }
381 }
382 } else {
383 // When coming out of idle, allow thing to start back up.
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800384 if (rocking) {
385 if (mLocalDeviceIdleController != null) {
386 if (!mReportedActive) {
387 mReportedActive = true;
388 mLocalDeviceIdleController.setJobsActive(true);
389 }
390 }
391 }
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700392 mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
393 }
394 }
395 }
396 }
397
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800398 void reportActive() {
Shreyas Basarge4cff8ac2015-12-10 21:32:52 +0000399 // active is true if pending queue contains jobs OR some job is running.
400 boolean active = mPendingJobs.size() > 0;
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800401 if (mPendingJobs.size() <= 0) {
402 for (int i=0; i<mActiveServices.size(); i++) {
403 JobServiceContext jsc = mActiveServices.get(i);
Shreyas Basarge5db09082016-01-07 13:38:29 +0000404 if (jsc.getRunningJob() != null) {
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800405 active = true;
406 break;
407 }
408 }
409 }
Shreyas Basarge4cff8ac2015-12-10 21:32:52 +0000410
411 if (mReportedActive != active) {
412 mReportedActive = active;
413 if (mLocalDeviceIdleController != null) {
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800414 mLocalDeviceIdleController.setJobsActive(active);
415 }
416 }
417 }
418
Christopher Tate7060b042014-06-09 19:50:00 -0700419 /**
420 * Initializes the system service.
421 * <p>
422 * Subclasses must define a single argument constructor that accepts the context
423 * and passes it to super.
424 * </p>
425 *
426 * @param context The system server context.
427 */
428 public JobSchedulerService(Context context) {
429 super(context);
430 // Create the controllers.
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700431 mControllers = new ArrayList<StateController>();
Christopher Tate7060b042014-06-09 19:50:00 -0700432 mControllers.add(ConnectivityController.get(this));
433 mControllers.add(TimeController.get(this));
434 mControllers.add(IdleController.get(this));
435 mControllers.add(BatteryController.get(this));
Amith Yamasanib0ff3222015-03-04 09:56:14 -0800436 mControllers.add(AppIdleController.get(this));
Dianne Hackborn1a30bd92016-01-11 11:05:00 -0800437 mControllers.add(ContentObserverController.get(this));
Christopher Tate7060b042014-06-09 19:50:00 -0700438
439 mHandler = new JobHandler(context.getMainLooper());
440 mJobSchedulerStub = new JobSchedulerStub();
Christopher Tate7060b042014-06-09 19:50:00 -0700441 mJobs = JobStore.initAndGet(this);
442 }
443
444 @Override
445 public void onStart() {
446 publishBinderService(Context.JOB_SCHEDULER_SERVICE, mJobSchedulerStub);
447 }
448
449 @Override
450 public void onBootPhase(int phase) {
451 if (PHASE_SYSTEM_SERVICES_READY == phase) {
Shreyas Basarge5db09082016-01-07 13:38:29 +0000452 // Register br for package removals and user removals.
Christopher Tate7060b042014-06-09 19:50:00 -0700453 final IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_REMOVED);
454 filter.addDataScheme("package");
455 getContext().registerReceiverAsUser(
456 mBroadcastReceiver, UserHandle.ALL, filter, null, null);
457 final IntentFilter userFilter = new IntentFilter(Intent.ACTION_USER_REMOVED);
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700458 userFilter.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED);
Dianne Hackborn08c47a52015-10-15 12:38:14 -0700459 userFilter.addAction(PowerManager.ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED);
Christopher Tate7060b042014-06-09 19:50:00 -0700460 getContext().registerReceiverAsUser(
461 mBroadcastReceiver, UserHandle.ALL, userFilter, null, null);
Shreyas Basarge5db09082016-01-07 13:38:29 +0000462 mPowerManager = (PowerManager)getContext().getSystemService(Context.POWER_SERVICE);
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700463 try {
464 ActivityManagerNative.getDefault().registerUidObserver(mUidObserver,
465 ActivityManager.UID_OBSERVER_IDLE);
466 } catch (RemoteException e) {
467 // ignored; both services live in system_server
468 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700469 } else if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {
Dianne Hackborn33d31c52016-02-16 10:30:33 -0800470 synchronized (mLock) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700471 // Let's go!
472 mReadyToRock = true;
473 mBatteryStats = IBatteryStats.Stub.asInterface(ServiceManager.getService(
474 BatteryStats.SERVICE_NAME));
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800475 mLocalDeviceIdleController
476 = LocalServices.getService(DeviceIdleController.LocalService.class);
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700477 // Create the "runners".
478 for (int i = 0; i < MAX_JOB_CONTEXTS_COUNT; i++) {
479 mActiveServices.add(
480 new JobServiceContext(this, mBatteryStats,
481 getContext().getMainLooper()));
482 }
483 // Attach jobs to their controllers.
Christopher Tate2f36fd62016-02-18 18:36:08 -0800484 mJobs.forEachJob(new JobStatusFunctor() {
485 @Override
486 public void process(JobStatus job) {
487 for (int controller = 0; controller < mControllers.size(); controller++) {
488 final StateController sc = mControllers.get(controller);
489 sc.deviceIdleModeChanged(mDeviceIdleMode);
490 sc.maybeStartTrackingJobLocked(job, null);
491 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700492 }
Christopher Tate2f36fd62016-02-18 18:36:08 -0800493 });
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700494 // GO GO GO!
495 mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
496 }
Christopher Tate7060b042014-06-09 19:50:00 -0700497 }
498 }
499
500 /**
501 * Called when we have a job status object that we need to insert in our
502 * {@link com.android.server.job.JobStore}, and make sure all the relevant controllers know
503 * about.
504 */
Dianne Hackborn1a30bd92016-01-11 11:05:00 -0800505 private void startTrackingJob(JobStatus jobStatus, JobStatus lastJob) {
Dianne Hackborn33d31c52016-02-16 10:30:33 -0800506 synchronized (mLock) {
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800507 final boolean update = mJobs.add(jobStatus);
508 if (mReadyToRock) {
509 for (int i = 0; i < mControllers.size(); i++) {
510 StateController controller = mControllers.get(i);
511 if (update) {
512 controller.maybeStopTrackingJobLocked(jobStatus, true);
513 }
514 controller.maybeStartTrackingJobLocked(jobStatus, lastJob);
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700515 }
Christopher Tate7060b042014-06-09 19:50:00 -0700516 }
Christopher Tate7060b042014-06-09 19:50:00 -0700517 }
518 }
519
520 /**
521 * Called when we want to remove a JobStatus object that we've finished executing. Returns the
522 * object removed.
523 */
Shreyas Basarge73f10252016-02-11 17:06:13 +0000524 private boolean stopTrackingJob(JobStatus jobStatus, boolean writeBack) {
Dianne Hackborn33d31c52016-02-16 10:30:33 -0800525 synchronized (mLock) {
Christopher Tate7060b042014-06-09 19:50:00 -0700526 // Remove from store as well as controllers.
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800527 final boolean removed = mJobs.remove(jobStatus, writeBack);
528 if (removed && mReadyToRock) {
529 for (int i=0; i<mControllers.size(); i++) {
530 StateController controller = mControllers.get(i);
531 controller.maybeStopTrackingJobLocked(jobStatus, false);
532 }
Christopher Tate7060b042014-06-09 19:50:00 -0700533 }
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800534 return removed;
Christopher Tate7060b042014-06-09 19:50:00 -0700535 }
Christopher Tate7060b042014-06-09 19:50:00 -0700536 }
537
Shreyas Basarge5db09082016-01-07 13:38:29 +0000538 private boolean stopJobOnServiceContextLocked(JobStatus job, int reason) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700539 for (int i=0; i<mActiveServices.size(); i++) {
540 JobServiceContext jsc = mActiveServices.get(i);
Christopher Tate7060b042014-06-09 19:50:00 -0700541 final JobStatus executing = jsc.getRunningJob();
542 if (executing != null && executing.matches(job.getUid(), job.getJobId())) {
Shreyas Basarge5db09082016-01-07 13:38:29 +0000543 jsc.cancelExecutingJob(reason);
Christopher Tate7060b042014-06-09 19:50:00 -0700544 return true;
545 }
546 }
547 return false;
548 }
549
550 /**
551 * @param job JobStatus we are querying against.
552 * @return Whether or not the job represented by the status object is currently being run or
553 * is pending.
554 */
555 private boolean isCurrentlyActiveLocked(JobStatus job) {
Dianne Hackbornfdb19562014-07-11 16:03:36 -0700556 for (int i=0; i<mActiveServices.size(); i++) {
557 JobServiceContext serviceContext = mActiveServices.get(i);
Christopher Tate7060b042014-06-09 19:50:00 -0700558 final JobStatus running = serviceContext.getRunningJob();
559 if (running != null && running.matches(job.getUid(), job.getJobId())) {
560 return true;
561 }
562 }
563 return false;
564 }
565
566 /**
Matthew Williams1bde39a2015-10-07 14:29:30 -0700567 * Reschedules the given job based on the job's backoff policy. It doesn't make sense to
568 * specify an override deadline on a failed job (the failed job will run even though it's not
569 * ready), so we reschedule it with {@link JobStatus#NO_LATEST_RUNTIME}, but specify that any
570 * ready job with {@link JobStatus#numFailures} > 0 will be executed.
571 *
Christopher Tate7060b042014-06-09 19:50:00 -0700572 * @param failureToReschedule Provided job status that we will reschedule.
573 * @return A newly instantiated JobStatus with the same constraints as the last job except
574 * with adjusted timing constraints.
Matthew Williams1bde39a2015-10-07 14:29:30 -0700575 *
576 * @see JobHandler#maybeQueueReadyJobsForExecutionLockedH
Christopher Tate7060b042014-06-09 19:50:00 -0700577 */
578 private JobStatus getRescheduleJobForFailure(JobStatus failureToReschedule) {
579 final long elapsedNowMillis = SystemClock.elapsedRealtime();
580 final JobInfo job = failureToReschedule.getJob();
581
582 final long initialBackoffMillis = job.getInitialBackoffMillis();
Matthew Williamsd1c06752014-08-22 14:15:28 -0700583 final int backoffAttempts = failureToReschedule.getNumFailures() + 1;
584 long delayMillis;
Christopher Tate7060b042014-06-09 19:50:00 -0700585
586 switch (job.getBackoffPolicy()) {
Matthew Williamsd1c06752014-08-22 14:15:28 -0700587 case JobInfo.BACKOFF_POLICY_LINEAR:
588 delayMillis = initialBackoffMillis * backoffAttempts;
Christopher Tate7060b042014-06-09 19:50:00 -0700589 break;
590 default:
591 if (DEBUG) {
592 Slog.v(TAG, "Unrecognised back-off policy, defaulting to exponential.");
593 }
Matthew Williamsd1c06752014-08-22 14:15:28 -0700594 case JobInfo.BACKOFF_POLICY_EXPONENTIAL:
595 delayMillis =
596 (long) Math.scalb(initialBackoffMillis, backoffAttempts - 1);
Christopher Tate7060b042014-06-09 19:50:00 -0700597 break;
598 }
Matthew Williamsd1c06752014-08-22 14:15:28 -0700599 delayMillis =
600 Math.min(delayMillis, JobInfo.MAX_BACKOFF_DELAY_MILLIS);
Dianne Hackborn1a30bd92016-01-11 11:05:00 -0800601 JobStatus newJob = new JobStatus(failureToReschedule, elapsedNowMillis + delayMillis,
Matthew Williamsd1c06752014-08-22 14:15:28 -0700602 JobStatus.NO_LATEST_RUNTIME, backoffAttempts);
Dianne Hackborn1a30bd92016-01-11 11:05:00 -0800603 for (int ic=0; ic<mControllers.size(); ic++) {
604 StateController controller = mControllers.get(ic);
605 controller.rescheduleForFailure(newJob, failureToReschedule);
606 }
607 return newJob;
Christopher Tate7060b042014-06-09 19:50:00 -0700608 }
609
610 /**
Matthew Williams1bde39a2015-10-07 14:29:30 -0700611 * Called after a periodic has executed so we can reschedule it. We take the last execution
612 * time of the job to be the time of completion (i.e. the time at which this function is
613 * called).
Christopher Tate7060b042014-06-09 19:50:00 -0700614 * This could be inaccurate b/c the job can run for as long as
615 * {@link com.android.server.job.JobServiceContext#EXECUTING_TIMESLICE_MILLIS}, but will lead
616 * to underscheduling at least, rather than if we had taken the last execution time to be the
617 * start of the execution.
618 * @return A new job representing the execution criteria for this instantiation of the
619 * recurring job.
620 */
621 private JobStatus getRescheduleJobForPeriodic(JobStatus periodicToReschedule) {
622 final long elapsedNow = SystemClock.elapsedRealtime();
623 // Compute how much of the period is remaining.
Matthew Williams1bde39a2015-10-07 14:29:30 -0700624 long runEarly = 0L;
625
626 // If this periodic was rescheduled it won't have a deadline.
627 if (periodicToReschedule.hasDeadlineConstraint()) {
628 runEarly = Math.max(periodicToReschedule.getLatestRunTimeElapsed() - elapsedNow, 0L);
629 }
Shreyas Basarge89ee6182015-12-17 15:16:36 +0000630 long flex = periodicToReschedule.getJob().getFlexMillis();
Christopher Tate7060b042014-06-09 19:50:00 -0700631 long period = periodicToReschedule.getJob().getIntervalMillis();
Shreyas Basarge89ee6182015-12-17 15:16:36 +0000632 long newLatestRuntimeElapsed = elapsedNow + runEarly + period;
633 long newEarliestRunTimeElapsed = newLatestRuntimeElapsed - flex;
Christopher Tate7060b042014-06-09 19:50:00 -0700634
635 if (DEBUG) {
636 Slog.v(TAG, "Rescheduling executed periodic. New execution window [" +
637 newEarliestRunTimeElapsed/1000 + ", " + newLatestRuntimeElapsed/1000 + "]s");
638 }
639 return new JobStatus(periodicToReschedule, newEarliestRunTimeElapsed,
640 newLatestRuntimeElapsed, 0 /* backoffAttempt */);
641 }
642
643 // JobCompletedListener implementations.
644
645 /**
646 * A job just finished executing. We fetch the
647 * {@link com.android.server.job.controllers.JobStatus} from the store and depending on
648 * whether we want to reschedule we readd it to the controllers.
649 * @param jobStatus Completed job.
650 * @param needsReschedule Whether the implementing class should reschedule this job.
651 */
652 @Override
653 public void onJobCompleted(JobStatus jobStatus, boolean needsReschedule) {
654 if (DEBUG) {
655 Slog.d(TAG, "Completed " + jobStatus + ", reschedule=" + needsReschedule);
656 }
Shreyas Basarge73f10252016-02-11 17:06:13 +0000657 // Do not write back immediately if this is a periodic job. The job may get lost if system
658 // shuts down before it is added back.
659 if (!stopTrackingJob(jobStatus, !jobStatus.getJob().isPeriodic())) {
Christopher Tate7060b042014-06-09 19:50:00 -0700660 if (DEBUG) {
Matthew Williamsee410da2014-07-25 11:30:40 -0700661 Slog.d(TAG, "Could not find job to remove. Was job removed while executing?");
Christopher Tate7060b042014-06-09 19:50:00 -0700662 }
Dianne Hackborn1a30bd92016-01-11 11:05:00 -0800663 // We still want to check for jobs to execute, because this job may have
664 // scheduled a new job under the same job id, and now we can run it.
665 mHandler.obtainMessage(MSG_CHECK_JOB_GREEDY).sendToTarget();
Christopher Tate7060b042014-06-09 19:50:00 -0700666 return;
667 }
Dianne Hackborn1a30bd92016-01-11 11:05:00 -0800668 // Note: there is a small window of time in here where, when rescheduling a job,
669 // we will stop monitoring its content providers. This should be fixed by stopping
670 // the old job after scheduling the new one, but since we have no lock held here
671 // that may cause ordering problems if the app removes jobStatus while in here.
Christopher Tate7060b042014-06-09 19:50:00 -0700672 if (needsReschedule) {
673 JobStatus rescheduled = getRescheduleJobForFailure(jobStatus);
Dianne Hackborn1a30bd92016-01-11 11:05:00 -0800674 startTrackingJob(rescheduled, jobStatus);
Christopher Tate7060b042014-06-09 19:50:00 -0700675 } else if (jobStatus.getJob().isPeriodic()) {
676 JobStatus rescheduledPeriodic = getRescheduleJobForPeriodic(jobStatus);
Dianne Hackborn1a30bd92016-01-11 11:05:00 -0800677 startTrackingJob(rescheduledPeriodic, jobStatus);
Christopher Tate7060b042014-06-09 19:50:00 -0700678 }
Shreyas Basarge4cff8ac2015-12-10 21:32:52 +0000679 reportActive();
680 mHandler.obtainMessage(MSG_CHECK_JOB_GREEDY).sendToTarget();
Christopher Tate7060b042014-06-09 19:50:00 -0700681 }
682
683 // StateChangedListener implementations.
684
685 /**
Matthew Williams48a30db2014-09-23 13:39:36 -0700686 * Posts a message to the {@link com.android.server.job.JobSchedulerService.JobHandler} that
687 * some controller's state has changed, so as to run through the list of jobs and start/stop
688 * any that are eligible.
Christopher Tate7060b042014-06-09 19:50:00 -0700689 */
690 @Override
691 public void onControllerStateChanged() {
Matthew Williams48a30db2014-09-23 13:39:36 -0700692 mHandler.obtainMessage(MSG_CHECK_JOB).sendToTarget();
Christopher Tate7060b042014-06-09 19:50:00 -0700693 }
694
695 @Override
696 public void onRunJobNow(JobStatus jobStatus) {
697 mHandler.obtainMessage(MSG_JOB_EXPIRED, jobStatus).sendToTarget();
698 }
699
Christopher Tate7060b042014-06-09 19:50:00 -0700700 private class JobHandler extends Handler {
701
702 public JobHandler(Looper looper) {
703 super(looper);
704 }
705
706 @Override
707 public void handleMessage(Message message) {
Dianne Hackborn33d31c52016-02-16 10:30:33 -0800708 synchronized (mLock) {
Matthew Williams48a30db2014-09-23 13:39:36 -0700709 if (!mReadyToRock) {
710 return;
711 }
712 }
Christopher Tate7060b042014-06-09 19:50:00 -0700713 switch (message.what) {
714 case MSG_JOB_EXPIRED:
Dianne Hackborn33d31c52016-02-16 10:30:33 -0800715 synchronized (mLock) {
Christopher Tate7060b042014-06-09 19:50:00 -0700716 JobStatus runNow = (JobStatus) message.obj;
Matthew Williamsbafeeb92014-08-08 11:51:06 -0700717 // runNow can be null, which is a controller's way of indicating that its
718 // state is such that all ready jobs should be run immediately.
Matthew Williams48a30db2014-09-23 13:39:36 -0700719 if (runNow != null && !mPendingJobs.contains(runNow)
720 && mJobs.containsJob(runNow)) {
Christopher Tate7060b042014-06-09 19:50:00 -0700721 mPendingJobs.add(runNow);
722 }
Matthew Williams48a30db2014-09-23 13:39:36 -0700723 queueReadyJobsForExecutionLockedH();
Christopher Tate7060b042014-06-09 19:50:00 -0700724 }
Christopher Tate7060b042014-06-09 19:50:00 -0700725 break;
726 case MSG_CHECK_JOB:
Dianne Hackborn33d31c52016-02-16 10:30:33 -0800727 synchronized (mLock) {
Shreyas Basarge4cff8ac2015-12-10 21:32:52 +0000728 if (mReportedActive) {
729 // if jobs are currently being run, queue all ready jobs for execution.
730 queueReadyJobsForExecutionLockedH();
731 } else {
732 // Check the list of jobs and run some of them if we feel inclined.
733 maybeQueueReadyJobsForExecutionLockedH();
734 }
735 }
736 break;
737 case MSG_CHECK_JOB_GREEDY:
Dianne Hackborn33d31c52016-02-16 10:30:33 -0800738 synchronized (mLock) {
Shreyas Basarge4cff8ac2015-12-10 21:32:52 +0000739 queueReadyJobsForExecutionLockedH();
Matthew Williams48a30db2014-09-23 13:39:36 -0700740 }
Christopher Tate7060b042014-06-09 19:50:00 -0700741 break;
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700742 case MSG_STOP_JOB:
743 cancelJobImpl((JobStatus)message.obj);
744 break;
Christopher Tate7060b042014-06-09 19:50:00 -0700745 }
746 maybeRunPendingJobsH();
747 // Don't remove JOB_EXPIRED in case one came along while processing the queue.
748 removeMessages(MSG_CHECK_JOB);
749 }
750
751 /**
752 * Run through list of jobs and execute all possible - at least one is expired so we do
753 * as many as we can.
754 */
Matthew Williams48a30db2014-09-23 13:39:36 -0700755 private void queueReadyJobsForExecutionLockedH() {
Matthew Williams48a30db2014-09-23 13:39:36 -0700756 if (DEBUG) {
757 Slog.d(TAG, "queuing all ready jobs for execution:");
758 }
Christopher Tate2f36fd62016-02-18 18:36:08 -0800759 mPendingJobs.clear();
760 mJobs.forEachJob(mReadyQueueFunctor);
761 mReadyQueueFunctor.postProcess();
762
Matthew Williams48a30db2014-09-23 13:39:36 -0700763 if (DEBUG) {
764 final int queuedJobs = mPendingJobs.size();
765 if (queuedJobs == 0) {
766 Slog.d(TAG, "No jobs pending.");
767 } else {
768 Slog.d(TAG, queuedJobs + " jobs queued.");
Matthew Williams75fc5252014-09-02 16:17:53 -0700769 }
Christopher Tate7060b042014-06-09 19:50:00 -0700770 }
771 }
772
Christopher Tate2f36fd62016-02-18 18:36:08 -0800773 class ReadyJobQueueFunctor implements JobStatusFunctor {
774 ArrayList<JobStatus> newReadyJobs;
775
776 @Override
777 public void process(JobStatus job) {
778 if (isReadyToBeExecutedLocked(job)) {
779 if (DEBUG) {
780 Slog.d(TAG, " queued " + job.toShortString());
781 }
782 if (newReadyJobs == null) {
783 newReadyJobs = new ArrayList<JobStatus>();
784 }
785 newReadyJobs.add(job);
786 } else if (areJobConstraintsNotSatisfiedLocked(job)) {
787 stopJobOnServiceContextLocked(job,
788 JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED);
789 }
790 }
791
792 public void postProcess() {
793 if (newReadyJobs != null) {
794 mPendingJobs.addAll(newReadyJobs);
795 }
796 newReadyJobs = null;
797 }
798 }
799 private final ReadyJobQueueFunctor mReadyQueueFunctor = new ReadyJobQueueFunctor();
800
Christopher Tate7060b042014-06-09 19:50:00 -0700801 /**
802 * The state of at least one job has changed. Here is where we could enforce various
803 * policies on when we want to execute jobs.
804 * Right now the policy is such:
805 * If >1 of the ready jobs is idle mode we send all of them off
806 * if more than 2 network connectivity jobs are ready we send them all off.
807 * If more than 4 jobs total are ready we send them all off.
808 * TODO: It would be nice to consolidate these sort of high-level policies somewhere.
809 */
Christopher Tate2f36fd62016-02-18 18:36:08 -0800810 class MaybeReadyJobQueueFunctor implements JobStatusFunctor {
811 int chargingCount;
812 int idleCount;
813 int backoffCount;
814 int connectivityCount;
815 int contentCount;
816 List<JobStatus> runnableJobs;
817
818 public MaybeReadyJobQueueFunctor() {
819 reset();
820 }
821
822 // Functor method invoked for each job via JobStore.forEachJob()
823 @Override
824 public void process(JobStatus job) {
Matthew Williams48a30db2014-09-23 13:39:36 -0700825 if (isReadyToBeExecutedLocked(job)) {
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700826 try {
827 if (ActivityManagerNative.getDefault().getAppStartMode(job.getUid(),
828 job.getJob().getService().getPackageName())
829 == ActivityManager.APP_START_MODE_DISABLED) {
830 Slog.w(TAG, "Aborting job " + job.getUid() + ":"
831 + job.getJob().toString() + " -- package not allowed to start");
832 mHandler.obtainMessage(MSG_STOP_JOB, job).sendToTarget();
Christopher Tate2f36fd62016-02-18 18:36:08 -0800833 return;
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700834 }
835 } catch (RemoteException e) {
836 }
Matthew Williams48a30db2014-09-23 13:39:36 -0700837 if (job.getNumFailures() > 0) {
838 backoffCount++;
Christopher Tate7060b042014-06-09 19:50:00 -0700839 }
Matthew Williams48a30db2014-09-23 13:39:36 -0700840 if (job.hasIdleConstraint()) {
841 idleCount++;
842 }
843 if (job.hasConnectivityConstraint() || job.hasUnmeteredConstraint()) {
844 connectivityCount++;
845 }
846 if (job.hasChargingConstraint()) {
847 chargingCount++;
848 }
Dianne Hackborn1a30bd92016-01-11 11:05:00 -0800849 if (job.hasContentTriggerConstraint()) {
850 contentCount++;
851 }
Dianne Hackbornbef28fe2015-10-29 17:57:11 -0700852 if (runnableJobs == null) {
853 runnableJobs = new ArrayList<>();
854 }
Matthew Williams48a30db2014-09-23 13:39:36 -0700855 runnableJobs.add(job);
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800856 } else if (areJobConstraintsNotSatisfiedLocked(job)) {
Shreyas Basarge5db09082016-01-07 13:38:29 +0000857 stopJobOnServiceContextLocked(job,
858 JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED);
Christopher Tate7060b042014-06-09 19:50:00 -0700859 }
Matthew Williams48a30db2014-09-23 13:39:36 -0700860 }
Christopher Tate2f36fd62016-02-18 18:36:08 -0800861
862 public void postProcess() {
863 if (backoffCount > 0 ||
864 idleCount >= MIN_IDLE_COUNT ||
865 connectivityCount >= MIN_CONNECTIVITY_COUNT ||
866 chargingCount >= MIN_CHARGING_COUNT ||
867 contentCount >= MIN_CONTENT_COUNT ||
868 (runnableJobs != null && runnableJobs.size() >= MIN_READY_JOBS_COUNT)) {
869 if (DEBUG) {
870 Slog.d(TAG, "maybeQueueReadyJobsForExecutionLockedH: Running jobs.");
871 }
872 mPendingJobs.addAll(runnableJobs);
873 } else {
874 if (DEBUG) {
875 Slog.d(TAG, "maybeQueueReadyJobsForExecutionLockedH: Not running anything.");
876 }
Christopher Tate7060b042014-06-09 19:50:00 -0700877 }
Christopher Tate2f36fd62016-02-18 18:36:08 -0800878
879 // Be ready for next time
880 reset();
Matthew Williams48a30db2014-09-23 13:39:36 -0700881 }
Christopher Tate2f36fd62016-02-18 18:36:08 -0800882
883 private void reset() {
884 chargingCount = 0;
885 idleCount = 0;
886 backoffCount = 0;
887 connectivityCount = 0;
888 contentCount = 0;
889 runnableJobs = null;
890 }
891 }
892 private final MaybeReadyJobQueueFunctor mMaybeQueueFunctor = new MaybeReadyJobQueueFunctor();
893
894 private void maybeQueueReadyJobsForExecutionLockedH() {
895 if (DEBUG) Slog.d(TAG, "Maybe queuing ready jobs...");
896
897 mPendingJobs.clear();
898 mJobs.forEachJob(mMaybeQueueFunctor);
899 mMaybeQueueFunctor.postProcess();
Christopher Tate7060b042014-06-09 19:50:00 -0700900 }
901
902 /**
903 * Criteria for moving a job into the pending queue:
904 * - It's ready.
905 * - It's not pending.
906 * - It's not already running on a JSC.
Matthew Williams9ae3dbe2014-08-21 13:47:47 -0700907 * - The user that requested the job is running.
Christopher Tate7060b042014-06-09 19:50:00 -0700908 */
909 private boolean isReadyToBeExecutedLocked(JobStatus job) {
Matthew Williams9ae3dbe2014-08-21 13:47:47 -0700910 final boolean jobReady = job.isReady();
911 final boolean jobPending = mPendingJobs.contains(job);
912 final boolean jobActive = isCurrentlyActiveLocked(job);
Shreyas Basarge5db09082016-01-07 13:38:29 +0000913 final boolean userRunning = mStartedUsers.contains(job.getUserId());
Matthew Williams9ae3dbe2014-08-21 13:47:47 -0700914 if (DEBUG) {
915 Slog.v(TAG, "isReadyToBeExecutedLocked: " + job.toShortString()
916 + " ready=" + jobReady + " pending=" + jobPending
Shreyas Basarge5db09082016-01-07 13:38:29 +0000917 + " active=" + jobActive + " userRunning=" + userRunning);
Matthew Williams9ae3dbe2014-08-21 13:47:47 -0700918 }
Shreyas Basarge5db09082016-01-07 13:38:29 +0000919 return userRunning && jobReady && !jobPending && !jobActive;
Christopher Tate7060b042014-06-09 19:50:00 -0700920 }
921
922 /**
923 * Criteria for cancelling an active job:
924 * - It's not ready
925 * - It's running on a JSC.
926 */
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800927 private boolean areJobConstraintsNotSatisfiedLocked(JobStatus job) {
Christopher Tate7060b042014-06-09 19:50:00 -0700928 return !job.isReady() && isCurrentlyActiveLocked(job);
929 }
930
931 /**
932 * Reconcile jobs in the pending queue against available execution contexts.
933 * A controller can force a job into the pending queue even if it's already running, but
934 * here is where we decide whether to actually execute it.
935 */
936 private void maybeRunPendingJobsH() {
Dianne Hackborn33d31c52016-02-16 10:30:33 -0800937 synchronized (mLock) {
Dianne Hackborn88e98df2015-03-23 13:29:14 -0700938 if (mDeviceIdleMode) {
939 // If device is idle, we will not schedule jobs to run.
940 return;
941 }
Matthew Williams75fc5252014-09-02 16:17:53 -0700942 if (DEBUG) {
943 Slog.d(TAG, "pending queue: " + mPendingJobs.size() + " jobs.");
944 }
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800945 assignJobsToContextsLocked();
Dianne Hackborn627dfa12015-11-11 18:10:30 -0800946 reportActive();
Christopher Tate7060b042014-06-09 19:50:00 -0700947 }
948 }
949 }
950
951 /**
Shreyas Basarge5db09082016-01-07 13:38:29 +0000952 * Takes jobs from pending queue and runs them on available contexts.
953 * If no contexts are available, preempts lower priority jobs to
954 * run higher priority ones.
955 * Lock on mJobs before calling this function.
956 */
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800957 private void assignJobsToContextsLocked() {
Shreyas Basarge5db09082016-01-07 13:38:29 +0000958 if (DEBUG) {
959 Slog.d(TAG, printPendingQueue());
960 }
961
962 // This array essentially stores the state of mActiveServices array.
963 // ith index stores the job present on the ith JobServiceContext.
964 // We manipulate this array until we arrive at what jobs should be running on
965 // what JobServiceContext.
966 JobStatus[] contextIdToJobMap = new JobStatus[MAX_JOB_CONTEXTS_COUNT];
967 // Indicates whether we need to act on this jobContext id
968 boolean[] act = new boolean[MAX_JOB_CONTEXTS_COUNT];
969 int[] preferredUidForContext = new int[MAX_JOB_CONTEXTS_COUNT];
970 for (int i=0; i<mActiveServices.size(); i++) {
971 contextIdToJobMap[i] = mActiveServices.get(i).getRunningJob();
972 preferredUidForContext[i] = mActiveServices.get(i).getPreferredUid();
973 }
974 if (DEBUG) {
975 Slog.d(TAG, printContextIdToJobMap(contextIdToJobMap, "running jobs initial"));
976 }
977 Iterator<JobStatus> it = mPendingJobs.iterator();
978 while (it.hasNext()) {
979 JobStatus nextPending = it.next();
980
981 // If job is already running, go to next job.
982 int jobRunningContext = findJobContextIdFromMap(nextPending, contextIdToJobMap);
983 if (jobRunningContext != -1) {
984 continue;
985 }
986
987 // Find a context for nextPending. The context should be available OR
988 // it should have lowest priority among all running jobs
989 // (sharing the same Uid as nextPending)
990 int minPriority = Integer.MAX_VALUE;
991 int minPriorityContextId = -1;
992 for (int i=0; i<mActiveServices.size(); i++) {
993 JobStatus job = contextIdToJobMap[i];
994 int preferredUid = preferredUidForContext[i];
995 if (job == null && (preferredUid == nextPending.getUid() ||
996 preferredUid == JobServiceContext.NO_PREFERRED_UID) ) {
997 minPriorityContextId = i;
998 break;
999 }
Shreyas Basarge347c2782016-01-15 18:24:36 +00001000 if (job == null) {
1001 // No job on this context, but nextPending can't run here because
1002 // the context has a preferred Uid.
1003 continue;
1004 }
Shreyas Basarge5db09082016-01-07 13:38:29 +00001005 if (job.getUid() != nextPending.getUid()) {
1006 continue;
1007 }
1008 if (job.getPriority() >= nextPending.getPriority()) {
1009 continue;
1010 }
1011 if (minPriority > nextPending.getPriority()) {
1012 minPriority = nextPending.getPriority();
1013 minPriorityContextId = i;
1014 }
1015 }
1016 if (minPriorityContextId != -1) {
1017 contextIdToJobMap[minPriorityContextId] = nextPending;
1018 act[minPriorityContextId] = true;
1019 }
1020 }
1021 if (DEBUG) {
1022 Slog.d(TAG, printContextIdToJobMap(contextIdToJobMap, "running jobs final"));
1023 }
1024 for (int i=0; i<mActiveServices.size(); i++) {
1025 boolean preservePreferredUid = false;
1026 if (act[i]) {
1027 JobStatus js = mActiveServices.get(i).getRunningJob();
1028 if (js != null) {
1029 if (DEBUG) {
1030 Slog.d(TAG, "preempting job: " + mActiveServices.get(i).getRunningJob());
1031 }
1032 // preferredUid will be set to uid of currently running job.
1033 mActiveServices.get(i).preemptExecutingJob();
1034 preservePreferredUid = true;
1035 } else {
1036 if (DEBUG) {
1037 Slog.d(TAG, "About to run job on context "
1038 + String.valueOf(i) + ", job: " + contextIdToJobMap[i]);
1039 }
Dianne Hackborn1a30bd92016-01-11 11:05:00 -08001040 for (int ic=0; ic<mControllers.size(); ic++) {
1041 StateController controller = mControllers.get(ic);
Dianne Hackbornb0001f62016-02-16 10:30:33 -08001042 controller.prepareForExecutionLocked(contextIdToJobMap[i]);
Dianne Hackborn1a30bd92016-01-11 11:05:00 -08001043 }
Shreyas Basarge5db09082016-01-07 13:38:29 +00001044 if (!mActiveServices.get(i).executeRunnableJob(contextIdToJobMap[i])) {
1045 Slog.d(TAG, "Error executing " + contextIdToJobMap[i]);
1046 }
1047 mPendingJobs.remove(contextIdToJobMap[i]);
1048 }
1049 }
1050 if (!preservePreferredUid) {
1051 mActiveServices.get(i).clearPreferredUid();
1052 }
1053 }
1054 }
1055
1056 int findJobContextIdFromMap(JobStatus jobStatus, JobStatus[] map) {
1057 for (int i=0; i<map.length; i++) {
1058 if (map[i] != null && map[i].matches(jobStatus.getUid(), jobStatus.getJobId())) {
1059 return i;
1060 }
1061 }
1062 return -1;
1063 }
1064
1065 /**
Christopher Tate7060b042014-06-09 19:50:00 -07001066 * Binder stub trampoline implementation
1067 */
1068 final class JobSchedulerStub extends IJobScheduler.Stub {
1069 /** Cache determination of whether a given app can persist jobs
1070 * key is uid of the calling app; value is undetermined/true/false
1071 */
1072 private final SparseArray<Boolean> mPersistCache = new SparseArray<Boolean>();
1073
1074 // Enforce that only the app itself (or shared uid participant) can schedule a
1075 // job that runs one of the app's services, as well as verifying that the
1076 // named service properly requires the BIND_JOB_SERVICE permission
1077 private void enforceValidJobRequest(int uid, JobInfo job) {
Christopher Tate5568f542014-06-18 13:53:31 -07001078 final IPackageManager pm = AppGlobals.getPackageManager();
Christopher Tate7060b042014-06-09 19:50:00 -07001079 final ComponentName service = job.getService();
1080 try {
Jeff Sharkeyc7bacab2016-02-09 15:56:11 -07001081 ServiceInfo si = pm.getServiceInfo(service,
1082 PackageManager.MATCH_DEBUG_TRIAGED_MISSING, UserHandle.getUserId(uid));
Christopher Tate5568f542014-06-18 13:53:31 -07001083 if (si == null) {
1084 throw new IllegalArgumentException("No such service " + service);
1085 }
Christopher Tate7060b042014-06-09 19:50:00 -07001086 if (si.applicationInfo.uid != uid) {
1087 throw new IllegalArgumentException("uid " + uid +
1088 " cannot schedule job in " + service.getPackageName());
1089 }
1090 if (!JobService.PERMISSION_BIND.equals(si.permission)) {
1091 throw new IllegalArgumentException("Scheduled service " + service
1092 + " does not require android.permission.BIND_JOB_SERVICE permission");
1093 }
Christopher Tate5568f542014-06-18 13:53:31 -07001094 } catch (RemoteException e) {
1095 // Can't happen; the Package Manager is in this same process
Christopher Tate7060b042014-06-09 19:50:00 -07001096 }
1097 }
1098
1099 private boolean canPersistJobs(int pid, int uid) {
1100 // If we get this far we're good to go; all we need to do now is check
1101 // whether the app is allowed to persist its scheduled work.
1102 final boolean canPersist;
1103 synchronized (mPersistCache) {
1104 Boolean cached = mPersistCache.get(uid);
1105 if (cached != null) {
1106 canPersist = cached.booleanValue();
1107 } else {
1108 // Persisting jobs is tantamount to running at boot, so we permit
1109 // it when the app has declared that it uses the RECEIVE_BOOT_COMPLETED
1110 // permission
1111 int result = getContext().checkPermission(
1112 android.Manifest.permission.RECEIVE_BOOT_COMPLETED, pid, uid);
1113 canPersist = (result == PackageManager.PERMISSION_GRANTED);
1114 mPersistCache.put(uid, canPersist);
1115 }
1116 }
1117 return canPersist;
1118 }
1119
1120 // IJobScheduler implementation
1121 @Override
1122 public int schedule(JobInfo job) throws RemoteException {
1123 if (DEBUG) {
Matthew Williamsee410da2014-07-25 11:30:40 -07001124 Slog.d(TAG, "Scheduling job: " + job.toString());
Christopher Tate7060b042014-06-09 19:50:00 -07001125 }
1126 final int pid = Binder.getCallingPid();
1127 final int uid = Binder.getCallingUid();
1128
1129 enforceValidJobRequest(uid, job);
Matthew Williams900c67f2014-07-09 12:46:53 -07001130 if (job.isPersisted()) {
1131 if (!canPersistJobs(pid, uid)) {
1132 throw new IllegalArgumentException("Error: requested job be persisted without"
1133 + " holding RECEIVE_BOOT_COMPLETED permission.");
1134 }
1135 }
Christopher Tate7060b042014-06-09 19:50:00 -07001136
1137 long ident = Binder.clearCallingIdentity();
1138 try {
Matthew Williams900c67f2014-07-09 12:46:53 -07001139 return JobSchedulerService.this.schedule(job, uid);
Christopher Tate7060b042014-06-09 19:50:00 -07001140 } finally {
1141 Binder.restoreCallingIdentity(ident);
1142 }
1143 }
1144
1145 @Override
Shreyas Basarge968ac752016-01-11 23:09:26 +00001146 public int scheduleAsPackage(JobInfo job, String packageName, int userId)
1147 throws RemoteException {
Christopher Tate2f36fd62016-02-18 18:36:08 -08001148 final int callerUid = Binder.getCallingUid();
Shreyas Basarge968ac752016-01-11 23:09:26 +00001149 if (DEBUG) {
Christopher Tate2f36fd62016-02-18 18:36:08 -08001150 Slog.d(TAG, "Caller uid " + callerUid + " scheduling job: " + job.toString()
1151 + " on behalf of " + packageName);
Shreyas Basarge968ac752016-01-11 23:09:26 +00001152 }
Christopher Tate2f36fd62016-02-18 18:36:08 -08001153
1154 if (packageName == null) {
1155 throw new NullPointerException("Must specify a package for scheduleAsPackage()");
Shreyas Basarge968ac752016-01-11 23:09:26 +00001156 }
Christopher Tate2f36fd62016-02-18 18:36:08 -08001157
1158 int mayScheduleForOthers = getContext().checkCallingOrSelfPermission(
1159 android.Manifest.permission.UPDATE_DEVICE_STATS);
1160 if (mayScheduleForOthers != PackageManager.PERMISSION_GRANTED) {
1161 throw new SecurityException("Caller uid " + callerUid
1162 + " not permitted to schedule jobs for other apps");
1163 }
1164
Shreyas Basarge968ac752016-01-11 23:09:26 +00001165 long ident = Binder.clearCallingIdentity();
1166 try {
Christopher Tate2f36fd62016-02-18 18:36:08 -08001167 return JobSchedulerService.this.scheduleAsPackage(job, callerUid,
1168 packageName, userId);
Shreyas Basarge968ac752016-01-11 23:09:26 +00001169 } finally {
1170 Binder.restoreCallingIdentity(ident);
1171 }
1172 }
1173
1174 @Override
Christopher Tate7060b042014-06-09 19:50:00 -07001175 public List<JobInfo> getAllPendingJobs() throws RemoteException {
1176 final int uid = Binder.getCallingUid();
1177
1178 long ident = Binder.clearCallingIdentity();
1179 try {
1180 return JobSchedulerService.this.getPendingJobs(uid);
1181 } finally {
1182 Binder.restoreCallingIdentity(ident);
1183 }
1184 }
1185
1186 @Override
1187 public void cancelAll() throws RemoteException {
1188 final int uid = Binder.getCallingUid();
1189
1190 long ident = Binder.clearCallingIdentity();
1191 try {
Dianne Hackbornbef28fe2015-10-29 17:57:11 -07001192 JobSchedulerService.this.cancelJobsForUid(uid, true);
Christopher Tate7060b042014-06-09 19:50:00 -07001193 } finally {
1194 Binder.restoreCallingIdentity(ident);
1195 }
1196 }
1197
1198 @Override
1199 public void cancel(int jobId) throws RemoteException {
1200 final int uid = Binder.getCallingUid();
1201
1202 long ident = Binder.clearCallingIdentity();
1203 try {
1204 JobSchedulerService.this.cancelJob(uid, jobId);
1205 } finally {
1206 Binder.restoreCallingIdentity(ident);
1207 }
1208 }
1209
1210 /**
1211 * "dumpsys" infrastructure
1212 */
1213 @Override
1214 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1215 getContext().enforceCallingOrSelfPermission(android.Manifest.permission.DUMP, TAG);
1216
1217 long identityToken = Binder.clearCallingIdentity();
1218 try {
1219 JobSchedulerService.this.dumpInternal(pw);
1220 } finally {
1221 Binder.restoreCallingIdentity(identityToken);
1222 }
1223 }
Shreyas Basarge5db09082016-01-07 13:38:29 +00001224 };
1225
1226 private String printContextIdToJobMap(JobStatus[] map, String initial) {
1227 StringBuilder s = new StringBuilder(initial + ": ");
1228 for (int i=0; i<map.length; i++) {
1229 s.append("(")
1230 .append(map[i] == null? -1: map[i].getJobId())
1231 .append(map[i] == null? -1: map[i].getUid())
1232 .append(")" );
1233 }
1234 return s.toString();
1235 }
1236
1237 private String printPendingQueue() {
1238 StringBuilder s = new StringBuilder("Pending queue: ");
1239 Iterator<JobStatus> it = mPendingJobs.iterator();
1240 while (it.hasNext()) {
1241 JobStatus js = it.next();
1242 s.append("(")
1243 .append(js.getJob().getId())
1244 .append(", ")
1245 .append(js.getUid())
1246 .append(") ");
1247 }
1248 return s.toString();
Jeff Sharkey5217cac2015-12-20 15:34:01 -07001249 }
Christopher Tate7060b042014-06-09 19:50:00 -07001250
Christopher Tate2f36fd62016-02-18 18:36:08 -08001251 void dumpInternal(final PrintWriter pw) {
Christopher Tatef973a7b2014-08-29 12:54:08 -07001252 final long now = SystemClock.elapsedRealtime();
Dianne Hackborn33d31c52016-02-16 10:30:33 -08001253 synchronized (mLock) {
Shreyas Basarge5db09082016-01-07 13:38:29 +00001254 pw.print("Started users: ");
1255 for (int i=0; i<mStartedUsers.size(); i++) {
1256 pw.print("u" + mStartedUsers.get(i) + " ");
1257 }
1258 pw.println();
Christopher Tate7060b042014-06-09 19:50:00 -07001259 pw.println("Registered jobs:");
1260 if (mJobs.size() > 0) {
Christopher Tate2f36fd62016-02-18 18:36:08 -08001261 mJobs.forEachJob(new JobStatusFunctor() {
1262 private int index = 0;
1263
1264 @Override
1265 public void process(JobStatus job) {
1266 pw.print(" Job #"); pw.print(index++); pw.print(": ");
1267 pw.println(job.toShortString());
1268 job.dump(pw, " ");
1269 pw.print(" Ready: ");
1270 pw.print(mHandler.isReadyToBeExecutedLocked(job));
1271 pw.print(" (job=");
1272 pw.print(job.isReady());
1273 pw.print(" pending=");
1274 pw.print(mPendingJobs.contains(job));
1275 pw.print(" active=");
1276 pw.print(isCurrentlyActiveLocked(job));
1277 pw.print(" user=");
1278 pw.print(mStartedUsers.contains(job.getUserId()));
1279 pw.println(")");
1280 }
1281 });
Christopher Tate7060b042014-06-09 19:50:00 -07001282 } else {
Christopher Tatef973a7b2014-08-29 12:54:08 -07001283 pw.println(" None.");
Christopher Tate7060b042014-06-09 19:50:00 -07001284 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001285 for (int i=0; i<mControllers.size(); i++) {
Christopher Tate7060b042014-06-09 19:50:00 -07001286 pw.println();
Dianne Hackbornb0001f62016-02-16 10:30:33 -08001287 mControllers.get(i).dumpControllerStateLocked(pw);
Christopher Tate7060b042014-06-09 19:50:00 -07001288 }
1289 pw.println();
Shreyas Basarge5db09082016-01-07 13:38:29 +00001290 pw.println(printPendingQueue());
Christopher Tate7060b042014-06-09 19:50:00 -07001291 pw.println();
1292 pw.println("Active jobs:");
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001293 for (int i=0; i<mActiveServices.size(); i++) {
1294 JobServiceContext jsc = mActiveServices.get(i);
Shreyas Basarge5db09082016-01-07 13:38:29 +00001295 if (jsc.getRunningJob() == null) {
Christopher Tate7060b042014-06-09 19:50:00 -07001296 continue;
1297 } else {
Christopher Tatef973a7b2014-08-29 12:54:08 -07001298 final long timeout = jsc.getTimeoutElapsed();
1299 pw.print("Running for: ");
1300 pw.print((now - jsc.getExecutionStartTimeElapsed())/1000);
1301 pw.print("s timeout=");
1302 pw.print(timeout);
1303 pw.print(" fromnow=");
1304 pw.println(timeout-now);
1305 jsc.getRunningJob().dump(pw, " ");
Christopher Tate7060b042014-06-09 19:50:00 -07001306 }
1307 }
Dianne Hackbornfdb19562014-07-11 16:03:36 -07001308 pw.println();
1309 pw.print("mReadyToRock="); pw.println(mReadyToRock);
Dianne Hackborn88e98df2015-03-23 13:29:14 -07001310 pw.print("mDeviceIdleMode="); pw.println(mDeviceIdleMode);
Dianne Hackborn627dfa12015-11-11 18:10:30 -08001311 pw.print("mReportedActive="); pw.println(mReportedActive);
Christopher Tate7060b042014-06-09 19:50:00 -07001312 }
1313 pw.println();
1314 }
1315}