blob: dc43f9279527b63e932cb2d0183c4a5a00a9ba27 [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 com.android.server;
18
Jeff Sharkey098d5802012-04-26 17:30:34 -070019import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT;
20import static org.xmlpull.v1.XmlPullParser.END_TAG;
21import static org.xmlpull.v1.XmlPullParser.START_TAG;
svetoslavganov75986cf2009-05-14 22:28:01 -070022
Dianne Hackborn41203752012-08-31 14:05:51 -070023import android.app.ActivityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.app.ActivityManagerNative;
Amith Yamasanif203aee2012-08-29 18:41:53 -070025import android.app.AppGlobals;
Daniel Sandler4a900acd2013-01-30 14:04:10 -050026import android.app.AppOpsManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.app.IActivityManager;
28import android.app.INotificationManager;
29import android.app.ITransientNotification;
30import android.app.Notification;
31import android.app.PendingIntent;
32import android.app.StatusBarManager;
33import android.content.BroadcastReceiver;
Daniel Sandler5feceeb2013-03-22 18:29:23 -070034import android.content.ComponentName;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070035import android.content.ContentResolver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.content.Context;
37import android.content.Intent;
38import android.content.IntentFilter;
Daniel Sandler5feceeb2013-03-22 18:29:23 -070039import android.content.ServiceConnection;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070040import android.content.pm.ApplicationInfo;
Daniel Sandler4a900acd2013-01-30 14:04:10 -050041import android.content.pm.PackageInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.content.pm.PackageManager;
43import android.content.pm.PackageManager.NameNotFoundException;
44import android.content.res.Resources;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070045import android.database.ContentObserver;
Daniel Sandlerf45564e2013-04-15 15:05:08 -040046import android.graphics.Bitmap;
svetoslavganov75986cf2009-05-14 22:28:01 -070047import android.media.AudioManager;
Jeff Sharkey098d5802012-04-26 17:30:34 -070048import android.media.IAudioService;
49import android.media.IRingtonePlayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.os.Binder;
Daniel Sandler5e62e3a2013-04-15 20:57:02 -040052import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.os.Handler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.os.IBinder;
55import android.os.Message;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070056import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.os.RemoteException;
Jeff Sharkey098d5802012-04-26 17:30:34 -070058import android.os.ServiceManager;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070059import android.os.UserHandle;
Daniel Sandler4b749ef2013-03-18 21:53:04 -040060import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.os.Vibrator;
62import android.provider.Settings;
Daniel Sandler5feceeb2013-03-22 18:29:23 -070063import android.service.notification.INotificationListener;
64import android.service.notification.NotificationListenerService;
65import android.service.notification.StatusBarNotification;
Daniel Sandlere96ffb12010-03-11 13:38:06 -050066import android.telephony.TelephonyManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070067import android.text.TextUtils;
Dianne Hackborn39606a02012-07-31 17:54:35 -070068import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069import android.util.EventLog;
70import android.util.Log;
Andy Stadler110988c2010-12-03 14:29:16 -080071import android.util.Slog;
Daniel Sandler0da673f2012-04-11 12:33:16 -040072import android.util.Xml;
svetoslavganov75986cf2009-05-14 22:28:01 -070073import android.view.accessibility.AccessibilityEvent;
74import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075import android.widget.Toast;
76
Jeff Sharkey098d5802012-04-26 17:30:34 -070077import org.xmlpull.v1.XmlPullParser;
78import org.xmlpull.v1.XmlPullParserException;
Jeff Sharkey098d5802012-04-26 17:30:34 -070079
Daniel Sandler0da673f2012-04-11 12:33:16 -040080import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import java.io.FileDescriptor;
Daniel Sandler0da673f2012-04-11 12:33:16 -040082import java.io.FileInputStream;
83import java.io.FileNotFoundException;
Daniel Sandler0da673f2012-04-11 12:33:16 -040084import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085import java.io.PrintWriter;
Daniel Sandlerf45564e2013-04-15 15:05:08 -040086import java.lang.reflect.Array;
Daniel Sandlerfde19b12013-01-17 00:21:05 -050087import java.util.ArrayDeque;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088import java.util.ArrayList;
89import java.util.Arrays;
Daniel Sandler0da673f2012-04-11 12:33:16 -040090import java.util.HashSet;
Daniel Sandlerfde19b12013-01-17 00:21:05 -050091import java.util.Iterator;
92import java.util.NoSuchElementException;
Daniel Sandler0da673f2012-04-11 12:33:16 -040093
94import libcore.io.IoUtils;
95
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096
Daniel Sandlerd0a2f862010-08-03 15:29:31 -040097/** {@hide} */
98public class NotificationManagerService extends INotificationManager.Stub
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099{
100 private static final String TAG = "NotificationService";
101 private static final boolean DBG = false;
102
Joe Onoratobd73d012010-06-04 11:44:54 -0700103 private static final int MAX_PACKAGE_NOTIFICATIONS = 50;
104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 // message codes
106 private static final int MESSAGE_TIMEOUT = 2;
107
108 private static final int LONG_DELAY = 3500; // 3.5 seconds
109 private static final int SHORT_DELAY = 2000; // 2 seconds
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800110
111 private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250};
Daniel Sandleredbb3802012-11-13 20:49:47 -0800112 private static final int VIBRATE_PATTERN_MAXLEN = 8 * 2 + 1; // up to eight bumps
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113
114 private static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION;
Daniel Sandler49a2ad12012-03-28 15:46:39 -0400115 private static final boolean SCORE_ONGOING_HIGHER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116
Daniel Sandler0da673f2012-04-11 12:33:16 -0400117 private static final int JUNK_SCORE = -1000;
118 private static final int NOTIFICATION_PRIORITY_MULTIPLIER = 10;
119 private static final int SCORE_DISPLAY_THRESHOLD = Notification.PRIORITY_MIN * NOTIFICATION_PRIORITY_MULTIPLIER;
120
Daniel Sandler526fa0e2012-12-04 14:51:50 -0500121 // Notifications with scores below this will not interrupt the user, either via LED or
122 // sound or vibration
123 private static final int SCORE_INTERRUPTION_THRESHOLD =
124 Notification.PRIORITY_LOW * NOTIFICATION_PRIORITY_MULTIPLIER;
125
Daniel Sandler0da673f2012-04-11 12:33:16 -0400126 private static final boolean ENABLE_BLOCKED_NOTIFICATIONS = true;
127 private static final boolean ENABLE_BLOCKED_TOASTS = true;
128
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700129 private static final String ENABLED_NOTIFICATION_LISTENERS_SEPARATOR = ":";
130
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 final Context mContext;
132 final IActivityManager mAm;
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400133 final UserManager mUserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 final IBinder mForegroundToken = new Binder();
135
136 private WorkerHandler mHandler;
Joe Onorato089de882010-04-12 08:18:45 -0700137 private StatusBarManagerService mStatusBar;
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500138 private LightsService.Light mNotificationLight;
139 private LightsService.Light mAttentionLight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140
Mike Lockwood670f9322010-01-20 12:13:36 -0500141 private int mDefaultNotificationColor;
142 private int mDefaultNotificationLedOn;
143 private int mDefaultNotificationLedOff;
144
Daniel Sandleredbb3802012-11-13 20:49:47 -0800145 private long[] mDefaultVibrationPattern;
146 private long[] mFallbackVibrationPattern;
147
Joe Onorato30275482009-07-08 17:09:14 -0700148 private boolean mSystemReady;
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400149 private int mDisabledNotifications;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150
Jeff Sharkey098d5802012-04-26 17:30:34 -0700151 private NotificationRecord mSoundNotification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 private NotificationRecord mVibrateNotification;
Jeff Sharkey098d5802012-04-26 17:30:34 -0700153
154 private IAudioService mAudioService;
Jeff Brownc2346132012-04-13 01:55:38 -0700155 private Vibrator mVibrator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500157 // for enabling and disabling notification pulse behavior
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400158 private boolean mScreenOn = true;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500159 private boolean mInCall = false;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500160 private boolean mNotificationPulseEnabled;
161
Daniel Sandler09a247e2013-02-14 10:24:17 -0500162 // used as a mutex for access to all active notifications & listeners
Fred Quintana6ecaff12009-09-25 14:23:13 -0700163 private final ArrayList<NotificationRecord> mNotificationList =
164 new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165
166 private ArrayList<ToastRecord> mToastQueue;
167
168 private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 private NotificationRecord mLedNotification;
svetoslavganov75986cf2009-05-14 22:28:01 -0700170
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500171 private final AppOpsManager mAppOps;
172
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700173 // contains connections to all connected listeners, including app services
174 // and system listeners
175 private ArrayList<NotificationListenerInfo> mListeners
176 = new ArrayList<NotificationListenerInfo>();
177 // things that will be put into mListeners as soon as they're ready
178 private ArrayList<String> mServicesBinding = new ArrayList<String>();
179 // lists the component names of all enabled (and therefore connected) listener
180 // app services for the current user only
181 private HashSet<ComponentName> mEnabledListenersForCurrentUser
182 = new HashSet<ComponentName>();
183 // Just the packages from mEnabledListenersForCurrentUser
184 private HashSet<String> mEnabledListenerPackageNames = new HashSet<String>();
Daniel Sandler09a247e2013-02-14 10:24:17 -0500185
Daniel Sandler0da673f2012-04-11 12:33:16 -0400186 // Notification control database. For now just contains disabled packages.
187 private AtomicFile mPolicyFile;
188 private HashSet<String> mBlockedPackages = new HashSet<String>();
189
190 private static final int DB_VERSION = 1;
191
192 private static final String TAG_BODY = "notification-policy";
193 private static final String ATTR_VERSION = "version";
194
195 private static final String TAG_BLOCKED_PKGS = "blocked-packages";
196 private static final String TAG_PACKAGE = "package";
197 private static final String ATTR_NAME = "name";
198
Daniel Sandler09a247e2013-02-14 10:24:17 -0500199 private class NotificationListenerInfo implements DeathRecipient {
200 INotificationListener listener;
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700201 ComponentName component;
Daniel Sandler09a247e2013-02-14 10:24:17 -0500202 int userid;
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400203 boolean isSystem;
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700204 ServiceConnection connection;
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400205
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700206 public NotificationListenerInfo(INotificationListener listener, ComponentName component,
207 int userid, boolean isSystem) {
Daniel Sandler09a247e2013-02-14 10:24:17 -0500208 this.listener = listener;
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700209 this.component = component;
Daniel Sandler09a247e2013-02-14 10:24:17 -0500210 this.userid = userid;
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400211 this.isSystem = isSystem;
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700212 this.connection = null;
213 }
214
215 public NotificationListenerInfo(INotificationListener listener, ComponentName component,
216 int userid, ServiceConnection connection) {
217 this.listener = listener;
218 this.component = component;
219 this.userid = userid;
220 this.isSystem = false;
221 this.connection = connection;
Daniel Sandler09a247e2013-02-14 10:24:17 -0500222 }
223
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400224 boolean enabledAndUserMatches(StatusBarNotification sbn) {
225 final int nid = sbn.getUserId();
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700226 if (!isEnabledForCurrentUser()) {
227 return false;
228 }
Daniel Sandler21ca44d2013-03-07 13:58:00 -0500229 if (this.userid == UserHandle.USER_ALL) return true;
Daniel Sandler21ca44d2013-03-07 13:58:00 -0500230 return (nid == UserHandle.USER_ALL || nid == this.userid);
231 }
232
Daniel Sandler09a247e2013-02-14 10:24:17 -0500233 public void notifyPostedIfUserMatch(StatusBarNotification sbn) {
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700234 if (!enabledAndUserMatches(sbn)) {
235 return;
236 }
Daniel Sandler09a247e2013-02-14 10:24:17 -0500237 try {
238 listener.onNotificationPosted(sbn);
239 } catch (RemoteException ex) {
Daniel Sandler1a497d32013-04-18 14:52:45 -0400240 Log.e(TAG, "unable to notify listener (posted): " + listener, ex);
Daniel Sandler09a247e2013-02-14 10:24:17 -0500241 }
242 }
243
244 public void notifyRemovedIfUserMatch(StatusBarNotification sbn) {
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400245 if (!enabledAndUserMatches(sbn)) return;
Daniel Sandler09a247e2013-02-14 10:24:17 -0500246 try {
247 listener.onNotificationRemoved(sbn);
248 } catch (RemoteException ex) {
Daniel Sandler1a497d32013-04-18 14:52:45 -0400249 Log.e(TAG, "unable to notify listener (removed): " + listener, ex);
Daniel Sandler09a247e2013-02-14 10:24:17 -0500250 }
251 }
252
253 @Override
254 public void binderDied() {
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700255 if (connection == null) {
256 // This is not a service; it won't be recreated. We can give up this connection.
257 unregisterListener(this.listener, this.userid);
258 }
Daniel Sandler09a247e2013-02-14 10:24:17 -0500259 }
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400260
261 /** convenience method for looking in mEnabledListenersForCurrentUser */
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700262 public boolean isEnabledForCurrentUser() {
263 if (this.isSystem) return true;
264 if (this.connection == null) return false;
265 return mEnabledListenersForCurrentUser.contains(this.component);
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400266 }
Daniel Sandler09a247e2013-02-14 10:24:17 -0500267 }
268
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500269 private static class Archive {
Daniel Sandler5e62e3a2013-04-15 20:57:02 -0400270 static final int BUFFER_SIZE = 250;
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500271 ArrayDeque<StatusBarNotification> mBuffer = new ArrayDeque<StatusBarNotification>(BUFFER_SIZE);
272
273 public Archive() {
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500274 }
Jeff Sharkey0c1baf92013-04-03 13:08:52 -0700275
Daniel Sandler5e62e3a2013-04-15 20:57:02 -0400276 public String toString() {
277 final StringBuilder sb = new StringBuilder();
278 final int N = mBuffer.size();
279 sb.append("Archive (");
280 sb.append(N);
281 sb.append(" notification");
282 sb.append((N==1)?")":"s)");
283 return sb.toString();
284 }
285
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500286 public void record(StatusBarNotification nr) {
Jeff Sharkey0c1baf92013-04-03 13:08:52 -0700287 // Nuke heavy parts of notification before storing in archive
Daniel Sandler1a497d32013-04-18 14:52:45 -0400288 nr.notification.lightenPayload();
Jeff Sharkey0c1baf92013-04-03 13:08:52 -0700289
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500290 if (mBuffer.size() == BUFFER_SIZE) {
291 mBuffer.removeFirst();
292 }
293 mBuffer.addLast(nr);
294 }
295
Daniel Sandler5e62e3a2013-04-15 20:57:02 -0400296
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500297 public void clear() {
298 mBuffer.clear();
299 }
300
301 public Iterator<StatusBarNotification> descendingIterator() {
302 return mBuffer.descendingIterator();
303 }
304 public Iterator<StatusBarNotification> ascendingIterator() {
305 return mBuffer.iterator();
306 }
307 public Iterator<StatusBarNotification> filter(
308 final Iterator<StatusBarNotification> iter, final String pkg, final int userId) {
309 return new Iterator<StatusBarNotification>() {
310 StatusBarNotification mNext = findNext();
311
312 private StatusBarNotification findNext() {
313 while (iter.hasNext()) {
314 StatusBarNotification nr = iter.next();
315 if ((pkg == null || nr.pkg == pkg)
316 && (userId == UserHandle.USER_ALL || nr.getUserId() == userId)) {
317 return nr;
318 }
319 }
320 return null;
321 }
322
323 @Override
324 public boolean hasNext() {
325 return mNext == null;
326 }
327
328 @Override
329 public StatusBarNotification next() {
330 StatusBarNotification next = mNext;
331 if (next == null) {
332 throw new NoSuchElementException();
333 }
334 mNext = findNext();
335 return next;
336 }
337
338 @Override
339 public void remove() {
340 iter.remove();
341 }
342 };
343 }
Daniel Sandler78d0d252013-02-12 08:14:52 -0500344
345 public StatusBarNotification[] getArray(int count) {
346 if (count == 0) count = Archive.BUFFER_SIZE;
347 final StatusBarNotification[] a
348 = new StatusBarNotification[Math.min(count, mBuffer.size())];
349 Iterator<StatusBarNotification> iter = descendingIterator();
350 int i=0;
351 while (iter.hasNext() && i < count) {
352 a[i++] = iter.next();
353 }
354 return a;
355 }
356
357 public StatusBarNotification[] getArray(int count, String pkg, int userId) {
358 if (count == 0) count = Archive.BUFFER_SIZE;
359 final StatusBarNotification[] a
360 = new StatusBarNotification[Math.min(count, mBuffer.size())];
361 Iterator<StatusBarNotification> iter = filter(descendingIterator(), pkg, userId);
362 int i=0;
363 while (iter.hasNext() && i < count) {
364 a[i++] = iter.next();
365 }
366 return a;
367 }
368
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500369 }
370
371 Archive mArchive = new Archive();
372
Daniel Sandler0da673f2012-04-11 12:33:16 -0400373 private void loadBlockDb() {
374 synchronized(mBlockedPackages) {
375 if (mPolicyFile == null) {
376 File dir = new File("/data/system");
377 mPolicyFile = new AtomicFile(new File(dir, "notification_policy.xml"));
378
379 mBlockedPackages.clear();
380
381 FileInputStream infile = null;
382 try {
383 infile = mPolicyFile.openRead();
384 final XmlPullParser parser = Xml.newPullParser();
385 parser.setInput(infile, null);
386
387 int type;
388 String tag;
389 int version = DB_VERSION;
390 while ((type = parser.next()) != END_DOCUMENT) {
391 tag = parser.getName();
392 if (type == START_TAG) {
393 if (TAG_BODY.equals(tag)) {
394 version = Integer.parseInt(parser.getAttributeValue(null, ATTR_VERSION));
395 } else if (TAG_BLOCKED_PKGS.equals(tag)) {
396 while ((type = parser.next()) != END_DOCUMENT) {
397 tag = parser.getName();
398 if (TAG_PACKAGE.equals(tag)) {
399 mBlockedPackages.add(parser.getAttributeValue(null, ATTR_NAME));
400 } else if (TAG_BLOCKED_PKGS.equals(tag) && type == END_TAG) {
401 break;
402 }
403 }
404 }
405 }
406 }
407 } catch (FileNotFoundException e) {
408 // No data yet
409 } catch (IOException e) {
410 Log.wtf(TAG, "Unable to read blocked notifications database", e);
411 } catch (NumberFormatException e) {
412 Log.wtf(TAG, "Unable to parse blocked notifications database", e);
413 } catch (XmlPullParserException e) {
414 Log.wtf(TAG, "Unable to parse blocked notifications database", e);
415 } finally {
416 IoUtils.closeQuietly(infile);
417 }
418 }
419 }
420 }
421
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500422 /**
423 * Use this when you just want to know if notifications are OK for this package.
424 */
425 public boolean areNotificationsEnabledForPackage(String pkg, int uid) {
Daniel Sandler0da673f2012-04-11 12:33:16 -0400426 checkCallerIsSystem();
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500427 return (mAppOps.checkOpNoThrow(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg)
428 == AppOpsManager.MODE_ALLOWED);
Daniel Sandler0da673f2012-04-11 12:33:16 -0400429 }
430
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500431 /** Use this when you actually want to post a notification or toast.
432 *
433 * Unchecked. Not exposed via Binder, but can be called in the course of enqueue*().
434 */
435 private boolean noteNotificationOp(String pkg, int uid) {
436 if (mAppOps.noteOpNoThrow(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg)
437 != AppOpsManager.MODE_ALLOWED) {
438 Slog.v(TAG, "notifications are disabled by AppOps for " + pkg);
439 return false;
Daniel Sandler0da673f2012-04-11 12:33:16 -0400440 }
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500441 return true;
Daniel Sandler0da673f2012-04-11 12:33:16 -0400442 }
443
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500444 public void setNotificationsEnabledForPackage(String pkg, int uid, boolean enabled) {
Daniel Sandler0da673f2012-04-11 12:33:16 -0400445 checkCallerIsSystem();
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500446 if (true||DBG) {
Daniel Sandler0da673f2012-04-11 12:33:16 -0400447 Slog.v(TAG, (enabled?"en":"dis") + "abling notifications for " + pkg);
448 }
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500449 mAppOps.setMode(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg,
450 enabled ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_IGNORED);
Daniel Sandler0da673f2012-04-11 12:33:16 -0400451 }
452
453
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 private static String idDebugString(Context baseContext, String packageName, int id) {
455 Context c = null;
456
457 if (packageName != null) {
458 try {
459 c = baseContext.createPackageContext(packageName, 0);
460 } catch (NameNotFoundException e) {
461 c = baseContext;
462 }
463 } else {
464 c = baseContext;
465 }
466
467 String pkg;
468 String type;
469 String name;
470
471 Resources r = c.getResources();
472 try {
473 return r.getResourceName(id);
474 } catch (Resources.NotFoundException e) {
475 return "<name unknown>";
476 }
477 }
478
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700479 /**
480 * System-only API for getting a list of current (i.e. not cleared) notifications.
481 *
482 * Requires ACCESS_NOTIFICATIONS which is signature|system.
483 */
484 @Override
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500485 public StatusBarNotification[] getActiveNotifications(String callingPkg) {
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400486 // enforce() will ensure the calling uid has the correct permission
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500487 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NOTIFICATIONS,
Daniel Sandler78d0d252013-02-12 08:14:52 -0500488 "NotificationManagerService.getActiveNotifications");
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500489
490 StatusBarNotification[] tmp = null;
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500491 int uid = Binder.getCallingUid();
492
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400493 // noteOp will check to make sure the callingPkg matches the uid
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500494 if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg)
495 == AppOpsManager.MODE_ALLOWED) {
496 synchronized (mNotificationList) {
497 tmp = new StatusBarNotification[mNotificationList.size()];
498 final int N = mNotificationList.size();
499 for (int i=0; i<N; i++) {
500 tmp[i] = mNotificationList.get(i).sbn;
501 }
502 }
503 }
504 return tmp;
505 }
506
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700507 /**
508 * System-only API for getting a list of recent (cleared, no longer shown) notifications.
509 *
510 * Requires ACCESS_NOTIFICATIONS which is signature|system.
511 */
512 @Override
Daniel Sandler78d0d252013-02-12 08:14:52 -0500513 public StatusBarNotification[] getHistoricalNotifications(String callingPkg, int count) {
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400514 // enforce() will ensure the calling uid has the correct permission
Daniel Sandler78d0d252013-02-12 08:14:52 -0500515 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NOTIFICATIONS,
516 "NotificationManagerService.getHistoricalNotifications");
517
518 StatusBarNotification[] tmp = null;
519 int uid = Binder.getCallingUid();
520
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400521 // noteOp will check to make sure the callingPkg matches the uid
Daniel Sandler78d0d252013-02-12 08:14:52 -0500522 if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg)
523 == AppOpsManager.MODE_ALLOWED) {
524 synchronized (mArchive) {
525 tmp = mArchive.getArray(count);
526 }
527 }
528 return tmp;
529 }
530
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700531 /**
532 * Called whenever packages change, the user switches, or ENABLED_NOTIFICATION_LISTENERS
533 * is altered. (For example in response to USER_SWITCHED in our broadcast receiver)
534 */
535 void rebindListenerServices() {
536 String flat = Settings.Secure.getString(
537 mContext.getContentResolver(),
538 Settings.Secure.ENABLED_NOTIFICATION_LISTENERS);
539
540 NotificationListenerInfo[] toRemove = new NotificationListenerInfo[mListeners.size()];
541 final ArrayList<ComponentName> toAdd;
542 final int currentUser = ActivityManager.getCurrentUser();
543
544 synchronized (mNotificationList) {
545 // unbind and remove all existing listeners
546 toRemove = mListeners.toArray(toRemove);
547
548 toAdd = new ArrayList<ComponentName>();
549 final HashSet<ComponentName> newEnabled = new HashSet<ComponentName>();
550 final HashSet<String> newPackages = new HashSet<String>();
551
552 // decode the list of components
553 if (flat != null) {
554 String[] components = flat.split(ENABLED_NOTIFICATION_LISTENERS_SEPARATOR);
555 for (int i=0; i<components.length; i++) {
556 final ComponentName component
557 = ComponentName.unflattenFromString(components[i]);
558 if (component != null) {
559 newEnabled.add(component);
560 toAdd.add(component);
561 newPackages.add(component.getPackageName());
562 }
563 }
564
565 mEnabledListenersForCurrentUser = newEnabled;
566 mEnabledListenerPackageNames = newPackages;
567 }
568 }
569
570 for (NotificationListenerInfo info : toRemove) {
571 final ComponentName component = info.component;
572 final int oldUser = info.userid;
573 Slog.v(TAG, "disabling notification listener for user " + oldUser + ": " + component);
574 unregisterListenerService(component, info.userid);
575 }
576
577 final int N = toAdd.size();
578 for (int i=0; i<N; i++) {
579 final ComponentName component = toAdd.get(i);
580 Slog.v(TAG, "enabling notification listener for user " + currentUser + ": "
581 + component);
582 registerListenerService(component, currentUser);
583 }
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400584 }
585
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700586 /**
587 * Register a listener binder directly with the notification manager.
588 *
589 * Only works with system callers. Apps should extend
590 * {@link android.service.notification.NotificationListenerService}.
591 */
Daniel Sandler09a247e2013-02-14 10:24:17 -0500592 @Override
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400593 public void registerListener(final INotificationListener listener,
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700594 final ComponentName component, final int userid) {
595 checkCallerIsSystem();
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400596
Daniel Sandler09a247e2013-02-14 10:24:17 -0500597 synchronized (mNotificationList) {
598 try {
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400599 NotificationListenerInfo info
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700600 = new NotificationListenerInfo(listener, component, userid, true);
Daniel Sandler09a247e2013-02-14 10:24:17 -0500601 listener.asBinder().linkToDeath(info, 0);
602 mListeners.add(info);
603 } catch (RemoteException e) {
604 // already dead
605 }
606 }
607 }
608
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700609 /**
610 * Version of registerListener that takes the name of a
611 * {@link android.service.notification.NotificationListenerService} to bind to.
612 *
613 * This is the mechanism by which third parties may subscribe to notifications.
614 */
615 private void registerListenerService(final ComponentName name, final int userid) {
616 checkCallerIsSystem();
617
618 if (DBG) Slog.v(TAG, "registerListenerService: " + name + " u=" + userid);
619
620 synchronized (mNotificationList) {
621 final String servicesBindingTag = name.toString() + "/" + userid;
622 if (mServicesBinding.contains(servicesBindingTag)) {
623 // stop registering this thing already! we're working on it
624 return;
625 }
626 mServicesBinding.add(servicesBindingTag);
627
628 final int N = mListeners.size();
629 for (int i=N-1; i>=0; i--) {
630 final NotificationListenerInfo info = mListeners.get(i);
631 if (name.equals(info.component)
632 && info.userid == userid) {
633 // cut old connections
634 if (DBG) Slog.v(TAG, " disconnecting old listener: " + info.listener);
635 mListeners.remove(i);
636 if (info.connection != null) {
637 mContext.unbindService(info.connection);
638 }
639 }
640 }
641
642 Intent intent = new Intent(NotificationListenerService.SERVICE_INTERFACE);
643 intent.setComponent(name);
644
645 intent.putExtra(Intent.EXTRA_CLIENT_LABEL,
646 com.android.internal.R.string.notification_listener_binding_label);
647 intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
648 mContext, 0, new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS), 0));
649
650 try {
651 if (DBG) Slog.v(TAG, "binding: " + intent);
652 if (!mContext.bindServiceAsUser(intent,
653 new ServiceConnection() {
654 INotificationListener mListener;
655 @Override
656 public void onServiceConnected(ComponentName name, IBinder service) {
657 synchronized (mNotificationList) {
658 mServicesBinding.remove(servicesBindingTag);
659 try {
660 mListener = INotificationListener.Stub.asInterface(service);
661 NotificationListenerInfo info = new NotificationListenerInfo(
662 mListener, name, userid, this);
663 service.linkToDeath(info, 0);
664 mListeners.add(info);
665 } catch (RemoteException e) {
666 // already dead
667 }
668 }
669 }
670
671 @Override
672 public void onServiceDisconnected(ComponentName name) {
673 Slog.v(TAG, "notification listener connection lost: " + name);
674 }
675 },
676 Context.BIND_AUTO_CREATE,
677 new UserHandle(userid)))
678 {
679 mServicesBinding.remove(servicesBindingTag);
680 Slog.w(TAG, "Unable to bind listener service: " + intent);
681 return;
682 }
683 } catch (SecurityException ex) {
684 Slog.e(TAG, "Unable to bind listener service: " + intent, ex);
685 return;
686 }
687 }
688 }
689
690 /**
691 * Remove a listener binder directly
692 */
Daniel Sandler09a247e2013-02-14 10:24:17 -0500693 @Override
694 public void unregisterListener(INotificationListener listener, int userid) {
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400695 // no need to check permissions; if your listener binder is in the list,
696 // that's proof that you had permission to add it in the first place
697
Daniel Sandler09a247e2013-02-14 10:24:17 -0500698 synchronized (mNotificationList) {
699 final int N = mListeners.size();
700 for (int i=N-1; i>=0; i--) {
701 final NotificationListenerInfo info = mListeners.get(i);
702 if (info.listener == listener && info.userid == userid) {
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700703 mListeners.remove(i);
704 if (info.connection != null) {
705 mContext.unbindService(info.connection);
706 }
Daniel Sandler09a247e2013-02-14 10:24:17 -0500707 }
708 }
709 }
710 }
711
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700712 /**
713 * Remove a listener service for the given user by ComponentName
714 */
715 private void unregisterListenerService(ComponentName name, int userid) {
716 checkCallerIsSystem();
717
718 synchronized (mNotificationList) {
719 final int N = mListeners.size();
720 for (int i=N-1; i>=0; i--) {
721 final NotificationListenerInfo info = mListeners.get(i);
722 if (name.equals(info.component)
723 && info.userid == userid) {
724 mListeners.remove(i);
725 if (info.connection != null) {
726 mContext.unbindService(info.connection);
727 }
728 }
729 }
730 }
731 }
732
733 /**
734 * asynchronously notify all listeners about a new notification
735 */
Daniel Sandler09a247e2013-02-14 10:24:17 -0500736 private void notifyPostedLocked(NotificationRecord n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -0400737 // make a copy in case changes are made to the underlying Notification object
738 final StatusBarNotification sbn = n.sbn.clone();
Daniel Sandler09a247e2013-02-14 10:24:17 -0500739 for (final NotificationListenerInfo info : mListeners) {
740 mHandler.post(new Runnable() {
741 @Override
742 public void run() {
743 info.notifyPostedIfUserMatch(sbn);
744 }});
745 }
746 }
747
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700748 /**
749 * asynchronously notify all listeners about a removed notification
750 */
Daniel Sandler09a247e2013-02-14 10:24:17 -0500751 private void notifyRemovedLocked(NotificationRecord n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -0400752 // make a copy in case changes are made to the underlying Notification object
753 // NOTE: this copy is lightweight: it doesn't include heavyweight parts of the notification
754 final StatusBarNotification sbn_light = n.sbn.cloneLight();
755
Daniel Sandler09a247e2013-02-14 10:24:17 -0500756 for (final NotificationListenerInfo info : mListeners) {
757 mHandler.post(new Runnable() {
758 @Override
759 public void run() {
Daniel Sandler1a497d32013-04-18 14:52:45 -0400760 info.notifyRemovedIfUserMatch(sbn_light);
Daniel Sandler09a247e2013-02-14 10:24:17 -0500761 }});
762 }
763 }
764
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700765 // -- APIs to support listeners clicking/clearing notifications --
766
767 private NotificationListenerInfo checkListenerToken(INotificationListener listener) {
768 final IBinder token = listener.asBinder();
769 final int N = mListeners.size();
770 for (int i=0; i<N; i++) {
771 final NotificationListenerInfo info = mListeners.get(i);
772 if (info.listener.asBinder() == token) return info;
773 }
774 throw new SecurityException("Disallowed call from unknown listener: " + listener);
775 }
776
777 /**
778 * Allow an INotificationListener to simulate a "clear all" operation.
779 *
780 * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onClearAllNotifications}
781 *
782 * @param token The binder for the listener, to check that the caller is allowed
783 */
784 public void clearAllNotificationsFromListener(INotificationListener token) {
785 NotificationListenerInfo info = checkListenerToken(token);
786 long identity = Binder.clearCallingIdentity();
787 try {
788 cancelAll(info.userid);
789 } finally {
790 Binder.restoreCallingIdentity(identity);
791 }
792 }
793
794 /**
795 * Allow an INotificationListener to simulate clearing (dismissing) a single notification.
796 *
797 * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onNotificationClear}
798 *
799 * @param token The binder for the listener, to check that the caller is allowed
800 */
801 public void clearNotificationFromListener(INotificationListener token, String pkg, String tag, int id) {
802 NotificationListenerInfo info = checkListenerToken(token);
803 long identity = Binder.clearCallingIdentity();
804 try {
805 cancelNotification(pkg, tag, id, 0,
806 Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE,
807 true,
808 info.userid);
809 } finally {
810 Binder.restoreCallingIdentity(identity);
811 }
812 }
813
814 // -- end of listener APIs --
815
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500816 public static final class NotificationRecord
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 {
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500818 final StatusBarNotification sbn;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800819 IBinder statusBarKey;
820
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500821 NotificationRecord(StatusBarNotification sbn)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 {
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500823 this.sbn = sbn;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 }
Fred Quintana6ecaff12009-09-25 14:23:13 -0700825
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500826 public Notification getNotification() { return sbn.notification; }
827 public int getFlags() { return sbn.notification.flags; }
828 public int getUserId() { return sbn.getUserId(); }
829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 void dump(PrintWriter pw, String prefix, Context baseContext) {
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500831 final Notification notification = sbn.notification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 pw.println(prefix + this);
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400833 pw.println(prefix + " uid=" + sbn.uid + " userId=" + sbn.getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800834 pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400835 + " / " + idDebugString(baseContext, sbn.pkg, notification.icon));
836 pw.println(prefix + " pri=" + notification.priority + " score=" + sbn.score);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 pw.println(prefix + " contentIntent=" + notification.contentIntent);
838 pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
839 pw.println(prefix + " tickerText=" + notification.tickerText);
840 pw.println(prefix + " contentView=" + notification.contentView);
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400841 pw.println(prefix + String.format(" defaults=0x%08x flags=0x%08x",
842 notification.defaults, notification.flags));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800843 pw.println(prefix + " sound=" + notification.sound);
844 pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate));
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400845 pw.println(prefix + String.format(" led=0x%08x onMs=%d offMs=%d",
846 notification.ledARGB, notification.ledOnMS, notification.ledOffMS));
847 if (notification.actions != null && notification.actions.length > 0) {
848 pw.println(prefix + " actions={");
849 final int N = notification.actions.length;
850 for (int i=0; i<N; i++) {
851 final Notification.Action action = notification.actions[i];
852 pw.println(String.format("%s [%d] \"%s\" -> %s",
853 prefix,
854 i,
855 action.title,
856 action.actionIntent.toString()
857 ));
858 }
859 pw.println(prefix + " }");
860 }
861 if (notification.extras != null && notification.extras.size() > 0) {
862 pw.println(prefix + " extras={");
863 for (String key : notification.extras.keySet()) {
864 pw.print(prefix + " " + key + "=");
865 Object val = notification.extras.get(key);
866 if (val == null) {
867 pw.println("null");
868 } else {
869 pw.print(val.toString());
870 if (val instanceof Bitmap) {
871 pw.print(String.format(" (%dx%d)",
872 ((Bitmap) val).getWidth(),
873 ((Bitmap) val).getHeight()));
874 } else if (val.getClass().isArray()) {
875 pw.println(" {");
876 final int N = Array.getLength(val);
877 for (int i=0; i<N; i++) {
878 if (i > 0) pw.println(",");
879 pw.print(prefix + " " + Array.get(val, i));
880 }
881 pw.print("\n" + prefix + " }");
882 }
883 pw.println();
884 }
885 }
886 pw.println(prefix + " }");
887 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 @Override
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500891 public final String toString() {
892 return String.format(
893 "NotificationRecord(0x%08x: pkg=%s user=%s id=%d tag=%s score=%d: %s)",
894 System.identityHashCode(this),
895 this.sbn.pkg, this.sbn.user, this.sbn.id, this.sbn.tag,
896 this.sbn.score, this.sbn.notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800897 }
898 }
899
900 private static final class ToastRecord
901 {
902 final int pid;
903 final String pkg;
904 final ITransientNotification callback;
905 int duration;
906
907 ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
908 {
909 this.pid = pid;
910 this.pkg = pkg;
911 this.callback = callback;
912 this.duration = duration;
913 }
914
915 void update(int duration) {
916 this.duration = duration;
917 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800918
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 void dump(PrintWriter pw, String prefix) {
920 pw.println(prefix + this);
921 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800922
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 @Override
924 public final String toString()
925 {
926 return "ToastRecord{"
927 + Integer.toHexString(System.identityHashCode(this))
928 + " pkg=" + pkg
929 + " callback=" + callback
930 + " duration=" + duration;
931 }
932 }
933
Joe Onorato089de882010-04-12 08:18:45 -0700934 private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks
935 = new StatusBarManagerService.NotificationCallbacks() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936
937 public void onSetDisabled(int status) {
938 synchronized (mNotificationList) {
939 mDisabledNotifications = status;
940 if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) {
941 // cancel whatever's going on
942 long identity = Binder.clearCallingIdentity();
943 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -0700944 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
945 if (player != null) {
946 player.stopAsync();
947 }
948 } catch (RemoteException e) {
949 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 Binder.restoreCallingIdentity(identity);
951 }
952
953 identity = Binder.clearCallingIdentity();
954 try {
955 mVibrator.cancel();
Jeff Sharkey098d5802012-04-26 17:30:34 -0700956 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800957 Binder.restoreCallingIdentity(identity);
958 }
959 }
960 }
961 }
962
963 public void onClearAll() {
Dianne Hackborn41203752012-08-31 14:05:51 -0700964 // XXX to be totally correct, the caller should tell us which user
965 // this is for.
966 cancelAll(ActivityManager.getCurrentUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 }
968
Fred Quintana6ecaff12009-09-25 14:23:13 -0700969 public void onNotificationClick(String pkg, String tag, int id) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700970 // XXX to be totally correct, the caller should tell us which user
971 // this is for.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700972 cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL,
Dianne Hackborn41203752012-08-31 14:05:51 -0700973 Notification.FLAG_FOREGROUND_SERVICE, false,
974 ActivityManager.getCurrentUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 }
976
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400977 public void onNotificationClear(String pkg, String tag, int id) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700978 // XXX to be totally correct, the caller should tell us which user
979 // this is for.
Joe Onorato46439ce2010-11-19 13:56:21 -0800980 cancelNotification(pkg, tag, id, 0,
981 Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE,
Dianne Hackborn41203752012-08-31 14:05:51 -0700982 true, ActivityManager.getCurrentUser());
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400983 }
984
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 public void onPanelRevealed() {
986 synchronized (mNotificationList) {
987 // sound
988 mSoundNotification = null;
Jeff Sharkey098d5802012-04-26 17:30:34 -0700989
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 long identity = Binder.clearCallingIdentity();
991 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -0700992 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
993 if (player != null) {
994 player.stopAsync();
995 }
996 } catch (RemoteException e) {
997 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 Binder.restoreCallingIdentity(identity);
999 }
1000
1001 // vibrate
1002 mVibrateNotification = null;
1003 identity = Binder.clearCallingIdentity();
1004 try {
1005 mVibrator.cancel();
Jeff Sharkey098d5802012-04-26 17:30:34 -07001006 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001007 Binder.restoreCallingIdentity(identity);
1008 }
1009
1010 // light
1011 mLights.clear();
1012 mLedNotification = null;
1013 updateLightsLocked();
1014 }
1015 }
Joe Onorato005847b2010-06-04 16:08:02 -04001016
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001017 public void onNotificationError(String pkg, String tag, int id,
1018 int uid, int initialPid, String message) {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -04001019 Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id
1020 + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")");
Dianne Hackborn41203752012-08-31 14:05:51 -07001021 // XXX to be totally correct, the caller should tell us which user
1022 // this is for.
1023 cancelNotification(pkg, tag, id, 0, 0, false, UserHandle.getUserId(uid));
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001024 long ident = Binder.clearCallingIdentity();
1025 try {
1026 ActivityManagerNative.getDefault().crashApplication(uid, initialPid, pkg,
1027 "Bad notification posted from package " + pkg
1028 + ": " + message);
1029 } catch (RemoteException e) {
1030 }
1031 Binder.restoreCallingIdentity(ident);
Joe Onorato005847b2010-06-04 16:08:02 -04001032 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001033 };
1034
1035 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
1036 @Override
1037 public void onReceive(Context context, Intent intent) {
1038 String action = intent.getAction();
1039
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001040 boolean queryRestart = false;
Daniel Sandler26ece572012-06-01 15:38:46 -04001041 boolean packageChanged = false;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001042
Mike Lockwood541c9942011-06-12 19:35:45 -04001043 if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001044 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Daniel Sandler26ece572012-06-01 15:38:46 -04001045 || (packageChanged=action.equals(Intent.ACTION_PACKAGE_CHANGED))
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001046 || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001047 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001048 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001049 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001050 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001051 } else if (queryRestart) {
1052 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001053 } else {
1054 Uri uri = intent.getData();
1055 if (uri == null) {
1056 return;
1057 }
1058 String pkgName = uri.getSchemeSpecificPart();
1059 if (pkgName == null) {
1060 return;
1061 }
Daniel Sandler26ece572012-06-01 15:38:46 -04001062 if (packageChanged) {
1063 // We cancel notifications for packages which have just been disabled
1064 final int enabled = mContext.getPackageManager()
1065 .getApplicationEnabledSetting(pkgName);
1066 if (enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED
1067 || enabled == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
1068 return;
1069 }
1070 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001071 pkgList = new String[]{pkgName};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001072 }
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001073
1074 boolean anyListenersInvolved = false;
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001075 if (pkgList != null && (pkgList.length > 0)) {
1076 for (String pkgName : pkgList) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001077 cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart,
1078 UserHandle.USER_ALL);
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001079 if (mEnabledListenerPackageNames.contains(pkgName)) {
1080 anyListenersInvolved = true;
1081 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001082 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 }
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001084
1085 if (anyListenersInvolved) {
1086 // make sure we're still bound to any of our
1087 // listeners who may have just upgraded
1088 rebindListenerServices();
1089 }
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001090 } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
1091 // Keep track of screen on/off state, but do not turn off the notification light
1092 // until user passes through the lock screen or views the notification.
1093 mScreenOn = true;
1094 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
1095 mScreenOn = false;
Daniel Sandlere96ffb12010-03-11 13:38:06 -05001096 } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001097 mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
1098 TelephonyManager.EXTRA_STATE_OFFHOOK));
Daniel Sandlere96ffb12010-03-11 13:38:06 -05001099 updateNotificationPulse();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001100 } else if (action.equals(Intent.ACTION_USER_STOPPED)) {
1101 int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
1102 if (userHandle >= 0) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001103 cancelAllNotificationsInt(null, 0, 0, true, userHandle);
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001104 }
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001105 } else if (action.equals(Intent.ACTION_USER_PRESENT)) {
1106 // turn off LED when user passes through lock screen
1107 mNotificationLight.turnOff();
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001108 } else if (action.equals(Intent.ACTION_USER_SWITCHED)) {
1109 // reload per-user settings
1110 mSettingsObserver.update(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 }
1112 }
1113 };
1114
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001115 class SettingsObserver extends ContentObserver {
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001116 private final Uri NOTIFICATION_LIGHT_PULSE_URI
1117 = Settings.System.getUriFor(Settings.System.NOTIFICATION_LIGHT_PULSE);
1118
1119 private final Uri ENABLED_NOTIFICATION_LISTENERS_URI
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001120 = Settings.Secure.getUriFor(Settings.Secure.ENABLED_NOTIFICATION_LISTENERS);
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001121
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001122 SettingsObserver(Handler handler) {
1123 super(handler);
1124 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001125
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001126 void observe() {
1127 ContentResolver resolver = mContext.getContentResolver();
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001128 resolver.registerContentObserver(NOTIFICATION_LIGHT_PULSE_URI,
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001129 false, this, UserHandle.USER_ALL);
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001130 resolver.registerContentObserver(ENABLED_NOTIFICATION_LISTENERS_URI,
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001131 false, this, UserHandle.USER_ALL);
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001132 update(null);
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001133 }
1134
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001135 @Override public void onChange(boolean selfChange, Uri uri) {
1136 update(uri);
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001137 }
1138
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001139 public void update(Uri uri) {
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001140 ContentResolver resolver = mContext.getContentResolver();
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001141 if (uri == null || NOTIFICATION_LIGHT_PULSE_URI.equals(uri)) {
1142 boolean pulseEnabled = Settings.System.getInt(resolver,
1143 Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0;
1144 if (mNotificationPulseEnabled != pulseEnabled) {
1145 mNotificationPulseEnabled = pulseEnabled;
1146 updateNotificationPulse();
1147 }
1148 }
1149 if (uri == null || ENABLED_NOTIFICATION_LISTENERS_URI.equals(uri)) {
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001150 rebindListenerServices();
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001151 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001152 }
1153 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001154
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001155 private SettingsObserver mSettingsObserver;
1156
Daniel Sandleredbb3802012-11-13 20:49:47 -08001157 static long[] getLongArray(Resources r, int resid, int maxlen, long[] def) {
1158 int[] ar = r.getIntArray(resid);
1159 if (ar == null) {
1160 return def;
1161 }
1162 final int len = ar.length > maxlen ? maxlen : ar.length;
1163 long[] out = new long[len];
1164 for (int i=0; i<len; i++) {
1165 out[i] = ar[i];
1166 }
1167 return out;
1168 }
1169
Joe Onorato089de882010-04-12 08:18:45 -07001170 NotificationManagerService(Context context, StatusBarManagerService statusBar,
Mike Lockwood3a322132009-11-24 00:30:52 -05001171 LightsService lights)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001172 {
1173 super();
1174 mContext = context;
Jeff Brownc2346132012-04-13 01:55:38 -07001175 mVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 mAm = ActivityManagerNative.getDefault();
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001177 mUserManager = (UserManager)context.getSystemService(Context.USER_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001178 mToastQueue = new ArrayList<ToastRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001179 mHandler = new WorkerHandler();
San Mehat3ee13172010-02-04 20:54:43 -08001180
Daniel Sandler4a900acd2013-01-30 14:04:10 -05001181 mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
1182
1183 importOldBlockDb();
Daniel Sandler0da673f2012-04-11 12:33:16 -04001184
Joe Onorato089de882010-04-12 08:18:45 -07001185 mStatusBar = statusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 statusBar.setNotificationCallbacks(mNotificationCallbacks);
1187
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001188 mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS);
1189 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
1190
Mike Lockwood670f9322010-01-20 12:13:36 -05001191 Resources resources = mContext.getResources();
1192 mDefaultNotificationColor = resources.getColor(
1193 com.android.internal.R.color.config_defaultNotificationColor);
1194 mDefaultNotificationLedOn = resources.getInteger(
1195 com.android.internal.R.integer.config_defaultNotificationLedOn);
1196 mDefaultNotificationLedOff = resources.getInteger(
1197 com.android.internal.R.integer.config_defaultNotificationLedOff);
1198
Daniel Sandleredbb3802012-11-13 20:49:47 -08001199 mDefaultVibrationPattern = getLongArray(resources,
1200 com.android.internal.R.array.config_defaultNotificationVibePattern,
1201 VIBRATE_PATTERN_MAXLEN,
1202 DEFAULT_VIBRATE_PATTERN);
1203
1204 mFallbackVibrationPattern = getLongArray(resources,
1205 com.android.internal.R.array.config_notificationFallbackVibePattern,
1206 VIBRATE_PATTERN_MAXLEN,
1207 DEFAULT_VIBRATE_PATTERN);
1208
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001209 // Don't start allowing notifications until the setup wizard has run once.
1210 // After that, including subsequent boots, init with notifications turned on.
1211 // This works on the first boot because the setup wizard will toggle this
1212 // flag at least once and we'll go back to 0 after that.
Jeff Brownbf6f6f92012-09-25 15:03:20 -07001213 if (0 == Settings.Global.getInt(mContext.getContentResolver(),
1214 Settings.Global.DEVICE_PROVISIONED, 0)) {
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001215 mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
1216 }
1217
Mike Lockwood35e16bf2010-11-30 19:53:36 -05001218 // register for various Intents
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 IntentFilter filter = new IntentFilter();
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001220 filter.addAction(Intent.ACTION_SCREEN_ON);
1221 filter.addAction(Intent.ACTION_SCREEN_OFF);
Daniel Sandlere96ffb12010-03-11 13:38:06 -05001222 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001223 filter.addAction(Intent.ACTION_USER_PRESENT);
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001224 filter.addAction(Intent.ACTION_USER_STOPPED);
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001225 filter.addAction(Intent.ACTION_USER_SWITCHED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001227 IntentFilter pkgFilter = new IntentFilter();
1228 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
Daniel Sandleraac0eb02011-08-06 22:51:56 -04001229 pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001230 pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
1231 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
1232 pkgFilter.addDataScheme("package");
1233 mContext.registerReceiver(mIntentReceiver, pkgFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001234 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001235 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001236
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001237 mSettingsObserver = new SettingsObserver(mHandler);
1238 mSettingsObserver.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001239 }
1240
Daniel Sandler4a900acd2013-01-30 14:04:10 -05001241 /**
1242 * Read the old XML-based app block database and import those blockages into the AppOps system.
1243 */
1244 private void importOldBlockDb() {
1245 loadBlockDb();
1246
1247 PackageManager pm = mContext.getPackageManager();
1248 for (String pkg : mBlockedPackages) {
1249 PackageInfo info = null;
1250 try {
1251 info = pm.getPackageInfo(pkg, 0);
1252 setNotificationsEnabledForPackage(pkg, info.applicationInfo.uid, false);
1253 } catch (NameNotFoundException e) {
1254 // forget you
1255 }
1256 }
1257 mBlockedPackages.clear();
1258 if (mPolicyFile != null) {
1259 mPolicyFile.delete();
1260 }
1261 }
1262
Joe Onorato30275482009-07-08 17:09:14 -07001263 void systemReady() {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001264 mAudioService = IAudioService.Stub.asInterface(
1265 ServiceManager.getService(Context.AUDIO_SERVICE));
1266
Joe Onorato30275482009-07-08 17:09:14 -07001267 // no beeping until we're basically done booting
1268 mSystemReady = true;
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001269
1270 // make sure our listener services are properly bound
1271 rebindListenerServices();
Joe Onorato30275482009-07-08 17:09:14 -07001272 }
1273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 // Toasts
1275 // ============================================================================
1276 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
1277 {
Daniel Sandlera7035902010-03-30 15:45:31 -04001278 if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001279
1280 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001281 Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 return ;
1283 }
1284
Daniel Sandler0da673f2012-04-11 12:33:16 -04001285 final boolean isSystemToast = ("android".equals(pkg));
1286
Daniel Sandler4a900acd2013-01-30 14:04:10 -05001287 if (ENABLE_BLOCKED_TOASTS && !noteNotificationOp(pkg, Binder.getCallingUid())) {
1288 if (!isSystemToast) {
1289 Slog.e(TAG, "Suppressing toast from package " + pkg + " by user request.");
1290 return;
1291 }
Daniel Sandler0da673f2012-04-11 12:33:16 -04001292 }
1293
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001294 synchronized (mToastQueue) {
1295 int callingPid = Binder.getCallingPid();
1296 long callingId = Binder.clearCallingIdentity();
1297 try {
1298 ToastRecord record;
1299 int index = indexOfToastLocked(pkg, callback);
1300 // If it's already in the queue, we update it in place, we don't
1301 // move it to the end of the queue.
1302 if (index >= 0) {
1303 record = mToastQueue.get(index);
1304 record.update(duration);
1305 } else {
Vairavan Srinivasanf9eb06c2011-01-21 18:08:36 -08001306 // Limit the number of toasts that any given package except the android
1307 // package can enqueue. Prevents DOS attacks and deals with leaks.
Daniel Sandler0da673f2012-04-11 12:33:16 -04001308 if (!isSystemToast) {
Vairavan Srinivasanf9eb06c2011-01-21 18:08:36 -08001309 int count = 0;
1310 final int N = mToastQueue.size();
1311 for (int i=0; i<N; i++) {
1312 final ToastRecord r = mToastQueue.get(i);
1313 if (r.pkg.equals(pkg)) {
1314 count++;
1315 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
1316 Slog.e(TAG, "Package has already posted " + count
1317 + " toasts. Not showing more. Package=" + pkg);
1318 return;
1319 }
1320 }
1321 }
1322 }
1323
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001324 record = new ToastRecord(callingPid, pkg, callback, duration);
1325 mToastQueue.add(record);
1326 index = mToastQueue.size() - 1;
1327 keepProcessAliveLocked(callingPid);
1328 }
1329 // If it's at index 0, it's the current toast. It doesn't matter if it's
1330 // new or just been updated. Call back and tell it to show itself.
1331 // If the callback fails, this will remove it from the list, so don't
1332 // assume that it's valid after this.
1333 if (index == 0) {
1334 showNextToastLocked();
1335 }
1336 } finally {
1337 Binder.restoreCallingIdentity(callingId);
1338 }
1339 }
1340 }
1341
1342 public void cancelToast(String pkg, ITransientNotification callback) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001343 Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344
1345 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001346 Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001347 return ;
1348 }
1349
1350 synchronized (mToastQueue) {
1351 long callingId = Binder.clearCallingIdentity();
1352 try {
1353 int index = indexOfToastLocked(pkg, callback);
1354 if (index >= 0) {
1355 cancelToastLocked(index);
1356 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001357 Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 }
1359 } finally {
1360 Binder.restoreCallingIdentity(callingId);
1361 }
1362 }
1363 }
1364
1365 private void showNextToastLocked() {
1366 ToastRecord record = mToastQueue.get(0);
1367 while (record != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001368 if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001369 try {
1370 record.callback.show();
1371 scheduleTimeoutLocked(record, false);
1372 return;
1373 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001374 Slog.w(TAG, "Object died trying to show notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 + " in package " + record.pkg);
1376 // remove it from the list and let the process die
1377 int index = mToastQueue.indexOf(record);
1378 if (index >= 0) {
1379 mToastQueue.remove(index);
1380 }
1381 keepProcessAliveLocked(record.pid);
1382 if (mToastQueue.size() > 0) {
1383 record = mToastQueue.get(0);
1384 } else {
1385 record = null;
1386 }
1387 }
1388 }
1389 }
1390
1391 private void cancelToastLocked(int index) {
1392 ToastRecord record = mToastQueue.get(index);
1393 try {
1394 record.callback.hide();
1395 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001396 Slog.w(TAG, "Object died trying to hide notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 + " in package " + record.pkg);
1398 // don't worry about this, we're about to remove it from
1399 // the list anyway
1400 }
1401 mToastQueue.remove(index);
1402 keepProcessAliveLocked(record.pid);
1403 if (mToastQueue.size() > 0) {
1404 // Show the next one. If the callback fails, this will remove
1405 // it from the list, so don't assume that the list hasn't changed
1406 // after this point.
1407 showNextToastLocked();
1408 }
1409 }
1410
1411 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
1412 {
1413 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
1414 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
1415 mHandler.removeCallbacksAndMessages(r);
1416 mHandler.sendMessageDelayed(m, delay);
1417 }
1418
1419 private void handleTimeout(ToastRecord record)
1420 {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001421 if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 synchronized (mToastQueue) {
1423 int index = indexOfToastLocked(record.pkg, record.callback);
1424 if (index >= 0) {
1425 cancelToastLocked(index);
1426 }
1427 }
1428 }
1429
1430 // lock on mToastQueue
1431 private int indexOfToastLocked(String pkg, ITransientNotification callback)
1432 {
1433 IBinder cbak = callback.asBinder();
1434 ArrayList<ToastRecord> list = mToastQueue;
1435 int len = list.size();
1436 for (int i=0; i<len; i++) {
1437 ToastRecord r = list.get(i);
1438 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
1439 return i;
1440 }
1441 }
1442 return -1;
1443 }
1444
1445 // lock on mToastQueue
1446 private void keepProcessAliveLocked(int pid)
1447 {
1448 int toastCount = 0; // toasts from this pid
1449 ArrayList<ToastRecord> list = mToastQueue;
1450 int N = list.size();
1451 for (int i=0; i<N; i++) {
1452 ToastRecord r = list.get(i);
1453 if (r.pid == pid) {
1454 toastCount++;
1455 }
1456 }
1457 try {
1458 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
1459 } catch (RemoteException e) {
1460 // Shouldn't happen.
1461 }
1462 }
1463
1464 private final class WorkerHandler extends Handler
1465 {
1466 @Override
1467 public void handleMessage(Message msg)
1468 {
1469 switch (msg.what)
1470 {
1471 case MESSAGE_TIMEOUT:
1472 handleTimeout((ToastRecord)msg.obj);
1473 break;
1474 }
1475 }
1476 }
1477
1478
1479 // Notifications
1480 // ============================================================================
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001481 public void enqueueNotificationWithTag(String pkg, String basePkg, String tag, int id,
1482 Notification notification, int[] idOut, int userId)
Fred Quintana6ecaff12009-09-25 14:23:13 -07001483 {
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001484 enqueueNotificationInternal(pkg, basePkg, Binder.getCallingUid(), Binder.getCallingPid(),
Dianne Hackborn41203752012-08-31 14:05:51 -07001485 tag, id, notification, idOut, userId);
Daniel Sandlerd0a2f862010-08-03 15:29:31 -04001486 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001487
1488 private final static int clamp(int x, int low, int high) {
1489 return (x < low) ? low : ((x > high) ? high : x);
Daniel Sandlere40451a2011-02-03 14:51:35 -05001490 }
1491
Daniel Sandlerd0a2f862010-08-03 15:29:31 -04001492 // Not exposed via Binder; for system use only (otherwise malicious apps could spoof the
1493 // uid/pid of another application)
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001494 public void enqueueNotificationInternal(String pkg, String basePkg, int callingUid,
1495 int callingPid, String tag, int id, Notification notification, int[] idOut, int userId)
Daniel Sandlerd0a2f862010-08-03 15:29:31 -04001496 {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001497 if (DBG) {
1498 Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id + " notification=" + notification);
1499 }
1500 checkCallerIsSystemOrSameApp(pkg);
1501 final boolean isSystemNotification = ("android".equals(pkg));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001502
Dianne Hackborn41203752012-08-31 14:05:51 -07001503 userId = ActivityManager.handleIncomingUser(callingPid,
Amith Yamasani2c7ebea2012-10-30 15:28:27 -07001504 callingUid, userId, true, false, "enqueueNotification", pkg);
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -07001505 final UserHandle user = new UserHandle(userId);
Dianne Hackborn41203752012-08-31 14:05:51 -07001506
Joe Onoratobd73d012010-06-04 11:44:54 -07001507 // Limit the number of notifications that any given package except the android
1508 // package can enqueue. Prevents DOS attacks and deals with leaks.
Daniel Sandler0da673f2012-04-11 12:33:16 -04001509 if (!isSystemNotification) {
Joe Onoratobd73d012010-06-04 11:44:54 -07001510 synchronized (mNotificationList) {
1511 int count = 0;
1512 final int N = mNotificationList.size();
1513 for (int i=0; i<N; i++) {
1514 final NotificationRecord r = mNotificationList.get(i);
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001515 if (r.sbn.pkg.equals(pkg) && r.sbn.getUserId() == userId) {
Joe Onoratobd73d012010-06-04 11:44:54 -07001516 count++;
1517 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
1518 Slog.e(TAG, "Package has already posted " + count
1519 + " notifications. Not showing more. package=" + pkg);
1520 return;
1521 }
1522 }
1523 }
1524 }
1525 }
1526
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001527 // This conditional is a dirty hack to limit the logging done on
1528 // behalf of the download manager without affecting other apps.
1529 if (!pkg.equals("com.android.providers.downloads")
1530 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Daniel Sandler321e9c52012-10-12 10:59:26 -07001531 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, tag, userId,
Daniel Sandlerb64cb882011-11-29 23:48:29 -05001532 notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 }
1534
1535 if (pkg == null || notification == null) {
1536 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
1537 + " id=" + id + " notification=" + notification);
1538 }
1539 if (notification.icon != 0) {
1540 if (notification.contentView == null) {
1541 throw new IllegalArgumentException("contentView required: pkg=" + pkg
1542 + " id=" + id + " notification=" + notification);
1543 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 }
1545
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001546 // === Scoring ===
Daniel Sandler0da673f2012-04-11 12:33:16 -04001547
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001548 // 0. Sanitize inputs
1549 notification.priority = clamp(notification.priority, Notification.PRIORITY_MIN, Notification.PRIORITY_MAX);
1550 // Migrate notification flags to scores
1551 if (0 != (notification.flags & Notification.FLAG_HIGH_PRIORITY)) {
1552 if (notification.priority < Notification.PRIORITY_MAX) notification.priority = Notification.PRIORITY_MAX;
Daniel Sandler49a2ad12012-03-28 15:46:39 -04001553 } else if (SCORE_ONGOING_HIGHER && 0 != (notification.flags & Notification.FLAG_ONGOING_EVENT)) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001554 if (notification.priority < Notification.PRIORITY_HIGH) notification.priority = Notification.PRIORITY_HIGH;
1555 }
Daniel Sandler0da673f2012-04-11 12:33:16 -04001556
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001557 // 1. initial score: buckets of 10, around the app
Daniel Sandler0da673f2012-04-11 12:33:16 -04001558 int score = notification.priority * NOTIFICATION_PRIORITY_MULTIPLIER; //[-20..20]
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001559
Daniel Sandler0da673f2012-04-11 12:33:16 -04001560 // 2. Consult external heuristics (TBD)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001561
Daniel Sandler0da673f2012-04-11 12:33:16 -04001562 // 3. Apply local rules
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001563
1564 // blocked apps
Daniel Sandler4a900acd2013-01-30 14:04:10 -05001565 if (ENABLE_BLOCKED_NOTIFICATIONS && !noteNotificationOp(pkg, callingUid)) {
1566 if (!isSystemNotification) {
1567 score = JUNK_SCORE;
1568 Slog.e(TAG, "Suppressing notification from package " + pkg + " by user request.");
1569 }
Daniel Sandler0da673f2012-04-11 12:33:16 -04001570 }
1571
1572 if (DBG) {
1573 Slog.v(TAG, "Assigned score=" + score + " to " + notification);
1574 }
1575
1576 if (score < SCORE_DISPLAY_THRESHOLD) {
1577 // Notification will be blocked because the score is too low.
1578 return;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001579 }
1580
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001581 // Should this notification make noise, vibe, or use the LED?
1582 final boolean canInterrupt = (score >= SCORE_INTERRUPTION_THRESHOLD);
1583
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001584 synchronized (mNotificationList) {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001585 final StatusBarNotification n = new StatusBarNotification(
1586 pkg, id, tag, callingUid, callingPid, score, notification, user);
1587 NotificationRecord r = new NotificationRecord(n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 NotificationRecord old = null;
1589
Dianne Hackborn41203752012-08-31 14:05:51 -07001590 int index = indexOfNotificationLocked(pkg, tag, id, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 if (index < 0) {
1592 mNotificationList.add(r);
1593 } else {
1594 old = mNotificationList.remove(index);
1595 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001596 // Make sure we don't lose the foreground service state.
1597 if (old != null) {
1598 notification.flags |=
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001599 old.getNotification().flags&Notification.FLAG_FOREGROUND_SERVICE;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001600 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001601 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001602
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001603 // Ensure if this is a foreground service that the proper additional
1604 // flags are set.
1605 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
1606 notification.flags |= Notification.FLAG_ONGOING_EVENT
1607 | Notification.FLAG_NO_CLEAR;
1608 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001609
Svetoslav Ganovc31ed392012-10-10 14:58:28 -07001610 final int currentUser;
1611 final long token = Binder.clearCallingIdentity();
1612 try {
1613 currentUser = ActivityManager.getCurrentUser();
1614 } finally {
1615 Binder.restoreCallingIdentity(token);
1616 }
1617
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001618 if (notification.icon != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001619 if (old != null && old.statusBarKey != null) {
1620 r.statusBarKey = old.statusBarKey;
1621 long identity = Binder.clearCallingIdentity();
1622 try {
Joe Onorato18e69df2010-05-17 22:26:12 -07001623 mStatusBar.updateNotification(r.statusBarKey, n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 }
1625 finally {
1626 Binder.restoreCallingIdentity(identity);
1627 }
1628 } else {
1629 long identity = Binder.clearCallingIdentity();
1630 try {
Joe Onorato18e69df2010-05-17 22:26:12 -07001631 r.statusBarKey = mStatusBar.addNotification(n);
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001632 if ((n.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0
1633 && canInterrupt) {
Mike Lockwoodece18ef2012-02-13 20:42:19 -08001634 mAttentionLight.pulse();
1635 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001636 }
1637 finally {
1638 Binder.restoreCallingIdentity(identity);
1639 }
1640 }
Svetoslav Ganovc31ed392012-10-10 14:58:28 -07001641 // Send accessibility events only for the current user.
1642 if (currentUser == userId) {
1643 sendAccessibilityEvent(notification, pkg);
1644 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001645
Daniel Sandler09a247e2013-02-14 10:24:17 -05001646 notifyPostedLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 } else {
Daniel Sandlere40451a2011-02-03 14:51:35 -05001648 Slog.e(TAG, "Ignoring notification with icon==0: " + notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649 if (old != null && old.statusBarKey != null) {
1650 long identity = Binder.clearCallingIdentity();
1651 try {
Joe Onorato0cbda992010-05-02 16:28:15 -07001652 mStatusBar.removeNotification(old.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001653 }
1654 finally {
1655 Binder.restoreCallingIdentity(identity);
1656 }
Daniel Sandler09a247e2013-02-14 10:24:17 -05001657
1658 notifyRemovedLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001660 return; // do not play sounds, show lights, etc. for invalid notifications
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 }
1662
1663 // If we're not supposed to beep, vibrate, etc. then don't.
1664 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
1665 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -07001666 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001667 && (r.getUserId() == UserHandle.USER_ALL ||
1668 (r.getUserId() == userId && r.getUserId() == currentUser))
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001669 && canInterrupt
Joe Onorato30275482009-07-08 17:09:14 -07001670 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -08001671
1672 final AudioManager audioManager = (AudioManager) mContext
1673 .getSystemService(Context.AUDIO_SERVICE);
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001674
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 // sound
Daniel Sandler31475232013-04-17 00:17:22 -04001676
1677 // should we use the default notification sound? (indicated either by DEFAULT_SOUND
1678 // or because notification.sound is pointing at Settings.System.NOTIFICATION_SOUND)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001679 final boolean useDefaultSound =
Daniel Sandler31475232013-04-17 00:17:22 -04001680 (notification.defaults & Notification.DEFAULT_SOUND) != 0
1681 || Settings.System.DEFAULT_NOTIFICATION_URI.equals(notification.sound);
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001682
1683 Uri soundUri = null;
1684 boolean hasValidSound = false;
1685
1686 if (useDefaultSound) {
1687 soundUri = Settings.System.DEFAULT_NOTIFICATION_URI;
1688
1689 // check to see if the default notification sound is silent
1690 ContentResolver resolver = mContext.getContentResolver();
1691 hasValidSound = Settings.System.getString(resolver,
1692 Settings.System.NOTIFICATION_SOUND) != null;
1693 } else if (notification.sound != null) {
1694 soundUri = notification.sound;
1695 hasValidSound = (soundUri != null);
1696 }
1697
1698 if (hasValidSound) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
1700 int audioStreamType;
1701 if (notification.audioStreamType >= 0) {
1702 audioStreamType = notification.audioStreamType;
1703 } else {
1704 audioStreamType = DEFAULT_STREAM_TYPE;
1705 }
1706 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -08001707 // do not play notifications if stream volume is 0
Jean-Michel Trivid6770542012-10-10 12:03:41 -07001708 // (typically because ringer mode is silent) or if speech recognition is active.
1709 if ((audioManager.getStreamVolume(audioStreamType) != 0)
1710 && !audioManager.isSpeechRecognitionActive()) {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001711 final long identity = Binder.clearCallingIdentity();
Eric Laurent524dc042009-11-27 05:07:55 -08001712 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001713 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
1714 if (player != null) {
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001715 player.playAsync(soundUri, user, looping, audioStreamType);
Jeff Sharkey098d5802012-04-26 17:30:34 -07001716 }
1717 } catch (RemoteException e) {
1718 } finally {
Eric Laurent524dc042009-11-27 05:07:55 -08001719 Binder.restoreCallingIdentity(identity);
1720 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001721 }
1722 }
1723
1724 // vibrate
Daniel Sandleredbb3802012-11-13 20:49:47 -08001725 // Does the notification want to specify its own vibration?
1726 final boolean hasCustomVibrate = notification.vibrate != null;
1727
David Agnew71789e12012-11-09 23:03:26 -05001728 // new in 4.2: if there was supposed to be a sound and we're in vibrate mode,
Daniel Sandler4a7a9b92012-11-20 12:59:41 -05001729 // and no other vibration is specified, we fall back to vibration
David Agnew71789e12012-11-09 23:03:26 -05001730 final boolean convertSoundToVibration =
Daniel Sandleredbb3802012-11-13 20:49:47 -08001731 !hasCustomVibrate
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001732 && hasValidSound
David Agnew71789e12012-11-09 23:03:26 -05001733 && (audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE);
1734
Daniel Sandler4a7a9b92012-11-20 12:59:41 -05001735 // The DEFAULT_VIBRATE flag trumps any custom vibration AND the fallback.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 final boolean useDefaultVibrate =
Daniel Sandleredbb3802012-11-13 20:49:47 -08001737 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
David Agnew71789e12012-11-09 23:03:26 -05001738
Daniel Sandleredbb3802012-11-13 20:49:47 -08001739 if ((useDefaultVibrate || convertSoundToVibration || hasCustomVibrate)
Eric Laurentbffc3d12012-05-07 17:43:49 -07001740 && !(audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001741 mVibrateNotification = r;
1742
Daniel Sandleredbb3802012-11-13 20:49:47 -08001743 if (useDefaultVibrate || convertSoundToVibration) {
1744 // Escalate privileges so we can use the vibrator even if the notifying app
1745 // does not have the VIBRATE permission.
1746 long identity = Binder.clearCallingIdentity();
1747 try {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001748 mVibrator.vibrate(r.sbn.uid, r.sbn.basePkg,
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001749 useDefaultVibrate ? mDefaultVibrationPattern
1750 : mFallbackVibrationPattern,
Daniel Sandleredbb3802012-11-13 20:49:47 -08001751 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
1752 } finally {
1753 Binder.restoreCallingIdentity(identity);
1754 }
1755 } else if (notification.vibrate.length > 1) {
1756 // If you want your own vibration pattern, you need the VIBRATE permission
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001757 mVibrator.vibrate(r.sbn.uid, r.sbn.basePkg, notification.vibrate,
Daniel Sandleredbb3802012-11-13 20:49:47 -08001758 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
1759 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001760 }
1761 }
1762
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001763 // light
1764 // the most recent thing gets the light
1765 mLights.remove(old);
1766 if (mLedNotification == old) {
1767 mLedNotification = null;
1768 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001769 //Slog.i(TAG, "notification.lights="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001770 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001771 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0
1772 && canInterrupt) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 mLights.add(r);
1774 updateLightsLocked();
1775 } else {
1776 if (old != null
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001777 && ((old.getFlags() & Notification.FLAG_SHOW_LIGHTS) != 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 updateLightsLocked();
1779 }
1780 }
1781 }
1782
1783 idOut[0] = id;
1784 }
1785
Joe Onorato30275482009-07-08 17:09:14 -07001786 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -07001787 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
1788 if (!manager.isEnabled()) {
1789 return;
1790 }
1791
1792 AccessibilityEvent event =
1793 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
1794 event.setPackageName(packageName);
1795 event.setClassName(Notification.class.getName());
1796 event.setParcelableData(notification);
1797 CharSequence tickerText = notification.tickerText;
1798 if (!TextUtils.isEmpty(tickerText)) {
1799 event.getText().add(tickerText);
1800 }
1801
1802 manager.sendAccessibilityEvent(event);
1803 }
1804
Joe Onorato46439ce2010-11-19 13:56:21 -08001805 private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete) {
1806 // tell the app
1807 if (sendDelete) {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001808 if (r.getNotification().deleteIntent != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001809 try {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001810 r.getNotification().deleteIntent.send();
Joe Onorato46439ce2010-11-19 13:56:21 -08001811 } catch (PendingIntent.CanceledException ex) {
1812 // do nothing - there's no relevant way to recover, and
1813 // no reason to let this propagate
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001814 Slog.w(TAG, "canceled PendingIntent for " + r.sbn.pkg, ex);
Joe Onorato46439ce2010-11-19 13:56:21 -08001815 }
1816 }
1817 }
1818
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 // status bar
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001820 if (r.getNotification().icon != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 long identity = Binder.clearCallingIdentity();
1822 try {
Joe Onorato0cbda992010-05-02 16:28:15 -07001823 mStatusBar.removeNotification(r.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 }
1825 finally {
1826 Binder.restoreCallingIdentity(identity);
1827 }
1828 r.statusBarKey = null;
Daniel Sandler09a247e2013-02-14 10:24:17 -05001829 notifyRemovedLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001830 }
1831
1832 // sound
1833 if (mSoundNotification == r) {
1834 mSoundNotification = null;
Jeff Sharkey098d5802012-04-26 17:30:34 -07001835 final long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001837 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
1838 if (player != null) {
1839 player.stopAsync();
1840 }
1841 } catch (RemoteException e) {
1842 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001843 Binder.restoreCallingIdentity(identity);
1844 }
1845 }
1846
1847 // vibrate
1848 if (mVibrateNotification == r) {
1849 mVibrateNotification = null;
1850 long identity = Binder.clearCallingIdentity();
1851 try {
1852 mVibrator.cancel();
1853 }
1854 finally {
1855 Binder.restoreCallingIdentity(identity);
1856 }
1857 }
1858
1859 // light
1860 mLights.remove(r);
1861 if (mLedNotification == r) {
1862 mLedNotification = null;
1863 }
Daniel Sandler23d7c702013-03-07 16:32:06 -05001864
1865 // Save it for users of getHistoricalNotifications()
1866 mArchive.record(r.sbn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 }
1868
1869 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001870 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001871 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 */
Fred Quintana6ecaff12009-09-25 14:23:13 -07001873 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Dianne Hackborn41203752012-08-31 14:05:51 -07001874 int mustNotHaveFlags, boolean sendDelete, int userId) {
Daniel Sandler321e9c52012-10-12 10:59:26 -07001875 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, tag, userId,
Daniel Sandlerb64cb882011-11-29 23:48:29 -05001876 mustHaveFlags, mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877
1878 synchronized (mNotificationList) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001879 int index = indexOfNotificationLocked(pkg, tag, id, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07001881 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001882
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001883 if ((r.getNotification().flags & mustHaveFlags) != mustHaveFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 return;
1885 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001886 if ((r.getNotification().flags & mustNotHaveFlags) != 0) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001887 return;
1888 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001889
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001890 mNotificationList.remove(index);
1891
Joe Onorato46439ce2010-11-19 13:56:21 -08001892 cancelNotificationLocked(r, sendDelete);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001893 updateLightsLocked();
1894 }
1895 }
1896 }
1897
1898 /**
Daniel Sandler321e9c52012-10-12 10:59:26 -07001899 * Determine whether the userId applies to the notification in question, either because
1900 * they match exactly, or one of them is USER_ALL (which is treated as a wildcard).
1901 */
1902 private boolean notificationMatchesUserId(NotificationRecord r, int userId) {
1903 return
1904 // looking for USER_ALL notifications? match everything
1905 userId == UserHandle.USER_ALL
1906 // a notification sent to USER_ALL matches any query
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001907 || r.getUserId() == UserHandle.USER_ALL
Daniel Sandler321e9c52012-10-12 10:59:26 -07001908 // an exact user match
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001909 || r.getUserId() == userId;
Daniel Sandler321e9c52012-10-12 10:59:26 -07001910 }
1911
1912 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001913 * Cancels all notifications from a given package that have all of the
1914 * {@code mustHaveFlags}.
1915 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001916 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
Dianne Hackborn41203752012-08-31 14:05:51 -07001917 int mustNotHaveFlags, boolean doit, int userId) {
Daniel Sandler321e9c52012-10-12 10:59:26 -07001918 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, userId,
1919 mustHaveFlags, mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920
1921 synchronized (mNotificationList) {
1922 final int N = mNotificationList.size();
1923 boolean canceledSomething = false;
1924 for (int i = N-1; i >= 0; --i) {
1925 NotificationRecord r = mNotificationList.get(i);
Daniel Sandler321e9c52012-10-12 10:59:26 -07001926 if (!notificationMatchesUserId(r, userId)) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001927 continue;
1928 }
Amith Yamasani5ec00e92012-11-07 16:58:30 -08001929 // Don't remove notifications to all, if there's no package name specified
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001930 if (r.getUserId() == UserHandle.USER_ALL && pkg == null) {
Amith Yamasani5ec00e92012-11-07 16:58:30 -08001931 continue;
1932 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001933 if ((r.getFlags() & mustHaveFlags) != mustHaveFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001934 continue;
1935 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001936 if ((r.getFlags() & mustNotHaveFlags) != 0) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001937 continue;
1938 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001939 if (pkg != null && !r.sbn.pkg.equals(pkg)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001940 continue;
1941 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001942 canceledSomething = true;
1943 if (!doit) {
1944 return true;
1945 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001946 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08001947 cancelNotificationLocked(r, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 }
1949 if (canceledSomething) {
1950 updateLightsLocked();
1951 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001952 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001953 }
1954 }
1955
Dianne Hackborn41203752012-08-31 14:05:51 -07001956 public void cancelNotificationWithTag(String pkg, String tag, int id, int userId) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001957 checkCallerIsSystemOrSameApp(pkg);
Dianne Hackborn41203752012-08-31 14:05:51 -07001958 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Amith Yamasani2c7ebea2012-10-30 15:28:27 -07001959 Binder.getCallingUid(), userId, true, false, "cancelNotificationWithTag", pkg);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001960 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -07001961 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001962 Binder.getCallingUid() == Process.SYSTEM_UID
Dianne Hackborn41203752012-08-31 14:05:51 -07001963 ? 0 : Notification.FLAG_FOREGROUND_SERVICE, false, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001964 }
1965
Dianne Hackborn41203752012-08-31 14:05:51 -07001966 public void cancelAllNotifications(String pkg, int userId) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001967 checkCallerIsSystemOrSameApp(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001968
Dianne Hackborn41203752012-08-31 14:05:51 -07001969 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Amith Yamasani2c7ebea2012-10-30 15:28:27 -07001970 Binder.getCallingUid(), userId, true, false, "cancelAllNotifications", pkg);
Dianne Hackborn41203752012-08-31 14:05:51 -07001971
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001972 // Calling from user space, don't allow the canceling of actively
1973 // running foreground services.
Dianne Hackborn41203752012-08-31 14:05:51 -07001974 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001975 }
1976
Daniel Sandler0da673f2012-04-11 12:33:16 -04001977 void checkCallerIsSystem() {
1978 int uid = Binder.getCallingUid();
Dianne Hackborn0c380492012-08-20 17:23:30 -07001979 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001980 return;
1981 }
1982 throw new SecurityException("Disallowed call for uid " + uid);
1983 }
1984
1985 void checkCallerIsSystemOrSameApp(String pkg) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001986 int uid = Binder.getCallingUid();
Dianne Hackborn0c380492012-08-20 17:23:30 -07001987 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001988 return;
1989 }
1990 try {
Amith Yamasanif203aee2012-08-29 18:41:53 -07001991 ApplicationInfo ai = AppGlobals.getPackageManager().getApplicationInfo(
1992 pkg, 0, UserHandle.getCallingUserId());
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001993 if (!UserHandle.isSameApp(ai.uid, uid)) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001994 throw new SecurityException("Calling uid " + uid + " gave package"
1995 + pkg + " which is owned by uid " + ai.uid);
1996 }
Amith Yamasanif203aee2012-08-29 18:41:53 -07001997 } catch (RemoteException re) {
1998 throw new SecurityException("Unknown package " + pkg + "\n" + re);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001999 }
2000 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002001
Dianne Hackborn41203752012-08-31 14:05:51 -07002002 void cancelAll(int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 synchronized (mNotificationList) {
2004 final int N = mNotificationList.size();
2005 for (int i=N-1; i>=0; i--) {
2006 NotificationRecord r = mNotificationList.get(i);
2007
Daniel Sandler321e9c52012-10-12 10:59:26 -07002008 if (!notificationMatchesUserId(r, userId)) {
Dianne Hackborn41203752012-08-31 14:05:51 -07002009 continue;
2010 }
2011
Daniel Sandlerfde19b12013-01-17 00:21:05 -05002012 if ((r.getFlags() & (Notification.FLAG_ONGOING_EVENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002013 | Notification.FLAG_NO_CLEAR)) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002014 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08002015 cancelNotificationLocked(r, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016 }
2017 }
2018
2019 updateLightsLocked();
2020 }
2021 }
2022
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 // lock on mNotificationList
2024 private void updateLightsLocked()
2025 {
The Android Open Source Project10592532009-03-18 17:39:46 -07002026 // handle notification lights
2027 if (mLedNotification == null) {
2028 // get next notification, if any
2029 int n = mLights.size();
2030 if (n > 0) {
2031 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002032 }
2033 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05002034
Mike Lockwood63b5ad92011-08-30 09:55:30 -04002035 // Don't flash while we are in a call or screen is on
2036 if (mLedNotification == null || mInCall || mScreenOn) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002037 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07002038 } else {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05002039 final Notification ledno = mLedNotification.sbn.notification;
2040 int ledARGB = ledno.ledARGB;
2041 int ledOnMS = ledno.ledOnMS;
2042 int ledOffMS = ledno.ledOffMS;
2043 if ((ledno.defaults & Notification.DEFAULT_LIGHTS) != 0) {
Mike Lockwood670f9322010-01-20 12:13:36 -05002044 ledARGB = mDefaultNotificationColor;
2045 ledOnMS = mDefaultNotificationLedOn;
2046 ledOffMS = mDefaultNotificationLedOff;
2047 }
2048 if (mNotificationPulseEnabled) {
2049 // pulse repeatedly
2050 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
2051 ledOnMS, ledOffMS);
Mike Lockwood670f9322010-01-20 12:13:36 -05002052 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002053 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 }
2055
2056 // lock on mNotificationList
Dianne Hackborn41203752012-08-31 14:05:51 -07002057 private int indexOfNotificationLocked(String pkg, String tag, int id, int userId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002058 {
2059 ArrayList<NotificationRecord> list = mNotificationList;
2060 final int len = list.size();
2061 for (int i=0; i<len; i++) {
2062 NotificationRecord r = list.get(i);
Daniel Sandlerfde19b12013-01-17 00:21:05 -05002063 if (!notificationMatchesUserId(r, userId) || r.sbn.id != id) {
Dianne Hackborn41203752012-08-31 14:05:51 -07002064 continue;
2065 }
Fred Quintana6ecaff12009-09-25 14:23:13 -07002066 if (tag == null) {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05002067 if (r.sbn.tag != null) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07002068 continue;
2069 }
2070 } else {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05002071 if (!tag.equals(r.sbn.tag)) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07002072 continue;
2073 }
2074 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05002075 if (r.sbn.pkg.equals(pkg)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 return i;
2077 }
2078 }
2079 return -1;
2080 }
2081
Mike Lockwoodc22404a2009-12-02 11:15:02 -05002082 private void updateNotificationPulse() {
2083 synchronized (mNotificationList) {
2084 updateLightsLocked();
2085 }
2086 }
2087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002088 // ======================================================================
2089 @Override
2090 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2091 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2092 != PackageManager.PERMISSION_GRANTED) {
2093 pw.println("Permission Denial: can't dump NotificationManager from from pid="
2094 + Binder.getCallingPid()
2095 + ", uid=" + Binder.getCallingUid());
2096 return;
2097 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002099 pw.println("Current Notification Manager state:");
2100
Daniel Sandler5feceeb2013-03-22 18:29:23 -07002101 pw.println(" Listeners (" + mEnabledListenersForCurrentUser.size()
2102 + ") enabled for current user:");
2103 for (ComponentName cmpt : mEnabledListenersForCurrentUser) {
2104 pw.println(" " + cmpt);
Daniel Sandler4b749ef2013-03-18 21:53:04 -04002105 }
Daniel Sandler4b749ef2013-03-18 21:53:04 -04002106
Daniel Sandler5feceeb2013-03-22 18:29:23 -07002107 pw.println(" Live listeners (" + mListeners.size() + "):");
Daniel Sandler4b749ef2013-03-18 21:53:04 -04002108 for (NotificationListenerInfo info : mListeners) {
Daniel Sandler5feceeb2013-03-22 18:29:23 -07002109 pw.println(" " + info.component
2110 + " (user " + info.userid + "): " + info.listener
2111 + (info.isSystem?" SYSTEM":""));
Daniel Sandler4b749ef2013-03-18 21:53:04 -04002112 }
2113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002114 int N;
2115
2116 synchronized (mToastQueue) {
2117 N = mToastQueue.size();
2118 if (N > 0) {
2119 pw.println(" Toast Queue:");
2120 for (int i=0; i<N; i++) {
2121 mToastQueue.get(i).dump(pw, " ");
2122 }
2123 pw.println(" ");
2124 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002126 }
2127
2128 synchronized (mNotificationList) {
2129 N = mNotificationList.size();
2130 if (N > 0) {
2131 pw.println(" Notification List:");
2132 for (int i=0; i<N; i++) {
2133 mNotificationList.get(i).dump(pw, " ", mContext);
2134 }
2135 pw.println(" ");
2136 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 N = mLights.size();
2139 if (N > 0) {
2140 pw.println(" Lights List:");
2141 for (int i=0; i<N; i++) {
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002142 pw.println(" " + mLights.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 }
2144 pw.println(" ");
2145 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002147 pw.println(" mSoundNotification=" + mSoundNotification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002148 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04002149 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
2150 pw.println(" mSystemReady=" + mSystemReady);
Daniel Sandler5e62e3a2013-04-15 20:57:02 -04002151 pw.println(" mArchive=" + mArchive.toString());
2152 Iterator<StatusBarNotification> iter = mArchive.descendingIterator();
2153 int i=0;
2154 while (iter.hasNext()) {
2155 pw.println(" " + iter.next());
2156 if (++i >= 5) {
2157 if (iter.hasNext()) pw.println(" ...");
2158 break;
2159 }
2160 }
2161
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002162 }
2163 }
2164}