blob: a23af3541e1966d945854be70260b820cf6c03b5 [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 */
Xiaohui Chen8dca36d2015-06-19 12:44:59 -070036public 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 Hackborn141f11c2016-04-05 15:46:12 -0700126 public void maybeStopTrackingJobLocked(JobStatus jobStatus, JobStatus incomingJob, boolean forUpdate) {
Amith Yamasanib0ff3222015-03-04 09:56:14 -0800127 }
128
129 @Override
Dianne Hackbornef3aa6e2016-04-29 18:18:08 -0700130 public void dumpControllerStateLocked(final PrintWriter pw, final int filterUid) {
Dianne Hackborne9a988c2016-05-27 17:59:40 -0700131 pw.print("AppIdle: parole on = ");
132 pw.println(mAppIdleParoleOn);
Dianne Hackborn8db0fc12016-04-12 13:48:25 -0700133 mJobSchedulerService.getJobStore().forEachJob(new JobStore.JobStatusFunctor() {
134 @Override public void process(JobStatus jobStatus) {
Dianne Hackbornef3aa6e2016-04-29 18:18:08 -0700135 // Skip printing details if the caller requested a filter
136 if (!jobStatus.shouldDump(filterUid)) {
137 return;
138 }
Dianne Hackborne9a988c2016-05-27 17:59:40 -0700139 pw.print(" #");
140 jobStatus.printUniqueId(pw);
141 pw.print(" from ");
142 UserHandle.formatUid(pw, jobStatus.getSourceUid());
143 pw.print(": ");
Dianne Hackborn8db0fc12016-04-12 13:48:25 -0700144 pw.print(jobStatus.getSourcePackageName());
Dianne Hackborne9a988c2016-05-27 17:59:40 -0700145 pw.print(", runnable=");
Dianne Hackborn8db0fc12016-04-12 13:48:25 -0700146 pw.println((jobStatus.satisfiedConstraints&JobStatus.CONSTRAINT_APP_NOT_IDLE) != 0);
147 }
148 });
Amith Yamasanib0ff3222015-03-04 09:56:14 -0800149 }
150
Xiaohui Chen8dca36d2015-06-19 12:44:59 -0700151 void setAppIdleParoleOn(boolean isAppIdleParoleOn) {
Amith Yamasanib0ff3222015-03-04 09:56:14 -0800152 // Flag if any app's idle state has changed
153 boolean changed = false;
Dianne Hackborn33d31c52016-02-16 10:30:33 -0800154 synchronized (mLock) {
Xiaohui Chen8dca36d2015-06-19 12:44:59 -0700155 if (mAppIdleParoleOn == isAppIdleParoleOn) {
Amith Yamasanib0ff3222015-03-04 09:56:14 -0800156 return;
157 }
Xiaohui Chen8dca36d2015-06-19 12:44:59 -0700158 mAppIdleParoleOn = isAppIdleParoleOn;
Dianne Hackborn8db0fc12016-04-12 13:48:25 -0700159 GlobalUpdateFunc update = new GlobalUpdateFunc();
160 mJobSchedulerService.getJobStore().forEachJob(update);
161 if (update.mChanged) {
162 changed = true;
Amith Yamasanib0ff3222015-03-04 09:56:14 -0800163 }
164 }
165 if (changed) {
166 mStateChangedListener.onControllerStateChanged();
167 }
168 }
Xiaohui Chen8dca36d2015-06-19 12:44:59 -0700169
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 Hackborn33d31c52016-02-16 10:30:33 -0800175 synchronized (mLock) {
Xiaohui Chen8dca36d2015-06-19 12:44:59 -0700176 if (mAppIdleParoleOn) {
177 return;
178 }
Dianne Hackborn8db0fc12016-04-12 13:48:25 -0700179 PackageUpdateFunc update = new PackageUpdateFunc(userId, packageName, idle);
180 mJobSchedulerService.getJobStore().forEachJob(update);
181 if (update.mChanged) {
182 changed = true;
Xiaohui Chen8dca36d2015-06-19 12:44:59 -0700183 }
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 Yamasanib0ff3222015-03-04 09:56:14 -0800198}