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

Commit c468bba1 authored by Chloris Kuo's avatar Chloris Kuo
Browse files

Reset notification channel importance

Test: atest, manual test on device
Change-Id: I290ce116ede1538354e8444b76ec69de79e338f2
parent 6365c0e7
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -96,6 +96,8 @@ interface INotificationManager
    NotificationChannelGroup getPopulatedNotificationChannelGroupForPackage(String pkg, int uid, String groupId, boolean includeDeleted);
    void updateNotificationChannelGroupForPackage(String pkg, int uid, in NotificationChannelGroup group);
    void updateNotificationChannelForPackage(String pkg, int uid, in NotificationChannel channel);
    void unlockNotificationChannel(String pkg, int uid, String channelId);
    void unlockAllNotificationChannels();
    NotificationChannel getNotificationChannel(String callingPkg, int userId, String pkg, String channelId);
    NotificationChannel getConversationNotificationChannel(String callingPkg, int userId, String pkg, String channelId, boolean returnParentIfNoConversationChannel, String conversationId);
    void createConversationNotificationChannelForPackage(String pkg, int uid, in NotificationChannel parentChannel, String conversationId);
+14 −0
Original line number Diff line number Diff line
@@ -3525,6 +3525,20 @@ public class NotificationManagerService extends SystemService {
            updateNotificationChannelInt(pkg, uid, channel, false);
        }

        @Override
        public void unlockNotificationChannel(String pkg, int uid, String channelId) {
            checkCallerIsSystemOrSystemUiOrShell("Caller not system or sysui or shell");
            mPreferencesHelper.unlockNotificationChannelImportance(pkg, uid, channelId);
            handleSavePolicyFile();
        }

        @Override
        public void unlockAllNotificationChannels() {
            checkCallerIsSystem();
            mPreferencesHelper.unlockAllNotificationChannels();
            handleSavePolicyFile();
        }

        @Override
        public ParceledListSlice<NotificationChannel> getNotificationChannelsForPackage(String pkg,
                int uid, boolean includeDeleted) {
+30 −0
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.server.notification;

import static android.app.AppOpsManager.OP_SYSTEM_ALERT_WINDOW;
import static android.app.NotificationChannel.PLACEHOLDER_CONVERSATION_ID;
import static android.app.NotificationChannel.USER_LOCKED_IMPORTANCE;
import static android.app.NotificationManager.BUBBLE_PREFERENCE_ALL;
import static android.app.NotificationManager.BUBBLE_PREFERENCE_NONE;
import static android.app.NotificationManager.IMPORTANCE_NONE;
@@ -955,6 +956,23 @@ public class PreferencesHelper implements RankingConfig {
        channel.unlockFields(channel.getUserLockedFields());
    }

    void unlockNotificationChannelImportance(String pkg, int uid, String updatedChannelId) {
        Objects.requireNonNull(updatedChannelId);
        synchronized (mPackagePreferences) {
            PackagePreferences r = getOrCreatePackagePreferencesLocked(pkg, uid);
            if (r == null) {
                throw new IllegalArgumentException("Invalid package");
            }

            NotificationChannel channel = r.channels.get(updatedChannelId);
            if (channel == null || channel.isDeleted()) {
                throw new IllegalArgumentException("Channel does not exist");
            }
            channel.unlockFields(USER_LOCKED_IMPORTANCE);
        }
    }


    @Override
    public void updateNotificationChannel(String pkg, int uid, NotificationChannel updatedChannel,
            boolean fromUser) {
@@ -2389,6 +2407,18 @@ public class PreferencesHelper implements RankingConfig {
        return mBadgingEnabled.get(userId, DEFAULT_SHOW_BADGE);
    }

    public void unlockAllNotificationChannels() {
        synchronized (mPackagePreferences) {
            final int numPackagePreferences = mPackagePreferences.size();
            for (int i = 0; i < numPackagePreferences; i++) {
                final PackagePreferences r = mPackagePreferences.valueAt(i);
                for (NotificationChannel channel : r.channels.values()) {
                    channel.unlockFields(USER_LOCKED_IMPORTANCE);
                }
            }
        }
    }

    private void updateConfig() {
        mRankingHandler.requestSort();
    }
+37 −0
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ import static android.app.AppOpsManager.MODE_ALLOWED;
import static android.app.AppOpsManager.MODE_DEFAULT;
import static android.app.AppOpsManager.OP_SYSTEM_ALERT_WINDOW;
import static android.app.NotificationChannel.CONVERSATION_CHANNEL_ID_FORMAT;
import static android.app.NotificationChannel.USER_LOCKED_IMPORTANCE;
import static android.app.NotificationManager.BUBBLE_PREFERENCE_ALL;
import static android.app.NotificationManager.BUBBLE_PREFERENCE_NONE;
import static android.app.NotificationManager.BUBBLE_PREFERENCE_SELECTED;
@@ -3796,4 +3797,40 @@ public class PreferencesHelperTest extends UiServiceTestCase {
            }
        }
    }

    @Test
    public void testUnlockNotificationChannelImportance() {
        NotificationChannel channel = new NotificationChannel("a", "a", IMPORTANCE_LOW);
        mHelper.createNotificationChannel(PKG_O, UID_O, channel, true, false);
        channel.lockFields(USER_LOCKED_IMPORTANCE);
        assertTrue((channel.getUserLockedFields() & USER_LOCKED_IMPORTANCE) != 0);

        mHelper.unlockNotificationChannelImportance(PKG_O, UID_O, channel.getId());
        assertTrue((channel.getUserLockedFields() & USER_LOCKED_IMPORTANCE) == 0);

    }

    @Test
    public void testUnlockAllNotificationChannels() {
        NotificationChannel channelA = new NotificationChannel("a", "a", IMPORTANCE_LOW);
        mHelper.createNotificationChannel(PKG_O, UID_O, channelA, true, false);
        NotificationChannel channelB = new NotificationChannel("b", "b", IMPORTANCE_DEFAULT);
        mHelper.createNotificationChannel(PKG_P, UID_P, channelB, true, false);
        NotificationChannel channelC = new NotificationChannel("c", "c", IMPORTANCE_HIGH);
        mHelper.createNotificationChannel(PKG_P, UID_O, channelC, false, false);

        channelA.lockFields(USER_LOCKED_IMPORTANCE);
        channelB.lockFields(USER_LOCKED_IMPORTANCE);
        channelC.lockFields(USER_LOCKED_IMPORTANCE);

        assertTrue((channelA.getUserLockedFields() & USER_LOCKED_IMPORTANCE) != 0);
        assertTrue((channelB.getUserLockedFields() & USER_LOCKED_IMPORTANCE) != 0);
        assertTrue((channelC.getUserLockedFields() & USER_LOCKED_IMPORTANCE) != 0);

        mHelper.unlockAllNotificationChannels();
        assertTrue((channelA.getUserLockedFields() & USER_LOCKED_IMPORTANCE) == 0);
        assertTrue((channelB.getUserLockedFields() & USER_LOCKED_IMPORTANCE) == 0);
        assertTrue((channelC.getUserLockedFields() & USER_LOCKED_IMPORTANCE) == 0);

    }
}