blob: 944a1f2cfc994d8b04bec8f2666ba6585dcb2a12 [file] [log] [blame]
Jason Monk584b2b22015-03-20 14:56:28 -04001/*
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 */
16package com.android.settings.notification;
17
18import android.app.INotificationManager;
19import android.app.Notification;
20import android.content.Context;
21import android.content.Intent;
22import android.content.pm.ApplicationInfo;
Julia Reynoldsc9a18842016-01-15 14:23:13 -050023import android.content.pm.PackageInfo;
Jason Monk584b2b22015-03-20 14:56:28 -040024import android.content.pm.PackageManager;
25import android.graphics.drawable.Drawable;
26import android.os.ServiceManager;
Julia Reynolds3e912e72016-03-08 15:39:03 -050027import android.os.UserHandle;
Jason Monk584b2b22015-03-20 14:56:28 -040028import android.service.notification.NotificationListenerService;
29import android.util.Log;
Julia Reynolds3e912e72016-03-08 15:39:03 -050030
31import com.android.internal.widget.LockPatternUtils;
Julia Reynoldsc9a18842016-01-15 14:23:13 -050032import com.android.settingslib.Utils;
Jason Monk584b2b22015-03-20 14:56:28 -040033
34public class NotificationBackend {
35 private static final String TAG = "NotificationBackend";
36
37 static INotificationManager sINM = INotificationManager.Stub.asInterface(
38 ServiceManager.getService(Context.NOTIFICATION_SERVICE));
39
Julia Reynolds3e912e72016-03-08 15:39:03 -050040 public AppRow loadAppRow(Context context, PackageManager pm, ApplicationInfo app) {
Jason Monk584b2b22015-03-20 14:56:28 -040041 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);
Julia Reynoldsed5c50a2016-02-12 09:17:37 -050052 row.appImportance = getImportance(row.pkg, row.uid);
53 row.appBypassDnd = getBypassZenMode(row.pkg, row.uid);
Julia Reynolds60e90ac2016-03-02 08:54:56 -050054 row.appVisOverride = getVisibilityOverride(row.pkg, row.uid);
Julia Reynolds3e912e72016-03-08 15:39:03 -050055 row.lockScreenSecure = new LockPatternUtils(context).isSecure(
56 UserHandle.myUserId());
Julia Reynolds6f526fc2015-11-19 11:33:11 -050057 return row;
58 }
59
Julia Reynolds3e912e72016-03-08 15:39:03 -050060 public AppRow loadAppRow(Context context, PackageManager pm, PackageInfo app) {
61 final AppRow row = loadAppRow(context, pm, app.applicationInfo);
Tony Mak6ba9e152016-07-14 15:32:26 +080062 row.systemApp = Utils.isSystemPackage(context.getResources(), pm, app);
Julia Reynoldsc9a18842016-01-15 14:23:13 -050063 return row;
64 }
65
Jason Monk584b2b22015-03-20 14:56:28 -040066 public boolean getNotificationsBanned(String pkg, int uid) {
67 try {
68 final boolean enabled = sINM.areNotificationsEnabledForPackage(pkg, uid);
69 return !enabled;
70 } catch (Exception e) {
71 Log.w(TAG, "Error calling NoMan", e);
72 return false;
73 }
74 }
75
Julia Reynoldsed5c50a2016-02-12 09:17:37 -050076 public boolean getBypassZenMode(String pkg, int uid) {
Jason Monk584b2b22015-03-20 14:56:28 -040077 try {
Julia Reynoldsed5c50a2016-02-12 09:17:37 -050078 return sINM.getPriority(pkg, uid) == Notification.PRIORITY_MAX;
Jason Monk584b2b22015-03-20 14:56:28 -040079 } catch (Exception e) {
80 Log.w(TAG, "Error calling NoMan", e);
81 return false;
82 }
83 }
84
Julia Reynoldsed5c50a2016-02-12 09:17:37 -050085 public boolean setBypassZenMode(String pkg, int uid, boolean bypassZen) {
Jason Monk584b2b22015-03-20 14:56:28 -040086 try {
Julia Reynoldsed5c50a2016-02-12 09:17:37 -050087 sINM.setPriority(pkg, uid,
Julia Reynolds6f526fc2015-11-19 11:33:11 -050088 bypassZen ? Notification.PRIORITY_MAX : Notification.PRIORITY_DEFAULT);
Jason Monk584b2b22015-03-20 14:56:28 -040089 return true;
90 } catch (Exception e) {
91 Log.w(TAG, "Error calling NoMan", e);
92 return false;
93 }
94 }
95
Julia Reynolds60e90ac2016-03-02 08:54:56 -050096 public int getVisibilityOverride(String pkg, int uid) {
Jason Monk584b2b22015-03-20 14:56:28 -040097 try {
Julia Reynolds60e90ac2016-03-02 08:54:56 -050098 return sINM.getVisibilityOverride(pkg, uid);
Jason Monk584b2b22015-03-20 14:56:28 -040099 } catch (Exception e) {
100 Log.w(TAG, "Error calling NoMan", e);
Julia Reynolds60e90ac2016-03-02 08:54:56 -0500101 return NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE;
Jason Monk584b2b22015-03-20 14:56:28 -0400102 }
103 }
104
Julia Reynolds60e90ac2016-03-02 08:54:56 -0500105 public boolean setVisibilityOverride(String pkg, int uid, int override) {
Jason Monk584b2b22015-03-20 14:56:28 -0400106 try {
Julia Reynolds60e90ac2016-03-02 08:54:56 -0500107 sINM.setVisibilityOverride(pkg, uid, override);
Jason Monk584b2b22015-03-20 14:56:28 -0400108 return true;
109 } catch (Exception e) {
110 Log.w(TAG, "Error calling NoMan", e);
111 return false;
112 }
113 }
114
Julia Reynoldsed5c50a2016-02-12 09:17:37 -0500115 public boolean setImportance(String pkg, int uid, int importance) {
Julia Reynolds9dbb20f2015-11-23 08:58:49 -0500116 try {
Julia Reynoldsed5c50a2016-02-12 09:17:37 -0500117 sINM.setImportance(pkg, uid, importance);
Julia Reynolds9dbb20f2015-11-23 08:58:49 -0500118 return true;
119 } catch (Exception e) {
120 Log.w(TAG, "Error calling NoMan", e);
121 return false;
122 }
123 }
124
Julia Reynoldsed5c50a2016-02-12 09:17:37 -0500125 public int getImportance(String pkg, int uid) {
Julia Reynolds9dbb20f2015-11-23 08:58:49 -0500126 try {
Julia Reynoldsed5c50a2016-02-12 09:17:37 -0500127 return sINM.getImportance(pkg, uid);
Julia Reynolds9dbb20f2015-11-23 08:58:49 -0500128 } catch (Exception e) {
129 Log.w(TAG, "Error calling NoMan", e);
Julia Reynoldsdf01cde2015-12-18 11:44:43 -0500130 return NotificationListenerService.Ranking.IMPORTANCE_UNSPECIFIED;
Julia Reynolds9dbb20f2015-11-23 08:58:49 -0500131 }
132 }
133
Jason Monk584b2b22015-03-20 14:56:28 -0400134 static class Row {
135 public String section;
136 }
137
138 public static class AppRow extends Row {
139 public String pkg;
140 public int uid;
141 public Drawable icon;
142 public CharSequence label;
143 public Intent settingsIntent;
144 public boolean banned;
Julia Reynolds6f526fc2015-11-19 11:33:11 -0500145 public boolean first; // first app in section
Julia Reynoldsc9a18842016-01-15 14:23:13 -0500146 public boolean systemApp;
Julia Reynolds92ea89c2016-01-25 16:37:28 -0500147 public int appImportance;
148 public boolean appBypassDnd;
Julia Reynolds60e90ac2016-03-02 08:54:56 -0500149 public int appVisOverride;
Julia Reynolds3e912e72016-03-08 15:39:03 -0500150 public boolean lockScreenSecure;
Jason Monk584b2b22015-03-20 14:56:28 -0400151 }
Jason Monk584b2b22015-03-20 14:56:28 -0400152}