blob: 6f01be1e23cc02cb806ba0538f1c710c11d49efc [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 Sandler4a900ac2013-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 Sandler4a900ac2013-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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.os.Handler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.os.IBinder;
54import android.os.Message;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070055import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070056import android.os.RemoteException;
Jeff Sharkey098d5802012-04-26 17:30:34 -070057import android.os.ServiceManager;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070058import android.os.UserHandle;
Daniel Sandler4b749ef2013-03-18 21:53:04 -040059import android.os.UserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.os.Vibrator;
61import android.provider.Settings;
Daniel Sandler5feceeb2013-03-22 18:29:23 -070062import android.service.notification.INotificationListener;
63import android.service.notification.NotificationListenerService;
64import android.service.notification.StatusBarNotification;
Daniel Sandlere96ffb12010-03-11 13:38:06 -050065import android.telephony.TelephonyManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070066import android.text.TextUtils;
Dianne Hackborn39606a02012-07-31 17:54:35 -070067import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.util.EventLog;
69import android.util.Log;
Andy Stadler110988c2010-12-03 14:29:16 -080070import android.util.Slog;
Daniel Sandler0da673f2012-04-11 12:33:16 -040071import android.util.Xml;
svetoslavganov75986cf2009-05-14 22:28:01 -070072import android.view.accessibility.AccessibilityEvent;
73import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074import android.widget.Toast;
75
Jeff Sharkey098d5802012-04-26 17:30:34 -070076import org.xmlpull.v1.XmlPullParser;
77import org.xmlpull.v1.XmlPullParserException;
Jeff Sharkey098d5802012-04-26 17:30:34 -070078
Daniel Sandler0da673f2012-04-11 12:33:16 -040079import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080import java.io.FileDescriptor;
Daniel Sandler0da673f2012-04-11 12:33:16 -040081import java.io.FileInputStream;
82import java.io.FileNotFoundException;
Daniel Sandler0da673f2012-04-11 12:33:16 -040083import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084import java.io.PrintWriter;
Daniel Sandlerf45564e2013-04-15 15:05:08 -040085import java.lang.reflect.Array;
Daniel Sandlerfde19b12013-01-17 00:21:05 -050086import java.util.ArrayDeque;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087import java.util.ArrayList;
88import java.util.Arrays;
Daniel Sandler0da673f2012-04-11 12:33:16 -040089import java.util.HashSet;
Daniel Sandlerfde19b12013-01-17 00:21:05 -050090import java.util.Iterator;
91import java.util.NoSuchElementException;
Daniel Sandler0da673f2012-04-11 12:33:16 -040092
93import libcore.io.IoUtils;
94
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095
Daniel Sandlerd0a2f862010-08-03 15:29:31 -040096/** {@hide} */
97public class NotificationManagerService extends INotificationManager.Stub
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098{
99 private static final String TAG = "NotificationService";
100 private static final boolean DBG = false;
101
Joe Onoratobd73d012010-06-04 11:44:54 -0700102 private static final int MAX_PACKAGE_NOTIFICATIONS = 50;
103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 // message codes
105 private static final int MESSAGE_TIMEOUT = 2;
106
107 private static final int LONG_DELAY = 3500; // 3.5 seconds
108 private static final int SHORT_DELAY = 2000; // 2 seconds
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800109
110 private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250};
Daniel Sandleredbb3802012-11-13 20:49:47 -0800111 private static final int VIBRATE_PATTERN_MAXLEN = 8 * 2 + 1; // up to eight bumps
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112
113 private static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION;
Daniel Sandler49a2ad12012-03-28 15:46:39 -0400114 private static final boolean SCORE_ONGOING_HIGHER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115
Daniel Sandler0da673f2012-04-11 12:33:16 -0400116 private static final int JUNK_SCORE = -1000;
117 private static final int NOTIFICATION_PRIORITY_MULTIPLIER = 10;
118 private static final int SCORE_DISPLAY_THRESHOLD = Notification.PRIORITY_MIN * NOTIFICATION_PRIORITY_MULTIPLIER;
119
Daniel Sandler526fa0e2012-12-04 14:51:50 -0500120 // Notifications with scores below this will not interrupt the user, either via LED or
121 // sound or vibration
122 private static final int SCORE_INTERRUPTION_THRESHOLD =
123 Notification.PRIORITY_LOW * NOTIFICATION_PRIORITY_MULTIPLIER;
124
Daniel Sandler0da673f2012-04-11 12:33:16 -0400125 private static final boolean ENABLE_BLOCKED_NOTIFICATIONS = true;
126 private static final boolean ENABLE_BLOCKED_TOASTS = true;
127
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700128 private static final String ENABLED_NOTIFICATION_LISTENERS_SEPARATOR = ":";
129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 final Context mContext;
131 final IActivityManager mAm;
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400132 final UserManager mUserManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 final IBinder mForegroundToken = new Binder();
134
135 private WorkerHandler mHandler;
Joe Onorato089de882010-04-12 08:18:45 -0700136 private StatusBarManagerService mStatusBar;
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500137 private LightsService.Light mNotificationLight;
138 private LightsService.Light mAttentionLight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139
Mike Lockwood670f9322010-01-20 12:13:36 -0500140 private int mDefaultNotificationColor;
141 private int mDefaultNotificationLedOn;
142 private int mDefaultNotificationLedOff;
143
Daniel Sandleredbb3802012-11-13 20:49:47 -0800144 private long[] mDefaultVibrationPattern;
145 private long[] mFallbackVibrationPattern;
146
Joe Onorato30275482009-07-08 17:09:14 -0700147 private boolean mSystemReady;
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400148 private int mDisabledNotifications;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149
Jeff Sharkey098d5802012-04-26 17:30:34 -0700150 private NotificationRecord mSoundNotification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 private NotificationRecord mVibrateNotification;
Jeff Sharkey098d5802012-04-26 17:30:34 -0700152
153 private IAudioService mAudioService;
Jeff Brownc2346132012-04-13 01:55:38 -0700154 private Vibrator mVibrator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500156 // for enabling and disabling notification pulse behavior
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400157 private boolean mScreenOn = true;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500158 private boolean mInCall = false;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500159 private boolean mNotificationPulseEnabled;
160
Daniel Sandler09a247e2013-02-14 10:24:17 -0500161 // used as a mutex for access to all active notifications & listeners
Fred Quintana6ecaff12009-09-25 14:23:13 -0700162 private final ArrayList<NotificationRecord> mNotificationList =
163 new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164
165 private ArrayList<ToastRecord> mToastQueue;
166
167 private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 private NotificationRecord mLedNotification;
svetoslavganov75986cf2009-05-14 22:28:01 -0700169
Daniel Sandler4a900ac2013-01-30 14:04:10 -0500170 private final AppOpsManager mAppOps;
171
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700172 // contains connections to all connected listeners, including app services
173 // and system listeners
174 private ArrayList<NotificationListenerInfo> mListeners
175 = new ArrayList<NotificationListenerInfo>();
176 // things that will be put into mListeners as soon as they're ready
177 private ArrayList<String> mServicesBinding = new ArrayList<String>();
178 // lists the component names of all enabled (and therefore connected) listener
179 // app services for the current user only
180 private HashSet<ComponentName> mEnabledListenersForCurrentUser
181 = new HashSet<ComponentName>();
182 // Just the packages from mEnabledListenersForCurrentUser
183 private HashSet<String> mEnabledListenerPackageNames = new HashSet<String>();
Daniel Sandler09a247e2013-02-14 10:24:17 -0500184
Daniel Sandler0da673f2012-04-11 12:33:16 -0400185 // Notification control database. For now just contains disabled packages.
186 private AtomicFile mPolicyFile;
187 private HashSet<String> mBlockedPackages = new HashSet<String>();
188
189 private static final int DB_VERSION = 1;
190
191 private static final String TAG_BODY = "notification-policy";
192 private static final String ATTR_VERSION = "version";
193
194 private static final String TAG_BLOCKED_PKGS = "blocked-packages";
195 private static final String TAG_PACKAGE = "package";
196 private static final String ATTR_NAME = "name";
197
Daniel Sandler09a247e2013-02-14 10:24:17 -0500198 private class NotificationListenerInfo implements DeathRecipient {
199 INotificationListener listener;
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700200 ComponentName component;
Daniel Sandler09a247e2013-02-14 10:24:17 -0500201 int userid;
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400202 boolean isSystem;
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700203 ServiceConnection connection;
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400204
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700205 public NotificationListenerInfo(INotificationListener listener, ComponentName component,
206 int userid, boolean isSystem) {
Daniel Sandler09a247e2013-02-14 10:24:17 -0500207 this.listener = listener;
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700208 this.component = component;
Daniel Sandler09a247e2013-02-14 10:24:17 -0500209 this.userid = userid;
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400210 this.isSystem = isSystem;
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700211 this.connection = null;
212 }
213
214 public NotificationListenerInfo(INotificationListener listener, ComponentName component,
215 int userid, ServiceConnection connection) {
216 this.listener = listener;
217 this.component = component;
218 this.userid = userid;
219 this.isSystem = false;
220 this.connection = connection;
Daniel Sandler09a247e2013-02-14 10:24:17 -0500221 }
222
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400223 boolean enabledAndUserMatches(StatusBarNotification sbn) {
224 final int nid = sbn.getUserId();
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700225 if (!isEnabledForCurrentUser()) {
226 return false;
227 }
Daniel Sandler21ca44d2013-03-07 13:58:00 -0500228 if (this.userid == UserHandle.USER_ALL) return true;
Daniel Sandler21ca44d2013-03-07 13:58:00 -0500229 return (nid == UserHandle.USER_ALL || nid == this.userid);
230 }
231
Daniel Sandler09a247e2013-02-14 10:24:17 -0500232 public void notifyPostedIfUserMatch(StatusBarNotification sbn) {
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700233 if (!enabledAndUserMatches(sbn)) {
234 return;
235 }
Daniel Sandler09a247e2013-02-14 10:24:17 -0500236 try {
237 listener.onNotificationPosted(sbn);
238 } catch (RemoteException ex) {
Daniel Sandler1a497d32013-04-18 14:52:45 -0400239 Log.e(TAG, "unable to notify listener (posted): " + listener, ex);
Daniel Sandler09a247e2013-02-14 10:24:17 -0500240 }
241 }
242
243 public void notifyRemovedIfUserMatch(StatusBarNotification sbn) {
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400244 if (!enabledAndUserMatches(sbn)) return;
Daniel Sandler09a247e2013-02-14 10:24:17 -0500245 try {
246 listener.onNotificationRemoved(sbn);
247 } catch (RemoteException ex) {
Daniel Sandler1a497d32013-04-18 14:52:45 -0400248 Log.e(TAG, "unable to notify listener (removed): " + listener, ex);
Daniel Sandler09a247e2013-02-14 10:24:17 -0500249 }
250 }
251
252 @Override
253 public void binderDied() {
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700254 if (connection == null) {
255 // This is not a service; it won't be recreated. We can give up this connection.
256 unregisterListener(this.listener, this.userid);
257 }
Daniel Sandler09a247e2013-02-14 10:24:17 -0500258 }
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400259
260 /** convenience method for looking in mEnabledListenersForCurrentUser */
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700261 public boolean isEnabledForCurrentUser() {
262 if (this.isSystem) return true;
263 if (this.connection == null) return false;
264 return mEnabledListenersForCurrentUser.contains(this.component);
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400265 }
Daniel Sandler09a247e2013-02-14 10:24:17 -0500266 }
267
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500268 private static class Archive {
Daniel Sandler5e62e3a2013-04-15 20:57:02 -0400269 static final int BUFFER_SIZE = 250;
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500270 ArrayDeque<StatusBarNotification> mBuffer = new ArrayDeque<StatusBarNotification>(BUFFER_SIZE);
271
272 public Archive() {
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500273 }
Jeff Sharkey0c1baf92013-04-03 13:08:52 -0700274
Daniel Sandler5e62e3a2013-04-15 20:57:02 -0400275 public String toString() {
276 final StringBuilder sb = new StringBuilder();
277 final int N = mBuffer.size();
278 sb.append("Archive (");
279 sb.append(N);
280 sb.append(" notification");
281 sb.append((N==1)?")":"s)");
282 return sb.toString();
283 }
284
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500285 public void record(StatusBarNotification nr) {
Jeff Sharkey0c1baf92013-04-03 13:08:52 -0700286 // Nuke heavy parts of notification before storing in archive
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -0400287 nr.getNotification().lightenPayload();
Jeff Sharkey0c1baf92013-04-03 13:08:52 -0700288
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500289 if (mBuffer.size() == BUFFER_SIZE) {
290 mBuffer.removeFirst();
291 }
292 mBuffer.addLast(nr);
293 }
294
Daniel Sandler5e62e3a2013-04-15 20:57:02 -0400295
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500296 public void clear() {
297 mBuffer.clear();
298 }
299
300 public Iterator<StatusBarNotification> descendingIterator() {
301 return mBuffer.descendingIterator();
302 }
303 public Iterator<StatusBarNotification> ascendingIterator() {
304 return mBuffer.iterator();
305 }
306 public Iterator<StatusBarNotification> filter(
307 final Iterator<StatusBarNotification> iter, final String pkg, final int userId) {
308 return new Iterator<StatusBarNotification>() {
309 StatusBarNotification mNext = findNext();
310
311 private StatusBarNotification findNext() {
312 while (iter.hasNext()) {
313 StatusBarNotification nr = iter.next();
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -0400314 if ((pkg == null || nr.getPkg() == pkg)
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500315 && (userId == UserHandle.USER_ALL || nr.getUserId() == userId)) {
316 return nr;
317 }
318 }
319 return null;
320 }
321
322 @Override
323 public boolean hasNext() {
324 return mNext == null;
325 }
326
327 @Override
328 public StatusBarNotification next() {
329 StatusBarNotification next = mNext;
330 if (next == null) {
331 throw new NoSuchElementException();
332 }
333 mNext = findNext();
334 return next;
335 }
336
337 @Override
338 public void remove() {
339 iter.remove();
340 }
341 };
342 }
Daniel Sandler78d0d252013-02-12 08:14:52 -0500343
344 public StatusBarNotification[] getArray(int count) {
345 if (count == 0) count = Archive.BUFFER_SIZE;
346 final StatusBarNotification[] a
347 = new StatusBarNotification[Math.min(count, mBuffer.size())];
348 Iterator<StatusBarNotification> iter = descendingIterator();
349 int i=0;
350 while (iter.hasNext() && i < count) {
351 a[i++] = iter.next();
352 }
353 return a;
354 }
355
356 public StatusBarNotification[] getArray(int count, String pkg, int userId) {
357 if (count == 0) count = Archive.BUFFER_SIZE;
358 final StatusBarNotification[] a
359 = new StatusBarNotification[Math.min(count, mBuffer.size())];
360 Iterator<StatusBarNotification> iter = filter(descendingIterator(), pkg, userId);
361 int i=0;
362 while (iter.hasNext() && i < count) {
363 a[i++] = iter.next();
364 }
365 return a;
366 }
367
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500368 }
369
370 Archive mArchive = new Archive();
371
Daniel Sandler0da673f2012-04-11 12:33:16 -0400372 private void loadBlockDb() {
373 synchronized(mBlockedPackages) {
374 if (mPolicyFile == null) {
375 File dir = new File("/data/system");
376 mPolicyFile = new AtomicFile(new File(dir, "notification_policy.xml"));
377
378 mBlockedPackages.clear();
379
380 FileInputStream infile = null;
381 try {
382 infile = mPolicyFile.openRead();
383 final XmlPullParser parser = Xml.newPullParser();
384 parser.setInput(infile, null);
385
386 int type;
387 String tag;
388 int version = DB_VERSION;
389 while ((type = parser.next()) != END_DOCUMENT) {
390 tag = parser.getName();
391 if (type == START_TAG) {
392 if (TAG_BODY.equals(tag)) {
393 version = Integer.parseInt(parser.getAttributeValue(null, ATTR_VERSION));
394 } else if (TAG_BLOCKED_PKGS.equals(tag)) {
395 while ((type = parser.next()) != END_DOCUMENT) {
396 tag = parser.getName();
397 if (TAG_PACKAGE.equals(tag)) {
398 mBlockedPackages.add(parser.getAttributeValue(null, ATTR_NAME));
399 } else if (TAG_BLOCKED_PKGS.equals(tag) && type == END_TAG) {
400 break;
401 }
402 }
403 }
404 }
405 }
406 } catch (FileNotFoundException e) {
407 // No data yet
408 } catch (IOException e) {
409 Log.wtf(TAG, "Unable to read blocked notifications database", e);
410 } catch (NumberFormatException e) {
411 Log.wtf(TAG, "Unable to parse blocked notifications database", e);
412 } catch (XmlPullParserException e) {
413 Log.wtf(TAG, "Unable to parse blocked notifications database", e);
414 } finally {
415 IoUtils.closeQuietly(infile);
416 }
417 }
418 }
419 }
420
Daniel Sandler4a900ac2013-01-30 14:04:10 -0500421 /**
422 * Use this when you just want to know if notifications are OK for this package.
423 */
424 public boolean areNotificationsEnabledForPackage(String pkg, int uid) {
Daniel Sandler0da673f2012-04-11 12:33:16 -0400425 checkCallerIsSystem();
Daniel Sandler4a900ac2013-01-30 14:04:10 -0500426 return (mAppOps.checkOpNoThrow(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg)
427 == AppOpsManager.MODE_ALLOWED);
Daniel Sandler0da673f2012-04-11 12:33:16 -0400428 }
429
Daniel Sandler4a900ac2013-01-30 14:04:10 -0500430 /** Use this when you actually want to post a notification or toast.
431 *
432 * Unchecked. Not exposed via Binder, but can be called in the course of enqueue*().
433 */
434 private boolean noteNotificationOp(String pkg, int uid) {
435 if (mAppOps.noteOpNoThrow(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg)
436 != AppOpsManager.MODE_ALLOWED) {
437 Slog.v(TAG, "notifications are disabled by AppOps for " + pkg);
438 return false;
Daniel Sandler0da673f2012-04-11 12:33:16 -0400439 }
Daniel Sandler4a900ac2013-01-30 14:04:10 -0500440 return true;
Daniel Sandler0da673f2012-04-11 12:33:16 -0400441 }
442
Daniel Sandler4a900ac2013-01-30 14:04:10 -0500443 public void setNotificationsEnabledForPackage(String pkg, int uid, boolean enabled) {
Daniel Sandler0da673f2012-04-11 12:33:16 -0400444 checkCallerIsSystem();
Daniel Sandler32e698b2013-04-18 10:51:35 -0400445
446 Slog.v(TAG, (enabled?"en":"dis") + "abling notifications for " + pkg);
447
Daniel Sandler4a900ac2013-01-30 14:04:10 -0500448 mAppOps.setMode(AppOpsManager.OP_POST_NOTIFICATION, uid, pkg,
449 enabled ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_IGNORED);
Daniel Sandler32e698b2013-04-18 10:51:35 -0400450
451 // Now, cancel any outstanding notifications that are part of a just-disabled app
452 if (ENABLE_BLOCKED_NOTIFICATIONS && !enabled) {
453 cancelAllNotificationsInt(pkg, 0, 0, true, UserHandle.getUserId(uid));
454 }
Daniel Sandler0da673f2012-04-11 12:33:16 -0400455 }
456
457
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800458 private static String idDebugString(Context baseContext, String packageName, int id) {
459 Context c = null;
460
461 if (packageName != null) {
462 try {
463 c = baseContext.createPackageContext(packageName, 0);
464 } catch (NameNotFoundException e) {
465 c = baseContext;
466 }
467 } else {
468 c = baseContext;
469 }
470
471 String pkg;
472 String type;
473 String name;
474
475 Resources r = c.getResources();
476 try {
477 return r.getResourceName(id);
478 } catch (Resources.NotFoundException e) {
479 return "<name unknown>";
480 }
481 }
482
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700483 /**
484 * System-only API for getting a list of current (i.e. not cleared) notifications.
485 *
486 * Requires ACCESS_NOTIFICATIONS which is signature|system.
487 */
488 @Override
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500489 public StatusBarNotification[] getActiveNotifications(String callingPkg) {
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400490 // enforce() will ensure the calling uid has the correct permission
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500491 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NOTIFICATIONS,
Daniel Sandler78d0d252013-02-12 08:14:52 -0500492 "NotificationManagerService.getActiveNotifications");
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500493
494 StatusBarNotification[] tmp = null;
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500495 int uid = Binder.getCallingUid();
496
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400497 // noteOp will check to make sure the callingPkg matches the uid
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500498 if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg)
499 == AppOpsManager.MODE_ALLOWED) {
500 synchronized (mNotificationList) {
501 tmp = new StatusBarNotification[mNotificationList.size()];
502 final int N = mNotificationList.size();
503 for (int i=0; i<N; i++) {
504 tmp[i] = mNotificationList.get(i).sbn;
505 }
506 }
507 }
508 return tmp;
509 }
510
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700511 /**
512 * System-only API for getting a list of recent (cleared, no longer shown) notifications.
513 *
514 * Requires ACCESS_NOTIFICATIONS which is signature|system.
515 */
516 @Override
Daniel Sandler78d0d252013-02-12 08:14:52 -0500517 public StatusBarNotification[] getHistoricalNotifications(String callingPkg, int count) {
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400518 // enforce() will ensure the calling uid has the correct permission
Daniel Sandler78d0d252013-02-12 08:14:52 -0500519 mContext.enforceCallingOrSelfPermission(android.Manifest.permission.ACCESS_NOTIFICATIONS,
520 "NotificationManagerService.getHistoricalNotifications");
521
522 StatusBarNotification[] tmp = null;
523 int uid = Binder.getCallingUid();
524
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400525 // noteOp will check to make sure the callingPkg matches the uid
Daniel Sandler78d0d252013-02-12 08:14:52 -0500526 if (mAppOps.noteOpNoThrow(AppOpsManager.OP_ACCESS_NOTIFICATIONS, uid, callingPkg)
527 == AppOpsManager.MODE_ALLOWED) {
528 synchronized (mArchive) {
529 tmp = mArchive.getArray(count);
530 }
531 }
532 return tmp;
533 }
534
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700535 /**
536 * Called whenever packages change, the user switches, or ENABLED_NOTIFICATION_LISTENERS
537 * is altered. (For example in response to USER_SWITCHED in our broadcast receiver)
538 */
539 void rebindListenerServices() {
540 String flat = Settings.Secure.getString(
541 mContext.getContentResolver(),
542 Settings.Secure.ENABLED_NOTIFICATION_LISTENERS);
543
544 NotificationListenerInfo[] toRemove = new NotificationListenerInfo[mListeners.size()];
545 final ArrayList<ComponentName> toAdd;
546 final int currentUser = ActivityManager.getCurrentUser();
547
548 synchronized (mNotificationList) {
549 // unbind and remove all existing listeners
550 toRemove = mListeners.toArray(toRemove);
551
552 toAdd = new ArrayList<ComponentName>();
553 final HashSet<ComponentName> newEnabled = new HashSet<ComponentName>();
554 final HashSet<String> newPackages = new HashSet<String>();
555
556 // decode the list of components
557 if (flat != null) {
558 String[] components = flat.split(ENABLED_NOTIFICATION_LISTENERS_SEPARATOR);
559 for (int i=0; i<components.length; i++) {
560 final ComponentName component
561 = ComponentName.unflattenFromString(components[i]);
562 if (component != null) {
563 newEnabled.add(component);
564 toAdd.add(component);
565 newPackages.add(component.getPackageName());
566 }
567 }
568
569 mEnabledListenersForCurrentUser = newEnabled;
570 mEnabledListenerPackageNames = newPackages;
571 }
572 }
573
574 for (NotificationListenerInfo info : toRemove) {
575 final ComponentName component = info.component;
576 final int oldUser = info.userid;
577 Slog.v(TAG, "disabling notification listener for user " + oldUser + ": " + component);
578 unregisterListenerService(component, info.userid);
579 }
580
581 final int N = toAdd.size();
582 for (int i=0; i<N; i++) {
583 final ComponentName component = toAdd.get(i);
584 Slog.v(TAG, "enabling notification listener for user " + currentUser + ": "
585 + component);
586 registerListenerService(component, currentUser);
587 }
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400588 }
589
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700590 /**
591 * Register a listener binder directly with the notification manager.
592 *
593 * Only works with system callers. Apps should extend
594 * {@link android.service.notification.NotificationListenerService}.
595 */
Daniel Sandler09a247e2013-02-14 10:24:17 -0500596 @Override
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400597 public void registerListener(final INotificationListener listener,
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700598 final ComponentName component, final int userid) {
599 checkCallerIsSystem();
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400600
Daniel Sandler09a247e2013-02-14 10:24:17 -0500601 synchronized (mNotificationList) {
602 try {
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400603 NotificationListenerInfo info
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700604 = new NotificationListenerInfo(listener, component, userid, true);
Daniel Sandler09a247e2013-02-14 10:24:17 -0500605 listener.asBinder().linkToDeath(info, 0);
606 mListeners.add(info);
607 } catch (RemoteException e) {
608 // already dead
609 }
610 }
611 }
612
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700613 /**
614 * Version of registerListener that takes the name of a
615 * {@link android.service.notification.NotificationListenerService} to bind to.
616 *
617 * This is the mechanism by which third parties may subscribe to notifications.
618 */
619 private void registerListenerService(final ComponentName name, final int userid) {
620 checkCallerIsSystem();
621
622 if (DBG) Slog.v(TAG, "registerListenerService: " + name + " u=" + userid);
623
624 synchronized (mNotificationList) {
625 final String servicesBindingTag = name.toString() + "/" + userid;
626 if (mServicesBinding.contains(servicesBindingTag)) {
627 // stop registering this thing already! we're working on it
628 return;
629 }
630 mServicesBinding.add(servicesBindingTag);
631
632 final int N = mListeners.size();
633 for (int i=N-1; i>=0; i--) {
634 final NotificationListenerInfo info = mListeners.get(i);
635 if (name.equals(info.component)
636 && info.userid == userid) {
637 // cut old connections
638 if (DBG) Slog.v(TAG, " disconnecting old listener: " + info.listener);
639 mListeners.remove(i);
640 if (info.connection != null) {
641 mContext.unbindService(info.connection);
642 }
643 }
644 }
645
646 Intent intent = new Intent(NotificationListenerService.SERVICE_INTERFACE);
647 intent.setComponent(name);
648
649 intent.putExtra(Intent.EXTRA_CLIENT_LABEL,
650 com.android.internal.R.string.notification_listener_binding_label);
651 intent.putExtra(Intent.EXTRA_CLIENT_INTENT, PendingIntent.getActivity(
652 mContext, 0, new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS), 0));
653
654 try {
655 if (DBG) Slog.v(TAG, "binding: " + intent);
656 if (!mContext.bindServiceAsUser(intent,
657 new ServiceConnection() {
658 INotificationListener mListener;
659 @Override
660 public void onServiceConnected(ComponentName name, IBinder service) {
661 synchronized (mNotificationList) {
662 mServicesBinding.remove(servicesBindingTag);
663 try {
664 mListener = INotificationListener.Stub.asInterface(service);
665 NotificationListenerInfo info = new NotificationListenerInfo(
666 mListener, name, userid, this);
667 service.linkToDeath(info, 0);
668 mListeners.add(info);
669 } catch (RemoteException e) {
670 // already dead
671 }
672 }
673 }
674
675 @Override
676 public void onServiceDisconnected(ComponentName name) {
677 Slog.v(TAG, "notification listener connection lost: " + name);
678 }
679 },
680 Context.BIND_AUTO_CREATE,
681 new UserHandle(userid)))
682 {
683 mServicesBinding.remove(servicesBindingTag);
684 Slog.w(TAG, "Unable to bind listener service: " + intent);
685 return;
686 }
687 } catch (SecurityException ex) {
688 Slog.e(TAG, "Unable to bind listener service: " + intent, ex);
689 return;
690 }
691 }
692 }
693
694 /**
695 * Remove a listener binder directly
696 */
Daniel Sandler09a247e2013-02-14 10:24:17 -0500697 @Override
698 public void unregisterListener(INotificationListener listener, int userid) {
Daniel Sandler4b749ef2013-03-18 21:53:04 -0400699 // no need to check permissions; if your listener binder is in the list,
700 // that's proof that you had permission to add it in the first place
701
Daniel Sandler09a247e2013-02-14 10:24:17 -0500702 synchronized (mNotificationList) {
703 final int N = mListeners.size();
704 for (int i=N-1; i>=0; i--) {
705 final NotificationListenerInfo info = mListeners.get(i);
706 if (info.listener == listener && info.userid == userid) {
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700707 mListeners.remove(i);
708 if (info.connection != null) {
709 mContext.unbindService(info.connection);
710 }
Daniel Sandler09a247e2013-02-14 10:24:17 -0500711 }
712 }
713 }
714 }
715
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700716 /**
717 * Remove a listener service for the given user by ComponentName
718 */
719 private void unregisterListenerService(ComponentName name, int userid) {
720 checkCallerIsSystem();
721
722 synchronized (mNotificationList) {
723 final int N = mListeners.size();
724 for (int i=N-1; i>=0; i--) {
725 final NotificationListenerInfo info = mListeners.get(i);
726 if (name.equals(info.component)
727 && info.userid == userid) {
728 mListeners.remove(i);
729 if (info.connection != null) {
730 mContext.unbindService(info.connection);
731 }
732 }
733 }
734 }
735 }
736
737 /**
738 * asynchronously notify all listeners about a new notification
739 */
Daniel Sandler09a247e2013-02-14 10:24:17 -0500740 private void notifyPostedLocked(NotificationRecord n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -0400741 // make a copy in case changes are made to the underlying Notification object
742 final StatusBarNotification sbn = n.sbn.clone();
Daniel Sandler09a247e2013-02-14 10:24:17 -0500743 for (final NotificationListenerInfo info : mListeners) {
744 mHandler.post(new Runnable() {
745 @Override
746 public void run() {
747 info.notifyPostedIfUserMatch(sbn);
748 }});
749 }
750 }
751
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700752 /**
753 * asynchronously notify all listeners about a removed notification
754 */
Daniel Sandler09a247e2013-02-14 10:24:17 -0500755 private void notifyRemovedLocked(NotificationRecord n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -0400756 // make a copy in case changes are made to the underlying Notification object
757 // NOTE: this copy is lightweight: it doesn't include heavyweight parts of the notification
758 final StatusBarNotification sbn_light = n.sbn.cloneLight();
759
Daniel Sandler09a247e2013-02-14 10:24:17 -0500760 for (final NotificationListenerInfo info : mListeners) {
761 mHandler.post(new Runnable() {
762 @Override
763 public void run() {
Daniel Sandler1a497d32013-04-18 14:52:45 -0400764 info.notifyRemovedIfUserMatch(sbn_light);
Daniel Sandler09a247e2013-02-14 10:24:17 -0500765 }});
766 }
767 }
768
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700769 // -- APIs to support listeners clicking/clearing notifications --
770
771 private NotificationListenerInfo checkListenerToken(INotificationListener listener) {
772 final IBinder token = listener.asBinder();
773 final int N = mListeners.size();
774 for (int i=0; i<N; i++) {
775 final NotificationListenerInfo info = mListeners.get(i);
776 if (info.listener.asBinder() == token) return info;
777 }
778 throw new SecurityException("Disallowed call from unknown listener: " + listener);
779 }
780
781 /**
782 * Allow an INotificationListener to simulate a "clear all" operation.
783 *
784 * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onClearAllNotifications}
785 *
786 * @param token The binder for the listener, to check that the caller is allowed
787 */
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -0400788 public void cancelAllNotificationsFromListener(INotificationListener token) {
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700789 NotificationListenerInfo info = checkListenerToken(token);
790 long identity = Binder.clearCallingIdentity();
791 try {
792 cancelAll(info.userid);
793 } finally {
794 Binder.restoreCallingIdentity(identity);
795 }
796 }
797
798 /**
799 * Allow an INotificationListener to simulate clearing (dismissing) a single notification.
800 *
801 * {@see com.android.server.StatusBarManagerService.NotificationCallbacks#onNotificationClear}
802 *
803 * @param token The binder for the listener, to check that the caller is allowed
804 */
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -0400805 public void cancelNotificationFromListener(INotificationListener token, String pkg, String tag, int id) {
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700806 NotificationListenerInfo info = checkListenerToken(token);
807 long identity = Binder.clearCallingIdentity();
808 try {
809 cancelNotification(pkg, tag, id, 0,
810 Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE,
811 true,
812 info.userid);
813 } finally {
814 Binder.restoreCallingIdentity(identity);
815 }
816 }
817
Daniel Sandler25cf8ce2013-04-24 15:34:57 -0400818 /**
819 * Allow an INotificationListener to request the list of outstanding notifications seen by
820 * the current user. Useful when starting up, after which point the listener callbacks should
821 * be used.
822 *
823 * @param token The binder for the listener, to check that the caller is allowed
824 */
825 public StatusBarNotification[] getActiveNotificationsFromListener(INotificationListener token) {
826 NotificationListenerInfo info = checkListenerToken(token);
827
828 StatusBarNotification[] result = new StatusBarNotification[0];
829 ArrayList<StatusBarNotification> list = new ArrayList<StatusBarNotification>();
830 synchronized (mNotificationList) {
831 final int N = mNotificationList.size();
832 for (int i=0; i<N; i++) {
833 StatusBarNotification sbn = mNotificationList.get(i).sbn;
834 if (info.enabledAndUserMatches(sbn)) {
835 list.add(sbn);
836 }
837 }
838 }
839 return list.toArray(result);
840 }
841
Daniel Sandler5feceeb2013-03-22 18:29:23 -0700842 // -- end of listener APIs --
843
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500844 public static final class NotificationRecord
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 {
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500846 final StatusBarNotification sbn;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 IBinder statusBarKey;
848
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500849 NotificationRecord(StatusBarNotification sbn)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800850 {
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500851 this.sbn = sbn;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 }
Fred Quintana6ecaff12009-09-25 14:23:13 -0700853
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -0400854 public Notification getNotification() { return sbn.getNotification(); }
855 public int getFlags() { return sbn.getNotification().flags; }
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500856 public int getUserId() { return sbn.getUserId(); }
857
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800858 void dump(PrintWriter pw, String prefix, Context baseContext) {
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -0400859 final Notification notification = sbn.getNotification();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800860 pw.println(prefix + this);
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -0400861 pw.println(prefix + " uid=" + sbn.getUid() + " userId=" + sbn.getUserId());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -0400863 + " / " + idDebugString(baseContext, sbn.getPkg(), notification.icon));
864 pw.println(prefix + " pri=" + notification.priority + " score=" + sbn.getScore());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 pw.println(prefix + " contentIntent=" + notification.contentIntent);
866 pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
867 pw.println(prefix + " tickerText=" + notification.tickerText);
868 pw.println(prefix + " contentView=" + notification.contentView);
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400869 pw.println(prefix + String.format(" defaults=0x%08x flags=0x%08x",
870 notification.defaults, notification.flags));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 pw.println(prefix + " sound=" + notification.sound);
872 pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate));
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400873 pw.println(prefix + String.format(" led=0x%08x onMs=%d offMs=%d",
874 notification.ledARGB, notification.ledOnMS, notification.ledOffMS));
875 if (notification.actions != null && notification.actions.length > 0) {
876 pw.println(prefix + " actions={");
877 final int N = notification.actions.length;
878 for (int i=0; i<N; i++) {
879 final Notification.Action action = notification.actions[i];
880 pw.println(String.format("%s [%d] \"%s\" -> %s",
881 prefix,
882 i,
883 action.title,
884 action.actionIntent.toString()
885 ));
886 }
887 pw.println(prefix + " }");
888 }
889 if (notification.extras != null && notification.extras.size() > 0) {
890 pw.println(prefix + " extras={");
891 for (String key : notification.extras.keySet()) {
892 pw.print(prefix + " " + key + "=");
893 Object val = notification.extras.get(key);
894 if (val == null) {
895 pw.println("null");
896 } else {
897 pw.print(val.toString());
898 if (val instanceof Bitmap) {
899 pw.print(String.format(" (%dx%d)",
900 ((Bitmap) val).getWidth(),
901 ((Bitmap) val).getHeight()));
902 } else if (val.getClass().isArray()) {
903 pw.println(" {");
904 final int N = Array.getLength(val);
905 for (int i=0; i<N; i++) {
906 if (i > 0) pw.println(",");
907 pw.print(prefix + " " + Array.get(val, i));
908 }
909 pw.print("\n" + prefix + " }");
910 }
911 pw.println();
912 }
913 }
914 pw.println(prefix + " }");
915 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 @Override
Daniel Sandlerfde19b12013-01-17 00:21:05 -0500919 public final String toString() {
920 return String.format(
921 "NotificationRecord(0x%08x: pkg=%s user=%s id=%d tag=%s score=%d: %s)",
922 System.identityHashCode(this),
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -0400923 this.sbn.getPkg(), this.sbn.getUser(), this.sbn.getId(), this.sbn.getTag(),
924 this.sbn.getScore(), this.sbn.getNotification());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 }
926 }
927
928 private static final class ToastRecord
929 {
930 final int pid;
931 final String pkg;
932 final ITransientNotification callback;
933 int duration;
934
935 ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
936 {
937 this.pid = pid;
938 this.pkg = pkg;
939 this.callback = callback;
940 this.duration = duration;
941 }
942
943 void update(int duration) {
944 this.duration = duration;
945 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800946
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800947 void dump(PrintWriter pw, String prefix) {
948 pw.println(prefix + this);
949 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800950
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800951 @Override
952 public final String toString()
953 {
954 return "ToastRecord{"
955 + Integer.toHexString(System.identityHashCode(this))
956 + " pkg=" + pkg
957 + " callback=" + callback
958 + " duration=" + duration;
959 }
960 }
961
Joe Onorato089de882010-04-12 08:18:45 -0700962 private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks
963 = new StatusBarManagerService.NotificationCallbacks() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964
965 public void onSetDisabled(int status) {
966 synchronized (mNotificationList) {
967 mDisabledNotifications = status;
968 if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) {
969 // cancel whatever's going on
970 long identity = Binder.clearCallingIdentity();
971 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -0700972 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
973 if (player != null) {
974 player.stopAsync();
975 }
976 } catch (RemoteException e) {
977 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 Binder.restoreCallingIdentity(identity);
979 }
980
981 identity = Binder.clearCallingIdentity();
982 try {
983 mVibrator.cancel();
Jeff Sharkey098d5802012-04-26 17:30:34 -0700984 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 Binder.restoreCallingIdentity(identity);
986 }
987 }
988 }
989 }
990
991 public void onClearAll() {
Dianne Hackborn41203752012-08-31 14:05:51 -0700992 // XXX to be totally correct, the caller should tell us which user
993 // this is for.
994 cancelAll(ActivityManager.getCurrentUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800995 }
996
Fred Quintana6ecaff12009-09-25 14:23:13 -0700997 public void onNotificationClick(String pkg, String tag, int id) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700998 // XXX to be totally correct, the caller should tell us which user
999 // this is for.
Fred Quintana6ecaff12009-09-25 14:23:13 -07001000 cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL,
Dianne Hackborn41203752012-08-31 14:05:51 -07001001 Notification.FLAG_FOREGROUND_SERVICE, false,
1002 ActivityManager.getCurrentUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 }
1004
Daniel Sandler0f0b11c2010-08-04 15:54:58 -04001005 public void onNotificationClear(String pkg, String tag, int id) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001006 // XXX to be totally correct, the caller should tell us which user
1007 // this is for.
Joe Onorato46439ce2010-11-19 13:56:21 -08001008 cancelNotification(pkg, tag, id, 0,
1009 Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE,
Dianne Hackborn41203752012-08-31 14:05:51 -07001010 true, ActivityManager.getCurrentUser());
Daniel Sandler0f0b11c2010-08-04 15:54:58 -04001011 }
1012
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001013 public void onPanelRevealed() {
1014 synchronized (mNotificationList) {
1015 // sound
1016 mSoundNotification = null;
Jeff Sharkey098d5802012-04-26 17:30:34 -07001017
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 long identity = Binder.clearCallingIdentity();
1019 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001020 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
1021 if (player != null) {
1022 player.stopAsync();
1023 }
1024 } catch (RemoteException e) {
1025 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 Binder.restoreCallingIdentity(identity);
1027 }
1028
1029 // vibrate
1030 mVibrateNotification = null;
1031 identity = Binder.clearCallingIdentity();
1032 try {
1033 mVibrator.cancel();
Jeff Sharkey098d5802012-04-26 17:30:34 -07001034 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001035 Binder.restoreCallingIdentity(identity);
1036 }
1037
1038 // light
1039 mLights.clear();
1040 mLedNotification = null;
1041 updateLightsLocked();
1042 }
1043 }
Joe Onorato005847b2010-06-04 16:08:02 -04001044
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001045 public void onNotificationError(String pkg, String tag, int id,
1046 int uid, int initialPid, String message) {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -04001047 Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id
1048 + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")");
Dianne Hackborn41203752012-08-31 14:05:51 -07001049 // XXX to be totally correct, the caller should tell us which user
1050 // this is for.
1051 cancelNotification(pkg, tag, id, 0, 0, false, UserHandle.getUserId(uid));
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -07001052 long ident = Binder.clearCallingIdentity();
1053 try {
1054 ActivityManagerNative.getDefault().crashApplication(uid, initialPid, pkg,
1055 "Bad notification posted from package " + pkg
1056 + ": " + message);
1057 } catch (RemoteException e) {
1058 }
1059 Binder.restoreCallingIdentity(ident);
Joe Onorato005847b2010-06-04 16:08:02 -04001060 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 };
1062
1063 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
1064 @Override
1065 public void onReceive(Context context, Intent intent) {
1066 String action = intent.getAction();
1067
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001068 boolean queryRestart = false;
Daniel Sandler26ece572012-06-01 15:38:46 -04001069 boolean packageChanged = false;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001070
Mike Lockwood541c9942011-06-12 19:35:45 -04001071 if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001072 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Daniel Sandler26ece572012-06-01 15:38:46 -04001073 || (packageChanged=action.equals(Intent.ACTION_PACKAGE_CHANGED))
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001074 || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001075 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001076 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001077 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001078 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001079 } else if (queryRestart) {
1080 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001081 } else {
1082 Uri uri = intent.getData();
1083 if (uri == null) {
1084 return;
1085 }
1086 String pkgName = uri.getSchemeSpecificPart();
1087 if (pkgName == null) {
1088 return;
1089 }
Daniel Sandler26ece572012-06-01 15:38:46 -04001090 if (packageChanged) {
1091 // We cancel notifications for packages which have just been disabled
1092 final int enabled = mContext.getPackageManager()
1093 .getApplicationEnabledSetting(pkgName);
1094 if (enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED
1095 || enabled == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
1096 return;
1097 }
1098 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001099 pkgList = new String[]{pkgName};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001100 }
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001101
1102 boolean anyListenersInvolved = false;
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001103 if (pkgList != null && (pkgList.length > 0)) {
1104 for (String pkgName : pkgList) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001105 cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart,
1106 UserHandle.USER_ALL);
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001107 if (mEnabledListenerPackageNames.contains(pkgName)) {
1108 anyListenersInvolved = true;
1109 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001110 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 }
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001112
1113 if (anyListenersInvolved) {
1114 // make sure we're still bound to any of our
1115 // listeners who may have just upgraded
1116 rebindListenerServices();
1117 }
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001118 } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
1119 // Keep track of screen on/off state, but do not turn off the notification light
1120 // until user passes through the lock screen or views the notification.
1121 mScreenOn = true;
1122 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
1123 mScreenOn = false;
Daniel Sandlere96ffb12010-03-11 13:38:06 -05001124 } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001125 mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
1126 TelephonyManager.EXTRA_STATE_OFFHOOK));
Daniel Sandlere96ffb12010-03-11 13:38:06 -05001127 updateNotificationPulse();
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001128 } else if (action.equals(Intent.ACTION_USER_STOPPED)) {
1129 int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
1130 if (userHandle >= 0) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001131 cancelAllNotificationsInt(null, 0, 0, true, userHandle);
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001132 }
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001133 } else if (action.equals(Intent.ACTION_USER_PRESENT)) {
1134 // turn off LED when user passes through lock screen
1135 mNotificationLight.turnOff();
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001136 } else if (action.equals(Intent.ACTION_USER_SWITCHED)) {
1137 // reload per-user settings
1138 mSettingsObserver.update(null);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001139 }
1140 }
1141 };
1142
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001143 class SettingsObserver extends ContentObserver {
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001144 private final Uri NOTIFICATION_LIGHT_PULSE_URI
1145 = Settings.System.getUriFor(Settings.System.NOTIFICATION_LIGHT_PULSE);
1146
1147 private final Uri ENABLED_NOTIFICATION_LISTENERS_URI
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001148 = Settings.Secure.getUriFor(Settings.Secure.ENABLED_NOTIFICATION_LISTENERS);
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001149
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001150 SettingsObserver(Handler handler) {
1151 super(handler);
1152 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001153
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001154 void observe() {
1155 ContentResolver resolver = mContext.getContentResolver();
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001156 resolver.registerContentObserver(NOTIFICATION_LIGHT_PULSE_URI,
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001157 false, this, UserHandle.USER_ALL);
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001158 resolver.registerContentObserver(ENABLED_NOTIFICATION_LISTENERS_URI,
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001159 false, this, UserHandle.USER_ALL);
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001160 update(null);
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001161 }
1162
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001163 @Override public void onChange(boolean selfChange, Uri uri) {
1164 update(uri);
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001165 }
1166
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001167 public void update(Uri uri) {
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001168 ContentResolver resolver = mContext.getContentResolver();
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001169 if (uri == null || NOTIFICATION_LIGHT_PULSE_URI.equals(uri)) {
1170 boolean pulseEnabled = Settings.System.getInt(resolver,
1171 Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0;
1172 if (mNotificationPulseEnabled != pulseEnabled) {
1173 mNotificationPulseEnabled = pulseEnabled;
1174 updateNotificationPulse();
1175 }
1176 }
1177 if (uri == null || ENABLED_NOTIFICATION_LISTENERS_URI.equals(uri)) {
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001178 rebindListenerServices();
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001179 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001180 }
1181 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001182
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001183 private SettingsObserver mSettingsObserver;
1184
Daniel Sandleredbb3802012-11-13 20:49:47 -08001185 static long[] getLongArray(Resources r, int resid, int maxlen, long[] def) {
1186 int[] ar = r.getIntArray(resid);
1187 if (ar == null) {
1188 return def;
1189 }
1190 final int len = ar.length > maxlen ? maxlen : ar.length;
1191 long[] out = new long[len];
1192 for (int i=0; i<len; i++) {
1193 out[i] = ar[i];
1194 }
1195 return out;
1196 }
1197
Joe Onorato089de882010-04-12 08:18:45 -07001198 NotificationManagerService(Context context, StatusBarManagerService statusBar,
Mike Lockwood3a322132009-11-24 00:30:52 -05001199 LightsService lights)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 {
1201 super();
1202 mContext = context;
Jeff Brownc2346132012-04-13 01:55:38 -07001203 mVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 mAm = ActivityManagerNative.getDefault();
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001205 mUserManager = (UserManager)context.getSystemService(Context.USER_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 mToastQueue = new ArrayList<ToastRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 mHandler = new WorkerHandler();
San Mehat3ee13172010-02-04 20:54:43 -08001208
Daniel Sandler4a900ac2013-01-30 14:04:10 -05001209 mAppOps = (AppOpsManager)context.getSystemService(Context.APP_OPS_SERVICE);
1210
1211 importOldBlockDb();
Daniel Sandler0da673f2012-04-11 12:33:16 -04001212
Joe Onorato089de882010-04-12 08:18:45 -07001213 mStatusBar = statusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 statusBar.setNotificationCallbacks(mNotificationCallbacks);
1215
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001216 mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS);
1217 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
1218
Mike Lockwood670f9322010-01-20 12:13:36 -05001219 Resources resources = mContext.getResources();
1220 mDefaultNotificationColor = resources.getColor(
1221 com.android.internal.R.color.config_defaultNotificationColor);
1222 mDefaultNotificationLedOn = resources.getInteger(
1223 com.android.internal.R.integer.config_defaultNotificationLedOn);
1224 mDefaultNotificationLedOff = resources.getInteger(
1225 com.android.internal.R.integer.config_defaultNotificationLedOff);
1226
Daniel Sandleredbb3802012-11-13 20:49:47 -08001227 mDefaultVibrationPattern = getLongArray(resources,
1228 com.android.internal.R.array.config_defaultNotificationVibePattern,
1229 VIBRATE_PATTERN_MAXLEN,
1230 DEFAULT_VIBRATE_PATTERN);
1231
1232 mFallbackVibrationPattern = getLongArray(resources,
1233 com.android.internal.R.array.config_notificationFallbackVibePattern,
1234 VIBRATE_PATTERN_MAXLEN,
1235 DEFAULT_VIBRATE_PATTERN);
1236
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001237 // Don't start allowing notifications until the setup wizard has run once.
1238 // After that, including subsequent boots, init with notifications turned on.
1239 // This works on the first boot because the setup wizard will toggle this
1240 // flag at least once and we'll go back to 0 after that.
Jeff Brownbf6f6f92012-09-25 15:03:20 -07001241 if (0 == Settings.Global.getInt(mContext.getContentResolver(),
1242 Settings.Global.DEVICE_PROVISIONED, 0)) {
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001243 mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
1244 }
1245
Mike Lockwood35e16bf2010-11-30 19:53:36 -05001246 // register for various Intents
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001247 IntentFilter filter = new IntentFilter();
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001248 filter.addAction(Intent.ACTION_SCREEN_ON);
1249 filter.addAction(Intent.ACTION_SCREEN_OFF);
Daniel Sandlere96ffb12010-03-11 13:38:06 -05001250 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001251 filter.addAction(Intent.ACTION_USER_PRESENT);
Dianne Hackborn80a4af22012-08-27 19:18:31 -07001252 filter.addAction(Intent.ACTION_USER_STOPPED);
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001253 filter.addAction(Intent.ACTION_USER_SWITCHED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001255 IntentFilter pkgFilter = new IntentFilter();
1256 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
Daniel Sandleraac0eb02011-08-06 22:51:56 -04001257 pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001258 pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
1259 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
1260 pkgFilter.addDataScheme("package");
1261 mContext.registerReceiver(mIntentReceiver, pkgFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -08001262 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -08001263 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001264
Daniel Sandler4b749ef2013-03-18 21:53:04 -04001265 mSettingsObserver = new SettingsObserver(mHandler);
1266 mSettingsObserver.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 }
1268
Daniel Sandler4a900ac2013-01-30 14:04:10 -05001269 /**
1270 * Read the old XML-based app block database and import those blockages into the AppOps system.
1271 */
1272 private void importOldBlockDb() {
1273 loadBlockDb();
1274
1275 PackageManager pm = mContext.getPackageManager();
1276 for (String pkg : mBlockedPackages) {
1277 PackageInfo info = null;
1278 try {
1279 info = pm.getPackageInfo(pkg, 0);
1280 setNotificationsEnabledForPackage(pkg, info.applicationInfo.uid, false);
1281 } catch (NameNotFoundException e) {
1282 // forget you
1283 }
1284 }
1285 mBlockedPackages.clear();
1286 if (mPolicyFile != null) {
1287 mPolicyFile.delete();
1288 }
1289 }
1290
Joe Onorato30275482009-07-08 17:09:14 -07001291 void systemReady() {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001292 mAudioService = IAudioService.Stub.asInterface(
1293 ServiceManager.getService(Context.AUDIO_SERVICE));
1294
Joe Onorato30275482009-07-08 17:09:14 -07001295 // no beeping until we're basically done booting
1296 mSystemReady = true;
Daniel Sandler5feceeb2013-03-22 18:29:23 -07001297
1298 // make sure our listener services are properly bound
1299 rebindListenerServices();
Joe Onorato30275482009-07-08 17:09:14 -07001300 }
1301
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001302 // Toasts
1303 // ============================================================================
1304 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
1305 {
Daniel Sandlera7035902010-03-30 15:45:31 -04001306 if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307
1308 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001309 Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 return ;
1311 }
1312
Daniel Sandler0da673f2012-04-11 12:33:16 -04001313 final boolean isSystemToast = ("android".equals(pkg));
1314
Daniel Sandler4a900ac2013-01-30 14:04:10 -05001315 if (ENABLE_BLOCKED_TOASTS && !noteNotificationOp(pkg, Binder.getCallingUid())) {
1316 if (!isSystemToast) {
1317 Slog.e(TAG, "Suppressing toast from package " + pkg + " by user request.");
1318 return;
1319 }
Daniel Sandler0da673f2012-04-11 12:33:16 -04001320 }
1321
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001322 synchronized (mToastQueue) {
1323 int callingPid = Binder.getCallingPid();
1324 long callingId = Binder.clearCallingIdentity();
1325 try {
1326 ToastRecord record;
1327 int index = indexOfToastLocked(pkg, callback);
1328 // If it's already in the queue, we update it in place, we don't
1329 // move it to the end of the queue.
1330 if (index >= 0) {
1331 record = mToastQueue.get(index);
1332 record.update(duration);
1333 } else {
Vairavan Srinivasanf9eb06c2011-01-21 18:08:36 -08001334 // Limit the number of toasts that any given package except the android
1335 // package can enqueue. Prevents DOS attacks and deals with leaks.
Daniel Sandler0da673f2012-04-11 12:33:16 -04001336 if (!isSystemToast) {
Vairavan Srinivasanf9eb06c2011-01-21 18:08:36 -08001337 int count = 0;
1338 final int N = mToastQueue.size();
1339 for (int i=0; i<N; i++) {
1340 final ToastRecord r = mToastQueue.get(i);
1341 if (r.pkg.equals(pkg)) {
1342 count++;
1343 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
1344 Slog.e(TAG, "Package has already posted " + count
1345 + " toasts. Not showing more. Package=" + pkg);
1346 return;
1347 }
1348 }
1349 }
1350 }
1351
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001352 record = new ToastRecord(callingPid, pkg, callback, duration);
1353 mToastQueue.add(record);
1354 index = mToastQueue.size() - 1;
1355 keepProcessAliveLocked(callingPid);
1356 }
1357 // If it's at index 0, it's the current toast. It doesn't matter if it's
1358 // new or just been updated. Call back and tell it to show itself.
1359 // If the callback fails, this will remove it from the list, so don't
1360 // assume that it's valid after this.
1361 if (index == 0) {
1362 showNextToastLocked();
1363 }
1364 } finally {
1365 Binder.restoreCallingIdentity(callingId);
1366 }
1367 }
1368 }
1369
1370 public void cancelToast(String pkg, ITransientNotification callback) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001371 Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001372
1373 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001374 Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001375 return ;
1376 }
1377
1378 synchronized (mToastQueue) {
1379 long callingId = Binder.clearCallingIdentity();
1380 try {
1381 int index = indexOfToastLocked(pkg, callback);
1382 if (index >= 0) {
1383 cancelToastLocked(index);
1384 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001385 Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001386 }
1387 } finally {
1388 Binder.restoreCallingIdentity(callingId);
1389 }
1390 }
1391 }
1392
1393 private void showNextToastLocked() {
1394 ToastRecord record = mToastQueue.get(0);
1395 while (record != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001396 if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001397 try {
1398 record.callback.show();
1399 scheduleTimeoutLocked(record, false);
1400 return;
1401 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001402 Slog.w(TAG, "Object died trying to show notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 + " in package " + record.pkg);
1404 // remove it from the list and let the process die
1405 int index = mToastQueue.indexOf(record);
1406 if (index >= 0) {
1407 mToastQueue.remove(index);
1408 }
1409 keepProcessAliveLocked(record.pid);
1410 if (mToastQueue.size() > 0) {
1411 record = mToastQueue.get(0);
1412 } else {
1413 record = null;
1414 }
1415 }
1416 }
1417 }
1418
1419 private void cancelToastLocked(int index) {
1420 ToastRecord record = mToastQueue.get(index);
1421 try {
1422 record.callback.hide();
1423 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001424 Slog.w(TAG, "Object died trying to hide notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001425 + " in package " + record.pkg);
1426 // don't worry about this, we're about to remove it from
1427 // the list anyway
1428 }
1429 mToastQueue.remove(index);
1430 keepProcessAliveLocked(record.pid);
1431 if (mToastQueue.size() > 0) {
1432 // Show the next one. If the callback fails, this will remove
1433 // it from the list, so don't assume that the list hasn't changed
1434 // after this point.
1435 showNextToastLocked();
1436 }
1437 }
1438
1439 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
1440 {
1441 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
1442 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
1443 mHandler.removeCallbacksAndMessages(r);
1444 mHandler.sendMessageDelayed(m, delay);
1445 }
1446
1447 private void handleTimeout(ToastRecord record)
1448 {
Joe Onorato8a9b2202010-02-26 18:56:32 -08001449 if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 synchronized (mToastQueue) {
1451 int index = indexOfToastLocked(record.pkg, record.callback);
1452 if (index >= 0) {
1453 cancelToastLocked(index);
1454 }
1455 }
1456 }
1457
1458 // lock on mToastQueue
1459 private int indexOfToastLocked(String pkg, ITransientNotification callback)
1460 {
1461 IBinder cbak = callback.asBinder();
1462 ArrayList<ToastRecord> list = mToastQueue;
1463 int len = list.size();
1464 for (int i=0; i<len; i++) {
1465 ToastRecord r = list.get(i);
1466 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
1467 return i;
1468 }
1469 }
1470 return -1;
1471 }
1472
1473 // lock on mToastQueue
1474 private void keepProcessAliveLocked(int pid)
1475 {
1476 int toastCount = 0; // toasts from this pid
1477 ArrayList<ToastRecord> list = mToastQueue;
1478 int N = list.size();
1479 for (int i=0; i<N; i++) {
1480 ToastRecord r = list.get(i);
1481 if (r.pid == pid) {
1482 toastCount++;
1483 }
1484 }
1485 try {
1486 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
1487 } catch (RemoteException e) {
1488 // Shouldn't happen.
1489 }
1490 }
1491
1492 private final class WorkerHandler extends Handler
1493 {
1494 @Override
1495 public void handleMessage(Message msg)
1496 {
1497 switch (msg.what)
1498 {
1499 case MESSAGE_TIMEOUT:
1500 handleTimeout((ToastRecord)msg.obj);
1501 break;
1502 }
1503 }
1504 }
1505
1506
1507 // Notifications
1508 // ============================================================================
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001509 public void enqueueNotificationWithTag(String pkg, String basePkg, String tag, int id,
1510 Notification notification, int[] idOut, int userId)
Fred Quintana6ecaff12009-09-25 14:23:13 -07001511 {
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001512 enqueueNotificationInternal(pkg, basePkg, Binder.getCallingUid(), Binder.getCallingPid(),
Dianne Hackborn41203752012-08-31 14:05:51 -07001513 tag, id, notification, idOut, userId);
Daniel Sandlerd0a2f862010-08-03 15:29:31 -04001514 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001515
1516 private final static int clamp(int x, int low, int high) {
1517 return (x < low) ? low : ((x > high) ? high : x);
Daniel Sandlere40451a2011-02-03 14:51:35 -05001518 }
1519
Daniel Sandlerd0a2f862010-08-03 15:29:31 -04001520 // Not exposed via Binder; for system use only (otherwise malicious apps could spoof the
1521 // uid/pid of another application)
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001522 public void enqueueNotificationInternal(String pkg, String basePkg, int callingUid,
1523 int callingPid, String tag, int id, Notification notification, int[] idOut, int userId)
Daniel Sandlerd0a2f862010-08-03 15:29:31 -04001524 {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001525 if (DBG) {
1526 Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id + " notification=" + notification);
1527 }
1528 checkCallerIsSystemOrSameApp(pkg);
1529 final boolean isSystemNotification = ("android".equals(pkg));
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001530
Dianne Hackborn41203752012-08-31 14:05:51 -07001531 userId = ActivityManager.handleIncomingUser(callingPid,
Amith Yamasani2c7ebea2012-10-30 15:28:27 -07001532 callingUid, userId, true, false, "enqueueNotification", pkg);
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -07001533 final UserHandle user = new UserHandle(userId);
Dianne Hackborn41203752012-08-31 14:05:51 -07001534
Joe Onoratobd73d012010-06-04 11:44:54 -07001535 // Limit the number of notifications that any given package except the android
1536 // package can enqueue. Prevents DOS attacks and deals with leaks.
Daniel Sandler0da673f2012-04-11 12:33:16 -04001537 if (!isSystemNotification) {
Joe Onoratobd73d012010-06-04 11:44:54 -07001538 synchronized (mNotificationList) {
1539 int count = 0;
1540 final int N = mNotificationList.size();
1541 for (int i=0; i<N; i++) {
1542 final NotificationRecord r = mNotificationList.get(i);
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -04001543 if (r.sbn.getPkg().equals(pkg) && r.sbn.getUserId() == userId) {
Joe Onoratobd73d012010-06-04 11:44:54 -07001544 count++;
1545 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
1546 Slog.e(TAG, "Package has already posted " + count
1547 + " notifications. Not showing more. package=" + pkg);
1548 return;
1549 }
1550 }
1551 }
1552 }
1553 }
1554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 // This conditional is a dirty hack to limit the logging done on
1556 // behalf of the download manager without affecting other apps.
1557 if (!pkg.equals("com.android.providers.downloads")
1558 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Daniel Sandler321e9c52012-10-12 10:59:26 -07001559 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, tag, userId,
Daniel Sandlerb64cb882011-11-29 23:48:29 -05001560 notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001561 }
1562
1563 if (pkg == null || notification == null) {
1564 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
1565 + " id=" + id + " notification=" + notification);
1566 }
1567 if (notification.icon != 0) {
1568 if (notification.contentView == null) {
1569 throw new IllegalArgumentException("contentView required: pkg=" + pkg
1570 + " id=" + id + " notification=" + notification);
1571 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001572 }
1573
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001574 // === Scoring ===
Daniel Sandler0da673f2012-04-11 12:33:16 -04001575
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001576 // 0. Sanitize inputs
1577 notification.priority = clamp(notification.priority, Notification.PRIORITY_MIN, Notification.PRIORITY_MAX);
1578 // Migrate notification flags to scores
1579 if (0 != (notification.flags & Notification.FLAG_HIGH_PRIORITY)) {
1580 if (notification.priority < Notification.PRIORITY_MAX) notification.priority = Notification.PRIORITY_MAX;
Daniel Sandler49a2ad12012-03-28 15:46:39 -04001581 } else if (SCORE_ONGOING_HIGHER && 0 != (notification.flags & Notification.FLAG_ONGOING_EVENT)) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001582 if (notification.priority < Notification.PRIORITY_HIGH) notification.priority = Notification.PRIORITY_HIGH;
1583 }
Daniel Sandler0da673f2012-04-11 12:33:16 -04001584
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001585 // 1. initial score: buckets of 10, around the app
Daniel Sandler0da673f2012-04-11 12:33:16 -04001586 int score = notification.priority * NOTIFICATION_PRIORITY_MULTIPLIER; //[-20..20]
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001587
Daniel Sandler0da673f2012-04-11 12:33:16 -04001588 // 2. Consult external heuristics (TBD)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001589
Daniel Sandler0da673f2012-04-11 12:33:16 -04001590 // 3. Apply local rules
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001591
1592 // blocked apps
Daniel Sandler4a900ac2013-01-30 14:04:10 -05001593 if (ENABLE_BLOCKED_NOTIFICATIONS && !noteNotificationOp(pkg, callingUid)) {
1594 if (!isSystemNotification) {
1595 score = JUNK_SCORE;
1596 Slog.e(TAG, "Suppressing notification from package " + pkg + " by user request.");
1597 }
Daniel Sandler0da673f2012-04-11 12:33:16 -04001598 }
1599
1600 if (DBG) {
1601 Slog.v(TAG, "Assigned score=" + score + " to " + notification);
1602 }
1603
1604 if (score < SCORE_DISPLAY_THRESHOLD) {
1605 // Notification will be blocked because the score is too low.
1606 return;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001607 }
1608
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001609 // Should this notification make noise, vibe, or use the LED?
1610 final boolean canInterrupt = (score >= SCORE_INTERRUPTION_THRESHOLD);
1611
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001612 synchronized (mNotificationList) {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001613 final StatusBarNotification n = new StatusBarNotification(
1614 pkg, id, tag, callingUid, callingPid, score, notification, user);
1615 NotificationRecord r = new NotificationRecord(n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001616 NotificationRecord old = null;
1617
Dianne Hackborn41203752012-08-31 14:05:51 -07001618 int index = indexOfNotificationLocked(pkg, tag, id, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001619 if (index < 0) {
1620 mNotificationList.add(r);
1621 } else {
1622 old = mNotificationList.remove(index);
1623 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001624 // Make sure we don't lose the foreground service state.
1625 if (old != null) {
1626 notification.flags |=
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001627 old.getNotification().flags&Notification.FLAG_FOREGROUND_SERVICE;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001628 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001629 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001630
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001631 // Ensure if this is a foreground service that the proper additional
1632 // flags are set.
1633 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
1634 notification.flags |= Notification.FLAG_ONGOING_EVENT
1635 | Notification.FLAG_NO_CLEAR;
1636 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001637
Svetoslav Ganovc31ed392012-10-10 14:58:28 -07001638 final int currentUser;
1639 final long token = Binder.clearCallingIdentity();
1640 try {
1641 currentUser = ActivityManager.getCurrentUser();
1642 } finally {
1643 Binder.restoreCallingIdentity(token);
1644 }
1645
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001646 if (notification.icon != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001647 if (old != null && old.statusBarKey != null) {
1648 r.statusBarKey = old.statusBarKey;
1649 long identity = Binder.clearCallingIdentity();
1650 try {
Joe Onorato18e69df2010-05-17 22:26:12 -07001651 mStatusBar.updateNotification(r.statusBarKey, n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 }
1653 finally {
1654 Binder.restoreCallingIdentity(identity);
1655 }
1656 } else {
1657 long identity = Binder.clearCallingIdentity();
1658 try {
Joe Onorato18e69df2010-05-17 22:26:12 -07001659 r.statusBarKey = mStatusBar.addNotification(n);
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -04001660 if ((n.getNotification().flags & Notification.FLAG_SHOW_LIGHTS) != 0
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001661 && canInterrupt) {
Mike Lockwoodece18ef2012-02-13 20:42:19 -08001662 mAttentionLight.pulse();
1663 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 }
1665 finally {
1666 Binder.restoreCallingIdentity(identity);
1667 }
1668 }
Svetoslav Ganovc31ed392012-10-10 14:58:28 -07001669 // Send accessibility events only for the current user.
1670 if (currentUser == userId) {
1671 sendAccessibilityEvent(notification, pkg);
1672 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001673
Daniel Sandler09a247e2013-02-14 10:24:17 -05001674 notifyPostedLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001675 } else {
Daniel Sandlere40451a2011-02-03 14:51:35 -05001676 Slog.e(TAG, "Ignoring notification with icon==0: " + notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 if (old != null && old.statusBarKey != null) {
1678 long identity = Binder.clearCallingIdentity();
1679 try {
Joe Onorato0cbda992010-05-02 16:28:15 -07001680 mStatusBar.removeNotification(old.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 }
1682 finally {
1683 Binder.restoreCallingIdentity(identity);
1684 }
Daniel Sandler09a247e2013-02-14 10:24:17 -05001685
1686 notifyRemovedLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001687 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001688 return; // do not play sounds, show lights, etc. for invalid notifications
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001689 }
1690
1691 // If we're not supposed to beep, vibrate, etc. then don't.
1692 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
1693 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -07001694 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001695 && (r.getUserId() == UserHandle.USER_ALL ||
1696 (r.getUserId() == userId && r.getUserId() == currentUser))
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001697 && canInterrupt
Joe Onorato30275482009-07-08 17:09:14 -07001698 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -08001699
1700 final AudioManager audioManager = (AudioManager) mContext
1701 .getSystemService(Context.AUDIO_SERVICE);
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001702
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001703 // sound
Daniel Sandler31475232013-04-17 00:17:22 -04001704
1705 // should we use the default notification sound? (indicated either by DEFAULT_SOUND
1706 // or because notification.sound is pointing at Settings.System.NOTIFICATION_SOUND)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 final boolean useDefaultSound =
Daniel Sandler31475232013-04-17 00:17:22 -04001708 (notification.defaults & Notification.DEFAULT_SOUND) != 0
1709 || Settings.System.DEFAULT_NOTIFICATION_URI.equals(notification.sound);
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001710
1711 Uri soundUri = null;
1712 boolean hasValidSound = false;
1713
1714 if (useDefaultSound) {
1715 soundUri = Settings.System.DEFAULT_NOTIFICATION_URI;
1716
1717 // check to see if the default notification sound is silent
1718 ContentResolver resolver = mContext.getContentResolver();
1719 hasValidSound = Settings.System.getString(resolver,
1720 Settings.System.NOTIFICATION_SOUND) != null;
1721 } else if (notification.sound != null) {
1722 soundUri = notification.sound;
1723 hasValidSound = (soundUri != null);
1724 }
1725
1726 if (hasValidSound) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001727 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
1728 int audioStreamType;
1729 if (notification.audioStreamType >= 0) {
1730 audioStreamType = notification.audioStreamType;
1731 } else {
1732 audioStreamType = DEFAULT_STREAM_TYPE;
1733 }
1734 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -08001735 // do not play notifications if stream volume is 0
Jean-Michel Trivid6770542012-10-10 12:03:41 -07001736 // (typically because ringer mode is silent) or if speech recognition is active.
1737 if ((audioManager.getStreamVolume(audioStreamType) != 0)
1738 && !audioManager.isSpeechRecognitionActive()) {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001739 final long identity = Binder.clearCallingIdentity();
Eric Laurent524dc042009-11-27 05:07:55 -08001740 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001741 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
1742 if (player != null) {
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001743 player.playAsync(soundUri, user, looping, audioStreamType);
Jeff Sharkey098d5802012-04-26 17:30:34 -07001744 }
1745 } catch (RemoteException e) {
1746 } finally {
Eric Laurent524dc042009-11-27 05:07:55 -08001747 Binder.restoreCallingIdentity(identity);
1748 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001749 }
1750 }
1751
1752 // vibrate
Daniel Sandleredbb3802012-11-13 20:49:47 -08001753 // Does the notification want to specify its own vibration?
1754 final boolean hasCustomVibrate = notification.vibrate != null;
1755
David Agnew71789e12012-11-09 23:03:26 -05001756 // 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 -05001757 // and no other vibration is specified, we fall back to vibration
David Agnew71789e12012-11-09 23:03:26 -05001758 final boolean convertSoundToVibration =
Daniel Sandleredbb3802012-11-13 20:49:47 -08001759 !hasCustomVibrate
Daniel Sandlerd4d2de22012-11-14 11:25:46 -08001760 && hasValidSound
David Agnew71789e12012-11-09 23:03:26 -05001761 && (audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE);
1762
Daniel Sandler4a7a9b92012-11-20 12:59:41 -05001763 // The DEFAULT_VIBRATE flag trumps any custom vibration AND the fallback.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001764 final boolean useDefaultVibrate =
Daniel Sandleredbb3802012-11-13 20:49:47 -08001765 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
David Agnew71789e12012-11-09 23:03:26 -05001766
Daniel Sandleredbb3802012-11-13 20:49:47 -08001767 if ((useDefaultVibrate || convertSoundToVibration || hasCustomVibrate)
Eric Laurentbffc3d12012-05-07 17:43:49 -07001768 && !(audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 mVibrateNotification = r;
1770
Daniel Sandleredbb3802012-11-13 20:49:47 -08001771 if (useDefaultVibrate || convertSoundToVibration) {
1772 // Escalate privileges so we can use the vibrator even if the notifying app
1773 // does not have the VIBRATE permission.
1774 long identity = Binder.clearCallingIdentity();
1775 try {
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -04001776 mVibrator.vibrate(r.sbn.getUid(), r.sbn.getBasePkg(),
Dianne Hackbornf265ea92013-01-31 15:00:51 -08001777 useDefaultVibrate ? mDefaultVibrationPattern
1778 : mFallbackVibrationPattern,
Daniel Sandleredbb3802012-11-13 20:49:47 -08001779 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
1780 } finally {
1781 Binder.restoreCallingIdentity(identity);
1782 }
1783 } else if (notification.vibrate.length > 1) {
1784 // If you want your own vibration pattern, you need the VIBRATE permission
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -04001785 mVibrator.vibrate(r.sbn.getUid(), r.sbn.getBasePkg(), notification.vibrate,
Daniel Sandleredbb3802012-11-13 20:49:47 -08001786 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
1787 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001788 }
1789 }
1790
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791 // light
1792 // the most recent thing gets the light
1793 mLights.remove(old);
1794 if (mLedNotification == old) {
1795 mLedNotification = null;
1796 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001797 //Slog.i(TAG, "notification.lights="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001798 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
Daniel Sandler526fa0e2012-12-04 14:51:50 -05001799 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0
1800 && canInterrupt) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001801 mLights.add(r);
1802 updateLightsLocked();
1803 } else {
1804 if (old != null
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001805 && ((old.getFlags() & Notification.FLAG_SHOW_LIGHTS) != 0)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806 updateLightsLocked();
1807 }
1808 }
1809 }
1810
1811 idOut[0] = id;
1812 }
1813
Joe Onorato30275482009-07-08 17:09:14 -07001814 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -07001815 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
1816 if (!manager.isEnabled()) {
1817 return;
1818 }
1819
1820 AccessibilityEvent event =
1821 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
1822 event.setPackageName(packageName);
1823 event.setClassName(Notification.class.getName());
1824 event.setParcelableData(notification);
1825 CharSequence tickerText = notification.tickerText;
1826 if (!TextUtils.isEmpty(tickerText)) {
1827 event.getText().add(tickerText);
1828 }
1829
1830 manager.sendAccessibilityEvent(event);
1831 }
1832
Joe Onorato46439ce2010-11-19 13:56:21 -08001833 private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete) {
1834 // tell the app
1835 if (sendDelete) {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001836 if (r.getNotification().deleteIntent != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001837 try {
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001838 r.getNotification().deleteIntent.send();
Joe Onorato46439ce2010-11-19 13:56:21 -08001839 } catch (PendingIntent.CanceledException ex) {
1840 // do nothing - there's no relevant way to recover, and
1841 // no reason to let this propagate
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -04001842 Slog.w(TAG, "canceled PendingIntent for " + r.sbn.getPkg(), ex);
Joe Onorato46439ce2010-11-19 13:56:21 -08001843 }
1844 }
1845 }
1846
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 // status bar
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001848 if (r.getNotification().icon != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001849 long identity = Binder.clearCallingIdentity();
1850 try {
Joe Onorato0cbda992010-05-02 16:28:15 -07001851 mStatusBar.removeNotification(r.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001852 }
1853 finally {
1854 Binder.restoreCallingIdentity(identity);
1855 }
1856 r.statusBarKey = null;
Daniel Sandler09a247e2013-02-14 10:24:17 -05001857 notifyRemovedLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 }
1859
1860 // sound
1861 if (mSoundNotification == r) {
1862 mSoundNotification = null;
Jeff Sharkey098d5802012-04-26 17:30:34 -07001863 final long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001865 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
1866 if (player != null) {
1867 player.stopAsync();
1868 }
1869 } catch (RemoteException e) {
1870 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871 Binder.restoreCallingIdentity(identity);
1872 }
1873 }
1874
1875 // vibrate
1876 if (mVibrateNotification == r) {
1877 mVibrateNotification = null;
1878 long identity = Binder.clearCallingIdentity();
1879 try {
1880 mVibrator.cancel();
1881 }
1882 finally {
1883 Binder.restoreCallingIdentity(identity);
1884 }
1885 }
1886
1887 // light
1888 mLights.remove(r);
1889 if (mLedNotification == r) {
1890 mLedNotification = null;
1891 }
Daniel Sandler23d7c702013-03-07 16:32:06 -05001892
1893 // Save it for users of getHistoricalNotifications()
1894 mArchive.record(r.sbn);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895 }
1896
1897 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001898 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001899 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 */
Fred Quintana6ecaff12009-09-25 14:23:13 -07001901 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Dianne Hackborn41203752012-08-31 14:05:51 -07001902 int mustNotHaveFlags, boolean sendDelete, int userId) {
Daniel Sandler321e9c52012-10-12 10:59:26 -07001903 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, tag, userId,
Daniel Sandlerb64cb882011-11-29 23:48:29 -05001904 mustHaveFlags, mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905
1906 synchronized (mNotificationList) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001907 int index = indexOfNotificationLocked(pkg, tag, id, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001908 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07001909 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001910
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001911 if ((r.getNotification().flags & mustHaveFlags) != mustHaveFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001912 return;
1913 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001914 if ((r.getNotification().flags & mustNotHaveFlags) != 0) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001915 return;
1916 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 mNotificationList.remove(index);
1919
Joe Onorato46439ce2010-11-19 13:56:21 -08001920 cancelNotificationLocked(r, sendDelete);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001921 updateLightsLocked();
1922 }
1923 }
1924 }
1925
1926 /**
Daniel Sandler321e9c52012-10-12 10:59:26 -07001927 * Determine whether the userId applies to the notification in question, either because
1928 * they match exactly, or one of them is USER_ALL (which is treated as a wildcard).
1929 */
1930 private boolean notificationMatchesUserId(NotificationRecord r, int userId) {
1931 return
1932 // looking for USER_ALL notifications? match everything
1933 userId == UserHandle.USER_ALL
1934 // a notification sent to USER_ALL matches any query
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001935 || r.getUserId() == UserHandle.USER_ALL
Daniel Sandler321e9c52012-10-12 10:59:26 -07001936 // an exact user match
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001937 || r.getUserId() == userId;
Daniel Sandler321e9c52012-10-12 10:59:26 -07001938 }
1939
1940 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 * Cancels all notifications from a given package that have all of the
1942 * {@code mustHaveFlags}.
1943 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001944 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
Dianne Hackborn41203752012-08-31 14:05:51 -07001945 int mustNotHaveFlags, boolean doit, int userId) {
Daniel Sandler321e9c52012-10-12 10:59:26 -07001946 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, userId,
1947 mustHaveFlags, mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001948
1949 synchronized (mNotificationList) {
1950 final int N = mNotificationList.size();
1951 boolean canceledSomething = false;
1952 for (int i = N-1; i >= 0; --i) {
1953 NotificationRecord r = mNotificationList.get(i);
Daniel Sandler321e9c52012-10-12 10:59:26 -07001954 if (!notificationMatchesUserId(r, userId)) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001955 continue;
1956 }
Amith Yamasani5ec00e92012-11-07 16:58:30 -08001957 // Don't remove notifications to all, if there's no package name specified
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001958 if (r.getUserId() == UserHandle.USER_ALL && pkg == null) {
Amith Yamasani5ec00e92012-11-07 16:58:30 -08001959 continue;
1960 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001961 if ((r.getFlags() & mustHaveFlags) != mustHaveFlags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962 continue;
1963 }
Daniel Sandlerfde19b12013-01-17 00:21:05 -05001964 if ((r.getFlags() & mustNotHaveFlags) != 0) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001965 continue;
1966 }
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -04001967 if (pkg != null && !r.sbn.getPkg().equals(pkg)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001968 continue;
1969 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001970 canceledSomething = true;
1971 if (!doit) {
1972 return true;
1973 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001974 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08001975 cancelNotificationLocked(r, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001976 }
1977 if (canceledSomething) {
1978 updateLightsLocked();
1979 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001980 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 }
1982 }
1983
Dianne Hackborn41203752012-08-31 14:05:51 -07001984 public void cancelNotificationWithTag(String pkg, String tag, int id, int userId) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001985 checkCallerIsSystemOrSameApp(pkg);
Dianne Hackborn41203752012-08-31 14:05:51 -07001986 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Amith Yamasani2c7ebea2012-10-30 15:28:27 -07001987 Binder.getCallingUid(), userId, true, false, "cancelNotificationWithTag", pkg);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001988 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -07001989 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001990 Binder.getCallingUid() == Process.SYSTEM_UID
Dianne Hackborn41203752012-08-31 14:05:51 -07001991 ? 0 : Notification.FLAG_FOREGROUND_SERVICE, false, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 }
1993
Dianne Hackborn41203752012-08-31 14:05:51 -07001994 public void cancelAllNotifications(String pkg, int userId) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001995 checkCallerIsSystemOrSameApp(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001996
Dianne Hackborn41203752012-08-31 14:05:51 -07001997 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Amith Yamasani2c7ebea2012-10-30 15:28:27 -07001998 Binder.getCallingUid(), userId, true, false, "cancelAllNotifications", pkg);
Dianne Hackborn41203752012-08-31 14:05:51 -07001999
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07002000 // Calling from user space, don't allow the canceling of actively
2001 // running foreground services.
Dianne Hackborn41203752012-08-31 14:05:51 -07002002 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002003 }
2004
Daniel Sandler0da673f2012-04-11 12:33:16 -04002005 void checkCallerIsSystem() {
2006 int uid = Binder.getCallingUid();
Dianne Hackborn0c380492012-08-20 17:23:30 -07002007 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04002008 return;
2009 }
2010 throw new SecurityException("Disallowed call for uid " + uid);
2011 }
2012
2013 void checkCallerIsSystemOrSameApp(String pkg) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07002014 int uid = Binder.getCallingUid();
Dianne Hackborn0c380492012-08-20 17:23:30 -07002015 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07002016 return;
2017 }
2018 try {
Amith Yamasanif203aee2012-08-29 18:41:53 -07002019 ApplicationInfo ai = AppGlobals.getPackageManager().getApplicationInfo(
2020 pkg, 0, UserHandle.getCallingUserId());
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07002021 if (!UserHandle.isSameApp(ai.uid, uid)) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07002022 throw new SecurityException("Calling uid " + uid + " gave package"
2023 + pkg + " which is owned by uid " + ai.uid);
2024 }
Amith Yamasanif203aee2012-08-29 18:41:53 -07002025 } catch (RemoteException re) {
2026 throw new SecurityException("Unknown package " + pkg + "\n" + re);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07002027 }
2028 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002029
Dianne Hackborn41203752012-08-31 14:05:51 -07002030 void cancelAll(int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002031 synchronized (mNotificationList) {
2032 final int N = mNotificationList.size();
2033 for (int i=N-1; i>=0; i--) {
2034 NotificationRecord r = mNotificationList.get(i);
2035
Daniel Sandler321e9c52012-10-12 10:59:26 -07002036 if (!notificationMatchesUserId(r, userId)) {
Dianne Hackborn41203752012-08-31 14:05:51 -07002037 continue;
2038 }
2039
Daniel Sandlerfde19b12013-01-17 00:21:05 -05002040 if ((r.getFlags() & (Notification.FLAG_ONGOING_EVENT
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002041 | Notification.FLAG_NO_CLEAR)) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002042 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08002043 cancelNotificationLocked(r, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 }
2045 }
2046
2047 updateLightsLocked();
2048 }
2049 }
2050
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002051 // lock on mNotificationList
2052 private void updateLightsLocked()
2053 {
The Android Open Source Project10592532009-03-18 17:39:46 -07002054 // handle notification lights
2055 if (mLedNotification == null) {
2056 // get next notification, if any
2057 int n = mLights.size();
2058 if (n > 0) {
2059 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002060 }
2061 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05002062
Mike Lockwood63b5ad92011-08-30 09:55:30 -04002063 // Don't flash while we are in a call or screen is on
2064 if (mLedNotification == null || mInCall || mScreenOn) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05002065 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07002066 } else {
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -04002067 final Notification ledno = mLedNotification.sbn.getNotification();
Daniel Sandlerfde19b12013-01-17 00:21:05 -05002068 int ledARGB = ledno.ledARGB;
2069 int ledOnMS = ledno.ledOnMS;
2070 int ledOffMS = ledno.ledOffMS;
2071 if ((ledno.defaults & Notification.DEFAULT_LIGHTS) != 0) {
Mike Lockwood670f9322010-01-20 12:13:36 -05002072 ledARGB = mDefaultNotificationColor;
2073 ledOnMS = mDefaultNotificationLedOn;
2074 ledOffMS = mDefaultNotificationLedOff;
2075 }
2076 if (mNotificationPulseEnabled) {
2077 // pulse repeatedly
2078 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
2079 ledOnMS, ledOffMS);
Mike Lockwood670f9322010-01-20 12:13:36 -05002080 }
The Android Open Source Project10592532009-03-18 17:39:46 -07002081 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002082 }
2083
2084 // lock on mNotificationList
Dianne Hackborn41203752012-08-31 14:05:51 -07002085 private int indexOfNotificationLocked(String pkg, String tag, int id, int userId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 {
2087 ArrayList<NotificationRecord> list = mNotificationList;
2088 final int len = list.size();
2089 for (int i=0; i<len; i++) {
2090 NotificationRecord r = list.get(i);
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -04002091 if (!notificationMatchesUserId(r, userId) || r.sbn.getId() != id) {
Dianne Hackborn41203752012-08-31 14:05:51 -07002092 continue;
2093 }
Fred Quintana6ecaff12009-09-25 14:23:13 -07002094 if (tag == null) {
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -04002095 if (r.sbn.getTag() != null) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07002096 continue;
2097 }
2098 } else {
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -04002099 if (!tag.equals(r.sbn.getTag())) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07002100 continue;
2101 }
2102 }
Daniel Sandlere6f7f2e2013-04-25 15:44:16 -04002103 if (r.sbn.getPkg().equals(pkg)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002104 return i;
2105 }
2106 }
2107 return -1;
2108 }
2109
Mike Lockwoodc22404a2009-12-02 11:15:02 -05002110 private void updateNotificationPulse() {
2111 synchronized (mNotificationList) {
2112 updateLightsLocked();
2113 }
2114 }
2115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002116 // ======================================================================
2117 @Override
2118 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
2119 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
2120 != PackageManager.PERMISSION_GRANTED) {
2121 pw.println("Permission Denial: can't dump NotificationManager from from pid="
2122 + Binder.getCallingPid()
2123 + ", uid=" + Binder.getCallingUid());
2124 return;
2125 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127 pw.println("Current Notification Manager state:");
2128
Daniel Sandler5feceeb2013-03-22 18:29:23 -07002129 pw.println(" Listeners (" + mEnabledListenersForCurrentUser.size()
2130 + ") enabled for current user:");
2131 for (ComponentName cmpt : mEnabledListenersForCurrentUser) {
2132 pw.println(" " + cmpt);
Daniel Sandler4b749ef2013-03-18 21:53:04 -04002133 }
Daniel Sandler4b749ef2013-03-18 21:53:04 -04002134
Daniel Sandler5feceeb2013-03-22 18:29:23 -07002135 pw.println(" Live listeners (" + mListeners.size() + "):");
Daniel Sandler4b749ef2013-03-18 21:53:04 -04002136 for (NotificationListenerInfo info : mListeners) {
Daniel Sandler5feceeb2013-03-22 18:29:23 -07002137 pw.println(" " + info.component
2138 + " (user " + info.userid + "): " + info.listener
2139 + (info.isSystem?" SYSTEM":""));
Daniel Sandler4b749ef2013-03-18 21:53:04 -04002140 }
2141
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002142 int N;
2143
2144 synchronized (mToastQueue) {
2145 N = mToastQueue.size();
2146 if (N > 0) {
2147 pw.println(" Toast Queue:");
2148 for (int i=0; i<N; i++) {
2149 mToastQueue.get(i).dump(pw, " ");
2150 }
2151 pw.println(" ");
2152 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002154 }
2155
2156 synchronized (mNotificationList) {
2157 N = mNotificationList.size();
2158 if (N > 0) {
2159 pw.println(" Notification List:");
2160 for (int i=0; i<N; i++) {
2161 mNotificationList.get(i).dump(pw, " ", mContext);
2162 }
2163 pw.println(" ");
2164 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002165
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002166 N = mLights.size();
2167 if (N > 0) {
2168 pw.println(" Lights List:");
2169 for (int i=0; i<N; i++) {
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002170 pw.println(" " + mLights.get(i));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002171 }
2172 pw.println(" ");
2173 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08002174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 pw.println(" mSoundNotification=" + mSoundNotification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002176 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04002177 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
2178 pw.println(" mSystemReady=" + mSystemReady);
Daniel Sandler5e62e3a2013-04-15 20:57:02 -04002179 pw.println(" mArchive=" + mArchive.toString());
2180 Iterator<StatusBarNotification> iter = mArchive.descendingIterator();
2181 int i=0;
2182 while (iter.hasNext()) {
2183 pw.println(" " + iter.next());
2184 if (++i >= 5) {
2185 if (iter.hasNext()) pw.println(" ...");
2186 break;
2187 }
2188 }
2189
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002190 }
2191 }
2192}