Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 1 | package com.android.server.usage; |
| 2 | |
Kweku Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 3 | import android.annotation.NonNull; |
Christopher Tate | b909c4d5 | 2019-10-21 12:50:37 -0700 | [diff] [blame] | 4 | import android.annotation.UserIdInt; |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 5 | import android.app.usage.AppStandbyInfo; |
| 6 | import android.app.usage.UsageEvents; |
| 7 | import android.app.usage.UsageStatsManager.StandbyBuckets; |
Kweku Adams | aa46194 | 2020-03-16 11:59:05 -0700 | [diff] [blame] | 8 | import android.app.usage.UsageStatsManager.SystemForcedReasons; |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 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 | */ |
| 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 Tate | b909c4d5 | 2019-10-21 12:50:37 -0700 | [diff] [blame] | 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. */ |
| 44 | public abstract void onAppIdleStateChanged(String packageName, @UserIdInt int userId, |
| 45 | boolean idle, int bucket, int reason); |
| 46 | |
| 47 | /** |
Kweku Adams | e660176 | 2020-05-01 15:06:18 -0700 | [diff] [blame] | 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 | */ |
| 51 | public void onParoleStateChanged(boolean isParoleOn) { |
| 52 | // No-op by default |
| 53 | } |
| 54 | |
| 55 | /** |
Christopher Tate | b909c4d5 | 2019-10-21 12:50:37 -0700 | [diff] [blame] | 56 | * 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 Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 64 | void onBootPhase(int phase); |
| 65 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 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 | */ |
| 72 | void postOneTimeCheckIdleStates(); |
| 73 | |
Michael Wachenschwanz | 793da64 | 2020-06-11 16:14:48 -0700 | [diff] [blame] | 74 | void reportEvent(UsageEvents.Event event, int userId); |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 75 | |
| 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 Adams | df33ae1 | 2019-10-08 11:51:41 -0700 | [diff] [blame] | 88 | /** |
| 89 | * @see #isAppIdleFiltered(String, int, int, long) |
| 90 | */ |
| 91 | boolean isAppIdleFiltered(String packageName, int userId, long elapsedRealtime, |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 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. |
Kweku Adams | f35ed370 | 2020-02-11 17:32:54 -0800 | [diff] [blame] | 97 | * This happens if the device is plugged in or otherwise temporarily allowed to make exceptions. |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 98 | * Called by interface impls. |
| 99 | */ |
| 100 | boolean isAppIdleFiltered(String packageName, int appId, int userId, |
| 101 | long elapsedRealtime); |
| 102 | |
Kweku Adams | e660176 | 2020-05-01 15:06:18 -0700 | [diff] [blame] | 103 | /** |
| 104 | * @return true if currently app idle parole mode is on. |
| 105 | */ |
| 106 | boolean isInParole(); |
| 107 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 108 | 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 Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 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. |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 121 | * If attempting to automatically place an app in the RESTRICTED bucket, use |
| 122 | * {@link #restrictApp(String, int, int)} instead. |
Kweku Adams | c4ee998 | 2020-01-08 11:14:39 -0800 | [diff] [blame] | 123 | */ |
| 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 Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 132 | |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 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 |
Kweku Adams | aa46194 | 2020-03-16 11:59:05 -0700 | [diff] [blame] | 140 | * UsageStatsManager.REASON_SUB_FORCED_SYSTEM_FLAG_* reasons. |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 141 | */ |
Kweku Adams | aa46194 | 2020-03-16 11:59:05 -0700 | [diff] [blame] | 142 | void restrictApp(@NonNull String packageName, int userId, |
| 143 | @SystemForcedReasons int restrictReason); |
Kweku Adams | c6a9b34 | 2020-01-08 18:37:26 -0800 | [diff] [blame] | 144 | |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 145 | 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 Wachenschwanz | 793da64 | 2020-06-11 16:14:48 -0700 | [diff] [blame] | 153 | void flushToDisk(); |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 154 | |
| 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 Wachenschwanz | 793da64 | 2020-06-11 16:14:48 -0700 | [diff] [blame] | 163 | void dumpUsers(IndentingPrintWriter idpw, int[] userIds, List<String> pkgs); |
Makoto Onuki | a72e04f | 2019-10-03 11:10:45 -0700 | [diff] [blame] | 164 | |
| 165 | void dumpState(String[] args, PrintWriter pw); |
| 166 | |
| 167 | boolean isAppIdleEnabled(); |
| 168 | } |