blob: b22be76a2ec4dce170b781c70ffad041c9b7c893 [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
Joe Onorato18e69df2010-05-17 22:26:12 -070019import com.android.internal.statusbar.StatusBarNotification;
svetoslavganov75986cf2009-05-14 22:28:01 -070020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.app.ActivityManagerNative;
22import android.app.IActivityManager;
23import android.app.INotificationManager;
24import android.app.ITransientNotification;
25import android.app.Notification;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070026import android.app.NotificationManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.app.PendingIntent;
28import android.app.StatusBarManager;
29import android.content.BroadcastReceiver;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070030import android.content.ContentResolver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.content.Context;
32import android.content.Intent;
33import android.content.IntentFilter;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070034import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.content.pm.PackageManager;
36import android.content.pm.PackageManager.NameNotFoundException;
37import android.content.res.Resources;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070038import android.database.ContentObserver;
svetoslavganov75986cf2009-05-14 22:28:01 -070039import android.media.AudioManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.net.Uri;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.os.Binder;
Andy Stadler110988c2010-12-03 14:29:16 -080042import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.Handler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.os.IBinder;
45import android.os.Message;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070046import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070047import android.os.RemoteException;
Amith Yamasani742a6712011-05-04 14:49:28 -070048import android.os.UserId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049import android.os.Vibrator;
50import android.provider.Settings;
Daniel Sandlere96ffb12010-03-11 13:38:06 -050051import android.telephony.TelephonyManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070052import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.util.EventLog;
54import android.util.Log;
Andy Stadler110988c2010-12-03 14:29:16 -080055import android.util.Slog;
svetoslavganov75986cf2009-05-14 22:28:01 -070056import android.view.accessibility.AccessibilityEvent;
57import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.widget.Toast;
59
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import java.io.FileDescriptor;
61import java.io.PrintWriter;
62import java.util.ArrayList;
63import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080064
Daniel Sandlerd0a2f862010-08-03 15:29:31 -040065/** {@hide} */
66public class NotificationManagerService extends INotificationManager.Stub
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067{
68 private static final String TAG = "NotificationService";
69 private static final boolean DBG = false;
70
Joe Onoratobd73d012010-06-04 11:44:54 -070071 private static final int MAX_PACKAGE_NOTIFICATIONS = 50;
72
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 // message codes
74 private static final int MESSAGE_TIMEOUT = 2;
75
76 private static final int LONG_DELAY = 3500; // 3.5 seconds
77 private static final int SHORT_DELAY = 2000; // 2 seconds
Doug Zongkerab5c49c2009-12-04 10:31:43 -080078
79 private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080
81 private static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION;
Daniel Sandler49a2ad12012-03-28 15:46:39 -040082 private static final boolean SCORE_ONGOING_HIGHER = false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083
84 final Context mContext;
85 final IActivityManager mAm;
86 final IBinder mForegroundToken = new Binder();
87
88 private WorkerHandler mHandler;
Joe Onorato089de882010-04-12 08:18:45 -070089 private StatusBarManagerService mStatusBar;
Mike Lockwood3cb67a32009-11-27 14:25:58 -050090 private LightsService.Light mNotificationLight;
91 private LightsService.Light mAttentionLight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092
Mike Lockwood670f9322010-01-20 12:13:36 -050093 private int mDefaultNotificationColor;
94 private int mDefaultNotificationLedOn;
95 private int mDefaultNotificationLedOff;
96
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 private NotificationRecord mSoundNotification;
Jean-Michel Trivi211957f2010-03-26 18:19:33 -070098 private NotificationPlayer mSound;
Joe Onorato30275482009-07-08 17:09:14 -070099 private boolean mSystemReady;
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400100 private int mDisabledNotifications;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101
102 private NotificationRecord mVibrateNotification;
Jeff Brownc2346132012-04-13 01:55:38 -0700103 private Vibrator mVibrator;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500105 // for enabling and disabling notification pulse behavior
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400106 private boolean mScreenOn = true;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500107 private boolean mInCall = false;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500108 private boolean mNotificationPulseEnabled;
109
Fred Quintana6ecaff12009-09-25 14:23:13 -0700110 private final ArrayList<NotificationRecord> mNotificationList =
111 new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112
113 private ArrayList<ToastRecord> mToastQueue;
114
115 private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 private NotificationRecord mLedNotification;
svetoslavganov75986cf2009-05-14 22:28:01 -0700117
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 private static String idDebugString(Context baseContext, String packageName, int id) {
119 Context c = null;
120
121 if (packageName != null) {
122 try {
123 c = baseContext.createPackageContext(packageName, 0);
124 } catch (NameNotFoundException e) {
125 c = baseContext;
126 }
127 } else {
128 c = baseContext;
129 }
130
131 String pkg;
132 String type;
133 String name;
134
135 Resources r = c.getResources();
136 try {
137 return r.getResourceName(id);
138 } catch (Resources.NotFoundException e) {
139 return "<name unknown>";
140 }
141 }
142
143 private static final class NotificationRecord
144 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700145 final String pkg;
146 final String tag;
147 final int id;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700148 final int uid;
149 final int initialPid;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700150 final Notification notification;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500151 final int score;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 IBinder statusBarKey;
153
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500154 NotificationRecord(String pkg, String tag, int id, int uid, int initialPid, int score, Notification notification)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 {
156 this.pkg = pkg;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700157 this.tag = tag;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 this.id = id;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700159 this.uid = uid;
160 this.initialPid = initialPid;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500161 this.score = score;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162 this.notification = notification;
163 }
Fred Quintana6ecaff12009-09-25 14:23:13 -0700164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 void dump(PrintWriter pw, String prefix, Context baseContext) {
166 pw.println(prefix + this);
167 pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
168 + " / " + idDebugString(baseContext, this.pkg, notification.icon));
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500169 pw.println(prefix + " pri=" + notification.priority);
170 pw.println(prefix + " score=" + this.score);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 pw.println(prefix + " contentIntent=" + notification.contentIntent);
172 pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
173 pw.println(prefix + " tickerText=" + notification.tickerText);
174 pw.println(prefix + " contentView=" + notification.contentView);
Amith Yamasani0dedffd2012-03-30 10:47:23 -0700175 pw.println(prefix + " uid=" + uid);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 pw.println(prefix + " defaults=0x" + Integer.toHexString(notification.defaults));
177 pw.println(prefix + " flags=0x" + Integer.toHexString(notification.flags));
178 pw.println(prefix + " sound=" + notification.sound);
179 pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate));
180 pw.println(prefix + " ledARGB=0x" + Integer.toHexString(notification.ledARGB)
181 + " ledOnMS=" + notification.ledOnMS
182 + " ledOffMS=" + notification.ledOffMS);
183 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 @Override
186 public final String toString()
187 {
188 return "NotificationRecord{"
189 + Integer.toHexString(System.identityHashCode(this))
190 + " pkg=" + pkg
Fred Quintana6ecaff12009-09-25 14:23:13 -0700191 + " id=" + Integer.toHexString(id)
Daniel Sandlere40451a2011-02-03 14:51:35 -0500192 + " tag=" + tag
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500193 + " score=" + score
Daniel Sandlere40451a2011-02-03 14:51:35 -0500194 + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 }
196 }
197
198 private static final class ToastRecord
199 {
200 final int pid;
201 final String pkg;
202 final ITransientNotification callback;
203 int duration;
204
205 ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
206 {
207 this.pid = pid;
208 this.pkg = pkg;
209 this.callback = callback;
210 this.duration = duration;
211 }
212
213 void update(int duration) {
214 this.duration = duration;
215 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800216
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 void dump(PrintWriter pw, String prefix) {
218 pw.println(prefix + this);
219 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800220
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 @Override
222 public final String toString()
223 {
224 return "ToastRecord{"
225 + Integer.toHexString(System.identityHashCode(this))
226 + " pkg=" + pkg
227 + " callback=" + callback
228 + " duration=" + duration;
229 }
230 }
231
Joe Onorato089de882010-04-12 08:18:45 -0700232 private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks
233 = new StatusBarManagerService.NotificationCallbacks() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234
235 public void onSetDisabled(int status) {
236 synchronized (mNotificationList) {
237 mDisabledNotifications = status;
238 if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) {
239 // cancel whatever's going on
240 long identity = Binder.clearCallingIdentity();
241 try {
242 mSound.stop();
243 }
244 finally {
245 Binder.restoreCallingIdentity(identity);
246 }
247
248 identity = Binder.clearCallingIdentity();
249 try {
250 mVibrator.cancel();
251 }
252 finally {
253 Binder.restoreCallingIdentity(identity);
254 }
255 }
256 }
257 }
258
259 public void onClearAll() {
260 cancelAll();
261 }
262
Fred Quintana6ecaff12009-09-25 14:23:13 -0700263 public void onNotificationClick(String pkg, String tag, int id) {
264 cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL,
jhtop.kim2e448f72011-07-13 17:15:32 +0900265 Notification.FLAG_FOREGROUND_SERVICE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800266 }
267
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400268 public void onNotificationClear(String pkg, String tag, int id) {
Joe Onorato46439ce2010-11-19 13:56:21 -0800269 cancelNotification(pkg, tag, id, 0,
270 Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE,
271 true);
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400272 }
273
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 public void onPanelRevealed() {
275 synchronized (mNotificationList) {
276 // sound
277 mSoundNotification = null;
278 long identity = Binder.clearCallingIdentity();
279 try {
280 mSound.stop();
281 }
282 finally {
283 Binder.restoreCallingIdentity(identity);
284 }
285
286 // vibrate
287 mVibrateNotification = null;
288 identity = Binder.clearCallingIdentity();
289 try {
290 mVibrator.cancel();
291 }
292 finally {
293 Binder.restoreCallingIdentity(identity);
294 }
295
296 // light
297 mLights.clear();
298 mLedNotification = null;
299 updateLightsLocked();
300 }
301 }
Joe Onorato005847b2010-06-04 16:08:02 -0400302
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700303 public void onNotificationError(String pkg, String tag, int id,
304 int uid, int initialPid, String message) {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400305 Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id
306 + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")");
Joe Onorato46439ce2010-11-19 13:56:21 -0800307 cancelNotification(pkg, tag, id, 0, 0, false);
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700308 long ident = Binder.clearCallingIdentity();
309 try {
310 ActivityManagerNative.getDefault().crashApplication(uid, initialPid, pkg,
311 "Bad notification posted from package " + pkg
312 + ": " + message);
313 } catch (RemoteException e) {
314 }
315 Binder.restoreCallingIdentity(ident);
Joe Onorato005847b2010-06-04 16:08:02 -0400316 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800317 };
318
319 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
320 @Override
321 public void onReceive(Context context, Intent intent) {
322 String action = intent.getAction();
323
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800324 boolean queryRestart = false;
325
Mike Lockwood541c9942011-06-12 19:35:45 -0400326 if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800327 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Daniel Sandleraac0eb02011-08-06 22:51:56 -0400328 || action.equals(Intent.ACTION_PACKAGE_CHANGED)
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800329 || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800330 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800331 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800332 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800333 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800334 } else if (queryRestart) {
335 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800336 } else {
337 Uri uri = intent.getData();
338 if (uri == null) {
339 return;
340 }
341 String pkgName = uri.getSchemeSpecificPart();
342 if (pkgName == null) {
343 return;
344 }
345 pkgList = new String[]{pkgName};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800347 if (pkgList != null && (pkgList.length > 0)) {
348 for (String pkgName : pkgList) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800349 cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800350 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 }
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400352 } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
353 // Keep track of screen on/off state, but do not turn off the notification light
354 // until user passes through the lock screen or views the notification.
355 mScreenOn = true;
356 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
357 mScreenOn = false;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500358 } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400359 mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(
360 TelephonyManager.EXTRA_STATE_OFFHOOK));
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500361 updateNotificationPulse();
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400362 } else if (action.equals(Intent.ACTION_USER_PRESENT)) {
363 // turn off LED when user passes through lock screen
364 mNotificationLight.turnOff();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800365 }
366 }
367 };
368
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700369 class SettingsObserver extends ContentObserver {
370 SettingsObserver(Handler handler) {
371 super(handler);
372 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800373
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700374 void observe() {
375 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500376 resolver.registerContentObserver(Settings.System.getUriFor(
377 Settings.System.NOTIFICATION_LIGHT_PULSE), false, this);
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700378 update();
379 }
380
381 @Override public void onChange(boolean selfChange) {
382 update();
383 }
384
385 public void update() {
386 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500387 boolean pulseEnabled = Settings.System.getInt(resolver,
388 Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0;
389 if (mNotificationPulseEnabled != pulseEnabled) {
390 mNotificationPulseEnabled = pulseEnabled;
391 updateNotificationPulse();
392 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700393 }
394 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500395
Joe Onorato089de882010-04-12 08:18:45 -0700396 NotificationManagerService(Context context, StatusBarManagerService statusBar,
Mike Lockwood3a322132009-11-24 00:30:52 -0500397 LightsService lights)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 {
399 super();
400 mContext = context;
Jeff Brownc2346132012-04-13 01:55:38 -0700401 mVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800402 mAm = ActivityManagerNative.getDefault();
Jean-Michel Trivi211957f2010-03-26 18:19:33 -0700403 mSound = new NotificationPlayer(TAG);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 mSound.setUsesWakeLock(context);
405 mToastQueue = new ArrayList<ToastRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800406 mHandler = new WorkerHandler();
San Mehat3ee13172010-02-04 20:54:43 -0800407
Joe Onorato089de882010-04-12 08:18:45 -0700408 mStatusBar = statusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 statusBar.setNotificationCallbacks(mNotificationCallbacks);
410
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500411 mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS);
412 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
413
Mike Lockwood670f9322010-01-20 12:13:36 -0500414 Resources resources = mContext.getResources();
415 mDefaultNotificationColor = resources.getColor(
416 com.android.internal.R.color.config_defaultNotificationColor);
417 mDefaultNotificationLedOn = resources.getInteger(
418 com.android.internal.R.integer.config_defaultNotificationLedOn);
419 mDefaultNotificationLedOff = resources.getInteger(
420 com.android.internal.R.integer.config_defaultNotificationLedOff);
421
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400422 // Don't start allowing notifications until the setup wizard has run once.
423 // After that, including subsequent boots, init with notifications turned on.
424 // This works on the first boot because the setup wizard will toggle this
425 // flag at least once and we'll go back to 0 after that.
426 if (0 == Settings.Secure.getInt(mContext.getContentResolver(),
427 Settings.Secure.DEVICE_PROVISIONED, 0)) {
428 mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
429 }
430
Mike Lockwood35e16bf2010-11-30 19:53:36 -0500431 // register for various Intents
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 IntentFilter filter = new IntentFilter();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500433 filter.addAction(Intent.ACTION_SCREEN_ON);
434 filter.addAction(Intent.ACTION_SCREEN_OFF);
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500435 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
Mike Lockwood63b5ad92011-08-30 09:55:30 -0400436 filter.addAction(Intent.ACTION_USER_PRESENT);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800438 IntentFilter pkgFilter = new IntentFilter();
439 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
Daniel Sandleraac0eb02011-08-06 22:51:56 -0400440 pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800441 pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
442 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
443 pkgFilter.addDataScheme("package");
444 mContext.registerReceiver(mIntentReceiver, pkgFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800445 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800446 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800447
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500448 SettingsObserver observer = new SettingsObserver(mHandler);
449 observer.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 }
451
Joe Onorato30275482009-07-08 17:09:14 -0700452 void systemReady() {
453 // no beeping until we're basically done booting
454 mSystemReady = true;
455 }
456
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 // Toasts
458 // ============================================================================
459 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
460 {
Daniel Sandlera7035902010-03-30 15:45:31 -0400461 if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462
463 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800464 Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800465 return ;
466 }
467
468 synchronized (mToastQueue) {
469 int callingPid = Binder.getCallingPid();
470 long callingId = Binder.clearCallingIdentity();
471 try {
472 ToastRecord record;
473 int index = indexOfToastLocked(pkg, callback);
474 // If it's already in the queue, we update it in place, we don't
475 // move it to the end of the queue.
476 if (index >= 0) {
477 record = mToastQueue.get(index);
478 record.update(duration);
479 } else {
Vairavan Srinivasanf9eb06c2011-01-21 18:08:36 -0800480 // Limit the number of toasts that any given package except the android
481 // package can enqueue. Prevents DOS attacks and deals with leaks.
482 if (!"android".equals(pkg)) {
483 int count = 0;
484 final int N = mToastQueue.size();
485 for (int i=0; i<N; i++) {
486 final ToastRecord r = mToastQueue.get(i);
487 if (r.pkg.equals(pkg)) {
488 count++;
489 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
490 Slog.e(TAG, "Package has already posted " + count
491 + " toasts. Not showing more. Package=" + pkg);
492 return;
493 }
494 }
495 }
496 }
497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 record = new ToastRecord(callingPid, pkg, callback, duration);
499 mToastQueue.add(record);
500 index = mToastQueue.size() - 1;
501 keepProcessAliveLocked(callingPid);
502 }
503 // If it's at index 0, it's the current toast. It doesn't matter if it's
504 // new or just been updated. Call back and tell it to show itself.
505 // If the callback fails, this will remove it from the list, so don't
506 // assume that it's valid after this.
507 if (index == 0) {
508 showNextToastLocked();
509 }
510 } finally {
511 Binder.restoreCallingIdentity(callingId);
512 }
513 }
514 }
515
516 public void cancelToast(String pkg, ITransientNotification callback) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800517 Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518
519 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800520 Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 return ;
522 }
523
524 synchronized (mToastQueue) {
525 long callingId = Binder.clearCallingIdentity();
526 try {
527 int index = indexOfToastLocked(pkg, callback);
528 if (index >= 0) {
529 cancelToastLocked(index);
530 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800531 Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 }
533 } finally {
534 Binder.restoreCallingIdentity(callingId);
535 }
536 }
537 }
538
539 private void showNextToastLocked() {
540 ToastRecord record = mToastQueue.get(0);
541 while (record != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800542 if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 try {
544 record.callback.show();
545 scheduleTimeoutLocked(record, false);
546 return;
547 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800548 Slog.w(TAG, "Object died trying to show notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800549 + " in package " + record.pkg);
550 // remove it from the list and let the process die
551 int index = mToastQueue.indexOf(record);
552 if (index >= 0) {
553 mToastQueue.remove(index);
554 }
555 keepProcessAliveLocked(record.pid);
556 if (mToastQueue.size() > 0) {
557 record = mToastQueue.get(0);
558 } else {
559 record = null;
560 }
561 }
562 }
563 }
564
565 private void cancelToastLocked(int index) {
566 ToastRecord record = mToastQueue.get(index);
567 try {
568 record.callback.hide();
569 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800570 Slog.w(TAG, "Object died trying to hide notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 + " in package " + record.pkg);
572 // don't worry about this, we're about to remove it from
573 // the list anyway
574 }
575 mToastQueue.remove(index);
576 keepProcessAliveLocked(record.pid);
577 if (mToastQueue.size() > 0) {
578 // Show the next one. If the callback fails, this will remove
579 // it from the list, so don't assume that the list hasn't changed
580 // after this point.
581 showNextToastLocked();
582 }
583 }
584
585 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
586 {
587 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
588 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
589 mHandler.removeCallbacksAndMessages(r);
590 mHandler.sendMessageDelayed(m, delay);
591 }
592
593 private void handleTimeout(ToastRecord record)
594 {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800595 if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 synchronized (mToastQueue) {
597 int index = indexOfToastLocked(record.pkg, record.callback);
598 if (index >= 0) {
599 cancelToastLocked(index);
600 }
601 }
602 }
603
604 // lock on mToastQueue
605 private int indexOfToastLocked(String pkg, ITransientNotification callback)
606 {
607 IBinder cbak = callback.asBinder();
608 ArrayList<ToastRecord> list = mToastQueue;
609 int len = list.size();
610 for (int i=0; i<len; i++) {
611 ToastRecord r = list.get(i);
612 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
613 return i;
614 }
615 }
616 return -1;
617 }
618
619 // lock on mToastQueue
620 private void keepProcessAliveLocked(int pid)
621 {
622 int toastCount = 0; // toasts from this pid
623 ArrayList<ToastRecord> list = mToastQueue;
624 int N = list.size();
625 for (int i=0; i<N; i++) {
626 ToastRecord r = list.get(i);
627 if (r.pid == pid) {
628 toastCount++;
629 }
630 }
631 try {
632 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
633 } catch (RemoteException e) {
634 // Shouldn't happen.
635 }
636 }
637
638 private final class WorkerHandler extends Handler
639 {
640 @Override
641 public void handleMessage(Message msg)
642 {
643 switch (msg.what)
644 {
645 case MESSAGE_TIMEOUT:
646 handleTimeout((ToastRecord)msg.obj);
647 break;
648 }
649 }
650 }
651
652
653 // Notifications
654 // ============================================================================
Andy Stadler110988c2010-12-03 14:29:16 -0800655 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800656 public void enqueueNotification(String pkg, int id, Notification notification, int[] idOut)
657 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700658 enqueueNotificationWithTag(pkg, null /* tag */, id, notification, idOut);
659 }
660
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400661 public void enqueueNotificationWithTag(String pkg, String tag, int id, Notification notification,
662 int[] idOut)
Fred Quintana6ecaff12009-09-25 14:23:13 -0700663 {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400664 enqueueNotificationInternal(pkg, Binder.getCallingUid(), Binder.getCallingPid(),
665 tag, id, notification, idOut);
666 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500667
668 private final static int clamp(int x, int low, int high) {
669 return (x < low) ? low : ((x > high) ? high : x);
Daniel Sandlere40451a2011-02-03 14:51:35 -0500670 }
671
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500672
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400673 // Not exposed via Binder; for system use only (otherwise malicious apps could spoof the
674 // uid/pid of another application)
675 public void enqueueNotificationInternal(String pkg, int callingUid, int callingPid,
676 String tag, int id, Notification notification, int[] idOut)
677 {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700678 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800679
Joe Onoratobd73d012010-06-04 11:44:54 -0700680 // Limit the number of notifications that any given package except the android
681 // package can enqueue. Prevents DOS attacks and deals with leaks.
682 if (!"android".equals(pkg)) {
683 synchronized (mNotificationList) {
684 int count = 0;
685 final int N = mNotificationList.size();
686 for (int i=0; i<N; i++) {
687 final NotificationRecord r = mNotificationList.get(i);
688 if (r.pkg.equals(pkg)) {
689 count++;
690 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
691 Slog.e(TAG, "Package has already posted " + count
692 + " notifications. Not showing more. package=" + pkg);
693 return;
694 }
695 }
696 }
697 }
698 }
699
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800700 // This conditional is a dirty hack to limit the logging done on
701 // behalf of the download manager without affecting other apps.
702 if (!pkg.equals("com.android.providers.downloads")
703 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Daniel Sandlerb64cb882011-11-29 23:48:29 -0500704 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, tag,
705 notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 }
707
708 if (pkg == null || notification == null) {
709 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
710 + " id=" + id + " notification=" + notification);
711 }
712 if (notification.icon != 0) {
713 if (notification.contentView == null) {
714 throw new IllegalArgumentException("contentView required: pkg=" + pkg
715 + " id=" + id + " notification=" + notification);
716 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800717 }
718
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500719 // === Scoring ===
720
721 // 0. Sanitize inputs
722 notification.priority = clamp(notification.priority, Notification.PRIORITY_MIN, Notification.PRIORITY_MAX);
723 // Migrate notification flags to scores
724 if (0 != (notification.flags & Notification.FLAG_HIGH_PRIORITY)) {
725 if (notification.priority < Notification.PRIORITY_MAX) notification.priority = Notification.PRIORITY_MAX;
Daniel Sandler49a2ad12012-03-28 15:46:39 -0400726 } else if (SCORE_ONGOING_HIGHER && 0 != (notification.flags & Notification.FLAG_ONGOING_EVENT)) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500727 if (notification.priority < Notification.PRIORITY_HIGH) notification.priority = Notification.PRIORITY_HIGH;
728 }
729
730 // 1. initial score: buckets of 10, around the app
731 int score = notification.priority * 10; //[-20..20]
732
733 // 2. Consult oracles (external heuristics)
734 // TODO(dsandler): oracles
735
736 // 3. Apply local heuristics & overrides
737
738 // blocked apps
739 // TODO(dsandler): add block db
740 if (pkg.startsWith("com.test.spammer.")) {
741 score = -1000;
742 }
743
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800744 synchronized (mNotificationList) {
Daniel Sandlere40451a2011-02-03 14:51:35 -0500745 NotificationRecord r = new NotificationRecord(pkg, tag, id,
746 callingUid, callingPid,
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500747 score,
Daniel Sandlere40451a2011-02-03 14:51:35 -0500748 notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 NotificationRecord old = null;
750
Fred Quintana6ecaff12009-09-25 14:23:13 -0700751 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800752 if (index < 0) {
753 mNotificationList.add(r);
754 } else {
755 old = mNotificationList.remove(index);
756 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700757 // Make sure we don't lose the foreground service state.
758 if (old != null) {
759 notification.flags |=
760 old.notification.flags&Notification.FLAG_FOREGROUND_SERVICE;
761 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800762 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800763
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700764 // Ensure if this is a foreground service that the proper additional
765 // flags are set.
766 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
767 notification.flags |= Notification.FLAG_ONGOING_EVENT
768 | Notification.FLAG_NO_CLEAR;
769 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800770
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800771 if (notification.icon != 0) {
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700772 StatusBarNotification n = new StatusBarNotification(pkg, id, tag,
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500773 r.uid, r.initialPid, score, notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 if (old != null && old.statusBarKey != null) {
775 r.statusBarKey = old.statusBarKey;
776 long identity = Binder.clearCallingIdentity();
777 try {
Joe Onorato18e69df2010-05-17 22:26:12 -0700778 mStatusBar.updateNotification(r.statusBarKey, n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 }
780 finally {
781 Binder.restoreCallingIdentity(identity);
782 }
783 } else {
784 long identity = Binder.clearCallingIdentity();
785 try {
Joe Onorato18e69df2010-05-17 22:26:12 -0700786 r.statusBarKey = mStatusBar.addNotification(n);
Mike Lockwoodece18ef2012-02-13 20:42:19 -0800787 if ((n.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
788 mAttentionLight.pulse();
789 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 }
791 finally {
792 Binder.restoreCallingIdentity(identity);
793 }
794 }
Joe Onorato30275482009-07-08 17:09:14 -0700795 sendAccessibilityEvent(notification, pkg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800796 } else {
Daniel Sandlere40451a2011-02-03 14:51:35 -0500797 Slog.e(TAG, "Ignoring notification with icon==0: " + notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 if (old != null && old.statusBarKey != null) {
799 long identity = Binder.clearCallingIdentity();
800 try {
Joe Onorato0cbda992010-05-02 16:28:15 -0700801 mStatusBar.removeNotification(old.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800802 }
803 finally {
804 Binder.restoreCallingIdentity(identity);
805 }
806 }
807 }
808
809 // If we're not supposed to beep, vibrate, etc. then don't.
810 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
811 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -0700812 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
813 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -0800814
815 final AudioManager audioManager = (AudioManager) mContext
816 .getSystemService(Context.AUDIO_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 // sound
818 final boolean useDefaultSound =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800819 (notification.defaults & Notification.DEFAULT_SOUND) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 if (useDefaultSound || notification.sound != null) {
821 Uri uri;
822 if (useDefaultSound) {
823 uri = Settings.System.DEFAULT_NOTIFICATION_URI;
824 } else {
825 uri = notification.sound;
826 }
827 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
828 int audioStreamType;
829 if (notification.audioStreamType >= 0) {
830 audioStreamType = notification.audioStreamType;
831 } else {
832 audioStreamType = DEFAULT_STREAM_TYPE;
833 }
834 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -0800835 // do not play notifications if stream volume is 0
836 // (typically because ringer mode is silent).
837 if (audioManager.getStreamVolume(audioStreamType) != 0) {
838 long identity = Binder.clearCallingIdentity();
839 try {
840 mSound.play(mContext, uri, looping, audioStreamType);
841 }
842 finally {
843 Binder.restoreCallingIdentity(identity);
844 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 }
846 }
847
848 // vibrate
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 final boolean useDefaultVibrate =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800850 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 if ((useDefaultVibrate || notification.vibrate != null)
852 && audioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_NOTIFICATION)) {
853 mVibrateNotification = r;
854
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800855 mVibrator.vibrate(useDefaultVibrate ? DEFAULT_VIBRATE_PATTERN
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 : notification.vibrate,
857 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
858 }
859 }
860
861 // this option doesn't shut off the lights
862
863 // light
864 // the most recent thing gets the light
865 mLights.remove(old);
866 if (mLedNotification == old) {
867 mLedNotification = null;
868 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800869 //Slog.i(TAG, "notification.lights="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800870 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
871 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
872 mLights.add(r);
873 updateLightsLocked();
874 } else {
875 if (old != null
876 && ((old.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0)) {
877 updateLightsLocked();
878 }
879 }
880 }
881
882 idOut[0] = id;
883 }
884
Joe Onorato30275482009-07-08 17:09:14 -0700885 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700886 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
887 if (!manager.isEnabled()) {
888 return;
889 }
890
891 AccessibilityEvent event =
892 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
893 event.setPackageName(packageName);
894 event.setClassName(Notification.class.getName());
895 event.setParcelableData(notification);
896 CharSequence tickerText = notification.tickerText;
897 if (!TextUtils.isEmpty(tickerText)) {
898 event.getText().add(tickerText);
899 }
900
901 manager.sendAccessibilityEvent(event);
902 }
903
Joe Onorato46439ce2010-11-19 13:56:21 -0800904 private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete) {
905 // tell the app
906 if (sendDelete) {
907 if (r.notification.deleteIntent != null) {
908 try {
909 r.notification.deleteIntent.send();
910 } catch (PendingIntent.CanceledException ex) {
911 // do nothing - there's no relevant way to recover, and
912 // no reason to let this propagate
913 Slog.w(TAG, "canceled PendingIntent for " + r.pkg, ex);
914 }
915 }
916 }
917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 // status bar
919 if (r.notification.icon != 0) {
920 long identity = Binder.clearCallingIdentity();
921 try {
Joe Onorato0cbda992010-05-02 16:28:15 -0700922 mStatusBar.removeNotification(r.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 }
924 finally {
925 Binder.restoreCallingIdentity(identity);
926 }
927 r.statusBarKey = null;
928 }
929
930 // sound
931 if (mSoundNotification == r) {
932 mSoundNotification = null;
933 long identity = Binder.clearCallingIdentity();
934 try {
935 mSound.stop();
936 }
937 finally {
938 Binder.restoreCallingIdentity(identity);
939 }
940 }
941
942 // vibrate
943 if (mVibrateNotification == r) {
944 mVibrateNotification = null;
945 long identity = Binder.clearCallingIdentity();
946 try {
947 mVibrator.cancel();
948 }
949 finally {
950 Binder.restoreCallingIdentity(identity);
951 }
952 }
953
954 // light
955 mLights.remove(r);
956 if (mLedNotification == r) {
957 mLedNotification = null;
958 }
959 }
960
961 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700962 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800963 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 */
Fred Quintana6ecaff12009-09-25 14:23:13 -0700965 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Joe Onorato46439ce2010-11-19 13:56:21 -0800966 int mustNotHaveFlags, boolean sendDelete) {
Daniel Sandlerb64cb882011-11-29 23:48:29 -0500967 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, tag,
968 mustHaveFlags, mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800969
970 synchronized (mNotificationList) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700971 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700973 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800974
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800975 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
976 return;
977 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700978 if ((r.notification.flags & mustNotHaveFlags) != 0) {
979 return;
980 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800981
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 mNotificationList.remove(index);
983
Joe Onorato46439ce2010-11-19 13:56:21 -0800984 cancelNotificationLocked(r, sendDelete);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 updateLightsLocked();
986 }
987 }
988 }
989
990 /**
991 * Cancels all notifications from a given package that have all of the
992 * {@code mustHaveFlags}.
993 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800994 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
995 int mustNotHaveFlags, boolean doit) {
Daniel Sandlerb64cb882011-11-29 23:48:29 -0500996 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, mustHaveFlags,
997 mustNotHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800998
999 synchronized (mNotificationList) {
1000 final int N = mNotificationList.size();
1001 boolean canceledSomething = false;
1002 for (int i = N-1; i >= 0; --i) {
1003 NotificationRecord r = mNotificationList.get(i);
1004 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
1005 continue;
1006 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001007 if ((r.notification.flags & mustNotHaveFlags) != 0) {
1008 continue;
1009 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 if (!r.pkg.equals(pkg)) {
1011 continue;
1012 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001013 canceledSomething = true;
1014 if (!doit) {
1015 return true;
1016 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001017 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08001018 cancelNotificationLocked(r, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001019 }
1020 if (canceledSomething) {
1021 updateLightsLocked();
1022 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001023 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 }
1025 }
1026
Andy Stadler110988c2010-12-03 14:29:16 -08001027 @Deprecated
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001028 public void cancelNotification(String pkg, int id) {
Fred Quintana6ecaff12009-09-25 14:23:13 -07001029 cancelNotificationWithTag(pkg, null /* tag */, id);
1030 }
1031
1032 public void cancelNotificationWithTag(String pkg, String tag, int id) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001033 checkIncomingCall(pkg);
1034 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -07001035 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001036 Binder.getCallingUid() == Process.SYSTEM_UID
Joe Onorato46439ce2010-11-19 13:56:21 -08001037 ? 0 : Notification.FLAG_FOREGROUND_SERVICE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001038 }
1039
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001040 public void cancelAllNotifications(String pkg) {
1041 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001042
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001043 // Calling from user space, don't allow the canceling of actively
1044 // running foreground services.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001045 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 }
1047
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001048 void checkIncomingCall(String pkg) {
1049 int uid = Binder.getCallingUid();
1050 if (uid == Process.SYSTEM_UID || uid == 0) {
1051 return;
1052 }
1053 try {
1054 ApplicationInfo ai = mContext.getPackageManager().getApplicationInfo(
1055 pkg, 0);
Amith Yamasani742a6712011-05-04 14:49:28 -07001056 if (!UserId.isSameApp(ai.uid, uid)) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001057 throw new SecurityException("Calling uid " + uid + " gave package"
1058 + pkg + " which is owned by uid " + ai.uid);
1059 }
1060 } catch (PackageManager.NameNotFoundException e) {
1061 throw new SecurityException("Unknown package " + pkg);
1062 }
1063 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001064
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001065 void cancelAll() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 synchronized (mNotificationList) {
1067 final int N = mNotificationList.size();
1068 for (int i=N-1; i>=0; i--) {
1069 NotificationRecord r = mNotificationList.get(i);
1070
1071 if ((r.notification.flags & (Notification.FLAG_ONGOING_EVENT
1072 | Notification.FLAG_NO_CLEAR)) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001073 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08001074 cancelNotificationLocked(r, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 }
1076 }
1077
1078 updateLightsLocked();
1079 }
1080 }
1081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 // lock on mNotificationList
1083 private void updateLightsLocked()
1084 {
The Android Open Source Project10592532009-03-18 17:39:46 -07001085 // handle notification lights
1086 if (mLedNotification == null) {
1087 // get next notification, if any
1088 int n = mLights.size();
1089 if (n > 0) {
1090 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 }
1092 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001093
Mike Lockwood63b5ad92011-08-30 09:55:30 -04001094 // Don't flash while we are in a call or screen is on
1095 if (mLedNotification == null || mInCall || mScreenOn) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001096 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001097 } else {
Mike Lockwood670f9322010-01-20 12:13:36 -05001098 int ledARGB = mLedNotification.notification.ledARGB;
1099 int ledOnMS = mLedNotification.notification.ledOnMS;
1100 int ledOffMS = mLedNotification.notification.ledOffMS;
1101 if ((mLedNotification.notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
1102 ledARGB = mDefaultNotificationColor;
1103 ledOnMS = mDefaultNotificationLedOn;
1104 ledOffMS = mDefaultNotificationLedOff;
1105 }
1106 if (mNotificationPulseEnabled) {
1107 // pulse repeatedly
1108 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
1109 ledOnMS, ledOffMS);
Mike Lockwood670f9322010-01-20 12:13:36 -05001110 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001111 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 }
1113
1114 // lock on mNotificationList
Fred Quintana6ecaff12009-09-25 14:23:13 -07001115 private int indexOfNotificationLocked(String pkg, String tag, int id)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 {
1117 ArrayList<NotificationRecord> list = mNotificationList;
1118 final int len = list.size();
1119 for (int i=0; i<len; i++) {
1120 NotificationRecord r = list.get(i);
Fred Quintana6ecaff12009-09-25 14:23:13 -07001121 if (tag == null) {
1122 if (r.tag != null) {
1123 continue;
1124 }
1125 } else {
1126 if (!tag.equals(r.tag)) {
1127 continue;
1128 }
1129 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 if (r.id == id && r.pkg.equals(pkg)) {
1131 return i;
1132 }
1133 }
1134 return -1;
1135 }
1136
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001137 private void updateNotificationPulse() {
1138 synchronized (mNotificationList) {
1139 updateLightsLocked();
1140 }
1141 }
1142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 // ======================================================================
1144 @Override
1145 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1146 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1147 != PackageManager.PERMISSION_GRANTED) {
1148 pw.println("Permission Denial: can't dump NotificationManager from from pid="
1149 + Binder.getCallingPid()
1150 + ", uid=" + Binder.getCallingUid());
1151 return;
1152 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001153
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001154 pw.println("Current Notification Manager state:");
1155
1156 int N;
1157
1158 synchronized (mToastQueue) {
1159 N = mToastQueue.size();
1160 if (N > 0) {
1161 pw.println(" Toast Queue:");
1162 for (int i=0; i<N; i++) {
1163 mToastQueue.get(i).dump(pw, " ");
1164 }
1165 pw.println(" ");
1166 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001168 }
1169
1170 synchronized (mNotificationList) {
1171 N = mNotificationList.size();
1172 if (N > 0) {
1173 pw.println(" Notification List:");
1174 for (int i=0; i<N; i++) {
1175 mNotificationList.get(i).dump(pw, " ", mContext);
1176 }
1177 pw.println(" ");
1178 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 N = mLights.size();
1181 if (N > 0) {
1182 pw.println(" Lights List:");
1183 for (int i=0; i<N; i++) {
1184 mLights.get(i).dump(pw, " ", mContext);
1185 }
1186 pw.println(" ");
1187 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001189 pw.println(" mSoundNotification=" + mSoundNotification);
1190 pw.println(" mSound=" + mSound);
1191 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001192 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
1193 pw.println(" mSystemReady=" + mSystemReady);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001194 }
1195 }
1196}