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

Commit 03d1240e authored by Hui Yu's avatar Hui Yu
Browse files

UsageStats tracking of screen usage.

1. Add UsageStats Event types:
ACTIVITY_RESUMED is synonym to existing MOVE_TO_FOREGROUND.
ACTIVITY_PAUSED is synonym to existing MOVE_TO_BACKGROUND.
ACTIVITY_STOPPED when an activity becomes invisible on the UI.
2. In UsageStats.java, add API getLastTimeVisible() to report last time the
app is visible (ACTIVITY_RESUMED or ACTIVITY_PAUSED), add API getTotalTimeVisible()
to report total time the app is visible.
The existing API getLastTimeUsed() can report last time the app is in
foreground (AKA have focus).
The existing API getTotalTimeInForeground() can report total time the
app is in foreground (AKA have focus).
3. UsageStats.getTotalTimeVisible() can report screen usage for
split-screen mode and picture-in-picture mode.
4. Because in the same package, activity can be instantiated multiple times,
In UsageEvents.Event class, add a member mInstaceId for activity's
instance ID, add interface getInstanceId() to retrieve the instance ID.

Bug: 112002260
Test: frameworks/base/services/tests/servicestests/src/com/android/server/usage/UsageStatsDatabaseTest.java
atest frameworks/base/core/tests/coretests/src/android/app/usage/UsageStatsTest.java

Change-Id: Ibcef2488e9620804c9f9220b027f976e8fa0c98b
parent 52fe5dd9
Loading
Loading
Loading
Loading
+7 −2
Original line number Diff line number Diff line
@@ -7614,13 +7614,16 @@ package android.app.usage {
    method public java.lang.String getPackageName();
    method public java.lang.String getShortcutId();
    method public long getTimeStamp();
    field public static final int ACTIVITY_PAUSED = 2; // 0x2
    field public static final int ACTIVITY_RESUMED = 1; // 0x1
    field public static final int ACTIVITY_STOPPED = 23; // 0x17
    field public static final int CONFIGURATION_CHANGE = 5; // 0x5
    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
    field public static final int KEYGUARD_SHOWN = 17; // 0x11
    field public static final int MOVE_TO_BACKGROUND = 2; // 0x2
    field public static final int MOVE_TO_FOREGROUND = 1; // 0x1
    field public static final deprecated int MOVE_TO_BACKGROUND = 2; // 0x2
    field public static final deprecated int MOVE_TO_FOREGROUND = 1; // 0x1
    field public static final int NONE = 0; // 0x0
    field public static final int SCREEN_INTERACTIVE = 15; // 0xf
    field public static final int SCREEN_NON_INTERACTIVE = 16; // 0x10
@@ -7637,9 +7640,11 @@ package android.app.usage {
    method public long getLastTimeForegroundServiceUsed();
    method public long getLastTimeStamp();
    method public long getLastTimeUsed();
    method public long getLastTimeVisible();
    method public java.lang.String getPackageName();
    method public long getTotalTimeForegroundServiceUsed();
    method public long getTotalTimeInForeground();
    method public long getTotalTimeVisible();
    method public void writeToParcel(android.os.Parcel, int);
    field public static final android.os.Parcelable.Creator<android.app.usage.UsageStats> CREATOR;
  }
+1 −0
Original line number Diff line number Diff line
@@ -935,6 +935,7 @@ package android.app.usage {
  }

  public static final class UsageEvents.Event {
    method public int getInstanceId();
    method public java.lang.String getNotificationChannelId();
    field public static final int NOTIFICATION_INTERRUPTION = 12; // 0xc
    field public static final int NOTIFICATION_SEEN = 10; // 0xa
+19 −1
Original line number Diff line number Diff line
@@ -196,8 +196,26 @@ public abstract class ActivityManagerInternal {

    public abstract void updateOomAdj();
    public abstract void updateCpuStats();
    public abstract void updateUsageStats(

    /**
     * Update battery stats on activity usage.
     * @param activity
     * @param uid
     * @param userId
     * @param started
     */
    public abstract void updateBatteryStats(
            ComponentName activity, int uid, int userId, boolean resumed);

    /**
     * Update UsageStats of the activity.
     * @param activity
     * @param userId
     * @param event
     * @param appToken ActivityRecord's appToken.
     */
    public abstract void updateActivityUsageStats(
            ComponentName activity, int userId, int event, IBinder appToken);
    public abstract void updateForegroundTimeIfOnBattery(
            String packageName, int uid, long cpuTimeDiff);
    public abstract void sendForegroundProfileChanged(int userId);
+17 −0
Original line number Diff line number Diff line
@@ -103,4 +103,21 @@ public class EventList {
        }
        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);
            }
        }
    }
}
+72 −8
Original line number Diff line number Diff line
@@ -49,14 +49,28 @@ public final class UsageEvents implements Parcelable {
         */
        public static final int NONE = 0;

        /**
         * @deprecated by {@link #ACTIVITY_RESUMED}
         */
        @Deprecated
        public static final int MOVE_TO_FOREGROUND = 1;

        /**
         * An event type denoting that an {@link android.app.Activity} moved to the foreground.
         * This event has a package name and class name associated with it and can be retrieved
         * using {@link #getPackageName()} and {@link #getClassName()}.
         * If a package has multiple activities, this event is reported for each activity that moves
         * to foreground.
         * This event is corresponding to {@link android.app.Activity#onResume()} of the
         * activity's lifecycle.
         */
        public static final int MOVE_TO_FOREGROUND = 1;
        public static final int ACTIVITY_RESUMED = MOVE_TO_FOREGROUND;

        /**
         * @deprecated by {@link #ACTIVITY_PAUSED}
         */
        @Deprecated
        public static final int MOVE_TO_BACKGROUND = 2;

        /**
         * An event type denoting that an {@link android.app.Activity} moved to the background.
@@ -64,19 +78,21 @@ public final class UsageEvents implements Parcelable {
         * using {@link #getPackageName()} and {@link #getClassName()}.
         * If a package has multiple activities, this event is reported for each activity that moves
         * to background.
         * This event is corresponding to {@link android.app.Activity#onPause()} of the activity's
         * lifecycle.
         */
        public static final int MOVE_TO_BACKGROUND = 2;
        public static final int ACTIVITY_PAUSED = MOVE_TO_BACKGROUND;

        /**
         * An event type denoting that a component was in the foreground when the stats
         * rolled-over. This is effectively treated as a {@link #MOVE_TO_BACKGROUND}.
         * rolled-over. This is effectively treated as a {@link #ACTIVITY_PAUSED}.
         * {@hide}
         */
        public static final int END_OF_DAY = 3;

        /**
         * An event type denoting that a component was in the foreground the previous day.
         * This is effectively treated as a {@link #MOVE_TO_FOREGROUND}.
         * This is effectively treated as a {@link #ACTIVITY_RESUMED}.
         * {@hide}
         */
        public static final int CONTINUE_PREVIOUS_DAY = 4;
@@ -206,11 +222,32 @@ public final class UsageEvents implements Parcelable {
         */
        public static final int ROLLOVER_FOREGROUND_SERVICE = 22;

        /**
         * An activity becomes invisible on the UI, corresponding to
         * {@link android.app.Activity#onStop()} of the activity's lifecycle.
         */
        public static final int ACTIVITY_STOPPED = 23;

        /**
         * An activity object is destroyed, corresponding to
         * {@link android.app.Activity#onDestroy()} of the activity's lifecycle.
         * {@hide}
         */
        public static final int ACTIVITY_DESTROYED = 24;

        /**
         * The event type demoting that a flush of UsageStatsDatabase to file system. Before the
         * flush all usage stats need to be updated to latest timestamp to make sure the most
         * up to date stats are persisted.
         * @hide
         */
        public static final int FLUSH_TO_DISK = 25;

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

        /** @hide */
        public static final int FLAG_IS_PACKAGE_INSTANT_APP = 1 << 0;
@@ -240,6 +277,12 @@ public final class UsageEvents implements Parcelable {
        @UnsupportedAppUsage
        public String mClass;


        /**
         * {@hide}
         */
        public int mInstanceId;

        /**
         * {@hide}
         */
@@ -310,10 +353,17 @@ public final class UsageEvents implements Parcelable {
        public Event() {
        }

        /** @hide */
        public Event(int type,  long timeStamp) {
            mEventType = type;
            mTimeStamp = timeStamp;
        }

        /** @hide */
        public Event(Event orig) {
            mPackage = orig.mPackage;
            mClass = orig.mClass;
            mInstanceId = orig.mInstanceId;
            mTimeStamp = orig.mTimeStamp;
            mEventType = orig.mEventType;
            mConfiguration = orig.mConfiguration;
@@ -341,6 +391,16 @@ public final class UsageEvents implements Parcelable {
            return mClass;
        }

        /**
         *  An activity can be instantiated multiple times, this is the unique activity instance ID.
         *  For non-activity class, instance ID is always zero.
         *  @hide
         */
        @SystemApi
        public int getInstanceId() {
            return mInstanceId;
        }

        /**
         * The time at which this event occurred, measured in milliseconds since the epoch.
         * <p/>
@@ -352,12 +412,14 @@ public final class UsageEvents implements Parcelable {

        /**
         * The event type.
         *
         * @see #MOVE_TO_BACKGROUND
         * @see #MOVE_TO_FOREGROUND
         * @see #ACTIVITY_PAUSED
         * @see #ACTIVITY_RESUMED
         * @see #CONFIGURATION_CHANGE
         * @see #USER_INTERACTION
         * @see #STANDBY_BUCKET_CHANGED
         * @see #FOREGROUND_SERVICE_START
         * @see #FOREGROUND_SERVICE_STOP
         * @see #ACTIVITY_STOPPED
         */
        public int getEventType() {
            return mEventType;
@@ -576,6 +638,7 @@ public final class UsageEvents implements Parcelable {
        }
        p.writeInt(packageIndex);
        p.writeInt(classIndex);
        p.writeInt(event.mInstanceId);
        p.writeInt(event.mEventType);
        p.writeLong(event.mTimeStamp);

@@ -618,6 +681,7 @@ public final class UsageEvents implements Parcelable {
        } else {
            eventOut.mClass = null;
        }
        eventOut.mInstanceId = p.readInt();
        eventOut.mEventType = p.readInt();
        eventOut.mTimeStamp = p.readLong();

Loading