The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 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 | |
| 17 | package android.app; |
| 18 | |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 19 | import com.android.internal.util.Preconditions; |
| 20 | |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 21 | import android.annotation.NonNull; |
| 22 | import android.annotation.Nullable; |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 23 | import android.annotation.SdkConstant; |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 24 | import android.app.Notification.Builder; |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 25 | import android.content.ComponentName; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.content.Context; |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 27 | import android.content.pm.ParceledListSlice; |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 28 | import android.graphics.drawable.Icon; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 29 | import android.net.Uri; |
Dan Sandler | 4e78706 | 2015-06-17 15:09:48 -0400 | [diff] [blame] | 30 | import android.os.Build; |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 31 | import android.os.Bundle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.os.Handler; |
| 33 | import android.os.IBinder; |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 34 | import android.os.Parcel; |
| 35 | import android.os.Parcelable; |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 36 | import android.os.RemoteException; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.os.ServiceManager; |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 38 | import android.os.StrictMode; |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 39 | import android.os.UserHandle; |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 40 | import android.provider.Settings.Global; |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 41 | import android.service.notification.IConditionListener; |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 42 | import android.service.notification.NotificationListenerService; |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 43 | import android.service.notification.StatusBarNotification; |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 44 | import android.service.notification.ZenModeConfig; |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 45 | import android.util.ArraySet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import android.util.Log; |
| 47 | |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 48 | import java.util.Objects; |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 49 | import java.util.List; |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 50 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | /** |
| 52 | * Class to notify the user of events that happen. This is how you tell |
| 53 | * the user that something has happened in the background. {@more} |
| 54 | * |
| 55 | * Notifications can take different forms: |
| 56 | * <ul> |
| 57 | * <li>A persistent icon that goes in the status bar and is accessible |
| 58 | * through the launcher, (when the user selects it, a designated Intent |
| 59 | * can be launched),</li> |
| 60 | * <li>Turning on or flashing LEDs on the device, or</li> |
| 61 | * <li>Alerting the user by flashing the backlight, playing a sound, |
| 62 | * or vibrating.</li> |
| 63 | * </ul> |
| 64 | * |
| 65 | * <p> |
Peter Collingbourne | b97c349 | 2010-10-13 20:04:52 +0100 | [diff] [blame] | 66 | * Each of the notify methods takes an int id parameter and optionally a |
| 67 | * {@link String} tag parameter, which may be {@code null}. These parameters |
| 68 | * are used to form a pair (tag, id), or ({@code null}, id) if tag is |
| 69 | * unspecified. This pair identifies this notification from your app to the |
| 70 | * system, so that pair should be unique within your app. If you call one |
| 71 | * of the notify methods with a (tag, id) pair that is currently active and |
| 72 | * a new set of notification parameters, it will be updated. For example, |
| 73 | * if you pass a new status bar icon, the old icon in the status bar will |
| 74 | * be replaced with the new one. This is also the same tag and id you pass |
| 75 | * to the {@link #cancel(int)} or {@link #cancel(String, int)} method to clear |
| 76 | * this notification. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | * |
| 78 | * <p> |
| 79 | * You do not instantiate this class directly; instead, retrieve it through |
| 80 | * {@link android.content.Context#getSystemService}. |
| 81 | * |
Joe Fernandez | 558459f | 2011-10-13 16:47:36 -0700 | [diff] [blame] | 82 | * <div class="special reference"> |
| 83 | * <h3>Developer Guides</h3> |
| 84 | * <p>For a guide to creating notifications, read the |
| 85 | * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a> |
| 86 | * developer guide.</p> |
| 87 | * </div> |
| 88 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 89 | * @see android.app.Notification |
| 90 | * @see android.content.Context#getSystemService |
| 91 | */ |
| 92 | public class NotificationManager |
| 93 | { |
| 94 | private static String TAG = "NotificationManager"; |
Joe Onorato | 43a1765 | 2011-04-06 19:22:23 -0700 | [diff] [blame] | 95 | private static boolean localLOGV = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 97 | /** |
| 98 | * Intent that is broadcast when the state of {@link #getEffectsSuppressor()} changes. |
| 99 | * This broadcast is only sent to registered receivers. |
| 100 | * |
| 101 | * @hide |
| 102 | */ |
| 103 | @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION) |
| 104 | public static final String ACTION_EFFECTS_SUPPRESSOR_CHANGED |
| 105 | = "android.os.action.ACTION_EFFECTS_SUPPRESSOR_CHANGED"; |
| 106 | |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 107 | /** |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 108 | * Intent that is broadcast when the state of {@link #isNotificationPolicyAccessGranted()} |
| 109 | * changes. |
| 110 | * |
| 111 | * This broadcast is only sent to registered receivers, and only to the apps that have changed. |
| 112 | */ |
| 113 | @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION) |
| 114 | public static final String ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED |
| 115 | = "android.app.action.NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED"; |
| 116 | |
| 117 | /** |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 118 | * Intent that is broadcast when the state of getNotificationPolicy() changes. |
| 119 | * This broadcast is only sent to registered receivers. |
| 120 | */ |
| 121 | @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION) |
| 122 | public static final String ACTION_NOTIFICATION_POLICY_CHANGED |
| 123 | = "android.app.action.NOTIFICATION_POLICY_CHANGED"; |
| 124 | |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 125 | /** |
| 126 | * Intent that is broadcast when the state of getCurrentInterruptionFilter() changes. |
| 127 | * This broadcast is only sent to registered receivers. |
| 128 | */ |
| 129 | @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION) |
| 130 | public static final String ACTION_INTERRUPTION_FILTER_CHANGED |
| 131 | = "android.app.action.INTERRUPTION_FILTER_CHANGED"; |
| 132 | |
| 133 | /** |
Jason Monk | a992732 | 2015-12-13 16:22:37 -0500 | [diff] [blame] | 134 | * Intent that is broadcast when the state of getCurrentInterruptionFilter() changes. |
| 135 | * @hide |
| 136 | */ |
| 137 | @SdkConstant(SdkConstant.SdkConstantType.BROADCAST_INTENT_ACTION) |
| 138 | public static final String ACTION_INTERRUPTION_FILTER_CHANGED_INTERNAL |
| 139 | = "android.app.action.INTERRUPTION_FILTER_CHANGED_INTERNAL"; |
| 140 | |
| 141 | /** |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 142 | * {@link #getCurrentInterruptionFilter() Interruption filter} constant - |
| 143 | * Normal interruption filter. |
| 144 | */ |
| 145 | public static final int INTERRUPTION_FILTER_ALL = 1; |
| 146 | |
| 147 | /** |
| 148 | * {@link #getCurrentInterruptionFilter() Interruption filter} constant - |
| 149 | * Priority interruption filter. |
| 150 | */ |
| 151 | public static final int INTERRUPTION_FILTER_PRIORITY = 2; |
| 152 | |
| 153 | /** |
| 154 | * {@link #getCurrentInterruptionFilter() Interruption filter} constant - |
| 155 | * No interruptions filter. |
| 156 | */ |
| 157 | public static final int INTERRUPTION_FILTER_NONE = 3; |
| 158 | |
| 159 | /** |
| 160 | * {@link #getCurrentInterruptionFilter() Interruption filter} constant - |
| 161 | * Alarms only interruption filter. |
| 162 | */ |
| 163 | public static final int INTERRUPTION_FILTER_ALARMS = 4; |
| 164 | |
| 165 | /** {@link #getCurrentInterruptionFilter() Interruption filter} constant - returned when |
| 166 | * the value is unavailable for any reason. |
| 167 | */ |
| 168 | public static final int INTERRUPTION_FILTER_UNKNOWN = 0; |
| 169 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | private static INotificationManager sService; |
| 171 | |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 172 | /** @hide */ |
| 173 | static public INotificationManager getService() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | { |
| 175 | if (sService != null) { |
| 176 | return sService; |
| 177 | } |
| 178 | IBinder b = ServiceManager.getService("notification"); |
| 179 | sService = INotificationManager.Stub.asInterface(b); |
| 180 | return sService; |
| 181 | } |
| 182 | |
| 183 | /*package*/ NotificationManager(Context context, Handler handler) |
| 184 | { |
| 185 | mContext = context; |
| 186 | } |
| 187 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 188 | /** {@hide} */ |
| 189 | public static NotificationManager from(Context context) { |
| 190 | return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); |
| 191 | } |
| 192 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | /** |
Daniel Sandler | e97a3bc | 2011-02-07 16:47:07 -0500 | [diff] [blame] | 194 | * Post a notification to be shown in the status bar. If a notification with |
| 195 | * the same id has already been posted by your application and has not yet been canceled, it |
| 196 | * will be replaced by the updated information. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 197 | * |
| 198 | * @param id An identifier for this notification unique within your |
| 199 | * application. |
Daniel Sandler | e97a3bc | 2011-02-07 16:47:07 -0500 | [diff] [blame] | 200 | * @param notification A {@link Notification} object describing what to show the user. Must not |
| 201 | * be null. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | */ |
| 203 | public void notify(int id, Notification notification) |
| 204 | { |
Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 205 | notify(null, id, notification); |
| 206 | } |
| 207 | |
| 208 | /** |
Daniel Sandler | e97a3bc | 2011-02-07 16:47:07 -0500 | [diff] [blame] | 209 | * Post a notification to be shown in the status bar. If a notification with |
| 210 | * the same tag and id has already been posted by your application and has not yet been |
| 211 | * canceled, it will be replaced by the updated information. |
Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 212 | * |
Peter Collingbourne | b97c349 | 2010-10-13 20:04:52 +0100 | [diff] [blame] | 213 | * @param tag A string identifier for this notification. May be {@code null}. |
| 214 | * @param id An identifier for this notification. The pair (tag, id) must be unique |
| 215 | * within your application. |
Daniel Sandler | e97a3bc | 2011-02-07 16:47:07 -0500 | [diff] [blame] | 216 | * @param notification A {@link Notification} object describing what to |
| 217 | * show the user. Must not be null. |
Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 218 | */ |
| 219 | public void notify(String tag, int id, Notification notification) |
| 220 | { |
Julia Reynolds | d9228f1 | 2015-10-20 10:37:27 -0400 | [diff] [blame] | 221 | notifyAsUser(tag, id, notification, new UserHandle(UserHandle.myUserId())); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /** |
| 225 | * @hide |
| 226 | */ |
| 227 | public void notifyAsUser(String tag, int id, Notification notification, UserHandle user) |
| 228 | { |
| 229 | int[] idOut = new int[1]; |
| 230 | INotificationManager service = getService(); |
| 231 | String pkg = mContext.getPackageName(); |
Julia Reynolds | da30354 | 2015-11-23 14:00:20 -0500 | [diff] [blame] | 232 | // Fix the notification as best we can. |
| 233 | Notification.addFieldsFromContext(mContext, notification); |
Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 234 | if (notification.sound != null) { |
| 235 | notification.sound = notification.sound.getCanonicalUri(); |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 236 | if (StrictMode.vmFileUriExposureEnabled()) { |
Jeff Sharkey | ac3be9a | 2016-02-01 10:39:30 -0700 | [diff] [blame] | 237 | notification.sound.checkFileUriExposed("Notification.sound"); |
Jeff Sharkey | a14acd2 | 2013-04-02 18:27:45 -0700 | [diff] [blame] | 238 | } |
Jeff Sharkey | 65c4a2b | 2012-09-25 17:22:27 -0700 | [diff] [blame] | 239 | } |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 240 | fixLegacySmallIcon(notification, pkg); |
Julia Reynolds | d9228f1 | 2015-10-20 10:37:27 -0400 | [diff] [blame] | 241 | if (mContext.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1) { |
| 242 | if (notification.getSmallIcon() == null) { |
| 243 | throw new IllegalArgumentException("Invalid notification (no valid small icon): " |
| 244 | + notification); |
| 245 | } |
| 246 | } |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 247 | if (localLOGV) Log.v(TAG, pkg + ": notify(" + id + ", " + notification + ")"); |
Julia Reynolds | d9228f1 | 2015-10-20 10:37:27 -0400 | [diff] [blame] | 248 | final Notification copy = notification.clone(); |
Julia Reynolds | d4ea741 | 2016-02-17 14:00:56 -0500 | [diff] [blame] | 249 | Builder.stripForDelivery(copy); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 250 | try { |
Dianne Hackborn | 95d7853 | 2013-09-11 09:51:14 -0700 | [diff] [blame] | 251 | service.enqueueNotificationWithTag(pkg, mContext.getOpPackageName(), tag, id, |
Julia Reynolds | d9228f1 | 2015-10-20 10:37:27 -0400 | [diff] [blame] | 252 | copy, idOut, user.getIdentifier()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | if (id != idOut[0]) { |
| 254 | Log.w(TAG, "notify: id corrupted: sent " + id + ", got back " + idOut[0]); |
| 255 | } |
| 256 | } catch (RemoteException e) { |
| 257 | } |
| 258 | } |
| 259 | |
Dan Sandler | d63f932 | 2015-05-06 15:18:49 -0400 | [diff] [blame] | 260 | private void fixLegacySmallIcon(Notification n, String pkg) { |
| 261 | if (n.getSmallIcon() == null && n.icon != 0) { |
| 262 | n.setSmallIcon(Icon.createWithResource(pkg, n.icon)); |
| 263 | } |
| 264 | } |
| 265 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | /** |
| 267 | * Cancel a previously shown notification. If it's transient, the view |
| 268 | * will be hidden. If it's persistent, it will be removed from the status |
| 269 | * bar. |
| 270 | */ |
| 271 | public void cancel(int id) |
| 272 | { |
Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 273 | cancel(null, id); |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * Cancel a previously shown notification. If it's transient, the view |
| 278 | * will be hidden. If it's persistent, it will be removed from the status |
| 279 | * bar. |
| 280 | */ |
| 281 | public void cancel(String tag, int id) |
| 282 | { |
Julia Reynolds | d9228f1 | 2015-10-20 10:37:27 -0400 | [diff] [blame] | 283 | cancelAsUser(tag, id, new UserHandle(UserHandle.myUserId())); |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /** |
| 287 | * @hide |
| 288 | */ |
| 289 | public void cancelAsUser(String tag, int id, UserHandle user) |
| 290 | { |
| 291 | INotificationManager service = getService(); |
| 292 | String pkg = mContext.getPackageName(); |
| 293 | if (localLOGV) Log.v(TAG, pkg + ": cancel(" + id + ")"); |
| 294 | try { |
| 295 | service.cancelNotificationWithTag(pkg, tag, id, user.getIdentifier()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | } catch (RemoteException e) { |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Cancel all previously shown notifications. See {@link #cancel} for the |
| 302 | * detailed behavior. |
| 303 | */ |
| 304 | public void cancelAll() |
| 305 | { |
| 306 | INotificationManager service = getService(); |
| 307 | String pkg = mContext.getPackageName(); |
| 308 | if (localLOGV) Log.v(TAG, pkg + ": cancelAll()"); |
| 309 | try { |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 310 | service.cancelAllNotifications(pkg, UserHandle.myUserId()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | } catch (RemoteException e) { |
| 312 | } |
| 313 | } |
| 314 | |
John Spurlock | b478252 | 2014-08-22 14:54:46 -0400 | [diff] [blame] | 315 | /** |
| 316 | * @hide |
| 317 | */ |
| 318 | public ComponentName getEffectsSuppressor() { |
| 319 | INotificationManager service = getService(); |
| 320 | try { |
| 321 | return service.getEffectsSuppressor(); |
| 322 | } catch (RemoteException e) { |
| 323 | return null; |
| 324 | } |
| 325 | } |
| 326 | |
John Spurlock | 2b122f4 | 2014-08-27 16:29:47 -0400 | [diff] [blame] | 327 | /** |
| 328 | * @hide |
| 329 | */ |
| 330 | public boolean matchesCallFilter(Bundle extras) { |
| 331 | INotificationManager service = getService(); |
| 332 | try { |
| 333 | return service.matchesCallFilter(extras); |
| 334 | } catch (RemoteException e) { |
| 335 | return false; |
| 336 | } |
| 337 | } |
| 338 | |
John Spurlock | 530052a | 2014-11-30 16:26:19 -0500 | [diff] [blame] | 339 | /** |
| 340 | * @hide |
| 341 | */ |
| 342 | public boolean isSystemConditionProviderEnabled(String path) { |
| 343 | INotificationManager service = getService(); |
| 344 | try { |
| 345 | return service.isSystemConditionProviderEnabled(path); |
| 346 | } catch (RemoteException e) { |
| 347 | return false; |
| 348 | } |
| 349 | } |
| 350 | |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 351 | /** |
| 352 | * @hide |
| 353 | */ |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 354 | public void setZenMode(int mode, Uri conditionId, String reason) { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 355 | INotificationManager service = getService(); |
| 356 | try { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 357 | service.setZenMode(mode, conditionId, reason); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 358 | } catch (RemoteException e) { |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * @hide |
| 364 | */ |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 365 | public int getZenMode() { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 366 | INotificationManager service = getService(); |
| 367 | try { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 368 | return service.getZenMode(); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 369 | } catch (RemoteException e) { |
| 370 | } |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 371 | return Global.ZEN_MODE_OFF; |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | /** |
| 375 | * @hide |
| 376 | */ |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 377 | public ZenModeConfig getZenModeConfig() { |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 378 | INotificationManager service = getService(); |
| 379 | try { |
John Spurlock | b2278d6 | 2015-04-07 12:47:12 -0400 | [diff] [blame] | 380 | return service.getZenModeConfig(); |
John Spurlock | cdb57ae | 2015-02-11 19:04:11 -0500 | [diff] [blame] | 381 | } catch (RemoteException e) { |
| 382 | } |
| 383 | return null; |
| 384 | } |
| 385 | |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 386 | /** |
Julia Reynolds | 43b70cd | 2016-01-14 15:05:34 -0500 | [diff] [blame] | 387 | * @hide |
| 388 | */ |
| 389 | public int getRuleInstanceCount(ComponentName owner) { |
| 390 | INotificationManager service = getService(); |
| 391 | try { |
| 392 | return service.getRuleInstanceCount(owner); |
| 393 | } catch (RemoteException e) { |
| 394 | } |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | /** |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 399 | * Returns AutomaticZenRules owned by the caller. |
| 400 | * |
| 401 | * <p> |
| 402 | * Only available if policy access is granted to this package. |
| 403 | * See {@link #isNotificationPolicyAccessGranted}. |
| 404 | */ |
| 405 | public List<AutomaticZenRule> getAutomaticZenRules() { |
| 406 | INotificationManager service = getService(); |
| 407 | try { |
| 408 | return service.getAutomaticZenRules(); |
| 409 | } catch (RemoteException e) { |
| 410 | } |
| 411 | return null; |
| 412 | } |
| 413 | |
| 414 | /** |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 415 | * Returns the AutomaticZenRule with the given id, if it exists and the caller has access. |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 416 | * |
| 417 | * <p> |
| 418 | * Only available if policy access is granted to this package. |
| 419 | * See {@link #isNotificationPolicyAccessGranted}. |
| 420 | * |
| 421 | * <p> |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 422 | * Returns null if there are no zen rules that match the given id, or if the calling package |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 423 | * doesn't own the matching rule. See {@link AutomaticZenRule#getOwner}. |
| 424 | */ |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 425 | public AutomaticZenRule getAutomaticZenRule(String id) { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 426 | INotificationManager service = getService(); |
| 427 | try { |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 428 | return service.getAutomaticZenRule(id); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 429 | } catch (RemoteException e) { |
| 430 | } |
| 431 | return null; |
| 432 | } |
| 433 | |
| 434 | /** |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 435 | * Creates the given zen rule. |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 436 | * |
| 437 | * <p> |
| 438 | * Only available if policy access is granted to this package. |
| 439 | * See {@link #isNotificationPolicyAccessGranted}. |
| 440 | * |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 441 | * @param automaticZenRule the rule to create. |
| 442 | * @return A fully populated {@link AutomaticZenRule} if the rule was persisted successfully, |
| 443 | * null otherwise. |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 444 | */ |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 445 | public AutomaticZenRule addAutomaticZenRule(AutomaticZenRule automaticZenRule) { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 446 | INotificationManager service = getService(); |
| 447 | try { |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 448 | return service.addAutomaticZenRule(automaticZenRule); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 449 | } catch (RemoteException e) { |
| 450 | } |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 451 | return null; |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | /** |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 455 | * Updates the given zen rule. |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 456 | * |
| 457 | * <p> |
| 458 | * Only available if policy access is granted to this package. |
| 459 | * See {@link #isNotificationPolicyAccessGranted}. |
| 460 | * |
| 461 | * <p> |
| 462 | * Callers can only update rules that they own. See {@link AutomaticZenRule#getOwner}. |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 463 | * @param automaticZenRule the rule to update. |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 464 | * @return Whether the rule was successfully updated. |
| 465 | */ |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 466 | public boolean updateAutomaticZenRule(AutomaticZenRule automaticZenRule) { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 467 | INotificationManager service = getService(); |
| 468 | try { |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 469 | return service.updateAutomaticZenRule(automaticZenRule); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 470 | } catch (RemoteException e) { |
| 471 | } |
| 472 | return false; |
| 473 | } |
| 474 | |
| 475 | /** |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 476 | * Deletes the automatic zen rule with the given id. |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 477 | * |
| 478 | * <p> |
| 479 | * Only available if policy access is granted to this package. |
| 480 | * See {@link #isNotificationPolicyAccessGranted}. |
| 481 | * |
| 482 | * <p> |
| 483 | * Callers can only delete rules that they own. See {@link AutomaticZenRule#getOwner}. |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 484 | * @param id the id of the rule to delete. |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 485 | * @return Whether the rule was successfully deleted. |
| 486 | */ |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 487 | public boolean removeAutomaticZenRule(String id) { |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 488 | INotificationManager service = getService(); |
| 489 | try { |
Julia Reynolds | 4fe98d6 | 2015-10-06 16:23:41 -0400 | [diff] [blame] | 490 | return service.removeAutomaticZenRule(id); |
Julia Reynolds | a47a27f | 2015-08-24 08:31:47 -0400 | [diff] [blame] | 491 | } catch (RemoteException e) { |
| 492 | } |
| 493 | return false; |
| 494 | } |
| 495 | |
| 496 | /** |
Julia Reynolds | c8e54e8 | 2015-11-30 16:43:05 -0500 | [diff] [blame] | 497 | * Deletes all automatic zen rules owned by the given package. |
| 498 | * |
| 499 | * @hide |
| 500 | */ |
| 501 | public boolean removeAutomaticZenRules(String packageName) { |
| 502 | INotificationManager service = getService(); |
| 503 | try { |
| 504 | return service.removeAutomaticZenRules(packageName); |
| 505 | } catch (RemoteException e) { |
| 506 | } |
| 507 | return false; |
| 508 | } |
| 509 | |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 510 | public int getImportance() { |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 511 | INotificationManager service = getService(); |
| 512 | try { |
Julia Reynolds | ef37f28 | 2016-02-12 09:11:27 -0500 | [diff] [blame] | 513 | return service.getPackageImportance(mContext.getPackageName()); |
Julia Reynolds | 81afbcd | 2016-02-09 14:54:08 -0500 | [diff] [blame] | 514 | } catch (RemoteException e) { |
| 515 | } |
| 516 | return NotificationListenerService.Ranking.IMPORTANCE_UNSPECIFIED; |
| 517 | } |
| 518 | |
| 519 | public boolean areNotificationsEnabled() { |
| 520 | INotificationManager service = getService(); |
| 521 | try { |
| 522 | return service.areNotificationsEnabled(mContext.getPackageName()); |
| 523 | } catch (RemoteException e) { |
| 524 | } |
| 525 | return false; |
| 526 | } |
| 527 | |
Julia Reynolds | c8e54e8 | 2015-11-30 16:43:05 -0500 | [diff] [blame] | 528 | /** |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 529 | * Checks the ability to read/modify notification policy for the calling package. |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 530 | * |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 531 | * <p> |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 532 | * Returns true if the calling package can read/modify notification policy. |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 533 | * |
| 534 | * <p> |
| 535 | * Request policy access by sending the user to the activity that matches the system intent |
| 536 | * action {@link android.provider.Settings#ACTION_NOTIFICATION_POLICY_ACCESS_SETTINGS}. |
| 537 | * |
| 538 | * <p> |
| 539 | * Use {@link #ACTION_NOTIFICATION_POLICY_ACCESS_GRANTED_CHANGED} to listen for |
| 540 | * user grant or denial of this access. |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 541 | */ |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 542 | public boolean isNotificationPolicyAccessGranted() { |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 543 | INotificationManager service = getService(); |
| 544 | try { |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 545 | return service.isNotificationPolicyAccessGranted(mContext.getOpPackageName()); |
| 546 | } catch (RemoteException e) { |
| 547 | } |
| 548 | return false; |
| 549 | } |
| 550 | |
| 551 | /** @hide */ |
| 552 | public boolean isNotificationPolicyAccessGrantedForPackage(String pkg) { |
| 553 | INotificationManager service = getService(); |
| 554 | try { |
| 555 | return service.isNotificationPolicyAccessGrantedForPackage(pkg); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 556 | } catch (RemoteException e) { |
| 557 | } |
| 558 | return false; |
| 559 | } |
| 560 | |
| 561 | /** |
| 562 | * Gets the current notification policy. |
| 563 | * |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 564 | * <p> |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 565 | * Only available if policy access is granted to this package. |
| 566 | * See {@link #isNotificationPolicyAccessGranted}. |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 567 | */ |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 568 | public Policy getNotificationPolicy() { |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 569 | INotificationManager service = getService(); |
| 570 | try { |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 571 | return service.getNotificationPolicy(mContext.getOpPackageName()); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 572 | } catch (RemoteException e) { |
| 573 | } |
| 574 | return null; |
| 575 | } |
| 576 | |
| 577 | /** |
| 578 | * Sets the current notification policy. |
| 579 | * |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 580 | * <p> |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 581 | * Only available if policy access is granted to this package. |
| 582 | * See {@link #isNotificationPolicyAccessGranted}. |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 583 | * |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 584 | * @param policy The new desired policy. |
| 585 | */ |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 586 | public void setNotificationPolicy(@NonNull Policy policy) { |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 587 | checkRequired("policy", policy); |
| 588 | INotificationManager service = getService(); |
| 589 | try { |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 590 | service.setNotificationPolicy(mContext.getOpPackageName(), policy); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 591 | } catch (RemoteException e) { |
| 592 | } |
| 593 | } |
| 594 | |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 595 | /** @hide */ |
| 596 | public void setNotificationPolicyAccessGranted(String pkg, boolean granted) { |
| 597 | INotificationManager service = getService(); |
| 598 | try { |
| 599 | service.setNotificationPolicyAccessGranted(pkg, granted); |
| 600 | } catch (RemoteException e) { |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | /** @hide */ |
| 605 | public ArraySet<String> getPackagesRequestingNotificationPolicyAccess() { |
| 606 | INotificationManager service = getService(); |
| 607 | try { |
| 608 | final String[] pkgs = service.getPackagesRequestingNotificationPolicyAccess(); |
| 609 | if (pkgs != null && pkgs.length > 0) { |
| 610 | final ArraySet<String> rt = new ArraySet<>(pkgs.length); |
| 611 | for (int i = 0; i < pkgs.length; i++) { |
| 612 | rt.add(pkgs[i]); |
| 613 | } |
| 614 | return rt; |
| 615 | } |
| 616 | } catch (RemoteException e) { |
| 617 | } |
| 618 | return new ArraySet<String>(); |
| 619 | } |
| 620 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 621 | private Context mContext; |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 622 | |
| 623 | private static void checkRequired(String name, Object value) { |
| 624 | if (value == null) { |
| 625 | throw new IllegalArgumentException(name + " is required"); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | /** |
| 630 | * Notification policy configuration. Represents user-preferences for notification |
| 631 | * filtering and prioritization. |
| 632 | */ |
| 633 | public static class Policy implements android.os.Parcelable { |
| 634 | /** Reminder notifications are prioritized. */ |
| 635 | public static final int PRIORITY_CATEGORY_REMINDERS = 1 << 0; |
| 636 | /** Event notifications are prioritized. */ |
| 637 | public static final int PRIORITY_CATEGORY_EVENTS = 1 << 1; |
| 638 | /** Message notifications are prioritized. */ |
| 639 | public static final int PRIORITY_CATEGORY_MESSAGES = 1 << 2; |
| 640 | /** Calls are prioritized. */ |
| 641 | public static final int PRIORITY_CATEGORY_CALLS = 1 << 3; |
| 642 | /** Calls from repeat callers are prioritized. */ |
| 643 | public static final int PRIORITY_CATEGORY_REPEAT_CALLERS = 1 << 4; |
| 644 | |
| 645 | private static final int[] ALL_PRIORITY_CATEGORIES = { |
| 646 | PRIORITY_CATEGORY_REMINDERS, |
| 647 | PRIORITY_CATEGORY_EVENTS, |
| 648 | PRIORITY_CATEGORY_MESSAGES, |
| 649 | PRIORITY_CATEGORY_CALLS, |
| 650 | PRIORITY_CATEGORY_REPEAT_CALLERS, |
| 651 | }; |
| 652 | |
| 653 | /** Any sender is prioritized. */ |
| 654 | public static final int PRIORITY_SENDERS_ANY = 0; |
| 655 | /** Saved contacts are prioritized. */ |
| 656 | public static final int PRIORITY_SENDERS_CONTACTS = 1; |
| 657 | /** Only starred contacts are prioritized. */ |
| 658 | public static final int PRIORITY_SENDERS_STARRED = 2; |
| 659 | |
| 660 | /** Notification categories to prioritize. Bitmask of PRIORITY_CATEGORY_* constants. */ |
| 661 | public final int priorityCategories; |
| 662 | |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 663 | /** Notification senders to prioritize for calls. One of: |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 664 | * PRIORITY_SENDERS_ANY, PRIORITY_SENDERS_CONTACTS, PRIORITY_SENDERS_STARRED */ |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 665 | public final int priorityCallSenders; |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 666 | |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 667 | /** Notification senders to prioritize for messages. One of: |
| 668 | * PRIORITY_SENDERS_ANY, PRIORITY_SENDERS_CONTACTS, PRIORITY_SENDERS_STARRED */ |
| 669 | public final int priorityMessageSenders; |
| 670 | |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 671 | /** |
| 672 | * @hide |
| 673 | */ |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 674 | public static final int SUPPRESSED_EFFECTS_UNSET = -1; |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 675 | /** |
| 676 | * Whether notification suppressed by DND should not interruption visually when the screen |
| 677 | * is off. |
| 678 | */ |
| 679 | public static final int SUPPRESSED_EFFECT_SCREEN_OFF = 1 << 0; |
| 680 | /** |
| 681 | * Whether notification suppressed by DND should not interruption visually when the screen |
| 682 | * is on. |
| 683 | */ |
| 684 | public static final int SUPPRESSED_EFFECT_SCREEN_ON = 1 << 1; |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 685 | |
| 686 | private static final int[] ALL_SUPPRESSED_EFFECTS = { |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 687 | SUPPRESSED_EFFECT_SCREEN_OFF, |
Julia Reynolds | 6172158 | 2016-01-05 08:35:25 -0500 | [diff] [blame] | 688 | SUPPRESSED_EFFECT_SCREEN_ON, |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 689 | }; |
| 690 | |
| 691 | /** |
| 692 | * Visual effects to suppress for a notification that is filtered by Do Not Disturb mode. |
| 693 | * Bitmask of SUPPRESSED_EFFECT_* constants. |
| 694 | */ |
| 695 | public final int suppressedVisualEffects; |
| 696 | |
| 697 | |
| 698 | @Deprecated |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 699 | public Policy(int priorityCategories, int priorityCallSenders, int priorityMessageSenders) { |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 700 | this(priorityCategories, priorityCallSenders, priorityMessageSenders, |
| 701 | SUPPRESSED_EFFECTS_UNSET); |
| 702 | } |
| 703 | |
| 704 | public Policy(int priorityCategories, int priorityCallSenders, int priorityMessageSenders, |
| 705 | int suppressedVisualEffects) { |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 706 | this.priorityCategories = priorityCategories; |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 707 | this.priorityCallSenders = priorityCallSenders; |
| 708 | this.priorityMessageSenders = priorityMessageSenders; |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 709 | this.suppressedVisualEffects = suppressedVisualEffects; |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | /** @hide */ |
| 713 | public Policy(Parcel source) { |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 714 | this(source.readInt(), source.readInt(), source.readInt(), source.readInt()); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | @Override |
| 718 | public void writeToParcel(Parcel dest, int flags) { |
| 719 | dest.writeInt(priorityCategories); |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 720 | dest.writeInt(priorityCallSenders); |
| 721 | dest.writeInt(priorityMessageSenders); |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 722 | dest.writeInt(suppressedVisualEffects); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 723 | } |
| 724 | |
| 725 | @Override |
| 726 | public int describeContents() { |
| 727 | return 0; |
| 728 | } |
| 729 | |
| 730 | @Override |
| 731 | public int hashCode() { |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 732 | return Objects.hash(priorityCategories, priorityCallSenders, priorityMessageSenders, |
| 733 | suppressedVisualEffects); |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | @Override |
| 737 | public boolean equals(Object o) { |
| 738 | if (!(o instanceof Policy)) return false; |
| 739 | if (o == this) return true; |
| 740 | final Policy other = (Policy) o; |
| 741 | return other.priorityCategories == priorityCategories |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 742 | && other.priorityCallSenders == priorityCallSenders |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 743 | && other.priorityMessageSenders == priorityMessageSenders |
| 744 | && other.suppressedVisualEffects == suppressedVisualEffects; |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | @Override |
| 748 | public String toString() { |
| 749 | return "NotificationManager.Policy[" |
| 750 | + "priorityCategories=" + priorityCategoriesToString(priorityCategories) |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 751 | + ",priorityCallSenders=" + prioritySendersToString(priorityCallSenders) |
| 752 | + ",priorityMessageSenders=" + prioritySendersToString(priorityMessageSenders) |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 753 | + ",suppressedVisualEffects=" |
| 754 | + suppressedEffectsToString(suppressedVisualEffects) |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 755 | + "]"; |
| 756 | } |
| 757 | |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 758 | public static String suppressedEffectsToString(int effects) { |
| 759 | if (effects <= 0) return ""; |
| 760 | final StringBuilder sb = new StringBuilder(); |
| 761 | for (int i = 0; i < ALL_SUPPRESSED_EFFECTS.length; i++) { |
| 762 | final int effect = ALL_SUPPRESSED_EFFECTS[i]; |
| 763 | if ((effects & effect) != 0) { |
| 764 | if (sb.length() > 0) sb.append(','); |
| 765 | sb.append(effectToString(effect)); |
| 766 | } |
| 767 | effects &= ~effect; |
| 768 | } |
| 769 | if (effects != 0) { |
| 770 | if (sb.length() > 0) sb.append(','); |
| 771 | sb.append("UNKNOWN_").append(effects); |
| 772 | } |
| 773 | return sb.toString(); |
| 774 | } |
| 775 | |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 776 | public static String priorityCategoriesToString(int priorityCategories) { |
| 777 | if (priorityCategories == 0) return ""; |
| 778 | final StringBuilder sb = new StringBuilder(); |
| 779 | for (int i = 0; i < ALL_PRIORITY_CATEGORIES.length; i++) { |
| 780 | final int priorityCategory = ALL_PRIORITY_CATEGORIES[i]; |
| 781 | if ((priorityCategories & priorityCategory) != 0) { |
| 782 | if (sb.length() > 0) sb.append(','); |
| 783 | sb.append(priorityCategoryToString(priorityCategory)); |
| 784 | } |
| 785 | priorityCategories &= ~priorityCategory; |
| 786 | } |
| 787 | if (priorityCategories != 0) { |
| 788 | if (sb.length() > 0) sb.append(','); |
| 789 | sb.append("PRIORITY_CATEGORY_UNKNOWN_").append(priorityCategories); |
| 790 | } |
| 791 | return sb.toString(); |
| 792 | } |
| 793 | |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 794 | private static String effectToString(int effect) { |
| 795 | switch (effect) { |
Julia Reynolds | d560729 | 2016-02-05 15:25:58 -0500 | [diff] [blame] | 796 | case SUPPRESSED_EFFECT_SCREEN_OFF: return "SUPPRESSED_EFFECT_SCREEN_OFF"; |
Julia Reynolds | 6172158 | 2016-01-05 08:35:25 -0500 | [diff] [blame] | 797 | case SUPPRESSED_EFFECT_SCREEN_ON: return "SUPPRESSED_EFFECT_SCREEN_ON"; |
Julia Reynolds | f612869ae | 2015-11-05 16:48:55 -0500 | [diff] [blame] | 798 | case SUPPRESSED_EFFECTS_UNSET: return "SUPPRESSED_EFFECTS_UNSET"; |
| 799 | default: return "UNKNOWN_" + effect; |
| 800 | } |
| 801 | } |
| 802 | |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 803 | private static String priorityCategoryToString(int priorityCategory) { |
| 804 | switch (priorityCategory) { |
| 805 | case PRIORITY_CATEGORY_REMINDERS: return "PRIORITY_CATEGORY_REMINDERS"; |
| 806 | case PRIORITY_CATEGORY_EVENTS: return "PRIORITY_CATEGORY_EVENTS"; |
| 807 | case PRIORITY_CATEGORY_MESSAGES: return "PRIORITY_CATEGORY_MESSAGES"; |
| 808 | case PRIORITY_CATEGORY_CALLS: return "PRIORITY_CATEGORY_CALLS"; |
| 809 | case PRIORITY_CATEGORY_REPEAT_CALLERS: return "PRIORITY_CATEGORY_REPEAT_CALLERS"; |
| 810 | default: return "PRIORITY_CATEGORY_UNKNOWN_" + priorityCategory; |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | public static String prioritySendersToString(int prioritySenders) { |
| 815 | switch (prioritySenders) { |
| 816 | case PRIORITY_SENDERS_ANY: return "PRIORITY_SENDERS_ANY"; |
| 817 | case PRIORITY_SENDERS_CONTACTS: return "PRIORITY_SENDERS_CONTACTS"; |
| 818 | case PRIORITY_SENDERS_STARRED: return "PRIORITY_SENDERS_STARRED"; |
| 819 | default: return "PRIORITY_SENDERS_UNKNOWN_" + prioritySenders; |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | public static final Parcelable.Creator<Policy> CREATOR = new Parcelable.Creator<Policy>() { |
| 824 | @Override |
| 825 | public Policy createFromParcel(Parcel in) { |
| 826 | return new Policy(in); |
| 827 | } |
| 828 | |
| 829 | @Override |
| 830 | public Policy[] newArray(int size) { |
| 831 | return new Policy[size]; |
| 832 | } |
| 833 | }; |
| 834 | |
John Spurlock | 1fc476d | 2015-04-14 16:05:20 -0400 | [diff] [blame] | 835 | } |
| 836 | |
Dan Sandler | 994349c | 2015-04-15 11:02:54 -0400 | [diff] [blame] | 837 | /** |
| 838 | * Recover a list of active notifications: ones that have been posted by the calling app that |
| 839 | * have not yet been dismissed by the user or {@link #cancel(String, int)}ed by the app. |
| 840 | * |
| 841 | * Each notification is embedded in a {@link StatusBarNotification} object, including the |
| 842 | * original <code>tag</code> and <code>id</code> supplied to |
| 843 | * {@link #notify(String, int, Notification) notify()} |
| 844 | * (via {@link StatusBarNotification#getTag() getTag()} and |
| 845 | * {@link StatusBarNotification#getId() getId()}) as well as a copy of the original |
| 846 | * {@link Notification} object (via {@link StatusBarNotification#getNotification()}). |
| 847 | * |
| 848 | * @return An array of {@link StatusBarNotification}. |
| 849 | */ |
| 850 | public StatusBarNotification[] getActiveNotifications() { |
| 851 | final INotificationManager service = getService(); |
| 852 | final String pkg = mContext.getPackageName(); |
| 853 | try { |
| 854 | final ParceledListSlice<StatusBarNotification> parceledList |
| 855 | = service.getAppActiveNotifications(pkg, UserHandle.myUserId()); |
| 856 | final List<StatusBarNotification> list = parceledList.getList(); |
| 857 | return list.toArray(new StatusBarNotification[list.size()]); |
| 858 | } catch (RemoteException e) { |
| 859 | Log.e(TAG, "Unable to talk to notification manager. Woe!", e); |
| 860 | } |
| 861 | return new StatusBarNotification[0]; |
| 862 | } |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 863 | |
| 864 | /** |
| 865 | * Gets the current notification interruption filter. |
| 866 | * |
| 867 | * <p> |
| 868 | * The interruption filter defines which notifications are allowed to interrupt the user |
| 869 | * (e.g. via sound & vibration) and is applied globally. |
| 870 | * @return One of the INTERRUPTION_FILTER_ constants, or INTERRUPTION_FILTER_UNKNOWN when |
| 871 | * unavailable. |
| 872 | * |
| 873 | * <p> |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 874 | * Only available if policy access is granted to this package. |
| 875 | * See {@link #isNotificationPolicyAccessGranted}. |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 876 | */ |
| 877 | public final int getCurrentInterruptionFilter() { |
| 878 | final INotificationManager service = getService(); |
| 879 | try { |
| 880 | return zenModeToInterruptionFilter(service.getZenMode()); |
| 881 | } catch (RemoteException e) { |
| 882 | Log.e(TAG, "Unable to talk to notification manager. Woe!", e); |
| 883 | } |
| 884 | return INTERRUPTION_FILTER_UNKNOWN; |
| 885 | } |
| 886 | |
| 887 | /** |
| 888 | * Sets the current notification interruption filter. |
| 889 | * |
| 890 | * <p> |
| 891 | * The interruption filter defines which notifications are allowed to interrupt the user |
| 892 | * (e.g. via sound & vibration) and is applied globally. |
| 893 | * @return One of the INTERRUPTION_FILTER_ constants, or INTERRUPTION_FILTER_UNKNOWN when |
| 894 | * unavailable. |
| 895 | * |
| 896 | * <p> |
John Spurlock | 7c74f78 | 2015-06-04 13:01:42 -0400 | [diff] [blame] | 897 | * Only available if policy access is granted to this package. |
| 898 | * See {@link #isNotificationPolicyAccessGranted}. |
John Spurlock | 8077493 | 2015-05-07 17:38:50 -0400 | [diff] [blame] | 899 | */ |
| 900 | public final void setInterruptionFilter(int interruptionFilter) { |
| 901 | final INotificationManager service = getService(); |
| 902 | try { |
| 903 | service.setInterruptionFilter(mContext.getOpPackageName(), interruptionFilter); |
| 904 | } catch (RemoteException e) { |
| 905 | Log.e(TAG, "Unable to talk to notification manager. Woe!", e); |
| 906 | } |
| 907 | } |
| 908 | |
| 909 | /** @hide */ |
| 910 | public static int zenModeToInterruptionFilter(int zen) { |
| 911 | switch (zen) { |
| 912 | case Global.ZEN_MODE_OFF: return INTERRUPTION_FILTER_ALL; |
| 913 | case Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS: return INTERRUPTION_FILTER_PRIORITY; |
| 914 | case Global.ZEN_MODE_ALARMS: return INTERRUPTION_FILTER_ALARMS; |
| 915 | case Global.ZEN_MODE_NO_INTERRUPTIONS: return INTERRUPTION_FILTER_NONE; |
| 916 | default: return INTERRUPTION_FILTER_UNKNOWN; |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | /** @hide */ |
| 921 | public static int zenModeFromInterruptionFilter(int interruptionFilter, int defValue) { |
| 922 | switch (interruptionFilter) { |
| 923 | case INTERRUPTION_FILTER_ALL: return Global.ZEN_MODE_OFF; |
| 924 | case INTERRUPTION_FILTER_PRIORITY: return Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; |
| 925 | case INTERRUPTION_FILTER_ALARMS: return Global.ZEN_MODE_ALARMS; |
| 926 | case INTERRUPTION_FILTER_NONE: return Global.ZEN_MODE_NO_INTERRUPTIONS; |
| 927 | default: return defValue; |
| 928 | } |
| 929 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 930 | } |