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.UsageEvents;
7 import android.app.usage.UsageStatsManager.StandbyBuckets;
8 import android.app.usage.UsageStatsManager.SystemForcedReasons;
9 import android.content.Context;
10 import android.os.Looper;
11 
12 import com.android.internal.util.IndentingPrintWriter;
13 
14 import java.io.PrintWriter;
15 import java.lang.reflect.Constructor;
16 import java.lang.reflect.InvocationTargetException;
17 import java.util.List;
18 import java.util.Set;
19 
20 public interface AppStandbyInternal {
21     /**
22      * TODO AppStandbyController should probably be a binder service, and then we shouldn't need
23      * this method.
24      */
newAppStandbyController(ClassLoader loader, Context context, Looper looper)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 
38     /**
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. */
onAppIdleStateChanged(String packageName, @UserIdInt int userId, boolean idle, int bucket, int reason)44         public abstract void onAppIdleStateChanged(String packageName, @UserIdInt int userId,
45                 boolean idle, int bucket, int reason);
46 
47         /**
48          * 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          */
onParoleStateChanged(boolean isParoleOn)51         public void onParoleStateChanged(boolean isParoleOn) {
52             // No-op by default
53         }
54 
55         /**
56          * Optional callback to inform the listener that the app has transitioned into
57          * an active state due to user interaction.
58          */
onUserInteractionStarted(String packageName, @UserIdInt int userId)59         public void onUserInteractionStarted(String packageName, @UserIdInt int userId) {
60             // No-op by default
61         }
62     }
63 
onBootPhase(int phase)64     void onBootPhase(int phase);
65 
postCheckIdleStates(int userId)66     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      */
postOneTimeCheckIdleStates()72     void postOneTimeCheckIdleStates();
73 
reportEvent(UsageEvents.Event event, int userId)74     void reportEvent(UsageEvents.Event event, int userId);
75 
setLastJobRunTime(String packageName, int userId, long elapsedRealtime)76     void setLastJobRunTime(String packageName, int userId, long elapsedRealtime);
77 
getTimeSinceLastJobRun(String packageName, int userId)78     long getTimeSinceLastJobRun(String packageName, int userId);
79 
onUserRemoved(int userId)80     void onUserRemoved(int userId);
81 
addListener(AppIdleStateChangeListener listener)82     void addListener(AppIdleStateChangeListener listener);
83 
removeListener(AppIdleStateChangeListener listener)84     void removeListener(AppIdleStateChangeListener listener);
85 
getAppId(String packageName)86     int getAppId(String packageName);
87 
88     /**
89      * @see #isAppIdleFiltered(String, int, int, long)
90      */
isAppIdleFiltered(String packageName, int userId, long elapsedRealtime, boolean shouldObfuscateInstantApps)91     boolean isAppIdleFiltered(String packageName, int userId, long elapsedRealtime,
92             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.
97      * This happens if the device is plugged in or otherwise temporarily allowed to make exceptions.
98      * Called by interface impls.
99      */
isAppIdleFiltered(String packageName, int appId, int userId, long elapsedRealtime)100     boolean isAppIdleFiltered(String packageName, int appId, int userId,
101             long elapsedRealtime);
102 
103     /**
104      * @return true if currently app idle parole mode is on.
105      */
isInParole()106     boolean isInParole();
107 
getIdleUidsForUser(int userId)108     int[] getIdleUidsForUser(int userId);
109 
setAppIdleAsync(String packageName, boolean idle, int userId)110     void setAppIdleAsync(String packageName, boolean idle, int userId);
111 
112     @StandbyBuckets
getAppStandbyBucket(String packageName, int userId, long elapsedRealtime, boolean shouldObfuscateInstantApps)113     int getAppStandbyBucket(String packageName, int userId,
114             long elapsedRealtime, boolean shouldObfuscateInstantApps);
115 
getAppStandbyBuckets(int userId)116     List<AppStandbyInfo> getAppStandbyBuckets(int userId);
117 
118     /**
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.
121      * If attempting to automatically place an app in the RESTRICTED bucket, use
122      * {@link #restrictApp(String, int, int)} instead.
123      */
setAppStandbyBucket(@onNull String packageName, int bucket, int userId, int callingUid, int callingPid)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      */
setAppStandbyBuckets(@onNull List<AppStandbyInfo> appBuckets, int userId, int callingUid, int callingPid)130     void setAppStandbyBuckets(@NonNull List<AppStandbyInfo> appBuckets, int userId, int callingUid,
131             int callingPid);
132 
133     /**
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
140      *                       UsageStatsManager.REASON_SUB_FORCED_SYSTEM_FLAG_* reasons.
141      */
restrictApp(@onNull String packageName, int userId, @SystemForcedReasons int restrictReason)142     void restrictApp(@NonNull String packageName, int userId,
143             @SystemForcedReasons int restrictReason);
144 
addActiveDeviceAdmin(String adminPkg, int userId)145     void addActiveDeviceAdmin(String adminPkg, int userId);
146 
setActiveAdminApps(Set<String> adminPkgs, int userId)147     void setActiveAdminApps(Set<String> adminPkgs, int userId);
148 
onAdminDataAvailable()149     void onAdminDataAvailable();
150 
clearCarrierPrivilegedApps()151     void clearCarrierPrivilegedApps();
152 
flushToDisk()153     void flushToDisk();
154 
initializeDefaultsForSystemApps(int userId)155     void initializeDefaultsForSystemApps(int userId);
156 
postReportContentProviderUsage(String name, String packageName, int userId)157     void postReportContentProviderUsage(String name, String packageName, int userId);
158 
postReportSyncScheduled(String packageName, int userId, boolean exempted)159     void postReportSyncScheduled(String packageName, int userId, boolean exempted);
160 
postReportExemptedSyncStart(String packageName, int userId)161     void postReportExemptedSyncStart(String packageName, int userId);
162 
dumpUsers(IndentingPrintWriter idpw, int[] userIds, List<String> pkgs)163     void dumpUsers(IndentingPrintWriter idpw, int[] userIds, List<String> pkgs);
164 
dumpState(String[] args, PrintWriter pw)165     void dumpState(String[] args, PrintWriter pw);
166 
isAppIdleEnabled()167     boolean isAppIdleEnabled();
168 }
169