Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
| 19 | import android.app.usage.UsageStatsManagerInternal; |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 20 | import android.content.Context; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 21 | import android.os.UserHandle; |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 22 | import android.util.Slog; |
| 23 | |
| 24 | import com.android.server.LocalServices; |
| 25 | import com.android.server.job.JobSchedulerService; |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 26 | import com.android.server.job.JobStore; |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 27 | |
| 28 | import java.io.PrintWriter; |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * Controls when apps are considered idle and if jobs pertaining to those apps should |
| 32 | * be executed. Apps that haven't been actively launched or accessed from a foreground app |
| 33 | * for a certain amount of time (maybe hours or days) are considered idle. When the app comes |
| 34 | * out of idle state, it will be allowed to run scheduled jobs. |
| 35 | */ |
Dianne Hackborn | 6466c1c | 2017-06-13 10:33:19 -0700 | [diff] [blame] | 36 | public final class AppIdleController extends StateController { |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 37 | |
| 38 | private static final String LOG_TAG = "AppIdleController"; |
Amith Yamasani | 55717a6 | 2015-04-03 17:22:36 -0700 | [diff] [blame] | 39 | private static final boolean DEBUG = false; |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 40 | |
| 41 | // Singleton factory |
| 42 | private static Object sCreationLock = new Object(); |
| 43 | private static volatile AppIdleController sController; |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 44 | private final JobSchedulerService mJobSchedulerService; |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 45 | private final UsageStatsManagerInternal mUsageStatsInternal; |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 46 | private boolean mInitializedParoleOn; |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 47 | boolean mAppIdleParoleOn; |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 48 | |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 49 | final class GlobalUpdateFunc implements JobStore.JobStatusFunctor { |
| 50 | boolean mChanged; |
| 51 | |
| 52 | @Override public void process(JobStatus jobStatus) { |
| 53 | String packageName = jobStatus.getSourcePackageName(); |
| 54 | final boolean appIdle = !mAppIdleParoleOn && mUsageStatsInternal.isAppIdle(packageName, |
| 55 | jobStatus.getSourceUid(), jobStatus.getSourceUserId()); |
| 56 | if (DEBUG) { |
| 57 | Slog.d(LOG_TAG, "Setting idle state of " + packageName + " to " + appIdle); |
| 58 | } |
| 59 | if (jobStatus.setAppNotIdleConstraintSatisfied(!appIdle)) { |
| 60 | mChanged = true; |
| 61 | } |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | final static class PackageUpdateFunc implements JobStore.JobStatusFunctor { |
| 66 | final int mUserId; |
| 67 | final String mPackage; |
| 68 | final boolean mIdle; |
| 69 | boolean mChanged; |
| 70 | |
| 71 | PackageUpdateFunc(int userId, String pkg, boolean idle) { |
| 72 | mUserId = userId; |
| 73 | mPackage = pkg; |
| 74 | mIdle = idle; |
| 75 | } |
| 76 | |
| 77 | @Override public void process(JobStatus jobStatus) { |
| 78 | if (jobStatus.getSourcePackageName().equals(mPackage) |
| 79 | && jobStatus.getSourceUserId() == mUserId) { |
| 80 | if (jobStatus.setAppNotIdleConstraintSatisfied(!mIdle)) { |
| 81 | if (DEBUG) { |
| 82 | Slog.d(LOG_TAG, "App Idle state changed, setting idle state of " |
| 83 | + mPackage + " to " + mIdle); |
| 84 | } |
| 85 | mChanged = true; |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | }; |
| 90 | |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 91 | public static AppIdleController get(JobSchedulerService service) { |
| 92 | synchronized (sCreationLock) { |
| 93 | if (sController == null) { |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 94 | sController = new AppIdleController(service, service.getContext(), |
| 95 | service.getLock()); |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 96 | } |
| 97 | return sController; |
| 98 | } |
| 99 | } |
| 100 | |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 101 | private AppIdleController(JobSchedulerService service, Context context, Object lock) { |
| 102 | super(service, context, lock); |
| 103 | mJobSchedulerService = service; |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 104 | mUsageStatsInternal = LocalServices.getService(UsageStatsManagerInternal.class); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 105 | mAppIdleParoleOn = true; |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 106 | mUsageStatsInternal.addAppIdleStateChangeListener(new AppIdleStateChangeListener()); |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 109 | @Override |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 110 | public void maybeStartTrackingJobLocked(JobStatus jobStatus, JobStatus lastJob) { |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 111 | if (!mInitializedParoleOn) { |
| 112 | mInitializedParoleOn = true; |
| 113 | mAppIdleParoleOn = mUsageStatsInternal.isAppIdleParoleOn(); |
| 114 | } |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 115 | String packageName = jobStatus.getSourcePackageName(); |
| 116 | final boolean appIdle = !mAppIdleParoleOn && mUsageStatsInternal.isAppIdle(packageName, |
| 117 | jobStatus.getSourceUid(), jobStatus.getSourceUserId()); |
| 118 | if (DEBUG) { |
| 119 | Slog.d(LOG_TAG, "Start tracking, setting idle state of " |
| 120 | + packageName + " to " + appIdle); |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 121 | } |
Dianne Hackborn | b0001f6 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 122 | jobStatus.setAppNotIdleConstraintSatisfied(!appIdle); |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | @Override |
Dianne Hackborn | f9bac16 | 2017-04-20 17:17:48 -0700 | [diff] [blame] | 126 | public void maybeStopTrackingJobLocked(JobStatus jobStatus, JobStatus incomingJob, |
| 127 | boolean forUpdate) { |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | @Override |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 131 | public void dumpControllerStateLocked(final PrintWriter pw, final int filterUid) { |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 132 | pw.print("AppIdle: parole on = "); |
| 133 | pw.println(mAppIdleParoleOn); |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 134 | mJobSchedulerService.getJobStore().forEachJob(new JobStore.JobStatusFunctor() { |
| 135 | @Override public void process(JobStatus jobStatus) { |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 136 | // Skip printing details if the caller requested a filter |
| 137 | if (!jobStatus.shouldDump(filterUid)) { |
| 138 | return; |
| 139 | } |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame] | 140 | pw.print(" #"); |
| 141 | jobStatus.printUniqueId(pw); |
| 142 | pw.print(" from "); |
| 143 | UserHandle.formatUid(pw, jobStatus.getSourceUid()); |
| 144 | pw.print(": "); |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 145 | pw.print(jobStatus.getSourcePackageName()); |
Dianne Hackborn | 7ab4025 | 2016-06-15 17:30:24 -0700 | [diff] [blame] | 146 | if ((jobStatus.satisfiedConstraints&JobStatus.CONSTRAINT_APP_NOT_IDLE) != 0) { |
| 147 | pw.println(" RUNNABLE"); |
| 148 | } else { |
| 149 | pw.println(" WAITING"); |
| 150 | } |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 151 | } |
| 152 | }); |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 155 | void setAppIdleParoleOn(boolean isAppIdleParoleOn) { |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 156 | // Flag if any app's idle state has changed |
| 157 | boolean changed = false; |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 158 | synchronized (mLock) { |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 159 | if (mAppIdleParoleOn == isAppIdleParoleOn) { |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 160 | return; |
| 161 | } |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 162 | mAppIdleParoleOn = isAppIdleParoleOn; |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 163 | GlobalUpdateFunc update = new GlobalUpdateFunc(); |
| 164 | mJobSchedulerService.getJobStore().forEachJob(update); |
| 165 | if (update.mChanged) { |
| 166 | changed = true; |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 167 | } |
| 168 | } |
| 169 | if (changed) { |
| 170 | mStateChangedListener.onControllerStateChanged(); |
| 171 | } |
| 172 | } |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 173 | |
Dianne Hackborn | 6466c1c | 2017-06-13 10:33:19 -0700 | [diff] [blame] | 174 | private final class AppIdleStateChangeListener |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 175 | extends UsageStatsManagerInternal.AppIdleStateChangeListener { |
| 176 | @Override |
Amith Yamasani | 84cd7b7 | 2017-11-07 13:59:37 -0800 | [diff] [blame] | 177 | public void onAppIdleStateChanged(String packageName, int userId, boolean idle, int bucket) { |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 178 | boolean changed = false; |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 179 | synchronized (mLock) { |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 180 | if (mAppIdleParoleOn) { |
| 181 | return; |
| 182 | } |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 183 | |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 184 | PackageUpdateFunc update = new PackageUpdateFunc(userId, packageName, idle); |
| 185 | mJobSchedulerService.getJobStore().forEachJob(update); |
| 186 | if (update.mChanged) { |
| 187 | changed = true; |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | if (changed) { |
| 191 | mStateChangedListener.onControllerStateChanged(); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public void onParoleStateChanged(boolean isParoleOn) { |
| 197 | if (DEBUG) { |
| 198 | Slog.d(LOG_TAG, "Parole on: " + isParoleOn); |
| 199 | } |
| 200 | setAppIdleParoleOn(isParoleOn); |
| 201 | } |
| 202 | } |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 203 | } |