diff options
| author | 2019-03-05 15:22:13 -0800 | |
|---|---|---|
| committer | 2019-03-07 15:00:43 -0800 | |
| commit | 80a8b4678d421a8c4b10bcdbd2ea244a40950d70 (patch) | |
| tree | f8bc772fc8ac7f38f25b6e42b306209bdd330dc4 | |
| parent | 30a4df94540e0f01dfe8f6ac06f4b87c0b5efa18 (diff) | |
Add new UsageStats event DEVICE_STARTUP
As a counterpart to DEVICE_SHUTDOWN event, DEVICE_STARTUP records the
device startup event and timestamp.
Bug: 126529272
Test: adb shell dumpsys usagestats | grep DEVICE
Change-Id: Ia977658f9a1f701ad802dc943e9eff71171e9957
| -rw-r--r-- | api/current.txt | 1 | ||||
| -rw-r--r-- | core/java/android/app/usage/UsageEvents.java | 18 | ||||
| -rw-r--r-- | services/usage/java/com/android/server/usage/UserUsageStatsService.java | 20 |
3 files changed, 30 insertions, 9 deletions
diff --git a/api/current.txt b/api/current.txt index 12b4d4b29c84..dd6b29a8e9fc 100644 --- a/api/current.txt +++ b/api/current.txt @@ -7728,6 +7728,7 @@ package android.app.usage { field public static final int ACTIVITY_STOPPED = 23; // 0x17 field public static final int CONFIGURATION_CHANGE = 5; // 0x5 field public static final int DEVICE_SHUTDOWN = 26; // 0x1a + field public static final int DEVICE_STARTUP = 27; // 0x1b field public static final int FOREGROUND_SERVICE_START = 19; // 0x13 field public static final int FOREGROUND_SERVICE_STOP = 20; // 0x14 field public static final int KEYGUARD_HIDDEN = 18; // 0x12 diff --git a/core/java/android/app/usage/UsageEvents.java b/core/java/android/app/usage/UsageEvents.java index 0d0e466e9688..b3d01fd68726 100644 --- a/core/java/android/app/usage/UsageEvents.java +++ b/core/java/android/app/usage/UsageEvents.java @@ -254,18 +254,32 @@ public final class UsageEvents implements Parcelable { public static final int FLUSH_TO_DISK = 25; /** - * An event type denoting that the device underwent a shutdown process. + * An event type denoting that the Android runtime underwent a shutdown process. * A DEVICE_SHUTDOWN event should be treated as if all started activities and foreground * services are now stopped and no explicit {@link #ACTIVITY_STOPPED} and * {@link #FOREGROUND_SERVICE_STOP} events will be generated for them. + * + * <p>The DEVICE_SHUTDOWN timestamp is actually the last time UsageStats database is + * persisted before the actual shutdown. Events (if there are any) between this timestamp + * and the actual shutdown is not persisted in the database. So any open events without + * matching close events between DEVICE_SHUTDOWN and {@link #DEVICE_STARTUP} should be + * ignored because the closing time is unknown.</p> */ public static final int DEVICE_SHUTDOWN = 26; /** + * An event type denoting that the Android runtime started up. This could be after a + * shutdown or a runtime restart. Any open events without matching close events between + * {@link #DEVICE_SHUTDOWN} and DEVICE_STARTUP should be ignored because the closing time is + * unknown. + */ + public static final int DEVICE_STARTUP = 27; + + /** * Keep in sync with the greatest event type value. * @hide */ - public static final int MAX_EVENT_TYPE = 26; + public static final int MAX_EVENT_TYPE = 27; /** @hide */ public static final int FLAG_IS_PACKAGE_INSTANT_APP = 1 << 0; diff --git a/services/usage/java/com/android/server/usage/UserUsageStatsService.java b/services/usage/java/com/android/server/usage/UserUsageStatsService.java index b9a5676a84ef..9498e161ff61 100644 --- a/services/usage/java/com/android/server/usage/UserUsageStatsService.java +++ b/services/usage/java/com/android/server/usage/UserUsageStatsService.java @@ -17,6 +17,7 @@ package com.android.server.usage; import static android.app.usage.UsageEvents.Event.DEVICE_SHUTDOWN; +import static android.app.usage.UsageEvents.Event.DEVICE_STARTUP; import static android.app.usage.UsageStatsManager.INTERVAL_BEST; import static android.app.usage.UsageStatsManager.INTERVAL_COUNT; import static android.app.usage.UsageStatsManager.INTERVAL_DAILY; @@ -137,15 +138,18 @@ class UserUsageStatsService { // During system reboot, add a DEVICE_SHUTDOWN event to the end of event list, the timestamp // is last time UsageStatsDatabase is persisted to disk. + // Also add a DEVICE_STARTUP event with current system timestamp. final IntervalStats currentDailyStats = mCurrentStats[INTERVAL_DAILY]; if (currentDailyStats != null) { - final int size = currentDailyStats.events.size(); - if (size == 0 || currentDailyStats.events.get(size - 1).mEventType != DEVICE_SHUTDOWN) { - // The last event in event list is not DEVICE_SHUTDOWN, then we insert one. - final Event event = new Event(DEVICE_SHUTDOWN, currentDailyStats.lastTimeSaved); - event.mPackage = Event.DEVICE_EVENT_PACKAGE_NAME; - currentDailyStats.addEvent(event); - } + // File system timestamp only has precision of 1 second, add 1000ms to make up + // for the loss of round up. + final Event shutdownEvent = + new Event(DEVICE_SHUTDOWN, currentDailyStats.lastTimeSaved + 1000); + shutdownEvent.mPackage = Event.DEVICE_EVENT_PACKAGE_NAME; + currentDailyStats.addEvent(shutdownEvent); + final Event startupEvent = new Event(DEVICE_STARTUP, currentTimeMillis); + startupEvent.mPackage = Event.DEVICE_EVENT_PACKAGE_NAME; + currentDailyStats.addEvent(startupEvent); } if (mDatabase.isNewUpdate()) { @@ -956,6 +960,8 @@ class UserUsageStatsService { return "KEYGUARD_HIDDEN"; case Event.DEVICE_SHUTDOWN: return "DEVICE_SHUTDOWN"; + case Event.DEVICE_STARTUP: + return "DEVICE_STARTUP"; default: return "UNKNOWN_TYPE_" + eventType; } |