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

Commit 23f7206d authored by Zhen Zhang's avatar Zhen Zhang
Browse files

Create component used metrics in UsageStats

Created a new metrics called mLastTimeComponentUsed in UsageStats.
It is persisted and loaded as other metrics.

Bug: 175829712
Test: core/tests/coretests/src/android/app/usage/UsageStatsTest
Test: cts/UsageStatsTest
Test: UsageStatsDatabaseTest
Test: UsageStatsServiceTest
Test: UsageStatsPersistenceTest
Change-Id: I10cf5c40cf5955246a6a04e96ac0352b4ed29acd
parent e3e90bd0
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -1795,6 +1795,7 @@ package android.app.usage {
  public final class UsageStats implements android.os.Parcelable {
    method public int getAppLaunchCount();
    method public long getLastTimeComponentUsed();
  }
  public final class UsageStatsManager {
+25 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import static android.app.usage.UsageEvents.Event.ACTIVITY_DESTROYED;
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_STOPPED;
import static android.app.usage.UsageEvents.Event.APP_COMPONENT_USED;
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;
@@ -108,6 +109,13 @@ public final class UsageStats implements Parcelable {
     */
    public long mTotalTimeForegroundServiceUsed;

    /**
     * Last time this package's component is used, measured in milliseconds since the epoch.
     * See {@link UsageEvents.Event#APP_COMPONENT_USED}
     * @hide
     */
    public long mLastTimeComponentUsed;

    /**
     * {@hide}
     */
@@ -166,6 +174,7 @@ public final class UsageStats implements Parcelable {
        mEndTimeStamp = stats.mEndTimeStamp;
        mLastTimeUsed = stats.mLastTimeUsed;
        mLastTimeVisible = stats.mLastTimeVisible;
        mLastTimeComponentUsed = stats.mLastTimeComponentUsed;
        mLastTimeForegroundServiceUsed = stats.mLastTimeForegroundServiceUsed;
        mTotalTimeInForeground = stats.mTotalTimeInForeground;
        mTotalTimeVisible = stats.mTotalTimeVisible;
@@ -264,6 +273,16 @@ public final class UsageStats implements Parcelable {
        return mTotalTimeForegroundServiceUsed;
    }

    /**
     * Get the last time this package's component was used, measured in milliseconds since the
     * epoch.
     * @hide
     */
    @SystemApi
    public long getLastTimeComponentUsed() {
        return mLastTimeComponentUsed;
    }

    /**
     * Returns the number of times the app was launched as an activity from outside of the app.
     * Excludes intra-app activity transitions.
@@ -323,6 +342,7 @@ public final class UsageStats implements Parcelable {
            mergeEventMap(mForegroundServices, right.mForegroundServices);
            mLastTimeUsed = Math.max(mLastTimeUsed, right.mLastTimeUsed);
            mLastTimeVisible = Math.max(mLastTimeVisible, right.mLastTimeVisible);
            mLastTimeComponentUsed = Math.max(mLastTimeComponentUsed, right.mLastTimeComponentUsed);
            mLastTimeForegroundServiceUsed = Math.max(mLastTimeForegroundServiceUsed,
                    right.mLastTimeForegroundServiceUsed);
        }
@@ -598,6 +618,9 @@ public final class UsageStats implements Parcelable {
                    mLastTimeVisible = timeStamp;
                }
                break;
            case APP_COMPONENT_USED:
                mLastTimeComponentUsed = timeStamp;
                break;
            default:
                break;
        }
@@ -620,6 +643,7 @@ public final class UsageStats implements Parcelable {
        dest.writeLong(mEndTimeStamp);
        dest.writeLong(mLastTimeUsed);
        dest.writeLong(mLastTimeVisible);
        dest.writeLong(mLastTimeComponentUsed);
        dest.writeLong(mLastTimeForegroundServiceUsed);
        dest.writeLong(mTotalTimeInForeground);
        dest.writeLong(mTotalTimeVisible);
@@ -674,6 +698,7 @@ public final class UsageStats implements Parcelable {
            stats.mEndTimeStamp = in.readLong();
            stats.mLastTimeUsed = in.readLong();
            stats.mLastTimeVisible = in.readLong();
            stats.mLastTimeComponentUsed = in.readLong();
            stats.mLastTimeForegroundServiceUsed = in.readLong();
            stats.mTotalTimeInForeground = in.readLong();
            stats.mTotalTimeVisible = in.readLong();
+2 −0
Original line number Diff line number Diff line
@@ -57,6 +57,8 @@ message IntervalStatsProto {
    // Time attributes stored as an offset of the IntervalStats's beginTime.
    optional int64 last_time_visible_ms = 10;
    optional int64 total_time_visible_ms = 11;
    // Time attributes stored as an offset of the IntervalStats's beginTime.
    optional int64 last_time_component_used_ms = 12;
  }

  // Stores the relevant information an IntervalStats will have about a Configuration
+1 −0
Original line number Diff line number Diff line
@@ -81,6 +81,7 @@ message UsageStatsObfuscatedProto {
  optional int64 total_time_service_used_ms = 9;
  optional int64 last_time_visible_ms = 10;
  optional int64 total_time_visible_ms = 11;
  optional int64 last_time_component_used_ms = 12;
}

/**
+2 −1
Original line number Diff line number Diff line
@@ -46,7 +46,8 @@ public class UsageStatsPersistenceTest {
    private static final String[] USAGESTATS_PERSISTED_FIELDS = {"mBeginTimeStamp", "mEndTimeStamp",
            "mPackageName", "mPackageToken", "mLastEvent", "mAppLaunchCount", "mChooserCounts",
            "mLastTimeUsed", "mTotalTimeInForeground", "mLastTimeForegroundServiceUsed",
            "mTotalTimeForegroundServiceUsed", "mLastTimeVisible", "mTotalTimeVisible"};
            "mTotalTimeForegroundServiceUsed", "mLastTimeVisible", "mTotalTimeVisible",
            "mLastTimeComponentUsed"};
    // All fields in this list are defined in UsageStats but not persisted
    private static final String[] USAGESTATS_IGNORED_FIELDS = {"CREATOR", "mActivities",
            "mForegroundServices", "mLaunchCount", "mChooserCountsObfuscated"};
Loading