Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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.controllers; |
| 18 | |
Suprabh Shukla | 106203b | 2017-11-02 21:23:44 -0700 | [diff] [blame] | 19 | import android.app.job.JobInfo; |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 20 | import android.content.BroadcastReceiver; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.IntentFilter; |
Suprabh Shukla | 106203b | 2017-11-02 21:23:44 -0700 | [diff] [blame] | 24 | import android.os.Handler; |
| 25 | import android.os.Looper; |
| 26 | import android.os.Message; |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 27 | import android.os.PowerManager; |
| 28 | import android.os.UserHandle; |
Suprabh Shukla | a78acfd | 2017-10-13 19:29:36 -0700 | [diff] [blame] | 29 | import android.util.ArraySet; |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 30 | import android.util.Slog; |
Suprabh Shukla | 106203b | 2017-11-02 21:23:44 -0700 | [diff] [blame] | 31 | import android.util.SparseBooleanArray; |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 32 | |
| 33 | import com.android.internal.util.ArrayUtils; |
| 34 | import com.android.server.DeviceIdleController; |
| 35 | import com.android.server.LocalServices; |
| 36 | import com.android.server.job.JobSchedulerService; |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 37 | import com.android.server.job.JobStore; |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 38 | |
| 39 | import java.io.PrintWriter; |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 40 | import java.util.Arrays; |
| 41 | |
| 42 | /** |
| 43 | * When device is dozing, set constraint for all jobs, except whitelisted apps, as not satisfied. |
| 44 | * When device is not dozing, set constraint for all jobs as satisfied. |
| 45 | */ |
Dianne Hackborn | 6466c1c | 2017-06-13 10:33:19 -0700 | [diff] [blame] | 46 | public final class DeviceIdleJobsController extends StateController { |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 47 | |
| 48 | private static final String LOG_TAG = "DeviceIdleJobsController"; |
| 49 | private static final boolean LOG_DEBUG = false; |
Suprabh Shukla | 106203b | 2017-11-02 21:23:44 -0700 | [diff] [blame] | 50 | private static final long BACKGROUND_JOBS_DELAY = 3000; |
| 51 | |
| 52 | static final int PROCESS_BACKGROUND_JOBS = 1; |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 53 | |
| 54 | // Singleton factory |
| 55 | private static Object sCreationLock = new Object(); |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 56 | private static DeviceIdleJobsController sController; |
| 57 | |
Suprabh Shukla | 106203b | 2017-11-02 21:23:44 -0700 | [diff] [blame] | 58 | /** |
| 59 | * These are jobs added with a special flag to indicate that they should be exempted from doze |
| 60 | * when the app is temp whitelisted or in the foreground. |
| 61 | */ |
| 62 | private final ArraySet<JobStatus> mAllowInIdleJobs; |
| 63 | private final SparseBooleanArray mForegroundUids; |
| 64 | private final DeviceIdleUpdateFunctor mDeviceIdleUpdateFunctor; |
| 65 | private final DeviceIdleJobsDelayHandler mHandler; |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 66 | private final JobSchedulerService mJobSchedulerService; |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 67 | private final PowerManager mPowerManager; |
| 68 | private final DeviceIdleController.LocalService mLocalDeviceIdleController; |
| 69 | |
| 70 | /** |
| 71 | * True when in device idle mode, so we don't want to schedule any jobs. |
| 72 | */ |
| 73 | private boolean mDeviceIdleMode; |
| 74 | private int[] mDeviceIdleWhitelistAppIds; |
Suprabh Shukla | a78acfd | 2017-10-13 19:29:36 -0700 | [diff] [blame] | 75 | private int[] mPowerSaveTempWhitelistAppIds; |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 76 | |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 77 | /** |
| 78 | * Returns a singleton for the DeviceIdleJobsController |
| 79 | */ |
| 80 | public static DeviceIdleJobsController get(JobSchedulerService service) { |
| 81 | synchronized (sCreationLock) { |
| 82 | if (sController == null) { |
| 83 | sController = new DeviceIdleJobsController(service, service.getContext(), |
| 84 | service.getLock()); |
| 85 | } |
| 86 | return sController; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // onReceive |
| 91 | private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
| 92 | @Override |
| 93 | public void onReceive(Context context, Intent intent) { |
Suprabh Shukla | a78acfd | 2017-10-13 19:29:36 -0700 | [diff] [blame] | 94 | switch (intent.getAction()) { |
| 95 | case PowerManager.ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED: |
| 96 | case PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED: |
| 97 | updateIdleMode(mPowerManager != null && (mPowerManager.isDeviceIdleMode() |
| 98 | || mPowerManager.isLightDeviceIdleMode())); |
| 99 | break; |
| 100 | case PowerManager.ACTION_POWER_SAVE_WHITELIST_CHANGED: |
| 101 | synchronized (mLock) { |
| 102 | mDeviceIdleWhitelistAppIds = |
| 103 | mLocalDeviceIdleController.getPowerSaveWhitelistUserAppIds(); |
| 104 | if (LOG_DEBUG) { |
| 105 | Slog.d(LOG_TAG, "Got whitelist " |
| 106 | + Arrays.toString(mDeviceIdleWhitelistAppIds)); |
| 107 | } |
| 108 | } |
| 109 | break; |
| 110 | case PowerManager.ACTION_POWER_SAVE_TEMP_WHITELIST_CHANGED: |
| 111 | synchronized (mLock) { |
| 112 | mPowerSaveTempWhitelistAppIds = |
| 113 | mLocalDeviceIdleController.getPowerSaveTempWhitelistAppIds(); |
| 114 | if (LOG_DEBUG) { |
| 115 | Slog.d(LOG_TAG, "Got temp whitelist " |
| 116 | + Arrays.toString(mPowerSaveTempWhitelistAppIds)); |
| 117 | } |
| 118 | boolean changed = false; |
Suprabh Shukla | 106203b | 2017-11-02 21:23:44 -0700 | [diff] [blame] | 119 | for (int i = 0; i < mAllowInIdleJobs.size(); i++) { |
| 120 | changed |= updateTaskStateLocked(mAllowInIdleJobs.valueAt(i)); |
Suprabh Shukla | a78acfd | 2017-10-13 19:29:36 -0700 | [diff] [blame] | 121 | } |
| 122 | if (changed) { |
| 123 | mStateChangedListener.onControllerStateChanged(); |
| 124 | } |
| 125 | } |
| 126 | break; |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | }; |
| 130 | |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 131 | private DeviceIdleJobsController(JobSchedulerService jobSchedulerService, Context context, |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 132 | Object lock) { |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 133 | super(jobSchedulerService, context, lock); |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 134 | |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 135 | mJobSchedulerService = jobSchedulerService; |
Suprabh Shukla | 106203b | 2017-11-02 21:23:44 -0700 | [diff] [blame] | 136 | mHandler = new DeviceIdleJobsDelayHandler(context.getMainLooper()); |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 137 | // Register for device idle mode changes |
| 138 | mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); |
| 139 | mLocalDeviceIdleController = |
| 140 | LocalServices.getService(DeviceIdleController.LocalService.class); |
Suprabh Shukla | a78acfd | 2017-10-13 19:29:36 -0700 | [diff] [blame] | 141 | mDeviceIdleWhitelistAppIds = mLocalDeviceIdleController.getPowerSaveWhitelistUserAppIds(); |
| 142 | mPowerSaveTempWhitelistAppIds = |
| 143 | mLocalDeviceIdleController.getPowerSaveTempWhitelistAppIds(); |
Suprabh Shukla | 106203b | 2017-11-02 21:23:44 -0700 | [diff] [blame] | 144 | mDeviceIdleUpdateFunctor = new DeviceIdleUpdateFunctor(); |
| 145 | mAllowInIdleJobs = new ArraySet<>(); |
| 146 | mForegroundUids = new SparseBooleanArray(); |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 147 | final IntentFilter filter = new IntentFilter(); |
| 148 | filter.addAction(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED); |
| 149 | filter.addAction(PowerManager.ACTION_LIGHT_DEVICE_IDLE_MODE_CHANGED); |
| 150 | filter.addAction(PowerManager.ACTION_POWER_SAVE_WHITELIST_CHANGED); |
Suprabh Shukla | a78acfd | 2017-10-13 19:29:36 -0700 | [diff] [blame] | 151 | filter.addAction(PowerManager.ACTION_POWER_SAVE_TEMP_WHITELIST_CHANGED); |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 152 | mContext.registerReceiverAsUser( |
| 153 | mBroadcastReceiver, UserHandle.ALL, filter, null, null); |
| 154 | } |
| 155 | |
| 156 | void updateIdleMode(boolean enabled) { |
| 157 | boolean changed = false; |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 158 | synchronized (mLock) { |
| 159 | if (mDeviceIdleMode != enabled) { |
| 160 | changed = true; |
| 161 | } |
| 162 | mDeviceIdleMode = enabled; |
| 163 | if (LOG_DEBUG) Slog.d(LOG_TAG, "mDeviceIdleMode=" + mDeviceIdleMode); |
Suprabh Shukla | 106203b | 2017-11-02 21:23:44 -0700 | [diff] [blame] | 164 | if (enabled) { |
| 165 | mHandler.removeMessages(PROCESS_BACKGROUND_JOBS); |
| 166 | mJobSchedulerService.getJobStore().forEachJob(mDeviceIdleUpdateFunctor); |
| 167 | } else { |
| 168 | // When coming out of doze, process all foreground uids immediately, while others |
| 169 | // will be processed after a delay of 3 seconds. |
| 170 | for (int i = 0; i < mForegroundUids.size(); i++) { |
| 171 | if (mForegroundUids.valueAt(i)) { |
| 172 | mJobSchedulerService.getJobStore().forEachJobForSourceUid( |
| 173 | mForegroundUids.keyAt(i), mDeviceIdleUpdateFunctor); |
| 174 | } |
| 175 | } |
| 176 | mHandler.sendEmptyMessageDelayed(PROCESS_BACKGROUND_JOBS, BACKGROUND_JOBS_DELAY); |
| 177 | } |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 178 | } |
| 179 | // Inform the job scheduler service about idle mode changes |
| 180 | if (changed) { |
| 181 | mStateChangedListener.onDeviceIdleStateChanged(enabled); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | /** |
Suprabh Shukla | 106203b | 2017-11-02 21:23:44 -0700 | [diff] [blame] | 186 | * Called by jobscheduler service to report uid state changes between active and idle |
| 187 | */ |
| 188 | public void setUidActiveLocked(int uid, boolean active) { |
| 189 | final boolean changed = (active != mForegroundUids.get(uid)); |
| 190 | if (!changed) { |
| 191 | return; |
| 192 | } |
| 193 | if (LOG_DEBUG) { |
| 194 | Slog.d(LOG_TAG, "uid " + uid + " going " + (active ? "active" : "inactive")); |
| 195 | } |
| 196 | mForegroundUids.put(uid, active); |
| 197 | mDeviceIdleUpdateFunctor.mChanged = false; |
| 198 | mJobSchedulerService.getJobStore().forEachJobForSourceUid(uid, mDeviceIdleUpdateFunctor); |
| 199 | if (mDeviceIdleUpdateFunctor.mChanged) { |
| 200 | mStateChangedListener.onControllerStateChanged(); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /** |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 205 | * Checks if the given job's scheduling app id exists in the device idle user whitelist. |
| 206 | */ |
| 207 | boolean isWhitelistedLocked(JobStatus job) { |
Suprabh Shukla | 106203b | 2017-11-02 21:23:44 -0700 | [diff] [blame] | 208 | return Arrays.binarySearch(mDeviceIdleWhitelistAppIds, |
| 209 | UserHandle.getAppId(job.getSourceUid())) >= 0; |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 210 | } |
| 211 | |
Suprabh Shukla | a78acfd | 2017-10-13 19:29:36 -0700 | [diff] [blame] | 212 | /** |
| 213 | * Checks if the given job's scheduling app id exists in the device idle temp whitelist. |
| 214 | */ |
| 215 | boolean isTempWhitelistedLocked(JobStatus job) { |
| 216 | return ArrayUtils.contains(mPowerSaveTempWhitelistAppIds, |
| 217 | UserHandle.getAppId(job.getSourceUid())); |
| 218 | } |
| 219 | |
| 220 | private boolean updateTaskStateLocked(JobStatus task) { |
Suprabh Shukla | 106203b | 2017-11-02 21:23:44 -0700 | [diff] [blame] | 221 | final boolean allowInIdle = ((task.getFlags()&JobInfo.FLAG_IMPORTANT_WHILE_FOREGROUND) != 0) |
| 222 | && (mForegroundUids.get(task.getSourceUid()) || isTempWhitelistedLocked(task)); |
| 223 | final boolean whitelisted = isWhitelistedLocked(task); |
| 224 | final boolean enableTask = !mDeviceIdleMode || whitelisted || allowInIdle; |
Suprabh Shukla | a78acfd | 2017-10-13 19:29:36 -0700 | [diff] [blame] | 225 | return task.setDeviceNotDozingConstraintSatisfied(enableTask, whitelisted); |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | @Override |
| 229 | public void maybeStartTrackingJobLocked(JobStatus jobStatus, JobStatus lastJob) { |
Suprabh Shukla | 106203b | 2017-11-02 21:23:44 -0700 | [diff] [blame] | 230 | if ((jobStatus.getFlags()&JobInfo.FLAG_IMPORTANT_WHILE_FOREGROUND) != 0) { |
| 231 | mAllowInIdleJobs.add(jobStatus); |
Suprabh Shukla | a78acfd | 2017-10-13 19:29:36 -0700 | [diff] [blame] | 232 | } |
Suprabh Shukla | 106203b | 2017-11-02 21:23:44 -0700 | [diff] [blame] | 233 | updateTaskStateLocked(jobStatus); |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | @Override |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 237 | public void maybeStopTrackingJobLocked(JobStatus jobStatus, JobStatus incomingJob, |
| 238 | boolean forUpdate) { |
Suprabh Shukla | 106203b | 2017-11-02 21:23:44 -0700 | [diff] [blame] | 239 | if ((jobStatus.getFlags()&JobInfo.FLAG_IMPORTANT_WHILE_FOREGROUND) != 0) { |
| 240 | mAllowInIdleJobs.remove(jobStatus); |
| 241 | } |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | @Override |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 245 | public void dumpControllerStateLocked(final PrintWriter pw, final int filterUid) { |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 246 | pw.println("DeviceIdleJobsController"); |
Suprabh Shukla | 106203b | 2017-11-02 21:23:44 -0700 | [diff] [blame] | 247 | pw.println("mDeviceIdleMode=" + mDeviceIdleMode); |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 248 | mJobSchedulerService.getJobStore().forEachJob(new JobStore.JobStatusFunctor() { |
| 249 | @Override public void process(JobStatus jobStatus) { |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 250 | if (!jobStatus.shouldDump(filterUid)) { |
| 251 | return; |
| 252 | } |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 253 | pw.print(" #"); |
| 254 | jobStatus.printUniqueId(pw); |
| 255 | pw.print(" from "); |
| 256 | UserHandle.formatUid(pw, jobStatus.getSourceUid()); |
| 257 | pw.print(": "); |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 258 | pw.print(jobStatus.getSourcePackageName()); |
Dianne Hackborn | 7ab4025 | 2016-06-15 17:30:24 -0700 | [diff] [blame] | 259 | pw.print((jobStatus.satisfiedConstraints |
| 260 | & JobStatus.CONSTRAINT_DEVICE_NOT_DOZING) != 0 |
| 261 | ? " RUNNABLE" : " WAITING"); |
| 262 | if (jobStatus.dozeWhitelisted) { |
| 263 | pw.print(" WHITELISTED"); |
| 264 | } |
Suprabh Shukla | 106203b | 2017-11-02 21:23:44 -0700 | [diff] [blame] | 265 | if (mAllowInIdleJobs.contains(jobStatus)) { |
| 266 | pw.print(" ALLOWED_IN_DOZE"); |
| 267 | } |
Dianne Hackborn | 7ab4025 | 2016-06-15 17:30:24 -0700 | [diff] [blame] | 268 | pw.println(); |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 269 | } |
| 270 | }); |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 271 | } |
Suprabh Shukla | 106203b | 2017-11-02 21:23:44 -0700 | [diff] [blame] | 272 | |
| 273 | final class DeviceIdleUpdateFunctor implements JobStore.JobStatusFunctor { |
| 274 | boolean mChanged; |
| 275 | |
| 276 | @Override |
| 277 | public void process(JobStatus jobStatus) { |
| 278 | mChanged |= updateTaskStateLocked(jobStatus); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | final class DeviceIdleJobsDelayHandler extends Handler { |
| 283 | public DeviceIdleJobsDelayHandler(Looper looper) { |
| 284 | super(looper); |
| 285 | } |
| 286 | |
| 287 | @Override |
| 288 | public void handleMessage(Message msg) { |
| 289 | switch (msg.what) { |
| 290 | case PROCESS_BACKGROUND_JOBS: |
| 291 | // Just process all the jobs, the ones in foreground should already be running. |
| 292 | synchronized (mLock) { |
| 293 | mDeviceIdleUpdateFunctor.mChanged = false; |
| 294 | mJobSchedulerService.getJobStore().forEachJob(mDeviceIdleUpdateFunctor); |
| 295 | if (mDeviceIdleUpdateFunctor.mChanged) { |
| 296 | mStateChangedListener.onControllerStateChanged(); |
| 297 | } |
| 298 | } |
| 299 | break; |
| 300 | } |
| 301 | } |
| 302 | } |
Amith Yamasani | cb926fc | 2016-03-14 17:15:20 -0700 | [diff] [blame] | 303 | } |