2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.android.settings.notification;
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;
31 import com.android.internal.widget.LockPatternUtils;
32 import com.android.settingslib.Utils;
34 public class NotificationBackend {
35 private static final String TAG = "NotificationBackend";
37 static INotificationManager sINM = INotificationManager.Stub.asInterface(
38 ServiceManager.getService(Context.NOTIFICATION_SERVICE));
40 public AppRow loadAppRow(Context context, PackageManager pm, ApplicationInfo app) {
41 final AppRow row = new AppRow();
42 row.pkg = app.packageName;
45 row.label = app.loadLabel(pm);
46 } catch (Throwable t) {
47 Log.e(TAG, "Error loading application label for " + row.pkg, t);
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());
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;
76 public boolean getNotificationsBanned(String pkg, int uid) {
78 final boolean enabled = sINM.areNotificationsEnabledForPackage(pkg, uid);
80 } catch (Exception e) {
81 Log.w(TAG, "Error calling NoMan", e);
86 public boolean getBypassZenMode(String pkg, int uid) {
88 return sINM.getPriority(pkg, uid) == Notification.PRIORITY_MAX;
89 } catch (Exception e) {
90 Log.w(TAG, "Error calling NoMan", e);
95 public boolean setBypassZenMode(String pkg, int uid, boolean bypassZen) {
97 sINM.setPriority(pkg, uid,
98 bypassZen ? Notification.PRIORITY_MAX : Notification.PRIORITY_DEFAULT);
100 } catch (Exception e) {
101 Log.w(TAG, "Error calling NoMan", e);
106 public int getVisibilityOverride(String pkg, int uid) {
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;
115 public boolean setVisibilityOverride(String pkg, int uid, int override) {
117 sINM.setVisibilityOverride(pkg, uid, override);
119 } catch (Exception e) {
120 Log.w(TAG, "Error calling NoMan", e);
125 public boolean setImportance(String pkg, int uid, int importance) {
127 sINM.setImportance(pkg, uid, importance);
129 } catch (Exception e) {
130 Log.w(TAG, "Error calling NoMan", e);
135 public int getImportance(String pkg, int uid) {
137 return sINM.getImportance(pkg, uid);
138 } catch (Exception e) {
139 Log.w(TAG, "Error calling NoMan", e);
140 return NotificationListenerService.Ranking.IMPORTANCE_UNSPECIFIED;
145 public String section;
148 public static class AppRow extends Row {
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;