1.源码路径:
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/NotificationListener.java
2.NotificationListener类是负责处理监听通知更新并将其传递给向用户显示的NotificationPresenter,也就是SystemUI中监听到系统通知的起点.
public class NotificationListener extends NotificationListenerWithPlugins {
private static final String TAG = "NotificationListener";
private static final boolean DEBUG = StatusBar.DEBUG;
private final Context mContext;
private final NotificationManager mNotificationManager;
private final Handler mMainHandler;
private final List<NotificationHandler> mNotificationHandlers = new ArrayList<>();
private final ArrayList<NotificationSettingsListener> mSettingsListeners = new ArrayList<>();
public NotificationListener(
Context context,
NotificationManager notificationManager,
@Main Handler mainHandler) {
mContext = context;
mNotificationManager = notificationManager;
mMainHandler = mainHandler;
}
public void addNotificationHandler(NotificationHandler handler) {
if (mNotificationHandlers.contains(handler)) {
throw new IllegalArgumentException("Listener is already added");
}
mNotificationHandlers.add(handler);
}
public void addNotificationSettingsListener(NotificationSettingsListener listener) {
mSettingsListeners.add(listener);
}
@Override
public void onListenerConnected() {
if (DEBUG) Log.d(TAG, "onListenerConnected");
onPluginConnected();
final StatusBarNotification[] notifications = getActiveNotifications();
if (notifications == null) {
Log.w(TAG, "onListenerConnected unable to get active notifications.");
return;
}
final RankingMap currentRanking = getCurrentRanking();
mMainHandler.post(() -> {
final List<Ranking> newRankings = new ArrayList<>();
for (StatusBarNotification sbn : notifications) {
newRankings.add(getRankingOrTemporaryStandIn(currentRanking, sbn.getKey()));
}
final RankingMap completeMap = new RankingMap(newRankings.toArray(new Ranking[0]));
for (StatusBarNotification sbn : notifications) {
for (NotificationHandler listener : mNotificationHandlers) {
listener.onNotificationPosted(sbn, completeMap);
}
}
for (NotificationHandler listener : mNotificationHandlers) {
listener.onNotificationsInitialized();
}
});
onSilentStatusBarIconsVisibilityChanged(
mNotificationManager.shouldHideSilentStatusBarIcons());
}
@Override
public void onNotificationPosted(final StatusBarNotification sbn,
final RankingMap rankingMap) {
if (DEBUG) Log.d(TAG, "onNotificationPosted: " + sbn);
/*
1.当收到通知时,会调用该方法,然后把通知Handler更新界面,所以可以在这个屏蔽通知的显示,可以通过参数设置,或者通过设置系统参数,这样可以通过配置参数就可以自由控制通知的显示,如下面,设置android_statusbar_visible参数,默认值为false,即不显示通知,
if(!SystemProperties.getBoolean("android_statusbar_visible",false)) return;
*/
if (sbn != null && !onPluginNotificationPosted(sbn, rankingMap)) {
mMainHandler.post(() -> {
processForRemoteInput(sbn.getNotification(), mContext);
for (NotificationHandler handler : mNotificationHandlers) {
handler.onNotificationPosted(sbn, rankingMap);
}
});
}
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap,
int reason) {
if (DEBUG) Log.d(TAG, "onNotificationRemoved: " + sbn + " reason: " + reason);
/*
2. 当通知被移除时,也需要坐下过滤
if(!SystemProperties.getBoolean("android_statusbar_visible",false)) return;
*/
if (sbn != null && !onPluginNotificationRemoved(sbn, rankingMap)) {
mMainHandler.post(() -> {
for (NotificationHandler handler : mNotificationHandlers) {
handler.onNotificationRemoved(sbn, rankingMap, reason);
}
});
}
}
@Override
public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap) {
onNotificationRemoved(sbn, rankingMap, UNDEFINED_DISMISS_REASON);
}
@Override
public void onNotificationRankingUpdate(final RankingMap rankingMap) {
if (DEBUG) Log.d(TAG, "onRankingUpdate");
/*
3.这里是更新通知在通知列表上的排列,通知也需要坐下过滤
if(!SystemProperties.getBoolean("android_statusbar_visible",false)) return;
*/
if (rankingMap != null) {
RankingMap r = onPluginRankingUpdate(rankingMap);
mMainHandler.post(() -> {
for (NotificationHandler handler : mNotificationHandlers) {
handler.onNotificationRankingUpdate(r);
}
});
}
}
......
}