blob: 16aee786d80c7520fd86fa5cbbe02a1b8787c870 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.app;
18
Julia Reynolds81afbcd2016-02-09 14:54:08 -050019import com.android.internal.util.Preconditions;
20
Julia Reynolds0edb50c2016-02-26 14:08:25 -050021import android.annotation.IntDef;
John Spurlock1fc476d2015-04-14 16:05:20 -040022import android.annotation.NonNull;
23import android.annotation.Nullable;
John Spurlockb4782522014-08-22 14:54:46 -040024import android.annotation.SdkConstant;
Christoph Studer4600f9b2014-07-22 22:44:43 +020025import android.app.Notification.Builder;
John Spurlockb4782522014-08-22 14:54:46 -040026import android.content.ComponentName;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.content.Context;
Dan Sandler994349c2015-04-15 11:02:54 -040028import android.content.pm.ParceledListSlice;
Dan Sandlerd63f9322015-05-06 15:18:49 -040029import android.graphics.drawable.Icon;
John Spurlockb2278d62015-04-07 12:47:12 -040030import android.net.Uri;
Dan Sandler4e787062015-06-17 15:09:48 -040031import android.os.Build;
John Spurlock2b122f42014-08-27 16:29:47 -040032import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.os.Handler;
34import android.os.IBinder;
John Spurlock1fc476d2015-04-14 16:05:20 -040035import android.os.Parcel;
36import android.os.Parcelable;
Jeff Sharkey69ddab42012-08-25 00:05:46 -070037import android.os.RemoteException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.os.ServiceManager;
Jeff Sharkeya14acd22013-04-02 18:27:45 -070039import android.os.StrictMode;
Dianne Hackborn41203752012-08-31 14:05:51 -070040import android.os.UserHandle;
John Spurlockb2278d62015-04-07 12:47:12 -040041import android.provider.Settings.Global;
John Spurlockcdb57ae2015-02-11 19:04:11 -050042import android.service.notification.IConditionListener;
Julia Reynolds81afbcd2016-02-09 14:54:08 -050043import android.service.notification.NotificationListenerService;
Dan Sandler994349c2015-04-15 11:02:54 -040044import android.service.notification.StatusBarNotification;
John Spurlockcdb57ae2015-02-11 19:04:11 -050045import android.service.notification.ZenModeConfig;
John Spurlock80774932015-05-07 17:38:50 -040046import android.util.ArraySet;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.util.Log;
48
Julia Reynolds0edb50c2016-02-26 14:08:25 -050049import java.lang.annotation.Retention;
50import java.lang.annotation.RetentionPolicy;
Julia Reynolds361e82d32016-02-26 18:19:49 -050051import java.util.Collection;
52import java.util.HashMap;
53import java.util.Map;
John Spurlock1fc476d2015-04-14 16:05:20 -040054import java.util.Objects;
Dan Sandler994349c2015-04-15 11:02:54 -040055import java.util.List;
Julia Reynolds361e82d32016-02-26 18:19:49 -050056import java.util.Set;
John Spurlock1fc476d2015-04-14 16:05:20 -040057
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058/**
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 Collingbourneb97c3492010-10-13 20:04:52 +010073 * 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 Project9066cfe2009-03-03 19:31:44 -080084 *
85 * <p>
86 * You do not instantiate this class directly; instead, retrieve it through
87 * {@link android.content.Context#getSystemService}.
88 *
Joe Fernandez558459f2011-10-13 16:47:36 -070089 * <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 Project9066cfe2009-03-03 19:31:44 -080096 * @see android.app.Notification
97 * @see android.content.Context#getSystemService
98 */
99public class NotificationManager
100{
101 private static String TAG = "NotificationManager";
Joe Onorato43a17652011-04-06 19:22:23 -0700102 private static boolean localLOGV = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103
John Spurlockb4782522014-08-22 14:54:46 -0400104 /**
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 Spurlock1fc476d2015-04-14 16:05:20 -0400114 /**
John Spurlock7c74f782015-06-04 13:01:42 -0400115 * 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 Spurlock1fc476d2015-04-14 16:05:20 -0400125 * 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 Spurlock80774932015-05-07 17:38:50 -0400132 /**
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 Monka9927322015-12-13 16:22:37 -0500141 * 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 Reynolds0edb50c2016-02-26 14:08:25 -0500148
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 Monka9927322015-12-13 16:22:37 -0500155 /**
John Spurlock80774932015-05-07 17:38:50 -0400156 * {@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 Project9066cfe2009-03-03 19:31:44 -0800184 private static INotificationManager sService;
185
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700186 /** @hide */
187 static public INotificationManager getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 {
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 Sharkey69ddab42012-08-25 00:05:46 -0700202 /** {@hide} */
203 public static NotificationManager from(Context context) {
204 return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
205 }
206
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 /**
Daniel Sandlere97a3bc2011-02-07 16:47:07 -0500208 * 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 Project9066cfe2009-03-03 19:31:44 -0800211 *
212 * @param id An identifier for this notification unique within your
213 * application.
Daniel Sandlere97a3bc2011-02-07 16:47:07 -0500214 * @param notification A {@link Notification} object describing what to show the user. Must not
215 * be null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 */
217 public void notify(int id, Notification notification)
218 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700219 notify(null, id, notification);
220 }
221
222 /**
Daniel Sandlere97a3bc2011-02-07 16:47:07 -0500223 * 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 Quintana6ecaff12009-09-25 14:23:13 -0700226 *
Peter Collingbourneb97c3492010-10-13 20:04:52 +0100227 * @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 Sandlere97a3bc2011-02-07 16:47:07 -0500230 * @param notification A {@link Notification} object describing what to
231 * show the user. Must not be null.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700232 */
233 public void notify(String tag, int id, Notification notification)
234 {
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400235 notifyAsUser(tag, id, notification, new UserHandle(UserHandle.myUserId()));
Dianne Hackborn41203752012-08-31 14:05:51 -0700236 }
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 Reynoldsda303542015-11-23 14:00:20 -0500246 // Fix the notification as best we can.
247 Notification.addFieldsFromContext(mContext, notification);
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -0700248 if (notification.sound != null) {
249 notification.sound = notification.sound.getCanonicalUri();
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700250 if (StrictMode.vmFileUriExposureEnabled()) {
Jeff Sharkeyac3be9a2016-02-01 10:39:30 -0700251 notification.sound.checkFileUriExposed("Notification.sound");
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700252 }
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -0700253 }
Dan Sandlerd63f9322015-05-06 15:18:49 -0400254 fixLegacySmallIcon(notification, pkg);
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400255 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 Hackborn41203752012-08-31 14:05:51 -0700261 if (localLOGV) Log.v(TAG, pkg + ": notify(" + id + ", " + notification + ")");
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400262 final Notification copy = notification.clone();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500263 Builder.stripForDelivery(copy);
Dianne Hackborn41203752012-08-31 14:05:51 -0700264 try {
Dianne Hackborn95d78532013-09-11 09:51:14 -0700265 service.enqueueNotificationWithTag(pkg, mContext.getOpPackageName(), tag, id,
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400266 copy, idOut, user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 if (id != idOut[0]) {
268 Log.w(TAG, "notify: id corrupted: sent " + id + ", got back " + idOut[0]);
269 }
270 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700271 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 }
273 }
274
Dan Sandlerd63f9322015-05-06 15:18:49 -0400275 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 Project9066cfe2009-03-03 19:31:44 -0800281 /**
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 Quintana6ecaff12009-09-25 14:23:13 -0700288 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 Reynoldsd9228f12015-10-20 10:37:27 -0400298 cancelAsUser(tag, id, new UserHandle(UserHandle.myUserId()));
Dianne Hackborn41203752012-08-31 14:05:51 -0700299 }
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 Project9066cfe2009-03-03 19:31:44 -0800311 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700312 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 }
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 Hackborn41203752012-08-31 14:05:51 -0700326 service.cancelAllNotifications(pkg, UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700328 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800329 }
330 }
331
John Spurlockb4782522014-08-22 14:54:46 -0400332 /**
333 * @hide
334 */
335 public ComponentName getEffectsSuppressor() {
336 INotificationManager service = getService();
337 try {
338 return service.getEffectsSuppressor();
339 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700340 throw e.rethrowFromSystemServer();
John Spurlockb4782522014-08-22 14:54:46 -0400341 }
342 }
343
John Spurlock2b122f42014-08-27 16:29:47 -0400344 /**
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 Sharkeyf8880562016-02-26 13:03:01 -0700352 throw e.rethrowFromSystemServer();
John Spurlock2b122f42014-08-27 16:29:47 -0400353 }
354 }
355
John Spurlock530052a2014-11-30 16:26:19 -0500356 /**
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 Sharkeyf8880562016-02-26 13:03:01 -0700364 throw e.rethrowFromSystemServer();
John Spurlock530052a2014-11-30 16:26:19 -0500365 }
366 }
367
John Spurlockcdb57ae2015-02-11 19:04:11 -0500368 /**
369 * @hide
370 */
John Spurlockb2278d62015-04-07 12:47:12 -0400371 public void setZenMode(int mode, Uri conditionId, String reason) {
John Spurlockcdb57ae2015-02-11 19:04:11 -0500372 INotificationManager service = getService();
373 try {
John Spurlockb2278d62015-04-07 12:47:12 -0400374 service.setZenMode(mode, conditionId, reason);
John Spurlockcdb57ae2015-02-11 19:04:11 -0500375 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700376 throw e.rethrowFromSystemServer();
John Spurlockcdb57ae2015-02-11 19:04:11 -0500377 }
378 }
379
380 /**
381 * @hide
382 */
John Spurlockb2278d62015-04-07 12:47:12 -0400383 public int getZenMode() {
John Spurlockcdb57ae2015-02-11 19:04:11 -0500384 INotificationManager service = getService();
385 try {
John Spurlockb2278d62015-04-07 12:47:12 -0400386 return service.getZenMode();
John Spurlockcdb57ae2015-02-11 19:04:11 -0500387 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700388 throw e.rethrowFromSystemServer();
John Spurlockcdb57ae2015-02-11 19:04:11 -0500389 }
390 }
391
392 /**
393 * @hide
394 */
John Spurlockb2278d62015-04-07 12:47:12 -0400395 public ZenModeConfig getZenModeConfig() {
John Spurlockcdb57ae2015-02-11 19:04:11 -0500396 INotificationManager service = getService();
397 try {
John Spurlockb2278d62015-04-07 12:47:12 -0400398 return service.getZenModeConfig();
John Spurlockcdb57ae2015-02-11 19:04:11 -0500399 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700400 throw e.rethrowFromSystemServer();
John Spurlockcdb57ae2015-02-11 19:04:11 -0500401 }
John Spurlockcdb57ae2015-02-11 19:04:11 -0500402 }
403
John Spurlock1fc476d2015-04-14 16:05:20 -0400404 /**
Julia Reynolds43b70cd2016-01-14 15:05:34 -0500405 * @hide
406 */
407 public int getRuleInstanceCount(ComponentName owner) {
408 INotificationManager service = getService();
409 try {
410 return service.getRuleInstanceCount(owner);
411 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700412 throw e.rethrowFromSystemServer();
Julia Reynolds43b70cd2016-01-14 15:05:34 -0500413 }
Julia Reynolds43b70cd2016-01-14 15:05:34 -0500414 }
415
416 /**
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400417 * Returns AutomaticZenRules owned by the caller.
418 *
419 * <p>
Julia Reynolds361e82d32016-02-26 18:19:49 -0500420 * Throws a SecurityException if policy access is granted to this package.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400421 * See {@link #isNotificationPolicyAccessGranted}.
422 */
Julia Reynolds361e82d32016-02-26 18:19:49 -0500423 public Map<String, AutomaticZenRule> getAutomaticZenRules() {
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400424 INotificationManager service = getService();
425 try {
Julia Reynolds361e82d32016-02-26 18:19:49 -0500426 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 Reynoldsa47a27f2015-08-24 08:31:47 -0400434 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700435 throw e.rethrowFromSystemServer();
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400436 }
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400437 }
438
439 /**
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400440 * Returns the AutomaticZenRule with the given id, if it exists and the caller has access.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400441 *
442 * <p>
Julia Reynolds361e82d32016-02-26 18:19:49 -0500443 * Throws a SecurityException if policy access is granted to this package.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400444 * See {@link #isNotificationPolicyAccessGranted}.
445 *
446 * <p>
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400447 * Returns null if there are no zen rules that match the given id, or if the calling package
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400448 * doesn't own the matching rule. See {@link AutomaticZenRule#getOwner}.
449 */
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400450 public AutomaticZenRule getAutomaticZenRule(String id) {
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400451 INotificationManager service = getService();
452 try {
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400453 return service.getAutomaticZenRule(id);
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400454 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700455 throw e.rethrowFromSystemServer();
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400456 }
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400457 }
458
459 /**
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400460 * Creates the given zen rule.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400461 *
462 * <p>
Julia Reynolds361e82d32016-02-26 18:19:49 -0500463 * Throws a SecurityException if policy access is granted to this package.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400464 * See {@link #isNotificationPolicyAccessGranted}.
465 *
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400466 * @param automaticZenRule the rule to create.
Julia Reynolds361e82d32016-02-26 18:19:49 -0500467 * @return The id of the newly created rule; null if the rule could not be created.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400468 */
Julia Reynolds361e82d32016-02-26 18:19:49 -0500469 public String addAutomaticZenRule(AutomaticZenRule automaticZenRule) {
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400470 INotificationManager service = getService();
471 try {
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400472 return service.addAutomaticZenRule(automaticZenRule);
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400473 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700474 throw e.rethrowFromSystemServer();
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400475 }
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400476 }
477
478 /**
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400479 * Updates the given zen rule.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400480 *
481 * <p>
Julia Reynolds361e82d32016-02-26 18:19:49 -0500482 * Throws a SecurityException if policy access is granted to this package.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400483 * See {@link #isNotificationPolicyAccessGranted}.
484 *
485 * <p>
486 * Callers can only update rules that they own. See {@link AutomaticZenRule#getOwner}.
Julia Reynolds361e82d32016-02-26 18:19:49 -0500487 * @param id The id of the rule to update
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400488 * @param automaticZenRule the rule to update.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400489 * @return Whether the rule was successfully updated.
490 */
Julia Reynolds361e82d32016-02-26 18:19:49 -0500491 public boolean updateAutomaticZenRule(String id, AutomaticZenRule automaticZenRule) {
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400492 INotificationManager service = getService();
493 try {
Julia Reynolds361e82d32016-02-26 18:19:49 -0500494 return service.updateAutomaticZenRule(id, automaticZenRule);
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400495 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700496 throw e.rethrowFromSystemServer();
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400497 }
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400498 }
499
500 /**
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400501 * Deletes the automatic zen rule with the given id.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400502 *
503 * <p>
Julia Reynolds361e82d32016-02-26 18:19:49 -0500504 * Throws a SecurityException if policy access is granted to this package.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400505 * See {@link #isNotificationPolicyAccessGranted}.
506 *
507 * <p>
508 * Callers can only delete rules that they own. See {@link AutomaticZenRule#getOwner}.
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400509 * @param id the id of the rule to delete.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400510 * @return Whether the rule was successfully deleted.
511 */
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400512 public boolean removeAutomaticZenRule(String id) {
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400513 INotificationManager service = getService();
514 try {
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400515 return service.removeAutomaticZenRule(id);
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400516 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700517 throw e.rethrowFromSystemServer();
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400518 }
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400519 }
520
521 /**
Julia Reynoldsc8e54e82015-11-30 16:43:05 -0500522 * 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 Sharkeyf8880562016-02-26 13:03:01 -0700531 throw e.rethrowFromSystemServer();
Julia Reynoldsc8e54e82015-11-30 16:43:05 -0500532 }
Julia Reynoldsc8e54e82015-11-30 16:43:05 -0500533 }
534
Julia Reynolds0edb50c2016-02-26 14:08:25 -0500535 /**
536 * Returns the user specified importance for notifications from the calling package.
537 */
538 public @NotificationListenerService.Ranking.Importance int getImportance() {
Julia Reynolds81afbcd2016-02-09 14:54:08 -0500539 INotificationManager service = getService();
540 try {
Julia Reynoldsef37f282016-02-12 09:11:27 -0500541 return service.getPackageImportance(mContext.getPackageName());
Julia Reynolds81afbcd2016-02-09 14:54:08 -0500542 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700543 throw e.rethrowFromSystemServer();
Julia Reynolds81afbcd2016-02-09 14:54:08 -0500544 }
Julia Reynolds81afbcd2016-02-09 14:54:08 -0500545 }
546
Julia Reynolds0edb50c2016-02-26 14:08:25 -0500547 /**
548 * Returns whether notifications from the calling package are blocked.
549 */
Julia Reynolds81afbcd2016-02-09 14:54:08 -0500550 public boolean areNotificationsEnabled() {
551 INotificationManager service = getService();
552 try {
553 return service.areNotificationsEnabled(mContext.getPackageName());
554 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700555 throw e.rethrowFromSystemServer();
Julia Reynolds81afbcd2016-02-09 14:54:08 -0500556 }
Julia Reynolds81afbcd2016-02-09 14:54:08 -0500557 }
558
Julia Reynoldsc8e54e82015-11-30 16:43:05 -0500559 /**
John Spurlock80774932015-05-07 17:38:50 -0400560 * Checks the ability to read/modify notification policy for the calling package.
John Spurlock1fc476d2015-04-14 16:05:20 -0400561 *
John Spurlock7c74f782015-06-04 13:01:42 -0400562 * <p>
John Spurlock80774932015-05-07 17:38:50 -0400563 * Returns true if the calling package can read/modify notification policy.
John Spurlock7c74f782015-06-04 13:01:42 -0400564 *
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 Spurlock1fc476d2015-04-14 16:05:20 -0400572 */
John Spurlock80774932015-05-07 17:38:50 -0400573 public boolean isNotificationPolicyAccessGranted() {
John Spurlock1fc476d2015-04-14 16:05:20 -0400574 INotificationManager service = getService();
575 try {
John Spurlock80774932015-05-07 17:38:50 -0400576 return service.isNotificationPolicyAccessGranted(mContext.getOpPackageName());
577 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700578 throw e.rethrowFromSystemServer();
John Spurlock80774932015-05-07 17:38:50 -0400579 }
John Spurlock80774932015-05-07 17:38:50 -0400580 }
581
582 /** @hide */
583 public boolean isNotificationPolicyAccessGrantedForPackage(String pkg) {
584 INotificationManager service = getService();
585 try {
586 return service.isNotificationPolicyAccessGrantedForPackage(pkg);
John Spurlock1fc476d2015-04-14 16:05:20 -0400587 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700588 throw e.rethrowFromSystemServer();
John Spurlock1fc476d2015-04-14 16:05:20 -0400589 }
John Spurlock1fc476d2015-04-14 16:05:20 -0400590 }
591
592 /**
593 * Gets the current notification policy.
594 *
John Spurlock80774932015-05-07 17:38:50 -0400595 * <p>
John Spurlock7c74f782015-06-04 13:01:42 -0400596 * Only available if policy access is granted to this package.
597 * See {@link #isNotificationPolicyAccessGranted}.
John Spurlock1fc476d2015-04-14 16:05:20 -0400598 */
John Spurlock80774932015-05-07 17:38:50 -0400599 public Policy getNotificationPolicy() {
John Spurlock1fc476d2015-04-14 16:05:20 -0400600 INotificationManager service = getService();
601 try {
John Spurlock80774932015-05-07 17:38:50 -0400602 return service.getNotificationPolicy(mContext.getOpPackageName());
John Spurlock1fc476d2015-04-14 16:05:20 -0400603 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700604 throw e.rethrowFromSystemServer();
John Spurlock1fc476d2015-04-14 16:05:20 -0400605 }
John Spurlock1fc476d2015-04-14 16:05:20 -0400606 }
607
608 /**
609 * Sets the current notification policy.
610 *
John Spurlock80774932015-05-07 17:38:50 -0400611 * <p>
John Spurlock7c74f782015-06-04 13:01:42 -0400612 * Only available if policy access is granted to this package.
613 * See {@link #isNotificationPolicyAccessGranted}.
John Spurlock80774932015-05-07 17:38:50 -0400614 *
John Spurlock1fc476d2015-04-14 16:05:20 -0400615 * @param policy The new desired policy.
616 */
John Spurlock80774932015-05-07 17:38:50 -0400617 public void setNotificationPolicy(@NonNull Policy policy) {
John Spurlock1fc476d2015-04-14 16:05:20 -0400618 checkRequired("policy", policy);
619 INotificationManager service = getService();
620 try {
John Spurlock80774932015-05-07 17:38:50 -0400621 service.setNotificationPolicy(mContext.getOpPackageName(), policy);
John Spurlock1fc476d2015-04-14 16:05:20 -0400622 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700623 throw e.rethrowFromSystemServer();
John Spurlock1fc476d2015-04-14 16:05:20 -0400624 }
625 }
626
John Spurlock80774932015-05-07 17:38:50 -0400627 /** @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 Sharkeyf8880562016-02-26 13:03:01 -0700633 throw e.rethrowFromSystemServer();
John Spurlock80774932015-05-07 17:38:50 -0400634 }
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 Sharkeyf8880562016-02-26 13:03:01 -0700650 throw e.rethrowFromSystemServer();
John Spurlock80774932015-05-07 17:38:50 -0400651 }
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700652 return new ArraySet<>();
John Spurlock80774932015-05-07 17:38:50 -0400653 }
654
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 private Context mContext;
John Spurlock1fc476d2015-04-14 16:05:20 -0400656
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 Reynoldscedacef2016-03-04 08:18:47 -0500665 * filtering.
John Spurlock1fc476d2015-04-14 16:05:20 -0400666 */
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 Spurlock80774932015-05-07 17:38:50 -0400697 /** Notification senders to prioritize for calls. One of:
John Spurlock1fc476d2015-04-14 16:05:20 -0400698 * PRIORITY_SENDERS_ANY, PRIORITY_SENDERS_CONTACTS, PRIORITY_SENDERS_STARRED */
John Spurlock80774932015-05-07 17:38:50 -0400699 public final int priorityCallSenders;
John Spurlock1fc476d2015-04-14 16:05:20 -0400700
John Spurlock80774932015-05-07 17:38:50 -0400701 /** 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 Reynoldsd5607292016-02-05 15:25:58 -0500705 /**
706 * @hide
707 */
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500708 public static final int SUPPRESSED_EFFECTS_UNSET = -1;
Julia Reynoldsd5607292016-02-05 15:25:58 -0500709 /**
Julia Reynoldscedacef2016-03-04 08:18:47 -0500710 * 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 Reynoldsd5607292016-02-05 15:25:58 -0500712 */
713 public static final int SUPPRESSED_EFFECT_SCREEN_OFF = 1 << 0;
714 /**
Julia Reynoldscedacef2016-03-04 08:18:47 -0500715 * Whether notifications suppressed by DND should not interrupt visually when the screen
716 * is on (e.g. by peeking onto the screen).
Julia Reynoldsd5607292016-02-05 15:25:58 -0500717 */
718 public static final int SUPPRESSED_EFFECT_SCREEN_ON = 1 << 1;
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500719
720 private static final int[] ALL_SUPPRESSED_EFFECTS = {
Julia Reynoldsd5607292016-02-05 15:25:58 -0500721 SUPPRESSED_EFFECT_SCREEN_OFF,
Julia Reynolds61721582016-01-05 08:35:25 -0500722 SUPPRESSED_EFFECT_SCREEN_ON,
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500723 };
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 Reynoldscedacef2016-03-04 08:18:47 -0500731 /**
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 Spurlock80774932015-05-07 17:38:50 -0400738 public Policy(int priorityCategories, int priorityCallSenders, int priorityMessageSenders) {
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500739 this(priorityCategories, priorityCallSenders, priorityMessageSenders,
740 SUPPRESSED_EFFECTS_UNSET);
741 }
742
Julia Reynoldscedacef2016-03-04 08:18:47 -0500743 /**
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 Reynoldsf612869ae2015-11-05 16:48:55 -0500752 public Policy(int priorityCategories, int priorityCallSenders, int priorityMessageSenders,
753 int suppressedVisualEffects) {
John Spurlock1fc476d2015-04-14 16:05:20 -0400754 this.priorityCategories = priorityCategories;
John Spurlock80774932015-05-07 17:38:50 -0400755 this.priorityCallSenders = priorityCallSenders;
756 this.priorityMessageSenders = priorityMessageSenders;
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500757 this.suppressedVisualEffects = suppressedVisualEffects;
John Spurlock1fc476d2015-04-14 16:05:20 -0400758 }
759
760 /** @hide */
761 public Policy(Parcel source) {
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500762 this(source.readInt(), source.readInt(), source.readInt(), source.readInt());
John Spurlock1fc476d2015-04-14 16:05:20 -0400763 }
764
765 @Override
766 public void writeToParcel(Parcel dest, int flags) {
767 dest.writeInt(priorityCategories);
John Spurlock80774932015-05-07 17:38:50 -0400768 dest.writeInt(priorityCallSenders);
769 dest.writeInt(priorityMessageSenders);
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500770 dest.writeInt(suppressedVisualEffects);
John Spurlock1fc476d2015-04-14 16:05:20 -0400771 }
772
773 @Override
774 public int describeContents() {
775 return 0;
776 }
777
778 @Override
779 public int hashCode() {
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500780 return Objects.hash(priorityCategories, priorityCallSenders, priorityMessageSenders,
781 suppressedVisualEffects);
John Spurlock1fc476d2015-04-14 16:05:20 -0400782 }
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 Spurlock80774932015-05-07 17:38:50 -0400790 && other.priorityCallSenders == priorityCallSenders
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500791 && other.priorityMessageSenders == priorityMessageSenders
792 && other.suppressedVisualEffects == suppressedVisualEffects;
John Spurlock1fc476d2015-04-14 16:05:20 -0400793 }
794
795 @Override
796 public String toString() {
797 return "NotificationManager.Policy["
798 + "priorityCategories=" + priorityCategoriesToString(priorityCategories)
John Spurlock80774932015-05-07 17:38:50 -0400799 + ",priorityCallSenders=" + prioritySendersToString(priorityCallSenders)
800 + ",priorityMessageSenders=" + prioritySendersToString(priorityMessageSenders)
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500801 + ",suppressedVisualEffects="
802 + suppressedEffectsToString(suppressedVisualEffects)
John Spurlock1fc476d2015-04-14 16:05:20 -0400803 + "]";
804 }
805
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500806 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 Spurlock1fc476d2015-04-14 16:05:20 -0400824 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 Reynoldsf612869ae2015-11-05 16:48:55 -0500842 private static String effectToString(int effect) {
843 switch (effect) {
Julia Reynoldsd5607292016-02-05 15:25:58 -0500844 case SUPPRESSED_EFFECT_SCREEN_OFF: return "SUPPRESSED_EFFECT_SCREEN_OFF";
Julia Reynolds61721582016-01-05 08:35:25 -0500845 case SUPPRESSED_EFFECT_SCREEN_ON: return "SUPPRESSED_EFFECT_SCREEN_ON";
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500846 case SUPPRESSED_EFFECTS_UNSET: return "SUPPRESSED_EFFECTS_UNSET";
847 default: return "UNKNOWN_" + effect;
848 }
849 }
850
John Spurlock1fc476d2015-04-14 16:05:20 -0400851 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 Spurlock1fc476d2015-04-14 16:05:20 -0400882 }
883
Dan Sandler994349c2015-04-15 11:02:54 -0400884 /**
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 Sharkeyc53962d2016-03-01 19:27:23 -0700906 throw e.rethrowFromSystemServer();
Dan Sandler994349c2015-04-15 11:02:54 -0400907 }
Dan Sandler994349c2015-04-15 11:02:54 -0400908 }
John Spurlock80774932015-05-07 17:38:50 -0400909
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 &amp; vibration) and is applied globally.
916 * @return One of the INTERRUPTION_FILTER_ constants, or INTERRUPTION_FILTER_UNKNOWN when
917 * unavailable.
918 *
919 * <p>
John Spurlock7c74f782015-06-04 13:01:42 -0400920 * Only available if policy access is granted to this package.
921 * See {@link #isNotificationPolicyAccessGranted}.
John Spurlock80774932015-05-07 17:38:50 -0400922 */
Julia Reynolds0edb50c2016-02-26 14:08:25 -0500923 public final @InterruptionFilter int getCurrentInterruptionFilter() {
John Spurlock80774932015-05-07 17:38:50 -0400924 final INotificationManager service = getService();
925 try {
926 return zenModeToInterruptionFilter(service.getZenMode());
927 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700928 throw e.rethrowFromSystemServer();
John Spurlock80774932015-05-07 17:38:50 -0400929 }
John Spurlock80774932015-05-07 17:38:50 -0400930 }
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 &amp; vibration) and is applied globally.
938 * @return One of the INTERRUPTION_FILTER_ constants, or INTERRUPTION_FILTER_UNKNOWN when
939 * unavailable.
940 *
941 * <p>
John Spurlock7c74f782015-06-04 13:01:42 -0400942 * Only available if policy access is granted to this package.
943 * See {@link #isNotificationPolicyAccessGranted}.
John Spurlock80774932015-05-07 17:38:50 -0400944 */
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 Sharkeyc53962d2016-03-01 19:27:23 -0700950 throw e.rethrowFromSystemServer();
John Spurlock80774932015-05-07 17:38:50 -0400951 }
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 Project9066cfe2009-03-03 19:31:44 -0800975}