Android9.0 SystemUI通知顯示流程

系統會通過兩個方法將通知上報給SystemUI的NotificationListener類:

@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();
    mPresenter.getHandler().post(() -> {
        for (StatusBarNotification sbn : notifications) {
            mEntryManager.addNotification(sbn, currentRanking);
        }
    });
}
 
@Override
public void onNotificationPosted(final StatusBarNotification sbn,
        final RankingMap rankingMap) {
    if (DEBUG) Log.d(TAG, "onNotificationPosted: " + sbn);
    if (sbn != null && !onPluginNotificationPosted(sbn, rankingMap)) {
        //......省略部分代碼......
    }
}

通知顯示流程圖如下:
androidP通知顯示流程圖.png
如果我們要對一些通知進行屏蔽,可在NotificationData類的filterAndSort()方法中進行處理,即修改shouldFilterOut()方法。如代碼所示屏蔽android包的USB channel通知。

public void filterAndSort() {
    mSortedAndFiltered.clear();
 
    synchronized (mEntries) {
        final int N = mEntries.size();
        for (int i = 0; i < N; i++) {
            Entry entry = mEntries.valueAt(i);
 
            if (shouldFilterOut(entry)) {
                continue;
            }
 
            mSortedAndFiltered.add(entry);
        }
    }
 
    Collections.sort(mSortedAndFiltered, mRankingComparator);
}
 
**
 * @return true if this notification should NOT be shown right now
 */
public boolean shouldFilterOut(Entry entry) {
    final StatusBarNotification sbn = entry.notification;
    //屏蔽android包channelId爲USB的通知.
    try {
        if(sbn.getPackageName().equals("android") && sbn.getNotification().getChannelId().equals("USB")){
            Logger.d(TAG,"block notification channel-USB  of package-android.");
            return true;
        }
    }catch (Exception e){
        e.printStackTrace();
        //do nothing.
    }
 
    if (!(mEnvironment.isDeviceProvisioned() ||
            showNotificationEvenIfUnprovisioned(sbn))) {
        return true;
    }
    //......省略部分代碼......
}

參照鏈接:https://blog.csdn.net/snail201211/article/details/85168402

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章