blob: a7ed2f56d6beb4fbdcca4255a8529dcb4f372dcd [file] [log] [blame]
Amith Yamasanib0ff3222015-03-04 09:56:14 -08001/*
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
17package com.android.server.job.controllers;
18
19import android.app.usage.UsageStatsManagerInternal;
Amith Yamasanib0ff3222015-03-04 09:56:14 -080020import android.content.Context;
Dianne Hackborne9a988c2016-05-27 17:59:40 -070021import android.os.UserHandle;
Amith Yamasanib0ff3222015-03-04 09:56:14 -080022import android.util.Slog;
23
24import com.android.server.LocalServices;
25import com.android.server.job.JobSchedulerService;
Dianne Hackborn8db0fc12016-04-12 13:48:25 -070026import com.android.server.job.JobStore;
Amith Yamasanib0ff3222015-03-04 09:56:14 -080027
28import java.io.PrintWriter;
Amith Yamasanib0ff3222015-03-04 09:56:14 -080029
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 Hackborn6466c1c2017-06-13 10:33:19 -070036public final class AppIdleController extends StateController {
Amith Yamasanib0ff3222015-03-04 09:56:14 -080037
38 private static final String LOG_TAG = "AppIdleController";
Amith Yamasani55717a62015-04-03 17:22:36 -070039 private static final boolean DEBUG = false;
Amith Yamasanib0ff3222015-03-04 09:56:14 -080040
41 // Singleton factory
42 private static Object sCreationLock = new Object();
43 private static volatile AppIdleController sController;
Dianne Hackborn8db0fc12016-04-12 13:48:25 -070044 private final JobSchedulerService mJobSchedulerService;
Amith Yamasanib0ff3222015-03-04 09:56:14 -080045 private final UsageStatsManagerInternal mUsageStatsInternal;
Dianne Hackborne9a988c2016-05-27 17:59:40 -070046 private boolean mInitializedParoleOn;
Xiaohui Chen8dca36d2015-06-19 12:44:59 -070047 boolean mAppIdleParoleOn;
Amith Yamasanib0ff3222015-03-04 09:56:14 -080048
Dianne Hackborn8db0fc12016-04-12 13:48:25 -070049 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 Yamasanib0ff3222015-03-04 09:56:14 -080091 public static AppIdleController get(JobSchedulerService service) {
92 synchronized (sCreationLock) {
93 if (sController == null) {
Dianne Hackborn33d31c52016-02-16 10:30:33 -080094 sController = new AppIdleController(service, service.getContext(),
95 service.getLock());
Amith Yamasanib0ff3222015-03-04 09:56:14 -080096 }
97 return sController;
98 }
99 }
100
Dianne Hackborn8db0fc12016-04-12 13:48:25 -0700101 private AppIdleController(JobSchedulerService service, Context context, Object lock) {
102 super(service, context, lock);
103 mJobSchedulerService = service;
Amith Yamasanib0ff3222015-03-04 09:56:14 -0800104 mUsageStatsInternal = LocalServices.getService(UsageStatsManagerInternal.class);
Dianne Hackborne9a988c2016-05-27 17:59:40 -0700105 mAppIdleParoleOn = true;
Xiaohui Chen8dca36d2015-06-19 12:44:59 -0700106 mUsageStatsInternal.addAppIdleStateChangeListener(new AppIdleStateChangeListener());
Amith Yamasanib0ff3222015-03-04 09:56:14 -0800107 }
108
Amith Yamasanib0ff3222015-03-04 09:56:14 -0800109 @Override
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800110 public void maybeStartTrackingJobLocked(JobStatus jobStatus, JobStatus lastJob) {
Dianne Hackborne9a988c2016-05-27 17:59:40 -0700111 if (!mInitializedParoleOn) {
112 mInitializedParoleOn = true;
113 mAppIdleParoleOn = mUsageStatsInternal.isAppIdleParoleOn();
114 }
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800115 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 Yamasanib0ff3222015-03-04 09:56:14 -0800121 }
Dianne Hackbornb0001f62016-02-16 10:30:33 -0800122 jobStatus.setAppNotIdleConstraintSatisfied(!appIdle);
Amith Yamasanib0ff3222015-03-04 09:56:14 -0800123 }
124
125 @Override
Dianne Hackbornf9bac162017-04-20 17:17:48 -0700126 public void maybeStopTrackingJobLocked(JobStatus jobStatus, JobStatus incomingJob,
127 boolean forUpdate) {
Amith Yamasanib0ff3222015-03-04 09:56:14 -0800128 }
129
130 @Override
Dianne Hackbornef3aa6e2016-04-29 18:18:08 -0700131 public void dumpControllerStateLocked(final PrintWriter pw, final int filterUid) {
Dianne Hackborne9a988c2016-05-27 17:59:40 -0700132 pw.print("AppIdle: parole on = ");
133 pw.println(mAppIdleParoleOn);
Dianne Hackborn8db0fc12016-04-12 13:48:25 -0700134 mJobSchedulerService.getJobStore().forEachJob(new JobStore.JobStatusFunctor() {
135 @Override public void process(JobStatus jobStatus) {
Dianne Hackbornef3aa6e2016-04-29 18:18:08 -0700136 // Skip printing details if the caller requested a filter
137 if (!jobStatus.shouldDump(filterUid)) {
138 return;
139 }
Dianne Hackborne9a988c2016-05-27 17:59:40 -0700140 pw.print(" #");
141 jobStatus.printUniqueId(pw);
142 pw.print(" from ");
143 UserHandle.formatUid(pw, jobStatus.getSourceUid());
144 pw.print(": ");
Dianne Hackborn8db0fc12016-04-12 13:48:25 -0700145 pw.print(jobStatus.getSourcePackageName());
Dianne Hackborn7ab40252016-06-15 17:30:24 -0700146 if ((jobStatus.satisfiedConstraints&JobStatus.CONSTRAINT_APP_NOT_IDLE) != 0) {
147 pw.println(" RUNNABLE");
148 } else {
149 pw.println(" WAITING");
150 }
Dianne Hackborn8db0fc12016-04-12 13:48:25 -0700151 }
152 });
Amith Yamasanib0ff3222015-03-04 09:56:14 -0800153 }
154
Xiaohui Chen8dca36d2015-06-19 12:44:59 -0700155 void setAppIdleParoleOn(boolean isAppIdleParoleOn) {
Amith Yamasanib0ff3222015-03-04 09:56:14 -0800156 // Flag if any app's idle state has changed
157 boolean changed = false;
Dianne Hackborn33d31c52016-02-16 10:30:33 -0800158 synchronized (mLock) {
Xiaohui Chen8dca36d2015-06-19 12:44:59 -0700159 if (mAppIdleParoleOn == isAppIdleParoleOn) {
Amith Yamasanib0ff3222015-03-04 09:56:14 -0800160 return;
161 }
Xiaohui Chen8dca36d2015-06-19 12:44:59 -0700162 mAppIdleParoleOn = isAppIdleParoleOn;
Dianne Hackborn8db0fc12016-04-12 13:48:25 -0700163 GlobalUpdateFunc update = new GlobalUpdateFunc();
164 mJobSchedulerService.getJobStore().forEachJob(update);
165 if (update.mChanged) {
166 changed = true;
Amith Yamasanib0ff3222015-03-04 09:56:14 -0800167 }
168 }
169 if (changed) {
170 mStateChangedListener.onControllerStateChanged();
171 }
172 }
Xiaohui Chen8dca36d2015-06-19 12:44:59 -0700173
Dianne Hackborn6466c1c2017-06-13 10:33:19 -0700174 private final class AppIdleStateChangeListener
Xiaohui Chen8dca36d2015-06-19 12:44:59 -0700175 extends UsageStatsManagerInternal.AppIdleStateChangeListener {
176 @Override
Amith Yamasani84cd7b72017-11-07 13:59:37 -0800177 public void onAppIdleStateChanged(String packageName, int userId, boolean idle, int bucket) {
Xiaohui Chen8dca36d2015-06-19 12:44:59 -0700178 boolean changed = false;
Dianne Hackborn33d31c52016-02-16 10:30:33 -0800179 synchronized (mLock) {
Xiaohui Chen8dca36d2015-06-19 12:44:59 -0700180 if (mAppIdleParoleOn) {
181 return;
182 }
Christopher Tatea732f012017-10-26 17:26:53 -0700183
Dianne Hackborn8db0fc12016-04-12 13:48:25 -0700184 PackageUpdateFunc update = new PackageUpdateFunc(userId, packageName, idle);
185 mJobSchedulerService.getJobStore().forEachJob(update);
186 if (update.mChanged) {
187 changed = true;
Xiaohui Chen8dca36d2015-06-19 12:44:59 -0700188 }
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 Yamasanib0ff3222015-03-04 09:56:14 -0800203}