Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b1d243a5 authored by Hui Yu's avatar Hui Yu
Browse files

UsageStats DEVICE_SHUTDOWN event.

DEVICE_SHUTDOWN event is used to close all open usage events that do
not have matching closing event when device is shut down. For example,
ACTIVITY_RESUMED or FOREGROUND_SERVICE_START are open events, the
DEVICE_SHUTDOWN event will close the usage session of the open events.

At orderly shutdown like selecting Power Off or Restart after pressing
power button, a DEVICE_SHUTDOWN event is sent to UsageStats.
UsageStats persists UsageStatsDatabase to disk immediately.

When power button is pressed for 3.5 seconds (configured by
config_veryLongPressTimeout in config.xml). A DEVICE_SHUTDOWN
event is sent to UsageStats. UsageStats persists UsageStatsDatabase
to disk immediately.

This is the mechanism that we do not lose UsageStats data when the
device is shut down.

When the device boots up, if the last event is not
DEVICE_SHUTDOWN, we add a DEVICE_SHUTDOWN with timestamp set to be the last
time database file is persisted. This is to handle the case device
shutdown abruptly due to power drained or cold temperature.

Bug: 111464278
Test: atest UsageStatsTest.java
Change-Id: I1e88063ba71d09042d02c6deb9f07d8581a15c30
parent 1960cd48
Loading
Loading
Loading
Loading
+1 −0
Original line number Original line Diff line number Diff line
@@ -7682,6 +7682,7 @@ package android.app.usage {
    field public static final int ACTIVITY_RESUMED = 1; // 0x1
    field public static final int ACTIVITY_RESUMED = 1; // 0x1
    field public static final int ACTIVITY_STOPPED = 23; // 0x17
    field public static final int ACTIVITY_STOPPED = 23; // 0x17
    field public static final int CONFIGURATION_CHANGE = 5; // 0x5
    field public static final int CONFIGURATION_CHANGE = 5; // 0x5
    field public static final int DEVICE_SHUTDOWN = 26; // 0x1a
    field public static final int FOREGROUND_SERVICE_START = 19; // 0x13
    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 FOREGROUND_SERVICE_STOP = 20; // 0x14
    field public static final int KEYGUARD_HIDDEN = 18; // 0x12
    field public static final int KEYGUARD_HIDDEN = 18; // 0x12
+6 −0
Original line number Original line Diff line number Diff line
@@ -320,4 +320,10 @@ public abstract class ActivityManagerInternal {


    /** Remove pending backup for the given userId. */
    /** Remove pending backup for the given userId. */
    public abstract void clearPendingBackup(int userId);
    public abstract void clearPendingBackup(int userId);

    /**
     * When power button is very long pressed, call this interface to do some pre-shutdown work
     * like persisting database etc.
     */
    public abstract void prepareForPossibleShutdown();
}
}
+0 −17
Original line number Original line Diff line number Diff line
@@ -103,21 +103,4 @@ public class EventList {
        }
        }
        return result;
        return result;
    }
    }

    /**
     * Remove events of certain type on or after a timestamp.
     * @param type The type of event to remove.
     * @param timeStamp the timeStamp on or after which to remove the event.
     */
    public void removeOnOrAfter(int type, long timeStamp) {
        for (int i = mEvents.size() - 1; i >= 0; i--) {
            UsageEvents.Event event = mEvents.get(i);
            if (event.mTimeStamp < timeStamp) {
                break;
            }
            if (event.mEventType == type) {
                mEvents.remove(i);
            }
        }
    }
}
}
+9 −1
Original line number Original line Diff line number Diff line
@@ -244,11 +244,19 @@ public final class UsageEvents implements Parcelable {
         */
         */
        public static final int FLUSH_TO_DISK = 25;
        public static final int FLUSH_TO_DISK = 25;


        /**
         * An event type denoting that the device 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.
         */
        public static final int DEVICE_SHUTDOWN = 26;

        /**
        /**
         * Keep in sync with the greatest event type value.
         * Keep in sync with the greatest event type value.
         * @hide
         * @hide
         */
         */
        public static final int MAX_EVENT_TYPE = 25;
        public static final int MAX_EVENT_TYPE = 26;


        /** @hide */
        /** @hide */
        public static final int FLAG_IS_PACKAGE_INSTANT_APP = 1 << 0;
        public static final int FLAG_IS_PACKAGE_INSTANT_APP = 1 << 0;
+4 −5
Original line number Original line Diff line number Diff line
@@ -21,6 +21,7 @@ import static android.app.usage.UsageEvents.Event.ACTIVITY_PAUSED;
import static android.app.usage.UsageEvents.Event.ACTIVITY_RESUMED;
import static android.app.usage.UsageEvents.Event.ACTIVITY_RESUMED;
import static android.app.usage.UsageEvents.Event.ACTIVITY_STOPPED;
import static android.app.usage.UsageEvents.Event.ACTIVITY_STOPPED;
import static android.app.usage.UsageEvents.Event.CONTINUING_FOREGROUND_SERVICE;
import static android.app.usage.UsageEvents.Event.CONTINUING_FOREGROUND_SERVICE;
import static android.app.usage.UsageEvents.Event.DEVICE_SHUTDOWN;
import static android.app.usage.UsageEvents.Event.END_OF_DAY;
import static android.app.usage.UsageEvents.Event.END_OF_DAY;
import static android.app.usage.UsageEvents.Event.FLUSH_TO_DISK;
import static android.app.usage.UsageEvents.Event.FLUSH_TO_DISK;
import static android.app.usage.UsageEvents.Event.FOREGROUND_SERVICE_START;
import static android.app.usage.UsageEvents.Event.FOREGROUND_SERVICE_START;
@@ -119,12 +120,9 @@ public final class UsageStats implements Parcelable {
    public int mLastEvent;
    public int mLastEvent;


    /**
    /**
     * If an activity is visible(onStart(), onPause() states) or in foreground (onResume() state),
     * it has one entry in this map. When an activity becomes invisible (onStop() or onDestroy()),
     * it is removed from this map.
     * Key is instanceId of the activity (ActivityRecode appToken hashCode)..
     * Key is instanceId of the activity (ActivityRecode appToken hashCode)..
     * Value is this activity's last event, one of ACTIVITY_RESUMED or
     * Value is this activity's last event, one of ACTIVITY_RESUMED, ACTIVITY_PAUSED or
     * ACTIVITY_PAUSED.
     * ACTIVITY_STOPPED.
     * {@hide}
     * {@hide}
     */
     */
    public SparseIntArray mActivities = new SparseIntArray();
    public SparseIntArray mActivities = new SparseIntArray();
@@ -560,6 +558,7 @@ public final class UsageStats implements Parcelable {
                mLastTimeForegroundServiceUsed = timeStamp;
                mLastTimeForegroundServiceUsed = timeStamp;
                mForegroundServices.put(className, eventType);
                mForegroundServices.put(className, eventType);
                break;
                break;
            case DEVICE_SHUTDOWN:
            case FLUSH_TO_DISK:
            case FLUSH_TO_DISK:
                // update usage of all active activities/services.
                // update usage of all active activities/services.
                if (hasForegroundActivity()) {
                if (hasForegroundActivity()) {
Loading