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

Commit e734d06f authored by Xin Guan's avatar Xin Guan
Browse files

Fix the breakage for the new REPORT_USAGE_STATS permission check

Fall back to the previous behavior when the new REPORT_USAGE_STATS
flag is default to ensure no app compact breakage.

Bug: 305648037
Test: share a picture in photo, make sure no crash.
      atest CtsUsageStatsTest
Change-Id: I3311ed93bc66cf749092b1add4cf6cba36ecfdd2
parent 38324149
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;