blob: 10aa7ebc136aa7f59a722a76bdaa55649e4b7067 [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 /** @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 Monka9927322015-12-13 16:22:37 -0500154 /**
John Spurlock80774932015-05-07 17:38:50 -0400155 * {@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 Project9066cfe2009-03-03 19:31:44 -0800183 private static INotificationManager sService;
184
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700185 /** @hide */
186 static public INotificationManager getService()
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 {
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 Sharkey69ddab42012-08-25 00:05:46 -0700201 /** {@hide} */
202 public static NotificationManager from(Context context) {
203 return (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
204 }
205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 /**
Daniel Sandlere97a3bc2011-02-07 16:47:07 -0500207 * 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 Project9066cfe2009-03-03 19:31:44 -0800210 *
211 * @param id An identifier for this notification unique within your
212 * application.
Daniel Sandlere97a3bc2011-02-07 16:47:07 -0500213 * @param notification A {@link Notification} object describing what to show the user. Must not
214 * be null.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 */
216 public void notify(int id, Notification notification)
217 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700218 notify(null, id, notification);
219 }
220
221 /**
Daniel Sandlere97a3bc2011-02-07 16:47:07 -0500222 * 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 Quintana6ecaff12009-09-25 14:23:13 -0700225 *
Peter Collingbourneb97c3492010-10-13 20:04:52 +0100226 * @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 Sandlere97a3bc2011-02-07 16:47:07 -0500229 * @param notification A {@link Notification} object describing what to
230 * show the user. Must not be null.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700231 */
232 public void notify(String tag, int id, Notification notification)
233 {
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400234 notifyAsUser(tag, id, notification, new UserHandle(UserHandle.myUserId()));
Dianne Hackborn41203752012-08-31 14:05:51 -0700235 }
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 Reynoldsda303542015-11-23 14:00:20 -0500245 // Fix the notification as best we can.
246 Notification.addFieldsFromContext(mContext, notification);
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -0700247 if (notification.sound != null) {
248 notification.sound = notification.sound.getCanonicalUri();
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700249 if (StrictMode.vmFileUriExposureEnabled()) {
Jeff Sharkeyac3be9a2016-02-01 10:39:30 -0700250 notification.sound.checkFileUriExposed("Notification.sound");
Jeff Sharkeya14acd22013-04-02 18:27:45 -0700251 }
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -0700252 }
Dan Sandlerd63f9322015-05-06 15:18:49 -0400253 fixLegacySmallIcon(notification, pkg);
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400254 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 Hackborn41203752012-08-31 14:05:51 -0700260 if (localLOGV) Log.v(TAG, pkg + ": notify(" + id + ", " + notification + ")");
Adrian Roos184bfe022016-03-03 13:41:44 -0800261 final Notification copy = Builder.maybeCloneStrippedForDelivery(notification);
Dianne Hackborn41203752012-08-31 14:05:51 -0700262 try {
Dianne Hackborn95d78532013-09-11 09:51:14 -0700263 service.enqueueNotificationWithTag(pkg, mContext.getOpPackageName(), tag, id,
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400264 copy, idOut, user.getIdentifier());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800265 if (id != idOut[0]) {
266 Log.w(TAG, "notify: id corrupted: sent " + id + ", got back " + idOut[0]);
267 }
268 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700269 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 }
271 }
272
Dan Sandlerd63f9322015-05-06 15:18:49 -0400273 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 Project9066cfe2009-03-03 19:31:44 -0800279 /**
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 Quintana6ecaff12009-09-25 14:23:13 -0700286 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 Reynoldsd9228f12015-10-20 10:37:27 -0400296 cancelAsUser(tag, id, new UserHandle(UserHandle.myUserId()));
Dianne Hackborn41203752012-08-31 14:05:51 -0700297 }
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 Project9066cfe2009-03-03 19:31:44 -0800309 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700310 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 }
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 Hackborn41203752012-08-31 14:05:51 -0700324 service.cancelAllNotifications(pkg, UserHandle.myUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700326 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 }
328 }
329
John Spurlockb4782522014-08-22 14:54:46 -0400330 /**
331 * @hide
332 */
333 public ComponentName getEffectsSuppressor() {
334 INotificationManager service = getService();
335 try {
336 return service.getEffectsSuppressor();
337 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700338 throw e.rethrowFromSystemServer();
John Spurlockb4782522014-08-22 14:54:46 -0400339 }
340 }
341
John Spurlock2b122f42014-08-27 16:29:47 -0400342 /**
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 Sharkeyf8880562016-02-26 13:03:01 -0700350 throw e.rethrowFromSystemServer();
John Spurlock2b122f42014-08-27 16:29:47 -0400351 }
352 }
353
John Spurlock530052a2014-11-30 16:26:19 -0500354 /**
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 Sharkeyf8880562016-02-26 13:03:01 -0700362 throw e.rethrowFromSystemServer();
John Spurlock530052a2014-11-30 16:26:19 -0500363 }
364 }
365
John Spurlockcdb57ae2015-02-11 19:04:11 -0500366 /**
367 * @hide
368 */
John Spurlockb2278d62015-04-07 12:47:12 -0400369 public void setZenMode(int mode, Uri conditionId, String reason) {
John Spurlockcdb57ae2015-02-11 19:04:11 -0500370 INotificationManager service = getService();
371 try {
John Spurlockb2278d62015-04-07 12:47:12 -0400372 service.setZenMode(mode, conditionId, reason);
John Spurlockcdb57ae2015-02-11 19:04:11 -0500373 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700374 throw e.rethrowFromSystemServer();
John Spurlockcdb57ae2015-02-11 19:04:11 -0500375 }
376 }
377
378 /**
379 * @hide
380 */
John Spurlockb2278d62015-04-07 12:47:12 -0400381 public int getZenMode() {
John Spurlockcdb57ae2015-02-11 19:04:11 -0500382 INotificationManager service = getService();
383 try {
John Spurlockb2278d62015-04-07 12:47:12 -0400384 return service.getZenMode();
John Spurlockcdb57ae2015-02-11 19:04:11 -0500385 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700386 throw e.rethrowFromSystemServer();
John Spurlockcdb57ae2015-02-11 19:04:11 -0500387 }
388 }
389
390 /**
391 * @hide
392 */
John Spurlockb2278d62015-04-07 12:47:12 -0400393 public ZenModeConfig getZenModeConfig() {
John Spurlockcdb57ae2015-02-11 19:04:11 -0500394 INotificationManager service = getService();
395 try {
John Spurlockb2278d62015-04-07 12:47:12 -0400396 return service.getZenModeConfig();
John Spurlockcdb57ae2015-02-11 19:04:11 -0500397 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700398 throw e.rethrowFromSystemServer();
John Spurlockcdb57ae2015-02-11 19:04:11 -0500399 }
John Spurlockcdb57ae2015-02-11 19:04:11 -0500400 }
401
John Spurlock1fc476d2015-04-14 16:05:20 -0400402 /**
Julia Reynolds43b70cd2016-01-14 15:05:34 -0500403 * @hide
404 */
405 public int getRuleInstanceCount(ComponentName owner) {
406 INotificationManager service = getService();
407 try {
408 return service.getRuleInstanceCount(owner);
409 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700410 throw e.rethrowFromSystemServer();
Julia Reynolds43b70cd2016-01-14 15:05:34 -0500411 }
Julia Reynolds43b70cd2016-01-14 15:05:34 -0500412 }
413
414 /**
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400415 * Returns AutomaticZenRules owned by the caller.
416 *
417 * <p>
Julia Reynolds361e82d32016-02-26 18:19:49 -0500418 * Throws a SecurityException if policy access is granted to this package.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400419 * See {@link #isNotificationPolicyAccessGranted}.
420 */
Julia Reynolds361e82d32016-02-26 18:19:49 -0500421 public Map<String, AutomaticZenRule> getAutomaticZenRules() {
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400422 INotificationManager service = getService();
423 try {
Julia Reynolds361e82d32016-02-26 18:19:49 -0500424 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 Reynoldsa47a27f2015-08-24 08:31:47 -0400432 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700433 throw e.rethrowFromSystemServer();
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400434 }
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400435 }
436
437 /**
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400438 * Returns the AutomaticZenRule with the given id, if it exists and the caller has access.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400439 *
440 * <p>
Julia Reynolds361e82d32016-02-26 18:19:49 -0500441 * Throws a SecurityException if policy access is granted to this package.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400442 * See {@link #isNotificationPolicyAccessGranted}.
443 *
444 * <p>
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400445 * 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 -0400446 * doesn't own the matching rule. See {@link AutomaticZenRule#getOwner}.
447 */
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400448 public AutomaticZenRule getAutomaticZenRule(String id) {
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400449 INotificationManager service = getService();
450 try {
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400451 return service.getAutomaticZenRule(id);
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400452 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700453 throw e.rethrowFromSystemServer();
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400454 }
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400455 }
456
457 /**
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400458 * Creates the given zen rule.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400459 *
460 * <p>
Julia Reynolds361e82d32016-02-26 18:19:49 -0500461 * Throws a SecurityException if policy access is granted to this package.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400462 * See {@link #isNotificationPolicyAccessGranted}.
463 *
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400464 * @param automaticZenRule the rule to create.
Julia Reynolds361e82d32016-02-26 18:19:49 -0500465 * @return The id of the newly created rule; null if the rule could not be created.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400466 */
Julia Reynolds361e82d32016-02-26 18:19:49 -0500467 public String addAutomaticZenRule(AutomaticZenRule automaticZenRule) {
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400468 INotificationManager service = getService();
469 try {
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400470 return service.addAutomaticZenRule(automaticZenRule);
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400471 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700472 throw e.rethrowFromSystemServer();
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400473 }
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400474 }
475
476 /**
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400477 * Updates the given zen rule.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400478 *
479 * <p>
Julia Reynolds361e82d32016-02-26 18:19:49 -0500480 * Throws a SecurityException if policy access is granted to this package.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400481 * See {@link #isNotificationPolicyAccessGranted}.
482 *
483 * <p>
484 * Callers can only update rules that they own. See {@link AutomaticZenRule#getOwner}.
Julia Reynolds361e82d32016-02-26 18:19:49 -0500485 * @param id The id of the rule to update
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400486 * @param automaticZenRule the rule to update.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400487 * @return Whether the rule was successfully updated.
488 */
Julia Reynolds361e82d32016-02-26 18:19:49 -0500489 public boolean updateAutomaticZenRule(String id, AutomaticZenRule automaticZenRule) {
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400490 INotificationManager service = getService();
491 try {
Julia Reynolds361e82d32016-02-26 18:19:49 -0500492 return service.updateAutomaticZenRule(id, automaticZenRule);
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400493 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700494 throw e.rethrowFromSystemServer();
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400495 }
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400496 }
497
498 /**
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400499 * Deletes the automatic zen rule with the given id.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400500 *
501 * <p>
Julia Reynolds361e82d32016-02-26 18:19:49 -0500502 * Throws a SecurityException if policy access is granted to this package.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400503 * See {@link #isNotificationPolicyAccessGranted}.
504 *
505 * <p>
506 * Callers can only delete rules that they own. See {@link AutomaticZenRule#getOwner}.
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400507 * @param id the id of the rule to delete.
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400508 * @return Whether the rule was successfully deleted.
509 */
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400510 public boolean removeAutomaticZenRule(String id) {
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400511 INotificationManager service = getService();
512 try {
Julia Reynolds4fe98d62015-10-06 16:23:41 -0400513 return service.removeAutomaticZenRule(id);
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400514 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700515 throw e.rethrowFromSystemServer();
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400516 }
Julia Reynoldsa47a27f2015-08-24 08:31:47 -0400517 }
518
519 /**
Julia Reynoldsc8e54e82015-11-30 16:43:05 -0500520 * 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 Sharkeyf8880562016-02-26 13:03:01 -0700529 throw e.rethrowFromSystemServer();
Julia Reynoldsc8e54e82015-11-30 16:43:05 -0500530 }
Julia Reynoldsc8e54e82015-11-30 16:43:05 -0500531 }
532
Julia Reynolds0edb50c2016-02-26 14:08:25 -0500533 /**
534 * Returns the user specified importance for notifications from the calling package.
535 */
536 public @NotificationListenerService.Ranking.Importance int getImportance() {
Julia Reynolds81afbcd2016-02-09 14:54:08 -0500537 INotificationManager service = getService();
538 try {
Julia Reynoldsef37f282016-02-12 09:11:27 -0500539 return service.getPackageImportance(mContext.getPackageName());
Julia Reynolds81afbcd2016-02-09 14:54:08 -0500540 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700541 throw e.rethrowFromSystemServer();
Julia Reynolds81afbcd2016-02-09 14:54:08 -0500542 }
Julia Reynolds81afbcd2016-02-09 14:54:08 -0500543 }
544
Julia Reynolds0edb50c2016-02-26 14:08:25 -0500545 /**
546 * Returns whether notifications from the calling package are blocked.
547 */
Julia Reynolds81afbcd2016-02-09 14:54:08 -0500548 public boolean areNotificationsEnabled() {
549 INotificationManager service = getService();
550 try {
551 return service.areNotificationsEnabled(mContext.getPackageName());
552 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700553 throw e.rethrowFromSystemServer();
Julia Reynolds81afbcd2016-02-09 14:54:08 -0500554 }
Julia Reynolds81afbcd2016-02-09 14:54:08 -0500555 }
556
Julia Reynoldsc8e54e82015-11-30 16:43:05 -0500557 /**
John Spurlock80774932015-05-07 17:38:50 -0400558 * Checks the ability to read/modify notification policy for the calling package.
John Spurlock1fc476d2015-04-14 16:05:20 -0400559 *
John Spurlock7c74f782015-06-04 13:01:42 -0400560 * <p>
John Spurlock80774932015-05-07 17:38:50 -0400561 * Returns true if the calling package can read/modify notification policy.
John Spurlock7c74f782015-06-04 13:01:42 -0400562 *
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 Spurlock1fc476d2015-04-14 16:05:20 -0400570 */
John Spurlock80774932015-05-07 17:38:50 -0400571 public boolean isNotificationPolicyAccessGranted() {
John Spurlock1fc476d2015-04-14 16:05:20 -0400572 INotificationManager service = getService();
573 try {
John Spurlock80774932015-05-07 17:38:50 -0400574 return service.isNotificationPolicyAccessGranted(mContext.getOpPackageName());
575 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700576 throw e.rethrowFromSystemServer();
John Spurlock80774932015-05-07 17:38:50 -0400577 }
John Spurlock80774932015-05-07 17:38:50 -0400578 }
579
580 /** @hide */
581 public boolean isNotificationPolicyAccessGrantedForPackage(String pkg) {
582 INotificationManager service = getService();
583 try {
584 return service.isNotificationPolicyAccessGrantedForPackage(pkg);
John Spurlock1fc476d2015-04-14 16:05:20 -0400585 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700586 throw e.rethrowFromSystemServer();
John Spurlock1fc476d2015-04-14 16:05:20 -0400587 }
John Spurlock1fc476d2015-04-14 16:05:20 -0400588 }
589
590 /**
591 * Gets the current notification policy.
592 *
John Spurlock80774932015-05-07 17:38:50 -0400593 * <p>
John Spurlock7c74f782015-06-04 13:01:42 -0400594 * Only available if policy access is granted to this package.
595 * See {@link #isNotificationPolicyAccessGranted}.
John Spurlock1fc476d2015-04-14 16:05:20 -0400596 */
John Spurlock80774932015-05-07 17:38:50 -0400597 public Policy getNotificationPolicy() {
John Spurlock1fc476d2015-04-14 16:05:20 -0400598 INotificationManager service = getService();
599 try {
John Spurlock80774932015-05-07 17:38:50 -0400600 return service.getNotificationPolicy(mContext.getOpPackageName());
John Spurlock1fc476d2015-04-14 16:05:20 -0400601 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700602 throw e.rethrowFromSystemServer();
John Spurlock1fc476d2015-04-14 16:05:20 -0400603 }
John Spurlock1fc476d2015-04-14 16:05:20 -0400604 }
605
606 /**
607 * Sets the current notification policy.
608 *
John Spurlock80774932015-05-07 17:38:50 -0400609 * <p>
John Spurlock7c74f782015-06-04 13:01:42 -0400610 * Only available if policy access is granted to this package.
611 * See {@link #isNotificationPolicyAccessGranted}.
John Spurlock80774932015-05-07 17:38:50 -0400612 *
John Spurlock1fc476d2015-04-14 16:05:20 -0400613 * @param policy The new desired policy.
614 */
John Spurlock80774932015-05-07 17:38:50 -0400615 public void setNotificationPolicy(@NonNull Policy policy) {
John Spurlock1fc476d2015-04-14 16:05:20 -0400616 checkRequired("policy", policy);
617 INotificationManager service = getService();
618 try {
John Spurlock80774932015-05-07 17:38:50 -0400619 service.setNotificationPolicy(mContext.getOpPackageName(), policy);
John Spurlock1fc476d2015-04-14 16:05:20 -0400620 } catch (RemoteException e) {
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700621 throw e.rethrowFromSystemServer();
John Spurlock1fc476d2015-04-14 16:05:20 -0400622 }
623 }
624
John Spurlock80774932015-05-07 17:38:50 -0400625 /** @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 Sharkeyf8880562016-02-26 13:03:01 -0700631 throw e.rethrowFromSystemServer();
John Spurlock80774932015-05-07 17:38:50 -0400632 }
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 Sharkeyf8880562016-02-26 13:03:01 -0700648 throw e.rethrowFromSystemServer();
John Spurlock80774932015-05-07 17:38:50 -0400649 }
Jeff Sharkeyf8880562016-02-26 13:03:01 -0700650 return new ArraySet<>();
John Spurlock80774932015-05-07 17:38:50 -0400651 }
652
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 private Context mContext;
John Spurlock1fc476d2015-04-14 16:05:20 -0400654
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 Reynoldscedacef2016-03-04 08:18:47 -0500663 * filtering.
John Spurlock1fc476d2015-04-14 16:05:20 -0400664 */
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 Spurlock80774932015-05-07 17:38:50 -0400695 /** Notification senders to prioritize for calls. One of:
John Spurlock1fc476d2015-04-14 16:05:20 -0400696 * PRIORITY_SENDERS_ANY, PRIORITY_SENDERS_CONTACTS, PRIORITY_SENDERS_STARRED */
John Spurlock80774932015-05-07 17:38:50 -0400697 public final int priorityCallSenders;
John Spurlock1fc476d2015-04-14 16:05:20 -0400698
John Spurlock80774932015-05-07 17:38:50 -0400699 /** 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 Reynoldsd5607292016-02-05 15:25:58 -0500703 /**
704 * @hide
705 */
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500706 public static final int SUPPRESSED_EFFECTS_UNSET = -1;
Julia Reynoldsd5607292016-02-05 15:25:58 -0500707 /**
Julia Reynoldscedacef2016-03-04 08:18:47 -0500708 * 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 Reynoldsd5607292016-02-05 15:25:58 -0500710 */
711 public static final int SUPPRESSED_EFFECT_SCREEN_OFF = 1 << 0;
712 /**
Julia Reynoldscedacef2016-03-04 08:18:47 -0500713 * Whether notifications suppressed by DND should not interrupt visually when the screen
714 * is on (e.g. by peeking onto the screen).
Julia Reynoldsd5607292016-02-05 15:25:58 -0500715 */
716 public static final int SUPPRESSED_EFFECT_SCREEN_ON = 1 << 1;
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500717
718 private static final int[] ALL_SUPPRESSED_EFFECTS = {
Julia Reynoldsd5607292016-02-05 15:25:58 -0500719 SUPPRESSED_EFFECT_SCREEN_OFF,
Julia Reynolds61721582016-01-05 08:35:25 -0500720 SUPPRESSED_EFFECT_SCREEN_ON,
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500721 };
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 Reynoldscedacef2016-03-04 08:18:47 -0500729 /**
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 Spurlock80774932015-05-07 17:38:50 -0400736 public Policy(int priorityCategories, int priorityCallSenders, int priorityMessageSenders) {
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500737 this(priorityCategories, priorityCallSenders, priorityMessageSenders,
738 SUPPRESSED_EFFECTS_UNSET);
739 }
740
Julia Reynoldscedacef2016-03-04 08:18:47 -0500741 /**
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 Reynoldsf612869ae2015-11-05 16:48:55 -0500750 public Policy(int priorityCategories, int priorityCallSenders, int priorityMessageSenders,
751 int suppressedVisualEffects) {
John Spurlock1fc476d2015-04-14 16:05:20 -0400752 this.priorityCategories = priorityCategories;
John Spurlock80774932015-05-07 17:38:50 -0400753 this.priorityCallSenders = priorityCallSenders;
754 this.priorityMessageSenders = priorityMessageSenders;
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500755 this.suppressedVisualEffects = suppressedVisualEffects;
John Spurlock1fc476d2015-04-14 16:05:20 -0400756 }
757
758 /** @hide */
759 public Policy(Parcel source) {
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500760 this(source.readInt(), source.readInt(), source.readInt(), source.readInt());
John Spurlock1fc476d2015-04-14 16:05:20 -0400761 }
762
763 @Override
764 public void writeToParcel(Parcel dest, int flags) {
765 dest.writeInt(priorityCategories);
John Spurlock80774932015-05-07 17:38:50 -0400766 dest.writeInt(priorityCallSenders);
767 dest.writeInt(priorityMessageSenders);
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500768 dest.writeInt(suppressedVisualEffects);
John Spurlock1fc476d2015-04-14 16:05:20 -0400769 }
770
771 @Override
772 public int describeContents() {
773 return 0;
774 }
775
776 @Override
777 public int hashCode() {
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500778 return Objects.hash(priorityCategories, priorityCallSenders, priorityMessageSenders,
779 suppressedVisualEffects);
John Spurlock1fc476d2015-04-14 16:05:20 -0400780 }
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 Spurlock80774932015-05-07 17:38:50 -0400788 && other.priorityCallSenders == priorityCallSenders
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500789 && other.priorityMessageSenders == priorityMessageSenders
790 && other.suppressedVisualEffects == suppressedVisualEffects;
John Spurlock1fc476d2015-04-14 16:05:20 -0400791 }
792
793 @Override
794 public String toString() {
795 return "NotificationManager.Policy["
796 + "priorityCategories=" + priorityCategoriesToString(priorityCategories)
John Spurlock80774932015-05-07 17:38:50 -0400797 + ",priorityCallSenders=" + prioritySendersToString(priorityCallSenders)
798 + ",priorityMessageSenders=" + prioritySendersToString(priorityMessageSenders)
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500799 + ",suppressedVisualEffects="
800 + suppressedEffectsToString(suppressedVisualEffects)
John Spurlock1fc476d2015-04-14 16:05:20 -0400801 + "]";
802 }
803
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500804 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 Spurlock1fc476d2015-04-14 16:05:20 -0400822 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 Reynoldsf612869ae2015-11-05 16:48:55 -0500840 private static String effectToString(int effect) {
841 switch (effect) {
Julia Reynoldsd5607292016-02-05 15:25:58 -0500842 case SUPPRESSED_EFFECT_SCREEN_OFF: return "SUPPRESSED_EFFECT_SCREEN_OFF";
Julia Reynolds61721582016-01-05 08:35:25 -0500843 case SUPPRESSED_EFFECT_SCREEN_ON: return "SUPPRESSED_EFFECT_SCREEN_ON";
Julia Reynoldsf612869ae2015-11-05 16:48:55 -0500844 case SUPPRESSED_EFFECTS_UNSET: return "SUPPRESSED_EFFECTS_UNSET";
845 default: return "UNKNOWN_" + effect;
846 }
847 }
848
John Spurlock1fc476d2015-04-14 16:05:20 -0400849 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 Spurlock1fc476d2015-04-14 16:05:20 -0400880 }
881
Dan Sandler994349c2015-04-15 11:02:54 -0400882 /**
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 Sharkeyc53962d2016-03-01 19:27:23 -0700904 throw e.rethrowFromSystemServer();
Dan Sandler994349c2015-04-15 11:02:54 -0400905 }
Dan Sandler994349c2015-04-15 11:02:54 -0400906 }
John Spurlock80774932015-05-07 17:38:50 -0400907
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 &amp; vibration) and is applied globally.
914 * @return One of the INTERRUPTION_FILTER_ constants, or INTERRUPTION_FILTER_UNKNOWN when
915 * unavailable.
916 *
917 * <p>
John Spurlock7c74f782015-06-04 13:01:42 -0400918 * Only available if policy access is granted to this package.
919 * See {@link #isNotificationPolicyAccessGranted}.
John Spurlock80774932015-05-07 17:38:50 -0400920 */
Julia Reynolds0edb50c2016-02-26 14:08:25 -0500921 public final @InterruptionFilter int getCurrentInterruptionFilter() {
John Spurlock80774932015-05-07 17:38:50 -0400922 final INotificationManager service = getService();
923 try {
924 return zenModeToInterruptionFilter(service.getZenMode());
925 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700926 throw e.rethrowFromSystemServer();
John Spurlock80774932015-05-07 17:38:50 -0400927 }
John Spurlock80774932015-05-07 17:38:50 -0400928 }
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 &amp; vibration) and is applied globally.
936 * @return One of the INTERRUPTION_FILTER_ constants, or INTERRUPTION_FILTER_UNKNOWN when
937 * unavailable.
938 *
939 * <p>
John Spurlock7c74f782015-06-04 13:01:42 -0400940 * Only available if policy access is granted to this package.
941 * See {@link #isNotificationPolicyAccessGranted}.
John Spurlock80774932015-05-07 17:38:50 -0400942 */
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 Sharkeyc53962d2016-03-01 19:27:23 -0700948 throw e.rethrowFromSystemServer();
John Spurlock80774932015-05-07 17:38:50 -0400949 }
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 Project9066cfe2009-03-03 19:31:44 -0800973}