blob: 71e6e668d8ad800350afeef5946c29f43f70a4aa [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;
John Spurlock7be1a3d2012-08-13 16:45:12 -040056import android.service.dreams.IDreamManager;
Daniel Sandlere96ffb12010-03-11 13:38:06 -050057import android.telephony.TelephonyManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070058import android.text.TextUtils;
Dianne Hackborn39606a02012-07-31 17:54:35 -070059import android.util.AtomicFile;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.util.EventLog;
61import android.util.Log;
Andy Stadler110988c2010-12-03 14:29:16 -080062import android.util.Slog;
Daniel Sandler0da673f2012-04-11 12:33:16 -040063import android.util.Xml;
svetoslavganov75986cf2009-05-14 22:28:01 -070064import android.view.accessibility.AccessibilityEvent;
65import android.view.accessibility.AccessibilityManager;
Jeff Sharkey6d515712012-09-20 16:06:08 -070066import android.widget.RemoteViews;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.widget.Toast;
68
Jeff Sharkey098d5802012-04-26 17:30:34 -070069import com.android.internal.statusbar.StatusBarNotification;
70import com.android.internal.util.FastXmlSerializer;
71
72import org.xmlpull.v1.XmlPullParser;
73import org.xmlpull.v1.XmlPullParserException;
74import org.xmlpull.v1.XmlSerializer;
75
Daniel Sandler0da673f2012-04-11 12:33:16 -040076import java.io.File;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077import java.io.FileDescriptor;
Daniel Sandler0da673f2012-04-11 12:33:16 -040078import java.io.FileInputStream;
79import java.io.FileNotFoundException;
80import java.io.FileOutputStream;
81import java.io.IOException;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082import java.io.PrintWriter;
83import java.util.ArrayList;
84import java.util.Arrays;
Daniel Sandler0da673f2012-04-11 12:33:16 -040085import java.util.HashSet;
86
87import libcore.io.IoUtils;
88
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089
Daniel Sandlerd0a2f862010-08-03 15:29:31 -040090/** {@hide} */
91public class NotificationManagerService extends INotificationManager.Stub
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092{
93 private static final String TAG = "NotificationService";
94 private static final boolean DBG = false;
95
Joe Onoratobd73d012010-06-04 11:44:54 -070096 private static final int MAX_PACKAGE_NOTIFICATIONS = 50;
97
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 // message codes
99 private static final int MESSAGE_TIMEOUT = 2;
100
101 private static final int LONG_DELAY = 3500; // 3.5 seconds
102 private static final int SHORT_DELAY = 2000; // 2 seconds
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800103
104 private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250};
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
Joe Onorato30275482009-07-08 17:09:14 -0700129 private boolean mSystemReady;
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400130 private int mDisabledNotifications;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131
Jeff Sharkey098d5802012-04-26 17:30:34 -0700132 private NotificationRecord mSoundNotification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800133 private NotificationRecord mVibrateNotification;
Jeff Sharkey098d5802012-04-26 17:30:34 -0700134
135 private IAudioService mAudioService;
Jeff Brownc2346132012-04-13 01:55:38 -0700136 private Vibrator mVibrator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500138 // for enabling and disabling notification pulse behavior
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400139 private boolean mScreenOn = true;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500140 private boolean mInCall = false;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500141 private boolean mNotificationPulseEnabled;
142
Fred Quintana6ecaff12009-09-25 14:23:13 -0700143 private final ArrayList<NotificationRecord> mNotificationList =
144 new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145
146 private ArrayList<ToastRecord> mToastQueue;
147
148 private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 private NotificationRecord mLedNotification;
svetoslavganov75986cf2009-05-14 22:28:01 -0700150
Daniel Sandler0da673f2012-04-11 12:33:16 -0400151 // Notification control database. For now just contains disabled packages.
152 private AtomicFile mPolicyFile;
153 private HashSet<String> mBlockedPackages = new HashSet<String>();
154
155 private static final int DB_VERSION = 1;
156
157 private static final String TAG_BODY = "notification-policy";
158 private static final String ATTR_VERSION = "version";
159
160 private static final String TAG_BLOCKED_PKGS = "blocked-packages";
161 private static final String TAG_PACKAGE = "package";
162 private static final String ATTR_NAME = "name";
163
164 private void loadBlockDb() {
165 synchronized(mBlockedPackages) {
166 if (mPolicyFile == null) {
167 File dir = new File("/data/system");
168 mPolicyFile = new AtomicFile(new File(dir, "notification_policy.xml"));
169
170 mBlockedPackages.clear();
171
172 FileInputStream infile = null;
173 try {
174 infile = mPolicyFile.openRead();
175 final XmlPullParser parser = Xml.newPullParser();
176 parser.setInput(infile, null);
177
178 int type;
179 String tag;
180 int version = DB_VERSION;
181 while ((type = parser.next()) != END_DOCUMENT) {
182 tag = parser.getName();
183 if (type == START_TAG) {
184 if (TAG_BODY.equals(tag)) {
185 version = Integer.parseInt(parser.getAttributeValue(null, ATTR_VERSION));
186 } else if (TAG_BLOCKED_PKGS.equals(tag)) {
187 while ((type = parser.next()) != END_DOCUMENT) {
188 tag = parser.getName();
189 if (TAG_PACKAGE.equals(tag)) {
190 mBlockedPackages.add(parser.getAttributeValue(null, ATTR_NAME));
191 } else if (TAG_BLOCKED_PKGS.equals(tag) && type == END_TAG) {
192 break;
193 }
194 }
195 }
196 }
197 }
198 } catch (FileNotFoundException e) {
199 // No data yet
200 } catch (IOException e) {
201 Log.wtf(TAG, "Unable to read blocked notifications database", e);
202 } catch (NumberFormatException e) {
203 Log.wtf(TAG, "Unable to parse blocked notifications database", e);
204 } catch (XmlPullParserException e) {
205 Log.wtf(TAG, "Unable to parse blocked notifications database", e);
206 } finally {
207 IoUtils.closeQuietly(infile);
208 }
209 }
210 }
211 }
212
213 private void writeBlockDb() {
214 synchronized(mBlockedPackages) {
215 FileOutputStream outfile = null;
216 try {
217 outfile = mPolicyFile.startWrite();
218
219 XmlSerializer out = new FastXmlSerializer();
220 out.setOutput(outfile, "utf-8");
221
222 out.startDocument(null, true);
223
224 out.startTag(null, TAG_BODY); {
225 out.attribute(null, ATTR_VERSION, String.valueOf(DB_VERSION));
226 out.startTag(null, TAG_BLOCKED_PKGS); {
227 // write all known network policies
228 for (String pkg : mBlockedPackages) {
229 out.startTag(null, TAG_PACKAGE); {
230 out.attribute(null, ATTR_NAME, pkg);
231 } out.endTag(null, TAG_PACKAGE);
232 }
233 } out.endTag(null, TAG_BLOCKED_PKGS);
234 } out.endTag(null, TAG_BODY);
235
236 out.endDocument();
237
238 mPolicyFile.finishWrite(outfile);
239 } catch (IOException e) {
240 if (outfile != null) {
241 mPolicyFile.failWrite(outfile);
242 }
243 }
244 }
245 }
246
247 public boolean areNotificationsEnabledForPackage(String pkg) {
248 checkCallerIsSystem();
249 return areNotificationsEnabledForPackageInt(pkg);
250 }
251
252 // Unchecked. Not exposed via Binder, but can be called in the course of enqueue*().
253 private boolean areNotificationsEnabledForPackageInt(String pkg) {
254 final boolean enabled = !mBlockedPackages.contains(pkg);
255 if (DBG) {
256 Slog.v(TAG, "notifications are " + (enabled?"en":"dis") + "abled for " + pkg);
257 }
258 return enabled;
259 }
260
261 public void setNotificationsEnabledForPackage(String pkg, boolean enabled) {
262 checkCallerIsSystem();
263 if (DBG) {
264 Slog.v(TAG, (enabled?"en":"dis") + "abling notifications for " + pkg);
265 }
266 if (enabled) {
267 mBlockedPackages.remove(pkg);
268 } else {
269 mBlockedPackages.add(pkg);
270
271 // Now, cancel any outstanding notifications that are part of a just-disabled app
272 if (ENABLE_BLOCKED_NOTIFICATIONS) {
273 synchronized (mNotificationList) {
274 final int N = mNotificationList.size();
275 for (int i=0; i<N; i++) {
276 final NotificationRecord r = mNotificationList.get(i);
277 if (r.pkg.equals(pkg)) {
278 cancelNotificationLocked(r, false);
279 }
280 }
281 }
282 }
283 // Don't bother canceling toasts, they'll go away soon enough.
284 }
285 writeBlockDb();
286 }
287
288
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800289 private static String idDebugString(Context baseContext, String packageName, int id) {
290 Context c = null;
291
292 if (packageName != null) {
293 try {
294 c = baseContext.createPackageContext(packageName, 0);
295 } catch (NameNotFoundException e) {
296 c = baseContext;
297 }
298 } else {
299 c = baseContext;
300 }
301
302 String pkg;
303 String type;
304 String name;
305
306 Resources r = c.getResources();
307 try {
308 return r.getResourceName(id);
309 } catch (Resources.NotFoundException e) {
310 return "<name unknown>";
311 }
312 }
313
314 private static final class NotificationRecord
315 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700316 final String pkg;
317 final String tag;
318 final int id;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700319 final int uid;
320 final int initialPid;
Dianne Hackborn41203752012-08-31 14:05:51 -0700321 final int userId;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700322 final Notification notification;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500323 final int score;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 IBinder statusBarKey;
325
Dianne Hackborn41203752012-08-31 14:05:51 -0700326 NotificationRecord(String pkg, String tag, int id, int uid, int initialPid,
327 int userId, int score, Notification notification)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 {
329 this.pkg = pkg;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700330 this.tag = tag;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 this.id = id;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700332 this.uid = uid;
333 this.initialPid = initialPid;
Dianne Hackborn41203752012-08-31 14:05:51 -0700334 this.userId = userId;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500335 this.score = score;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 this.notification = notification;
337 }
Fred Quintana6ecaff12009-09-25 14:23:13 -0700338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 void dump(PrintWriter pw, String prefix, Context baseContext) {
340 pw.println(prefix + this);
341 pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
342 + " / " + idDebugString(baseContext, this.pkg, notification.icon));
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500343 pw.println(prefix + " pri=" + notification.priority);
344 pw.println(prefix + " score=" + this.score);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800345 pw.println(prefix + " contentIntent=" + notification.contentIntent);
346 pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
347 pw.println(prefix + " tickerText=" + notification.tickerText);
348 pw.println(prefix + " contentView=" + notification.contentView);
Dianne Hackborn41203752012-08-31 14:05:51 -0700349 pw.println(prefix + " uid=" + uid + " userId=" + userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 pw.println(prefix + " defaults=0x" + Integer.toHexString(notification.defaults));
351 pw.println(prefix + " flags=0x" + Integer.toHexString(notification.flags));
352 pw.println(prefix + " sound=" + notification.sound);
353 pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate));
354 pw.println(prefix + " ledARGB=0x" + Integer.toHexString(notification.ledARGB)
355 + " ledOnMS=" + notification.ledOnMS
356 + " ledOffMS=" + notification.ledOffMS);
357 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800358
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 @Override
360 public final String toString()
361 {
362 return "NotificationRecord{"
363 + Integer.toHexString(System.identityHashCode(this))
364 + " pkg=" + pkg
Fred Quintana6ecaff12009-09-25 14:23:13 -0700365 + " id=" + Integer.toHexString(id)
Daniel Sandlere40451a2011-02-03 14:51:35 -0500366 + " tag=" + tag
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500367 + " score=" + score
Daniel Sandlere40451a2011-02-03 14:51:35 -0500368 + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 }
370 }
371
372 private static final class ToastRecord
373 {
374 final int pid;
375 final String pkg;
376 final ITransientNotification callback;
377 int duration;
378
379 ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
380 {
381 this.pid = pid;
382 this.pkg = pkg;
383 this.callback = callback;
384 this.duration = duration;
385 }
386
387 void update(int duration) {
388 this.duration = duration;
389 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800390
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 void dump(PrintWriter pw, String prefix) {
392 pw.println(prefix + this);
393 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 @Override
396 public final String toString()
397 {
398 return "ToastRecord{"
399 + Integer.toHexString(System.identityHashCode(this))
400 + " pkg=" + pkg
401 + " callback=" + callback
402 + " duration=" + duration;
403 }
404 }
405
Joe Onorato089de882010-04-12 08:18:45 -0700406 private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks
407 = new StatusBarManagerService.NotificationCallbacks() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800408
409 public void onSetDisabled(int status) {
410 synchronized (mNotificationList) {
411 mDisabledNotifications = status;
412 if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) {
413 // cancel whatever's going on
414 long identity = Binder.clearCallingIdentity();
415 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -0700416 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
417 if (player != null) {
418 player.stopAsync();
419 }
420 } catch (RemoteException e) {
421 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 Binder.restoreCallingIdentity(identity);
423 }
424
425 identity = Binder.clearCallingIdentity();
426 try {
427 mVibrator.cancel();
Jeff Sharkey098d5802012-04-26 17:30:34 -0700428 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 Binder.restoreCallingIdentity(identity);
430 }
431 }
432 }
433 }
434
435 public void onClearAll() {
Dianne Hackborn41203752012-08-31 14:05:51 -0700436 // XXX to be totally correct, the caller should tell us which user
437 // this is for.
438 cancelAll(ActivityManager.getCurrentUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439 }
440
Fred Quintana6ecaff12009-09-25 14:23:13 -0700441 public void onNotificationClick(String pkg, String tag, int id) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700442 // XXX to be totally correct, the caller should tell us which user
443 // this is for.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700444 cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL,
Dianne Hackborn41203752012-08-31 14:05:51 -0700445 Notification.FLAG_FOREGROUND_SERVICE, false,
446 ActivityManager.getCurrentUser());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 }
448
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400449 public void onNotificationClear(String pkg, String tag, int id) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700450 // XXX to be totally correct, the caller should tell us which user
451 // this is for.
Joe Onorato46439ce2010-11-19 13:56:21 -0800452 cancelNotification(pkg, tag, id, 0,
453 Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE,
Dianne Hackborn41203752012-08-31 14:05:51 -0700454 true, ActivityManager.getCurrentUser());
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400455 }
456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 public void onPanelRevealed() {
458 synchronized (mNotificationList) {
459 // sound
460 mSoundNotification = null;
Jeff Sharkey098d5802012-04-26 17:30:34 -0700461
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 long identity = Binder.clearCallingIdentity();
463 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -0700464 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
465 if (player != null) {
466 player.stopAsync();
467 }
468 } catch (RemoteException e) {
469 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 Binder.restoreCallingIdentity(identity);
471 }
472
473 // vibrate
474 mVibrateNotification = null;
475 identity = Binder.clearCallingIdentity();
476 try {
477 mVibrator.cancel();
Jeff Sharkey098d5802012-04-26 17:30:34 -0700478 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 Binder.restoreCallingIdentity(identity);
480 }
481
482 // light
483 mLights.clear();
484 mLedNotification = null;
485 updateLightsLocked();
486 }
487 }
Joe Onorato005847b2010-06-04 16:08:02 -0400488
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700489 public void onNotificationError(String pkg, String tag, int id,
490 int uid, int initialPid, String message) {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400491 Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id
492 + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")");
Dianne Hackborn41203752012-08-31 14:05:51 -0700493 // XXX to be totally correct, the caller should tell us which user
494 // this is for.
495 cancelNotification(pkg, tag, id, 0, 0, false, UserHandle.getUserId(uid));
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700496 long ident = Binder.clearCallingIdentity();
497 try {
498 ActivityManagerNative.getDefault().crashApplication(uid, initialPid, pkg,
499 "Bad notification posted from package " + pkg
500 + ": " + message);
501 } catch (RemoteException e) {
502 }
503 Binder.restoreCallingIdentity(ident);
Joe Onorato005847b2010-06-04 16:08:02 -0400504 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505 };
506
507 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
508 @Override
509 public void onReceive(Context context, Intent intent) {
510 String action = intent.getAction();
511
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800512 boolean queryRestart = false;
Daniel Sandler26ece572012-06-01 15:38:46 -0400513 boolean packageChanged = false;
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800514
Mike Lockwood541c9942011-06-12 19:35:45 -0400515 if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800516 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Daniel Sandler26ece572012-06-01 15:38:46 -0400517 || (packageChanged=action.equals(Intent.ACTION_PACKAGE_CHANGED))
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800518 || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800519 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800520 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800521 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800522 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800523 } else if (queryRestart) {
524 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800525 } else {
526 Uri uri = intent.getData();
527 if (uri == null) {
528 return;
529 }
530 String pkgName = uri.getSchemeSpecificPart();
531 if (pkgName == null) {
532 return;
533 }
Daniel Sandler26ece572012-06-01 15:38:46 -0400534 if (packageChanged) {
535 // We cancel notifications for packages which have just been disabled
536 final int enabled = mContext.getPackageManager()
537 .getApplicationEnabledSetting(pkgName);
538 if (enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED
539 || enabled == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
540 return;
541 }
542 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800543 pkgList = new String[]{pkgName};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800545 if (pkgList != null && (pkgList.length > 0)) {
546 for (String pkgName : pkgList) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700547 cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart,
548 UserHandle.USER_ALL);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800549 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 }
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400551 } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
552 // Keep track of screen on/off state, but do not turn off the notification light
553 // until user passes through the lock screen or views the notification.
554 mScreenOn = true;
555 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
556 mScreenOn = false;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500557 } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400558 mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
559 TelephonyManager.EXTRA_STATE_OFFHOOK));
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500560 updateNotificationPulse();
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700561 } else if (action.equals(Intent.ACTION_USER_STOPPED)) {
562 int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1);
563 if (userHandle >= 0) {
Dianne Hackborn41203752012-08-31 14:05:51 -0700564 cancelAllNotificationsInt(null, 0, 0, true, userHandle);
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700565 }
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400566 } else if (action.equals(Intent.ACTION_USER_PRESENT)) {
567 // turn off LED when user passes through lock screen
568 mNotificationLight.turnOff();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 }
570 }
571 };
572
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700573 class SettingsObserver extends ContentObserver {
574 SettingsObserver(Handler handler) {
575 super(handler);
576 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800577
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700578 void observe() {
579 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500580 resolver.registerContentObserver(Settings.System.getUriFor(
581 Settings.System.NOTIFICATION_LIGHT_PULSE), false, this);
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700582 update();
583 }
584
585 @Override public void onChange(boolean selfChange) {
586 update();
587 }
588
589 public void update() {
590 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500591 boolean pulseEnabled = Settings.System.getInt(resolver,
592 Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0;
593 if (mNotificationPulseEnabled != pulseEnabled) {
594 mNotificationPulseEnabled = pulseEnabled;
595 updateNotificationPulse();
596 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700597 }
598 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500599
Joe Onorato089de882010-04-12 08:18:45 -0700600 NotificationManagerService(Context context, StatusBarManagerService statusBar,
Mike Lockwood3a322132009-11-24 00:30:52 -0500601 LightsService lights)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 {
603 super();
604 mContext = context;
Jeff Brownc2346132012-04-13 01:55:38 -0700605 mVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800606 mAm = ActivityManagerNative.getDefault();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 mToastQueue = new ArrayList<ToastRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800608 mHandler = new WorkerHandler();
San Mehat3ee13172010-02-04 20:54:43 -0800609
Daniel Sandler0da673f2012-04-11 12:33:16 -0400610 loadBlockDb();
611
Joe Onorato089de882010-04-12 08:18:45 -0700612 mStatusBar = statusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800613 statusBar.setNotificationCallbacks(mNotificationCallbacks);
614
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500615 mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS);
616 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
617
Mike Lockwood670f9322010-01-20 12:13:36 -0500618 Resources resources = mContext.getResources();
619 mDefaultNotificationColor = resources.getColor(
620 com.android.internal.R.color.config_defaultNotificationColor);
621 mDefaultNotificationLedOn = resources.getInteger(
622 com.android.internal.R.integer.config_defaultNotificationLedOn);
623 mDefaultNotificationLedOff = resources.getInteger(
624 com.android.internal.R.integer.config_defaultNotificationLedOff);
625
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400626 // Don't start allowing notifications until the setup wizard has run once.
627 // After that, including subsequent boots, init with notifications turned on.
628 // This works on the first boot because the setup wizard will toggle this
629 // flag at least once and we'll go back to 0 after that.
630 if (0 == Settings.Secure.getInt(mContext.getContentResolver(),
631 Settings.Secure.DEVICE_PROVISIONED, 0)) {
632 mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
633 }
634
Mike Lockwood35e16bf2010-11-30 19:53:36 -0500635 // register for various Intents
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800636 IntentFilter filter = new IntentFilter();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500637 filter.addAction(Intent.ACTION_SCREEN_ON);
638 filter.addAction(Intent.ACTION_SCREEN_OFF);
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500639 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400640 filter.addAction(Intent.ACTION_USER_PRESENT);
Dianne Hackborn80a4af22012-08-27 19:18:31 -0700641 filter.addAction(Intent.ACTION_USER_STOPPED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800643 IntentFilter pkgFilter = new IntentFilter();
644 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
Daniel Sandleraac0eb02011-08-06 22:51:56 -0400645 pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800646 pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
647 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
648 pkgFilter.addDataScheme("package");
649 mContext.registerReceiver(mIntentReceiver, pkgFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800650 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800651 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800652
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500653 SettingsObserver observer = new SettingsObserver(mHandler);
654 observer.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800655 }
656
Joe Onorato30275482009-07-08 17:09:14 -0700657 void systemReady() {
Jeff Sharkey098d5802012-04-26 17:30:34 -0700658 mAudioService = IAudioService.Stub.asInterface(
659 ServiceManager.getService(Context.AUDIO_SERVICE));
660
Joe Onorato30275482009-07-08 17:09:14 -0700661 // no beeping until we're basically done booting
662 mSystemReady = true;
663 }
664
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 // Toasts
666 // ============================================================================
667 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
668 {
Daniel Sandlera7035902010-03-30 15:45:31 -0400669 if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670
671 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800672 Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 return ;
674 }
675
Daniel Sandler0da673f2012-04-11 12:33:16 -0400676 final boolean isSystemToast = ("android".equals(pkg));
677
678 if (ENABLE_BLOCKED_TOASTS && !isSystemToast && !areNotificationsEnabledForPackageInt(pkg)) {
679 Slog.e(TAG, "Suppressing toast from package " + pkg + " by user request.");
680 return;
681 }
682
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800683 synchronized (mToastQueue) {
684 int callingPid = Binder.getCallingPid();
685 long callingId = Binder.clearCallingIdentity();
686 try {
687 ToastRecord record;
688 int index = indexOfToastLocked(pkg, callback);
689 // If it's already in the queue, we update it in place, we don't
690 // move it to the end of the queue.
691 if (index >= 0) {
692 record = mToastQueue.get(index);
693 record.update(duration);
694 } else {
Vairavan Srinivasanf9eb06c2011-01-21 18:08:36 -0800695 // Limit the number of toasts that any given package except the android
696 // package can enqueue. Prevents DOS attacks and deals with leaks.
Daniel Sandler0da673f2012-04-11 12:33:16 -0400697 if (!isSystemToast) {
Vairavan Srinivasanf9eb06c2011-01-21 18:08:36 -0800698 int count = 0;
699 final int N = mToastQueue.size();
700 for (int i=0; i<N; i++) {
701 final ToastRecord r = mToastQueue.get(i);
702 if (r.pkg.equals(pkg)) {
703 count++;
704 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
705 Slog.e(TAG, "Package has already posted " + count
706 + " toasts. Not showing more. Package=" + pkg);
707 return;
708 }
709 }
710 }
711 }
712
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 record = new ToastRecord(callingPid, pkg, callback, duration);
714 mToastQueue.add(record);
715 index = mToastQueue.size() - 1;
716 keepProcessAliveLocked(callingPid);
717 }
718 // If it's at index 0, it's the current toast. It doesn't matter if it's
719 // new or just been updated. Call back and tell it to show itself.
720 // If the callback fails, this will remove it from the list, so don't
721 // assume that it's valid after this.
722 if (index == 0) {
723 showNextToastLocked();
724 }
725 } finally {
726 Binder.restoreCallingIdentity(callingId);
727 }
728 }
729 }
730
731 public void cancelToast(String pkg, ITransientNotification callback) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800732 Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733
734 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800735 Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 return ;
737 }
738
739 synchronized (mToastQueue) {
740 long callingId = Binder.clearCallingIdentity();
741 try {
742 int index = indexOfToastLocked(pkg, callback);
743 if (index >= 0) {
744 cancelToastLocked(index);
745 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800746 Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 }
748 } finally {
749 Binder.restoreCallingIdentity(callingId);
750 }
751 }
752 }
753
754 private void showNextToastLocked() {
755 ToastRecord record = mToastQueue.get(0);
756 while (record != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800757 if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 try {
759 record.callback.show();
760 scheduleTimeoutLocked(record, false);
761 return;
762 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800763 Slog.w(TAG, "Object died trying to show notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800764 + " in package " + record.pkg);
765 // remove it from the list and let the process die
766 int index = mToastQueue.indexOf(record);
767 if (index >= 0) {
768 mToastQueue.remove(index);
769 }
770 keepProcessAliveLocked(record.pid);
771 if (mToastQueue.size() > 0) {
772 record = mToastQueue.get(0);
773 } else {
774 record = null;
775 }
776 }
777 }
778 }
779
780 private void cancelToastLocked(int index) {
781 ToastRecord record = mToastQueue.get(index);
782 try {
783 record.callback.hide();
784 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800785 Slog.w(TAG, "Object died trying to hide notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800786 + " in package " + record.pkg);
787 // don't worry about this, we're about to remove it from
788 // the list anyway
789 }
790 mToastQueue.remove(index);
791 keepProcessAliveLocked(record.pid);
792 if (mToastQueue.size() > 0) {
793 // Show the next one. If the callback fails, this will remove
794 // it from the list, so don't assume that the list hasn't changed
795 // after this point.
796 showNextToastLocked();
797 }
798 }
799
800 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
801 {
802 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
803 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
804 mHandler.removeCallbacksAndMessages(r);
805 mHandler.sendMessageDelayed(m, delay);
806 }
807
808 private void handleTimeout(ToastRecord record)
809 {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800810 if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 synchronized (mToastQueue) {
812 int index = indexOfToastLocked(record.pkg, record.callback);
813 if (index >= 0) {
814 cancelToastLocked(index);
815 }
816 }
817 }
818
819 // lock on mToastQueue
820 private int indexOfToastLocked(String pkg, ITransientNotification callback)
821 {
822 IBinder cbak = callback.asBinder();
823 ArrayList<ToastRecord> list = mToastQueue;
824 int len = list.size();
825 for (int i=0; i<len; i++) {
826 ToastRecord r = list.get(i);
827 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
828 return i;
829 }
830 }
831 return -1;
832 }
833
834 // lock on mToastQueue
835 private void keepProcessAliveLocked(int pid)
836 {
837 int toastCount = 0; // toasts from this pid
838 ArrayList<ToastRecord> list = mToastQueue;
839 int N = list.size();
840 for (int i=0; i<N; i++) {
841 ToastRecord r = list.get(i);
842 if (r.pid == pid) {
843 toastCount++;
844 }
845 }
846 try {
847 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
848 } catch (RemoteException e) {
849 // Shouldn't happen.
850 }
851 }
852
853 private final class WorkerHandler extends Handler
854 {
855 @Override
856 public void handleMessage(Message msg)
857 {
858 switch (msg.what)
859 {
860 case MESSAGE_TIMEOUT:
861 handleTimeout((ToastRecord)msg.obj);
862 break;
863 }
864 }
865 }
866
867
868 // Notifications
869 // ============================================================================
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400870 public void enqueueNotificationWithTag(String pkg, String tag, int id, Notification notification,
Dianne Hackborn41203752012-08-31 14:05:51 -0700871 int[] idOut, int userId)
Fred Quintana6ecaff12009-09-25 14:23:13 -0700872 {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400873 enqueueNotificationInternal(pkg, Binder.getCallingUid(), Binder.getCallingPid(),
Dianne Hackborn41203752012-08-31 14:05:51 -0700874 tag, id, notification, idOut, userId);
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400875 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500876
877 private final static int clamp(int x, int low, int high) {
878 return (x < low) ? low : ((x > high) ? high : x);
Daniel Sandlere40451a2011-02-03 14:51:35 -0500879 }
880
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400881 // Not exposed via Binder; for system use only (otherwise malicious apps could spoof the
882 // uid/pid of another application)
883 public void enqueueNotificationInternal(String pkg, int callingUid, int callingPid,
Dianne Hackborn41203752012-08-31 14:05:51 -0700884 String tag, int id, Notification notification, int[] idOut, int userId)
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400885 {
Daniel Sandler0da673f2012-04-11 12:33:16 -0400886 if (DBG) {
887 Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id + " notification=" + notification);
888 }
889 checkCallerIsSystemOrSameApp(pkg);
890 final boolean isSystemNotification = ("android".equals(pkg));
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800891
Dianne Hackborn41203752012-08-31 14:05:51 -0700892 userId = ActivityManager.handleIncomingUser(callingPid,
893 callingUid, userId, false, true, "enqueueNotification", pkg);
894
Joe Onoratobd73d012010-06-04 11:44:54 -0700895 // Limit the number of notifications that any given package except the android
896 // package can enqueue. Prevents DOS attacks and deals with leaks.
Daniel Sandler0da673f2012-04-11 12:33:16 -0400897 if (!isSystemNotification) {
Joe Onoratobd73d012010-06-04 11:44:54 -0700898 synchronized (mNotificationList) {
899 int count = 0;
900 final int N = mNotificationList.size();
901 for (int i=0; i<N; i++) {
902 final NotificationRecord r = mNotificationList.get(i);
903 if (r.pkg.equals(pkg)) {
904 count++;
905 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
906 Slog.e(TAG, "Package has already posted " + count
907 + " notifications. Not showing more. package=" + pkg);
908 return;
909 }
910 }
911 }
912 }
913 }
914
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 // This conditional is a dirty hack to limit the logging done on
916 // behalf of the download manager without affecting other apps.
917 if (!pkg.equals("com.android.providers.downloads")
918 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Daniel Sandlerb64cb882011-11-29 23:48:29 -0500919 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, tag,
920 notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 }
922
923 if (pkg == null || notification == null) {
924 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
925 + " id=" + id + " notification=" + notification);
926 }
927 if (notification.icon != 0) {
928 if (notification.contentView == null) {
929 throw new IllegalArgumentException("contentView required: pkg=" + pkg
930 + " id=" + id + " notification=" + notification);
931 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 }
933
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500934 // === Scoring ===
Daniel Sandler0da673f2012-04-11 12:33:16 -0400935
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500936 // 0. Sanitize inputs
937 notification.priority = clamp(notification.priority, Notification.PRIORITY_MIN, Notification.PRIORITY_MAX);
938 // Migrate notification flags to scores
939 if (0 != (notification.flags & Notification.FLAG_HIGH_PRIORITY)) {
940 if (notification.priority < Notification.PRIORITY_MAX) notification.priority = Notification.PRIORITY_MAX;
Daniel Sandler49a2ad12012-03-28 15:46:39 -0400941 } else if (SCORE_ONGOING_HIGHER && 0 != (notification.flags & Notification.FLAG_ONGOING_EVENT)) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500942 if (notification.priority < Notification.PRIORITY_HIGH) notification.priority = Notification.PRIORITY_HIGH;
943 }
Daniel Sandler0da673f2012-04-11 12:33:16 -0400944
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500945 // 1. initial score: buckets of 10, around the app
Daniel Sandler0da673f2012-04-11 12:33:16 -0400946 int score = notification.priority * NOTIFICATION_PRIORITY_MULTIPLIER; //[-20..20]
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500947
Daniel Sandler0da673f2012-04-11 12:33:16 -0400948 // 2. Consult external heuristics (TBD)
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500949
Daniel Sandler0da673f2012-04-11 12:33:16 -0400950 // 3. Apply local rules
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500951
952 // blocked apps
Daniel Sandler0da673f2012-04-11 12:33:16 -0400953 if (ENABLE_BLOCKED_NOTIFICATIONS && !isSystemNotification && !areNotificationsEnabledForPackageInt(pkg)) {
954 score = JUNK_SCORE;
955 Slog.e(TAG, "Suppressing notification from package " + pkg + " by user request.");
956 }
957
958 if (DBG) {
959 Slog.v(TAG, "Assigned score=" + score + " to " + notification);
960 }
961
962 if (score < SCORE_DISPLAY_THRESHOLD) {
963 // Notification will be blocked because the score is too low.
964 return;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500965 }
966
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800967 synchronized (mNotificationList) {
Daniel Sandlere40451a2011-02-03 14:51:35 -0500968 NotificationRecord r = new NotificationRecord(pkg, tag, id,
Dianne Hackborn41203752012-08-31 14:05:51 -0700969 callingUid, callingPid, userId,
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500970 score,
Daniel Sandlere40451a2011-02-03 14:51:35 -0500971 notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 NotificationRecord old = null;
973
Dianne Hackborn41203752012-08-31 14:05:51 -0700974 int index = indexOfNotificationLocked(pkg, tag, id, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 if (index < 0) {
976 mNotificationList.add(r);
977 } else {
978 old = mNotificationList.remove(index);
979 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700980 // Make sure we don't lose the foreground service state.
981 if (old != null) {
982 notification.flags |=
983 old.notification.flags&Notification.FLAG_FOREGROUND_SERVICE;
984 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800986
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700987 // Ensure if this is a foreground service that the proper additional
988 // flags are set.
989 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
990 notification.flags |= Notification.FLAG_ONGOING_EVENT
991 | Notification.FLAG_NO_CLEAR;
992 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800993
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800994 if (notification.icon != 0) {
Jeff Sharkey6d515712012-09-20 16:06:08 -0700995 final UserHandle user = new UserHandle(userId);
996 final StatusBarNotification n = new StatusBarNotification(
997 pkg, id, tag, r.uid, r.initialPid, score, notification, user);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998 if (old != null && old.statusBarKey != null) {
999 r.statusBarKey = old.statusBarKey;
1000 long identity = Binder.clearCallingIdentity();
1001 try {
Joe Onorato18e69df2010-05-17 22:26:12 -07001002 mStatusBar.updateNotification(r.statusBarKey, n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001003 }
1004 finally {
1005 Binder.restoreCallingIdentity(identity);
1006 }
1007 } else {
1008 long identity = Binder.clearCallingIdentity();
1009 try {
Joe Onorato18e69df2010-05-17 22:26:12 -07001010 r.statusBarKey = mStatusBar.addNotification(n);
Mike Lockwoodece18ef2012-02-13 20:42:19 -08001011 if ((n.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
1012 mAttentionLight.pulse();
1013 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001014 }
1015 finally {
1016 Binder.restoreCallingIdentity(identity);
1017 }
1018 }
Joe Onorato30275482009-07-08 17:09:14 -07001019 sendAccessibilityEvent(notification, pkg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 } else {
Daniel Sandlere40451a2011-02-03 14:51:35 -05001021 Slog.e(TAG, "Ignoring notification with icon==0: " + notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001022 if (old != null && old.statusBarKey != null) {
1023 long identity = Binder.clearCallingIdentity();
1024 try {
Joe Onorato0cbda992010-05-02 16:28:15 -07001025 mStatusBar.removeNotification(old.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001026 }
1027 finally {
1028 Binder.restoreCallingIdentity(identity);
1029 }
1030 }
1031 }
1032
1033 // If we're not supposed to beep, vibrate, etc. then don't.
1034 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
1035 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -07001036 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
Daniel Sandler24518e42012-09-04 13:23:01 -04001037 && (r.userId == UserHandle.USER_ALL || r.userId == userId)
Joe Onorato30275482009-07-08 17:09:14 -07001038 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -08001039
1040 final AudioManager audioManager = (AudioManager) mContext
1041 .getSystemService(Context.AUDIO_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 // sound
1043 final boolean useDefaultSound =
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001044 (notification.defaults & Notification.DEFAULT_SOUND) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001045 if (useDefaultSound || notification.sound != null) {
1046 Uri uri;
1047 if (useDefaultSound) {
1048 uri = Settings.System.DEFAULT_NOTIFICATION_URI;
1049 } else {
1050 uri = notification.sound;
1051 }
1052 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
1053 int audioStreamType;
1054 if (notification.audioStreamType >= 0) {
1055 audioStreamType = notification.audioStreamType;
1056 } else {
1057 audioStreamType = DEFAULT_STREAM_TYPE;
1058 }
1059 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -08001060 // do not play notifications if stream volume is 0
1061 // (typically because ringer mode is silent).
1062 if (audioManager.getStreamVolume(audioStreamType) != 0) {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001063 final long identity = Binder.clearCallingIdentity();
Eric Laurent524dc042009-11-27 05:07:55 -08001064 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001065 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
1066 if (player != null) {
1067 player.playAsync(uri, looping, audioStreamType);
1068 }
1069 } catch (RemoteException e) {
1070 } finally {
Eric Laurent524dc042009-11-27 05:07:55 -08001071 Binder.restoreCallingIdentity(identity);
1072 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 }
1074 }
1075
1076 // vibrate
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001077 final boolean useDefaultVibrate =
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001078 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001079 if ((useDefaultVibrate || notification.vibrate != null)
Eric Laurentbffc3d12012-05-07 17:43:49 -07001080 && !(audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 mVibrateNotification = r;
1082
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001083 mVibrator.vibrate(useDefaultVibrate ? DEFAULT_VIBRATE_PATTERN
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 : notification.vibrate,
1085 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
1086 }
1087 }
1088
1089 // this option doesn't shut off the lights
1090
1091 // light
1092 // the most recent thing gets the light
1093 mLights.remove(old);
1094 if (mLedNotification == old) {
1095 mLedNotification = null;
1096 }
Joe Onorato8a9b2202010-02-26 18:56:32 -08001097 //Slog.i(TAG, "notification.lights="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
1099 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
1100 mLights.add(r);
1101 updateLightsLocked();
1102 } else {
1103 if (old != null
1104 && ((old.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0)) {
1105 updateLightsLocked();
1106 }
1107 }
1108 }
1109
1110 idOut[0] = id;
1111 }
1112
Joe Onorato30275482009-07-08 17:09:14 -07001113 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -07001114 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
1115 if (!manager.isEnabled()) {
1116 return;
1117 }
1118
1119 AccessibilityEvent event =
1120 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
1121 event.setPackageName(packageName);
1122 event.setClassName(Notification.class.getName());
1123 event.setParcelableData(notification);
1124 CharSequence tickerText = notification.tickerText;
1125 if (!TextUtils.isEmpty(tickerText)) {
1126 event.getText().add(tickerText);
1127 }
1128
1129 manager.sendAccessibilityEvent(event);
1130 }
1131
Joe Onorato46439ce2010-11-19 13:56:21 -08001132 private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete) {
1133 // tell the app
1134 if (sendDelete) {
1135 if (r.notification.deleteIntent != null) {
1136 try {
1137 r.notification.deleteIntent.send();
1138 } catch (PendingIntent.CanceledException ex) {
1139 // do nothing - there's no relevant way to recover, and
1140 // no reason to let this propagate
1141 Slog.w(TAG, "canceled PendingIntent for " + r.pkg, ex);
1142 }
1143 }
1144 }
1145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001146 // status bar
1147 if (r.notification.icon != 0) {
1148 long identity = Binder.clearCallingIdentity();
1149 try {
Joe Onorato0cbda992010-05-02 16:28:15 -07001150 mStatusBar.removeNotification(r.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001151 }
1152 finally {
1153 Binder.restoreCallingIdentity(identity);
1154 }
1155 r.statusBarKey = null;
1156 }
1157
1158 // sound
1159 if (mSoundNotification == r) {
1160 mSoundNotification = null;
Jeff Sharkey098d5802012-04-26 17:30:34 -07001161 final long identity = Binder.clearCallingIdentity();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001162 try {
Jeff Sharkey098d5802012-04-26 17:30:34 -07001163 final IRingtonePlayer player = mAudioService.getRingtonePlayer();
1164 if (player != null) {
1165 player.stopAsync();
1166 }
1167 } catch (RemoteException e) {
1168 } finally {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 Binder.restoreCallingIdentity(identity);
1170 }
1171 }
1172
1173 // vibrate
1174 if (mVibrateNotification == r) {
1175 mVibrateNotification = null;
1176 long identity = Binder.clearCallingIdentity();
1177 try {
1178 mVibrator.cancel();
1179 }
1180 finally {
1181 Binder.restoreCallingIdentity(identity);
1182 }
1183 }
1184
1185 // light
1186 mLights.remove(r);
1187 if (mLedNotification == r) {
1188 mLedNotification = null;
1189 }
1190 }
1191
1192 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001193 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001194 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 */
Fred Quintana6ecaff12009-09-25 14:23:13 -07001196 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Dianne Hackborn41203752012-08-31 14:05:51 -07001197 int mustNotHaveFlags, boolean sendDelete, int userId) {
Daniel Sandlerb64cb882011-11-29 23:48:29 -05001198 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, tag,
1199 mustHaveFlags, mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200
1201 synchronized (mNotificationList) {
Dianne Hackborn41203752012-08-31 14:05:51 -07001202 int index = indexOfNotificationLocked(pkg, tag, id, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001203 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07001204 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001205
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001206 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
1207 return;
1208 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001209 if ((r.notification.flags & mustNotHaveFlags) != 0) {
1210 return;
1211 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 mNotificationList.remove(index);
1214
Joe Onorato46439ce2010-11-19 13:56:21 -08001215 cancelNotificationLocked(r, sendDelete);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001216 updateLightsLocked();
1217 }
1218 }
1219 }
1220
1221 /**
1222 * Cancels all notifications from a given package that have all of the
1223 * {@code mustHaveFlags}.
1224 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001225 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
Dianne Hackborn41203752012-08-31 14:05:51 -07001226 int mustNotHaveFlags, boolean doit, int userId) {
Daniel Sandlerb64cb882011-11-29 23:48:29 -05001227 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, mustHaveFlags,
1228 mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229
1230 synchronized (mNotificationList) {
1231 final int N = mNotificationList.size();
1232 boolean canceledSomething = false;
1233 for (int i = N-1; i >= 0; --i) {
1234 NotificationRecord r = mNotificationList.get(i);
Dianne Hackborn41203752012-08-31 14:05:51 -07001235 if (userId != UserHandle.USER_ALL && r.userId != userId) {
1236 continue;
1237 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
1239 continue;
1240 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001241 if ((r.notification.flags & mustNotHaveFlags) != 0) {
1242 continue;
1243 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001244 if (!r.pkg.equals(pkg)) {
1245 continue;
1246 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001247 canceledSomething = true;
1248 if (!doit) {
1249 return true;
1250 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08001252 cancelNotificationLocked(r, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001253 }
1254 if (canceledSomething) {
1255 updateLightsLocked();
1256 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001257 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001258 }
1259 }
1260
Dianne Hackborn41203752012-08-31 14:05:51 -07001261 public void cancelNotificationWithTag(String pkg, String tag, int id, int userId) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001262 checkCallerIsSystemOrSameApp(pkg);
Dianne Hackborn41203752012-08-31 14:05:51 -07001263 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
1264 Binder.getCallingUid(), userId, false, true, "cancelNotificationWithTag", pkg);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001265 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -07001266 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001267 Binder.getCallingUid() == Process.SYSTEM_UID
Dianne Hackborn41203752012-08-31 14:05:51 -07001268 ? 0 : Notification.FLAG_FOREGROUND_SERVICE, false, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 }
1270
Dianne Hackborn41203752012-08-31 14:05:51 -07001271 public void cancelAllNotifications(String pkg, int userId) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001272 checkCallerIsSystemOrSameApp(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001273
Dianne Hackborn41203752012-08-31 14:05:51 -07001274 userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(),
1275 Binder.getCallingUid(), userId, true, true, "cancelAllNotifications", pkg);
1276
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001277 // Calling from user space, don't allow the canceling of actively
1278 // running foreground services.
Dianne Hackborn41203752012-08-31 14:05:51 -07001279 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true, userId);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001280 }
1281
Daniel Sandler0da673f2012-04-11 12:33:16 -04001282 void checkCallerIsSystem() {
1283 int uid = Binder.getCallingUid();
Dianne Hackborn0c380492012-08-20 17:23:30 -07001284 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) {
Daniel Sandler0da673f2012-04-11 12:33:16 -04001285 return;
1286 }
1287 throw new SecurityException("Disallowed call for uid " + uid);
1288 }
1289
1290 void checkCallerIsSystemOrSameApp(String pkg) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001291 int uid = Binder.getCallingUid();
Dianne Hackborn0c380492012-08-20 17:23:30 -07001292 if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001293 return;
1294 }
1295 try {
Amith Yamasanif203aee2012-08-29 18:41:53 -07001296 ApplicationInfo ai = AppGlobals.getPackageManager().getApplicationInfo(
1297 pkg, 0, UserHandle.getCallingUserId());
Dianne Hackbornf02b60a2012-08-16 10:48:27 -07001298 if (!UserHandle.isSameApp(ai.uid, uid)) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001299 throw new SecurityException("Calling uid " + uid + " gave package"
1300 + pkg + " which is owned by uid " + ai.uid);
1301 }
Amith Yamasanif203aee2012-08-29 18:41:53 -07001302 } catch (RemoteException re) {
1303 throw new SecurityException("Unknown package " + pkg + "\n" + re);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001304 }
1305 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001306
Dianne Hackborn41203752012-08-31 14:05:51 -07001307 void cancelAll(int userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 synchronized (mNotificationList) {
1309 final int N = mNotificationList.size();
1310 for (int i=N-1; i>=0; i--) {
1311 NotificationRecord r = mNotificationList.get(i);
1312
Dianne Hackborn41203752012-08-31 14:05:51 -07001313 if (r.userId != userId) {
1314 continue;
1315 }
1316
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 if ((r.notification.flags & (Notification.FLAG_ONGOING_EVENT
1318 | Notification.FLAG_NO_CLEAR)) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08001320 cancelNotificationLocked(r, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001321 }
1322 }
1323
1324 updateLightsLocked();
1325 }
1326 }
1327
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 // lock on mNotificationList
1329 private void updateLightsLocked()
1330 {
The Android Open Source Project10592532009-03-18 17:39:46 -07001331 // handle notification lights
1332 if (mLedNotification == null) {
1333 // get next notification, if any
1334 int n = mLights.size();
1335 if (n > 0) {
1336 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001337 }
1338 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001339
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001340 // Don't flash while we are in a call or screen is on
1341 if (mLedNotification == null || mInCall || mScreenOn) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001342 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001343 } else {
Mike Lockwood670f9322010-01-20 12:13:36 -05001344 int ledARGB = mLedNotification.notification.ledARGB;
1345 int ledOnMS = mLedNotification.notification.ledOnMS;
1346 int ledOffMS = mLedNotification.notification.ledOffMS;
1347 if ((mLedNotification.notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
1348 ledARGB = mDefaultNotificationColor;
1349 ledOnMS = mDefaultNotificationLedOn;
1350 ledOffMS = mDefaultNotificationLedOff;
1351 }
1352 if (mNotificationPulseEnabled) {
1353 // pulse repeatedly
1354 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
1355 ledOnMS, ledOffMS);
Mike Lockwood670f9322010-01-20 12:13:36 -05001356 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001357 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001358 }
1359
1360 // lock on mNotificationList
Dianne Hackborn41203752012-08-31 14:05:51 -07001361 private int indexOfNotificationLocked(String pkg, String tag, int id, int userId)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 {
1363 ArrayList<NotificationRecord> list = mNotificationList;
1364 final int len = list.size();
1365 for (int i=0; i<len; i++) {
1366 NotificationRecord r = list.get(i);
Dianne Hackborn41203752012-08-31 14:05:51 -07001367 if (r.userId != userId || r.id != id) {
1368 continue;
1369 }
Fred Quintana6ecaff12009-09-25 14:23:13 -07001370 if (tag == null) {
1371 if (r.tag != null) {
1372 continue;
1373 }
1374 } else {
1375 if (!tag.equals(r.tag)) {
1376 continue;
1377 }
1378 }
Dianne Hackborn41203752012-08-31 14:05:51 -07001379 if (r.pkg.equals(pkg)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001380 return i;
1381 }
1382 }
1383 return -1;
1384 }
1385
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001386 private void updateNotificationPulse() {
1387 synchronized (mNotificationList) {
1388 updateLightsLocked();
1389 }
1390 }
1391
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001392 // ======================================================================
1393 @Override
1394 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1395 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1396 != PackageManager.PERMISSION_GRANTED) {
1397 pw.println("Permission Denial: can't dump NotificationManager from from pid="
1398 + Binder.getCallingPid()
1399 + ", uid=" + Binder.getCallingUid());
1400 return;
1401 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001402
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 pw.println("Current Notification Manager state:");
1404
1405 int N;
1406
1407 synchronized (mToastQueue) {
1408 N = mToastQueue.size();
1409 if (N > 0) {
1410 pw.println(" Toast Queue:");
1411 for (int i=0; i<N; i++) {
1412 mToastQueue.get(i).dump(pw, " ");
1413 }
1414 pw.println(" ");
1415 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001416
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 }
1418
1419 synchronized (mNotificationList) {
1420 N = mNotificationList.size();
1421 if (N > 0) {
1422 pw.println(" Notification List:");
1423 for (int i=0; i<N; i++) {
1424 mNotificationList.get(i).dump(pw, " ", mContext);
1425 }
1426 pw.println(" ");
1427 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001429 N = mLights.size();
1430 if (N > 0) {
1431 pw.println(" Lights List:");
1432 for (int i=0; i<N; i++) {
1433 mLights.get(i).dump(pw, " ", mContext);
1434 }
1435 pw.println(" ");
1436 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001437
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001438 pw.println(" mSoundNotification=" + mSoundNotification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001440 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
1441 pw.println(" mSystemReady=" + mSystemReady);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001442 }
1443 }
1444}