blob: fafea340ccc342ffcbf6b8b02c3566a0f6517119 [file] [log] [blame]
Adam Lesinski35168002014-07-21 15:25:30 -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 */
16package android.app.usage;
17
Makoto Onukiad623012017-05-15 09:29:34 -070018import android.annotation.IntDef;
Michael Wachenschwanz0b4ab1f2019-01-07 13:59:10 -080019import android.annotation.Nullable;
Amith Yamasani7ec89412018-02-07 08:48:49 -080020import android.annotation.SystemApi;
Mathew Inwood61e8ae62018-08-14 14:17:44 +010021import android.annotation.UnsupportedAppUsage;
Adam Lesinski7f61e962014-09-02 16:43:52 -070022import android.content.res.Configuration;
Mathew Inwood31755f92018-12-20 13:53:36 +000023import android.os.Build;
Adam Lesinski35168002014-07-21 15:25:30 -070024import android.os.Parcel;
25import android.os.Parcelable;
26
Makoto Onukiad623012017-05-15 09:29:34 -070027import java.lang.annotation.Retention;
28import java.lang.annotation.RetentionPolicy;
Adam Lesinski35168002014-07-21 15:25:30 -070029import java.util.Arrays;
30import java.util.List;
31
32/**
33 * A result returned from {@link android.app.usage.UsageStatsManager#queryEvents(long, long)}
34 * from which to read {@link android.app.usage.UsageEvents.Event} objects.
35 */
36public final class UsageEvents implements Parcelable {
37
Makoto Onukiad623012017-05-15 09:29:34 -070038 /** @hide */
39 public static final String INSTANT_APP_PACKAGE_NAME = "android.instant_app";
40
41 /** @hide */
42 public static final String INSTANT_APP_CLASS_NAME = "android.instant_class";
43
Adam Lesinski35168002014-07-21 15:25:30 -070044 /**
45 * An event representing a state change for a component.
46 */
47 public static final class Event {
48
49 /**
50 * No event type.
51 */
52 public static final int NONE = 0;
53
54 /**
Hui Yue497ad22019-01-25 15:54:57 -080055 * A device level event like {@link #DEVICE_SHUTDOWN} does not have package name, but some
56 * user code always expect a non-null {@link #mPackage} for every event. Use
57 * {@link #DEVICE_EVENT_PACKAGE_NAME} as packageName for these device level events.
58 * @hide
59 */
60 public static final String DEVICE_EVENT_PACKAGE_NAME = "android";
61
62 /**
Hui Yu03d12402018-12-06 18:00:37 -080063 * @deprecated by {@link #ACTIVITY_RESUMED}
64 */
65 @Deprecated
66 public static final int MOVE_TO_FOREGROUND = 1;
67
68 /**
Hui Yue361a232018-10-04 15:05:21 -070069 * An event type denoting that an {@link android.app.Activity} moved to the foreground.
70 * This event has a package name and class name associated with it and can be retrieved
71 * using {@link #getPackageName()} and {@link #getClassName()}.
72 * If a package has multiple activities, this event is reported for each activity that moves
73 * to foreground.
Hui Yu03d12402018-12-06 18:00:37 -080074 * This event is corresponding to {@link android.app.Activity#onResume()} of the
75 * activity's lifecycle.
Adam Lesinski35168002014-07-21 15:25:30 -070076 */
Hui Yu03d12402018-12-06 18:00:37 -080077 public static final int ACTIVITY_RESUMED = MOVE_TO_FOREGROUND;
78
79 /**
80 * @deprecated by {@link #ACTIVITY_PAUSED}
81 */
82 @Deprecated
83 public static final int MOVE_TO_BACKGROUND = 2;
Adam Lesinski35168002014-07-21 15:25:30 -070084
85 /**
Hui Yue361a232018-10-04 15:05:21 -070086 * An event type denoting that an {@link android.app.Activity} moved to the background.
87 * This event has a package name and class name associated with it and can be retrieved
88 * using {@link #getPackageName()} and {@link #getClassName()}.
89 * If a package has multiple activities, this event is reported for each activity that moves
90 * to background.
Hui Yu03d12402018-12-06 18:00:37 -080091 * This event is corresponding to {@link android.app.Activity#onPause()} of the activity's
92 * lifecycle.
Adam Lesinski35168002014-07-21 15:25:30 -070093 */
Hui Yu03d12402018-12-06 18:00:37 -080094 public static final int ACTIVITY_PAUSED = MOVE_TO_BACKGROUND;
Adam Lesinski35168002014-07-21 15:25:30 -070095
96 /**
97 * An event type denoting that a component was in the foreground when the stats
Hui Yu03d12402018-12-06 18:00:37 -080098 * rolled-over. This is effectively treated as a {@link #ACTIVITY_PAUSED}.
Adam Lesinski35168002014-07-21 15:25:30 -070099 * {@hide}
100 */
101 public static final int END_OF_DAY = 3;
102
103 /**
104 * An event type denoting that a component was in the foreground the previous day.
Hui Yu03d12402018-12-06 18:00:37 -0800105 * This is effectively treated as a {@link #ACTIVITY_RESUMED}.
Adam Lesinski35168002014-07-21 15:25:30 -0700106 * {@hide}
107 */
108 public static final int CONTINUE_PREVIOUS_DAY = 4;
109
110 /**
Adam Lesinski7f61e962014-09-02 16:43:52 -0700111 * An event type denoting that the device configuration has changed.
112 */
113 public static final int CONFIGURATION_CHANGE = 5;
114
115 /**
Adam Lesinskic8e87292015-06-10 15:33:45 -0700116 * An event type denoting that a package was interacted with in some way by the system.
117 * @hide
Adam Lesinski978a1ed2015-03-02 11:37:24 -0800118 */
Michael Wachenschwanzf0acb022018-03-15 20:17:22 -0700119 @SystemApi
Adam Lesinskic8e87292015-06-10 15:33:45 -0700120 public static final int SYSTEM_INTERACTION = 6;
121
122 /**
123 * An event type denoting that a package was interacted with in some way by the user.
124 */
125 public static final int USER_INTERACTION = 7;
Adam Lesinski978a1ed2015-03-02 11:37:24 -0800126
127 /**
Makoto Onukiac042502016-05-20 16:39:42 -0700128 * An event type denoting that an action equivalent to a ShortcutInfo is taken by the user.
129 *
130 * @see android.content.pm.ShortcutManager#reportShortcutUsed(String)
131 */
132 public static final int SHORTCUT_INVOCATION = 8;
133
134 /**
Kang Li53b43142016-11-14 14:38:25 -0800135 * An event type denoting that a package was selected by the user for ChooserActivity.
136 * @hide
137 */
138 public static final int CHOOSER_ACTION = 9;
139
Amith Yamasani803eab692017-11-09 17:47:04 -0800140 /**
141 * An event type denoting that a notification was viewed by the user.
142 * @hide
143 */
Amith Yamasani7ec89412018-02-07 08:48:49 -0800144 @SystemApi
Amith Yamasani803eab692017-11-09 17:47:04 -0800145 public static final int NOTIFICATION_SEEN = 10;
146
Amith Yamasanibfc4bf52018-01-19 06:55:08 -0800147 /**
Suprabh Shukla4e12de82018-03-08 18:34:15 -0800148 * An event type denoting a change in App Standby Bucket. The new bucket can be
Amith Yamasaniff1575f2018-04-08 22:41:38 -0700149 * retrieved by calling {@link #getAppStandbyBucket()}.
Suprabh Shukla4e12de82018-03-08 18:34:15 -0800150 *
151 * @see UsageStatsManager#getAppStandbyBucket()
Amith Yamasanibfc4bf52018-01-19 06:55:08 -0800152 */
153 public static final int STANDBY_BUCKET_CHANGED = 11;
154
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500155 /**
156 * An event type denoting that an app posted an interruptive notification. Visual and
157 * audible interruptions are included.
158 * @hide
159 */
160 @SystemApi
161 public static final int NOTIFICATION_INTERRUPTION = 12;
162
Jason Monk1918ef72018-03-14 09:20:39 -0400163 /**
164 * A Slice was pinned by the default launcher or the default assistant.
165 * @hide
166 */
167 @SystemApi
168 public static final int SLICE_PINNED_PRIV = 13;
169
170 /**
171 * A Slice was pinned by an app.
172 * @hide
173 */
174 @SystemApi
175 public static final int SLICE_PINNED = 14;
176
Dianne Hackbornced54392018-02-26 13:07:42 -0800177 /**
178 * An event type denoting that the screen has gone in to an interactive state (turned
179 * on for full user interaction, not ambient display or other non-interactive state).
180 */
181 public static final int SCREEN_INTERACTIVE = 15;
182
183 /**
184 * An event type denoting that the screen has gone in to a non-interactive state
185 * (completely turned off or turned on only in a non-interactive state like ambient
186 * display).
187 */
188 public static final int SCREEN_NON_INTERACTIVE = 16;
189
Dianne Hackborn3378aa92018-03-30 17:43:49 -0700190 /**
191 * An event type denoting that the screen's keyguard has been shown, whether or not
192 * the screen is off.
193 */
194 public static final int KEYGUARD_SHOWN = 17;
195
196 /**
197 * An event type denoting that the screen's keyguard has been hidden. This typically
198 * happens when the user unlocks their phone after turning it on.
199 */
200 public static final int KEYGUARD_HIDDEN = 18;
201
Michael Wachenschwanzc8c26362018-09-07 14:59:25 -0700202 /**
Hui Yue361a232018-10-04 15:05:21 -0700203 * An event type denoting start of a foreground service.
204 * This event has a package name and class name associated with it and can be retrieved
205 * using {@link #getPackageName()} and {@link #getClassName()}.
206 * If a package has multiple foreground services, this event is reported for each service
207 * that is started.
208 */
209 public static final int FOREGROUND_SERVICE_START = 19;
210
211 /**
212 * An event type denoting stop of a foreground service.
213 * This event has a package name and class name associated with it and can be retrieved
214 * using {@link #getPackageName()} and {@link #getClassName()}.
215 * If a package has multiple foreground services, this event is reported for each service
216 * that is stopped.
217 */
218 public static final int FOREGROUND_SERVICE_STOP = 20;
219
220 /**
221 * An event type denoting that a foreground service is at started state at beginning of a
222 * time interval.
223 * This is effectively treated as a {@link #FOREGROUND_SERVICE_START}.
224 * {@hide}
225 */
226 public static final int CONTINUING_FOREGROUND_SERVICE = 21;
227
228 /**
229 * An event type denoting that a foreground service is at started state when the stats
230 * rolled-over at the end of a time interval.
231 * {@hide}
232 */
233 public static final int ROLLOVER_FOREGROUND_SERVICE = 22;
234
235 /**
Hui Yu03d12402018-12-06 18:00:37 -0800236 * An activity becomes invisible on the UI, corresponding to
237 * {@link android.app.Activity#onStop()} of the activity's lifecycle.
238 */
239 public static final int ACTIVITY_STOPPED = 23;
240
241 /**
242 * An activity object is destroyed, corresponding to
243 * {@link android.app.Activity#onDestroy()} of the activity's lifecycle.
244 * {@hide}
245 */
246 public static final int ACTIVITY_DESTROYED = 24;
247
248 /**
249 * The event type demoting that a flush of UsageStatsDatabase to file system. Before the
250 * flush all usage stats need to be updated to latest timestamp to make sure the most
251 * up to date stats are persisted.
252 * @hide
253 */
254 public static final int FLUSH_TO_DISK = 25;
255
256 /**
Hui Yub1d243a2018-12-13 12:02:00 -0800257 * An event type denoting that the device underwent a shutdown process.
258 * A DEVICE_SHUTDOWN event should be treated as if all started activities and foreground
259 * services are now stopped and no explicit {@link #ACTIVITY_STOPPED} and
260 * {@link #FOREGROUND_SERVICE_STOP} events will be generated for them.
261 */
262 public static final int DEVICE_SHUTDOWN = 26;
263
264 /**
Michael Wachenschwanzc8c26362018-09-07 14:59:25 -0700265 * Keep in sync with the greatest event type value.
266 * @hide
267 */
Hui Yub1d243a2018-12-13 12:02:00 -0800268 public static final int MAX_EVENT_TYPE = 26;
Michael Wachenschwanzc8c26362018-09-07 14:59:25 -0700269
Makoto Onukiad623012017-05-15 09:29:34 -0700270 /** @hide */
271 public static final int FLAG_IS_PACKAGE_INSTANT_APP = 1 << 0;
272
273 /** @hide */
Jeff Sharkeyce8db992017-12-13 20:05:05 -0700274 @IntDef(flag = true, prefix = { "FLAG_" }, value = {
275 FLAG_IS_PACKAGE_INSTANT_APP,
276 })
Makoto Onukiad623012017-05-15 09:29:34 -0700277 @Retention(RetentionPolicy.SOURCE)
278 public @interface EventFlags {}
279
Kang Li53b43142016-11-14 14:38:25 -0800280 /**
Michael Wachenschwanzc8c26362018-09-07 14:59:25 -0700281 * Bitwise OR all valid flag constants to create this constant.
282 * @hide
283 */
284 public static final int VALID_FLAG_BITS = FLAG_IS_PACKAGE_INSTANT_APP;
285
286 /**
Adam Lesinski35168002014-07-21 15:25:30 -0700287 * {@hide}
288 */
Mathew Inwood31755f92018-12-20 13:53:36 +0000289 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Adam Lesinski9d960752014-08-25 14:48:12 -0700290 public String mPackage;
291
292 /**
293 * {@hide}
294 */
Mathew Inwood31755f92018-12-20 13:53:36 +0000295 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Adam Lesinski9d960752014-08-25 14:48:12 -0700296 public String mClass;
Adam Lesinski35168002014-07-21 15:25:30 -0700297
Hui Yu03d12402018-12-06 18:00:37 -0800298 /**
299 * {@hide}
300 */
301 public int mInstanceId;
302
Adam Lesinski35168002014-07-21 15:25:30 -0700303 /**
304 * {@hide}
305 */
Michael Wachenschwanz0b4ab1f2019-01-07 13:59:10 -0800306 public String mTaskRootPackage;
307
308 /**
309 * {@hide}
310 */
311 public String mTaskRootClass;
312
313 /**
314 * {@hide}
315 */
Mathew Inwood31755f92018-12-20 13:53:36 +0000316 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Adam Lesinski35168002014-07-21 15:25:30 -0700317 public long mTimeStamp;
318
319 /**
320 * {@hide}
321 */
Mathew Inwood31755f92018-12-20 13:53:36 +0000322 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Adam Lesinski35168002014-07-21 15:25:30 -0700323 public int mEventType;
324
325 /**
Adam Lesinski7f61e962014-09-02 16:43:52 -0700326 * Only present for {@link #CONFIGURATION_CHANGE} event types.
327 * {@hide}
328 */
Mathew Inwood31755f92018-12-20 13:53:36 +0000329 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Adam Lesinski7f61e962014-09-02 16:43:52 -0700330 public Configuration mConfiguration;
331
332 /**
Makoto Onukiac042502016-05-20 16:39:42 -0700333 * ID of the shortcut.
334 * Only present for {@link #SHORTCUT_INVOCATION} event types.
335 * {@hide}
336 */
337 public String mShortcutId;
338
339 /**
Kang Li53b43142016-11-14 14:38:25 -0800340 * Action type passed to ChooserActivity
341 * Only present for {@link #CHOOSER_ACTION} event types.
342 * {@hide}
343 */
344 public String mAction;
345
346 /**
347 * Content type passed to ChooserActivity.
348 * Only present for {@link #CHOOSER_ACTION} event types.
349 * {@hide}
350 */
351 public String mContentType;
352
353 /**
354 * Content annotations passed to ChooserActivity.
355 * Only present for {@link #CHOOSER_ACTION} event types.
356 * {@hide}
357 */
358 public String[] mContentAnnotations;
359
Amith Yamasanibfc4bf52018-01-19 06:55:08 -0800360 /**
Amith Yamasani119be9a2018-02-18 22:23:00 -0800361 * The app standby bucket assigned and reason. Bucket is the high order 16 bits, reason
362 * is the low order 16 bits.
Amith Yamasanibfc4bf52018-01-19 06:55:08 -0800363 * Only present for {@link #STANDBY_BUCKET_CHANGED} event types
364 * {@hide}
365 */
Amith Yamasani119be9a2018-02-18 22:23:00 -0800366 public int mBucketAndReason;
Amith Yamasanibfc4bf52018-01-19 06:55:08 -0800367
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500368 /**
369 * The id of the {@link android.app.NotificationChannel} to which an interruptive
370 * notification was posted.
371 * Only present for {@link #NOTIFICATION_INTERRUPTION} event types.
372 * {@hide}
373 */
374 public String mNotificationChannelId;
375
Makoto Onukiad623012017-05-15 09:29:34 -0700376 /** @hide */
377 @EventFlags
378 public int mFlags;
379
380 public Event() {
381 }
382
383 /** @hide */
Hui Yu03d12402018-12-06 18:00:37 -0800384 public Event(int type, long timeStamp) {
385 mEventType = type;
386 mTimeStamp = timeStamp;
387 }
388
389 /** @hide */
Makoto Onukiad623012017-05-15 09:29:34 -0700390 public Event(Event orig) {
391 mPackage = orig.mPackage;
392 mClass = orig.mClass;
Hui Yu03d12402018-12-06 18:00:37 -0800393 mInstanceId = orig.mInstanceId;
Michael Wachenschwanz0b4ab1f2019-01-07 13:59:10 -0800394 mTaskRootPackage = orig.mTaskRootPackage;
395 mTaskRootClass = orig.mTaskRootClass;
Makoto Onukiad623012017-05-15 09:29:34 -0700396 mTimeStamp = orig.mTimeStamp;
397 mEventType = orig.mEventType;
398 mConfiguration = orig.mConfiguration;
399 mShortcutId = orig.mShortcutId;
400 mAction = orig.mAction;
401 mContentType = orig.mContentType;
402 mContentAnnotations = orig.mContentAnnotations;
403 mFlags = orig.mFlags;
Amith Yamasani119be9a2018-02-18 22:23:00 -0800404 mBucketAndReason = orig.mBucketAndReason;
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500405 mNotificationChannelId = orig.mNotificationChannelId;
Makoto Onukiad623012017-05-15 09:29:34 -0700406 }
407
Kang Li53b43142016-11-14 14:38:25 -0800408 /**
Adam Lesinski9d960752014-08-25 14:48:12 -0700409 * The package name of the source of this event.
410 */
411 public String getPackageName() {
412 return mPackage;
413 }
414
415 /**
Dimuthu Gamage88ebac42019-01-14 08:28:13 -0800416 * Indicates whether it is an instant app.
417 * STOPSHIP b/111407095: Add GTS tests for the newly added API method.
418 * @hide
419 */
420 @SystemApi
421 public boolean isInstantApp() {
422 return (mFlags & FLAG_IS_PACKAGE_INSTANT_APP) == FLAG_IS_PACKAGE_INSTANT_APP;
423 }
424
425 /**
Adam Lesinski9d960752014-08-25 14:48:12 -0700426 * The class name of the source of this event. This may be null for
427 * certain events.
428 */
429 public String getClassName() {
430 return mClass;
Adam Lesinski35168002014-07-21 15:25:30 -0700431 }
432
433 /**
Hui Yu03d12402018-12-06 18:00:37 -0800434 * An activity can be instantiated multiple times, this is the unique activity instance ID.
435 * For non-activity class, instance ID is always zero.
436 * @hide
437 */
438 @SystemApi
439 public int getInstanceId() {
440 return mInstanceId;
441 }
442
443 /**
Michael Wachenschwanz0b4ab1f2019-01-07 13:59:10 -0800444 * The package name of the task root when this event was reported.
445 * Or {@code null} for queries from apps without {@link
446 * android.Manifest.permission#PACKAGE_USAGE_STATS}
447 * @hide
448 */
449 @SystemApi
450 public @Nullable String getTaskRootPackageName() {
451 return mTaskRootPackage;
452 }
453
454 /**
455 * The class name of the task root when this event was reported.
456 * Or {@code null} for queries from apps without {@link
457 * android.Manifest.permission#PACKAGE_USAGE_STATS}
458 * @hide
459 */
460 @SystemApi
461 public @Nullable String getTaskRootClassName() {
462 return mTaskRootClass;
463 }
464
465 /**
Adam Lesinskicc562a82014-08-27 11:52:52 -0700466 * The time at which this event occurred, measured in milliseconds since the epoch.
467 * <p/>
468 * See {@link System#currentTimeMillis()}.
Adam Lesinski35168002014-07-21 15:25:30 -0700469 */
470 public long getTimeStamp() {
471 return mTimeStamp;
472 }
473
474 /**
475 * The event type.
Hui Yu03d12402018-12-06 18:00:37 -0800476 * @see #ACTIVITY_PAUSED
477 * @see #ACTIVITY_RESUMED
Suprabh Shukla4e12de82018-03-08 18:34:15 -0800478 * @see #CONFIGURATION_CHANGE
479 * @see #USER_INTERACTION
480 * @see #STANDBY_BUCKET_CHANGED
Hui Yu03d12402018-12-06 18:00:37 -0800481 * @see #FOREGROUND_SERVICE_START
482 * @see #FOREGROUND_SERVICE_STOP
483 * @see #ACTIVITY_STOPPED
Adam Lesinski35168002014-07-21 15:25:30 -0700484 */
485 public int getEventType() {
486 return mEventType;
487 }
Adam Lesinski7f61e962014-09-02 16:43:52 -0700488
489 /**
490 * Returns a {@link Configuration} for this event if the event is of type
491 * {@link #CONFIGURATION_CHANGE}, otherwise it returns null.
492 */
493 public Configuration getConfiguration() {
494 return mConfiguration;
495 }
Makoto Onukiac042502016-05-20 16:39:42 -0700496
497 /**
498 * Returns the ID of a {@link android.content.pm.ShortcutInfo} for this event
499 * if the event is of type {@link #SHORTCUT_INVOCATION}, otherwise it returns null.
500 *
501 * @see android.content.pm.ShortcutManager#reportShortcutUsed(String)
502 */
503 public String getShortcutId() {
504 return mShortcutId;
505 }
Makoto Onukiad623012017-05-15 09:29:34 -0700506
Amith Yamasani7ec89412018-02-07 08:48:49 -0800507 /**
508 * Returns the standby bucket of the app, if the event is of type
509 * {@link #STANDBY_BUCKET_CHANGED}, otherwise returns 0.
510 * @return the standby bucket associated with the event.
Amith Yamasaniff1575f2018-04-08 22:41:38 -0700511 * @hide
Amith Yamasani7ec89412018-02-07 08:48:49 -0800512 */
Amith Yamasani7ec89412018-02-07 08:48:49 -0800513 public int getStandbyBucket() {
Amith Yamasani119be9a2018-02-18 22:23:00 -0800514 return (mBucketAndReason & 0xFFFF0000) >>> 16;
515 }
516
517 /**
Amith Yamasaniff1575f2018-04-08 22:41:38 -0700518 * Returns the standby bucket of the app, if the event is of type
519 * {@link #STANDBY_BUCKET_CHANGED}, otherwise returns 0.
520 * @return the standby bucket associated with the event.
521 *
522 */
523 public int getAppStandbyBucket() {
524 return (mBucketAndReason & 0xFFFF0000) >>> 16;
525 }
526
527 /**
Amith Yamasani119be9a2018-02-18 22:23:00 -0800528 * Returns the reason for the bucketing, if the event is of type
529 * {@link #STANDBY_BUCKET_CHANGED}, otherwise returns 0. Reason values include
530 * the main reason which is one of REASON_MAIN_*, OR'ed with REASON_SUB_*, if there
531 * are sub-reasons for the main reason, such as REASON_SUB_USAGE_* when the main reason
532 * is REASON_MAIN_USAGE.
533 * @hide
534 */
535 public int getStandbyReason() {
536 return mBucketAndReason & 0x0000FFFF;
Amith Yamasani7ec89412018-02-07 08:48:49 -0800537 }
538
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500539 /**
540 * Returns the ID of the {@link android.app.NotificationChannel} for this event if the
541 * event is of type {@link #NOTIFICATION_INTERRUPTION}, otherwise it returns null;
542 * @hide
543 */
544 @SystemApi
545 public String getNotificationChannelId() {
546 return mNotificationChannelId;
547 }
548
Makoto Onukiad623012017-05-15 09:29:34 -0700549 /** @hide */
550 public Event getObfuscatedIfInstantApp() {
Dimuthu Gamage88ebac42019-01-14 08:28:13 -0800551 if (!isInstantApp()) {
Makoto Onukiad623012017-05-15 09:29:34 -0700552 return this;
553 }
554 final Event ret = new Event(this);
555 ret.mPackage = INSTANT_APP_PACKAGE_NAME;
556 ret.mClass = INSTANT_APP_CLASS_NAME;
557
558 // Note there are other string fields too, but they're for app shortcuts and choosers,
559 // which instant apps can't use anyway, so there's no need to hide them.
560 return ret;
561 }
Adam Lesinski35168002014-07-21 15:25:30 -0700562 }
563
564 // Only used when creating the resulting events. Not used for reading/unparceling.
Mathew Inwood31755f92018-12-20 13:53:36 +0000565 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Adam Lesinski35168002014-07-21 15:25:30 -0700566 private List<Event> mEventsToWrite = null;
567
568 // Only used for reading/unparceling events.
Mathew Inwood61e8ae62018-08-14 14:17:44 +0100569 @UnsupportedAppUsage
Adam Lesinski35168002014-07-21 15:25:30 -0700570 private Parcel mParcel = null;
Mathew Inwood31755f92018-12-20 13:53:36 +0000571 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Adam Lesinski35168002014-07-21 15:25:30 -0700572 private final int mEventCount;
573
Mathew Inwood31755f92018-12-20 13:53:36 +0000574 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Adam Lesinski35168002014-07-21 15:25:30 -0700575 private int mIndex = 0;
576
Michael Wachenschwanz0b4ab1f2019-01-07 13:59:10 -0800577 // Only used when parceling events. If false, task roots will be omitted from the parcel
578 private final boolean mIncludeTaskRoots;
579
Adam Lesinski35168002014-07-21 15:25:30 -0700580 /*
581 * In order to save space, since ComponentNames will be duplicated everywhere,
582 * we use a map and index into it.
583 */
Mathew Inwood31755f92018-12-20 13:53:36 +0000584 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Adam Lesinski9d960752014-08-25 14:48:12 -0700585 private String[] mStringPool;
Adam Lesinski35168002014-07-21 15:25:30 -0700586
587 /**
588 * Construct the iterator from a parcel.
589 * {@hide}
590 */
Mathew Inwood31755f92018-12-20 13:53:36 +0000591 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Adam Lesinski35168002014-07-21 15:25:30 -0700592 public UsageEvents(Parcel in) {
Michael Wachenschwanz76d03fc2018-05-24 17:21:02 +0000593 byte[] bytes = in.readBlob();
594 Parcel data = Parcel.obtain();
595 data.unmarshall(bytes, 0, bytes.length);
596 data.setDataPosition(0);
597 mEventCount = data.readInt();
598 mIndex = data.readInt();
Adam Lesinski35168002014-07-21 15:25:30 -0700599 if (mEventCount > 0) {
Michael Wachenschwanz76d03fc2018-05-24 17:21:02 +0000600 mStringPool = data.createStringArray();
Adam Lesinski35168002014-07-21 15:25:30 -0700601
Michael Wachenschwanz76d03fc2018-05-24 17:21:02 +0000602 final int listByteLength = data.readInt();
603 final int positionInParcel = data.readInt();
Adam Lesinski35168002014-07-21 15:25:30 -0700604 mParcel = Parcel.obtain();
605 mParcel.setDataPosition(0);
Michael Wachenschwanz76d03fc2018-05-24 17:21:02 +0000606 mParcel.appendFrom(data, data.dataPosition(), listByteLength);
Adam Lesinski35168002014-07-21 15:25:30 -0700607 mParcel.setDataSize(mParcel.dataPosition());
608 mParcel.setDataPosition(positionInParcel);
609 }
Michael Wachenschwanz0b4ab1f2019-01-07 13:59:10 -0800610 mIncludeTaskRoots = true;
Adam Lesinski35168002014-07-21 15:25:30 -0700611 }
612
613 /**
614 * Create an empty iterator.
615 * {@hide}
616 */
617 UsageEvents() {
618 mEventCount = 0;
Michael Wachenschwanz0b4ab1f2019-01-07 13:59:10 -0800619 mIncludeTaskRoots = true;
620 }
621
622 /**
623 * Construct the iterator in preparation for writing it to a parcel.
624 * Defaults to excluding task roots from the parcel.
625 * {@hide}
626 */
627 public UsageEvents(List<Event> events, String[] stringPool) {
628 this(events, stringPool, false);
Adam Lesinski35168002014-07-21 15:25:30 -0700629 }
630
631 /**
632 * Construct the iterator in preparation for writing it to a parcel.
633 * {@hide}
634 */
Michael Wachenschwanz0b4ab1f2019-01-07 13:59:10 -0800635 public UsageEvents(List<Event> events, String[] stringPool, boolean includeTaskRoots) {
Adam Lesinski9d960752014-08-25 14:48:12 -0700636 mStringPool = stringPool;
Adam Lesinski35168002014-07-21 15:25:30 -0700637 mEventCount = events.size();
638 mEventsToWrite = events;
Michael Wachenschwanz0b4ab1f2019-01-07 13:59:10 -0800639 mIncludeTaskRoots = includeTaskRoots;
Adam Lesinski35168002014-07-21 15:25:30 -0700640 }
641
642 /**
643 * Returns whether or not there are more events to read using
644 * {@link #getNextEvent(android.app.usage.UsageEvents.Event)}.
645 *
646 * @return true if there are more events, false otherwise.
647 */
648 public boolean hasNextEvent() {
649 return mIndex < mEventCount;
650 }
651
652 /**
653 * Retrieve the next {@link android.app.usage.UsageEvents.Event} from the collection and put the
654 * resulting data into {@code eventOut}.
655 *
656 * @param eventOut The {@link android.app.usage.UsageEvents.Event} object that will receive the
657 * next event data.
658 * @return true if an event was available, false if there are no more events.
659 */
660 public boolean getNextEvent(Event eventOut) {
661 if (mIndex >= mEventCount) {
662 return false;
663 }
664
Adam Lesinski7f61e962014-09-02 16:43:52 -0700665 readEventFromParcel(mParcel, eventOut);
Adam Lesinski9d960752014-08-25 14:48:12 -0700666
Adam Lesinski35168002014-07-21 15:25:30 -0700667 mIndex++;
Adam Lesinski35168002014-07-21 15:25:30 -0700668 if (mIndex >= mEventCount) {
669 mParcel.recycle();
670 mParcel = null;
671 }
672 return true;
673 }
674
675 /**
676 * Resets the collection so that it can be iterated over from the beginning.
Adam Lesinski54e064b2014-10-08 12:33:16 -0700677 *
678 * @hide When this object is iterated to completion, the parcel is destroyed and
679 * so resetToStart doesn't work.
Adam Lesinski35168002014-07-21 15:25:30 -0700680 */
681 public void resetToStart() {
682 mIndex = 0;
683 if (mParcel != null) {
684 mParcel.setDataPosition(0);
685 }
686 }
687
Mathew Inwood31755f92018-12-20 13:53:36 +0000688 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Adam Lesinski9d960752014-08-25 14:48:12 -0700689 private int findStringIndex(String str) {
690 final int index = Arrays.binarySearch(mStringPool, str);
691 if (index < 0) {
692 throw new IllegalStateException("String '" + str + "' is not in the string pool");
693 }
694 return index;
695 }
696
Adam Lesinski7f61e962014-09-02 16:43:52 -0700697 /**
698 * Writes a single event to the parcel. Modify this when updating {@link Event}.
699 */
Mathew Inwood31755f92018-12-20 13:53:36 +0000700 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Adam Lesinski7f61e962014-09-02 16:43:52 -0700701 private void writeEventToParcel(Event event, Parcel p, int flags) {
702 final int packageIndex;
703 if (event.mPackage != null) {
704 packageIndex = findStringIndex(event.mPackage);
705 } else {
706 packageIndex = -1;
707 }
708
709 final int classIndex;
710 if (event.mClass != null) {
711 classIndex = findStringIndex(event.mClass);
712 } else {
713 classIndex = -1;
714 }
Michael Wachenschwanz0b4ab1f2019-01-07 13:59:10 -0800715
716 final int taskRootPackageIndex;
717 if (mIncludeTaskRoots && event.mTaskRootPackage != null) {
718 taskRootPackageIndex = findStringIndex(event.mTaskRootPackage);
719 } else {
720 taskRootPackageIndex = -1;
721 }
722
723 final int taskRootClassIndex;
724 if (mIncludeTaskRoots && event.mTaskRootClass != null) {
725 taskRootClassIndex = findStringIndex(event.mTaskRootClass);
726 } else {
727 taskRootClassIndex = -1;
728 }
Adam Lesinski7f61e962014-09-02 16:43:52 -0700729 p.writeInt(packageIndex);
730 p.writeInt(classIndex);
Hui Yu03d12402018-12-06 18:00:37 -0800731 p.writeInt(event.mInstanceId);
Michael Wachenschwanz0b4ab1f2019-01-07 13:59:10 -0800732 p.writeInt(taskRootPackageIndex);
733 p.writeInt(taskRootClassIndex);
Adam Lesinski7f61e962014-09-02 16:43:52 -0700734 p.writeInt(event.mEventType);
735 p.writeLong(event.mTimeStamp);
736
Makoto Onukiac042502016-05-20 16:39:42 -0700737 switch (event.mEventType) {
738 case Event.CONFIGURATION_CHANGE:
739 event.mConfiguration.writeToParcel(p, flags);
740 break;
741 case Event.SHORTCUT_INVOCATION:
742 p.writeString(event.mShortcutId);
743 break;
Kang Li53b43142016-11-14 14:38:25 -0800744 case Event.CHOOSER_ACTION:
745 p.writeString(event.mAction);
746 p.writeString(event.mContentType);
747 p.writeStringArray(event.mContentAnnotations);
748 break;
Amith Yamasanibfc4bf52018-01-19 06:55:08 -0800749 case Event.STANDBY_BUCKET_CHANGED:
Amith Yamasani119be9a2018-02-18 22:23:00 -0800750 p.writeInt(event.mBucketAndReason);
Amith Yamasanibfc4bf52018-01-19 06:55:08 -0800751 break;
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500752 case Event.NOTIFICATION_INTERRUPTION:
753 p.writeString(event.mNotificationChannelId);
754 break;
Adam Lesinski7f61e962014-09-02 16:43:52 -0700755 }
Dimuthu Gamage88ebac42019-01-14 08:28:13 -0800756 p.writeInt(event.mFlags);
Adam Lesinski7f61e962014-09-02 16:43:52 -0700757 }
758
759 /**
760 * Reads a single event from the parcel. Modify this when updating {@link Event}.
761 */
Mathew Inwood31755f92018-12-20 13:53:36 +0000762 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Adam Lesinski7f61e962014-09-02 16:43:52 -0700763 private void readEventFromParcel(Parcel p, Event eventOut) {
764 final int packageIndex = p.readInt();
765 if (packageIndex >= 0) {
766 eventOut.mPackage = mStringPool[packageIndex];
767 } else {
768 eventOut.mPackage = null;
769 }
770
771 final int classIndex = p.readInt();
772 if (classIndex >= 0) {
773 eventOut.mClass = mStringPool[classIndex];
774 } else {
775 eventOut.mClass = null;
776 }
Hui Yu03d12402018-12-06 18:00:37 -0800777 eventOut.mInstanceId = p.readInt();
Michael Wachenschwanz0b4ab1f2019-01-07 13:59:10 -0800778
779 final int taskRootPackageIndex = p.readInt();
780 if (taskRootPackageIndex >= 0) {
781 eventOut.mTaskRootPackage = mStringPool[taskRootPackageIndex];
782 } else {
783 eventOut.mTaskRootPackage = null;
784 }
785
786 final int taskRootClassIndex = p.readInt();
787 if (taskRootClassIndex >= 0) {
788 eventOut.mTaskRootClass = mStringPool[taskRootClassIndex];
789 } else {
790 eventOut.mTaskRootClass = null;
791 }
792
Adam Lesinski7f61e962014-09-02 16:43:52 -0700793 eventOut.mEventType = p.readInt();
794 eventOut.mTimeStamp = p.readLong();
795
Makoto Onukiac042502016-05-20 16:39:42 -0700796 // Fill out the event-dependant fields.
797 eventOut.mConfiguration = null;
798 eventOut.mShortcutId = null;
Kang Li53b43142016-11-14 14:38:25 -0800799 eventOut.mAction = null;
800 eventOut.mContentType = null;
801 eventOut.mContentAnnotations = null;
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500802 eventOut.mNotificationChannelId = null;
Makoto Onukiac042502016-05-20 16:39:42 -0700803
804 switch (eventOut.mEventType) {
805 case Event.CONFIGURATION_CHANGE:
806 // Extract the configuration for configuration change events.
807 eventOut.mConfiguration = Configuration.CREATOR.createFromParcel(p);
808 break;
809 case Event.SHORTCUT_INVOCATION:
810 eventOut.mShortcutId = p.readString();
811 break;
Kang Li53b43142016-11-14 14:38:25 -0800812 case Event.CHOOSER_ACTION:
813 eventOut.mAction = p.readString();
814 eventOut.mContentType = p.readString();
815 eventOut.mContentAnnotations = p.createStringArray();
816 break;
Amith Yamasanibfc4bf52018-01-19 06:55:08 -0800817 case Event.STANDBY_BUCKET_CHANGED:
Amith Yamasani119be9a2018-02-18 22:23:00 -0800818 eventOut.mBucketAndReason = p.readInt();
Amith Yamasanibfc4bf52018-01-19 06:55:08 -0800819 break;
Julia Reynolds1fac86e2018-03-07 08:30:37 -0500820 case Event.NOTIFICATION_INTERRUPTION:
821 eventOut.mNotificationChannelId = p.readString();
822 break;
Adam Lesinski7f61e962014-09-02 16:43:52 -0700823 }
Dimuthu Gamage88ebac42019-01-14 08:28:13 -0800824 eventOut.mFlags = p.readInt();
Adam Lesinski7f61e962014-09-02 16:43:52 -0700825 }
826
827 @Override
828 public int describeContents() {
829 return 0;
830 }
831
Adam Lesinski35168002014-07-21 15:25:30 -0700832 @Override
833 public void writeToParcel(Parcel dest, int flags) {
Michael Wachenschwanz76d03fc2018-05-24 17:21:02 +0000834 Parcel data = Parcel.obtain();
835 data.writeInt(mEventCount);
836 data.writeInt(mIndex);
Adam Lesinski35168002014-07-21 15:25:30 -0700837 if (mEventCount > 0) {
Michael Wachenschwanz76d03fc2018-05-24 17:21:02 +0000838 data.writeStringArray(mStringPool);
Adam Lesinski35168002014-07-21 15:25:30 -0700839
840 if (mEventsToWrite != null) {
841 // Write out the events
842 Parcel p = Parcel.obtain();
843 try {
844 p.setDataPosition(0);
845 for (int i = 0; i < mEventCount; i++) {
846 final Event event = mEventsToWrite.get(i);
Adam Lesinski7f61e962014-09-02 16:43:52 -0700847 writeEventToParcel(event, p, flags);
Adam Lesinski35168002014-07-21 15:25:30 -0700848 }
Adam Lesinski7f61e962014-09-02 16:43:52 -0700849
Adam Lesinski35168002014-07-21 15:25:30 -0700850 final int listByteLength = p.dataPosition();
851
852 // Write the total length of the data.
Michael Wachenschwanz76d03fc2018-05-24 17:21:02 +0000853 data.writeInt(listByteLength);
Adam Lesinski35168002014-07-21 15:25:30 -0700854
855 // Write our current position into the data.
Michael Wachenschwanz76d03fc2018-05-24 17:21:02 +0000856 data.writeInt(0);
Adam Lesinski35168002014-07-21 15:25:30 -0700857
858 // Write the data.
Michael Wachenschwanz76d03fc2018-05-24 17:21:02 +0000859 data.appendFrom(p, 0, listByteLength);
Adam Lesinski35168002014-07-21 15:25:30 -0700860 } finally {
861 p.recycle();
862 }
863
864 } else if (mParcel != null) {
865 // Write the total length of the data.
Michael Wachenschwanz76d03fc2018-05-24 17:21:02 +0000866 data.writeInt(mParcel.dataSize());
Adam Lesinski35168002014-07-21 15:25:30 -0700867
868 // Write out current position into the data.
Michael Wachenschwanz76d03fc2018-05-24 17:21:02 +0000869 data.writeInt(mParcel.dataPosition());
Adam Lesinski35168002014-07-21 15:25:30 -0700870
871 // Write the data.
Michael Wachenschwanz76d03fc2018-05-24 17:21:02 +0000872 data.appendFrom(mParcel, 0, mParcel.dataSize());
Adam Lesinski35168002014-07-21 15:25:30 -0700873 } else {
874 throw new IllegalStateException(
875 "Either mParcel or mEventsToWrite must not be null");
876 }
877 }
Michael Wachenschwanz76d03fc2018-05-24 17:21:02 +0000878 // Data can be too large for a transact. Write the data as a Blob, which will be written to
879 // ashmem if too large.
880 dest.writeBlob(data.marshall());
Adam Lesinski35168002014-07-21 15:25:30 -0700881 }
882
883 public static final Creator<UsageEvents> CREATOR = new Creator<UsageEvents>() {
884 @Override
885 public UsageEvents createFromParcel(Parcel source) {
886 return new UsageEvents(source);
887 }
888
889 @Override
890 public UsageEvents[] newArray(int size) {
891 return new UsageEvents[size];
892 }
893 };
Adam Lesinski35168002014-07-21 15:25:30 -0700894}