1 package com.android.server.usage;
2 
3 import android.annotation.NonNull;
4 import android.annotation.UserIdInt;
5 import android.app.usage.AppStandbyInfo;
6 import android.app.usage.UsageStatsManager.StandbyBuckets;
7 import android.app.usage.UsageStatsManager.SystemForcedReasons;
8 import android.content.Context;
9 import android.util.IndentingPrintWriter;
10 
11 import java.io.PrintWriter;
12 import java.lang.reflect.Constructor;
13 import java.lang.reflect.InvocationTargetException;
14 import java.util.List;
15 import java.util.Set;
16 
17 public interface AppStandbyInternal {
18     /**
19      * TODO AppStandbyController should probably be a binder service, and then we shouldn't need
20      * this method.
21      */
newAppStandbyController(ClassLoader loader, Context context)22     static AppStandbyInternal newAppStandbyController(ClassLoader loader, Context context) {
23         try {
24             final Class<?> clazz = Class.forName("com.android.server.usage.AppStandbyController",
25                     true, loader);
26             final Constructor<?> ctor =  clazz.getConstructor(Context.class);
27             return (AppStandbyInternal) ctor.newInstance(context);
28         } catch (NoSuchMethodException | InstantiationException
29                 | IllegalAccessException | InvocationTargetException | ClassNotFoundException e) {
30             throw new RuntimeException("Unable to instantiate AppStandbyController!", e);
31         }
32     }
33 
34     /**
35      * Listener interface for notifications that an app's idle state changed.
36      */
37     abstract static class AppIdleStateChangeListener {
38 
39         /** Callback to inform listeners that the idle state has changed to a new bucket. */
onAppIdleStateChanged(String packageName, @UserIdInt int userId, boolean idle, int bucket, int reason)40         public abstract void onAppIdleStateChanged(String packageName, @UserIdInt int userId,
41                 boolean idle, int bucket, int reason);
42 
43         /**
44          * Callback to inform listeners that the parole state has changed. This means apps are
45          * allowed to do work even if they're idle or in a low bucket.
46          */
onParoleStateChanged(boolean isParoleOn)47         public void onParoleStateChanged(boolean isParoleOn) {
48             // No-op by default
49         }
50 
51         /**
52          * Optional callback to inform the listener that the app has transitioned into
53          * an active state due to user interaction.
54          */
onUserInteractionStarted(String packageName, @UserIdInt int userId)55         public void onUserInteractionStarted(String packageName, @UserIdInt int userId) {
56             // No-op by default
57         }
58     }
59 
onBootPhase(int phase)60     void onBootPhase(int phase);
61 
postCheckIdleStates(int userId)62     void postCheckIdleStates(int userId);
63 
64     /**
65      * We send a different message to check idle states once, otherwise we would end up
66      * scheduling a series of repeating checkIdleStates each time we fired off one.
67      */
postOneTimeCheckIdleStates()68     void postOneTimeCheckIdleStates();
69 
setLastJobRunTime(String packageName, int userId, long elapsedRealtime)70     void setLastJobRunTime(String packageName, int userId, long elapsedRealtime);
71 
getTimeSinceLastJobRun(String packageName, int userId)72     long getTimeSinceLastJobRun(String packageName, int userId);
73 
onUserRemoved(int userId)74     void onUserRemoved(int userId);
75 
addListener(AppIdleStateChangeListener listener)76     void addListener(AppIdleStateChangeListener listener);
77 
removeListener(AppIdleStateChangeListener listener)78     void removeListener(AppIdleStateChangeListener listener);
79 
getAppId(String packageName)80     int getAppId(String packageName);
81 
82     /**
83      * @see #isAppIdleFiltered(String, int, int, long)
84      */
isAppIdleFiltered(String packageName, int userId, long elapsedRealtime, boolean shouldObfuscateInstantApps)85     boolean isAppIdleFiltered(String packageName, int userId, long elapsedRealtime,
86             boolean shouldObfuscateInstantApps);
87 
88     /**
89      * Checks if an app has been idle for a while and filters out apps that are excluded.
90      * It returns false if the current system state allows all apps to be considered active.
91      * This happens if the device is plugged in or otherwise temporarily allowed to make exceptions.
92      * Called by interface impls.
93      */
isAppIdleFiltered(String packageName, int appId, int userId, long elapsedRealtime)94     boolean isAppIdleFiltered(String packageName, int appId, int userId,
95             long elapsedRealtime);
96 
97     /**
98      * @return true if currently app idle parole mode is on.
99      */
isInParole()100     boolean isInParole();
101 
getIdleUidsForUser(int userId)102     int[] getIdleUidsForUser(int userId);
103 
setAppIdleAsync(String packageName, boolean idle, int userId)104     void setAppIdleAsync(String packageName, boolean idle, int userId);
105 
106     @StandbyBuckets
getAppStandbyBucket(String packageName, int userId, long elapsedRealtime, boolean shouldObfuscateInstantApps)107     int getAppStandbyBucket(String packageName, int userId,
108             long elapsedRealtime, boolean shouldObfuscateInstantApps);
109 
getAppStandbyBuckets(int userId)110     List<AppStandbyInfo> getAppStandbyBuckets(int userId);
111 
112     /**
113      * Changes an app's standby bucket to the provided value. The caller can only set the standby
114      * bucket for a different app than itself.
115      * If attempting to automatically place an app in the RESTRICTED bucket, use
116      * {@link #restrictApp(String, int, int)} instead.
117      */
setAppStandbyBucket(@onNull String packageName, int bucket, int userId, int callingUid, int callingPid)118     void setAppStandbyBucket(@NonNull String packageName, int bucket, int userId, int callingUid,
119             int callingPid);
120 
121     /**
122      * Changes the app standby bucket for multiple apps at once.
123      */
setAppStandbyBuckets(@onNull List<AppStandbyInfo> appBuckets, int userId, int callingUid, int callingPid)124     void setAppStandbyBuckets(@NonNull List<AppStandbyInfo> appBuckets, int userId, int callingUid,
125             int callingPid);
126 
127     /**
128      * Put the specified app in the
129      * {@link android.app.usage.UsageStatsManager#STANDBY_BUCKET_RESTRICTED}
130      * bucket. If it has been used by the user recently, the restriction will delayed until an
131      * appropriate time.
132      *
133      * @param restrictReason The restrictReason for restricting the app. Should be one of the
134      *                       UsageStatsManager.REASON_SUB_FORCED_SYSTEM_FLAG_* reasons.
135      */
restrictApp(@onNull String packageName, int userId, @SystemForcedReasons int restrictReason)136     void restrictApp(@NonNull String packageName, int userId,
137             @SystemForcedReasons int restrictReason);
138 
139     /**
140      * Put the specified app in the
141      * {@link android.app.usage.UsageStatsManager#STANDBY_BUCKET_RESTRICTED}
142      * bucket. If it has been used by the user recently, the restriction will delayed
143      * until an appropriate time. This should only be used in cases where
144      * {@link #restrictApp(String, int, int)} is not sufficient.
145      *
146      * @param mainReason     The main reason for restricting the app. Must be either {@link
147      *                       android.app.usage.UsageStatsManager#REASON_MAIN_FORCED_BY_SYSTEM} or
148      *                       {@link android.app.usage.UsageStatsManager#REASON_MAIN_FORCED_BY_USER}.
149      *                       Calls providing any other value will be ignored.
150      * @param restrictReason The restrictReason for restricting the app. Should be one of the
151      *                       UsageStatsManager.REASON_SUB_FORCED_SYSTEM_FLAG_* reasons.
152      */
restrictApp(@onNull String packageName, int userId, int mainReason, @SystemForcedReasons int restrictReason)153     void restrictApp(@NonNull String packageName, int userId, int mainReason,
154             @SystemForcedReasons int restrictReason);
155 
addActiveDeviceAdmin(String adminPkg, int userId)156     void addActiveDeviceAdmin(String adminPkg, int userId);
157 
setActiveAdminApps(Set<String> adminPkgs, int userId)158     void setActiveAdminApps(Set<String> adminPkgs, int userId);
159 
onAdminDataAvailable()160     void onAdminDataAvailable();
161 
clearCarrierPrivilegedApps()162     void clearCarrierPrivilegedApps();
163 
flushToDisk()164     void flushToDisk();
165 
initializeDefaultsForSystemApps(int userId)166     void initializeDefaultsForSystemApps(int userId);
167 
postReportContentProviderUsage(String name, String packageName, int userId)168     void postReportContentProviderUsage(String name, String packageName, int userId);
169 
postReportSyncScheduled(String packageName, int userId, boolean exempted)170     void postReportSyncScheduled(String packageName, int userId, boolean exempted);
171 
postReportExemptedSyncStart(String packageName, int userId)172     void postReportExemptedSyncStart(String packageName, int userId);
173 
dumpUsers(IndentingPrintWriter idpw, int[] userIds, List<String> pkgs)174     void dumpUsers(IndentingPrintWriter idpw, int[] userIds, List<String> pkgs);
175 
dumpState(String[] args, PrintWriter pw)176     void dumpState(String[] args, PrintWriter pw);
177 
isAppIdleEnabled()178     boolean isAppIdleEnabled();
179 }
180