AndroidQ(10.0) SystemUI 增加Notification控制白名单

简介: AndroidQ(10.0) SystemUI 增加Notification控制白名单

前言


定制系统一般都会要求状态栏左上角只显示固定的通知消息,避免预装其它app乱发通知消息


解决办法


找到 SystemUI 中控制消息现实的地方,将其拦截


frameworks\base\packages\SystemUI\src\com\android\systemui\statusbar\NotificationListener.java

    private boolean isNeedAddNotification(String  pkgName){
        java.util.List<String> pkgList =  new java.util.ArrayList<String>();
        pkgList.clear();
        pkgList.add("android");
        return pkgList.contains(pkgName);
    }
    @Override
    public void onNotificationPosted(final StatusBarNotification sbn,
            final RankingMap rankingMap) {
        if (DEBUG) Log.d(TAG, "onNotificationPosted: " + sbn);
        //cczheng add for notificationarea only show pstn and android icon 
        if (sbn != null){
            String  pkgName = sbn.getOpPkg();
            if (!isNeedAddNotification(pkgName)) {
                Log.d("NotificationListener","onNotificationPosted forbidden "+pkgName);
                return;
            }
        }//end
        if (sbn != null && !onPluginNotificationPosted(sbn, rankingMap)) {
            Dependency.get(Dependency.MAIN_HANDLER).post(() -> {
                processForRemoteInput(sbn.getNotification(), mContext);
                String key = sbn.getKey();
                boolean isUpdate =
                        mEntryManager.getNotificationData().get(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.getNotificationData()
                                .updateRanking(rankingMap);
                    }
                    return;
                }
                if (isUpdate) {
                    mEntryManager.updateNotification(sbn, rankingMap);
                } else {
                    Log.d("NotificationListener", "addNotification: " + sbn);
                    mEntryManager.addNotification(sbn, rankingMap);
                }
            });
        }
    }
    @Override
    public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap,
            int reason) {
        if (DEBUG) Log.d(TAG, "onNotificationRemoved: " + sbn + " reason: " + reason);
         //cczheng add for notificationarea only show pstn and android icon 
        if (sbn != null){
            String  pkgName = sbn.getOpPkg();
            if (!isNeedAddNotification(pkgName)) {
                Log.d("NotificationListener","onNotificationPosted forbidden "+pkgName);
                return;
            }
        }//end
        if (sbn != null && !onPluginNotificationRemoved(sbn, rankingMap)) {
            final String key = sbn.getKey();
            Dependency.get(Dependency.MAIN_HANDLER).post(() -> {
                mEntryManager.removeNotification(key, rankingMap, reason);
            });
        }
    }
    @Override
    public void onNotificationRemoved(StatusBarNotification sbn, RankingMap rankingMap) {
        onNotificationRemoved(sbn, rankingMap, UNDEFINED_DISMISS_REASON);
    }

这里只保留 android 进程发送过来的通知,其余一律禁止显示,这里为了演示做的简陋,采用了list存储,

可根据需求增加 Provider 来增删改,来看下log

D/NotificationListener: onNotificationPosted: StatusBarNotification(pkg=android user=UserHandle{-1} id=26 tag=null key=-1|android|26|null|1000: Notification(channel=DEVELOPER pri=0 contentView=null vibrate=null sound=null tick defaults=0x0 flags=0x2 color=0xff607d8b vis=PUBLIC))
D/NotificationListener: addNotification: StatusBarNotification(pkg=android user=UserHandle{-1} id=26 tag=null key=-1|android|26|null|1000: Notification(channel=DEVELOPER pri=0 contentView=null vibrate=null sound=null tick defaults=0x0 flags=0x2 color=0xff607d8b vis=PUBLIC))
D/NotificationListener: onNotificationPosted: StatusBarNotification(pkg=com.android.dyboot user=UserHandle{0} id=4403 tag=null key=0|com.android.dyboot|4403|null|10069: Notification(channel=a3845e9cfc7d494593e892990b7c7bec pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x22 color=0x00000000 vis=PRIVATE))
D/NotificationListener: onNotificationPosted forbidden com.android.dyboot


目录
相关文章
|
XML 数据库 Android开发
android 修改默认APN
android 修改默认APN
558 0
|
1月前
|
Android开发
Android 动态修改参数配置
Android 动态修改参数配置
19 0
|
1月前
|
Java Android开发
Android Mediatek 禁用拨号应用的部分UI显示
Android Mediatek 禁用拨号应用的部分UI显示
20 0
|
Android开发
Android 修改系统屏幕亮度及监听
Android 修改系统屏幕亮度及监听
582 0
Android 修改系统屏幕亮度及监听
|
XML Java Android开发
Android6.0 源码修改之Settings音量调节界面增加通话音量调节
Android6.0 源码修改之Settings音量调节界面增加通话音量调节
76 0
|
XML 缓存 Java
android10.0(Q) Settings 添加设置项——动态方式
android10.0(Q) Settings 添加设置项——动态方式
518 0
|
XML Java Android开发
Android6.0 源码修改之屏蔽导航栏虚拟按键(Home和RecentAPP)/动态显示和隐藏NavigationBar
Android6.0 源码修改之屏蔽导航栏虚拟按键(Home和RecentAPP)/动态显示和隐藏NavigationBar
186 0
|
Java Android开发
Android5.1更改网络优先级
Android5.1更改网络优先级
140 0
|
Android开发
Android 修改系统音量及监听
Android 修改系统音量及监听
822 0
Android 修改系统音量及监听