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