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

Commit 4b230198 authored by Matías Hernández's avatar Matías Hernández
Browse files

Lock state of NotificationComparator while sorting in RankingHelper

When the default dialer or default SMS package is changed, the NotificationComparator and NotificationMessagingUtil update their cached copy of this value, which is used to evaluate isImportantOngoing and isImportantMessaging (two of the criteria involved in the comparison). However, if this is changed _while a sort() is ongoing_, then the results of element comparison could potentially be inconsistent whenever notifications posted by the new or previous package are involved. In the worst case this could result in a system crash ("IllegalArgumentException: Comparison method violates its general contract!").

This CL introduces a lock object in NotificationComparator that should be acquired and held for the full duration of the sort. Default package changes that arrive within this time will be processed afterwards.

Fixes: 293249306
Test: atest NotificationComparatorTest -- testChangeDialerPackageWhileSorting() crashes without the synchronized block around records.sort(comparator).
Change-Id: I67251437ae827c39b135f5d45cfe682aa852d09a
parent 701e4404
Loading
Loading
Loading
Loading
+15 −8
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package com.android.internal.util;

import android.annotation.Nullable;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
@@ -39,10 +40,12 @@ public class NotificationMessagingUtil {

    private static final String DEFAULT_SMS_APP_SETTING = Settings.Secure.SMS_DEFAULT_APPLICATION;
    private final Context mContext;
    private SparseArray<String> mDefaultSmsApp = new SparseArray<>();
    private final SparseArray<String> mDefaultSmsApp = new SparseArray<>();
    private final Object mStateLock;

    public NotificationMessagingUtil(Context context) {
    public NotificationMessagingUtil(Context context, @Nullable Object stateLock) {
        mContext = context;
        mStateLock = stateLock != null ? stateLock : new Object();
        mContext.getContentResolver().registerContentObserver(
                Settings.Secure.getUriFor(DEFAULT_SMS_APP_SETTING), false, mSmsContentObserver);
    }
@@ -63,16 +66,20 @@ public class NotificationMessagingUtil {
    private boolean isDefaultMessagingApp(StatusBarNotification sbn) {
        final int userId = sbn.getUserId();
        if (userId == UserHandle.USER_NULL || userId == UserHandle.USER_ALL) return false;
        synchronized (mStateLock) {
            if (mDefaultSmsApp.get(userId) == null) {
                cacheDefaultSmsApp(userId);
            }
            return Objects.equals(mDefaultSmsApp.get(userId), sbn.getPackageName());
        }
    }

    private void cacheDefaultSmsApp(int userId) {
        mDefaultSmsApp.put(userId, Settings.Secure.getStringForUser(
                mContext.getContentResolver(),
                Settings.Secure.SMS_DEFAULT_APPLICATION, userId));
        String smsApp = Settings.Secure.getStringForUser(mContext.getContentResolver(),
                Settings.Secure.SMS_DEFAULT_APPLICATION, userId);
        synchronized (mStateLock) {
            mDefaultSmsApp.put(userId, smsApp);
        }
    }

    private final ContentObserver mSmsContentObserver = new ContentObserver(
+3 −3
Original line number Diff line number Diff line
@@ -24,11 +24,11 @@ import com.android.internal.logging.UiEventLoggerImpl;
import com.android.internal.util.NotificationMessagingUtil;
import com.android.internal.widget.LockPatternUtils;

import javax.inject.Singleton;

import dagger.Module;
import dagger.Provides;

import javax.inject.Singleton;

/**
 * Provides items imported from com.android.internal.
 */
@@ -51,7 +51,7 @@ public class AndroidInternalsModule {
    /** */
    @Provides
    public NotificationMessagingUtil provideNotificationMessagingUtil(Context context) {
        return new NotificationMessagingUtil(context);
        return new NotificationMessagingUtil(context, null);
    }

    /** Provides an instance of {@link com.android.internal.logging.UiEventLogger} */
+16 −5
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.telecom.TelecomManager;

import com.android.internal.os.BackgroundThread;
import com.android.internal.util.NotificationMessagingUtil;

import java.util.Comparator;
@@ -33,18 +34,23 @@ import java.util.Objects;
/**
 * Sorts notifications individually into attention-relevant order.
 */
public class NotificationComparator
        implements Comparator<NotificationRecord> {
class NotificationComparator implements Comparator<NotificationRecord> {

    private final Context mContext;
    private final NotificationMessagingUtil mMessagingUtil;
    private String mDefaultPhoneApp;

    /**
     * Lock that must be held during a sort() call that uses this {@link Comparator}, AND to make
     * any changes to the state of this object that could affect the results of {@link #compare}.
     */
    public final Object mStateLock = new Object();

    public NotificationComparator(Context context) {
        mContext = context;
        mContext.registerReceiver(mPhoneAppBroadcastReceiver,
                new IntentFilter(TelecomManager.ACTION_DEFAULT_DIALER_CHANGED));
        mMessagingUtil = new NotificationMessagingUtil(mContext);
        mMessagingUtil = new NotificationMessagingUtil(mContext, mStateLock);
    }

    @Override
@@ -212,8 +218,13 @@ public class NotificationComparator
    private final BroadcastReceiver mPhoneAppBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            BackgroundThread.getExecutor().execute(() -> {
                synchronized (mStateLock) {
                    mDefaultPhoneApp =
                    intent.getStringExtra(TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME);
                            intent.getStringExtra(
                                    TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME);
                }
            });
        }
    };
}
+5 −2
Original line number Diff line number Diff line
@@ -103,8 +103,11 @@ public class RankingHelper {
            notificationList.get(i).setGlobalSortKey(null);
        }

        // rank each record individually
        Collections.sort(notificationList, mPreliminaryComparator);
        // Rank each record individually.
        // Lock comparator state for consistent compare() results.
        synchronized (mPreliminaryComparator.mStateLock) {
            notificationList.sort(mPreliminaryComparator);
        }

        synchronized (mProxyByGroupTmp) {
            // record individual ranking result and nominate proxies for each group
+1 −1
Original line number Diff line number Diff line
@@ -55,7 +55,7 @@ public class ZenModeFiltering {

    public ZenModeFiltering(Context context) {
        mContext = context;
        mMessagingUtil = new NotificationMessagingUtil(mContext);
        mMessagingUtil = new NotificationMessagingUtil(mContext, null);
    }

    public ZenModeFiltering(Context context, NotificationMessagingUtil messagingUtil) {
Loading