blob: 9a93f637e0725a47eead00022f84dfbd7017d77b [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) {
240 // not there?
241 }
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) {
249 // not there?
250 }
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
288 nr.notification.tickerView = null;
289 nr.notification.contentView = null;
290 nr.notification.bigContentView = null;
291 nr.notification.largeIcon = null;
Daniel Sandler5e62e3a2013-04-15 20:57:02 -0400292 final Bundle extras = nr.notification.extras;
293 extras.remove(Notification.EXTRA_LARGE_ICON);
294 extras.remove(Notification.EXTRA_LARGE_ICON_BIG);
295 extras.remove(Notification.EXTRA_PICTURE);
Jeff Sharkey0c1baf92013-04-03 13:08:52 -0700296
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500297 if (mBuffer.size() == BUFFER_SIZE) {
298 mBuffer.removeFirst();
299 }
300 mBuffer.addLast(nr);
301 }
302
Daniel Sandler5e62e3a2013-04-15 20:57:02 -0400303
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500304 public void clear() {
305 mBuffer.clear();
306 }
307
308 public Iterator<StatusBarNotification> descendingIterator() {
309 return mBuffer.descendingIterator();
310 }
311 public Iterator<StatusBarNotification> ascendingIterator() {
312 return mBuffer.iterator();
313 }
314 public Iterator<StatusBarNotification> filter(
315 final Iterator<StatusBarNotification> iter, final String pkg, final int userId) {
316 return new Iterator<StatusBarNotification>() {
317 StatusBarNotification mNext = findNext();
318
319 private StatusBarNotification findNext() {
320 while (iter.hasNext()) {
321 StatusBarNotification nr = iter.next();
322 if ((pkg == null || nr.pkg == pkg)
323 && (userId == UserHandle.USER_ALL || nr.getUserId() == userId)) {
324 return nr;
325 }
326 }
327 return null;
328 }
329
330 @Override
331 public boolean hasNext() {
332 return mNext == null;
333 }
334
335 @Override
336 public StatusBarNotification next() {
337 StatusBarNotification next = mNext;
338 if (next == null) {
339 throw new NoSuchElementException();
340 }
341 mNext = findNext();
342 return next;
343 }
344
345 @Override
346 public void remove() {
347 iter.remove();
348 }
349 };
350 }
Daniel Sandler78d0d252013-02-12 08:14:52 -0500351
352 public StatusBarNotification[] getArray(int count) {
353 if (count == 0) count = Archive.BUFFER_SIZE;
354 final StatusBarNotification[] a
355 = new StatusBarNotification[Math.min(count, mBuffer.size())];
356 Iterator<StatusBarNotification> iter = descendingIterator();
357 int i=0;
358 while (iter.hasNext() && i < count) {
359 a[i++] = iter.next();
360 }
361 return a;
362 }
363
364 public StatusBarNotification[] getArray(int count, String pkg, int userId) {
365 if (count == 0) count = Archive.BUFFER_SIZE;
366 final StatusBarNotification[] a
367 = new StatusBarNotification[Math.min(count, mBuffer.size())];
368 Iterator<StatusBarNotification> iter = filter(descendingIterator(), pkg, userId);
369 int i=0;
370 while (iter.hasNext() && i < count) {
371 a[i++] = iter.next();
372 }
373 return a;
374 }
375
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500376 }
377
378 Archive mArchive = new Archive();
379
Daniel Sandler0da673f2012-04-11 12:33:16 -0400380 private void loadBlockDb() {
381 synchronized(mBlockedPackages) {
382 if (mPolicyFile == null) {
383 File dir = new File("/data/system");
384 mPolicyFile = new AtomicFile(new File(dir, "notification_policy.xml"));
385
386 mBlockedPackages.clear();
387
388 FileInputStream infile = null;
389 try {
390 infile = mPolicyFile.openRead();
391 final XmlPullParser parser = Xml.newPullParser();
392 parser.setInput(infile, null);
393
394 int type;
395 String tag;
396 int version = DB_VERSION;
397 while ((type = parser.next()) != END_DOCUMENT) {
398 tag = parser.getName();
399 if (type == START_TAG) {
400 if (TAG_BODY.equals(tag)) {
401 version = Integer.parseInt(parser.getAttributeValue(null, ATTR_VERSION));
402 } else if (TAG_BLOCKED_PKGS.equals(tag)) {
403 while ((type = parser.next()) != END_DOCUMENT) {
404 tag = parser.getName();
405 if (TAG_PACKAGE.equals(tag)) {
406 mBlockedPackages.add(parser.getAttributeValue(null, ATTR_NAME));
407 } else if (TAG_BLOCKED_PKGS.equals(tag) && type == END_TAG) {
408 break;
409 }
410 }
411 }
412 }
413 }
414 } catch (FileNotFoundException e) {
415 // No data yet
416 } catch (IOException e) {
417 Log.wtf(TAG, "Unable to read blocked notifications database", e);
418 } catch (NumberFormatException e) {
419 Log.wtf(TAG, "Unable to parse blocked notifications database", e);
420 } catch (XmlPullParserException e) {
421 Log.wtf(TAG, "Unable to parse blocked notifications database", e);
422 } finally {
423 IoUtils.closeQuietly(infile);
424 }
425 }
426 }
427 }
428
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500429 /**
430 * Use this when you just want to know if notifications are OK for this package.
431 */
432 public boolean areNotificationsEnabledForPackage(String pkg, int uid) {
Daniel Sandler0da673f2012-04-11 12:33:16 -0400433 checkCallerIsSystem();
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500434 return (mAppOps.checkOpNoThrow(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg)
435 == AppOpsManager.MODE_ALLOWED);
Daniel Sandler0da673f2012-04-11 12:33:16 -0400436 }
437
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500438 /** Use this when you actually want to post a notification or toast.
439 *
440 * Unchecked. Not exposed via Binder, but can be called in the course of enqueue*().
441 */
442 private boolean noteNotificationOp(String pkg, int uid) {
443 if (mAppOps.noteOpNoThrow(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg)
444 != AppOpsManager.MODE_ALLOWED) {
445 Slog.v(TAG, "notifications are disabled by AppOps for " + pkg);
446 return false;
Daniel Sandler0da673f2012-04-11 12:33:16 -0400447 }
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500448 return true;
Daniel Sandler0da673f2012-04-11 12:33:16 -0400449 }
450
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500451 public void setNotificationsEnabledForPackage(String pkg, int uid, boolean enabled) {
Daniel Sandler0da673f2012-04-11 12:33:16 -0400452 checkCallerIsSystem();
Daniel Sandler32e698b2013-04-18 10:51:35 -0400453
454 Slog.v(TAG, (enabled?"en":"dis") + "abling notifications for " + pkg);
455
Daniel Sandler4a900acd2013-01-30 14:04:10 -0500456 mAppOps.setMode(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg,
457 enabled ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_IGNORED);
Daniel Sandler32e698b2013-04-18 10:51:35 -0400458
459 // Now, cancel any outstanding notifications that are part of a just-disabled app
460 if (ENABLE_BLOCKED_NOTIFICATIONS && !enabled) {
461 cancelAllNotificationsInt(pkg, 0, 0, true, UserHandle.getUserId(uid));
462 }
Daniel Sandler0da673f2012-04-11 12:33:16 -0400463 }
464
465
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 private static String idDebugString(Context baseContext, String packageName, int id) {
467 Context c = null;
468
469 if (packageName != null) {
470 try {
471 c = baseContext.createPackageContext(packageName, 0);
472 } catch (NameNotFoundException e) {
473 c = baseContext;
474 }
475 } else {
476 c = baseContext;
477 }
478
479 String pkg;
480 String type;
481 String name;
482
483 Resources r = c.getResources();
484 try {
485 return r.getResourceName(id);
486 } catch (Resources.NotFoundException e) {
487 return "<name unknown>";
488 }
489 }
490
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700491 /**
492 * System-only API for getting a list of current (i.e. not cleared) notifications.
493 *
494 * Requires ACCESS_NOTIFICATIONS which is signature|system.
495 */
496 @Override
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500497 public StatusBarNotification[] getActiveNotifications(String callingPkg) {
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400498 // enforce() will ensure the calling uid has the correct permission
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500499 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NOTIFICATIONS,
Daniel Sandler78d0d252013-02-12 08:14:52 -0500500 "NotificationManagerService.getActiveNotifications");
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500501
502 StatusBarNotification[] tmp = null;
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500503 int uid = Binder.getCallingUid();
504
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400505 // noteOp will check to make sure the callingPkg matches the uid
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500506 if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg)
507 == AppOpsManager.MODE_ALLOWED) {
508 synchronized (mNotificationList) {
509 tmp = new StatusBarNotification[mNotificationList.size()];
510 final int N = mNotificationList.size();
511 for (int i=0; i<N; i++) {
512 tmp[i] = mNotificationList.get(i).sbn;
513 }
514 }
515 }
516 return tmp;
517 }
518
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700519 /**
520 * System-only API for getting a list of recent (cleared, no longer shown) notifications.
521 *
522 * Requires ACCESS_NOTIFICATIONS which is signature|system.
523 */
524 @Override
Daniel Sandler78d0d252013-02-12 08:14:52 -0500525 public StatusBarNotification[] getHistoricalNotifications(String callingPkg, int count) {
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400526 // enforce() will ensure the calling uid has the correct permission
Daniel Sandler78d0d252013-02-12 08:14:52 -0500527 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NOTIFICATIONS,
528 "NotificationManagerService.getHistoricalNotifications");
529
530 StatusBarNotification[] tmp = null;
531 int uid = Binder.getCallingUid();
532
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400533 // noteOp will check to make sure the callingPkg matches the uid
Daniel Sandler78d0d252013-02-12 08:14:52 -0500534 if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg)
535 == AppOpsManager.MODE_ALLOWED) {
536 synchronized (mArchive) {
537 tmp = mArchive.getArray(count);
538 }
539 }
540 return tmp;
541 }
542
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700543 /**
544 * Called whenever packages change, the user switches, or ENABLED_NOTIFICATION_LISTENERS
545 * is altered. (For example in response to USER_SWITCHED in our broadcast receiver)
546 */
547 void rebindListenerServices() {
548 String flat = Settings.Secure.getString(
549 mContext.getContentResolver(),
550 Settings.Secure.ENABLED_NOTIFICATION_LISTENERS);
551
552 NotificationListenerInfo[] toRemove = new NotificationListenerInfo[mListeners.size()];
553 final ArrayList<ComponentName> toAdd;
554 final int currentUser = ActivityManager.getCurrentUser();
555
556 synchronized (mNotificationList) {
557 // unbind and remove all existing listeners
558 toRemove = mListeners.toArray(toRemove);
559
560 toAdd = new ArrayList<ComponentName>();
561 final HashSet<ComponentName> newEnabled = new HashSet<ComponentName>();
562 final HashSet<String> newPackages = new HashSet<String>();
563
564 // decode the list of components
565 if (flat != null) {
566 String[] components = flat.split(ENABLED_NOTIFICATION_LISTENERS_SEPARATOR);
567 for (int i=0; i<components.length; i++) {
568 final ComponentName component
569 = ComponentName.unflattenFromString(components[i]);
570 if (component != null) {
571 newEnabled.add(component);
572 toAdd.add(component);
573 newPackages.add(component.getPackageName());
574 }
575 }
576
577 mEnabledListenersForCurrentUser = newEnabled;
578 mEnabledListenerPackageNames = newPackages;
579 }
580 }
581
582 for (NotificationListenerInfo info : toRemove) {
583 final ComponentName component = info.component;
584 final int oldUser = info.userid;
585 Slog.v(TAG, "disabling notification listener for user " + oldUser + ": " + component);
586 unregisterListenerService(component, info.userid);
587 }
588
589 final int N = toAdd.size();
590 for (int i=0; i<N; i++) {
591 final ComponentName component = toAdd.get(i);
592 Slog.v(TAG, "enabling notification listener for user " + currentUser + ": "
593 + component);
594 registerListenerService(component, currentUser);
595 }
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400596 }
597
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700598 /**
599 * Register a listener binder directly with the notification manager.
600 *
601 * Only works with system callers. Apps should extend
602 * {@link android.service.notification.NotificationListenerService}.
603 */
Daniel Sandler09a247e2013-02-14 10:24:17 -0500604 @Override
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400605 public void registerListener(final INotificationListener listener,
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700606 final ComponentName component, final int userid) {
607 checkCallerIsSystem();
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400608
Daniel Sandler09a247e2013-02-14 10:24:17 -0500609 synchronized (mNotificationList) {
610 try {
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400611 NotificationListenerInfo info
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700612 = new NotificationListenerInfo(listener, component, userid, true);
Daniel Sandler09a247e2013-02-14 10:24:17 -0500613 listener.asBinder().linkToDeath(info, 0);
614 mListeners.add(info);
615 } catch (RemoteException e) {
616 // already dead
617 }
618 }
619 }
620
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700621 /**
622 * Version of registerListener that takes the name of a
623 * {@link android.service.notification.NotificationListenerService} to bind to.
624 *
625 * This is the mechanism by which third parties may subscribe to notifications.
626 */
627 private void registerListenerService(final ComponentName name, final int userid) {
628 checkCallerIsSystem();
629
630 if (DBG) Slog.v(TAG, "registerListenerService: " + name + " u=" + userid);
631
632 synchronized (mNotificationList) {
633 final String servicesBindingTag = name.toString() + "/" + userid;
634 if (mServicesBinding.contains(servicesBindingTag)) {
635 // stop registering this thing already! we're working on it
636 return;
637 }
638 mServicesBinding.add(servicesBindingTag);
639
640 final int N = mListeners.size();
641 for (int i=N-1; i>=0; i--) {
642 final NotificationListenerInfo info = mListeners.get(i);
643 if (name.equals(info.component)
644 && info.userid == userid) {
645 // cut old connections
646 if (DBG) Slog.v(TAG, " disconnecting old listener: " + info.listener);
647 mListeners.remove(i);
648 if (info.connection != null) {
649 mContext.unbindService(info.connection);
650 }
651 }
652 }
653
654 Intent intent = new Intent(NotificationListenerService.SERVICE_INTERFACE);
655 intent.setComponent(name);
656
657 intent.putExtra(Intent.EXTRA_CLIENT_LABEL,
658 com.android.internal.R.string.notification_listener_binding_label);
659 intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
660 mContext, 0, new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS), 0));
661
662 try {
663 if (DBG) Slog.v(TAG, "binding: " + intent);
664 if (!mContext.bindServiceAsUser(intent,
665 new ServiceConnection() {
666 INotificationListener mListener;
667 @Override
668 public void onServiceConnected(ComponentName name, IBinder service) {
669 synchronized (mNotificationList) {
670 mServicesBinding.remove(servicesBindingTag);
671 try {
672 mListener = INotificationListener.Stub.asInterface(service);
673 NotificationListenerInfo info = new NotificationListenerInfo(
674 mListener, name, userid, this);
675 service.linkToDeath(info, 0);
676 mListeners.add(info);
677 } catch (RemoteException e) {
678 // already dead
679 }
680 }
681 }
682
683 @Override
684 public void onServiceDisconnected(ComponentName name) {
685 Slog.v(TAG, "notification listener connection lost: " + name);
686 }
687 },
688 Context.BIND_AUTO_CREATE,
689 new UserHandle(userid)))
690 {
691 mServicesBinding.remove(servicesBindingTag);
692 Slog.w(TAG, "Unable to bind listener service: " + intent);
693 return;
694 }
695 } catch (SecurityException ex) {
696 Slog.e(TAG, "Unable to bind listener service: " + intent, ex);
697 return;
698 }
699 }
700 }
701
702 /**
703 * Remove a listener binder directly
704 */
Daniel Sandler09a247e2013-02-14 10:24:17 -0500705 @Override
706 public void unregisterListener(INotificationListener listener, int userid) {
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400707 // no need to check permissions; if your listener binder is in the list,
708 // that's proof that you had permission to add it in the first place
709
Daniel Sandler09a247e2013-02-14 10:24:17 -0500710 synchronized (mNotificationList) {
711 final int N = mListeners.size();
712 for (int i=N-1; i>=0; i--) {
713 final NotificationListenerInfo info = mListeners.get(i);
714 if (info.listener == listener && info.userid == userid) {
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700715 mListeners.remove(i);
716 if (info.connection != null) {
717 mContext.unbindService(info.connection);
718 }
Daniel Sandler09a247e2013-02-14 10:24:17 -0500719 }
720 }
721 }
722 }
723
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700724 /**
725 * Remove a listener service for the given user by ComponentName
726 */
727 private void unregisterListenerService(ComponentName name, int userid) {
728 checkCallerIsSystem();
729
730 synchronized (mNotificationList) {
731 final int N = mListeners.size();
732 for (int i=N-1; i>=0; i--) {
733 final NotificationListenerInfo info = mListeners.get(i);
734 if (name.equals(info.component)
735 && info.userid == userid) {
736 mListeners.remove(i);
737 if (info.connection != null) {
738 mContext.unbindService(info.connection);
739 }
740 }
741 }
742 }
743 }
744
745 /**
746 * asynchronously notify all listeners about a new notification
747 */
Daniel Sandler09a247e2013-02-14 10:24:17 -0500748 private void notifyPostedLocked(NotificationRecord n) {
749 final StatusBarNotification sbn = n.sbn;
750 for (final NotificationListenerInfo info : mListeners) {
751 mHandler.post(new Runnable() {
752 @Override
753 public void run() {
754 info.notifyPostedIfUserMatch(sbn);
755 }});
756 }
757 }
758
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700759 /**
760 * asynchronously notify all listeners about a removed notification
761 */
Daniel Sandler09a247e2013-02-14 10:24:17 -0500762 private void notifyRemovedLocked(NotificationRecord n) {
763 final StatusBarNotification sbn = n.sbn;
764 for (final NotificationListenerInfo info : mListeners) {
765 mHandler.post(new Runnable() {
766 @Override
767 public void run() {
768 info.notifyRemovedIfUserMatch(sbn);
769 }});
770 }
771 }
772
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700773 // -- APIs to support listeners clicking/clearing notifications --
774
775 private NotificationListenerInfo checkListenerToken(INotificationListener listener) {
776 final IBinder token = listener.asBinder();
777 final int N = mListeners.size();
778 for (int i=0; i<N; i++) {
779 final NotificationListenerInfo info = mListeners.get(i);
780 if (info.listener.asBinder() == token) return info;
781 }
782 throw new SecurityException("Disallowed call from unknown listener: " + listener);
783 }
784
785 /**
786 * Allow an INotificationListener to simulate a "clear all" operation.
787 *
788 * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onClearAllNotifications}
789 *
790 * @param token The binder for the listener, to check that the caller is allowed
791 */
792 public void clearAllNotificationsFromListener(INotificationListener token) {
793 NotificationListenerInfo info = checkListenerToken(token);
794 long identity = Binder.clearCallingIdentity();
795 try {
796 cancelAll(info.userid);
797 } finally {
798 Binder.restoreCallingIdentity(identity);
799 }
800 }
801
802 /**
803 * Allow an INotificationListener to simulate clearing (dismissing) a single notification.
804 *
805 * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onNotificationClear}
806 *
807 * @param token The binder for the listener, to check that the caller is allowed
808 */
809 public void clearNotificationFromListener(INotificationListener token, String pkg, String tag, int id) {
810 NotificationListenerInfo info = checkListenerToken(token);
811 long identity = Binder.clearCallingIdentity();
812 try {
813 cancelNotification(pkg, tag, id, 0,
814 Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE,
815 true,
816 info.userid);
817 } finally {
818 Binder.restoreCallingIdentity(identity);
819 }
820 }
821
822 // -- end of listener APIs --
823
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500824 public static final class NotificationRecord
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825 {
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500826 final StatusBarNotification sbn;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800827 IBinder statusBarKey;
828
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500829 NotificationRecord(StatusBarNotification sbn)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 {
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500831 this.sbn = sbn;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 }
Fred Quintana6ecaff12009-09-25 14:23:13 -0700833
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500834 public Notification getNotification() { return sbn.notification; }
835 public int getFlags() { return sbn.notification.flags; }
836 public int getUserId() { return sbn.getUserId(); }
837
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800838 void dump(PrintWriter pw, String prefix, Context baseContext) {
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500839 final Notification notification = sbn.notification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 pw.println(prefix + this);
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400841 pw.println(prefix + " uid=" + sbn.uid + " userId=" + sbn.getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800842 pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400843 + " / " + idDebugString(baseContext, sbn.pkg, notification.icon));
844 pw.println(prefix + " pri=" + notification.priority + " score=" + sbn.score);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 pw.println(prefix + " contentIntent=" + notification.contentIntent);
846 pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
847 pw.println(prefix + " tickerText=" + notification.tickerText);
848 pw.println(prefix + " contentView=" + notification.contentView);
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400849 pw.println(prefix + String.format(" defaults=0x%08x flags=0x%08x",
850 notification.defaults, notification.flags));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 pw.println(prefix + " sound=" + notification.sound);
852 pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate));
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400853 pw.println(prefix + String.format(" led=0x%08x onMs=%d offMs=%d",
854 notification.ledARGB, notification.ledOnMS, notification.ledOffMS));
855 if (notification.actions != null && notification.actions.length > 0) {
856 pw.println(prefix + " actions={");
857 final int N = notification.actions.length;
858 for (int i=0; i<N; i++) {
859 final Notification.Action action = notification.actions[i];
860 pw.println(String.format("%s [%d] \"%s\" -> %s",
861 prefix,
862 i,
863 action.title,
864 action.actionIntent.toString()
865 ));
866 }
867 pw.println(prefix + " }");
868 }
869 if (notification.extras != null && notification.extras.size() > 0) {
870 pw.println(prefix + " extras={");
871 for (String key : notification.extras.keySet()) {
872 pw.print(prefix + " " + key + "=");
873 Object val = notification.extras.get(key);
874 if (val == null) {
875 pw.println("null");
876 } else {
877 pw.print(val.toString());
878 if (val instanceof Bitmap) {
879 pw.print(String.format(" (%dx%d)",
880 ((Bitmap) val).getWidth(),
881 ((Bitmap) val).getHeight()));
882 } else if (val.getClass().isArray()) {
883 pw.println(" {");
884 final int N = Array.getLength(val);
885 for (int i=0; i<N; i++) {
886 if (i > 0) pw.println(",");
887 pw.print(prefix + " " + Array.get(val, i));
888 }
889 pw.print("\n" + prefix + " }");
890 }
891 pw.println();
892 }
893 }
894 pw.println(prefix + " }");
895 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800898 @Override
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500899 public final String toString() {
900 return String.format(
901 "NotificationRecord(0x%08x: pkg=%s user=%s id=%d tag=%s score=%d: %s)",
902 System.identityHashCode(this),
903 this.sbn.pkg, this.sbn.user, this.sbn.id, this.sbn.tag,
904 this.sbn.score, this.sbn.notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 }
906 }
907
908 private static final class ToastRecord
909 {
910 final int pid;
911 final String pkg;
912 final ITransientNotification callback;
913 int duration;
914
915 ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
916 {
917 this.pid = pid;
918 this.pkg = pkg;
919 this.callback = callback;
920 this.duration = duration;
921 }
922
923 void update(int duration) {
924 this.duration = duration;
925 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 void dump(PrintWriter pw, String prefix) {
928 pw.println(prefix + this);
929 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800930
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800931 @Override
932 public final String toString()
933 {
934 return "ToastRecord{"
935 + Integer.toHexString(System.identityHashCode(this))
936 + " pkg=" + pkg
937 + " callback=" + callback
938 + " duration=" + duration;
939 }
940 }
941
Joe Onorato089de882010-04-12 08:18:45 -0700942 private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks
943 = new StatusBarManagerService.NotificationCallbacks() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800944
945 public void onSetDisabled(int status) {
946 synchronized (mNotificationList) {
947 mDisabledNotifications = status;
948 if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) {
949 // cancel whatever's going on
950 long identity = Binder.clearCallingIdentity();
951 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -0700952 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
953 if (player != null) {
954 player.stopAsync();
955 }
956 } catch (RemoteException e) {
957 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800958 Binder.restoreCallingIdentity(identity);
959 }
960
961 identity = Binder.clearCallingIdentity();
962 try {
963 mVibrator.cancel();
Jeff Sharkey098d5802012-04-26 17:30:34 -0700964 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800965 Binder.restoreCallingIdentity(identity);
966 }
967 }
968 }
969 }
970
971 public void onClearAll() {
Dianne Hackborn41203752012-08-31 14:05:51 -0700972 // XXX to be totally correct, the caller should tell us which user
973 // this is for.
974 cancelAll(ActivityManager.getCurrentUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 }
976
Fred Quintana6ecaff12009-09-25 14:23:13 -0700977 public void onNotificationClick(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.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700980 cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL,
Dianne Hackborn41203752012-08-31 14:05:51 -0700981 Notification.FLAG_FOREGROUND_SERVICE, false,
982 ActivityManager.getCurrentUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 }
984
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400985 public void onNotificationClear(String pkg, String tag, int id) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700986 // XXX to be totally correct, the caller should tell us which user
987 // this is for.
Joe Onorato46439ce2010-11-19 13:56:21 -0800988 cancelNotification(pkg, tag, id, 0,
989 Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE,
Dianne Hackborn41203752012-08-31 14:05:51 -0700990 true, ActivityManager.getCurrentUser());
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400991 }
992
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800993 public void onPanelRevealed() {
994 synchronized (mNotificationList) {
995 // sound
996 mSoundNotification = null;
Jeff Sharkey098d5802012-04-26 17:30:34 -0700997
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 long identity = Binder.clearCallingIdentity();
999 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001000 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
1001 if (player != null) {
1002 player.stopAsync();
1003 }
1004 } catch (RemoteException e) {
1005 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001006 Binder.restoreCallingIdentity(identity);
1007 }
1008
1009 // vibrate
1010 mVibrateNotification = null;
1011 identity = Binder.clearCallingIdentity();
1012 try {
1013 mVibrator.cancel();
Jeff Sharkey098d5802012-04-26 17:30:34 -07001014 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001015 Binder.restoreCallingIdentity(identity);
1016 }
1017
1018 // light
1019 mLights.clear();
1020 mLedNotification = null;
1021 updateLightsLocked();
1022 }
1023 }
Joe Onorato005847b2010-06-04 16:08:02 -04001024
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001025 public void onNotificationError(String pkg, String tag, int id,
1026 int uid, int initialPid, String message) {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -04001027 Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id
1028 + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")");
Dianne Hackborn41203752012-08-31 14:05:51 -07001029 // XXX to be totally correct, the caller should tell us which user
1030 // this is for.
1031 cancelNotification(pkg, tag, id, 0, 0, false, UserHandle.getUserId(uid));
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001032 long ident = Binder.clearCallingIdentity();
1033 try {
1034 ActivityManagerNative.getDefault().crashApplication(uid, initialPid, pkg,
1035 "Bad notification posted from package " + pkg
1036 + ": " + message);
1037 } catch (RemoteException e) {
1038 }
1039 Binder.restoreCallingIdentity(ident);
Joe Onorato005847b2010-06-04 16:08:02 -04001040 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 };
1042
1043 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
1044 @Override
1045 public void onReceive(Context context, Intent intent) {
1046 String action = intent.getAction();
1047
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001048 boolean queryRestart = false;
Daniel Sandler26ece572012-06-01 15:38:46 -04001049 boolean packageChanged = false;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001050
Mike Lockwood541c9942011-06-12 19:35:45 -04001051 if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001052 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Daniel Sandler26ece572012-06-01 15:38:46 -04001053 || (packageChanged=action.equals(Intent.ACTION_PACKAGE_CHANGED))
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001054 || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001055 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001056 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001057 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001058 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001059 } else if (queryRestart) {
1060 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001061 } else {
1062 Uri uri = intent.getData();
1063 if (uri == null) {
1064 return;
1065 }
1066 String pkgName = uri.getSchemeSpecificPart();
1067 if (pkgName == null) {
1068 return;
1069 }
Daniel Sandler26ece572012-06-01 15:38:46 -04001070 if (packageChanged) {
1071 // We cancel notifications for packages which have just been disabled
1072 final int enabled = mContext.getPackageManager()
1073 .getApplicationEnabledSetting(pkgName);
1074 if (enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED
1075 || enabled == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
1076 return;
1077 }
1078 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001079 pkgList = new String[]{pkgName};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 }
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001081
1082 boolean anyListenersInvolved = false;
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001083 if (pkgList != null && (pkgList.length > 0)) {
1084 for (String pkgName : pkgList) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001085 cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart,
1086 UserHandle.USER_ALL);
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001087 if (mEnabledListenerPackageNames.contains(pkgName)) {
1088 anyListenersInvolved = true;
1089 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001090 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 }
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001092
1093 if (anyListenersInvolved) {
1094 // make sure we're still bound to any of our
1095 // listeners who may have just upgraded
1096 rebindListenerServices();
1097 }
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001098 } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
1099 // Keep track of screen on/off state, but do not turn off the notification light
1100 // until user passes through the lock screen or views the notification.
1101 mScreenOn = true;
1102 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
1103 mScreenOn = false;
Daniel Sandlere96ffb12010-03-11 13:38:06 -05001104 } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001105 mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
1106 TelephonyManager.EXTRA_STATE_OFFHOOK));
Daniel Sandlere96ffb12010-03-11 13:38:06 -05001107 updateNotificationPulse();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001108 } else if (action.equals(Intent.ACTION_USER_STOPPED)) {
1109 int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
1110 if (userHandle >= 0) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001111 cancelAllNotificationsInt(null, 0, 0, true, userHandle);
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001112 }
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001113 } else if (action.equals(Intent.ACTION_USER_PRESENT)) {
1114 // turn off LED when user passes through lock screen
1115 mNotificationLight.turnOff();
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001116 } else if (action.equals(Intent.ACTION_USER_SWITCHED)) {
1117 // reload per-user settings
1118 mSettingsObserver.update(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 }
1120 }
1121 };
1122
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001123 class SettingsObserver extends ContentObserver {
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001124 private final Uri NOTIFICATION_LIGHT_PULSE_URI
1125 = Settings.System.getUriFor(Settings.System.NOTIFICATION_LIGHT_PULSE);
1126
1127 private final Uri ENABLED_NOTIFICATION_LISTENERS_URI
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001128 = Settings.Secure.getUriFor(Settings.Secure.ENABLED_NOTIFICATION_LISTENERS);
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001129
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001130 SettingsObserver(Handler handler) {
1131 super(handler);
1132 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001133
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001134 void observe() {
1135 ContentResolver resolver = mContext.getContentResolver();
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001136 resolver.registerContentObserver(NOTIFICATION_LIGHT_PULSE_URI,
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001137 false, this, UserHandle.USER_ALL);
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001138 resolver.registerContentObserver(ENABLED_NOTIFICATION_LISTENERS_URI,
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001139 false, this, UserHandle.USER_ALL);
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001140 update(null);
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001141 }
1142
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001143 @Override public void onChange(boolean selfChange, Uri uri) {
1144 update(uri);
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001145 }
1146
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001147 public void update(Uri uri) {
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001148 ContentResolver resolver = mContext.getContentResolver();
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001149 if (uri == null || NOTIFICATION_LIGHT_PULSE_URI.equals(uri)) {
1150 boolean pulseEnabled = Settings.System.getInt(resolver,
1151 Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0;
1152 if (mNotificationPulseEnabled != pulseEnabled) {
1153 mNotificationPulseEnabled = pulseEnabled;
1154 updateNotificationPulse();
1155 }
1156 }
1157 if (uri == null || ENABLED_NOTIFICATION_LISTENERS_URI.equals(uri)) {
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001158 rebindListenerServices();
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001159 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001160 }
1161 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001162
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001163 private SettingsObserver mSettingsObserver;
1164
Daniel Sandleredbb3802012-11-13 20:49:47 -08001165 static long[] getLongArray(Resources r, int resid, int maxlen, long[] def) {
1166 int[] ar = r.getIntArray(resid);
1167 if (ar == null) {
1168 return def;
1169 }
1170 final int len = ar.length > maxlen ? maxlen : ar.length;
1171 long[] out = new long[len];
1172 for (int i=0; i<len; i++) {
1173 out[i] = ar[i];
1174 }
1175 return out;
1176 }
1177
Joe Onorato089de882010-04-12 08:18:45 -07001178 NotificationManagerService(Context context, StatusBarManagerService statusBar,
Mike Lockwood3a322132009-11-24 00:30:52 -05001179 LightsService lights)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 {
1181 super();
1182 mContext = context;
Jeff Brownc2346132012-04-13 01:55:38 -07001183 mVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001184 mAm = ActivityManagerNative.getDefault();
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001185 mUserManager = (UserManager)context.getSystemService(Context.USER_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 mToastQueue = new ArrayList<ToastRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 mHandler = new WorkerHandler();
San Mehat3ee13172010-02-04 20:54:43 -08001188
Daniel Sandler4a900acd2013-01-30 14:04:10 -05001189 mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
1190
1191 importOldBlockDb();
Daniel Sandler0da673f2012-04-11 12:33:16 -04001192
Joe Onorato089de882010-04-12 08:18:45 -07001193 mStatusBar = statusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 statusBar.setNotificationCallbacks(mNotificationCallbacks);
1195
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001196 mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS);
1197 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
1198
Mike Lockwood670f9322010-01-20 12:13:36 -05001199 Resources resources = mContext.getResources();
1200 mDefaultNotificationColor = resources.getColor(
1201 com.android.internal.R.color.config_defaultNotificationColor);
1202 mDefaultNotificationLedOn = resources.getInteger(
1203 com.android.internal.R.integer.config_defaultNotificationLedOn);
1204 mDefaultNotificationLedOff = resources.getInteger(
1205 com.android.internal.R.integer.config_defaultNotificationLedOff);
1206
Daniel Sandleredbb3802012-11-13 20:49:47 -08001207 mDefaultVibrationPattern = getLongArray(resources,
1208 com.android.internal.R.array.config_defaultNotificationVibePattern,
1209 VIBRATE_PATTERN_MAXLEN,
1210 DEFAULT_VIBRATE_PATTERN);
1211
1212 mFallbackVibrationPattern = getLongArray(resources,
1213 com.android.internal.R.array.config_notificationFallbackVibePattern,
1214 VIBRATE_PATTERN_MAXLEN,
1215 DEFAULT_VIBRATE_PATTERN);
1216
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001217 // Don't start allowing notifications until the setup wizard has run once.
1218 // After that, including subsequent boots, init with notifications turned on.
1219 // This works on the first boot because the setup wizard will toggle this
1220 // flag at least once and we'll go back to 0 after that.
Jeff Brownbf6f6f92012-09-25 15:03:20 -07001221 if (0 == Settings.Global.getInt(mContext.getContentResolver(),
1222 Settings.Global.DEVICE_PROVISIONED, 0)) {
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001223 mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
1224 }
1225
Mike Lockwood35e16bf2010-11-30 19:53:36 -05001226 // register for various Intents
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 IntentFilter filter = new IntentFilter();
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001228 filter.addAction(Intent.ACTION_SCREEN_ON);
1229 filter.addAction(Intent.ACTION_SCREEN_OFF);
Daniel Sandlere96ffb12010-03-11 13:38:06 -05001230 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001231 filter.addAction(Intent.ACTION_USER_PRESENT);
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001232 filter.addAction(Intent.ACTION_USER_STOPPED);
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001233 filter.addAction(Intent.ACTION_USER_SWITCHED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001235 IntentFilter pkgFilter = new IntentFilter();
1236 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
Daniel Sandleraac0eb02011-08-06 22:51:56 -04001237 pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001238 pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
1239 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
1240 pkgFilter.addDataScheme("package");
1241 mContext.registerReceiver(mIntentReceiver, pkgFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001242 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001243 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001244
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001245 mSettingsObserver = new SettingsObserver(mHandler);
1246 mSettingsObserver.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 }
1248
Daniel Sandler4a900acd2013-01-30 14:04:10 -05001249 /**
1250 * Read the old XML-based app block database and import those blockages into the AppOps system.
1251 */
1252 private void importOldBlockDb() {
1253 loadBlockDb();
1254
1255 PackageManager pm = mContext.getPackageManager();
1256 for (String pkg : mBlockedPackages) {
1257 PackageInfo info = null;
1258 try {
1259 info = pm.getPackageInfo(pkg, 0);
1260 setNotificationsEnabledForPackage(pkg, info.applicationInfo.uid, false);
1261 } catch (NameNotFoundException e) {
1262 // forget you
1263 }
1264 }
1265 mBlockedPackages.clear();
1266 if (mPolicyFile != null) {
1267 mPolicyFile.delete();
1268 }
1269 }
1270
Joe Onorato30275482009-07-08 17:09:14 -07001271 void systemReady() {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001272 mAudioService = IAudioService.Stub.asInterface(
1273 ServiceManager.getService(Context.AUDIO_SERVICE));
1274
Joe Onorato30275482009-07-08 17:09:14 -07001275 // no beeping until we're basically done booting
1276 mSystemReady = true;
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001277
1278 // make sure our listener services are properly bound
1279 rebindListenerServices();
Joe Onorato30275482009-07-08 17:09:14 -07001280 }
1281
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 // Toasts
1283 // ============================================================================
1284 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
1285 {
Daniel Sandlera7035902010-03-30 15:45:31 -04001286 if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287
1288 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001289 Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 return ;
1291 }
1292
Daniel Sandler0da673f2012-04-11 12:33:16 -04001293 final boolean isSystemToast = ("android".equals(pkg));
1294
Daniel Sandler4a900acd2013-01-30 14:04:10 -05001295 if (ENABLE_BLOCKED_TOASTS && !noteNotificationOp(pkg, Binder.getCallingUid())) {
1296 if (!isSystemToast) {
1297 Slog.e(TAG, "Suppressing toast from package " + pkg + " by user request.");
1298 return;
1299 }
Daniel Sandler0da673f2012-04-11 12:33:16 -04001300 }
1301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 synchronized (mToastQueue) {
1303 int callingPid = Binder.getCallingPid();
1304 long callingId = Binder.clearCallingIdentity();
1305 try {
1306 ToastRecord record;
1307 int index = indexOfToastLocked(pkg, callback);
1308 // If it's already in the queue, we update it in place, we don't
1309 // move it to the end of the queue.
1310 if (index >= 0) {
1311 record = mToastQueue.get(index);
1312 record.update(duration);
1313 } else {
Vairavan Srinivasanf9eb06c2011-01-21 18:08:36 -08001314 // Limit the number of toasts that any given package except the android
1315 // package can enqueue. Prevents DOS attacks and deals with leaks.
Daniel Sandler0da673f2012-04-11 12:33:16 -04001316 if (!isSystemToast) {
Vairavan Srinivasanf9eb06c2011-01-21 18:08:36 -08001317 int count = 0;
1318 final int N = mToastQueue.size();
1319 for (int i=0; i<N; i++) {
1320 final ToastRecord r = mToastQueue.get(i);
1321 if (r.pkg.equals(pkg)) {
1322 count++;
1323 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
1324 Slog.e(TAG, "Package has already posted " + count
1325 + " toasts. Not showing more. Package=" + pkg);
1326 return;
1327 }
1328 }
1329 }
1330 }
1331
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001332 record = new ToastRecord(callingPid, pkg, callback, duration);
1333 mToastQueue.add(record);
1334 index = mToastQueue.size() - 1;
1335 keepProcessAliveLocked(callingPid);
1336 }
1337 // If it's at index 0, it's the current toast. It doesn't matter if it's
1338 // new or just been updated. Call back and tell it to show itself.
1339 // If the callback fails, this will remove it from the list, so don't
1340 // assume that it's valid after this.
1341 if (index == 0) {
1342 showNextToastLocked();
1343 }
1344 } finally {
1345 Binder.restoreCallingIdentity(callingId);
1346 }
1347 }
1348 }
1349
1350 public void cancelToast(String pkg, ITransientNotification callback) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001351 Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352
1353 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001354 Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001355 return ;
1356 }
1357
1358 synchronized (mToastQueue) {
1359 long callingId = Binder.clearCallingIdentity();
1360 try {
1361 int index = indexOfToastLocked(pkg, callback);
1362 if (index >= 0) {
1363 cancelToastLocked(index);
1364 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001365 Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001366 }
1367 } finally {
1368 Binder.restoreCallingIdentity(callingId);
1369 }
1370 }
1371 }
1372
1373 private void showNextToastLocked() {
1374 ToastRecord record = mToastQueue.get(0);
1375 while (record != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001376 if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001377 try {
1378 record.callback.show();
1379 scheduleTimeoutLocked(record, false);
1380 return;
1381 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001382 Slog.w(TAG, "Object died trying to show notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 + " in package " + record.pkg);
1384 // remove it from the list and let the process die
1385 int index = mToastQueue.indexOf(record);
1386 if (index >= 0) {
1387 mToastQueue.remove(index);
1388 }
1389 keepProcessAliveLocked(record.pid);
1390 if (mToastQueue.size() > 0) {
1391 record = mToastQueue.get(0);
1392 } else {
1393 record = null;
1394 }
1395 }
1396 }
1397 }
1398
1399 private void cancelToastLocked(int index) {
1400 ToastRecord record = mToastQueue.get(index);
1401 try {
1402 record.callback.hide();
1403 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001404 Slog.w(TAG, "Object died trying to hide notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001405 + " in package " + record.pkg);
1406 // don't worry about this, we're about to remove it from
1407 // the list anyway
1408 }
1409 mToastQueue.remove(index);
1410 keepProcessAliveLocked(record.pid);
1411 if (mToastQueue.size() > 0) {
1412 // Show the next one. If the callback fails, this will remove
1413 // it from the list, so don't assume that the list hasn't changed
1414 // after this point.
1415 showNextToastLocked();
1416 }
1417 }
1418
1419 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
1420 {
1421 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
1422 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
1423 mHandler.removeCallbacksAndMessages(r);
1424 mHandler.sendMessageDelayed(m, delay);
1425 }
1426
1427 private void handleTimeout(ToastRecord record)
1428 {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001429 if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 synchronized (mToastQueue) {
1431 int index = indexOfToastLocked(record.pkg, record.callback);
1432 if (index >= 0) {
1433 cancelToastLocked(index);
1434 }
1435 }
1436 }
1437
1438 // lock on mToastQueue
1439 private int indexOfToastLocked(String pkg, ITransientNotification callback)
1440 {
1441 IBinder cbak = callback.asBinder();
1442 ArrayList<ToastRecord> list = mToastQueue;
1443 int len = list.size();
1444 for (int i=0; i<len; i++) {
1445 ToastRecord r = list.get(i);
1446 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
1447 return i;
1448 }
1449 }
1450 return -1;
1451 }
1452
1453 // lock on mToastQueue
1454 private void keepProcessAliveLocked(int pid)
1455 {
1456 int toastCount = 0; // toasts from this pid
1457 ArrayList<ToastRecord> list = mToastQueue;
1458 int N = list.size();
1459 for (int i=0; i<N; i++) {
1460 ToastRecord r = list.get(i);
1461 if (r.pid == pid) {
1462 toastCount++;
1463 }
1464 }
1465 try {
1466 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
1467 } catch (RemoteException e) {
1468 // Shouldn't happen.
1469 }
1470 }
1471
1472 private final class WorkerHandler extends Handler
1473 {
1474 @Override
1475 public void handleMessage(Message msg)
1476 {
1477 switch (msg.what)
1478 {
1479 case MESSAGE_TIMEOUT:
1480 handleTimeout((ToastRecord)msg.obj);
1481 break;
1482 }
1483 }
1484 }
1485
1486
1487 // Notifications
1488 // ============================================================================
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001489 public void enqueueNotificationWithTag(String pkg, String basePkg, String tag, int id,
1490 Notification notification, int[] idOut, int userId)
Fred Quintana6ecaff12009-09-25 14:23:13 -07001491 {
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001492 enqueueNotificationInternal(pkg, basePkg, Binder.getCallingUid(), Binder.getCallingPid(),
Dianne Hackborn41203752012-08-31 14:05:51 -07001493 tag, id, notification, idOut, userId);
Daniel Sandlerd0a2f862010-08-03 15:29:31 -04001494 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001495
1496 private final static int clamp(int x, int low, int high) {
1497 return (x < low) ? low : ((x > high) ? high : x);
Daniel Sandlere40451a2011-02-03 14:51:35 -05001498 }
1499
Daniel Sandlerd0a2f862010-08-03 15:29:31 -04001500 // Not exposed via Binder; for system use only (otherwise malicious apps could spoof the
1501 // uid/pid of another application)
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001502 public void enqueueNotificationInternal(String pkg, String basePkg, int callingUid,
1503 int callingPid, String tag, int id, Notification notification, int[] idOut, int userId)
Daniel Sandlerd0a2f862010-08-03 15:29:31 -04001504 {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001505 if (DBG) {
1506 Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id + " notification=" + notification);
1507 }
1508 checkCallerIsSystemOrSameApp(pkg);
1509 final boolean isSystemNotification = ("android".equals(pkg));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001510
Dianne Hackborn41203752012-08-31 14:05:51 -07001511 userId = ActivityManager.handleIncomingUser(callingPid,
Amith Yamasani2c7ebea2012-10-30 15:28:27 -07001512 callingUid, userId, true, false, "enqueueNotification", pkg);
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -07001513 final UserHandle user = new UserHandle(userId);
Dianne Hackborn41203752012-08-31 14:05:51 -07001514
Joe Onoratobd73d012010-06-04 11:44:54 -07001515 // Limit the number of notifications that any given package except the android
1516 // package can enqueue. Prevents DOS attacks and deals with leaks.
Daniel Sandler0da673f2012-04-11 12:33:16 -04001517 if (!isSystemNotification) {
Joe Onoratobd73d012010-06-04 11:44:54 -07001518 synchronized (mNotificationList) {
1519 int count = 0;
1520 final int N = mNotificationList.size();
1521 for (int i=0; i<N; i++) {
1522 final NotificationRecord r = mNotificationList.get(i);
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001523 if (r.sbn.pkg.equals(pkg) && r.sbn.getUserId() == userId) {
Joe Onoratobd73d012010-06-04 11:44:54 -07001524 count++;
1525 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
1526 Slog.e(TAG, "Package has already posted " + count
1527 + " notifications. Not showing more. package=" + pkg);
1528 return;
1529 }
1530 }
1531 }
1532 }
1533 }
1534
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001535 // This conditional is a dirty hack to limit the logging done on
1536 // behalf of the download manager without affecting other apps.
1537 if (!pkg.equals("com.android.providers.downloads")
1538 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Daniel Sandler321e9c52012-10-12 10:59:26 -07001539 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, tag, userId,
Daniel Sandlerb64cb882011-11-29 23:48:29 -05001540 notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001541 }
1542
1543 if (pkg == null || notification == null) {
1544 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
1545 + " id=" + id + " notification=" + notification);
1546 }
1547 if (notification.icon != 0) {
1548 if (notification.contentView == null) {
1549 throw new IllegalArgumentException("contentView required: pkg=" + pkg
1550 + " id=" + id + " notification=" + notification);
1551 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 }
1553
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001554 // === Scoring ===
Daniel Sandler0da673f2012-04-11 12:33:16 -04001555
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001556 // 0. Sanitize inputs
1557 notification.priority = clamp(notification.priority, Notification.PRIORITY_MIN, Notification.PRIORITY_MAX);
1558 // Migrate notification flags to scores
1559 if (0 != (notification.flags & Notification.FLAG_HIGH_PRIORITY)) {
1560 if (notification.priority < Notification.PRIORITY_MAX) notification.priority = Notification.PRIORITY_MAX;
Daniel Sandler49a2ad12012-03-28 15:46:39 -04001561 } else if (SCORE_ONGOING_HIGHER && 0 != (notification.flags & Notification.FLAG_ONGOING_EVENT)) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001562 if (notification.priority < Notification.PRIORITY_HIGH) notification.priority = Notification.PRIORITY_HIGH;
1563 }
Daniel Sandler0da673f2012-04-11 12:33:16 -04001564
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001565 // 1. initial score: buckets of 10, around the app
Daniel Sandler0da673f2012-04-11 12:33:16 -04001566 int score = notification.priority * NOTIFICATION_PRIORITY_MULTIPLIER; //[-20..20]
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001567
Daniel Sandler0da673f2012-04-11 12:33:16 -04001568 // 2. Consult external heuristics (TBD)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001569
Daniel Sandler0da673f2012-04-11 12:33:16 -04001570 // 3. Apply local rules
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001571
1572 // blocked apps
Daniel Sandler4a900acd2013-01-30 14:04:10 -05001573 if (ENABLE_BLOCKED_NOTIFICATIONS && !noteNotificationOp(pkg, callingUid)) {
1574 if (!isSystemNotification) {
1575 score = JUNK_SCORE;
1576 Slog.e(TAG, "Suppressing notification from package " + pkg + " by user request.");
1577 }
Daniel Sandler0da673f2012-04-11 12:33:16 -04001578 }
1579
1580 if (DBG) {
1581 Slog.v(TAG, "Assigned score=" + score + " to " + notification);
1582 }
1583
1584 if (score < SCORE_DISPLAY_THRESHOLD) {
1585 // Notification will be blocked because the score is too low.
1586 return;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001587 }
1588
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001589 // Should this notification make noise, vibe, or use the LED?
1590 final boolean canInterrupt = (score >= SCORE_INTERRUPTION_THRESHOLD);
1591
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592 synchronized (mNotificationList) {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001593 final StatusBarNotification n = new StatusBarNotification(
1594 pkg, id, tag, callingUid, callingPid, score, notification, user);
1595 NotificationRecord r = new NotificationRecord(n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596 NotificationRecord old = null;
1597
Dianne Hackborn41203752012-08-31 14:05:51 -07001598 int index = indexOfNotificationLocked(pkg, tag, id, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001599 if (index < 0) {
1600 mNotificationList.add(r);
1601 } else {
1602 old = mNotificationList.remove(index);
1603 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001604 // Make sure we don't lose the foreground service state.
1605 if (old != null) {
1606 notification.flags |=
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001607 old.getNotification().flags&Notification.FLAG_FOREGROUND_SERVICE;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001608 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001610
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001611 // Ensure if this is a foreground service that the proper additional
1612 // flags are set.
1613 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
1614 notification.flags |= Notification.FLAG_ONGOING_EVENT
1615 | Notification.FLAG_NO_CLEAR;
1616 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001617
Svetoslav Ganovc31ed392012-10-10 14:58:28 -07001618 final int currentUser;
1619 final long token = Binder.clearCallingIdentity();
1620 try {
1621 currentUser = ActivityManager.getCurrentUser();
1622 } finally {
1623 Binder.restoreCallingIdentity(token);
1624 }
1625
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001626 if (notification.icon != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001627 if (old != null && old.statusBarKey != null) {
1628 r.statusBarKey = old.statusBarKey;
1629 long identity = Binder.clearCallingIdentity();
1630 try {
Joe Onorato18e69df2010-05-17 22:26:12 -07001631 mStatusBar.updateNotification(r.statusBarKey, n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001632 }
1633 finally {
1634 Binder.restoreCallingIdentity(identity);
1635 }
1636 } else {
1637 long identity = Binder.clearCallingIdentity();
1638 try {
Joe Onorato18e69df2010-05-17 22:26:12 -07001639 r.statusBarKey = mStatusBar.addNotification(n);
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001640 if ((n.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0
1641 && canInterrupt) {
Mike Lockwoodece18ef2012-02-13 20:42:19 -08001642 mAttentionLight.pulse();
1643 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 }
1645 finally {
1646 Binder.restoreCallingIdentity(identity);
1647 }
1648 }
Svetoslav Ganovc31ed392012-10-10 14:58:28 -07001649 // Send accessibility events only for the current user.
1650 if (currentUser == userId) {
1651 sendAccessibilityEvent(notification, pkg);
1652 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001653
Daniel Sandler09a247e2013-02-14 10:24:17 -05001654 notifyPostedLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001655 } else {
Daniel Sandlere40451a2011-02-03 14:51:35 -05001656 Slog.e(TAG, "Ignoring notification with icon==0: " + notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001657 if (old != null && old.statusBarKey != null) {
1658 long identity = Binder.clearCallingIdentity();
1659 try {
Joe Onorato0cbda992010-05-02 16:28:15 -07001660 mStatusBar.removeNotification(old.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 }
1662 finally {
1663 Binder.restoreCallingIdentity(identity);
1664 }
Daniel Sandler09a247e2013-02-14 10:24:17 -05001665
1666 notifyRemovedLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001668 return; // do not play sounds, show lights, etc. for invalid notifications
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001669 }
1670
1671 // If we're not supposed to beep, vibrate, etc. then don't.
1672 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
1673 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -07001674 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001675 && (r.getUserId() == UserHandle.USER_ALL ||
1676 (r.getUserId() == userId && r.getUserId() == currentUser))
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001677 && canInterrupt
Joe Onorato30275482009-07-08 17:09:14 -07001678 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -08001679
1680 final AudioManager audioManager = (AudioManager) mContext
1681 .getSystemService(Context.AUDIO_SERVICE);
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 // sound
Daniel Sandler31475232013-04-17 00:17:22 -04001684
1685 // should we use the default notification sound? (indicated either by DEFAULT_SOUND
1686 // or because notification.sound is pointing at Settings.System.NOTIFICATION_SOUND)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 final boolean useDefaultSound =
Daniel Sandler31475232013-04-17 00:17:22 -04001688 (notification.defaults & Notification.DEFAULT_SOUND) != 0
1689 || Settings.System.DEFAULT_NOTIFICATION_URI.equals(notification.sound);
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001690
1691 Uri soundUri = null;
1692 boolean hasValidSound = false;
1693
1694 if (useDefaultSound) {
1695 soundUri = Settings.System.DEFAULT_NOTIFICATION_URI;
1696
1697 // check to see if the default notification sound is silent
1698 ContentResolver resolver = mContext.getContentResolver();
1699 hasValidSound = Settings.System.getString(resolver,
1700 Settings.System.NOTIFICATION_SOUND) != null;
1701 } else if (notification.sound != null) {
1702 soundUri = notification.sound;
1703 hasValidSound = (soundUri != null);
1704 }
1705
1706 if (hasValidSound) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
1708 int audioStreamType;
1709 if (notification.audioStreamType >= 0) {
1710 audioStreamType = notification.audioStreamType;
1711 } else {
1712 audioStreamType = DEFAULT_STREAM_TYPE;
1713 }
1714 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -08001715 // do not play notifications if stream volume is 0
Jean-Michel Trivid6770542012-10-10 12:03:41 -07001716 // (typically because ringer mode is silent) or if speech recognition is active.
1717 if ((audioManager.getStreamVolume(audioStreamType) != 0)
1718 && !audioManager.isSpeechRecognitionActive()) {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001719 final long identity = Binder.clearCallingIdentity();
Eric Laurent524dc042009-11-27 05:07:55 -08001720 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001721 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
1722 if (player != null) {
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001723 player.playAsync(soundUri, user, looping, audioStreamType);
Jeff Sharkey098d5802012-04-26 17:30:34 -07001724 }
1725 } catch (RemoteException e) {
1726 } finally {
Eric Laurent524dc042009-11-27 05:07:55 -08001727 Binder.restoreCallingIdentity(identity);
1728 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 }
1730 }
1731
1732 // vibrate
Daniel Sandleredbb3802012-11-13 20:49:47 -08001733 // Does the notification want to specify its own vibration?
1734 final boolean hasCustomVibrate = notification.vibrate != null;
1735
David Agnew71789e12012-11-09 23:03:26 -05001736 // 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 -05001737 // and no other vibration is specified, we fall back to vibration
David Agnew71789e12012-11-09 23:03:26 -05001738 final boolean convertSoundToVibration =
Daniel Sandleredbb3802012-11-13 20:49:47 -08001739 !hasCustomVibrate
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001740 && hasValidSound
David Agnew71789e12012-11-09 23:03:26 -05001741 && (audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE);
1742
Daniel Sandler4a7a9b92012-11-20 12:59:41 -05001743 // The DEFAULT_VIBRATE flag trumps any custom vibration AND the fallback.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 final boolean useDefaultVibrate =
Daniel Sandleredbb3802012-11-13 20:49:47 -08001745 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
David Agnew71789e12012-11-09 23:03:26 -05001746
Daniel Sandleredbb3802012-11-13 20:49:47 -08001747 if ((useDefaultVibrate || convertSoundToVibration || hasCustomVibrate)
Eric Laurentbffc3d12012-05-07 17:43:49 -07001748 && !(audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 mVibrateNotification = r;
1750
Daniel Sandleredbb3802012-11-13 20:49:47 -08001751 if (useDefaultVibrate || convertSoundToVibration) {
1752 // Escalate privileges so we can use the vibrator even if the notifying app
1753 // does not have the VIBRATE permission.
1754 long identity = Binder.clearCallingIdentity();
1755 try {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001756 mVibrator.vibrate(r.sbn.uid, r.sbn.basePkg,
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001757 useDefaultVibrate ? mDefaultVibrationPattern
1758 : mFallbackVibrationPattern,
Daniel Sandleredbb3802012-11-13 20:49:47 -08001759 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
1760 } finally {
1761 Binder.restoreCallingIdentity(identity);
1762 }
1763 } else if (notification.vibrate.length > 1) {
1764 // If you want your own vibration pattern, you need the VIBRATE permission
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001765 mVibrator.vibrate(r.sbn.uid, r.sbn.basePkg, notification.vibrate,
Daniel Sandleredbb3802012-11-13 20:49:47 -08001766 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
1767 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 }
1769 }
1770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001771 // light
1772 // the most recent thing gets the light
1773 mLights.remove(old);
1774 if (mLedNotification == old) {
1775 mLedNotification = null;
1776 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001777 //Slog.i(TAG, "notification.lights="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001778 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001779 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0
1780 && canInterrupt) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001781 mLights.add(r);
1782 updateLightsLocked();
1783 } else {
1784 if (old != null
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001785 && ((old.getFlags() & Notification.FLAG_SHOW_LIGHTS) != 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001786 updateLightsLocked();
1787 }
1788 }
1789 }
1790
1791 idOut[0] = id;
1792 }
1793
Joe Onorato30275482009-07-08 17:09:14 -07001794 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -07001795 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
1796 if (!manager.isEnabled()) {
1797 return;
1798 }
1799
1800 AccessibilityEvent event =
1801 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
1802 event.setPackageName(packageName);
1803 event.setClassName(Notification.class.getName());
1804 event.setParcelableData(notification);
1805 CharSequence tickerText = notification.tickerText;
1806 if (!TextUtils.isEmpty(tickerText)) {
1807 event.getText().add(tickerText);
1808 }
1809
1810 manager.sendAccessibilityEvent(event);
1811 }
1812
Joe Onorato46439ce2010-11-19 13:56:21 -08001813 private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete) {
1814 // tell the app
1815 if (sendDelete) {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001816 if (r.getNotification().deleteIntent != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001817 try {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001818 r.getNotification().deleteIntent.send();
Joe Onorato46439ce2010-11-19 13:56:21 -08001819 } catch (PendingIntent.CanceledException ex) {
1820 // do nothing - there's no relevant way to recover, and
1821 // no reason to let this propagate
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001822 Slog.w(TAG, "canceled PendingIntent for " + r.sbn.pkg, ex);
Joe Onorato46439ce2010-11-19 13:56:21 -08001823 }
1824 }
1825 }
1826
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 // status bar
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001828 if (r.getNotification().icon != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001829 long identity = Binder.clearCallingIdentity();
1830 try {
Joe Onorato0cbda992010-05-02 16:28:15 -07001831 mStatusBar.removeNotification(r.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001832 }
1833 finally {
1834 Binder.restoreCallingIdentity(identity);
1835 }
1836 r.statusBarKey = null;
Daniel Sandler09a247e2013-02-14 10:24:17 -05001837 notifyRemovedLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 }
1839
1840 // sound
1841 if (mSoundNotification == r) {
1842 mSoundNotification = null;
Jeff Sharkey098d5802012-04-26 17:30:34 -07001843 final long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001845 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
1846 if (player != null) {
1847 player.stopAsync();
1848 }
1849 } catch (RemoteException e) {
1850 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851 Binder.restoreCallingIdentity(identity);
1852 }
1853 }
1854
1855 // vibrate
1856 if (mVibrateNotification == r) {
1857 mVibrateNotification = null;
1858 long identity = Binder.clearCallingIdentity();
1859 try {
1860 mVibrator.cancel();
1861 }
1862 finally {
1863 Binder.restoreCallingIdentity(identity);
1864 }
1865 }
1866
1867 // light
1868 mLights.remove(r);
1869 if (mLedNotification == r) {
1870 mLedNotification = null;
1871 }
Daniel Sandler23d7c702013-03-07 16:32:06 -05001872
1873 // Save it for users of getHistoricalNotifications()
1874 mArchive.record(r.sbn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 }
1876
1877 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001878 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001879 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 */
Fred Quintana6ecaff12009-09-25 14:23:13 -07001881 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Dianne Hackborn41203752012-08-31 14:05:51 -07001882 int mustNotHaveFlags, boolean sendDelete, int userId) {
Daniel Sandler321e9c52012-10-12 10:59:26 -07001883 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, tag, userId,
Daniel Sandlerb64cb882011-11-29 23:48:29 -05001884 mustHaveFlags, mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885
1886 synchronized (mNotificationList) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001887 int index = indexOfNotificationLocked(pkg, tag, id, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07001889 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001890
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001891 if ((r.getNotification().flags & mustHaveFlags) != mustHaveFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001892 return;
1893 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001894 if ((r.getNotification().flags & mustNotHaveFlags) != 0) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001895 return;
1896 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001897
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 mNotificationList.remove(index);
1899
Joe Onorato46439ce2010-11-19 13:56:21 -08001900 cancelNotificationLocked(r, sendDelete);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001901 updateLightsLocked();
1902 }
1903 }
1904 }
1905
1906 /**
Daniel Sandler321e9c52012-10-12 10:59:26 -07001907 * Determine whether the userId applies to the notification in question, either because
1908 * they match exactly, or one of them is USER_ALL (which is treated as a wildcard).
1909 */
1910 private boolean notificationMatchesUserId(NotificationRecord r, int userId) {
1911 return
1912 // looking for USER_ALL notifications? match everything
1913 userId == UserHandle.USER_ALL
1914 // a notification sent to USER_ALL matches any query
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001915 || r.getUserId() == UserHandle.USER_ALL
Daniel Sandler321e9c52012-10-12 10:59:26 -07001916 // an exact user match
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001917 || r.getUserId() == userId;
Daniel Sandler321e9c52012-10-12 10:59:26 -07001918 }
1919
1920 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001921 * Cancels all notifications from a given package that have all of the
1922 * {@code mustHaveFlags}.
1923 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001924 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
Dianne Hackborn41203752012-08-31 14:05:51 -07001925 int mustNotHaveFlags, boolean doit, int userId) {
Daniel Sandler321e9c52012-10-12 10:59:26 -07001926 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, userId,
1927 mustHaveFlags, mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928
1929 synchronized (mNotificationList) {
1930 final int N = mNotificationList.size();
1931 boolean canceledSomething = false;
1932 for (int i = N-1; i >= 0; --i) {
1933 NotificationRecord r = mNotificationList.get(i);
Daniel Sandler321e9c52012-10-12 10:59:26 -07001934 if (!notificationMatchesUserId(r, userId)) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001935 continue;
1936 }
Amith Yamasani5ec00e92012-11-07 16:58:30 -08001937 // Don't remove notifications to all, if there's no package name specified
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001938 if (r.getUserId() == UserHandle.USER_ALL && pkg == null) {
Amith Yamasani5ec00e92012-11-07 16:58:30 -08001939 continue;
1940 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001941 if ((r.getFlags() & mustHaveFlags) != mustHaveFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001942 continue;
1943 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001944 if ((r.getFlags() & mustNotHaveFlags) != 0) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001945 continue;
1946 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001947 if (pkg != null && !r.sbn.pkg.equals(pkg)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948 continue;
1949 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001950 canceledSomething = true;
1951 if (!doit) {
1952 return true;
1953 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08001955 cancelNotificationLocked(r, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001956 }
1957 if (canceledSomething) {
1958 updateLightsLocked();
1959 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001960 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 }
1962 }
1963
Dianne Hackborn41203752012-08-31 14:05:51 -07001964 public void cancelNotificationWithTag(String pkg, String tag, int id, int userId) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001965 checkCallerIsSystemOrSameApp(pkg);
Dianne Hackborn41203752012-08-31 14:05:51 -07001966 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Amith Yamasani2c7ebea2012-10-30 15:28:27 -07001967 Binder.getCallingUid(), userId, true, false, "cancelNotificationWithTag", pkg);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001968 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -07001969 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001970 Binder.getCallingUid() == Process.SYSTEM_UID
Dianne Hackborn41203752012-08-31 14:05:51 -07001971 ? 0 : Notification.FLAG_FOREGROUND_SERVICE, false, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 }
1973
Dianne Hackborn41203752012-08-31 14:05:51 -07001974 public void cancelAllNotifications(String pkg, int userId) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001975 checkCallerIsSystemOrSameApp(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001976
Dianne Hackborn41203752012-08-31 14:05:51 -07001977 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Amith Yamasani2c7ebea2012-10-30 15:28:27 -07001978 Binder.getCallingUid(), userId, true, false, "cancelAllNotifications", pkg);
Dianne Hackborn41203752012-08-31 14:05:51 -07001979
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001980 // Calling from user space, don't allow the canceling of actively
1981 // running foreground services.
Dianne Hackborn41203752012-08-31 14:05:51 -07001982 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001983 }
1984
Daniel Sandler0da673f2012-04-11 12:33:16 -04001985 void checkCallerIsSystem() {
1986 int uid = Binder.getCallingUid();
Dianne Hackborn0c380492012-08-20 17:23:30 -07001987 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001988 return;
1989 }
1990 throw new SecurityException("Disallowed call for uid " + uid);
1991 }
1992
1993 void checkCallerIsSystemOrSameApp(String pkg) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001994 int uid = Binder.getCallingUid();
Dianne Hackborn0c380492012-08-20 17:23:30 -07001995 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001996 return;
1997 }
1998 try {
Amith Yamasanif203aee2012-08-29 18:41:53 -07001999 ApplicationInfo ai = AppGlobals.getPackageManager().getApplicationInfo(
2000 pkg, 0, UserHandle.getCallingUserId());
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002001 if (!UserHandle.isSameApp(ai.uid, uid)) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07002002 throw new SecurityException("Calling uid " + uid + " gave package"
2003 + pkg + " which is owned by uid " + ai.uid);
2004 }
Amith Yamasanif203aee2012-08-29 18:41:53 -07002005 } catch (RemoteException re) {
2006 throw new SecurityException("Unknown package " + pkg + "\n" + re);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07002007 }
2008 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002009
Dianne Hackborn41203752012-08-31 14:05:51 -07002010 void cancelAll(int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002011 synchronized (mNotificationList) {
2012 final int N = mNotificationList.size();
2013 for (int i=N-1; i>=0; i--) {
2014 NotificationRecord r = mNotificationList.get(i);
2015
Daniel Sandler321e9c52012-10-12 10:59:26 -07002016 if (!notificationMatchesUserId(r, userId)) {
Dianne Hackborn41203752012-08-31 14:05:51 -07002017 continue;
2018 }
2019
Daniel Sandlerfde19b12013-01-17 00:21:05 -05002020 if ((r.getFlags() & (Notification.FLAG_ONGOING_EVENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021 | Notification.FLAG_NO_CLEAR)) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08002023 cancelNotificationLocked(r, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002024 }
2025 }
2026
2027 updateLightsLocked();
2028 }
2029 }
2030
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 // lock on mNotificationList
2032 private void updateLightsLocked()
2033 {
The Android Open Source Project10592532009-03-18 17:39:46 -07002034 // handle notification lights
2035 if (mLedNotification == null) {
2036 // get next notification, if any
2037 int n = mLights.size();
2038 if (n > 0) {
2039 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 }
2041 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05002042
Mike Lockwood63b5ad92011-08-30 09:55:30 -04002043 // Don't flash while we are in a call or screen is on
2044 if (mLedNotification == null || mInCall || mScreenOn) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002045 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07002046 } else {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05002047 final Notification ledno = mLedNotification.sbn.notification;
2048 int ledARGB = ledno.ledARGB;
2049 int ledOnMS = ledno.ledOnMS;
2050 int ledOffMS = ledno.ledOffMS;
2051 if ((ledno.defaults & Notification.DEFAULT_LIGHTS) != 0) {
Mike Lockwood670f9322010-01-20 12:13:36 -05002052 ledARGB = mDefaultNotificationColor;
2053 ledOnMS = mDefaultNotificationLedOn;
2054 ledOffMS = mDefaultNotificationLedOff;
2055 }
2056 if (mNotificationPulseEnabled) {
2057 // pulse repeatedly
2058 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
2059 ledOnMS, ledOffMS);
Mike Lockwood670f9322010-01-20 12:13:36 -05002060 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002061 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002062 }
2063
2064 // lock on mNotificationList
Dianne Hackborn41203752012-08-31 14:05:51 -07002065 private int indexOfNotificationLocked(String pkg, String tag, int id, int userId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002066 {
2067 ArrayList<NotificationRecord> list = mNotificationList;
2068 final int len = list.size();
2069 for (int i=0; i<len; i++) {
2070 NotificationRecord r = list.get(i);
Daniel Sandlerfde19b12013-01-17 00:21:05 -05002071 if (!notificationMatchesUserId(r, userId) || r.sbn.id != id) {
Dianne Hackborn41203752012-08-31 14:05:51 -07002072 continue;
2073 }
Fred Quintana6ecaff12009-09-25 14:23:13 -07002074 if (tag == null) {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05002075 if (r.sbn.tag != null) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07002076 continue;
2077 }
2078 } else {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05002079 if (!tag.equals(r.sbn.tag)) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07002080 continue;
2081 }
2082 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05002083 if (r.sbn.pkg.equals(pkg)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084 return i;
2085 }
2086 }
2087 return -1;
2088 }
2089
Mike Lockwoodc22404a2009-12-02 11:15:02 -05002090 private void updateNotificationPulse() {
2091 synchronized (mNotificationList) {
2092 updateLightsLocked();
2093 }
2094 }
2095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002096 // ======================================================================
2097 @Override
2098 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2099 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2100 != PackageManager.PERMISSION_GRANTED) {
2101 pw.println("Permission Denial: can't dump NotificationManager from from pid="
2102 + Binder.getCallingPid()
2103 + ", uid=" + Binder.getCallingUid());
2104 return;
2105 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002107 pw.println("Current Notification Manager state:");
2108
Daniel Sandler5feceeb2013-03-22 18:29:23 -07002109 pw.println(" Listeners (" + mEnabledListenersForCurrentUser.size()
2110 + ") enabled for current user:");
2111 for (ComponentName cmpt : mEnabledListenersForCurrentUser) {
2112 pw.println(" " + cmpt);
Daniel Sandler4b749ef2013-03-18 21:53:04 -04002113 }
Daniel Sandler4b749ef2013-03-18 21:53:04 -04002114
Daniel Sandler5feceeb2013-03-22 18:29:23 -07002115 pw.println(" Live listeners (" + mListeners.size() + "):");
Daniel Sandler4b749ef2013-03-18 21:53:04 -04002116 for (NotificationListenerInfo info : mListeners) {
Daniel Sandler5feceeb2013-03-22 18:29:23 -07002117 pw.println(" " + info.component
2118 + " (user " + info.userid + "): " + info.listener
2119 + (info.isSystem?" SYSTEM":""));
Daniel Sandler4b749ef2013-03-18 21:53:04 -04002120 }
2121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002122 int N;
2123
2124 synchronized (mToastQueue) {
2125 N = mToastQueue.size();
2126 if (N > 0) {
2127 pw.println(" Toast Queue:");
2128 for (int i=0; i<N; i++) {
2129 mToastQueue.get(i).dump(pw, " ");
2130 }
2131 pw.println(" ");
2132 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002134 }
2135
2136 synchronized (mNotificationList) {
2137 N = mNotificationList.size();
2138 if (N > 0) {
2139 pw.println(" Notification List:");
2140 for (int i=0; i<N; i++) {
2141 mNotificationList.get(i).dump(pw, " ", mContext);
2142 }
2143 pw.println(" ");
2144 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 N = mLights.size();
2147 if (N > 0) {
2148 pw.println(" Lights List:");
2149 for (int i=0; i<N; i++) {
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002150 pw.println(" " + mLights.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002151 }
2152 pw.println(" ");
2153 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002154
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002155 pw.println(" mSoundNotification=" + mSoundNotification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002156 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04002157 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
2158 pw.println(" mSystemReady=" + mSystemReady);
Daniel Sandler5e62e3a2013-04-15 20:57:02 -04002159 pw.println(" mArchive=" + mArchive.toString());
2160 Iterator<StatusBarNotification> iter = mArchive.descendingIterator();
2161 int i=0;
2162 while (iter.hasNext()) {
2163 pw.println(" " + iter.next());
2164 if (++i >= 5) {
2165 if (iter.hasNext()) pw.println(" ...");
2166 break;
2167 }
2168 }
2169
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 }
2171 }
2172}