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