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

Commit 157db238 authored by Kevin Han's avatar Kevin Han
Browse files

Add new component usage event (1/2)

Add a new usage event to UsageStats when a component in a package is
used (content provider binding, explicit broadcast, activity resume)
and it is considered to be important usage.

The follow-up CL will handle the service binding case as it is a little
complicated due to job bindings.

Bug: 175829712
Test: adb shell dumpsys usagestats <package-name>
Test: atest UserUsageStatsServiceTest
Change-Id: I1f2967dcf5999a8f0affea94bce4c0873a04e022
parent af1ec28d
Loading
Loading
Loading
Loading
+10 −1
Original line number Diff line number Diff line
@@ -333,11 +333,20 @@ public final class UsageEvents implements Parcelable {
         */
        public static final int LOCUS_ID_SET = 30;

        /**
         * An event type denoting that a component in the package has been used (e.g. broadcast
         * receiver, service, content provider). This generally matches up with usage that would
         * cause an app to leave force stop. The component itself is not provided as we are only
         * interested in whether the package is used, not the component itself.
         * @hide
         */
        public static final int APP_COMPONENT_USED = 31;

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

        /** @hide */
        public static final int FLAG_IS_PACKAGE_INSTANT_APP = 1 << 0;
+9 −9
Original line number Diff line number Diff line
@@ -78,15 +78,15 @@ public class UsageStatsPersistenceTest {
            "VALID_FLAG_BITS", "UNASSIGNED_TOKEN", "MAX_EVENT_TYPE"};
    // All fields in this list are final constants defining event types and not persisted
    private static final String[] EVENT_TYPES = {"NONE", "ACTIVITY_DESTROYED", "ACTIVITY_PAUSED",
            "ACTIVITY_RESUMED", "ACTIVITY_STOPPED", "CHOOSER_ACTION", "CONFIGURATION_CHANGE",
            "CONTINUE_PREVIOUS_DAY", "CONTINUING_FOREGROUND_SERVICE", "DEVICE_SHUTDOWN",
            "DEVICE_STARTUP", "END_OF_DAY", "FLUSH_TO_DISK", "FOREGROUND_SERVICE_START",
            "FOREGROUND_SERVICE_STOP", "KEYGUARD_HIDDEN", "KEYGUARD_SHOWN", "LOCUS_ID_SET",
            "MOVE_TO_BACKGROUND", "MOVE_TO_FOREGROUND", "NOTIFICATION_INTERRUPTION",
            "NOTIFICATION_SEEN", "ROLLOVER_FOREGROUND_SERVICE", "SCREEN_INTERACTIVE",
            "SCREEN_NON_INTERACTIVE", "SHORTCUT_INVOCATION", "SLICE_PINNED", "SLICE_PINNED_PRIV",
            "STANDBY_BUCKET_CHANGED", "SYSTEM_INTERACTION", "USER_INTERACTION", "USER_STOPPED",
            "USER_UNLOCKED"};
            "ACTIVITY_RESUMED", "ACTIVITY_STOPPED", "APP_COMPONENT_USED", "CHOOSER_ACTION",
            "CONFIGURATION_CHANGE", "CONTINUE_PREVIOUS_DAY", "CONTINUING_FOREGROUND_SERVICE",
            "DEVICE_SHUTDOWN", "DEVICE_STARTUP", "END_OF_DAY", "FLUSH_TO_DISK",
            "FOREGROUND_SERVICE_START", "FOREGROUND_SERVICE_STOP", "KEYGUARD_HIDDEN",
            "KEYGUARD_SHOWN", "LOCUS_ID_SET", "MOVE_TO_BACKGROUND", "MOVE_TO_FOREGROUND",
            "NOTIFICATION_INTERRUPTION", "NOTIFICATION_SEEN", "ROLLOVER_FOREGROUND_SERVICE",
            "SCREEN_INTERACTIVE", "SCREEN_NON_INTERACTIVE", "SHORTCUT_INVOCATION", "SLICE_PINNED",
            "SLICE_PINNED_PRIV", "STANDBY_BUCKET_CHANGED", "SYSTEM_INTERACTION", "USER_INTERACTION",
            "USER_STOPPED", "USER_UNLOCKED"};

    @Test
    public void testUsageEventsFields() {
+9 −0
Original line number Diff line number Diff line
@@ -2684,6 +2684,11 @@ public class ActivityManagerService extends IActivityManager.Stub
        }
        if (mUsageStatsService != null) {
            mUsageStatsService.reportEvent(activity, userId, event, appToken.hashCode(), taskRoot);
            if (event == Event.ACTIVITY_RESUMED) {
                // Report component usage as an activity is an app component
                mUsageStatsService.reportEvent(
                        activity.getPackageName(), userId, Event.APP_COMPONENT_USED);
            }
        }
        ContentCaptureManagerInternal contentCaptureService = mContentCaptureService;
        if (contentCaptureService != null && (event == Event.ACTIVITY_PAUSED
@@ -6099,6 +6104,10 @@ public class ActivityManagerService extends IActivityManager.Stub
            updateOomAdjLocked(app, OomAdjuster.OOM_ADJ_REASON_PROCESS_BEGIN);
        }
        // Report usage as process is persistent and being started.
        mUsageStatsService.reportEvent(info.packageName, UserHandle.getUserId(app.uid),
                Event.APP_COMPONENT_USED);
        // This package really, really can not be stopped.
        try {
            AppGlobals.getPackageManager().setPackageStoppedState(
+9 −0
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import android.app.AppOpsManager;
import android.app.BroadcastOptions;
import android.app.IApplicationThread;
import android.app.PendingIntent;
import android.app.usage.UsageEvents.Event;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.IIntentReceiver;
@@ -52,6 +53,7 @@ import android.os.SystemClock;
import android.os.Trace;
import android.os.UserHandle;
import android.permission.IPermissionManager;
import android.text.TextUtils;
import android.util.EventLog;
import android.util.Slog;
import android.util.SparseIntArray;
@@ -1634,6 +1636,13 @@ public final class BroadcastQueue {
                    brOptions.getTemporaryAppAllowlistReason());
        }

        // Report that a component is used for explicit broadcasts.
        if (!r.intent.isExcludingStopped() && r.curComponent != null
                && !TextUtils.equals(r.curComponent.getPackageName(), r.callerPackage)) {
            mService.mUsageStatsService.reportEvent(
                    r.curComponent.getPackageName(), r.userId, Event.APP_COMPONENT_USED);
        }

        // Broadcast is being executed, its package can't be stopped.
        try {
            AppGlobals.getPackageManager().setPackageStoppedState(
+8 −0
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import android.app.AppOpsManager;
import android.app.ApplicationExitInfo;
import android.app.ContentProviderHolder;
import android.app.IApplicationThread;
import android.app.usage.UsageEvents.Event;
import android.content.ComponentName;
import android.content.ContentProvider;
import android.content.ContentResolver;
@@ -57,6 +58,7 @@ import android.os.RemoteException;
import android.os.SystemClock;
import android.os.UserHandle;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Log;
import android.util.Slog;
@@ -412,6 +414,12 @@ public class ContentProviderHelper {
                    final long origId = Binder.clearCallingIdentity();

                    try {
                        if (!TextUtils.equals(cpr.appInfo.packageName, callingPackage)) {
                            // Report component used since a content provider is being bound.
                            mService.mUsageStatsService.reportEvent(
                                    cpr.appInfo.packageName, userId, Event.APP_COMPONENT_USED);
                        }

                        // Content provider is now in use, its package can't be stopped.
                        try {
                            checkTime(startTime,
Loading