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

Commit ad842da7 authored by Varun Shah's avatar Varun Shah
Browse files

Update certain APIs to require the REPORT_USAGE_STATS permission.

Add requirement of either being the system or having the
REPORT_USAGE_STATS permission in order to call any report events related
APIs. This includes the reportChooserSelection and reportUserInteraction
APIs.

Also add the permission to Shell so the APIs can be tested via CTS.

Bug: 296056771
Bug: 218679369
Test: atest CtsUsageStatsTest
Change-Id: I3b28e00b9da73cd4bc051f3c5e03e29a734a1df0
parent 1983fd77
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -1106,6 +1106,7 @@ public final class UsageStatsManager {
     * <p><em>This method is only for use by the system</em>
     * @hide
     */
    @RequiresPermission(android.Manifest.permission.REPORT_USAGE_STATS)
    public void reportUserInteraction(@NonNull String packageName, int userId) {
        try {
            mService.reportUserInteraction(packageName, userId);
@@ -1396,6 +1397,7 @@ public final class UsageStatsManager {
     * {@link UsageEvents}
     * @hide
     */
    @RequiresPermission(android.Manifest.permission.REPORT_USAGE_STATS)
    public void reportChooserSelection(String packageName, int userId, String contentType,
                                       String[] annotations, String action) {
        try {
+1 −0
Original line number Diff line number Diff line
@@ -167,6 +167,7 @@
    <uses-permission android:name="android.permission.FORCE_BACK" />
    <uses-permission android:name="android.permission.BATTERY_STATS" />
    <uses-permission android:name="android.permission.PACKAGE_USAGE_STATS" />
    <uses-permission android:name="android.permission.REPORT_USAGE_STATS" />
    <uses-permission android:name="android.permission.INTERNAL_SYSTEM_WINDOW" />
    <uses-permission android:name="android.permission.INJECT_EVENTS" />
    <uses-permission android:name="android.permission.RETRIEVE_WINDOW_CONTENT" />
+24 −8
Original line number Diff line number Diff line
@@ -2061,6 +2061,15 @@ public class UsageStatsService extends SystemService implements
            return mode == AppOpsManager.MODE_ALLOWED;
        }

        private boolean canReportUsageStats() {
            if (isCallingUidSystem()) {
                return true; // System UID can always report UsageStats
            }

            return getContext().checkCallingPermission(Manifest.permission.REPORT_USAGE_STATS)
                    == PackageManager.PERMISSION_GRANTED;
        }

        private boolean hasObserverPermission() {
            final int callingUid = Binder.getCallingUid();
            DevicePolicyManagerInternal dpmInternal = getDpmInternal();
@@ -2541,14 +2550,19 @@ public class UsageStatsService extends SystemService implements
        @Override
        public void reportChooserSelection(@NonNull String packageName, int userId,
                @NonNull String contentType, String[] annotations, @NonNull String action) {
            if (packageName == null) {
                throw new IllegalArgumentException("Package selection must not be null.");
            }
            // A valid contentType and action must be provided for chooser selection events.
            if (contentType == null || contentType.isBlank()
                    || action == null || action.isBlank()) {
            // A valid package name, content type, and action must be provided for these events
            Objects.requireNonNull(packageName);
            Objects.requireNonNull(contentType);
            Objects.requireNonNull(action);
            if (contentType.isBlank() || action.isBlank()) {
                return;
            }

            if (!canReportUsageStats()) {
                throw new SecurityException("Only the system or holders of the REPORT_USAGE_STATS"
                        + " permission are allowed to call reportChooserSelection");
            }

            // Verify if this package exists before reporting an event for it.
            if (mPackageManagerInternal.getPackageUid(packageName, 0, userId) < 0) {
                Slog.w(TAG, "Event report user selecting an invalid package");
@@ -2566,9 +2580,11 @@ public class UsageStatsService extends SystemService implements
        @Override
        public void reportUserInteraction(String packageName, int userId) {
            Objects.requireNonNull(packageName);
            if (!isCallingUidSystem()) {
                throw new SecurityException("Only system is allowed to call reportUserInteraction");
            if (!canReportUsageStats()) {
                throw new SecurityException("Only the system or holders of the REPORT_USAGE_STATS"
                        + " permission are allowed to call reportUserInteraction");
            }

            final Event event = new Event(USER_INTERACTION, SystemClock.elapsedRealtime());
            event.mPackage = packageName;
            reportEventOrAddToQueue(userId, event);