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