blob: 43ce521fa9f35d1e1a09578c27d23e91f70174a2 [file] [log] [blame]
Adam Lesinski0debc9a2014-07-16 19:09:13 -07001/**
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
17package android.app.usage;
18
Amith Yamasani53f06ea2018-01-05 17:53:46 -080019import android.annotation.UserIdInt;
Amith Yamasaniafbccb72017-11-27 10:44:24 -080020import android.app.usage.UsageStatsManager.StandbyBuckets;
Adam Lesinski0debc9a2014-07-16 19:09:13 -070021import android.content.ComponentName;
Adam Lesinski7f61e962014-09-02 16:43:52 -070022import android.content.res.Configuration;
Varun Shah2546cef2019-01-11 15:50:54 -080023import android.os.UserHandle;
Adam Lesinski0debc9a2014-07-16 19:09:13 -070024
Daniel Nishicf9d19e2017-01-23 14:33:42 -080025import java.util.List;
Sudheer Shanka101c3532018-01-08 16:28:42 -080026import java.util.Set;
Ritesh Reddy8a6ce2c2015-12-17 17:03:54 +000027
Adam Lesinski0debc9a2014-07-16 19:09:13 -070028/**
29 * UsageStatsManager local system service interface.
30 *
31 * {@hide} Only for use within the system server.
32 */
33public abstract class UsageStatsManagerInternal {
34
35 /**
36 * Reports an event to the UsageStatsManager.
37 *
Adam Lesinski7f61e962014-09-02 16:43:52 -070038 * @param component The component for which this event occurred.
Adam Lesinski3c153512014-07-23 17:34:34 -070039 * @param userId The user id to which the component belongs to.
Adam Lesinski7f61e962014-09-02 16:43:52 -070040 * @param eventType The event that occurred. Valid values can be found at
Hui Yu03d12402018-12-06 18:00:37 -080041 * {@link UsageEvents}
42 * @param instanceId For activity, hashCode of ActivityRecord's appToken.
43 * For non-activity, it is not used.
Michael Wachenschwanz0b4ab1f2019-01-07 13:59:10 -080044 * @param taskRoot For activity, the name of the package at the root of the task
45 * For non-activity, it is not used.
Adam Lesinski0debc9a2014-07-16 19:09:13 -070046 */
Hui Yu03d12402018-12-06 18:00:37 -080047 public abstract void reportEvent(ComponentName component, @UserIdInt int userId, int eventType,
Michael Wachenschwanz0b4ab1f2019-01-07 13:59:10 -080048 int instanceId, ComponentName taskRoot);
Adam Lesinski7f61e962014-09-02 16:43:52 -070049
50 /**
Adam Lesinski978a1ed2015-03-02 11:37:24 -080051 * Reports an event to the UsageStatsManager.
52 *
53 * @param packageName The package for which this event occurred.
54 * @param userId The user id to which the component belongs to.
55 * @param eventType The event that occurred. Valid values can be found at
56 * {@link UsageEvents}
57 */
Amith Yamasani53f06ea2018-01-05 17:53:46 -080058 public abstract void reportEvent(String packageName, @UserIdInt int userId, int eventType);
Adam Lesinski978a1ed2015-03-02 11:37:24 -080059
60 /**
Adam Lesinski7f61e962014-09-02 16:43:52 -070061 * Reports a configuration change to the UsageStatsManager.
62 *
63 * @param config The new device configuration.
64 */
Amith Yamasani53f06ea2018-01-05 17:53:46 -080065 public abstract void reportConfigurationChange(Configuration config, @UserIdInt int userId);
Adam Lesinski0debc9a2014-07-16 19:09:13 -070066
67 /**
Julia Reynolds1fac86e2018-03-07 08:30:37 -050068 * Reports that an application has posted an interruptive notification.
69 *
70 * @param packageName The package name of the app that posted the notification
71 * @param channelId The ID of the NotificationChannel to which the notification was posted
72 * @param userId The user in which the notification was posted
73 */
74 public abstract void reportInterruptiveNotification(String packageName, String channelId,
75 @UserIdInt int userId);
76
77 /**
Makoto Onukiac042502016-05-20 16:39:42 -070078 * Reports that an action equivalent to a ShortcutInfo is taken by the user.
79 *
80 * @param packageName The package name of the shortcut publisher
81 * @param shortcutId The ID of the shortcut in question
82 * @param userId The user in which the content provider was accessed.
83 *
84 * @see android.content.pm.ShortcutManager#reportShortcutUsed(String)
85 */
Amith Yamasani53f06ea2018-01-05 17:53:46 -080086 public abstract void reportShortcutUsage(String packageName, String shortcutId,
87 @UserIdInt int userId);
Makoto Onukiac042502016-05-20 16:39:42 -070088
89 /**
Amith Yamasani37a40c22015-06-17 13:25:42 -070090 * Reports that a content provider has been accessed by a foreground app.
91 * @param name The authority of the content provider
92 * @param pkgName The package name of the content provider
93 * @param userId The user in which the content provider was accessed.
94 */
Amith Yamasani53f06ea2018-01-05 17:53:46 -080095 public abstract void reportContentProviderUsage(String name, String pkgName,
96 @UserIdInt int userId);
Amith Yamasani37a40c22015-06-17 13:25:42 -070097
98 /**
Adam Lesinski0debc9a2014-07-16 19:09:13 -070099 * Prepares the UsageStatsService for shutdown.
100 */
101 public abstract void prepareShutdown();
Amith Yamasanib0ff3222015-03-04 09:56:14 -0800102
103 /**
Hui Yub1d243a2018-12-13 12:02:00 -0800104 * When the device power button is long pressed for 3.5 seconds, prepareForPossibleShutdown()
105 * is called.
106 */
107 public abstract void prepareForPossibleShutdown();
108
109 /**
Amith Yamasanib0ff3222015-03-04 09:56:14 -0800110 * Returns true if the app has not been used for a certain amount of time. How much time?
111 * Could be hours, could be days, who knows?
112 *
113 * @param packageName
Dianne Hackbornfcc95a62015-11-02 13:43:29 -0800114 * @param uidForAppId The uid of the app, which will be used for its app id
Amith Yamasanib0ff3222015-03-04 09:56:14 -0800115 * @param userId
116 * @return
117 */
Amith Yamasani53f06ea2018-01-05 17:53:46 -0800118 public abstract boolean isAppIdle(String packageName, int uidForAppId, @UserIdInt int userId);
Amith Yamasanib0ff3222015-03-04 09:56:14 -0800119
120 /**
Christopher Tatea732f012017-10-26 17:26:53 -0700121 * Returns the app standby bucket that the app is currently in. This accessor does
122 * <em>not</em> obfuscate instant apps.
123 *
124 * @param packageName
125 * @param userId
126 * @param nowElapsed The current time, in the elapsedRealtime time base
127 * @return the AppStandby bucket code the app currently resides in. If the app is
128 * unknown in the given user, STANDBY_BUCKET_NEVER is returned.
129 */
Amith Yamasani53f06ea2018-01-05 17:53:46 -0800130 @StandbyBuckets public abstract int getAppStandbyBucket(String packageName,
131 @UserIdInt int userId, long nowElapsed);
Christopher Tatea732f012017-10-26 17:26:53 -0700132
133 /**
Dianne Hackborn4a503b12015-08-06 22:19:06 -0700134 * Returns all of the uids for a given user where all packages associating with that uid
135 * are in the app idle state -- there are no associated apps that are not idle. This means
136 * all of the returned uids can be safely considered app idle.
137 */
Amith Yamasani53f06ea2018-01-05 17:53:46 -0800138 public abstract int[] getIdleUidsForUser(@UserIdInt int userId);
Dianne Hackborn4a503b12015-08-06 22:19:06 -0700139
140 /**
Xiaohui Chen8dca36d2015-06-19 12:44:59 -0700141 * @return True if currently app idle parole mode is on. This means all idle apps are allow to
142 * run for a short period of time.
143 */
144 public abstract boolean isAppIdleParoleOn();
145
146 /**
Amith Yamasanib0ff3222015-03-04 09:56:14 -0800147 * Sets up a listener for changes to packages being accessed.
148 * @param listener A listener within the system process.
149 */
150 public abstract void addAppIdleStateChangeListener(
151 AppIdleStateChangeListener listener);
152
153 /**
154 * Removes a listener that was previously added for package usage state changes.
155 * @param listener The listener within the system process to remove.
156 */
157 public abstract void removeAppIdleStateChangeListener(
158 AppIdleStateChangeListener listener);
159
Xiaohui Chen8dca36d2015-06-19 12:44:59 -0700160 public static abstract class AppIdleStateChangeListener {
Amith Yamasani84cd7b72017-11-07 13:59:37 -0800161
162 /** Callback to inform listeners that the idle state has changed to a new bucket. */
Amith Yamasani53f06ea2018-01-05 17:53:46 -0800163 public abstract void onAppIdleStateChanged(String packageName, @UserIdInt int userId,
Amith Yamasani119be9a2018-02-18 22:23:00 -0800164 boolean idle, int bucket, int reason);
Amith Yamasani84cd7b72017-11-07 13:59:37 -0800165
166 /**
167 * Callback to inform listeners that the parole state has changed. This means apps are
168 * allowed to do work even if they're idle or in a low bucket.
169 */
Xiaohui Chen8dca36d2015-06-19 12:44:59 -0700170 public abstract void onParoleStateChanged(boolean isParoleOn);
Christopher Tated117b292018-01-05 17:32:36 -0800171
172 /**
173 * Optional callback to inform the listener that the app has transitioned into
174 * an active state due to user interaction.
175 */
176 public void onUserInteractionStarted(String packageName, @UserIdInt int userId) {
177 // No-op by default
178 }
Amith Yamasanib0ff3222015-03-04 09:56:14 -0800179 }
180
Amith Yamasani53f06ea2018-01-05 17:53:46 -0800181 /** Backup/Restore API */
182 public abstract byte[] getBackupPayload(@UserIdInt int userId, String key);
Ritesh Reddy8a6ce2c2015-12-17 17:03:54 +0000183
Amith Yamasani53f06ea2018-01-05 17:53:46 -0800184 /**
185 * ?
186 * @param userId
187 * @param key
188 * @param payload
189 */
190 public abstract void applyRestoredPayload(@UserIdInt int userId, String key, byte[] payload);
Ritesh Reddy8a6ce2c2015-12-17 17:03:54 +0000191
Makoto Onukiad623012017-05-15 09:29:34 -0700192 /**
Sudheer Shanka101c3532018-01-08 16:28:42 -0800193 * Called by DevicePolicyManagerService to inform that a new admin has been added.
194 *
195 * @param packageName the package in which the admin component is part of.
196 * @param userId the userId in which the admin has been added.
197 */
198 public abstract void onActiveAdminAdded(String packageName, int userId);
199
200 /**
201 * Called by DevicePolicyManagerService to inform about the active admins in an user.
202 *
203 * @param adminApps the set of active admins in {@param userId} or null if there are none.
204 * @param userId the userId to which the admin apps belong.
205 */
206 public abstract void setActiveAdminApps(Set<String> adminApps, int userId);
207
208 /**
Sudheer Shankac53c47f2018-01-16 12:01:00 -0800209 * Called by DevicePolicyManagerService during boot to inform that admin data is loaded and
210 * pushed to UsageStatsService.
211 */
212 public abstract void onAdminDataAvailable();
213
214 /**
Makoto Onukiad623012017-05-15 09:29:34 -0700215 * Return usage stats.
216 *
217 * @param obfuscateInstantApps whether instant app package names need to be obfuscated in the
218 * result.
219 */
Amith Yamasani53f06ea2018-01-05 17:53:46 -0800220 public abstract List<UsageStats> queryUsageStatsForUser(@UserIdInt int userId, int interval,
221 long beginTime, long endTime, boolean obfuscateInstantApps);
222
223 /**
224 * Used to persist the last time a job was run for this app, in order to make decisions later
225 * whether a job should be deferred until later. The time passed in should be in elapsed
226 * realtime since boot.
227 * @param packageName the app that executed a job.
228 * @param userId the user associated with the job.
229 * @param elapsedRealtime the time when the job was executed, in elapsed realtime millis since
230 * boot.
231 */
232 public abstract void setLastJobRunTime(String packageName, @UserIdInt int userId,
233 long elapsedRealtime);
234
235 /**
236 * Returns the time in millis since a job was executed for this app, in elapsed realtime
237 * timebase. This value can be larger than the current elapsed realtime if the job was executed
238 * before the device was rebooted. The default value is {@link Long#MAX_VALUE}.
239 * @param packageName the app you're asking about.
240 * @param userId the user associated with the job.
241 * @return the time in millis since a job was last executed for the app, provided it was
242 * indicated here before by a call to {@link #setLastJobRunTime(String, int, long)}.
243 */
244 public abstract long getTimeSinceLastJobRun(String packageName, @UserIdInt int userId);
Christopher Tated117b292018-01-05 17:32:36 -0800245
246 /**
247 * Report a few data points about an app's job state at the current time.
248 *
249 * @param packageName the app whose job state is being described
250 * @param userId which user the app is associated with
251 * @param numDeferredJobs the number of pending jobs that were deferred
252 * due to bucketing policy
253 * @param timeSinceLastJobRun number of milliseconds since the last time one of
254 * this app's jobs was executed
255 */
256 public abstract void reportAppJobState(String packageName, @UserIdInt int userId,
257 int numDeferredJobs, long timeSinceLastJobRun);
Makoto Onuki75ad2492018-03-28 14:42:42 -0700258
259 /**
Makoto Onukid5f25d22018-05-22 16:02:17 -0700260 * Report a sync is scheduled by a foreground app.
261 *
262 * @param packageName name of the package that owns the sync adapter.
263 * @param userId which user the app is associated with
264 */
265 public abstract void reportExemptedSyncScheduled(String packageName, @UserIdInt int userId);
266
267 /**
268 * Report a sync that was scheduled by a foreground app is about to be executed.
Makoto Onuki75ad2492018-03-28 14:42:42 -0700269 *
270 * @param packageName name of the package that owns the sync adapter.
271 * @param userId which user the app is associated with
272 */
273 public abstract void reportExemptedSyncStart(String packageName, @UserIdInt int userId);
Varun Shah2546cef2019-01-11 15:50:54 -0800274
275 /**
276 * Returns an object describing the app usage limit for the given package which was set via
277 * {@link UsageStatsManager#registerAppUsageLimitObserver}.
278 * If there are multiple limits that apply to the package, the one with the smallest
279 * time remaining will be returned.
280 *
281 * @param packageName name of the package whose app usage limit will be returned
282 * @param user the user associated with the limit
283 * @return an {@link AppUsageLimitData} object describing the app time limit containing
284 * the given package, with the smallest time remaining.
285 */
286 public abstract AppUsageLimitData getAppUsageLimit(String packageName, UserHandle user);
287
288 /** A class which is used to share the usage limit data for an app or a group of apps. */
289 public static class AppUsageLimitData {
Varun Shah2546cef2019-01-11 15:50:54 -0800290 private final long mTotalUsageLimit;
291 private final long mUsageRemaining;
292
Varun Shah9c6f72b2019-01-25 21:13:56 -0800293 public AppUsageLimitData(long totalUsageLimit, long usageRemaining) {
Varun Shah2546cef2019-01-11 15:50:54 -0800294 this.mTotalUsageLimit = totalUsageLimit;
295 this.mUsageRemaining = usageRemaining;
296 }
297
Varun Shah2546cef2019-01-11 15:50:54 -0800298 public long getTotalUsageLimit() {
299 return mTotalUsageLimit;
300 }
301 public long getUsageRemaining() {
302 return mUsageRemaining;
303 }
304 }
Adam Lesinski0debc9a2014-07-16 19:09:13 -0700305}