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

Commit 2246b33f authored by Ned Burns's avatar Ned Burns
Browse files

Unify listener interfaces in NotificationListener

Now the NotificationListener is agnostic as to whether it's firing
events to the NEM, the NotifCollection, or both.

Should allow us to switch between the NEM and NotifCollection in the
future.

Also inlines the old feature flag ENABLE_CHILD_NOTIFICATIONS.

Bug: 112656837
Test: atest
Test: manual
Change-Id: Ic6a49c08db7a06c021f36c7f15c2dbd22ef27eaa
parent 92454a87
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.app.AlarmManager;
import android.app.IActivityManager;
import android.app.IWallpaperManager;
import android.app.KeyguardManager;
import android.app.NotificationManager;
import android.app.WallpaperManager;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
@@ -129,6 +130,12 @@ public class SystemServicesModule {
        return LocalBluetoothManager.create(context, bgHandler, UserHandle.ALL);
    }

    @Singleton
    @Provides
    static NotificationManager provideNotificationManager(Context context) {
        return context.getSystemService(NotificationManager.class);
    }

    @Singleton
    @Provides
    static PackageManagerWrapper providePackageManagerWrapper() {
+26 −58
Original line number Diff line number Diff line
@@ -19,9 +19,7 @@ package com.android.systemui.statusbar;
import static com.android.systemui.statusbar.RemoteInputController.processForRemoteInput;
import static com.android.systemui.statusbar.notification.NotificationEntryManager.UNDEFINED_DISMISS_REASON;
import static com.android.systemui.statusbar.phone.StatusBar.DEBUG;
import static com.android.systemui.statusbar.phone.StatusBar.ENABLE_CHILD_NOTIFICATIONS;

import android.annotation.Nullable;
import android.annotation.SuppressLint;
import android.app.NotificationManager;
import android.content.ComponentName;
@@ -33,8 +31,6 @@ import android.service.notification.StatusBarNotification;
import android.util.Log;

import com.android.systemui.dagger.qualifiers.MainHandler;
import com.android.systemui.statusbar.notification.NotificationEntryManager;
import com.android.systemui.statusbar.phone.NotificationGroupManager;
import com.android.systemui.statusbar.phone.NotificationListenerWithPlugins;

import java.util.ArrayList;
@@ -52,31 +48,33 @@ import javax.inject.Singleton;
public class NotificationListener extends NotificationListenerWithPlugins {
    private static final String TAG = "NotificationListener";

    // Dependencies:
    private final NotificationEntryManager mEntryManager;
    private final NotificationGroupManager mGroupManager;

    private final Context mContext;
    private final NotificationManager mNotificationManager;
    private final Handler mMainHandler;
    private final List<NotifServiceListener> mNotificationListeners = new ArrayList<>();
    private final ArrayList<NotificationSettingsListener> mSettingsListeners = new ArrayList<>();
    @Nullable private NotifServiceListener mDownstreamListener;

    @Inject
    public NotificationListener(Context context, @MainHandler Handler mainHandler,
            NotificationEntryManager notificationEntryManager,
            NotificationGroupManager notificationGroupManager) {
    public NotificationListener(
            Context context,
            NotificationManager notificationManager,
            @MainHandler Handler mainHandler) {
        mContext = context;
        mNotificationManager = notificationManager;
        mMainHandler = mainHandler;
        mEntryManager = notificationEntryManager;
        mGroupManager = notificationGroupManager;
    }

    public void addNotificationSettingsListener(NotificationSettingsListener listener) {
        mSettingsListeners.add(listener);
    /** Registers a listener that's notified when notifications are added/removed/etc. */
    public void addNotificationListener(NotifServiceListener listener) {
        if (mNotificationListeners.contains(listener)) {
            throw new IllegalArgumentException("Listener is already added");
        }
        mNotificationListeners.add(listener);
    }

    public void setDownstreamListener(NotifServiceListener downstreamListener) {
        mDownstreamListener = downstreamListener;
    /** Registers a listener that's notified when any notification-related settings change. */
    public void addNotificationSettingsListener(NotificationSettingsListener listener) {
        mSettingsListeners.add(listener);
    }

    @Override
@@ -102,14 +100,13 @@ public class NotificationListener extends NotificationListenerWithPlugins {
            final RankingMap completeMap = new RankingMap(newRankings.toArray(new Ranking[0]));

            for (StatusBarNotification sbn : notifications) {
                if (mDownstreamListener != null) {
                    mDownstreamListener.onNotificationPosted(sbn, completeMap);
                for (NotifServiceListener listener : mNotificationListeners) {
                    listener.onNotificationPosted(sbn, completeMap);
                }
                mEntryManager.addNotification(sbn, completeMap);
            }
        });
        NotificationManager noMan = mContext.getSystemService(NotificationManager.class);
        onSilentStatusBarIconsVisibilityChanged(noMan.shouldHideSilentStatusBarIcons());
        onSilentStatusBarIconsVisibilityChanged(
                mNotificationManager.shouldHideSilentStatusBarIcons());
    }

    @Override
@@ -120,34 +117,8 @@ public class NotificationListener extends NotificationListenerWithPlugins {
            mMainHandler.post(() -> {
                processForRemoteInput(sbn.getNotification(), mContext);

                if (mDownstreamListener != null) {
                    mDownstreamListener.onNotificationPosted(sbn, rankingMap);
                }

                String key = sbn.getKey();
                boolean isUpdate = mEntryManager.getActiveNotificationUnfiltered(key) != null;
                // In case we don't allow child notifications, we ignore children of
                // notifications that have a summary, since` we're not going to show them
                // anyway. This is true also when the summary is canceled,
                // because children are automatically canceled by NoMan in that case.
                if (!ENABLE_CHILD_NOTIFICATIONS
                        && mGroupManager.isChildInGroupWithSummary(sbn)) {
                    if (DEBUG) {
                        Log.d(TAG, "Ignoring group child due to existing summary: " + sbn);
                    }

                    // Remove existing notification to avoid stale data.
                    if (isUpdate) {
                        mEntryManager.removeNotification(key, rankingMap, UNDEFINED_DISMISS_REASON);
                    } else {
                        mEntryManager.updateRanking(rankingMap, "onNotificationPosted");
                    }
                    return;
                }
                if (isUpdate) {
                    mEntryManager.updateNotification(sbn, rankingMap);
                } else {
                    mEntryManager.addNotification(sbn, rankingMap);
                for (NotifServiceListener listener : mNotificationListeners) {
                    listener.onNotificationPosted(sbn, rankingMap);
                }
            });
        }
@@ -158,12 +129,10 @@ public class NotificationListener extends NotificationListenerWithPlugins {
            int reason) {
        if (DEBUG) Log.d(TAG, "onNotificationRemoved: " + sbn + " reason: " + reason);
        if (sbn != null && !onPluginNotificationRemoved(sbn, rankingMap)) {
            final String key = sbn.getKey();
            mMainHandler.post(() -> {
                if (mDownstreamListener != null) {
                    mDownstreamListener.onNotificationRemoved(sbn, rankingMap, reason);
                for (NotifServiceListener listener : mNotificationListeners) {
                    listener.onNotificationRemoved(sbn, rankingMap, reason);
                }
                mEntryManager.removeNotification(key, rankingMap, reason);
            });
        }
    }
@@ -179,10 +148,9 @@ public class NotificationListener extends NotificationListenerWithPlugins {
        if (rankingMap != null) {
            RankingMap r = onPluginRankingUpdate(rankingMap);
            mMainHandler.post(() -> {
                if (mDownstreamListener != null) {
                    mDownstreamListener.onNotificationRankingUpdate(rankingMap);
                for (NotifServiceListener listener : mNotificationListeners) {
                    listener.onNotificationRankingUpdate(r);
                }
                mEntryManager.updateNotificationRanking(r);
            });
        }
    }
+37 −0
Original line number Diff line number Diff line
@@ -33,6 +33,8 @@ import com.android.internal.statusbar.NotificationVisibility;
import com.android.systemui.Dependency;
import com.android.systemui.Dumpable;
import com.android.systemui.statusbar.NotificationLifetimeExtender;
import com.android.systemui.statusbar.NotificationListener;
import com.android.systemui.statusbar.NotificationListener.NotifServiceListener;
import com.android.systemui.statusbar.NotificationPresenter;
import com.android.systemui.statusbar.NotificationRemoteInputManager;
import com.android.systemui.statusbar.NotificationRemoveInterceptor;
@@ -174,6 +176,11 @@ public class NotificationEntryManager implements
        mKeyguardEnvironment = keyguardEnvironment;
    }

    /** Once called, the NEM will start processing notification events from system server. */
    public void attach(NotificationListener notificationListener) {
        notificationListener.addNotificationListener(mNotifListener);
    }

    /** Adds a {@link NotificationEntryListener}. */
    public void addNotificationEntryListener(NotificationEntryListener listener) {
        mNotificationEntryListeners.add(listener);
@@ -318,6 +325,36 @@ public class NotificationEntryManager implements
        }
    }

    private final NotifServiceListener mNotifListener = new NotifServiceListener() {
        @Override
        public void onNotificationPosted(StatusBarNotification sbn, RankingMap rankingMap) {
            final boolean isUpdate = mActiveNotifications.containsKey(sbn.getKey());
            if (isUpdate) {
                updateNotification(sbn, rankingMap);
            } else {
                addNotification(sbn, rankingMap);
            }
        }

        @Override
        public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap) {
            removeNotification(sbn.getKey(), rankingMap, UNDEFINED_DISMISS_REASON);
        }

        @Override
        public void onNotificationRemoved(
                StatusBarNotification sbn,
                RankingMap rankingMap,
                int reason) {
            removeNotification(sbn.getKey(), rankingMap, reason);
        }

        @Override
        public void onNotificationRankingUpdate(RankingMap rankingMap) {
            updateNotificationRanking(rankingMap);
        }
    };

    /**
     * Equivalent to the old NotificationData#add
     * @param entry - an entry which is prepared for display
+0 −6
Original line number Diff line number Diff line
@@ -32,7 +32,6 @@ import com.android.systemui.statusbar.NotificationLockscreenUserManager;
import com.android.systemui.statusbar.notification.collection.NotificationEntry;
import com.android.systemui.statusbar.phone.NotificationGroupManager;
import com.android.systemui.statusbar.phone.ShadeController;
import com.android.systemui.statusbar.phone.StatusBar;

import javax.inject.Inject;
import javax.inject.Singleton;
@@ -120,11 +119,6 @@ public class NotificationFilter {
            return true;
        }

        if (!StatusBar.ENABLE_CHILD_NOTIFICATIONS
                && mGroupManager.isChildInGroupWithSummary(sbn)) {
            return true;
        }

        if (getFsc().isDisclosureNotification(sbn)
                && !getFsc().isDisclosureNeededForUser(sbn.getUserId())) {
            // this is a foreground-service disclosure for a user that does not need to show one
+1 −1
Original line number Diff line number Diff line
@@ -116,7 +116,7 @@ public class NotifCollection {
        }
        mAttached = true;

        listenerService.setDownstreamListener(mNotifServiceListener);
        listenerService.addNotificationListener(mNotifServiceListener);
    }

    /**
Loading