【Notification】屏蔽特定应用的通知提示

需要默认屏蔽特定app的通知提示
设置app是否接收通知的界面
点击每个条目进去的界面
【Notification】屏蔽特定应用的通知提示_第1张图片

AppNotificationSettings extends SettingsPreferenceFragment
private SwitchPreference mBlock; //条目通过Preference设置
mBlock.setChecked(mAppRow.banned);

mBlock.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
    final boolean block = (Boolean) newValue;
    return mBackend.setNotificationsBanned(pkg, uid, block);
}
    });

    // Users cannot block notifications from system/signature packages
    //通过工具类判断app是系统包(如计算器)时移除设置通知设置的preference
    if (Utils.isSystemPackage(pm, info)) {
        getPreferenceScreen().removePreference(mBlock);
        mPriority.setDependency(null); // don't have it depend on a preference that's gone
    }

通过对Block的当前状态通过mAppRow设置
对preference状态的监听,通过mBackend实现
import com.android.settings.notification.NotificationAppList.AppRow;
import com.android.settings.notification.NotificationAppList.Backend;

查看NotificationAppList.java

    //对AppRow中的属性进行初始化
    public static AppRow loadAppRow(PackageManager pm, ApplicationInfo app,
            Backend backend) {
        final AppRow row = new AppRow();
        row.pkg = app.packageName;
        row.uid = app.uid;
        try {
            row.label = app.loadLabel(pm);
        } catch (Throwable t) {
            Log.e(TAG, "Error loading application label for " + row.pkg, t);
            row.label = row.pkg;
        }
        row.icon = app.loadIcon(pm);
        row.banned = backend.getNotificationsBanned(row.pkg, row.uid);// 是否禁止通知
        row.priority = backend.getHighPriority(row.pkg, row.uid);
        row.sensitive = backend.getSensitive(row.pkg, row.uid);
        return row;
}


查看INotificationManager接口
find frameworks/ -name “INotification*”
frameworks/support/v4/java/android/support/v4/app/INotificationSideChannel.aidl
frameworks/base/core/java/com/mediatek/common/mom/INotificationListener.aidl
frameworks/base/core/java/android/app/INotificationManager.aidl
frameworks/base/core/java/android/service/notification/INotificationListener.aidl

.aidl文件(接口定义语言,用于进程间通讯)
frameworks/base/core/java/android/app/INotificationManager.aidl
实现的service路径为
frameworks\base\services\java\com\android\server\NotificationManagerService.java

对接受Notification属性的获取

mService的类型IAppOpsService
frameworks/base/core/java/com/android/internal/app/IAppOpsService.aidl
frameworks/base/services/core/java/com/android/server/AppOpsService.java

返回的值为MODE_IGNORED时,boolean areNotificationsEnabledForPackage(String pkg, int uid)会返回false
mService.checkOperation(op, uid, packageName) = MODE_ALLOWED时,则允许接收通知 ;

【Notification】屏蔽特定应用的通知提示_第2张图片

对接受Notification的属性设置

@Override
        public void setNotificationsEnabledForPackage(String pkg, int uid, boolean enabled) {
            checkCallerIsSystem();

            setNotificationsEnabledForPackageImpl(pkg, uid, enabled);
        }


AppOpsManager的路径:frameworks/base/core/java/android/app/AppOpsManager.java
【Notification】屏蔽特定应用的通知提示_第3张图片
public static final int OP_POST_NOTIFICATION = 11;
uid = app.uid
pkg = app.packageName;
mode = enabled?AppOpsManager.MODE_ALLOWED:AppOpsManager.MODE_IGNORED
【Notification】屏蔽特定应用的通知提示_第4张图片

查看代码中哪些位置调用了setNotificationsEnabledForPackageImpl方法

除此处还有两处都是对方法的重写与具体实现

具体解决步骤

当需要屏蔽所有应用通知没有例外时
在AppOpsManager中有关于app许多参数设置的默认值,比如图中第十二个就是默认对app的通知开启或关闭,AppOpsService中的checkOperation方法下就进行了判断,当op为空时,返回的时默认的MODE。所以讲原本的MODE_ALLOWED改为MODE_IGNORED后,编译frameworks/base后push进手机重启就会发现所有app全部被屏蔽通知没有例外。当有特定的app需要开启通知时,我们可以在checkOperation中进行修改。
【Notification】屏蔽特定应用的通知提示_第5张图片

   @Override
    public int checkOperation(int code, int uid, String packageName) {
        verifyIncomingUid(uid);
        verifyIncomingOp(code);
        synchronized (this) {
            if (isOpRestricted(uid, code, packageName)) {
                return AppOpsManager.MODE_IGNORED;
            }
            Op op = getOpLocked(AppOpsManager.opToSwitch(code), uid, packageName, false);
            if (op == null) {
                return AppOpsManager.opToDefaultMode(code);
            }
            return op.mode;
        }
}

在checkOperation中进行判断时需要判断code值,否则容易出现点击重启的状况,修改后代码如下。

public int checkOperation(int code, int uid, String packageName) {
        verifyIncomingUid(uid);
        verifyIncomingOp(code);
        synchronized (this) {
         //chenzilong add for ZELY-41 block app notifications 20160331 start
         if(code == AppOpsManager.OP_POST_NOTIFICATION){
             if ((packageName.equals("com.advan.advanstore")||packageName.equals("com.stkj.android.freeshare"))){
                 return AppOpsManager.MODE_ALLOWED;
             }else{
                 return AppOpsManager.MODE_IGNORED;
             }
         }
         // chenzilong add for ZELY-41 block app notifications 20160331 end

            if (isOpRestricted(uid, code, packageName)) {
                return AppOpsManager.MODE_IGNORED;
            }
            Op op = getOpLocked(AppOpsManager.opToSwitch(code), uid, packageName, false);
            if (op == null) {
                return AppOpsManager.opToDefaultMode(code);
            }
            return op.mode;
        }

【Notification】屏蔽特定应用的通知提示_第6张图片
最后的实现结果

你可能感兴趣的:(android,应用,通知)