blob: 70d37bfc82851f83b313ec869c0226dea616abdd [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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.app.IActivityManager;
27import android.app.INotificationManager;
28import android.app.ITransientNotification;
29import android.app.Notification;
30import android.app.PendingIntent;
31import android.app.StatusBarManager;
32import android.content.BroadcastReceiver;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070033import android.content.ContentResolver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080034import android.content.Context;
35import android.content.Intent;
36import android.content.IntentFilter;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070037import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.content.pm.PackageManager;
39import android.content.pm.PackageManager.NameNotFoundException;
40import android.content.res.Resources;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070041import android.database.ContentObserver;
svetoslavganov75986cf2009-05-14 22:28:01 -070042import android.media.AudioManager;
Jeff Sharkey098d5802012-04-26 17:30:34 -070043import android.media.IAudioService;
44import android.media.IRingtonePlayer;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.os.Binder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.os.Handler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.os.IBinder;
49import android.os.Message;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070050import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070051import android.os.RemoteException;
Jeff Sharkey098d5802012-04-26 17:30:34 -070052import android.os.ServiceManager;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070053import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.os.Vibrator;
55import android.provider.Settings;
Daniel Sandlere96ffb12010-03-11 13:38:06 -050056import android.telephony.TelephonyManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.text.TextUtils;
Dianne Hackborn39606a02012-07-31 17:54:35 -070058import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059import android.util.EventLog;
60import android.util.Log;
Andy Stadler110988c2010-12-03 14:29:16 -080061import android.util.Slog;
Daniel Sandler0da673f2012-04-11 12:33:16 -040062import android.util.Xml;
svetoslavganov75986cf2009-05-14 22:28:01 -070063import android.view.accessibility.AccessibilityEvent;
64import android.view.accessibility.AccessibilityManager;
Jeff Sharkey6d515712012-09-20 16:06:08 -070065import android.widget.RemoteViews;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import android.widget.Toast;
67
Jeff Sharkey098d5802012-04-26 17:30:34 -070068import com.android.internal.statusbar.StatusBarNotification;
69import com.android.internal.util.FastXmlSerializer;
70
71import org.xmlpull.v1.XmlPullParser;
72import org.xmlpull.v1.XmlPullParserException;
73import org.xmlpull.v1.XmlSerializer;
74
Daniel Sandler0da673f2012-04-11 12:33:16 -040075import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import java.io.FileDescriptor;
Daniel Sandler0da673f2012-04-11 12:33:16 -040077import java.io.FileInputStream;
78import java.io.FileNotFoundException;
79import java.io.FileOutputStream;
80import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081import java.io.PrintWriter;
82import java.util.ArrayList;
83import java.util.Arrays;
Daniel Sandler0da673f2012-04-11 12:33:16 -040084import java.util.HashSet;
85
86import libcore.io.IoUtils;
87
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088
Daniel Sandlerd0a2f862010-08-03 15:29:31 -040089/** {@hide} */
90public class NotificationManagerService extends INotificationManager.Stub
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091{
92 private static final String TAG = "NotificationService";
93 private static final boolean DBG = false;
94
Joe Onoratobd73d012010-06-04 11:44:54 -070095 private static final int MAX_PACKAGE_NOTIFICATIONS = 50;
96
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 // message codes
98 private static final int MESSAGE_TIMEOUT = 2;
99
100 private static final int LONG_DELAY = 3500; // 3.5 seconds
101 private static final int SHORT_DELAY = 2000; // 2 seconds
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800102
103 private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250};
Daniel Sandleraaec7262012-11-13 20:49:47 -0800104 private static final int VIBRATE_PATTERN_MAXLEN = 8 * 2 + 1; // up to eight bumps
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105
106 private static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION;
Daniel Sandler49a2ad12012-03-28 15:46:39 -0400107 private static final boolean SCORE_ONGOING_HIGHER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108
Daniel Sandler0da673f2012-04-11 12:33:16 -0400109 private static final int JUNK_SCORE = -1000;
110 private static final int NOTIFICATION_PRIORITY_MULTIPLIER = 10;
111 private static final int SCORE_DISPLAY_THRESHOLD = Notification.PRIORITY_MIN * NOTIFICATION_PRIORITY_MULTIPLIER;
112
113 private static final boolean ENABLE_BLOCKED_NOTIFICATIONS = true;
114 private static final boolean ENABLE_BLOCKED_TOASTS = true;
115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 final Context mContext;
117 final IActivityManager mAm;
118 final IBinder mForegroundToken = new Binder();
119
120 private WorkerHandler mHandler;
Joe Onorato089de882010-04-12 08:18:45 -0700121 private StatusBarManagerService mStatusBar;
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500122 private LightsService.Light mNotificationLight;
123 private LightsService.Light mAttentionLight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124
Mike Lockwood670f9322010-01-20 12:13:36 -0500125 private int mDefaultNotificationColor;
126 private int mDefaultNotificationLedOn;
127 private int mDefaultNotificationLedOff;
128
Daniel Sandleraaec7262012-11-13 20:49:47 -0800129 private long[] mDefaultVibrationPattern;
130 private long[] mFallbackVibrationPattern;
131
Joe Onorato30275482009-07-08 17:09:14 -0700132 private boolean mSystemReady;
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400133 private int mDisabledNotifications;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134
Jeff Sharkey098d5802012-04-26 17:30:34 -0700135 private NotificationRecord mSoundNotification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 private NotificationRecord mVibrateNotification;
Jeff Sharkey098d5802012-04-26 17:30:34 -0700137
138 private IAudioService mAudioService;
Jeff Brownc2346132012-04-13 01:55:38 -0700139 private Vibrator mVibrator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500141 // for enabling and disabling notification pulse behavior
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400142 private boolean mScreenOn = true;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500143 private boolean mInCall = false;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500144 private boolean mNotificationPulseEnabled;
145
Fred Quintana6ecaff12009-09-25 14:23:13 -0700146 private final ArrayList<NotificationRecord> mNotificationList =
147 new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148
149 private ArrayList<ToastRecord> mToastQueue;
150
151 private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 private NotificationRecord mLedNotification;
svetoslavganov75986cf2009-05-14 22:28:01 -0700153
Daniel Sandler0da673f2012-04-11 12:33:16 -0400154 // Notification control database. For now just contains disabled packages.
155 private AtomicFile mPolicyFile;
156 private HashSet<String> mBlockedPackages = new HashSet<String>();
157
158 private static final int DB_VERSION = 1;
159
160 private static final String TAG_BODY = "notification-policy";
161 private static final String ATTR_VERSION = "version";
162
163 private static final String TAG_BLOCKED_PKGS = "blocked-packages";
164 private static final String TAG_PACKAGE = "package";
165 private static final String ATTR_NAME = "name";
166
167 private void loadBlockDb() {
168 synchronized(mBlockedPackages) {
169 if (mPolicyFile == null) {
170 File dir = new File("/data/system");
171 mPolicyFile = new AtomicFile(new File(dir, "notification_policy.xml"));
172
173 mBlockedPackages.clear();
174
175 FileInputStream infile = null;
176 try {
177 infile = mPolicyFile.openRead();
178 final XmlPullParser parser = Xml.newPullParser();
179 parser.setInput(infile, null);
180
181 int type;
182 String tag;
183 int version = DB_VERSION;
184 while ((type = parser.next()) != END_DOCUMENT) {
185 tag = parser.getName();
186 if (type == START_TAG) {
187 if (TAG_BODY.equals(tag)) {
188 version = Integer.parseInt(parser.getAttributeValue(null, ATTR_VERSION));
189 } else if (TAG_BLOCKED_PKGS.equals(tag)) {
190 while ((type = parser.next()) != END_DOCUMENT) {
191 tag = parser.getName();
192 if (TAG_PACKAGE.equals(tag)) {
193 mBlockedPackages.add(parser.getAttributeValue(null, ATTR_NAME));
194 } else if (TAG_BLOCKED_PKGS.equals(tag) && type == END_TAG) {
195 break;
196 }
197 }
198 }
199 }
200 }
201 } catch (FileNotFoundException e) {
202 // No data yet
203 } catch (IOException e) {
204 Log.wtf(TAG, "Unable to read blocked notifications database", e);
205 } catch (NumberFormatException e) {
206 Log.wtf(TAG, "Unable to parse blocked notifications database", e);
207 } catch (XmlPullParserException e) {
208 Log.wtf(TAG, "Unable to parse blocked notifications database", e);
209 } finally {
210 IoUtils.closeQuietly(infile);
211 }
212 }
213 }
214 }
215
216 private void writeBlockDb() {
217 synchronized(mBlockedPackages) {
218 FileOutputStream outfile = null;
219 try {
220 outfile = mPolicyFile.startWrite();
221
222 XmlSerializer out = new FastXmlSerializer();
223 out.setOutput(outfile, "utf-8");
224
225 out.startDocument(null, true);
226
227 out.startTag(null, TAG_BODY); {
228 out.attribute(null, ATTR_VERSION, String.valueOf(DB_VERSION));
229 out.startTag(null, TAG_BLOCKED_PKGS); {
230 // write all known network policies
231 for (String pkg : mBlockedPackages) {
232 out.startTag(null, TAG_PACKAGE); {
233 out.attribute(null, ATTR_NAME, pkg);
234 } out.endTag(null, TAG_PACKAGE);
235 }
236 } out.endTag(null, TAG_BLOCKED_PKGS);
237 } out.endTag(null, TAG_BODY);
238
239 out.endDocument();
240
241 mPolicyFile.finishWrite(outfile);
242 } catch (IOException e) {
243 if (outfile != null) {
244 mPolicyFile.failWrite(outfile);
245 }
246 }
247 }
248 }
249
250 public boolean areNotificationsEnabledForPackage(String pkg) {
251 checkCallerIsSystem();
252 return areNotificationsEnabledForPackageInt(pkg);
253 }
254
255 // Unchecked. Not exposed via Binder, but can be called in the course of enqueue*().
256 private boolean areNotificationsEnabledForPackageInt(String pkg) {
257 final boolean enabled = !mBlockedPackages.contains(pkg);
258 if (DBG) {
259 Slog.v(TAG, "notifications are " + (enabled?"en":"dis") + "abled for " + pkg);
260 }
261 return enabled;
262 }
263
264 public void setNotificationsEnabledForPackage(String pkg, boolean enabled) {
265 checkCallerIsSystem();
266 if (DBG) {
267 Slog.v(TAG, (enabled?"en":"dis") + "abling notifications for " + pkg);
268 }
269 if (enabled) {
270 mBlockedPackages.remove(pkg);
271 } else {
272 mBlockedPackages.add(pkg);
273
274 // Now, cancel any outstanding notifications that are part of a just-disabled app
275 if (ENABLE_BLOCKED_NOTIFICATIONS) {
276 synchronized (mNotificationList) {
277 final int N = mNotificationList.size();
278 for (int i=0; i<N; i++) {
279 final NotificationRecord r = mNotificationList.get(i);
280 if (r.pkg.equals(pkg)) {
281 cancelNotificationLocked(r, false);
282 }
283 }
284 }
285 }
286 // Don't bother canceling toasts, they'll go away soon enough.
287 }
288 writeBlockDb();
289 }
290
291
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 private static String idDebugString(Context baseContext, String packageName, int id) {
293 Context c = null;
294
295 if (packageName != null) {
296 try {
297 c = baseContext.createPackageContext(packageName, 0);
298 } catch (NameNotFoundException e) {
299 c = baseContext;
300 }
301 } else {
302 c = baseContext;
303 }
304
305 String pkg;
306 String type;
307 String name;
308
309 Resources r = c.getResources();
310 try {
311 return r.getResourceName(id);
312 } catch (Resources.NotFoundException e) {
313 return "<name unknown>";
314 }
315 }
316
317 private static final class NotificationRecord
318 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700319 final String pkg;
320 final String tag;
321 final int id;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700322 final int uid;
323 final int initialPid;
Dianne Hackborn41203752012-08-31 14:05:51 -0700324 final int userId;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700325 final Notification notification;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500326 final int score;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 IBinder statusBarKey;
328
Dianne Hackborn41203752012-08-31 14:05:51 -0700329 NotificationRecord(String pkg, String tag, int id, int uid, int initialPid,
330 int userId, int score, Notification notification)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 {
332 this.pkg = pkg;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700333 this.tag = tag;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 this.id = id;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700335 this.uid = uid;
336 this.initialPid = initialPid;
Dianne Hackborn41203752012-08-31 14:05:51 -0700337 this.userId = userId;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500338 this.score = score;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 this.notification = notification;
340 }
Fred Quintana6ecaff12009-09-25 14:23:13 -0700341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 void dump(PrintWriter pw, String prefix, Context baseContext) {
343 pw.println(prefix + this);
344 pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
345 + " / " + idDebugString(baseContext, this.pkg, notification.icon));
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500346 pw.println(prefix + " pri=" + notification.priority);
347 pw.println(prefix + " score=" + this.score);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 pw.println(prefix + " contentIntent=" + notification.contentIntent);
349 pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
350 pw.println(prefix + " tickerText=" + notification.tickerText);
351 pw.println(prefix + " contentView=" + notification.contentView);
Dianne Hackborn41203752012-08-31 14:05:51 -0700352 pw.println(prefix + " uid=" + uid + " userId=" + userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 pw.println(prefix + " defaults=0x" + Integer.toHexString(notification.defaults));
354 pw.println(prefix + " flags=0x" + Integer.toHexString(notification.flags));
355 pw.println(prefix + " sound=" + notification.sound);
356 pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate));
357 pw.println(prefix + " ledARGB=0x" + Integer.toHexString(notification.ledARGB)
358 + " ledOnMS=" + notification.ledOnMS
359 + " ledOffMS=" + notification.ledOffMS);
360 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 @Override
363 public final String toString()
364 {
365 return "NotificationRecord{"
366 + Integer.toHexString(System.identityHashCode(this))
367 + " pkg=" + pkg
Fred Quintana6ecaff12009-09-25 14:23:13 -0700368 + " id=" + Integer.toHexString(id)
Daniel Sandlere40451a2011-02-03 14:51:35 -0500369 + " tag=" + tag
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500370 + " score=" + score
Daniel Sandlere40451a2011-02-03 14:51:35 -0500371 + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 }
373 }
374
375 private static final class ToastRecord
376 {
377 final int pid;
378 final String pkg;
379 final ITransientNotification callback;
380 int duration;
381
382 ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
383 {
384 this.pid = pid;
385 this.pkg = pkg;
386 this.callback = callback;
387 this.duration = duration;
388 }
389
390 void update(int duration) {
391 this.duration = duration;
392 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800393
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800394 void dump(PrintWriter pw, String prefix) {
395 pw.println(prefix + this);
396 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800397
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 @Override
399 public final String toString()
400 {
401 return "ToastRecord{"
402 + Integer.toHexString(System.identityHashCode(this))
403 + " pkg=" + pkg
404 + " callback=" + callback
405 + " duration=" + duration;
406 }
407 }
408
Joe Onorato089de882010-04-12 08:18:45 -0700409 private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks
410 = new StatusBarManagerService.NotificationCallbacks() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411
412 public void onSetDisabled(int status) {
413 synchronized (mNotificationList) {
414 mDisabledNotifications = status;
415 if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) {
416 // cancel whatever's going on
417 long identity = Binder.clearCallingIdentity();
418 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -0700419 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
420 if (player != null) {
421 player.stopAsync();
422 }
423 } catch (RemoteException e) {
424 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 Binder.restoreCallingIdentity(identity);
426 }
427
428 identity = Binder.clearCallingIdentity();
429 try {
430 mVibrator.cancel();
Jeff Sharkey098d5802012-04-26 17:30:34 -0700431 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 Binder.restoreCallingIdentity(identity);
433 }
434 }
435 }
436 }
437
438 public void onClearAll() {
Dianne Hackborn41203752012-08-31 14:05:51 -0700439 // XXX to be totally correct, the caller should tell us which user
440 // this is for.
441 cancelAll(ActivityManager.getCurrentUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 }
443
Fred Quintana6ecaff12009-09-25 14:23:13 -0700444 public void onNotificationClick(String pkg, String tag, int id) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700445 // XXX to be totally correct, the caller should tell us which user
446 // this is for.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700447 cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL,
Dianne Hackborn41203752012-08-31 14:05:51 -0700448 Notification.FLAG_FOREGROUND_SERVICE, false,
449 ActivityManager.getCurrentUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 }
451
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400452 public void onNotificationClear(String pkg, String tag, int id) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700453 // XXX to be totally correct, the caller should tell us which user
454 // this is for.
Joe Onorato46439ce2010-11-19 13:56:21 -0800455 cancelNotification(pkg, tag, id, 0,
456 Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE,
Dianne Hackborn41203752012-08-31 14:05:51 -0700457 true, ActivityManager.getCurrentUser());
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400458 }
459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 public void onPanelRevealed() {
461 synchronized (mNotificationList) {
462 // sound
463 mSoundNotification = null;
Jeff Sharkey098d5802012-04-26 17:30:34 -0700464
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 long identity = Binder.clearCallingIdentity();
466 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -0700467 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
468 if (player != null) {
469 player.stopAsync();
470 }
471 } catch (RemoteException e) {
472 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 Binder.restoreCallingIdentity(identity);
474 }
475
476 // vibrate
477 mVibrateNotification = null;
478 identity = Binder.clearCallingIdentity();
479 try {
480 mVibrator.cancel();
Jeff Sharkey098d5802012-04-26 17:30:34 -0700481 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 Binder.restoreCallingIdentity(identity);
483 }
484
485 // light
486 mLights.clear();
487 mLedNotification = null;
488 updateLightsLocked();
489 }
490 }
Joe Onorato005847b2010-06-04 16:08:02 -0400491
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700492 public void onNotificationError(String pkg, String tag, int id,
493 int uid, int initialPid, String message) {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400494 Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id
495 + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")");
Dianne Hackborn41203752012-08-31 14:05:51 -0700496 // XXX to be totally correct, the caller should tell us which user
497 // this is for.
498 cancelNotification(pkg, tag, id, 0, 0, false, UserHandle.getUserId(uid));
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700499 long ident = Binder.clearCallingIdentity();
500 try {
501 ActivityManagerNative.getDefault().crashApplication(uid, initialPid, pkg,
502 "Bad notification posted from package " + pkg
503 + ": " + message);
504 } catch (RemoteException e) {
505 }
506 Binder.restoreCallingIdentity(ident);
Joe Onorato005847b2010-06-04 16:08:02 -0400507 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 };
509
510 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
511 @Override
512 public void onReceive(Context context, Intent intent) {
513 String action = intent.getAction();
514
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800515 boolean queryRestart = false;
Daniel Sandler26ece572012-06-01 15:38:46 -0400516 boolean packageChanged = false;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800517
Mike Lockwood541c9942011-06-12 19:35:45 -0400518 if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800519 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Daniel Sandler26ece572012-06-01 15:38:46 -0400520 || (packageChanged=action.equals(Intent.ACTION_PACKAGE_CHANGED))
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800521 || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800522 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800523 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800524 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800525 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800526 } else if (queryRestart) {
527 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800528 } else {
529 Uri uri = intent.getData();
530 if (uri == null) {
531 return;
532 }
533 String pkgName = uri.getSchemeSpecificPart();
534 if (pkgName == null) {
535 return;
536 }
Daniel Sandler26ece572012-06-01 15:38:46 -0400537 if (packageChanged) {
538 // We cancel notifications for packages which have just been disabled
539 final int enabled = mContext.getPackageManager()
540 .getApplicationEnabledSetting(pkgName);
541 if (enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED
542 || enabled == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
543 return;
544 }
545 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800546 pkgList = new String[]{pkgName};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800547 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800548 if (pkgList != null && (pkgList.length > 0)) {
549 for (String pkgName : pkgList) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700550 cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart,
551 UserHandle.USER_ALL);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800552 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 }
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400554 } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
555 // Keep track of screen on/off state, but do not turn off the notification light
556 // until user passes through the lock screen or views the notification.
557 mScreenOn = true;
558 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
559 mScreenOn = false;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500560 } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400561 mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
562 TelephonyManager.EXTRA_STATE_OFFHOOK));
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500563 updateNotificationPulse();
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700564 } else if (action.equals(Intent.ACTION_USER_STOPPED)) {
565 int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
566 if (userHandle >= 0) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700567 cancelAllNotificationsInt(null, 0, 0, true, userHandle);
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700568 }
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400569 } else if (action.equals(Intent.ACTION_USER_PRESENT)) {
570 // turn off LED when user passes through lock screen
571 mNotificationLight.turnOff();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800572 }
573 }
574 };
575
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700576 class SettingsObserver extends ContentObserver {
577 SettingsObserver(Handler handler) {
578 super(handler);
579 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800580
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700581 void observe() {
582 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500583 resolver.registerContentObserver(Settings.System.getUriFor(
584 Settings.System.NOTIFICATION_LIGHT_PULSE), false, this);
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700585 update();
586 }
587
588 @Override public void onChange(boolean selfChange) {
589 update();
590 }
591
592 public void update() {
593 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500594 boolean pulseEnabled = Settings.System.getInt(resolver,
595 Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0;
596 if (mNotificationPulseEnabled != pulseEnabled) {
597 mNotificationPulseEnabled = pulseEnabled;
598 updateNotificationPulse();
599 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700600 }
601 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500602
Daniel Sandleraaec7262012-11-13 20:49:47 -0800603 static long[] getLongArray(Resources r, int resid, int maxlen, long[] def) {
604 int[] ar = r.getIntArray(resid);
605 if (ar == null) {
606 return def;
607 }
608 final int len = ar.length > maxlen ? maxlen : ar.length;
609 long[] out = new long[len];
610 for (int i=0; i<len; i++) {
611 out[i] = ar[i];
612 }
613 return out;
614 }
615
Joe Onorato089de882010-04-12 08:18:45 -0700616 NotificationManagerService(Context context, StatusBarManagerService statusBar,
Mike Lockwood3a322132009-11-24 00:30:52 -0500617 LightsService lights)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800618 {
619 super();
620 mContext = context;
Jeff Brownc2346132012-04-13 01:55:38 -0700621 mVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800622 mAm = ActivityManagerNative.getDefault();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800623 mToastQueue = new ArrayList<ToastRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 mHandler = new WorkerHandler();
San Mehat3ee13172010-02-04 20:54:43 -0800625
Daniel Sandler0da673f2012-04-11 12:33:16 -0400626 loadBlockDb();
627
Joe Onorato089de882010-04-12 08:18:45 -0700628 mStatusBar = statusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800629 statusBar.setNotificationCallbacks(mNotificationCallbacks);
630
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500631 mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS);
632 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
633
Mike Lockwood670f9322010-01-20 12:13:36 -0500634 Resources resources = mContext.getResources();
635 mDefaultNotificationColor = resources.getColor(
636 com.android.internal.R.color.config_defaultNotificationColor);
637 mDefaultNotificationLedOn = resources.getInteger(
638 com.android.internal.R.integer.config_defaultNotificationLedOn);
639 mDefaultNotificationLedOff = resources.getInteger(
640 com.android.internal.R.integer.config_defaultNotificationLedOff);
641
Daniel Sandleraaec7262012-11-13 20:49:47 -0800642 mDefaultVibrationPattern = getLongArray(resources,
643 com.android.internal.R.array.config_defaultNotificationVibePattern,
644 VIBRATE_PATTERN_MAXLEN,
645 DEFAULT_VIBRATE_PATTERN);
646
647 mFallbackVibrationPattern = getLongArray(resources,
648 com.android.internal.R.array.config_notificationFallbackVibePattern,
649 VIBRATE_PATTERN_MAXLEN,
650 DEFAULT_VIBRATE_PATTERN);
651
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400652 // Don't start allowing notifications until the setup wizard has run once.
653 // After that, including subsequent boots, init with notifications turned on.
654 // This works on the first boot because the setup wizard will toggle this
655 // flag at least once and we'll go back to 0 after that.
Jeff Brownbf6f6f92012-09-25 15:03:20 -0700656 if (0 == Settings.Global.getInt(mContext.getContentResolver(),
657 Settings.Global.DEVICE_PROVISIONED, 0)) {
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400658 mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
659 }
660
Mike Lockwood35e16bf2010-11-30 19:53:36 -0500661 // register for various Intents
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800662 IntentFilter filter = new IntentFilter();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500663 filter.addAction(Intent.ACTION_SCREEN_ON);
664 filter.addAction(Intent.ACTION_SCREEN_OFF);
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500665 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400666 filter.addAction(Intent.ACTION_USER_PRESENT);
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700667 filter.addAction(Intent.ACTION_USER_STOPPED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800668 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800669 IntentFilter pkgFilter = new IntentFilter();
670 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
Daniel Sandleraac0eb02011-08-06 22:51:56 -0400671 pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800672 pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
673 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
674 pkgFilter.addDataScheme("package");
675 mContext.registerReceiver(mIntentReceiver, pkgFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800676 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800677 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800678
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500679 SettingsObserver observer = new SettingsObserver(mHandler);
680 observer.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800681 }
682
Joe Onorato30275482009-07-08 17:09:14 -0700683 void systemReady() {
Jeff Sharkey098d5802012-04-26 17:30:34 -0700684 mAudioService = IAudioService.Stub.asInterface(
685 ServiceManager.getService(Context.AUDIO_SERVICE));
686
Joe Onorato30275482009-07-08 17:09:14 -0700687 // no beeping until we're basically done booting
688 mSystemReady = true;
689 }
690
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800691 // Toasts
692 // ============================================================================
693 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
694 {
Daniel Sandlera7035902010-03-30 15:45:31 -0400695 if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696
697 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800698 Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800699 return ;
700 }
701
Daniel Sandler0da673f2012-04-11 12:33:16 -0400702 final boolean isSystemToast = ("android".equals(pkg));
703
704 if (ENABLE_BLOCKED_TOASTS && !isSystemToast && !areNotificationsEnabledForPackageInt(pkg)) {
705 Slog.e(TAG, "Suppressing toast from package " + pkg + " by user request.");
706 return;
707 }
708
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 synchronized (mToastQueue) {
710 int callingPid = Binder.getCallingPid();
711 long callingId = Binder.clearCallingIdentity();
712 try {
713 ToastRecord record;
714 int index = indexOfToastLocked(pkg, callback);
715 // If it's already in the queue, we update it in place, we don't
716 // move it to the end of the queue.
717 if (index >= 0) {
718 record = mToastQueue.get(index);
719 record.update(duration);
720 } else {
Vairavan Srinivasanf9eb06c2011-01-21 18:08:36 -0800721 // Limit the number of toasts that any given package except the android
722 // package can enqueue. Prevents DOS attacks and deals with leaks.
Daniel Sandler0da673f2012-04-11 12:33:16 -0400723 if (!isSystemToast) {
Vairavan Srinivasanf9eb06c2011-01-21 18:08:36 -0800724 int count = 0;
725 final int N = mToastQueue.size();
726 for (int i=0; i<N; i++) {
727 final ToastRecord r = mToastQueue.get(i);
728 if (r.pkg.equals(pkg)) {
729 count++;
730 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
731 Slog.e(TAG, "Package has already posted " + count
732 + " toasts. Not showing more. Package=" + pkg);
733 return;
734 }
735 }
736 }
737 }
738
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800739 record = new ToastRecord(callingPid, pkg, callback, duration);
740 mToastQueue.add(record);
741 index = mToastQueue.size() - 1;
742 keepProcessAliveLocked(callingPid);
743 }
744 // If it's at index 0, it's the current toast. It doesn't matter if it's
745 // new or just been updated. Call back and tell it to show itself.
746 // If the callback fails, this will remove it from the list, so don't
747 // assume that it's valid after this.
748 if (index == 0) {
749 showNextToastLocked();
750 }
751 } finally {
752 Binder.restoreCallingIdentity(callingId);
753 }
754 }
755 }
756
757 public void cancelToast(String pkg, ITransientNotification callback) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800758 Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759
760 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800761 Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 return ;
763 }
764
765 synchronized (mToastQueue) {
766 long callingId = Binder.clearCallingIdentity();
767 try {
768 int index = indexOfToastLocked(pkg, callback);
769 if (index >= 0) {
770 cancelToastLocked(index);
771 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800772 Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 }
774 } finally {
775 Binder.restoreCallingIdentity(callingId);
776 }
777 }
778 }
779
780 private void showNextToastLocked() {
781 ToastRecord record = mToastQueue.get(0);
782 while (record != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800783 if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800784 try {
785 record.callback.show();
786 scheduleTimeoutLocked(record, false);
787 return;
788 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800789 Slog.w(TAG, "Object died trying to show notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 + " in package " + record.pkg);
791 // remove it from the list and let the process die
792 int index = mToastQueue.indexOf(record);
793 if (index >= 0) {
794 mToastQueue.remove(index);
795 }
796 keepProcessAliveLocked(record.pid);
797 if (mToastQueue.size() > 0) {
798 record = mToastQueue.get(0);
799 } else {
800 record = null;
801 }
802 }
803 }
804 }
805
806 private void cancelToastLocked(int index) {
807 ToastRecord record = mToastQueue.get(index);
808 try {
809 record.callback.hide();
810 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800811 Slog.w(TAG, "Object died trying to hide notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 + " in package " + record.pkg);
813 // don't worry about this, we're about to remove it from
814 // the list anyway
815 }
816 mToastQueue.remove(index);
817 keepProcessAliveLocked(record.pid);
818 if (mToastQueue.size() > 0) {
819 // Show the next one. If the callback fails, this will remove
820 // it from the list, so don't assume that the list hasn't changed
821 // after this point.
822 showNextToastLocked();
823 }
824 }
825
826 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
827 {
828 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
829 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
830 mHandler.removeCallbacksAndMessages(r);
831 mHandler.sendMessageDelayed(m, delay);
832 }
833
834 private void handleTimeout(ToastRecord record)
835 {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800836 if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 synchronized (mToastQueue) {
838 int index = indexOfToastLocked(record.pkg, record.callback);
839 if (index >= 0) {
840 cancelToastLocked(index);
841 }
842 }
843 }
844
845 // lock on mToastQueue
846 private int indexOfToastLocked(String pkg, ITransientNotification callback)
847 {
848 IBinder cbak = callback.asBinder();
849 ArrayList<ToastRecord> list = mToastQueue;
850 int len = list.size();
851 for (int i=0; i<len; i++) {
852 ToastRecord r = list.get(i);
853 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
854 return i;
855 }
856 }
857 return -1;
858 }
859
860 // lock on mToastQueue
861 private void keepProcessAliveLocked(int pid)
862 {
863 int toastCount = 0; // toasts from this pid
864 ArrayList<ToastRecord> list = mToastQueue;
865 int N = list.size();
866 for (int i=0; i<N; i++) {
867 ToastRecord r = list.get(i);
868 if (r.pid == pid) {
869 toastCount++;
870 }
871 }
872 try {
873 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
874 } catch (RemoteException e) {
875 // Shouldn't happen.
876 }
877 }
878
879 private final class WorkerHandler extends Handler
880 {
881 @Override
882 public void handleMessage(Message msg)
883 {
884 switch (msg.what)
885 {
886 case MESSAGE_TIMEOUT:
887 handleTimeout((ToastRecord)msg.obj);
888 break;
889 }
890 }
891 }
892
893
894 // Notifications
895 // ============================================================================
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400896 public void enqueueNotificationWithTag(String pkg, String tag, int id, Notification notification,
Dianne Hackborn41203752012-08-31 14:05:51 -0700897 int[] idOut, int userId)
Fred Quintana6ecaff12009-09-25 14:23:13 -0700898 {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400899 enqueueNotificationInternal(pkg, Binder.getCallingUid(), Binder.getCallingPid(),
Dianne Hackborn41203752012-08-31 14:05:51 -0700900 tag, id, notification, idOut, userId);
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400901 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500902
903 private final static int clamp(int x, int low, int high) {
904 return (x < low) ? low : ((x > high) ? high : x);
Daniel Sandlere40451a2011-02-03 14:51:35 -0500905 }
906
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400907 // Not exposed via Binder; for system use only (otherwise malicious apps could spoof the
908 // uid/pid of another application)
909 public void enqueueNotificationInternal(String pkg, int callingUid, int callingPid,
Dianne Hackborn41203752012-08-31 14:05:51 -0700910 String tag, int id, Notification notification, int[] idOut, int userId)
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400911 {
Daniel Sandler0da673f2012-04-11 12:33:16 -0400912 if (DBG) {
913 Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id + " notification=" + notification);
914 }
915 checkCallerIsSystemOrSameApp(pkg);
916 final boolean isSystemNotification = ("android".equals(pkg));
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800917
Dianne Hackborn41203752012-08-31 14:05:51 -0700918 userId = ActivityManager.handleIncomingUser(callingPid,
Amith Yamasani2c7ebea2012-10-30 15:28:27 -0700919 callingUid, userId, true, false, "enqueueNotification", pkg);
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -0700920 final UserHandle user = new UserHandle(userId);
Dianne Hackborn41203752012-08-31 14:05:51 -0700921
Joe Onoratobd73d012010-06-04 11:44:54 -0700922 // Limit the number of notifications that any given package except the android
923 // package can enqueue. Prevents DOS attacks and deals with leaks.
Daniel Sandler0da673f2012-04-11 12:33:16 -0400924 if (!isSystemNotification) {
Joe Onoratobd73d012010-06-04 11:44:54 -0700925 synchronized (mNotificationList) {
926 int count = 0;
927 final int N = mNotificationList.size();
928 for (int i=0; i<N; i++) {
929 final NotificationRecord r = mNotificationList.get(i);
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -0700930 if (r.pkg.equals(pkg) && r.userId == userId) {
Joe Onoratobd73d012010-06-04 11:44:54 -0700931 count++;
932 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
933 Slog.e(TAG, "Package has already posted " + count
934 + " notifications. Not showing more. package=" + pkg);
935 return;
936 }
937 }
938 }
939 }
940 }
941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 // This conditional is a dirty hack to limit the logging done on
943 // behalf of the download manager without affecting other apps.
944 if (!pkg.equals("com.android.providers.downloads")
945 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Daniel Sandler321e9c52012-10-12 10:59:26 -0700946 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, tag, userId,
Daniel Sandlerb64cb882011-11-29 23:48:29 -0500947 notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 }
949
950 if (pkg == null || notification == null) {
951 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
952 + " id=" + id + " notification=" + notification);
953 }
954 if (notification.icon != 0) {
955 if (notification.contentView == null) {
956 throw new IllegalArgumentException("contentView required: pkg=" + pkg
957 + " id=" + id + " notification=" + notification);
958 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800959 }
960
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500961 // === Scoring ===
Daniel Sandler0da673f2012-04-11 12:33:16 -0400962
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500963 // 0. Sanitize inputs
964 notification.priority = clamp(notification.priority, Notification.PRIORITY_MIN, Notification.PRIORITY_MAX);
965 // Migrate notification flags to scores
966 if (0 != (notification.flags & Notification.FLAG_HIGH_PRIORITY)) {
967 if (notification.priority < Notification.PRIORITY_MAX) notification.priority = Notification.PRIORITY_MAX;
Daniel Sandler49a2ad12012-03-28 15:46:39 -0400968 } else if (SCORE_ONGOING_HIGHER && 0 != (notification.flags & Notification.FLAG_ONGOING_EVENT)) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500969 if (notification.priority < Notification.PRIORITY_HIGH) notification.priority = Notification.PRIORITY_HIGH;
970 }
Daniel Sandler0da673f2012-04-11 12:33:16 -0400971
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500972 // 1. initial score: buckets of 10, around the app
Daniel Sandler0da673f2012-04-11 12:33:16 -0400973 int score = notification.priority * NOTIFICATION_PRIORITY_MULTIPLIER; //[-20..20]
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500974
Daniel Sandler0da673f2012-04-11 12:33:16 -0400975 // 2. Consult external heuristics (TBD)
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500976
Daniel Sandler0da673f2012-04-11 12:33:16 -0400977 // 3. Apply local rules
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500978
979 // blocked apps
Daniel Sandler0da673f2012-04-11 12:33:16 -0400980 if (ENABLE_BLOCKED_NOTIFICATIONS && !isSystemNotification && !areNotificationsEnabledForPackageInt(pkg)) {
981 score = JUNK_SCORE;
982 Slog.e(TAG, "Suppressing notification from package " + pkg + " by user request.");
983 }
984
985 if (DBG) {
986 Slog.v(TAG, "Assigned score=" + score + " to " + notification);
987 }
988
989 if (score < SCORE_DISPLAY_THRESHOLD) {
990 // Notification will be blocked because the score is too low.
991 return;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500992 }
993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 synchronized (mNotificationList) {
Daniel Sandlere40451a2011-02-03 14:51:35 -0500995 NotificationRecord r = new NotificationRecord(pkg, tag, id,
Dianne Hackborn41203752012-08-31 14:05:51 -0700996 callingUid, callingPid, userId,
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500997 score,
Daniel Sandlere40451a2011-02-03 14:51:35 -0500998 notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800999 NotificationRecord old = null;
1000
Dianne Hackborn41203752012-08-31 14:05:51 -07001001 int index = indexOfNotificationLocked(pkg, tag, id, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001002 if (index < 0) {
1003 mNotificationList.add(r);
1004 } else {
1005 old = mNotificationList.remove(index);
1006 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001007 // Make sure we don't lose the foreground service state.
1008 if (old != null) {
1009 notification.flags |=
1010 old.notification.flags&Notification.FLAG_FOREGROUND_SERVICE;
1011 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001013
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001014 // Ensure if this is a foreground service that the proper additional
1015 // flags are set.
1016 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
1017 notification.flags |= Notification.FLAG_ONGOING_EVENT
1018 | Notification.FLAG_NO_CLEAR;
1019 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001020
Svetoslav Ganovc31ed392012-10-10 14:58:28 -07001021 final int currentUser;
1022 final long token = Binder.clearCallingIdentity();
1023 try {
1024 currentUser = ActivityManager.getCurrentUser();
1025 } finally {
1026 Binder.restoreCallingIdentity(token);
1027 }
1028
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 if (notification.icon != 0) {
Jeff Sharkey6d515712012-09-20 16:06:08 -07001030 final StatusBarNotification n = new StatusBarNotification(
1031 pkg, id, tag, r.uid, r.initialPid, score, notification, user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 if (old != null && old.statusBarKey != null) {
1033 r.statusBarKey = old.statusBarKey;
1034 long identity = Binder.clearCallingIdentity();
1035 try {
Joe Onorato18e69df2010-05-17 22:26:12 -07001036 mStatusBar.updateNotification(r.statusBarKey, n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 }
1038 finally {
1039 Binder.restoreCallingIdentity(identity);
1040 }
1041 } else {
1042 long identity = Binder.clearCallingIdentity();
1043 try {
Joe Onorato18e69df2010-05-17 22:26:12 -07001044 r.statusBarKey = mStatusBar.addNotification(n);
Mike Lockwoodece18ef2012-02-13 20:42:19 -08001045 if ((n.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
1046 mAttentionLight.pulse();
1047 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001048 }
1049 finally {
1050 Binder.restoreCallingIdentity(identity);
1051 }
1052 }
Svetoslav Ganovc31ed392012-10-10 14:58:28 -07001053 // Send accessibility events only for the current user.
1054 if (currentUser == userId) {
1055 sendAccessibilityEvent(notification, pkg);
1056 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001057 } else {
Daniel Sandlere40451a2011-02-03 14:51:35 -05001058 Slog.e(TAG, "Ignoring notification with icon==0: " + notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001059 if (old != null && old.statusBarKey != null) {
1060 long identity = Binder.clearCallingIdentity();
1061 try {
Joe Onorato0cbda992010-05-02 16:28:15 -07001062 mStatusBar.removeNotification(old.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001063 }
1064 finally {
1065 Binder.restoreCallingIdentity(identity);
1066 }
1067 }
1068 }
1069
1070 // If we're not supposed to beep, vibrate, etc. then don't.
1071 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
1072 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -07001073 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
Jeff Sharkey8637bd92012-10-05 15:25:17 -07001074 && (r.userId == UserHandle.USER_ALL ||
Jeff Sharkeyb78738f2012-10-05 16:03:01 -07001075 (r.userId == userId && r.userId == currentUser))
Joe Onorato30275482009-07-08 17:09:14 -07001076 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -08001077
1078 final AudioManager audioManager = (AudioManager) mContext
1079 .getSystemService(Context.AUDIO_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 // sound
1081 final boolean useDefaultSound =
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001082 (notification.defaults & Notification.DEFAULT_SOUND) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 if (useDefaultSound || notification.sound != null) {
1084 Uri uri;
1085 if (useDefaultSound) {
1086 uri = Settings.System.DEFAULT_NOTIFICATION_URI;
1087 } else {
1088 uri = notification.sound;
1089 }
1090 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
1091 int audioStreamType;
1092 if (notification.audioStreamType >= 0) {
1093 audioStreamType = notification.audioStreamType;
1094 } else {
1095 audioStreamType = DEFAULT_STREAM_TYPE;
1096 }
1097 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -08001098 // do not play notifications if stream volume is 0
Jean-Michel Trivid6770542012-10-10 12:03:41 -07001099 // (typically because ringer mode is silent) or if speech recognition is active.
1100 if ((audioManager.getStreamVolume(audioStreamType) != 0)
1101 && !audioManager.isSpeechRecognitionActive()) {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001102 final long identity = Binder.clearCallingIdentity();
Eric Laurent524dc042009-11-27 05:07:55 -08001103 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001104 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
1105 if (player != null) {
Jeff Sharkey65c4a2b2012-09-25 17:22:27 -07001106 player.playAsync(uri, user, looping, audioStreamType);
Jeff Sharkey098d5802012-04-26 17:30:34 -07001107 }
1108 } catch (RemoteException e) {
1109 } finally {
Eric Laurent524dc042009-11-27 05:07:55 -08001110 Binder.restoreCallingIdentity(identity);
1111 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 }
1113 }
1114
1115 // vibrate
Daniel Sandleraaec7262012-11-13 20:49:47 -08001116 // Does the notification want to specify its own vibration?
1117 final boolean hasCustomVibrate = notification.vibrate != null;
1118
David Agnew4c0fc032012-11-09 23:03:26 -05001119 // new in 4.2: if there was supposed to be a sound and we're in vibrate mode,
Daniel Sandleraaec7262012-11-13 20:49:47 -08001120 // and no other vibration is specified, we apply the default vibration anyway
David Agnew4c0fc032012-11-09 23:03:26 -05001121 final boolean convertSoundToVibration =
Daniel Sandleraaec7262012-11-13 20:49:47 -08001122 !hasCustomVibrate
David Agnew4c0fc032012-11-09 23:03:26 -05001123 && (useDefaultSound || notification.sound != null)
1124 && (audioManager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE);
1125
Daniel Sandleraaec7262012-11-13 20:49:47 -08001126 // The DEFAULT_VIBRATE flag trumps any custom vibration.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001127 final boolean useDefaultVibrate =
Daniel Sandleraaec7262012-11-13 20:49:47 -08001128 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
David Agnew4c0fc032012-11-09 23:03:26 -05001129
Daniel Sandleraaec7262012-11-13 20:49:47 -08001130 if ((useDefaultVibrate || convertSoundToVibration || hasCustomVibrate)
Eric Laurentbffc3d12012-05-07 17:43:49 -07001131 && !(audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 mVibrateNotification = r;
1133
Daniel Sandleraaec7262012-11-13 20:49:47 -08001134 if (useDefaultVibrate || convertSoundToVibration) {
1135 // Escalate privileges so we can use the vibrator even if the notifying app
1136 // does not have the VIBRATE permission.
1137 long identity = Binder.clearCallingIdentity();
1138 try {
1139 mVibrator.vibrate(convertSoundToVibration ? mFallbackVibrationPattern
1140 : mDefaultVibrationPattern,
1141 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
1142 } finally {
1143 Binder.restoreCallingIdentity(identity);
1144 }
1145 } else if (notification.vibrate.length > 1) {
1146 // If you want your own vibration pattern, you need the VIBRATE permission
1147 mVibrator.vibrate(notification.vibrate,
1148 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
1149 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 }
1151 }
1152
1153 // this option doesn't shut off the lights
1154
1155 // light
1156 // the most recent thing gets the light
1157 mLights.remove(old);
1158 if (mLedNotification == old) {
1159 mLedNotification = null;
1160 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001161 //Slog.i(TAG, "notification.lights="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
1163 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
1164 mLights.add(r);
1165 updateLightsLocked();
1166 } else {
1167 if (old != null
1168 && ((old.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0)) {
1169 updateLightsLocked();
1170 }
1171 }
1172 }
1173
1174 idOut[0] = id;
1175 }
1176
Joe Onorato30275482009-07-08 17:09:14 -07001177 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -07001178 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
1179 if (!manager.isEnabled()) {
1180 return;
1181 }
1182
1183 AccessibilityEvent event =
1184 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
1185 event.setPackageName(packageName);
1186 event.setClassName(Notification.class.getName());
1187 event.setParcelableData(notification);
1188 CharSequence tickerText = notification.tickerText;
1189 if (!TextUtils.isEmpty(tickerText)) {
1190 event.getText().add(tickerText);
1191 }
1192
1193 manager.sendAccessibilityEvent(event);
1194 }
1195
Joe Onorato46439ce2010-11-19 13:56:21 -08001196 private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete) {
1197 // tell the app
1198 if (sendDelete) {
1199 if (r.notification.deleteIntent != null) {
1200 try {
1201 r.notification.deleteIntent.send();
1202 } catch (PendingIntent.CanceledException ex) {
1203 // do nothing - there's no relevant way to recover, and
1204 // no reason to let this propagate
1205 Slog.w(TAG, "canceled PendingIntent for " + r.pkg, ex);
1206 }
1207 }
1208 }
1209
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001210 // status bar
1211 if (r.notification.icon != 0) {
1212 long identity = Binder.clearCallingIdentity();
1213 try {
Joe Onorato0cbda992010-05-02 16:28:15 -07001214 mStatusBar.removeNotification(r.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 }
1216 finally {
1217 Binder.restoreCallingIdentity(identity);
1218 }
1219 r.statusBarKey = null;
1220 }
1221
1222 // sound
1223 if (mSoundNotification == r) {
1224 mSoundNotification = null;
Jeff Sharkey098d5802012-04-26 17:30:34 -07001225 final long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001227 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
1228 if (player != null) {
1229 player.stopAsync();
1230 }
1231 } catch (RemoteException e) {
1232 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 Binder.restoreCallingIdentity(identity);
1234 }
1235 }
1236
1237 // vibrate
1238 if (mVibrateNotification == r) {
1239 mVibrateNotification = null;
1240 long identity = Binder.clearCallingIdentity();
1241 try {
1242 mVibrator.cancel();
1243 }
1244 finally {
1245 Binder.restoreCallingIdentity(identity);
1246 }
1247 }
1248
1249 // light
1250 mLights.remove(r);
1251 if (mLedNotification == r) {
1252 mLedNotification = null;
1253 }
1254 }
1255
1256 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001257 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001258 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 */
Fred Quintana6ecaff12009-09-25 14:23:13 -07001260 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Dianne Hackborn41203752012-08-31 14:05:51 -07001261 int mustNotHaveFlags, boolean sendDelete, int userId) {
Daniel Sandler321e9c52012-10-12 10:59:26 -07001262 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, tag, userId,
Daniel Sandlerb64cb882011-11-29 23:48:29 -05001263 mustHaveFlags, mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264
1265 synchronized (mNotificationList) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001266 int index = indexOfNotificationLocked(pkg, tag, id, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07001268 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001269
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
1271 return;
1272 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001273 if ((r.notification.flags & mustNotHaveFlags) != 0) {
1274 return;
1275 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001276
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 mNotificationList.remove(index);
1278
Joe Onorato46439ce2010-11-19 13:56:21 -08001279 cancelNotificationLocked(r, sendDelete);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 updateLightsLocked();
1281 }
1282 }
1283 }
1284
1285 /**
Daniel Sandler321e9c52012-10-12 10:59:26 -07001286 * Determine whether the userId applies to the notification in question, either because
1287 * they match exactly, or one of them is USER_ALL (which is treated as a wildcard).
1288 */
1289 private boolean notificationMatchesUserId(NotificationRecord r, int userId) {
1290 return
1291 // looking for USER_ALL notifications? match everything
1292 userId == UserHandle.USER_ALL
1293 // a notification sent to USER_ALL matches any query
1294 || r.userId == UserHandle.USER_ALL
1295 // an exact user match
1296 || r.userId == userId;
1297 }
1298
1299 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 * Cancels all notifications from a given package that have all of the
1301 * {@code mustHaveFlags}.
1302 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001303 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
Dianne Hackborn41203752012-08-31 14:05:51 -07001304 int mustNotHaveFlags, boolean doit, int userId) {
Daniel Sandler321e9c52012-10-12 10:59:26 -07001305 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, userId,
1306 mustHaveFlags, mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307
1308 synchronized (mNotificationList) {
1309 final int N = mNotificationList.size();
1310 boolean canceledSomething = false;
1311 for (int i = N-1; i >= 0; --i) {
1312 NotificationRecord r = mNotificationList.get(i);
Daniel Sandler321e9c52012-10-12 10:59:26 -07001313 if (!notificationMatchesUserId(r, userId)) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001314 continue;
1315 }
Amith Yamasani5ec00e92012-11-07 16:58:30 -08001316 // Don't remove notifications to all, if there's no package name specified
1317 if (r.userId == UserHandle.USER_ALL && pkg == null) {
1318 continue;
1319 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
1321 continue;
1322 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001323 if ((r.notification.flags & mustNotHaveFlags) != 0) {
1324 continue;
1325 }
John Spurlock078a4902012-10-04 12:00:53 -04001326 if (pkg != null && !r.pkg.equals(pkg)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001327 continue;
1328 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001329 canceledSomething = true;
1330 if (!doit) {
1331 return true;
1332 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001333 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08001334 cancelNotificationLocked(r, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001335 }
1336 if (canceledSomething) {
1337 updateLightsLocked();
1338 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001339 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 }
1341 }
1342
Dianne Hackborn41203752012-08-31 14:05:51 -07001343 public void cancelNotificationWithTag(String pkg, String tag, int id, int userId) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001344 checkCallerIsSystemOrSameApp(pkg);
Dianne Hackborn41203752012-08-31 14:05:51 -07001345 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Amith Yamasani2c7ebea2012-10-30 15:28:27 -07001346 Binder.getCallingUid(), userId, true, false, "cancelNotificationWithTag", pkg);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001347 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -07001348 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001349 Binder.getCallingUid() == Process.SYSTEM_UID
Dianne Hackborn41203752012-08-31 14:05:51 -07001350 ? 0 : Notification.FLAG_FOREGROUND_SERVICE, false, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001351 }
1352
Dianne Hackborn41203752012-08-31 14:05:51 -07001353 public void cancelAllNotifications(String pkg, int userId) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001354 checkCallerIsSystemOrSameApp(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001355
Dianne Hackborn41203752012-08-31 14:05:51 -07001356 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
Amith Yamasani2c7ebea2012-10-30 15:28:27 -07001357 Binder.getCallingUid(), userId, true, false, "cancelAllNotifications", pkg);
Dianne Hackborn41203752012-08-31 14:05:51 -07001358
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001359 // Calling from user space, don't allow the canceling of actively
1360 // running foreground services.
Dianne Hackborn41203752012-08-31 14:05:51 -07001361 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 }
1363
Daniel Sandler0da673f2012-04-11 12:33:16 -04001364 void checkCallerIsSystem() {
1365 int uid = Binder.getCallingUid();
Dianne Hackborn0c380492012-08-20 17:23:30 -07001366 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001367 return;
1368 }
1369 throw new SecurityException("Disallowed call for uid " + uid);
1370 }
1371
1372 void checkCallerIsSystemOrSameApp(String pkg) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001373 int uid = Binder.getCallingUid();
Dianne Hackborn0c380492012-08-20 17:23:30 -07001374 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001375 return;
1376 }
1377 try {
Amith Yamasanif203aee2012-08-29 18:41:53 -07001378 ApplicationInfo ai = AppGlobals.getPackageManager().getApplicationInfo(
1379 pkg, 0, UserHandle.getCallingUserId());
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001380 if (!UserHandle.isSameApp(ai.uid, uid)) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001381 throw new SecurityException("Calling uid " + uid + " gave package"
1382 + pkg + " which is owned by uid " + ai.uid);
1383 }
Amith Yamasanif203aee2012-08-29 18:41:53 -07001384 } catch (RemoteException re) {
1385 throw new SecurityException("Unknown package " + pkg + "\n" + re);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001386 }
1387 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001388
Dianne Hackborn41203752012-08-31 14:05:51 -07001389 void cancelAll(int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 synchronized (mNotificationList) {
1391 final int N = mNotificationList.size();
1392 for (int i=N-1; i>=0; i--) {
1393 NotificationRecord r = mNotificationList.get(i);
1394
Daniel Sandler321e9c52012-10-12 10:59:26 -07001395 if (!notificationMatchesUserId(r, userId)) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001396 continue;
1397 }
1398
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399 if ((r.notification.flags & (Notification.FLAG_ONGOING_EVENT
1400 | Notification.FLAG_NO_CLEAR)) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001401 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08001402 cancelNotificationLocked(r, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 }
1404 }
1405
1406 updateLightsLocked();
1407 }
1408 }
1409
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001410 // lock on mNotificationList
1411 private void updateLightsLocked()
1412 {
The Android Open Source Project10592532009-03-18 17:39:46 -07001413 // handle notification lights
1414 if (mLedNotification == null) {
1415 // get next notification, if any
1416 int n = mLights.size();
1417 if (n > 0) {
1418 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001419 }
1420 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001421
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001422 // Don't flash while we are in a call or screen is on
1423 if (mLedNotification == null || mInCall || mScreenOn) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001424 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001425 } else {
Mike Lockwood670f9322010-01-20 12:13:36 -05001426 int ledARGB = mLedNotification.notification.ledARGB;
1427 int ledOnMS = mLedNotification.notification.ledOnMS;
1428 int ledOffMS = mLedNotification.notification.ledOffMS;
1429 if ((mLedNotification.notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
1430 ledARGB = mDefaultNotificationColor;
1431 ledOnMS = mDefaultNotificationLedOn;
1432 ledOffMS = mDefaultNotificationLedOff;
1433 }
1434 if (mNotificationPulseEnabled) {
1435 // pulse repeatedly
1436 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
1437 ledOnMS, ledOffMS);
Mike Lockwood670f9322010-01-20 12:13:36 -05001438 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001439 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001440 }
1441
1442 // lock on mNotificationList
Dianne Hackborn41203752012-08-31 14:05:51 -07001443 private int indexOfNotificationLocked(String pkg, String tag, int id, int userId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001444 {
1445 ArrayList<NotificationRecord> list = mNotificationList;
1446 final int len = list.size();
1447 for (int i=0; i<len; i++) {
1448 NotificationRecord r = list.get(i);
Daniel Sandler321e9c52012-10-12 10:59:26 -07001449 if (!notificationMatchesUserId(r, userId) || r.id != id) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001450 continue;
1451 }
Fred Quintana6ecaff12009-09-25 14:23:13 -07001452 if (tag == null) {
1453 if (r.tag != null) {
1454 continue;
1455 }
1456 } else {
1457 if (!tag.equals(r.tag)) {
1458 continue;
1459 }
1460 }
Dianne Hackborn41203752012-08-31 14:05:51 -07001461 if (r.pkg.equals(pkg)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 return i;
1463 }
1464 }
1465 return -1;
1466 }
1467
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001468 private void updateNotificationPulse() {
1469 synchronized (mNotificationList) {
1470 updateLightsLocked();
1471 }
1472 }
1473
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001474 // ======================================================================
1475 @Override
1476 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1477 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1478 != PackageManager.PERMISSION_GRANTED) {
1479 pw.println("Permission Denial: can't dump NotificationManager from from pid="
1480 + Binder.getCallingPid()
1481 + ", uid=" + Binder.getCallingUid());
1482 return;
1483 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001484
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001485 pw.println("Current Notification Manager state:");
1486
1487 int N;
1488
1489 synchronized (mToastQueue) {
1490 N = mToastQueue.size();
1491 if (N > 0) {
1492 pw.println(" Toast Queue:");
1493 for (int i=0; i<N; i++) {
1494 mToastQueue.get(i).dump(pw, " ");
1495 }
1496 pw.println(" ");
1497 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001498
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 }
1500
1501 synchronized (mNotificationList) {
1502 N = mNotificationList.size();
1503 if (N > 0) {
1504 pw.println(" Notification List:");
1505 for (int i=0; i<N; i++) {
1506 mNotificationList.get(i).dump(pw, " ", mContext);
1507 }
1508 pw.println(" ");
1509 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001510
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001511 N = mLights.size();
1512 if (N > 0) {
1513 pw.println(" Lights List:");
1514 for (int i=0; i<N; i++) {
1515 mLights.get(i).dump(pw, " ", mContext);
1516 }
1517 pw.println(" ");
1518 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001519
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 pw.println(" mSoundNotification=" + mSoundNotification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001521 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001522 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
1523 pw.println(" mSystemReady=" + mSystemReady);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001524 }
1525 }
1526}