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

Commit b2804662 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Configure notif permission migration behavior" into tm-dev

parents 3992f2e9 351d60a9
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -2988,6 +2988,12 @@

    </string-array>

    <!-- When migrating notification settings into the permission framework, whether all existing
         apps should be marked as 'user-set' (true) or whether only the apps that have explicitly
         modified notification settings should be marked as 'user-set' (false). Users will not see
         system generated permission prompts for 'user-set' apps. -->
    <bool name="config_notificationForceUserSetOnUpgrade">true</bool>

    <!-- Default Gravity setting for the system Toast view. Equivalent to: Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM -->
    <integer name="config_toastDefaultGravity">0x00000051</integer>

+1 −0
Original line number Diff line number Diff line
@@ -4770,5 +4770,6 @@
  <java-symbol type="integer" name="config_bg_current_drain_exempted_types" />
  <java-symbol type="bool" name="config_bg_current_drain_high_threshold_by_bg_location" />
  <java-symbol type="drawable" name="ic_swap_horiz" />
  <java-symbol type="bool" name="config_notificationForceUserSetOnUpgrade" />

</resources>
+8 −1
Original line number Diff line number Diff line
@@ -630,6 +630,7 @@ public class NotificationManagerService extends SystemService {
    private int mWarnRemoteViewsSizeBytes;
    private int mStripRemoteViewsSizeBytes;
    final boolean mEnableAppSettingMigration;
    private boolean mForceUserSetOnUpgrade;
    private MetricsLogger mMetricsLogger;
    private TriPredicate<String, Integer, String> mAllowedManagedServicePackages;
@@ -2294,6 +2295,7 @@ public class NotificationManagerService extends SystemService {
        mMsgPkgsAllowedAsConvos = Set.of(getStringArrayResource(
                com.android.internal.R.array.config_notificationMsgPkgsAllowedAsConvos));
        mStatsManager = statsManager;
        mToastRateLimiter = toastRateLimiter;
@@ -2386,6 +2388,9 @@ public class NotificationManagerService extends SystemService {
        WorkerHandler handler = new WorkerHandler(Looper.myLooper());
        mForceUserSetOnUpgrade = getContext().getResources().getBoolean(
                R.bool.config_notificationForceUserSetOnUpgrade);
        init(handler, new RankingHandlerWorker(mRankingThread.getLooper()),
                AppGlobals.getPackageManager(), getContext().getPackageManager(),
                getLocalService(LightsManager.class),
@@ -2414,7 +2419,8 @@ public class NotificationManagerService extends SystemService {
                LocalServices.getService(ActivityManagerInternal.class),
                createToastRateLimiter(), new PermissionHelper(LocalServices.getService(
                        PermissionManagerServiceInternal.class), AppGlobals.getPackageManager(),
                        AppGlobals.getPermissionManager(), mEnableAppSettingMigration),
                        AppGlobals.getPermissionManager(), mEnableAppSettingMigration,
                        mForceUserSetOnUpgrade),
                LocalServices.getService(UsageStatsManagerInternal.class));
        publishBinderService(Context.NOTIFICATION_SERVICE, mService, /* allowIsolated= */ false,
@@ -6069,6 +6075,7 @@ public class NotificationManagerService extends SystemService {
                    pw.println("  mMaxPackageEnqueueRate=" + mMaxPackageEnqueueRate);
                    pw.println("  hideSilentStatusBar="
                            + mPreferencesHelper.shouldHideSilentStatusIcons());
                    pw.println("  mForceUserSetOnUpgrade=" + mForceUserSetOnUpgrade);
                }
                pw.println("  mArchive=" + mArchive.toString());
                mArchive.dumpImpl(pw, filter);
+12 −7
Original line number Diff line number Diff line
@@ -57,13 +57,16 @@ public final class PermissionHelper {
    private final IPermissionManager mPermManager;
    // TODO (b/194833441): Remove when the migration is enabled
    private final boolean mMigrationEnabled;
    private final boolean mForceUserSetOnUpgrade;

    public PermissionHelper(PermissionManagerServiceInternal pmi, IPackageManager packageManager,
            IPermissionManager permManager, boolean migrationEnabled) {
            IPermissionManager permManager, boolean migrationEnabled,
            boolean forceUserSetOnUpgrade) {
        mPmi = pmi;
        mPackageManager = packageManager;
        mPermManager = permManager;
        mMigrationEnabled = migrationEnabled;
        mForceUserSetOnUpgrade = forceUserSetOnUpgrade;
    }

    public boolean isMigrationEnabled() {
@@ -223,8 +226,9 @@ public final class PermissionHelper {
            return;
        }
        if (!isPermissionFixed(pkgPerm.packageName, pkgPerm.userId)) {
            boolean userSet = mForceUserSetOnUpgrade ? true : pkgPerm.userModifiedSettings;
            setNotificationPermission(pkgPerm.packageName, pkgPerm.userId, pkgPerm.granted,
                    pkgPerm.userSet, !pkgPerm.userSet);
                    userSet, !userSet);
        }
    }

@@ -305,13 +309,13 @@ public final class PermissionHelper {
        public final String packageName;
        public final @UserIdInt int userId;
        public final boolean granted;
        public final boolean userSet;
        public final boolean userModifiedSettings;

        public PackagePermission(String pkg, int userId, boolean granted, boolean userSet) {
            this.packageName = pkg;
            this.userId = userId;
            this.granted = granted;
            this.userSet = userSet;
            this.userModifiedSettings = userSet;
        }

        @Override
@@ -319,13 +323,14 @@ public final class PermissionHelper {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            PackagePermission that = (PackagePermission) o;
            return userId == that.userId && granted == that.granted && userSet == that.userSet
            return userId == that.userId && granted == that.granted && userModifiedSettings
                    == that.userModifiedSettings
                    && Objects.equals(packageName, that.packageName);
        }

        @Override
        public int hashCode() {
            return Objects.hash(packageName, userId, granted, userSet);
            return Objects.hash(packageName, userId, granted, userModifiedSettings);
        }

        @Override
@@ -334,7 +339,7 @@ public final class PermissionHelper {
                    "packageName='" + packageName + '\'' +
                    ", userId=" + userId +
                    ", granted=" + granted +
                    ", userSet=" + userSet +
                    ", userSet=" + userModifiedSettings +
                    '}';
        }
    }
+22 −2
Original line number Diff line number Diff line
@@ -88,7 +88,7 @@ public class PermissionHelperTest extends UiServiceTestCase {
    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        mPermissionHelper = new PermissionHelper(mPmi, mPackageManager, mPermManager, true);
        mPermissionHelper = new PermissionHelper(mPmi, mPackageManager, mPermManager, true, false);
        PackageInfo testPkgInfo = new PackageInfo();
        testPkgInfo.requestedPermissions = new String[]{ Manifest.permission.POST_NOTIFICATIONS };
        when(mPackageManager.getPackageInfo(anyString(), anyLong(), anyInt()))
@@ -100,7 +100,7 @@ public class PermissionHelperTest extends UiServiceTestCase {
    public void testMethodsThrowIfMigrationDisabled() throws IllegalAccessException,
            InvocationTargetException {
        PermissionHelper permHelper =
                new PermissionHelper(mPmi, mPackageManager, mPermManager, false);
                new PermissionHelper(mPmi, mPackageManager, mPermManager, false, false);

        Method[] allMethods = PermissionHelper.class.getDeclaredMethods();
        for (Method method : allMethods) {
@@ -301,6 +301,26 @@ public class PermissionHelperTest extends UiServiceTestCase {
                FLAG_PERMISSION_USER_SET, true, 10);
    }

    @Test
    public void testSetNotificationPermission_pkgPerm_grantedByDefaultPermSet_allUserSet()
            throws Exception {
        mPermissionHelper = new PermissionHelper(mPmi, mPackageManager, mPermManager, true, true);
        when(mPmi.checkPermission(anyString(), anyString(), anyInt()))
                .thenReturn(PERMISSION_DENIED);
        when(mPermManager.getPermissionFlags(anyString(),
                eq(Manifest.permission.POST_NOTIFICATIONS),
                anyInt())).thenReturn(FLAG_PERMISSION_GRANTED_BY_DEFAULT);
        PermissionHelper.PackagePermission pkgPerm = new PermissionHelper.PackagePermission(
                "pkg", 10, true, false);

        mPermissionHelper.setNotificationPermission(pkgPerm);
        verify(mPermManager).grantRuntimePermission(
                "pkg", Manifest.permission.POST_NOTIFICATIONS, 10);
        verify(mPermManager).updatePermissionFlags("pkg", Manifest.permission.POST_NOTIFICATIONS,
                FLAG_PERMISSION_USER_SET | FLAG_PERMISSION_REVIEW_REQUIRED,
                FLAG_PERMISSION_USER_SET, true, 10);
    }

    @Test
    public void testSetNotificationPermission_revokeUserSet() throws Exception {
        when(mPmi.checkPermission(anyString(), anyString(), anyInt()))