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

Commit 3a001809 authored by Julia Reynolds's avatar Julia Reynolds
Browse files

Fix a T -> T restore issue

For pkgs that aren't preinstalled, we were erroneously
migrating notif settings to the permission system, which
was bad because we didn't have notif data in the backup to
migrate.

Test: PreferencesHelperTest
Test: manual - restore a T backup onto T, check B&R package notif settings
(not overwritten) & monitor logs - no processes killed because of denying
the permission
Bug: 218990774

Change-Id: I9fc0e8abeb6b1fbb54dfd4a5d45e6b3dabce04e9
parent deb87aab
Loading
Loading
Loading
Loading
+4 −1
Original line number Diff line number Diff line
@@ -391,6 +391,7 @@ public class PreferencesHelper implements RankingConfig {

                            if (migrateToPermission) {
                                r.importance = appImportance;
                                r.migrateToPm = true;
                                if (r.uid != UNKNOWN_UID) {
                                    // Don't call into permission system until we have a valid uid
                                    PackagePermission pkgPerm = new PackagePermission(
@@ -2575,7 +2576,7 @@ public class PreferencesHelper implements RankingConfig {
                        synchronized (mPackagePreferences) {
                            mPackagePreferences.put(packagePreferencesKey(r.pkg, r.uid), r);
                        }
                        if (mPermissionHelper.isMigrationEnabled()) {
                        if (mPermissionHelper.isMigrationEnabled() && r.migrateToPm) {
                            try {
                                PackagePermission p = new PackagePermission(
                                        r.pkg, UserHandle.getUserId(r.uid),
@@ -2841,6 +2842,8 @@ public class PreferencesHelper implements RankingConfig {
        boolean userDemotedMsgApp = false;
        boolean hasSentValidBubble = false;

        boolean migrateToPm = false;

        Delegate delegate = null;
        ArrayMap<String, NotificationChannel> channels = new ArrayMap<>();
        Map<String, NotificationChannelGroup> groups = new ConcurrentHashMap<>();
+67 −0
Original line number Diff line number Diff line
@@ -788,6 +788,73 @@ public class PreferencesHelperTest extends UiServiceTestCase {
        verify(mPermissionHelper, never()).setNotificationPermission(any());
    }

    @Test
    public void testReadXml_oldXml_migration_NoUid() throws Exception {
        when(mPermissionHelper.isMigrationEnabled()).thenReturn(true);
        mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper,
                mPermissionHelper, mLogger, mAppOpsManager, mStatsEventBuilderFactory);

        when(mPm.getPackageUidAsUser("something", USER_SYSTEM)).thenReturn(UNKNOWN_UID);
        String xml = "<ranking version=\"2\">\n"
                + "<package name=\"something\" show_badge=\"true\">\n"
                + "<channel id=\"idn\" name=\"name\" importance=\"2\"/>\n"
                + "</package>\n"
                + "</ranking>\n";
        NotificationChannel idn = new NotificationChannel("idn", "name", IMPORTANCE_LOW);
        idn.setSound(null, new AudioAttributes.Builder()
                .setUsage(USAGE_NOTIFICATION)
                .setContentType(CONTENT_TYPE_SONIFICATION)
                .setFlags(0)
                .build());
        idn.setShowBadge(false);

        loadByteArrayXml(xml.getBytes(), true, USER_SYSTEM);
        verify(mPermissionHelper, never()).setNotificationPermission(any());

        when(mPm.getPackageUidAsUser("something", USER_SYSTEM)).thenReturn(1234);
        final ApplicationInfo app = new ApplicationInfo();
        app.targetSdkVersion = Build.VERSION_CODES.N_MR1 + 1;
        when(mPm.getApplicationInfoAsUser(eq("something"), anyInt(), anyInt())).thenReturn(app);

        mHelper.onPackagesChanged(false, 0, new String[] {"something"}, new int[] {1234});


        verify(mPermissionHelper, times(1)).setNotificationPermission(any());
    }

    @Test
    public void testReadXml_newXml_noMigration_NoUid() throws Exception {
        when(mPermissionHelper.isMigrationEnabled()).thenReturn(true);
        mHelper = new PreferencesHelper(getContext(), mPm, mHandler, mMockZenModeHelper,
                mPermissionHelper, mLogger, mAppOpsManager, mStatsEventBuilderFactory);

        when(mPm.getPackageUidAsUser("something", USER_SYSTEM)).thenReturn(UNKNOWN_UID);
        String xml = "<ranking version=\"3\">\n"
                + "<package name=\"something\" show_badge=\"true\">\n"
                + "<channel id=\"idn\" name=\"name\" importance=\"2\"/>\n"
                + "</package>\n"
                + "</ranking>\n";
        NotificationChannel idn = new NotificationChannel("idn", "name", IMPORTANCE_LOW);
        idn.setSound(null, new AudioAttributes.Builder()
                .setUsage(USAGE_NOTIFICATION)
                .setContentType(CONTENT_TYPE_SONIFICATION)
                .setFlags(0)
                .build());
        idn.setShowBadge(false);

        loadByteArrayXml(xml.getBytes(), true, USER_SYSTEM);

        when(mPm.getPackageUidAsUser("something", USER_SYSTEM)).thenReturn(1234);
        final ApplicationInfo app = new ApplicationInfo();
        app.targetSdkVersion = Build.VERSION_CODES.N_MR1 + 1;
        when(mPm.getApplicationInfoAsUser(eq("something"), anyInt(), anyInt())).thenReturn(app);

        mHelper.onPackagesChanged(false, 0, new String[] {"something"}, new int[] {1234});


        verify(mPermissionHelper, never()).setNotificationPermission(any());
    }

    @Test
    public void testChannelXmlForNonBackup_postMigration() throws Exception {
        when(mPermissionHelper.isMigrationEnabled()).thenReturn(true);