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 | */ |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 36 | public 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 | 141f11c | 2016-04-05 15:46:12 -0700 | [diff] [blame] | 126 | public void maybeStopTrackingJobLocked(JobStatus jobStatus, JobStatus incomingJob, boolean forUpdate) { |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | @Override |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 130 | public void dumpControllerStateLocked(final PrintWriter pw, final int filterUid) { |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame^] | 131 | pw.print("AppIdle: parole on = "); |
| 132 | pw.println(mAppIdleParoleOn); |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 133 | mJobSchedulerService.getJobStore().forEachJob(new JobStore.JobStatusFunctor() { |
| 134 | @Override public void process(JobStatus jobStatus) { |
Dianne Hackborn | ef3aa6e | 2016-04-29 18:18:08 -0700 | [diff] [blame] | 135 | // Skip printing details if the caller requested a filter |
| 136 | if (!jobStatus.shouldDump(filterUid)) { |
| 137 | return; |
| 138 | } |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame^] | 139 | pw.print(" #"); |
| 140 | jobStatus.printUniqueId(pw); |
| 141 | pw.print(" from "); |
| 142 | UserHandle.formatUid(pw, jobStatus.getSourceUid()); |
| 143 | pw.print(": "); |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 144 | pw.print(jobStatus.getSourcePackageName()); |
Dianne Hackborn | e9a988c | 2016-05-27 17:59:40 -0700 | [diff] [blame^] | 145 | pw.print(", runnable="); |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 146 | pw.println((jobStatus.satisfiedConstraints&JobStatus.CONSTRAINT_APP_NOT_IDLE) != 0); |
| 147 | } |
| 148 | }); |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 151 | void setAppIdleParoleOn(boolean isAppIdleParoleOn) { |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 152 | // Flag if any app's idle state has changed |
| 153 | boolean changed = false; |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 154 | synchronized (mLock) { |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 155 | if (mAppIdleParoleOn == isAppIdleParoleOn) { |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 156 | return; |
| 157 | } |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 158 | mAppIdleParoleOn = isAppIdleParoleOn; |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 159 | GlobalUpdateFunc update = new GlobalUpdateFunc(); |
| 160 | mJobSchedulerService.getJobStore().forEachJob(update); |
| 161 | if (update.mChanged) { |
| 162 | changed = true; |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | if (changed) { |
| 166 | mStateChangedListener.onControllerStateChanged(); |
| 167 | } |
| 168 | } |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 169 | |
| 170 | private class AppIdleStateChangeListener |
| 171 | extends UsageStatsManagerInternal.AppIdleStateChangeListener { |
| 172 | @Override |
| 173 | public void onAppIdleStateChanged(String packageName, int userId, boolean idle) { |
| 174 | boolean changed = false; |
Dianne Hackborn | 33d31c5 | 2016-02-16 10:30:33 -0800 | [diff] [blame] | 175 | synchronized (mLock) { |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 176 | if (mAppIdleParoleOn) { |
| 177 | return; |
| 178 | } |
Dianne Hackborn | 8db0fc1 | 2016-04-12 13:48:25 -0700 | [diff] [blame] | 179 | PackageUpdateFunc update = new PackageUpdateFunc(userId, packageName, idle); |
| 180 | mJobSchedulerService.getJobStore().forEachJob(update); |
| 181 | if (update.mChanged) { |
| 182 | changed = true; |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | if (changed) { |
| 186 | mStateChangedListener.onControllerStateChanged(); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | @Override |
| 191 | public void onParoleStateChanged(boolean isParoleOn) { |
| 192 | if (DEBUG) { |
| 193 | Slog.d(LOG_TAG, "Parole on: " + isParoleOn); |
| 194 | } |
| 195 | setAppIdleParoleOn(isParoleOn); |
| 196 | } |
| 197 | } |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 198 | } |