blob: 6641b5be651d6aa10bdc3e2137c01b3155d46304 [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;
Varun Shah3a315202019-07-24 16:33:42 -070024import android.os.UserManager;
Adam Lesinski0debc9a2014-07-16 19:09:13 -070025
Christopher Tateb909c4d52019-10-21 12:50:37 -070026import com.android.server.usage.AppStandbyInternal.AppIdleStateChangeListener;
27
Daniel Nishicf9d19e2017-01-23 14:33:42 -080028import java.util.List;
Sudheer Shanka101c3532018-01-08 16:28:42 -080029import java.util.Set;
Ritesh Reddy8a6ce2c2015-12-17 17:03:54 +000030
Adam Lesinski0debc9a2014-07-16 19:09:13 -070031/**
32 * UsageStatsManager local system service interface.
33 *
34 * {@hide} Only for use within the system server.
35 */
36public abstract class UsageStatsManagerInternal {
37
38 /**
Varun Shah3a315202019-07-24 16:33:42 -070039 * Reports an event to the UsageStatsManager. <br/>
40 * <em>Note: Starting from {@link android.os.Build.VERSION_CODES#R Android R}, if the user's
41 * device is not in an unlocked state (as defined by {@link UserManager#isUserUnlocked()}),
42 * then this event will be added to a queue and processed once the device is unlocked.</em>
Adam Lesinski0debc9a2014-07-16 19:09:13 -070043 *
Adam Lesinski7f61e962014-09-02 16:43:52 -070044 * @param component The component for which this event occurred.
Adam Lesinski3c153512014-07-23 17:34:34 -070045 * @param userId The user id to which the component belongs to.
Adam Lesinski7f61e962014-09-02 16:43:52 -070046 * @param eventType The event that occurred. Valid values can be found at
Hui Yu03d12402018-12-06 18:00:37 -080047 * {@link UsageEvents}
48 * @param instanceId For activity, hashCode of ActivityRecord's appToken.
49 * For non-activity, it is not used.
Michael Wachenschwanz0b4ab1f2019-01-07 13:59:10 -080050 * @param taskRoot For activity, the name of the package at the root of the task
51 * For non-activity, it is not used.
Adam Lesinski0debc9a2014-07-16 19:09:13 -070052 */
Hui Yu03d12402018-12-06 18:00:37 -080053 public abstract void reportEvent(ComponentName component, @UserIdInt int userId, int eventType,
Michael Wachenschwanz0b4ab1f2019-01-07 13:59:10 -080054 int instanceId, ComponentName taskRoot);
Adam Lesinski7f61e962014-09-02 16:43:52 -070055
56 /**
Varun Shah3a315202019-07-24 16:33:42 -070057 * Reports an event to the UsageStatsManager. <br/>
58 * <em>Note: Starting from {@link android.os.Build.VERSION_CODES#R Android R}, if the user's
59 * device is not in an unlocked state (as defined by {@link UserManager#isUserUnlocked()}),
60 * then this event will be added to a queue and processed once the device is unlocked.</em>
Adam Lesinski978a1ed2015-03-02 11:37:24 -080061 *
62 * @param packageName The package for which this event occurred.
63 * @param userId The user id to which the component belongs to.
64 * @param eventType The event that occurred. Valid values can be found at
65 * {@link UsageEvents}
66 */
Amith Yamasani53f06ea2018-01-05 17:53:46 -080067 public abstract void reportEvent(String packageName, @UserIdInt int userId, int eventType);
Adam Lesinski978a1ed2015-03-02 11:37:24 -080068
69 /**
Varun Shah3a315202019-07-24 16:33:42 -070070 * Reports a configuration change to the UsageStatsManager. <br/>
71 * <em>Note: Starting from {@link android.os.Build.VERSION_CODES#R Android R}, if the user's
72 * device is not in an unlocked state (as defined by {@link UserManager#isUserUnlocked()}),
73 * then this event will be added to a queue and processed once the device is unlocked.</em>
Adam Lesinski7f61e962014-09-02 16:43:52 -070074 *
75 * @param config The new device configuration.
76 */
Amith Yamasani53f06ea2018-01-05 17:53:46 -080077 public abstract void reportConfigurationChange(Configuration config, @UserIdInt int userId);
Adam Lesinski0debc9a2014-07-16 19:09:13 -070078
79 /**
Varun Shah3a315202019-07-24 16:33:42 -070080 * Reports that an application has posted an interruptive notification. <br/>
81 * <em>Note: Starting from {@link android.os.Build.VERSION_CODES#R Android R}, if the user's
82 * device is not in an unlocked state (as defined by {@link UserManager#isUserUnlocked()}),
83 * then this event will be added to a queue and processed once the device is unlocked.</em>
Julia Reynolds1fac86e2018-03-07 08:30:37 -050084 *
85 * @param packageName The package name of the app that posted the notification
86 * @param channelId The ID of the NotificationChannel to which the notification was posted
87 * @param userId The user in which the notification was posted
88 */
89 public abstract void reportInterruptiveNotification(String packageName, String channelId,
90 @UserIdInt int userId);
91
92 /**
Varun Shah3a315202019-07-24 16:33:42 -070093 * Reports that an action equivalent to a ShortcutInfo is taken by the user. <br/>
94 * <em>Note: Starting from {@link android.os.Build.VERSION_CODES#R Android R}, if the user's
95 * device is not in an unlocked state (as defined by {@link UserManager#isUserUnlocked()}),
96 * then this event will be added to a queue and processed once the device is unlocked.</em>
Makoto Onukiac042502016-05-20 16:39:42 -070097 *
98 * @param packageName The package name of the shortcut publisher
99 * @param shortcutId The ID of the shortcut in question
100 * @param userId The user in which the content provider was accessed.
101 *
102 * @see android.content.pm.ShortcutManager#reportShortcutUsed(String)
103 */
Amith Yamasani53f06ea2018-01-05 17:53:46 -0800104 public abstract void reportShortcutUsage(String packageName, String shortcutId,
105 @UserIdInt int userId);
Makoto Onukiac042502016-05-20 16:39:42 -0700106
107 /**
Amith Yamasani37a40c22015-06-17 13:25:42 -0700108 * Reports that a content provider has been accessed by a foreground app.
109 * @param name The authority of the content provider
110 * @param pkgName The package name of the content provider
111 * @param userId The user in which the content provider was accessed.
112 */
Amith Yamasani53f06ea2018-01-05 17:53:46 -0800113 public abstract void reportContentProviderUsage(String name, String pkgName,
114 @UserIdInt int userId);
Amith Yamasani37a40c22015-06-17 13:25:42 -0700115
116 /**
Adam Lesinski0debc9a2014-07-16 19:09:13 -0700117 * Prepares the UsageStatsService for shutdown.
118 */
119 public abstract void prepareShutdown();
Amith Yamasanib0ff3222015-03-04 09:56:14 -0800120
121 /**
Hui Yub1d243a2018-12-13 12:02:00 -0800122 * When the device power button is long pressed for 3.5 seconds, prepareForPossibleShutdown()
123 * is called.
124 */
125 public abstract void prepareForPossibleShutdown();
126
127 /**
Amith Yamasanib0ff3222015-03-04 09:56:14 -0800128 * Returns true if the app has not been used for a certain amount of time. How much time?
129 * Could be hours, could be days, who knows?
130 *
131 * @param packageName
Dianne Hackbornfcc95a62015-11-02 13:43:29 -0800132 * @param uidForAppId The uid of the app, which will be used for its app id
Amith Yamasanib0ff3222015-03-04 09:56:14 -0800133 * @param userId
134 * @return
135 */
Amith Yamasani53f06ea2018-01-05 17:53:46 -0800136 public abstract boolean isAppIdle(String packageName, int uidForAppId, @UserIdInt int userId);
Amith Yamasanib0ff3222015-03-04 09:56:14 -0800137
138 /**
Christopher Tatea732f012017-10-26 17:26:53 -0700139 * Returns the app standby bucket that the app is currently in. This accessor does
140 * <em>not</em> obfuscate instant apps.
141 *
142 * @param packageName
143 * @param userId
144 * @param nowElapsed The current time, in the elapsedRealtime time base
145 * @return the AppStandby bucket code the app currently resides in. If the app is
146 * unknown in the given user, STANDBY_BUCKET_NEVER is returned.
147 */
Amith Yamasani53f06ea2018-01-05 17:53:46 -0800148 @StandbyBuckets public abstract int getAppStandbyBucket(String packageName,
149 @UserIdInt int userId, long nowElapsed);
Christopher Tatea732f012017-10-26 17:26:53 -0700150
151 /**
Dianne Hackborn4a503b12015-08-06 22:19:06 -0700152 * Returns all of the uids for a given user where all packages associating with that uid
153 * are in the app idle state -- there are no associated apps that are not idle. This means
154 * all of the returned uids can be safely considered app idle.
155 */
Amith Yamasani53f06ea2018-01-05 17:53:46 -0800156 public abstract int[] getIdleUidsForUser(@UserIdInt int userId);
Dianne Hackborn4a503b12015-08-06 22:19:06 -0700157
Amith Yamasani53f06ea2018-01-05 17:53:46 -0800158 /** Backup/Restore API */
159 public abstract byte[] getBackupPayload(@UserIdInt int userId, String key);
Ritesh Reddy8a6ce2c2015-12-17 17:03:54 +0000160
Amith Yamasani53f06ea2018-01-05 17:53:46 -0800161 /**
162 * ?
163 * @param userId
164 * @param key
165 * @param payload
166 */
167 public abstract void applyRestoredPayload(@UserIdInt int userId, String key, byte[] payload);
Ritesh Reddy8a6ce2c2015-12-17 17:03:54 +0000168
Makoto Onukiad623012017-05-15 09:29:34 -0700169 /**
Sudheer Shanka101c3532018-01-08 16:28:42 -0800170 * Called by DevicePolicyManagerService to inform that a new admin has been added.
171 *
172 * @param packageName the package in which the admin component is part of.
173 * @param userId the userId in which the admin has been added.
174 */
175 public abstract void onActiveAdminAdded(String packageName, int userId);
176
177 /**
178 * Called by DevicePolicyManagerService to inform about the active admins in an user.
179 *
180 * @param adminApps the set of active admins in {@param userId} or null if there are none.
181 * @param userId the userId to which the admin apps belong.
182 */
183 public abstract void setActiveAdminApps(Set<String> adminApps, int userId);
184
185 /**
Sudheer Shankac53c47f2018-01-16 12:01:00 -0800186 * Called by DevicePolicyManagerService during boot to inform that admin data is loaded and
187 * pushed to UsageStatsService.
188 */
189 public abstract void onAdminDataAvailable();
190
191 /**
Makoto Onukiad623012017-05-15 09:29:34 -0700192 * Return usage stats.
193 *
194 * @param obfuscateInstantApps whether instant app package names need to be obfuscated in the
195 * result.
196 */
Amith Yamasani53f06ea2018-01-05 17:53:46 -0800197 public abstract List<UsageStats> queryUsageStatsForUser(@UserIdInt int userId, int interval,
198 long beginTime, long endTime, boolean obfuscateInstantApps);
199
200 /**
201 * Used to persist the last time a job was run for this app, in order to make decisions later
202 * whether a job should be deferred until later. The time passed in should be in elapsed
203 * realtime since boot.
204 * @param packageName the app that executed a job.
205 * @param userId the user associated with the job.
206 * @param elapsedRealtime the time when the job was executed, in elapsed realtime millis since
207 * boot.
208 */
209 public abstract void setLastJobRunTime(String packageName, @UserIdInt int userId,
210 long elapsedRealtime);
211
212 /**
213 * Returns the time in millis since a job was executed for this app, in elapsed realtime
214 * timebase. This value can be larger than the current elapsed realtime if the job was executed
215 * before the device was rebooted. The default value is {@link Long#MAX_VALUE}.
216 * @param packageName the app you're asking about.
217 * @param userId the user associated with the job.
218 * @return the time in millis since a job was last executed for the app, provided it was
219 * indicated here before by a call to {@link #setLastJobRunTime(String, int, long)}.
220 */
221 public abstract long getTimeSinceLastJobRun(String packageName, @UserIdInt int userId);
Christopher Tated117b292018-01-05 17:32:36 -0800222
223 /**
224 * Report a few data points about an app's job state at the current time.
225 *
226 * @param packageName the app whose job state is being described
227 * @param userId which user the app is associated with
228 * @param numDeferredJobs the number of pending jobs that were deferred
229 * due to bucketing policy
230 * @param timeSinceLastJobRun number of milliseconds since the last time one of
231 * this app's jobs was executed
232 */
233 public abstract void reportAppJobState(String packageName, @UserIdInt int userId,
234 int numDeferredJobs, long timeSinceLastJobRun);
Makoto Onuki75ad2492018-03-28 14:42:42 -0700235
236 /**
Michael Wachenschwanzc3295202019-02-20 17:19:52 -0800237 * Report a sync that was scheduled.
Makoto Onukid5f25d22018-05-22 16:02:17 -0700238 *
239 * @param packageName name of the package that owns the sync adapter.
240 * @param userId which user the app is associated with
Michael Wachenschwanzc3295202019-02-20 17:19:52 -0800241 * @param exempted is sync app standby exempted
Makoto Onukid5f25d22018-05-22 16:02:17 -0700242 */
Michael Wachenschwanzc3295202019-02-20 17:19:52 -0800243 public abstract void reportSyncScheduled(String packageName, @UserIdInt int userId,
244 boolean exempted);
Makoto Onukid5f25d22018-05-22 16:02:17 -0700245
246 /**
247 * Report a sync that was scheduled by a foreground app is about to be executed.
Makoto Onuki75ad2492018-03-28 14:42:42 -0700248 *
249 * @param packageName name of the package that owns the sync adapter.
250 * @param userId which user the app is associated with
251 */
252 public abstract void reportExemptedSyncStart(String packageName, @UserIdInt int userId);
Varun Shah2546cef2019-01-11 15:50:54 -0800253
254 /**
255 * Returns an object describing the app usage limit for the given package which was set via
256 * {@link UsageStatsManager#registerAppUsageLimitObserver}.
257 * If there are multiple limits that apply to the package, the one with the smallest
258 * time remaining will be returned.
259 *
260 * @param packageName name of the package whose app usage limit will be returned
261 * @param user the user associated with the limit
262 * @return an {@link AppUsageLimitData} object describing the app time limit containing
263 * the given package, with the smallest time remaining.
264 */
265 public abstract AppUsageLimitData getAppUsageLimit(String packageName, UserHandle user);
266
267 /** A class which is used to share the usage limit data for an app or a group of apps. */
268 public static class AppUsageLimitData {
Varun Shah2546cef2019-01-11 15:50:54 -0800269 private final long mTotalUsageLimit;
270 private final long mUsageRemaining;
271
Varun Shah9c6f72b2019-01-25 21:13:56 -0800272 public AppUsageLimitData(long totalUsageLimit, long usageRemaining) {
Varun Shah2546cef2019-01-11 15:50:54 -0800273 this.mTotalUsageLimit = totalUsageLimit;
274 this.mUsageRemaining = usageRemaining;
275 }
276
Varun Shah2546cef2019-01-11 15:50:54 -0800277 public long getTotalUsageLimit() {
278 return mTotalUsageLimit;
279 }
280 public long getUsageRemaining() {
281 return mUsageRemaining;
282 }
283 }
Adam Lesinski0debc9a2014-07-16 19:09:13 -0700284}