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
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user