OSDN Git Service

Suppress notif block/silence settings for essential packages.
[android-x86/packages-apps-Settings.git] / src / com / android / settings / notification / NotificationBackend.java
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.settings.notification;
17
18 import android.app.INotificationManager;
19 import android.app.Notification;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.pm.ApplicationInfo;
23 import android.content.pm.PackageInfo;
24 import android.content.pm.PackageManager;
25 import android.graphics.drawable.Drawable;
26 import android.os.ServiceManager;
27 import android.os.UserHandle;
28 import android.service.notification.NotificationListenerService;
29 import android.util.Log;
30
31 import com.android.internal.widget.LockPatternUtils;
32 import com.android.settingslib.Utils;
33
34 public class NotificationBackend {
35     private static final String TAG = "NotificationBackend";
36
37     static INotificationManager sINM = INotificationManager.Stub.asInterface(
38             ServiceManager.getService(Context.NOTIFICATION_SERVICE));
39
40     public AppRow loadAppRow(Context context, PackageManager pm, ApplicationInfo app) {
41         final AppRow row = new AppRow();
42         row.pkg = app.packageName;
43         row.uid = app.uid;
44         try {
45             row.label = app.loadLabel(pm);
46         } catch (Throwable t) {
47             Log.e(TAG, "Error loading application label for " + row.pkg, t);
48             row.label = row.pkg;
49         }
50         row.icon = app.loadIcon(pm);
51         row.banned = getNotificationsBanned(row.pkg, row.uid);
52         row.appImportance = getImportance(row.pkg, row.uid);
53         row.appBypassDnd = getBypassZenMode(row.pkg, row.uid);
54         row.appVisOverride = getVisibilityOverride(row.pkg, row.uid);
55         row.lockScreenSecure = new LockPatternUtils(context).isSecure(
56                 UserHandle.myUserId());
57         return row;
58     }
59
60     public AppRow loadAppRow(Context context, PackageManager pm, PackageInfo app) {
61         final AppRow row = loadAppRow(context, pm, app.applicationInfo);
62         row.cantBlock = Utils.isSystemPackage(context.getResources(), pm, app);
63         final String[] nonBlockablePkgs = context.getResources().getStringArray(
64                     com.android.internal.R.array.config_nonBlockableNotificationPackages);
65         if (nonBlockablePkgs != null) {
66             int N = nonBlockablePkgs.length;
67             for (int i = 0; i < N; i++) {
68                 if (app.packageName.equals(nonBlockablePkgs[i])) {
69                     row.cantBlock = row.cantSilence = true;
70                 }
71             }
72         }
73         return row;
74     }
75
76     public boolean getNotificationsBanned(String pkg, int uid) {
77         try {
78             final boolean enabled = sINM.areNotificationsEnabledForPackage(pkg, uid);
79             return !enabled;
80         } catch (Exception e) {
81             Log.w(TAG, "Error calling NoMan", e);
82             return false;
83         }
84     }
85
86     public boolean getBypassZenMode(String pkg, int uid) {
87         try {
88             return sINM.getPriority(pkg, uid) == Notification.PRIORITY_MAX;
89         } catch (Exception e) {
90             Log.w(TAG, "Error calling NoMan", e);
91             return false;
92         }
93     }
94
95     public boolean setBypassZenMode(String pkg, int uid, boolean bypassZen) {
96         try {
97             sINM.setPriority(pkg, uid,
98                     bypassZen ? Notification.PRIORITY_MAX : Notification.PRIORITY_DEFAULT);
99             return true;
100         } catch (Exception e) {
101             Log.w(TAG, "Error calling NoMan", e);
102             return false;
103         }
104     }
105
106     public int getVisibilityOverride(String pkg, int uid) {
107         try {
108             return sINM.getVisibilityOverride(pkg, uid);
109         } catch (Exception e) {
110             Log.w(TAG, "Error calling NoMan", e);
111             return NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE;
112         }
113     }
114
115     public boolean setVisibilityOverride(String pkg, int uid, int override) {
116         try {
117             sINM.setVisibilityOverride(pkg, uid, override);
118             return true;
119         } catch (Exception e) {
120             Log.w(TAG, "Error calling NoMan", e);
121             return false;
122         }
123     }
124
125     public boolean setImportance(String pkg, int uid, int importance) {
126         try {
127             sINM.setImportance(pkg, uid, importance);
128             return true;
129         } catch (Exception e) {
130             Log.w(TAG, "Error calling NoMan", e);
131             return false;
132         }
133     }
134
135     public int getImportance(String pkg, int uid) {
136         try {
137             return sINM.getImportance(pkg, uid);
138         } catch (Exception e) {
139             Log.w(TAG, "Error calling NoMan", e);
140             return NotificationListenerService.Ranking.IMPORTANCE_UNSPECIFIED;
141         }
142     }
143
144     static class Row {
145         public String section;
146     }
147
148     public static class AppRow extends Row {
149         public String pkg;
150         public int uid;
151         public Drawable icon;
152         public CharSequence label;
153         public Intent settingsIntent;
154         public boolean banned;
155         public boolean first;  // first app in section
156         public boolean cantBlock;
157         public boolean cantSilence;
158         public int appImportance;
159         public boolean appBypassDnd;
160         public int appVisOverride;
161         public boolean lockScreenSecure;
162     }
163 }