blob: e15f0f37fc62ec4b4c54eaafadbbcb35ad5851a5 [file] [log] [blame]
Makoto Onukia72e04f2019-10-03 11:10:45 -07001package com.android.server.usage;
2
Kweku Adamsc4ee9982020-01-08 11:14:39 -08003import android.annotation.NonNull;
Christopher Tateb909c4d52019-10-21 12:50:37 -07004import android.annotation.UserIdInt;
Makoto Onukia72e04f2019-10-03 11:10:45 -07005import android.app.usage.AppStandbyInfo;
6import android.app.usage.UsageEvents;
7import android.app.usage.UsageStatsManager.StandbyBuckets;
Kweku Adamsaa461942020-03-16 11:59:05 -07008import android.app.usage.UsageStatsManager.SystemForcedReasons;
Makoto Onukia72e04f2019-10-03 11:10:45 -07009import android.content.Context;
10import android.os.Looper;
11
12import com.android.internal.util.IndentingPrintWriter;
13
14import java.io.PrintWriter;
15import java.lang.reflect.Constructor;
16import java.lang.reflect.InvocationTargetException;
17import java.util.List;
18import java.util.Set;
19
20public interface AppStandbyInternal {
21 /**
22 * TODO AppStandbyController should probably be a binder service, and then we shouldn't need
23 * this method.
24 */
25 static AppStandbyInternal newAppStandbyController(ClassLoader loader, Context context,
26 Looper looper) {
27 try {
28 final Class<?> clazz = Class.forName("com.android.server.usage.AppStandbyController",
29 true, loader);
30 final Constructor<?> ctor = clazz.getConstructor(Context.class, Looper.class);
31 return (AppStandbyInternal) ctor.newInstance(context, looper);
32 } catch (NoSuchMethodException | InstantiationException
33 | IllegalAccessException | InvocationTargetException | ClassNotFoundException e) {
34 throw new RuntimeException("Unable to instantiate AppStandbyController!", e);
35 }
36 }
37
Christopher Tateb909c4d52019-10-21 12:50:37 -070038 /**
39 * Listener interface for notifications that an app's idle state changed.
40 */
41 abstract static class AppIdleStateChangeListener {
42
43 /** Callback to inform listeners that the idle state has changed to a new bucket. */
44 public abstract void onAppIdleStateChanged(String packageName, @UserIdInt int userId,
45 boolean idle, int bucket, int reason);
46
47 /**
Kweku Adamse6601762020-05-01 15:06:18 -070048 * Callback to inform listeners that the parole state has changed. This means apps are
49 * allowed to do work even if they're idle or in a low bucket.
50 */
51 public void onParoleStateChanged(boolean isParoleOn) {
52 // No-op by default
53 }
54
55 /**
Christopher Tateb909c4d52019-10-21 12:50:37 -070056 * Optional callback to inform the listener that the app has transitioned into
57 * an active state due to user interaction.
58 */
59 public void onUserInteractionStarted(String packageName, @UserIdInt int userId) {
60 // No-op by default
61 }
62 }
63
Makoto Onukia72e04f2019-10-03 11:10:45 -070064 void onBootPhase(int phase);
65
Makoto Onukia72e04f2019-10-03 11:10:45 -070066 void postCheckIdleStates(int userId);
67
68 /**
69 * We send a different message to check idle states once, otherwise we would end up
70 * scheduling a series of repeating checkIdleStates each time we fired off one.
71 */
72 void postOneTimeCheckIdleStates();
73
Michael Wachenschwanz793da642020-06-11 16:14:48 -070074 void reportEvent(UsageEvents.Event event, int userId);
Makoto Onukia72e04f2019-10-03 11:10:45 -070075
76 void setLastJobRunTime(String packageName, int userId, long elapsedRealtime);
77
78 long getTimeSinceLastJobRun(String packageName, int userId);
79
80 void onUserRemoved(int userId);
81
82 void addListener(AppIdleStateChangeListener listener);
83
84 void removeListener(AppIdleStateChangeListener listener);
85
86 int getAppId(String packageName);
87
Kweku Adamsdf33ae12019-10-08 11:51:41 -070088 /**
89 * @see #isAppIdleFiltered(String, int, int, long)
90 */
91 boolean isAppIdleFiltered(String packageName, int userId, long elapsedRealtime,
Makoto Onukia72e04f2019-10-03 11:10:45 -070092 boolean shouldObfuscateInstantApps);
93
94 /**
95 * Checks if an app has been idle for a while and filters out apps that are excluded.
96 * It returns false if the current system state allows all apps to be considered active.
Kweku Adamsf35ed3702020-02-11 17:32:54 -080097 * This happens if the device is plugged in or otherwise temporarily allowed to make exceptions.
Makoto Onukia72e04f2019-10-03 11:10:45 -070098 * Called by interface impls.
99 */
100 boolean isAppIdleFiltered(String packageName, int appId, int userId,
101 long elapsedRealtime);
102
Kweku Adamse6601762020-05-01 15:06:18 -0700103 /**
104 * @return true if currently app idle parole mode is on.
105 */
106 boolean isInParole();
107
Makoto Onukia72e04f2019-10-03 11:10:45 -0700108 int[] getIdleUidsForUser(int userId);
109
110 void setAppIdleAsync(String packageName, boolean idle, int userId);
111
112 @StandbyBuckets
113 int getAppStandbyBucket(String packageName, int userId,
114 long elapsedRealtime, boolean shouldObfuscateInstantApps);
115
116 List<AppStandbyInfo> getAppStandbyBuckets(int userId);
117
Kweku Adamsc4ee9982020-01-08 11:14:39 -0800118 /**
119 * Changes an app's standby bucket to the provided value. The caller can only set the standby
120 * bucket for a different app than itself.
Kweku Adamsc6a9b342020-01-08 18:37:26 -0800121 * If attempting to automatically place an app in the RESTRICTED bucket, use
122 * {@link #restrictApp(String, int, int)} instead.
Kweku Adamsc4ee9982020-01-08 11:14:39 -0800123 */
124 void setAppStandbyBucket(@NonNull String packageName, int bucket, int userId, int callingUid,
125 int callingPid);
126
127 /**
128 * Changes the app standby bucket for multiple apps at once.
129 */
130 void setAppStandbyBuckets(@NonNull List<AppStandbyInfo> appBuckets, int userId, int callingUid,
131 int callingPid);
Makoto Onukia72e04f2019-10-03 11:10:45 -0700132
Kweku Adamsc6a9b342020-01-08 18:37:26 -0800133 /**
134 * Put the specified app in the
135 * {@link android.app.usage.UsageStatsManager#STANDBY_BUCKET_RESTRICTED}
136 * bucket. If it has been used by the user recently, the restriction will delayed until an
137 * appropriate time.
138 *
139 * @param restrictReason The restrictReason for restricting the app. Should be one of the
Kweku Adamsaa461942020-03-16 11:59:05 -0700140 * UsageStatsManager.REASON_SUB_FORCED_SYSTEM_FLAG_* reasons.
Kweku Adamsc6a9b342020-01-08 18:37:26 -0800141 */
Kweku Adamsaa461942020-03-16 11:59:05 -0700142 void restrictApp(@NonNull String packageName, int userId,
143 @SystemForcedReasons int restrictReason);
Kweku Adamsc6a9b342020-01-08 18:37:26 -0800144
Makoto Onukia72e04f2019-10-03 11:10:45 -0700145 void addActiveDeviceAdmin(String adminPkg, int userId);
146
147 void setActiveAdminApps(Set<String> adminPkgs, int userId);
148
149 void onAdminDataAvailable();
150
151 void clearCarrierPrivilegedApps();
152
Michael Wachenschwanz793da642020-06-11 16:14:48 -0700153 void flushToDisk();
Makoto Onukia72e04f2019-10-03 11:10:45 -0700154
155 void initializeDefaultsForSystemApps(int userId);
156
157 void postReportContentProviderUsage(String name, String packageName, int userId);
158
159 void postReportSyncScheduled(String packageName, int userId, boolean exempted);
160
161 void postReportExemptedSyncStart(String packageName, int userId);
162
Michael Wachenschwanz793da642020-06-11 16:14:48 -0700163 void dumpUsers(IndentingPrintWriter idpw, int[] userIds, List<String> pkgs);
Makoto Onukia72e04f2019-10-03 11:10:45 -0700164
165 void dumpState(String[] args, PrintWriter pw);
166
167 boolean isAppIdleEnabled();
168}