Adam Lesinski | 0debc9a | 2014-07-16 19:09:13 -0700 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | * use this file except in compliance with the License. You may obtain a copy |
| 6 | * 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, WITHOUT |
| 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | * License for the specific language governing permissions and limitations |
| 14 | * under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.app.usage; |
| 18 | |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 19 | import android.annotation.UserIdInt; |
Amith Yamasani | afbccb7 | 2017-11-27 10:44:24 -0800 | [diff] [blame] | 20 | import android.app.usage.UsageStatsManager.StandbyBuckets; |
Adam Lesinski | 0debc9a | 2014-07-16 19:09:13 -0700 | [diff] [blame] | 21 | import android.content.ComponentName; |
Adam Lesinski | 7f61e96 | 2014-09-02 16:43:52 -0700 | [diff] [blame] | 22 | import android.content.res.Configuration; |
Adam Lesinski | 0debc9a | 2014-07-16 19:09:13 -0700 | [diff] [blame] | 23 | |
Daniel Nishi | cf9d19e | 2017-01-23 14:33:42 -0800 | [diff] [blame] | 24 | import java.util.List; |
Sudheer Shanka | 101c353 | 2018-01-08 16:28:42 -0800 | [diff] [blame] | 25 | import java.util.Set; |
Ritesh Reddy | 8a6ce2c | 2015-12-17 17:03:54 +0000 | [diff] [blame] | 26 | |
Adam Lesinski | 0debc9a | 2014-07-16 19:09:13 -0700 | [diff] [blame] | 27 | /** |
| 28 | * UsageStatsManager local system service interface. |
| 29 | * |
| 30 | * {@hide} Only for use within the system server. |
| 31 | */ |
| 32 | public abstract class UsageStatsManagerInternal { |
| 33 | |
| 34 | /** |
| 35 | * Reports an event to the UsageStatsManager. |
| 36 | * |
Adam Lesinski | 7f61e96 | 2014-09-02 16:43:52 -0700 | [diff] [blame] | 37 | * @param component The component for which this event occurred. |
Adam Lesinski | 3c15351 | 2014-07-23 17:34:34 -0700 | [diff] [blame] | 38 | * @param userId The user id to which the component belongs to. |
Adam Lesinski | 7f61e96 | 2014-09-02 16:43:52 -0700 | [diff] [blame] | 39 | * @param eventType The event that occurred. Valid values can be found at |
Adam Lesinski | 3516800 | 2014-07-21 15:25:30 -0700 | [diff] [blame] | 40 | * {@link UsageEvents} |
Adam Lesinski | 0debc9a | 2014-07-16 19:09:13 -0700 | [diff] [blame] | 41 | */ |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 42 | public abstract void reportEvent(ComponentName component, @UserIdInt int userId, int eventType); |
Adam Lesinski | 7f61e96 | 2014-09-02 16:43:52 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
Adam Lesinski | 978a1ed | 2015-03-02 11:37:24 -0800 | [diff] [blame] | 45 | * Reports an event to the UsageStatsManager. |
| 46 | * |
| 47 | * @param packageName The package for which this event occurred. |
| 48 | * @param userId The user id to which the component belongs to. |
| 49 | * @param eventType The event that occurred. Valid values can be found at |
| 50 | * {@link UsageEvents} |
| 51 | */ |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 52 | public abstract void reportEvent(String packageName, @UserIdInt int userId, int eventType); |
Adam Lesinski | 978a1ed | 2015-03-02 11:37:24 -0800 | [diff] [blame] | 53 | |
| 54 | /** |
Adam Lesinski | 7f61e96 | 2014-09-02 16:43:52 -0700 | [diff] [blame] | 55 | * Reports a configuration change to the UsageStatsManager. |
| 56 | * |
| 57 | * @param config The new device configuration. |
| 58 | */ |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 59 | public abstract void reportConfigurationChange(Configuration config, @UserIdInt int userId); |
Adam Lesinski | 0debc9a | 2014-07-16 19:09:13 -0700 | [diff] [blame] | 60 | |
| 61 | /** |
Julia Reynolds | 1fac86e | 2018-03-07 08:30:37 -0500 | [diff] [blame^] | 62 | * Reports that an application has posted an interruptive notification. |
| 63 | * |
| 64 | * @param packageName The package name of the app that posted the notification |
| 65 | * @param channelId The ID of the NotificationChannel to which the notification was posted |
| 66 | * @param userId The user in which the notification was posted |
| 67 | */ |
| 68 | public abstract void reportInterruptiveNotification(String packageName, String channelId, |
| 69 | @UserIdInt int userId); |
| 70 | |
| 71 | /** |
Makoto Onuki | ac04250 | 2016-05-20 16:39:42 -0700 | [diff] [blame] | 72 | * Reports that an action equivalent to a ShortcutInfo is taken by the user. |
| 73 | * |
| 74 | * @param packageName The package name of the shortcut publisher |
| 75 | * @param shortcutId The ID of the shortcut in question |
| 76 | * @param userId The user in which the content provider was accessed. |
| 77 | * |
| 78 | * @see android.content.pm.ShortcutManager#reportShortcutUsed(String) |
| 79 | */ |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 80 | public abstract void reportShortcutUsage(String packageName, String shortcutId, |
| 81 | @UserIdInt int userId); |
Makoto Onuki | ac04250 | 2016-05-20 16:39:42 -0700 | [diff] [blame] | 82 | |
| 83 | /** |
Amith Yamasani | 37a40c2 | 2015-06-17 13:25:42 -0700 | [diff] [blame] | 84 | * Reports that a content provider has been accessed by a foreground app. |
| 85 | * @param name The authority of the content provider |
| 86 | * @param pkgName The package name of the content provider |
| 87 | * @param userId The user in which the content provider was accessed. |
| 88 | */ |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 89 | public abstract void reportContentProviderUsage(String name, String pkgName, |
| 90 | @UserIdInt int userId); |
Amith Yamasani | 37a40c2 | 2015-06-17 13:25:42 -0700 | [diff] [blame] | 91 | |
| 92 | /** |
Adam Lesinski | 0debc9a | 2014-07-16 19:09:13 -0700 | [diff] [blame] | 93 | * Prepares the UsageStatsService for shutdown. |
| 94 | */ |
| 95 | public abstract void prepareShutdown(); |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 96 | |
| 97 | /** |
| 98 | * Returns true if the app has not been used for a certain amount of time. How much time? |
| 99 | * Could be hours, could be days, who knows? |
| 100 | * |
| 101 | * @param packageName |
Dianne Hackborn | fcc95a6 | 2015-11-02 13:43:29 -0800 | [diff] [blame] | 102 | * @param uidForAppId The uid of the app, which will be used for its app id |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 103 | * @param userId |
| 104 | * @return |
| 105 | */ |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 106 | public abstract boolean isAppIdle(String packageName, int uidForAppId, @UserIdInt int userId); |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 107 | |
| 108 | /** |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 109 | * Returns the app standby bucket that the app is currently in. This accessor does |
| 110 | * <em>not</em> obfuscate instant apps. |
| 111 | * |
| 112 | * @param packageName |
| 113 | * @param userId |
| 114 | * @param nowElapsed The current time, in the elapsedRealtime time base |
| 115 | * @return the AppStandby bucket code the app currently resides in. If the app is |
| 116 | * unknown in the given user, STANDBY_BUCKET_NEVER is returned. |
| 117 | */ |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 118 | @StandbyBuckets public abstract int getAppStandbyBucket(String packageName, |
| 119 | @UserIdInt int userId, long nowElapsed); |
Christopher Tate | a732f01 | 2017-10-26 17:26:53 -0700 | [diff] [blame] | 120 | |
| 121 | /** |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 122 | * Returns all of the uids for a given user where all packages associating with that uid |
| 123 | * are in the app idle state -- there are no associated apps that are not idle. This means |
| 124 | * all of the returned uids can be safely considered app idle. |
| 125 | */ |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 126 | public abstract int[] getIdleUidsForUser(@UserIdInt int userId); |
Dianne Hackborn | 4a503b1 | 2015-08-06 22:19:06 -0700 | [diff] [blame] | 127 | |
| 128 | /** |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 129 | * @return True if currently app idle parole mode is on. This means all idle apps are allow to |
| 130 | * run for a short period of time. |
| 131 | */ |
| 132 | public abstract boolean isAppIdleParoleOn(); |
| 133 | |
| 134 | /** |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 135 | * Sets up a listener for changes to packages being accessed. |
| 136 | * @param listener A listener within the system process. |
| 137 | */ |
| 138 | public abstract void addAppIdleStateChangeListener( |
| 139 | AppIdleStateChangeListener listener); |
| 140 | |
| 141 | /** |
| 142 | * Removes a listener that was previously added for package usage state changes. |
| 143 | * @param listener The listener within the system process to remove. |
| 144 | */ |
| 145 | public abstract void removeAppIdleStateChangeListener( |
| 146 | AppIdleStateChangeListener listener); |
| 147 | |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 148 | public static abstract class AppIdleStateChangeListener { |
Amith Yamasani | 84cd7b7 | 2017-11-07 13:59:37 -0800 | [diff] [blame] | 149 | |
| 150 | /** Callback to inform listeners that the idle state has changed to a new bucket. */ |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 151 | public abstract void onAppIdleStateChanged(String packageName, @UserIdInt int userId, |
Amith Yamasani | 119be9a | 2018-02-18 22:23:00 -0800 | [diff] [blame] | 152 | boolean idle, int bucket, int reason); |
Amith Yamasani | 84cd7b7 | 2017-11-07 13:59:37 -0800 | [diff] [blame] | 153 | |
| 154 | /** |
| 155 | * Callback to inform listeners that the parole state has changed. This means apps are |
| 156 | * allowed to do work even if they're idle or in a low bucket. |
| 157 | */ |
Xiaohui Chen | 8dca36d | 2015-06-19 12:44:59 -0700 | [diff] [blame] | 158 | public abstract void onParoleStateChanged(boolean isParoleOn); |
Christopher Tate | d117b29 | 2018-01-05 17:32:36 -0800 | [diff] [blame] | 159 | |
| 160 | /** |
| 161 | * Optional callback to inform the listener that the app has transitioned into |
| 162 | * an active state due to user interaction. |
| 163 | */ |
| 164 | public void onUserInteractionStarted(String packageName, @UserIdInt int userId) { |
| 165 | // No-op by default |
| 166 | } |
Amith Yamasani | b0ff322 | 2015-03-04 09:56:14 -0800 | [diff] [blame] | 167 | } |
| 168 | |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 169 | /** Backup/Restore API */ |
| 170 | public abstract byte[] getBackupPayload(@UserIdInt int userId, String key); |
Ritesh Reddy | 8a6ce2c | 2015-12-17 17:03:54 +0000 | [diff] [blame] | 171 | |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 172 | /** |
| 173 | * ? |
| 174 | * @param userId |
| 175 | * @param key |
| 176 | * @param payload |
| 177 | */ |
| 178 | public abstract void applyRestoredPayload(@UserIdInt int userId, String key, byte[] payload); |
Ritesh Reddy | 8a6ce2c | 2015-12-17 17:03:54 +0000 | [diff] [blame] | 179 | |
Makoto Onuki | ad62301 | 2017-05-15 09:29:34 -0700 | [diff] [blame] | 180 | /** |
Sudheer Shanka | 101c353 | 2018-01-08 16:28:42 -0800 | [diff] [blame] | 181 | * Called by DevicePolicyManagerService to inform that a new admin has been added. |
| 182 | * |
| 183 | * @param packageName the package in which the admin component is part of. |
| 184 | * @param userId the userId in which the admin has been added. |
| 185 | */ |
| 186 | public abstract void onActiveAdminAdded(String packageName, int userId); |
| 187 | |
| 188 | /** |
| 189 | * Called by DevicePolicyManagerService to inform about the active admins in an user. |
| 190 | * |
| 191 | * @param adminApps the set of active admins in {@param userId} or null if there are none. |
| 192 | * @param userId the userId to which the admin apps belong. |
| 193 | */ |
| 194 | public abstract void setActiveAdminApps(Set<String> adminApps, int userId); |
| 195 | |
| 196 | /** |
Sudheer Shanka | c53c47f | 2018-01-16 12:01:00 -0800 | [diff] [blame] | 197 | * Called by DevicePolicyManagerService during boot to inform that admin data is loaded and |
| 198 | * pushed to UsageStatsService. |
| 199 | */ |
| 200 | public abstract void onAdminDataAvailable(); |
| 201 | |
| 202 | /** |
Makoto Onuki | ad62301 | 2017-05-15 09:29:34 -0700 | [diff] [blame] | 203 | * Return usage stats. |
| 204 | * |
| 205 | * @param obfuscateInstantApps whether instant app package names need to be obfuscated in the |
| 206 | * result. |
| 207 | */ |
Amith Yamasani | 53f06ea | 2018-01-05 17:53:46 -0800 | [diff] [blame] | 208 | public abstract List<UsageStats> queryUsageStatsForUser(@UserIdInt int userId, int interval, |
| 209 | long beginTime, long endTime, boolean obfuscateInstantApps); |
| 210 | |
| 211 | /** |
| 212 | * Used to persist the last time a job was run for this app, in order to make decisions later |
| 213 | * whether a job should be deferred until later. The time passed in should be in elapsed |
| 214 | * realtime since boot. |
| 215 | * @param packageName the app that executed a job. |
| 216 | * @param userId the user associated with the job. |
| 217 | * @param elapsedRealtime the time when the job was executed, in elapsed realtime millis since |
| 218 | * boot. |
| 219 | */ |
| 220 | public abstract void setLastJobRunTime(String packageName, @UserIdInt int userId, |
| 221 | long elapsedRealtime); |
| 222 | |
| 223 | /** |
| 224 | * Returns the time in millis since a job was executed for this app, in elapsed realtime |
| 225 | * timebase. This value can be larger than the current elapsed realtime if the job was executed |
| 226 | * before the device was rebooted. The default value is {@link Long#MAX_VALUE}. |
| 227 | * @param packageName the app you're asking about. |
| 228 | * @param userId the user associated with the job. |
| 229 | * @return the time in millis since a job was last executed for the app, provided it was |
| 230 | * indicated here before by a call to {@link #setLastJobRunTime(String, int, long)}. |
| 231 | */ |
| 232 | public abstract long getTimeSinceLastJobRun(String packageName, @UserIdInt int userId); |
Christopher Tate | d117b29 | 2018-01-05 17:32:36 -0800 | [diff] [blame] | 233 | |
| 234 | /** |
| 235 | * Report a few data points about an app's job state at the current time. |
| 236 | * |
| 237 | * @param packageName the app whose job state is being described |
| 238 | * @param userId which user the app is associated with |
| 239 | * @param numDeferredJobs the number of pending jobs that were deferred |
| 240 | * due to bucketing policy |
| 241 | * @param timeSinceLastJobRun number of milliseconds since the last time one of |
| 242 | * this app's jobs was executed |
| 243 | */ |
| 244 | public abstract void reportAppJobState(String packageName, @UserIdInt int userId, |
| 245 | int numDeferredJobs, long timeSinceLastJobRun); |
Adam Lesinski | 0debc9a | 2014-07-16 19:09:13 -0700 | [diff] [blame] | 246 | } |