| Jason Monk | 584b2b2 | 2015-03-20 14:56:28 -0400 | [diff] [blame] | 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; |
| Julia Reynolds | c9a1884 | 2016-01-15 14:23:13 -0500 | [diff] [blame] | 23 | import android.content.pm.PackageInfo; |
| Jason Monk | 584b2b2 | 2015-03-20 14:56:28 -0400 | [diff] [blame] | 24 | import android.content.pm.PackageManager; |
| 25 | import android.graphics.drawable.Drawable; |
| 26 | import android.os.ServiceManager; |
| Julia Reynolds | 3e912e7 | 2016-03-08 15:39:03 -0500 | [diff] [blame] | 27 | import android.os.UserHandle; |
| Jason Monk | 584b2b2 | 2015-03-20 14:56:28 -0400 | [diff] [blame] | 28 | import android.service.notification.NotificationListenerService; |
| 29 | import android.util.Log; |
| Julia Reynolds | 3e912e7 | 2016-03-08 15:39:03 -0500 | [diff] [blame] | 30 | |
| 31 | import com.android.internal.widget.LockPatternUtils; |
| Julia Reynolds | c9a1884 | 2016-01-15 14:23:13 -0500 | [diff] [blame] | 32 | import com.android.settingslib.Utils; |
| Jason Monk | 584b2b2 | 2015-03-20 14:56:28 -0400 | [diff] [blame] | 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 | |
| Julia Reynolds | 3e912e7 | 2016-03-08 15:39:03 -0500 | [diff] [blame] | 40 | public AppRow loadAppRow(Context context, PackageManager pm, ApplicationInfo app) { |
| Jason Monk | 584b2b2 | 2015-03-20 14:56:28 -0400 | [diff] [blame] | 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); |
| Julia Reynolds | ed5c50a | 2016-02-12 09:17:37 -0500 | [diff] [blame] | 52 | row.appImportance = getImportance(row.pkg, row.uid); |
| 53 | row.appBypassDnd = getBypassZenMode(row.pkg, row.uid); |
| Julia Reynolds | 60e90ac | 2016-03-02 08:54:56 -0500 | [diff] [blame] | 54 | row.appVisOverride = getVisibilityOverride(row.pkg, row.uid); |
| Julia Reynolds | 3e912e7 | 2016-03-08 15:39:03 -0500 | [diff] [blame] | 55 | row.lockScreenSecure = new LockPatternUtils(context).isSecure( |
| 56 | UserHandle.myUserId()); |
| Julia Reynolds | 6f526fc | 2015-11-19 11:33:11 -0500 | [diff] [blame] | 57 | return row; |
| 58 | } |
| 59 | |
| Julia Reynolds | 3e912e7 | 2016-03-08 15:39:03 -0500 | [diff] [blame] | 60 | public AppRow loadAppRow(Context context, PackageManager pm, PackageInfo app) { |
| 61 | final AppRow row = loadAppRow(context, pm, app.applicationInfo); |
| Tony Mak | 6ba9e15 | 2016-07-14 15:32:26 +0800 | [diff] [blame] | 62 | row.systemApp = Utils.isSystemPackage(context.getResources(), pm, app); |
| Julia Reynolds | c9a1884 | 2016-01-15 14:23:13 -0500 | [diff] [blame] | 63 | return row; |
| 64 | } |
| 65 | |
| Jason Monk | 584b2b2 | 2015-03-20 14:56:28 -0400 | [diff] [blame] | 66 | 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 Reynolds | ed5c50a | 2016-02-12 09:17:37 -0500 | [diff] [blame] | 76 | public boolean getBypassZenMode(String pkg, int uid) { |
| Jason Monk | 584b2b2 | 2015-03-20 14:56:28 -0400 | [diff] [blame] | 77 | try { |
| Julia Reynolds | ed5c50a | 2016-02-12 09:17:37 -0500 | [diff] [blame] | 78 | return sINM.getPriority(pkg, uid) == Notification.PRIORITY_MAX; |
| Jason Monk | 584b2b2 | 2015-03-20 14:56:28 -0400 | [diff] [blame] | 79 | } catch (Exception e) { |
| 80 | Log.w(TAG, "Error calling NoMan", e); |
| 81 | return false; |
| 82 | } |
| 83 | } |
| 84 | |
| Julia Reynolds | ed5c50a | 2016-02-12 09:17:37 -0500 | [diff] [blame] | 85 | public boolean setBypassZenMode(String pkg, int uid, boolean bypassZen) { |
| Jason Monk | 584b2b2 | 2015-03-20 14:56:28 -0400 | [diff] [blame] | 86 | try { |
| Julia Reynolds | ed5c50a | 2016-02-12 09:17:37 -0500 | [diff] [blame] | 87 | sINM.setPriority(pkg, uid, |
| Julia Reynolds | 6f526fc | 2015-11-19 11:33:11 -0500 | [diff] [blame] | 88 | bypassZen ? Notification.PRIORITY_MAX : Notification.PRIORITY_DEFAULT); |
| Jason Monk | 584b2b2 | 2015-03-20 14:56:28 -0400 | [diff] [blame] | 89 | return true; |
| 90 | } catch (Exception e) { |
| 91 | Log.w(TAG, "Error calling NoMan", e); |
| 92 | return false; |
| 93 | } |
| 94 | } |
| 95 | |
| Julia Reynolds | 60e90ac | 2016-03-02 08:54:56 -0500 | [diff] [blame] | 96 | public int getVisibilityOverride(String pkg, int uid) { |
| Jason Monk | 584b2b2 | 2015-03-20 14:56:28 -0400 | [diff] [blame] | 97 | try { |
| Julia Reynolds | 60e90ac | 2016-03-02 08:54:56 -0500 | [diff] [blame] | 98 | return sINM.getVisibilityOverride(pkg, uid); |
| Jason Monk | 584b2b2 | 2015-03-20 14:56:28 -0400 | [diff] [blame] | 99 | } catch (Exception e) { |
| 100 | Log.w(TAG, "Error calling NoMan", e); |
| Julia Reynolds | 60e90ac | 2016-03-02 08:54:56 -0500 | [diff] [blame] | 101 | return NotificationListenerService.Ranking.VISIBILITY_NO_OVERRIDE; |
| Jason Monk | 584b2b2 | 2015-03-20 14:56:28 -0400 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| Julia Reynolds | 60e90ac | 2016-03-02 08:54:56 -0500 | [diff] [blame] | 105 | public boolean setVisibilityOverride(String pkg, int uid, int override) { |
| Jason Monk | 584b2b2 | 2015-03-20 14:56:28 -0400 | [diff] [blame] | 106 | try { |
| Julia Reynolds | 60e90ac | 2016-03-02 08:54:56 -0500 | [diff] [blame] | 107 | sINM.setVisibilityOverride(pkg, uid, override); |
| Jason Monk | 584b2b2 | 2015-03-20 14:56:28 -0400 | [diff] [blame] | 108 | return true; |
| 109 | } catch (Exception e) { |
| 110 | Log.w(TAG, "Error calling NoMan", e); |
| 111 | return false; |
| 112 | } |
| 113 | } |
| 114 | |
| Julia Reynolds | ed5c50a | 2016-02-12 09:17:37 -0500 | [diff] [blame] | 115 | public boolean setImportance(String pkg, int uid, int importance) { |
| Julia Reynolds | 9dbb20f | 2015-11-23 08:58:49 -0500 | [diff] [blame] | 116 | try { |
| Julia Reynolds | ed5c50a | 2016-02-12 09:17:37 -0500 | [diff] [blame] | 117 | sINM.setImportance(pkg, uid, importance); |
| Julia Reynolds | 9dbb20f | 2015-11-23 08:58:49 -0500 | [diff] [blame] | 118 | return true; |
| 119 | } catch (Exception e) { |
| 120 | Log.w(TAG, "Error calling NoMan", e); |
| 121 | return false; |
| 122 | } |
| 123 | } |
| 124 | |
| Julia Reynolds | ed5c50a | 2016-02-12 09:17:37 -0500 | [diff] [blame] | 125 | public int getImportance(String pkg, int uid) { |
| Julia Reynolds | 9dbb20f | 2015-11-23 08:58:49 -0500 | [diff] [blame] | 126 | try { |
| Julia Reynolds | ed5c50a | 2016-02-12 09:17:37 -0500 | [diff] [blame] | 127 | return sINM.getImportance(pkg, uid); |
| Julia Reynolds | 9dbb20f | 2015-11-23 08:58:49 -0500 | [diff] [blame] | 128 | } catch (Exception e) { |
| 129 | Log.w(TAG, "Error calling NoMan", e); |
| Julia Reynolds | df01cde | 2015-12-18 11:44:43 -0500 | [diff] [blame] | 130 | return NotificationListenerService.Ranking.IMPORTANCE_UNSPECIFIED; |
| Julia Reynolds | 9dbb20f | 2015-11-23 08:58:49 -0500 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | |
| Jason Monk | 584b2b2 | 2015-03-20 14:56:28 -0400 | [diff] [blame] | 134 | 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 Reynolds | 6f526fc | 2015-11-19 11:33:11 -0500 | [diff] [blame] | 145 | public boolean first; // first app in section |
| Julia Reynolds | c9a1884 | 2016-01-15 14:23:13 -0500 | [diff] [blame] | 146 | public boolean systemApp; |
| Julia Reynolds | 92ea89c | 2016-01-25 16:37:28 -0500 | [diff] [blame] | 147 | public int appImportance; |
| 148 | public boolean appBypassDnd; |
| Julia Reynolds | 60e90ac | 2016-03-02 08:54:56 -0500 | [diff] [blame] | 149 | public int appVisOverride; |
| Julia Reynolds | 3e912e7 | 2016-03-08 15:39:03 -0500 | [diff] [blame] | 150 | public boolean lockScreenSecure; |
| Jason Monk | 584b2b2 | 2015-03-20 14:56:28 -0400 | [diff] [blame] | 151 | } |
| Jason Monk | 584b2b2 | 2015-03-20 14:56:28 -0400 | [diff] [blame] | 152 | } |