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

Commit 645ed07f authored by Matías Hernández's avatar Matías Hernández
Browse files

Include user profiles when calculating areChannelsBypassingDnd

This also means we need to syncChannelsBypassingDnd() whenever users are removed, in case the removed user was a profile of the current one.

(Also some minor cleanup in PreferencesHelper -- dead code related to invalid shortcuts -- and PreferencesHelperTest).

Fixes: 291874057
Test: atest PreferencesHelperTest
Change-Id: I87e9a1c62a90bdcbad42ea8abfa4db87f706ea15
parent 705e4190
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -212,6 +212,11 @@ public class IntArray implements Cloneable {
        return -1;
    }

    /** Returns {@code true} if this array contains the specified value. */
    public boolean contains(int value) {
        return indexOf(value) != -1;
    }

    /**
     * Removes the value at the specified index from this array.
     */
+3 −0
Original line number Diff line number Diff line
@@ -55,9 +55,11 @@ public class IntArrayTest {
        a.add(5, 20);
        assertThat(a.get(5)).isEqualTo(20);
        assertThat(a.indexOf(20)).isEqualTo(5);
        assertThat(a.contains(20)).isTrue();
        verify(a, 1, 2, 0, 0, 0, 20, 10, 0, 0);

        assertThat(a.indexOf(99)).isEqualTo(-1);
        assertThat(a.contains(99)).isFalse();

        a.resize(15);
        a.set(14, 30);
@@ -71,6 +73,7 @@ public class IntArrayTest {
        backingArray[2] = 30;
        verify(a, backingArray);
        assertThat(a.indexOf(30)).isEqualTo(2);
        assertThat(a.contains(30)).isTrue();

        a.resize(2);
        assertThat(backingArray[2]).isEqualTo(0);
+3 −2
Original line number Diff line number Diff line
@@ -1926,6 +1926,7 @@ public class NotificationManagerService extends SystemService {
                mConditionProviders.onUserRemoved(userId);
                mAssistants.onUserRemoved(userId);
                mHistoryManager.onUserRemoved(userId);
                mPreferencesHelper.syncChannelsBypassingDnd();
                handleSavePolicyFile();
            } else if (action.equals(Intent.ACTION_USER_UNLOCKED)) {
                final int userId = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, USER_NULL);
@@ -2377,6 +2378,7 @@ public class NotificationManagerService extends SystemService {
        });
        mPermissionHelper = permissionHelper;
        mNotificationChannelLogger = channelLogger;
        mUserProfiles.updateCache(getContext());
        mPreferencesHelper = new PreferencesHelper(getContext(),
                mPackageManagerClient,
                mRankingHandler,
@@ -2385,6 +2387,7 @@ public class NotificationManagerService extends SystemService {
                mPermissionManager,
                mNotificationChannelLogger,
                mAppOps,
                mUserProfiles,
                new SysUiStatsEvent.BuilderFactory(),
                mShowReviewPermissionsNotification);
        mRankingHelper = new RankingHelper(getContext(),
@@ -2440,8 +2443,6 @@ public class NotificationManagerService extends SystemService {
        mZenModeHelper.initZenMode();
        mInterruptionFilter = mZenModeHelper.getZenModeListenerInterruptionFilter();
        mUserProfiles.updateCache(getContext());
        if (mPackageManagerClient.hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
            telephonyManager.listen(new PhoneStateListener() {
                @Override
+12 −22
Original line number Diff line number Diff line
@@ -38,7 +38,6 @@ import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.UserIdInt;
import android.app.ActivityManager;
import android.app.AppOpsManager;
import android.app.Notification;
import android.app.NotificationChannel;
@@ -53,7 +52,6 @@ import android.content.pm.ParceledListSlice;
import android.content.pm.UserInfo;
import android.metrics.LogMaker;
import android.net.Uri;
import android.os.Binder;
import android.os.Build;
import android.os.Process;
import android.os.UserHandle;
@@ -114,7 +112,6 @@ public class PreferencesHelper implements RankingConfig {
    private static final int XML_VERSION_REVIEW_PERMISSIONS_NOTIFICATION = 4;
    @VisibleForTesting
    static final int UNKNOWN_UID = UserHandle.USER_NULL;
    private static final String NON_BLOCKABLE_CHANNEL_DELIM = ":";

    @VisibleForTesting
    static final int NOTIFICATION_CHANNEL_COUNT_LIMIT = 5000;
@@ -196,6 +193,7 @@ public class PreferencesHelper implements RankingConfig {
    private final PermissionManager mPermissionManager;
    private final NotificationChannelLogger mNotificationChannelLogger;
    private final AppOpsManager mAppOps;
    private final ManagedServices.UserProfiles mUserProfiles;

    private SparseBooleanArray mBadgingEnabled;
    private SparseBooleanArray mBubblesEnabled;
@@ -204,14 +202,12 @@ public class PreferencesHelper implements RankingConfig {
    private boolean mIsMediaNotificationFilteringEnabled = DEFAULT_MEDIA_NOTIFICATION_FILTERING;
    private boolean mCurrentUserHasChannelsBypassingDnd;
    private boolean mHideSilentStatusBarIcons = DEFAULT_HIDE_SILENT_STATUS_BAR_ICONS;
    private boolean mShowReviewPermissionsNotification;

    private boolean mAllowInvalidShortcuts = false;
    private final boolean mShowReviewPermissionsNotification;

    public PreferencesHelper(Context context, PackageManager pm, RankingHandler rankingHandler,
            ZenModeHelper zenHelper, PermissionHelper permHelper, PermissionManager permManager,
            NotificationChannelLogger notificationChannelLogger,
            AppOpsManager appOpsManager,
            AppOpsManager appOpsManager, ManagedServices.UserProfiles userProfiles,
            SysUiStatsEvent.BuilderFactory statsEventBuilderFactory,
            boolean showReviewPermissionsNotification) {
        mContext = context;
@@ -222,6 +218,7 @@ public class PreferencesHelper implements RankingConfig {
        mPm = pm;
        mNotificationChannelLogger = notificationChannelLogger;
        mAppOps = appOpsManager;
        mUserProfiles = userProfiles;
        mStatsEventBuilderFactory = statsEventBuilderFactory;
        mShowReviewPermissionsNotification = showReviewPermissionsNotification;

@@ -435,7 +432,7 @@ public class PreferencesHelper implements RankingConfig {
                channel.getConversationId() != null &&
                        channel.getConversationId().contains(
                                PLACEHOLDER_CONVERSATION_ID);
        return mAllowInvalidShortcuts || (!mAllowInvalidShortcuts && !isInvalidShortcutChannel);
        return !isInvalidShortcutChannel;
    }

    private boolean isDeletionOk(NotificationChannel nc) {
@@ -1790,8 +1787,9 @@ public class PreferencesHelper implements RankingConfig {
     * Syncs {@link #mCurrentUserHasChannelsBypassingDnd} with the current user's notification
     * policy before updating. Must be called:
     * <ul>
     *     <li>On system init, after channels and DND configurations are loaded.</li>
     *     <li>When the current user changes, after the corresponding DND config is loaded.</li>
     *     <li>On system init, after channels and DND configurations are loaded.
     *     <li>When the current user is switched, after the corresponding DND config is loaded.
     *     <li>If users are removed (the removed user could've been a profile of the current one).
     * </ul>
     */
    void syncChannelsBypassingDnd() {
@@ -1805,20 +1803,19 @@ public class PreferencesHelper implements RankingConfig {
    /**
     * Updates the user's NotificationPolicy based on whether the current userId has channels
     * bypassing DND. It should be called whenever a channel is created, updated, or deleted, or
     * when the current user is switched.
     * when the current user (or its profiles) change.
     */
    private void updateCurrentUserHasChannelsBypassingDnd(int callingUid,
            boolean fromSystemOrSystemUi) {
        ArraySet<Pair<String, Integer>> candidatePkgs = new ArraySet<>();

        final int currentUserId = getCurrentUser();
        final IntArray currentUserIds = mUserProfiles.getCurrentProfileIds();
        synchronized (mPackagePreferences) {
            final int numPackagePreferences = mPackagePreferences.size();
            for (int i = 0; i < numPackagePreferences; i++) {
                final PackagePreferences r = mPackagePreferences.valueAt(i);
                // Package isn't associated with the current userId
                if (currentUserId != UserHandle.getUserId(r.uid)) {
                    continue;
                if (!currentUserIds.contains(UserHandle.getUserId(r.uid))) {
                    continue; // Package isn't associated with any profile of the current userId.
                }

                for (NotificationChannel channel : r.channels.values()) {
@@ -1842,13 +1839,6 @@ public class PreferencesHelper implements RankingConfig {
        }
    }

    private int getCurrentUser() {
        final long identity = Binder.clearCallingIdentity();
        int currentUserId = ActivityManager.getCurrentUser();
        Binder.restoreCallingIdentity(identity);
        return currentUserId;
    }

    private boolean channelIsLiveLocked(PackagePreferences pkgPref, NotificationChannel channel) {
        // Channel is in a group that's blocked
        if (isGroupBlocked(pkgPref.pkg, pkgPref.uid, channel.getGroup())) {
+63 −101

File changed.

Preview size limit exceeded, changes collapsed.