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

Commit 99a9e680 authored by Varun Shah's avatar Varun Shah Committed by Android (Google) Code Review
Browse files

Merge "Fix the breakage for the new REPORT_USAGE_STATS permission check" into main

parents bfb8a426 e734d06f
Loading
Loading
Loading
Loading
+24 −15
Original line number Diff line number Diff line
@@ -2127,14 +2127,12 @@ public class UsageStatsService extends SystemService implements
        }

        private boolean canReportUsageStats() {
            final boolean isSystem = isCallingUidSystem();
            if (!Flags.reportUsageStatsPermission()) {
                // If the flag is disabled, do no check for the new permission and instead return
                // true only if the calling uid is system since System UID can always report stats.
                return isSystem;
            }
            return isSystem
                    || getContext().checkCallingPermission(Manifest.permission.REPORT_USAGE_STATS)
            if (isCallingUidSystem()) {
                // System UID can always report UsageStats
                return true;
            }

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

@@ -2627,10 +2625,13 @@ public class UsageStatsService extends SystemService implements
                return;
            }

            if (Flags.reportUsageStatsPermission()) {
                if (!canReportUsageStats()) {
                throw new SecurityException("Only the system or holders of the REPORT_USAGE_STATS"
                    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) {
@@ -2649,10 +2650,18 @@ public class UsageStatsService extends SystemService implements
        @Override
        public void reportUserInteraction(String packageName, int userId) {
            Objects.requireNonNull(packageName);
            if (Flags.reportUsageStatsPermission()) {
                if (!canReportUsageStats()) {
                throw new SecurityException("Only the system or holders of the REPORT_USAGE_STATS"
                    throw new SecurityException(
                        "Only the system or holders of the REPORT_USAGE_STATS"
                            + " permission are allowed to call reportUserInteraction");
                }
            } else {
                if (!isCallingUidSystem()) {
                    throw new SecurityException("Only system is allowed to call"
                            + " reportUserInteraction");
                }
            }

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