blob: 6de7e6a52703560a47a9c33bb8aeac996e3f2088 [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;
Joe Onorato7a0f36b2010-06-07 10:24:36 -070020import com.android.server.StatusBarManagerService;
svetoslavganov75986cf2009-05-14 22:28:01 -070021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.app.ActivityManagerNative;
23import android.app.IActivityManager;
24import android.app.INotificationManager;
25import android.app.ITransientNotification;
26import android.app.Notification;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070027import android.app.NotificationManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.app.PendingIntent;
29import android.app.StatusBarManager;
30import android.content.BroadcastReceiver;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070031import android.content.ComponentName;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070032import android.content.ContentResolver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.content.Context;
34import android.content.Intent;
35import android.content.IntentFilter;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070036import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.content.pm.PackageManager;
38import android.content.pm.PackageManager.NameNotFoundException;
39import android.content.res.Resources;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070040import android.database.ContentObserver;
Mike Lockwoodff2544c2010-06-28 09:17:50 -040041import android.hardware.Usb;
svetoslavganov75986cf2009-05-14 22:28:01 -070042import android.media.AudioManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.net.Uri;
44import android.os.BatteryManager;
Mike Lockwoodff2544c2010-06-28 09:17:50 -040045import android.os.Bundle;
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;
50import android.os.Power;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070051import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070052import android.os.RemoteException;
Mike Lockwooded760372009-07-09 07:07:27 -040053import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.os.Vibrator;
55import android.provider.Settings;
Daniel Sandlere96ffb12010-03-11 13:38:06 -050056import android.telephony.TelephonyManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080059import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.util.Log;
svetoslavganov75986cf2009-05-14 22:28:01 -070061import android.view.accessibility.AccessibilityEvent;
62import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.widget.Toast;
Daniel Sandler0f0b11c2010-08-04 15:54:58 -040064import android.widget.Toast;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066import java.io.FileDescriptor;
67import java.io.PrintWriter;
68import java.util.ArrayList;
69import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070
Daniel Sandlerd0a2f862010-08-03 15:29:31 -040071/** {@hide} */
72public class NotificationManagerService extends INotificationManager.Stub
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073{
74 private static final String TAG = "NotificationService";
75 private static final boolean DBG = false;
76
Joe Onoratobd73d012010-06-04 11:44:54 -070077 private static final int MAX_PACKAGE_NOTIFICATIONS = 50;
78
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 // message codes
80 private static final int MESSAGE_TIMEOUT = 2;
81
82 private static final int LONG_DELAY = 3500; // 3.5 seconds
83 private static final int SHORT_DELAY = 2000; // 2 seconds
Doug Zongkerab5c49c2009-12-04 10:31:43 -080084
85 private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086
87 private static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION;
88
89 final Context mContext;
90 final IActivityManager mAm;
91 final IBinder mForegroundToken = new Binder();
92
93 private WorkerHandler mHandler;
Joe Onorato089de882010-04-12 08:18:45 -070094 private StatusBarManagerService mStatusBar;
Mike Lockwood3a322132009-11-24 00:30:52 -050095 private LightsService mLightsService;
Mike Lockwood3cb67a32009-11-27 14:25:58 -050096 private LightsService.Light mNotificationLight;
97 private LightsService.Light mAttentionLight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098
Mike Lockwood670f9322010-01-20 12:13:36 -050099 private int mDefaultNotificationColor;
100 private int mDefaultNotificationLedOn;
101 private int mDefaultNotificationLedOff;
102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 private NotificationRecord mSoundNotification;
Jean-Michel Trivi211957f2010-03-26 18:19:33 -0700104 private NotificationPlayer mSound;
Joe Onorato30275482009-07-08 17:09:14 -0700105 private boolean mSystemReady;
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400106 private int mDisabledNotifications;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107
108 private NotificationRecord mVibrateNotification;
109 private Vibrator mVibrator = new Vibrator();
110
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500111 // for enabling and disabling notification pulse behavior
112 private boolean mScreenOn = true;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500113 private boolean mInCall = false;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500114 private boolean mNotificationPulseEnabled;
Mike Lockwood2117f6f2010-09-09 09:48:08 -0400115 // This is true if we have received a new notification while the screen is off
116 // (that is, if mLedNotification was set while the screen was off)
117 // This is reset to false when the screen is turned on.
118 private boolean mPendingPulseNotification;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500119
120 // for adb connected notifications
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700121 private boolean mAdbNotificationShown = false;
122 private Notification mAdbNotification;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800123
Fred Quintana6ecaff12009-09-25 14:23:13 -0700124 private final ArrayList<NotificationRecord> mNotificationList =
125 new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126
127 private ArrayList<ToastRecord> mToastQueue;
128
129 private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 private NotificationRecord mLedNotification;
svetoslavganov75986cf2009-05-14 22:28:01 -0700131
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 private static String idDebugString(Context baseContext, String packageName, int id) {
133 Context c = null;
134
135 if (packageName != null) {
136 try {
137 c = baseContext.createPackageContext(packageName, 0);
138 } catch (NameNotFoundException e) {
139 c = baseContext;
140 }
141 } else {
142 c = baseContext;
143 }
144
145 String pkg;
146 String type;
147 String name;
148
149 Resources r = c.getResources();
150 try {
151 return r.getResourceName(id);
152 } catch (Resources.NotFoundException e) {
153 return "<name unknown>";
154 }
155 }
156
157 private static final class NotificationRecord
158 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700159 final String pkg;
160 final String tag;
161 final int id;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700162 final int uid;
163 final int initialPid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 ITransientNotification callback;
165 int duration;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700166 final Notification notification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 IBinder statusBarKey;
168
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700169 NotificationRecord(String pkg, String tag, int id, int uid, int initialPid,
170 Notification notification)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 {
172 this.pkg = pkg;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700173 this.tag = tag;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 this.id = id;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700175 this.uid = uid;
176 this.initialPid = initialPid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 this.notification = notification;
178 }
Fred Quintana6ecaff12009-09-25 14:23:13 -0700179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 void dump(PrintWriter pw, String prefix, Context baseContext) {
181 pw.println(prefix + this);
182 pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
183 + " / " + idDebugString(baseContext, this.pkg, notification.icon));
184 pw.println(prefix + " contentIntent=" + notification.contentIntent);
185 pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
186 pw.println(prefix + " tickerText=" + notification.tickerText);
187 pw.println(prefix + " contentView=" + notification.contentView);
188 pw.println(prefix + " defaults=0x" + Integer.toHexString(notification.defaults));
189 pw.println(prefix + " flags=0x" + Integer.toHexString(notification.flags));
190 pw.println(prefix + " sound=" + notification.sound);
191 pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate));
192 pw.println(prefix + " ledARGB=0x" + Integer.toHexString(notification.ledARGB)
193 + " ledOnMS=" + notification.ledOnMS
194 + " ledOffMS=" + notification.ledOffMS);
195 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 @Override
198 public final String toString()
199 {
200 return "NotificationRecord{"
201 + Integer.toHexString(System.identityHashCode(this))
202 + " pkg=" + pkg
Fred Quintana6ecaff12009-09-25 14:23:13 -0700203 + " id=" + Integer.toHexString(id)
204 + " tag=" + tag + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 }
206 }
207
208 private static final class ToastRecord
209 {
210 final int pid;
211 final String pkg;
212 final ITransientNotification callback;
213 int duration;
214
215 ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
216 {
217 this.pid = pid;
218 this.pkg = pkg;
219 this.callback = callback;
220 this.duration = duration;
221 }
222
223 void update(int duration) {
224 this.duration = duration;
225 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800226
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 void dump(PrintWriter pw, String prefix) {
228 pw.println(prefix + this);
229 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800230
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 @Override
232 public final String toString()
233 {
234 return "ToastRecord{"
235 + Integer.toHexString(System.identityHashCode(this))
236 + " pkg=" + pkg
237 + " callback=" + callback
238 + " duration=" + duration;
239 }
240 }
241
Joe Onorato089de882010-04-12 08:18:45 -0700242 private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks
243 = new StatusBarManagerService.NotificationCallbacks() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244
245 public void onSetDisabled(int status) {
246 synchronized (mNotificationList) {
247 mDisabledNotifications = status;
248 if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) {
249 // cancel whatever's going on
250 long identity = Binder.clearCallingIdentity();
251 try {
252 mSound.stop();
253 }
254 finally {
255 Binder.restoreCallingIdentity(identity);
256 }
257
258 identity = Binder.clearCallingIdentity();
259 try {
260 mVibrator.cancel();
261 }
262 finally {
263 Binder.restoreCallingIdentity(identity);
264 }
265 }
266 }
267 }
268
269 public void onClearAll() {
270 cancelAll();
271 }
272
Fred Quintana6ecaff12009-09-25 14:23:13 -0700273 public void onNotificationClick(String pkg, String tag, int id) {
274 cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL,
Joe Onorato46439ce2010-11-19 13:56:21 -0800275 Notification.FLAG_FOREGROUND_SERVICE, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 }
277
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400278 public void onNotificationClear(String pkg, String tag, int id) {
Joe Onorato46439ce2010-11-19 13:56:21 -0800279 cancelNotification(pkg, tag, id, 0,
280 Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE,
281 true);
Daniel Sandler0f0b11c2010-08-04 15:54:58 -0400282 }
283
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 public void onPanelRevealed() {
285 synchronized (mNotificationList) {
286 // sound
287 mSoundNotification = null;
288 long identity = Binder.clearCallingIdentity();
289 try {
290 mSound.stop();
291 }
292 finally {
293 Binder.restoreCallingIdentity(identity);
294 }
295
296 // vibrate
297 mVibrateNotification = null;
298 identity = Binder.clearCallingIdentity();
299 try {
300 mVibrator.cancel();
301 }
302 finally {
303 Binder.restoreCallingIdentity(identity);
304 }
305
306 // light
307 mLights.clear();
308 mLedNotification = null;
309 updateLightsLocked();
310 }
311 }
Joe Onorato005847b2010-06-04 16:08:02 -0400312
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700313 public void onNotificationError(String pkg, String tag, int id,
314 int uid, int initialPid, String message) {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400315 Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id
316 + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")");
Joe Onorato46439ce2010-11-19 13:56:21 -0800317 cancelNotification(pkg, tag, id, 0, 0, false);
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700318 long ident = Binder.clearCallingIdentity();
319 try {
320 ActivityManagerNative.getDefault().crashApplication(uid, initialPid, pkg,
321 "Bad notification posted from package " + pkg
322 + ": " + message);
323 } catch (RemoteException e) {
324 }
325 Binder.restoreCallingIdentity(ident);
Joe Onorato005847b2010-06-04 16:08:02 -0400326 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 };
328
329 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
330 @Override
331 public void onReceive(Context context, Intent intent) {
332 String action = intent.getAction();
333
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800334 boolean queryRestart = false;
335
Joe Onoratode1b3592010-10-25 20:36:47 -0700336 if (action.equals(Usb.ACTION_USB_STATE)) {
Mike Lockwoodff2544c2010-06-28 09:17:50 -0400337 Bundle extras = intent.getExtras();
338 boolean usbConnected = extras.getBoolean(Usb.USB_CONNECTED);
339 boolean adbEnabled = (Usb.USB_FUNCTION_ENABLED.equals(
340 extras.getString(Usb.USB_FUNCTION_ADB)));
341 updateAdbNotification(usbConnected && adbEnabled);
342 } else if (action.equals(Usb.ACTION_USB_DISCONNECTED)) {
343 updateAdbNotification(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800345 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800346 || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800347 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800348 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800349 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800350 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800351 } else if (queryRestart) {
352 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800353 } else {
354 Uri uri = intent.getData();
355 if (uri == null) {
356 return;
357 }
358 String pkgName = uri.getSchemeSpecificPart();
359 if (pkgName == null) {
360 return;
361 }
362 pkgList = new String[]{pkgName};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800364 if (pkgList != null && (pkgList.length > 0)) {
365 for (String pkgName : pkgList) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800366 cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800367 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500369 } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
370 mScreenOn = true;
371 updateNotificationPulse();
372 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
373 mScreenOn = false;
374 updateNotificationPulse();
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500375 } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
376 mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK));
377 updateNotificationPulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 }
379 }
380 };
381
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700382 class SettingsObserver extends ContentObserver {
383 SettingsObserver(Handler handler) {
384 super(handler);
385 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800386
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700387 void observe() {
388 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500389 resolver.registerContentObserver(Settings.System.getUriFor(
390 Settings.System.NOTIFICATION_LIGHT_PULSE), false, this);
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700391 update();
392 }
393
394 @Override public void onChange(boolean selfChange) {
395 update();
396 }
397
398 public void update() {
399 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500400 boolean pulseEnabled = Settings.System.getInt(resolver,
401 Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0;
402 if (mNotificationPulseEnabled != pulseEnabled) {
403 mNotificationPulseEnabled = pulseEnabled;
404 updateNotificationPulse();
405 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700406 }
407 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500408
Joe Onorato089de882010-04-12 08:18:45 -0700409 NotificationManagerService(Context context, StatusBarManagerService statusBar,
Mike Lockwood3a322132009-11-24 00:30:52 -0500410 LightsService lights)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 {
412 super();
413 mContext = context;
414 mAm = ActivityManagerNative.getDefault();
Jean-Michel Trivi211957f2010-03-26 18:19:33 -0700415 mSound = new NotificationPlayer(TAG);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416 mSound.setUsesWakeLock(context);
417 mToastQueue = new ArrayList<ToastRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 mHandler = new WorkerHandler();
San Mehat3ee13172010-02-04 20:54:43 -0800419
Joe Onorato089de882010-04-12 08:18:45 -0700420 mStatusBar = statusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 statusBar.setNotificationCallbacks(mNotificationCallbacks);
422
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500423 mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS);
424 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
425
Mike Lockwood670f9322010-01-20 12:13:36 -0500426 Resources resources = mContext.getResources();
427 mDefaultNotificationColor = resources.getColor(
428 com.android.internal.R.color.config_defaultNotificationColor);
429 mDefaultNotificationLedOn = resources.getInteger(
430 com.android.internal.R.integer.config_defaultNotificationLedOn);
431 mDefaultNotificationLedOff = resources.getInteger(
432 com.android.internal.R.integer.config_defaultNotificationLedOff);
433
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400434 // Don't start allowing notifications until the setup wizard has run once.
435 // After that, including subsequent boots, init with notifications turned on.
436 // This works on the first boot because the setup wizard will toggle this
437 // flag at least once and we'll go back to 0 after that.
438 if (0 == Settings.Secure.getInt(mContext.getContentResolver(),
439 Settings.Secure.DEVICE_PROVISIONED, 0)) {
440 mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
441 }
442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 // register for battery changed notifications
444 IntentFilter filter = new IntentFilter();
Mike Lockwoodff2544c2010-06-28 09:17:50 -0400445 filter.addAction(Usb.ACTION_USB_STATE);
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500446 filter.addAction(Intent.ACTION_SCREEN_ON);
447 filter.addAction(Intent.ACTION_SCREEN_OFF);
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500448 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800449 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800450 IntentFilter pkgFilter = new IntentFilter();
451 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
452 pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
453 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
454 pkgFilter.addDataScheme("package");
455 mContext.registerReceiver(mIntentReceiver, pkgFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800456 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800457 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800458
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500459 SettingsObserver observer = new SettingsObserver(mHandler);
460 observer.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 }
462
Joe Onorato30275482009-07-08 17:09:14 -0700463 void systemReady() {
464 // no beeping until we're basically done booting
465 mSystemReady = true;
466 }
467
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 // Toasts
469 // ============================================================================
470 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
471 {
Daniel Sandlera7035902010-03-30 15:45:31 -0400472 if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473
474 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800475 Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 return ;
477 }
478
479 synchronized (mToastQueue) {
480 int callingPid = Binder.getCallingPid();
481 long callingId = Binder.clearCallingIdentity();
482 try {
483 ToastRecord record;
484 int index = indexOfToastLocked(pkg, callback);
485 // If it's already in the queue, we update it in place, we don't
486 // move it to the end of the queue.
487 if (index >= 0) {
488 record = mToastQueue.get(index);
489 record.update(duration);
490 } else {
491 record = new ToastRecord(callingPid, pkg, callback, duration);
492 mToastQueue.add(record);
493 index = mToastQueue.size() - 1;
494 keepProcessAliveLocked(callingPid);
495 }
496 // If it's at index 0, it's the current toast. It doesn't matter if it's
497 // new or just been updated. Call back and tell it to show itself.
498 // If the callback fails, this will remove it from the list, so don't
499 // assume that it's valid after this.
500 if (index == 0) {
501 showNextToastLocked();
502 }
503 } finally {
504 Binder.restoreCallingIdentity(callingId);
505 }
506 }
507 }
508
509 public void cancelToast(String pkg, ITransientNotification callback) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800510 Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511
512 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800513 Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800514 return ;
515 }
516
517 synchronized (mToastQueue) {
518 long callingId = Binder.clearCallingIdentity();
519 try {
520 int index = indexOfToastLocked(pkg, callback);
521 if (index >= 0) {
522 cancelToastLocked(index);
523 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800524 Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 }
526 } finally {
527 Binder.restoreCallingIdentity(callingId);
528 }
529 }
530 }
531
532 private void showNextToastLocked() {
533 ToastRecord record = mToastQueue.get(0);
534 while (record != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800535 if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 try {
537 record.callback.show();
538 scheduleTimeoutLocked(record, false);
539 return;
540 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800541 Slog.w(TAG, "Object died trying to show notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800542 + " in package " + record.pkg);
543 // remove it from the list and let the process die
544 int index = mToastQueue.indexOf(record);
545 if (index >= 0) {
546 mToastQueue.remove(index);
547 }
548 keepProcessAliveLocked(record.pid);
549 if (mToastQueue.size() > 0) {
550 record = mToastQueue.get(0);
551 } else {
552 record = null;
553 }
554 }
555 }
556 }
557
558 private void cancelToastLocked(int index) {
559 ToastRecord record = mToastQueue.get(index);
560 try {
561 record.callback.hide();
562 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800563 Slog.w(TAG, "Object died trying to hide notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 + " in package " + record.pkg);
565 // don't worry about this, we're about to remove it from
566 // the list anyway
567 }
568 mToastQueue.remove(index);
569 keepProcessAliveLocked(record.pid);
570 if (mToastQueue.size() > 0) {
571 // Show the next one. If the callback fails, this will remove
572 // it from the list, so don't assume that the list hasn't changed
573 // after this point.
574 showNextToastLocked();
575 }
576 }
577
578 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
579 {
580 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
581 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
582 mHandler.removeCallbacksAndMessages(r);
583 mHandler.sendMessageDelayed(m, delay);
584 }
585
586 private void handleTimeout(ToastRecord record)
587 {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800588 if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800589 synchronized (mToastQueue) {
590 int index = indexOfToastLocked(record.pkg, record.callback);
591 if (index >= 0) {
592 cancelToastLocked(index);
593 }
594 }
595 }
596
597 // lock on mToastQueue
598 private int indexOfToastLocked(String pkg, ITransientNotification callback)
599 {
600 IBinder cbak = callback.asBinder();
601 ArrayList<ToastRecord> list = mToastQueue;
602 int len = list.size();
603 for (int i=0; i<len; i++) {
604 ToastRecord r = list.get(i);
605 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
606 return i;
607 }
608 }
609 return -1;
610 }
611
612 // lock on mToastQueue
613 private void keepProcessAliveLocked(int pid)
614 {
615 int toastCount = 0; // toasts from this pid
616 ArrayList<ToastRecord> list = mToastQueue;
617 int N = list.size();
618 for (int i=0; i<N; i++) {
619 ToastRecord r = list.get(i);
620 if (r.pid == pid) {
621 toastCount++;
622 }
623 }
624 try {
625 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
626 } catch (RemoteException e) {
627 // Shouldn't happen.
628 }
629 }
630
631 private final class WorkerHandler extends Handler
632 {
633 @Override
634 public void handleMessage(Message msg)
635 {
636 switch (msg.what)
637 {
638 case MESSAGE_TIMEOUT:
639 handleTimeout((ToastRecord)msg.obj);
640 break;
641 }
642 }
643 }
644
645
646 // Notifications
647 // ============================================================================
648 public void enqueueNotification(String pkg, int id, Notification notification, int[] idOut)
649 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700650 enqueueNotificationWithTag(pkg, null /* tag */, id, notification, idOut);
651 }
652
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400653 public void enqueueNotificationWithTag(String pkg, String tag, int id, Notification notification,
654 int[] idOut)
Fred Quintana6ecaff12009-09-25 14:23:13 -0700655 {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400656 enqueueNotificationInternal(pkg, Binder.getCallingUid(), Binder.getCallingPid(),
657 tag, id, notification, idOut);
658 }
659
660 // Not exposed via Binder; for system use only (otherwise malicious apps could spoof the
661 // uid/pid of another application)
662 public void enqueueNotificationInternal(String pkg, int callingUid, int callingPid,
663 String tag, int id, Notification notification, int[] idOut)
664 {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700665 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800666
Joe Onoratobd73d012010-06-04 11:44:54 -0700667 // Limit the number of notifications that any given package except the android
668 // package can enqueue. Prevents DOS attacks and deals with leaks.
669 if (!"android".equals(pkg)) {
670 synchronized (mNotificationList) {
671 int count = 0;
672 final int N = mNotificationList.size();
673 for (int i=0; i<N; i++) {
674 final NotificationRecord r = mNotificationList.get(i);
675 if (r.pkg.equals(pkg)) {
676 count++;
677 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
678 Slog.e(TAG, "Package has already posted " + count
679 + " notifications. Not showing more. package=" + pkg);
680 return;
681 }
682 }
683 }
684 }
685 }
686
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 // This conditional is a dirty hack to limit the logging done on
688 // behalf of the download manager without affecting other apps.
689 if (!pkg.equals("com.android.providers.downloads")
690 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800691 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 }
693
694 if (pkg == null || notification == null) {
695 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
696 + " id=" + id + " notification=" + notification);
697 }
698 if (notification.icon != 0) {
699 if (notification.contentView == null) {
700 throw new IllegalArgumentException("contentView required: pkg=" + pkg
701 + " id=" + id + " notification=" + notification);
702 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800703 }
704
705 synchronized (mNotificationList) {
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700706 NotificationRecord r = new NotificationRecord(pkg, tag, id,
707 callingUid, callingPid, notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 NotificationRecord old = null;
709
Fred Quintana6ecaff12009-09-25 14:23:13 -0700710 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 if (index < 0) {
712 mNotificationList.add(r);
713 } else {
714 old = mNotificationList.remove(index);
715 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700716 // Make sure we don't lose the foreground service state.
717 if (old != null) {
718 notification.flags |=
719 old.notification.flags&Notification.FLAG_FOREGROUND_SERVICE;
720 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800721 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800722
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700723 // Ensure if this is a foreground service that the proper additional
724 // flags are set.
725 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
726 notification.flags |= Notification.FLAG_ONGOING_EVENT
727 | Notification.FLAG_NO_CLEAR;
728 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800729
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800730 if (notification.icon != 0) {
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700731 StatusBarNotification n = new StatusBarNotification(pkg, id, tag,
732 r.uid, r.initialPid, notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 if (old != null && old.statusBarKey != null) {
734 r.statusBarKey = old.statusBarKey;
735 long identity = Binder.clearCallingIdentity();
736 try {
Joe Onorato18e69df2010-05-17 22:26:12 -0700737 mStatusBar.updateNotification(r.statusBarKey, n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800738 }
739 finally {
740 Binder.restoreCallingIdentity(identity);
741 }
742 } else {
743 long identity = Binder.clearCallingIdentity();
744 try {
Joe Onorato18e69df2010-05-17 22:26:12 -0700745 r.statusBarKey = mStatusBar.addNotification(n);
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500746 mAttentionLight.pulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800747 }
748 finally {
749 Binder.restoreCallingIdentity(identity);
750 }
751 }
Joe Onorato30275482009-07-08 17:09:14 -0700752 sendAccessibilityEvent(notification, pkg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800753 } else {
754 if (old != null && old.statusBarKey != null) {
755 long identity = Binder.clearCallingIdentity();
756 try {
Joe Onorato0cbda992010-05-02 16:28:15 -0700757 mStatusBar.removeNotification(old.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 }
759 finally {
760 Binder.restoreCallingIdentity(identity);
761 }
762 }
763 }
764
765 // If we're not supposed to beep, vibrate, etc. then don't.
766 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
767 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -0700768 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
769 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -0800770
771 final AudioManager audioManager = (AudioManager) mContext
772 .getSystemService(Context.AUDIO_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773 // sound
774 final boolean useDefaultSound =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800775 (notification.defaults & Notification.DEFAULT_SOUND) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 if (useDefaultSound || notification.sound != null) {
777 Uri uri;
778 if (useDefaultSound) {
779 uri = Settings.System.DEFAULT_NOTIFICATION_URI;
780 } else {
781 uri = notification.sound;
782 }
783 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
784 int audioStreamType;
785 if (notification.audioStreamType >= 0) {
786 audioStreamType = notification.audioStreamType;
787 } else {
788 audioStreamType = DEFAULT_STREAM_TYPE;
789 }
790 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -0800791 // do not play notifications if stream volume is 0
792 // (typically because ringer mode is silent).
793 if (audioManager.getStreamVolume(audioStreamType) != 0) {
794 long identity = Binder.clearCallingIdentity();
795 try {
796 mSound.play(mContext, uri, looping, audioStreamType);
797 }
798 finally {
799 Binder.restoreCallingIdentity(identity);
800 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 }
802 }
803
804 // vibrate
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800805 final boolean useDefaultVibrate =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800806 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800807 if ((useDefaultVibrate || notification.vibrate != null)
808 && audioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_NOTIFICATION)) {
809 mVibrateNotification = r;
810
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800811 mVibrator.vibrate(useDefaultVibrate ? DEFAULT_VIBRATE_PATTERN
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800812 : notification.vibrate,
813 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
814 }
815 }
816
817 // this option doesn't shut off the lights
818
819 // light
820 // the most recent thing gets the light
821 mLights.remove(old);
822 if (mLedNotification == old) {
823 mLedNotification = null;
824 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800825 //Slog.i(TAG, "notification.lights="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
827 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
828 mLights.add(r);
829 updateLightsLocked();
830 } else {
831 if (old != null
832 && ((old.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0)) {
833 updateLightsLocked();
834 }
835 }
836 }
837
838 idOut[0] = id;
839 }
840
Joe Onorato30275482009-07-08 17:09:14 -0700841 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700842 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
843 if (!manager.isEnabled()) {
844 return;
845 }
846
847 AccessibilityEvent event =
848 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
849 event.setPackageName(packageName);
850 event.setClassName(Notification.class.getName());
851 event.setParcelableData(notification);
852 CharSequence tickerText = notification.tickerText;
853 if (!TextUtils.isEmpty(tickerText)) {
854 event.getText().add(tickerText);
855 }
856
857 manager.sendAccessibilityEvent(event);
858 }
859
Joe Onorato46439ce2010-11-19 13:56:21 -0800860 private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete) {
861 // tell the app
862 if (sendDelete) {
863 if (r.notification.deleteIntent != null) {
864 try {
865 r.notification.deleteIntent.send();
866 } catch (PendingIntent.CanceledException ex) {
867 // do nothing - there's no relevant way to recover, and
868 // no reason to let this propagate
869 Slog.w(TAG, "canceled PendingIntent for " + r.pkg, ex);
870 }
871 }
872 }
873
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 // status bar
875 if (r.notification.icon != 0) {
876 long identity = Binder.clearCallingIdentity();
877 try {
Joe Onorato0cbda992010-05-02 16:28:15 -0700878 mStatusBar.removeNotification(r.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 }
880 finally {
881 Binder.restoreCallingIdentity(identity);
882 }
883 r.statusBarKey = null;
884 }
885
886 // sound
887 if (mSoundNotification == r) {
888 mSoundNotification = null;
889 long identity = Binder.clearCallingIdentity();
890 try {
891 mSound.stop();
892 }
893 finally {
894 Binder.restoreCallingIdentity(identity);
895 }
896 }
897
898 // vibrate
899 if (mVibrateNotification == r) {
900 mVibrateNotification = null;
901 long identity = Binder.clearCallingIdentity();
902 try {
903 mVibrator.cancel();
904 }
905 finally {
906 Binder.restoreCallingIdentity(identity);
907 }
908 }
909
910 // light
911 mLights.remove(r);
912 if (mLedNotification == r) {
913 mLedNotification = null;
914 }
915 }
916
917 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700918 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800919 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 */
Fred Quintana6ecaff12009-09-25 14:23:13 -0700921 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Joe Onorato46439ce2010-11-19 13:56:21 -0800922 int mustNotHaveFlags, boolean sendDelete) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800923 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800924
925 synchronized (mNotificationList) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700926 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700928 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800929
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
931 return;
932 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700933 if ((r.notification.flags & mustNotHaveFlags) != 0) {
934 return;
935 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800936
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800937 mNotificationList.remove(index);
938
Joe Onorato46439ce2010-11-19 13:56:21 -0800939 cancelNotificationLocked(r, sendDelete);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 updateLightsLocked();
941 }
942 }
943 }
944
945 /**
946 * Cancels all notifications from a given package that have all of the
947 * {@code mustHaveFlags}.
948 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800949 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
950 int mustNotHaveFlags, boolean doit) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800951 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800952
953 synchronized (mNotificationList) {
954 final int N = mNotificationList.size();
955 boolean canceledSomething = false;
956 for (int i = N-1; i >= 0; --i) {
957 NotificationRecord r = mNotificationList.get(i);
958 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
959 continue;
960 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700961 if ((r.notification.flags & mustNotHaveFlags) != 0) {
962 continue;
963 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964 if (!r.pkg.equals(pkg)) {
965 continue;
966 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800967 canceledSomething = true;
968 if (!doit) {
969 return true;
970 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800971 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -0800972 cancelNotificationLocked(r, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 }
974 if (canceledSomething) {
975 updateLightsLocked();
976 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800977 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800978 }
979 }
980
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800981
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700982 public void cancelNotification(String pkg, int id) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700983 cancelNotificationWithTag(pkg, null /* tag */, id);
984 }
985
986 public void cancelNotificationWithTag(String pkg, String tag, int id) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700987 checkIncomingCall(pkg);
988 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700989 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700990 Binder.getCallingUid() == Process.SYSTEM_UID
Joe Onorato46439ce2010-11-19 13:56:21 -0800991 ? 0 : Notification.FLAG_FOREGROUND_SERVICE, false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800992 }
993
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700994 public void cancelAllNotifications(String pkg) {
995 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800996
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700997 // Calling from user space, don't allow the canceling of actively
998 // running foreground services.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800999 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 }
1001
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001002 void checkIncomingCall(String pkg) {
1003 int uid = Binder.getCallingUid();
1004 if (uid == Process.SYSTEM_UID || uid == 0) {
1005 return;
1006 }
1007 try {
1008 ApplicationInfo ai = mContext.getPackageManager().getApplicationInfo(
1009 pkg, 0);
1010 if (ai.uid != uid) {
1011 throw new SecurityException("Calling uid " + uid + " gave package"
1012 + pkg + " which is owned by uid " + ai.uid);
1013 }
1014 } catch (PackageManager.NameNotFoundException e) {
1015 throw new SecurityException("Unknown package " + pkg);
1016 }
1017 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001018
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001019 void cancelAll() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001020 synchronized (mNotificationList) {
1021 final int N = mNotificationList.size();
1022 for (int i=N-1; i>=0; i--) {
1023 NotificationRecord r = mNotificationList.get(i);
1024
1025 if ((r.notification.flags & (Notification.FLAG_ONGOING_EVENT
1026 | Notification.FLAG_NO_CLEAR)) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001027 mNotificationList.remove(i);
Joe Onorato46439ce2010-11-19 13:56:21 -08001028 cancelNotificationLocked(r, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001029 }
1030 }
1031
1032 updateLightsLocked();
1033 }
1034 }
1035
1036 private void updateLights() {
1037 synchronized (mNotificationList) {
1038 updateLightsLocked();
1039 }
1040 }
1041
1042 // lock on mNotificationList
1043 private void updateLightsLocked()
1044 {
Mike Lockwood2117f6f2010-09-09 09:48:08 -04001045 // clear pending pulse notification if screen is on
1046 if (mScreenOn || mLedNotification == null) {
1047 mPendingPulseNotification = false;
1048 }
1049
The Android Open Source Project10592532009-03-18 17:39:46 -07001050 // handle notification lights
1051 if (mLedNotification == null) {
1052 // get next notification, if any
1053 int n = mLights.size();
1054 if (n > 0) {
1055 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 }
Mike Lockwood2117f6f2010-09-09 09:48:08 -04001057 if (mLedNotification != null && !mScreenOn) {
1058 mPendingPulseNotification = true;
1059 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001061
1062 // we only flash if screen is off and persistent pulsing is enabled
Daniel Sandlere96ffb12010-03-11 13:38:06 -05001063 // and we are not currently in a call
Mike Lockwood2117f6f2010-09-09 09:48:08 -04001064 if (!mPendingPulseNotification || mScreenOn || mInCall) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001065 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001066 } else {
Mike Lockwood670f9322010-01-20 12:13:36 -05001067 int ledARGB = mLedNotification.notification.ledARGB;
1068 int ledOnMS = mLedNotification.notification.ledOnMS;
1069 int ledOffMS = mLedNotification.notification.ledOffMS;
1070 if ((mLedNotification.notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
1071 ledARGB = mDefaultNotificationColor;
1072 ledOnMS = mDefaultNotificationLedOn;
1073 ledOffMS = mDefaultNotificationLedOff;
1074 }
1075 if (mNotificationPulseEnabled) {
1076 // pulse repeatedly
1077 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
1078 ledOnMS, ledOffMS);
1079 } else {
1080 // pulse only once
1081 mNotificationLight.pulse(ledARGB, ledOnMS);
1082 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001083 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 }
1085
1086 // lock on mNotificationList
Fred Quintana6ecaff12009-09-25 14:23:13 -07001087 private int indexOfNotificationLocked(String pkg, String tag, int id)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001088 {
1089 ArrayList<NotificationRecord> list = mNotificationList;
1090 final int len = list.size();
1091 for (int i=0; i<len; i++) {
1092 NotificationRecord r = list.get(i);
Fred Quintana6ecaff12009-09-25 14:23:13 -07001093 if (tag == null) {
1094 if (r.tag != null) {
1095 continue;
1096 }
1097 } else {
1098 if (!tag.equals(r.tag)) {
1099 continue;
1100 }
1101 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 if (r.id == id && r.pkg.equals(pkg)) {
1103 return i;
1104 }
1105 }
1106 return -1;
1107 }
1108
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001109 // This is here instead of StatusBarPolicy because it is an important
1110 // security feature that we don't want people customizing the platform
1111 // to accidentally lose.
Mike Lockwoodff2544c2010-06-28 09:17:50 -04001112 private void updateAdbNotification(boolean adbEnabled) {
1113 if (adbEnabled) {
Mike Lockwooded760372009-07-09 07:07:27 -04001114 if ("0".equals(SystemProperties.get("persist.adb.notify"))) {
1115 return;
1116 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001117 if (!mAdbNotificationShown) {
1118 NotificationManager notificationManager = (NotificationManager) mContext
1119 .getSystemService(Context.NOTIFICATION_SERVICE);
1120 if (notificationManager != null) {
1121 Resources r = mContext.getResources();
1122 CharSequence title = r.getText(
1123 com.android.internal.R.string.adb_active_notification_title);
1124 CharSequence message = r.getText(
1125 com.android.internal.R.string.adb_active_notification_message);
1126
1127 if (mAdbNotification == null) {
1128 mAdbNotification = new Notification();
Daniel Sandler39576c82010-03-25 16:02:33 -04001129 mAdbNotification.icon = com.android.internal.R.drawable.stat_sys_adb;
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001130 mAdbNotification.when = 0;
1131 mAdbNotification.flags = Notification.FLAG_ONGOING_EVENT;
1132 mAdbNotification.tickerText = title;
Daniel Sandler39576c82010-03-25 16:02:33 -04001133 mAdbNotification.defaults = 0; // please be quiet
1134 mAdbNotification.sound = null;
1135 mAdbNotification.vibrate = null;
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001136 }
1137
1138 Intent intent = new Intent(
1139 Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS);
1140 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1141 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1142 // Note: we are hard-coding the component because this is
1143 // an important security UI that we don't want anyone
1144 // intercepting.
1145 intent.setComponent(new ComponentName("com.android.settings",
1146 "com.android.settings.DevelopmentSettings"));
1147 PendingIntent pi = PendingIntent.getActivity(mContext, 0,
1148 intent, 0);
1149
1150 mAdbNotification.setLatestEventInfo(mContext, title, message, pi);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001151
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001152 mAdbNotificationShown = true;
1153 notificationManager.notify(
1154 com.android.internal.R.string.adb_active_notification_title,
1155 mAdbNotification);
1156 }
1157 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001158
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001159 } else if (mAdbNotificationShown) {
1160 NotificationManager notificationManager = (NotificationManager) mContext
1161 .getSystemService(Context.NOTIFICATION_SERVICE);
1162 if (notificationManager != null) {
1163 mAdbNotificationShown = false;
1164 notificationManager.cancel(
1165 com.android.internal.R.string.adb_active_notification_title);
1166 }
1167 }
1168 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001169
1170 private void updateNotificationPulse() {
1171 synchronized (mNotificationList) {
1172 updateLightsLocked();
1173 }
1174 }
1175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001176 // ======================================================================
1177 @Override
1178 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1179 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1180 != PackageManager.PERMISSION_GRANTED) {
1181 pw.println("Permission Denial: can't dump NotificationManager from from pid="
1182 + Binder.getCallingPid()
1183 + ", uid=" + Binder.getCallingUid());
1184 return;
1185 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001187 pw.println("Current Notification Manager state:");
1188
1189 int N;
1190
1191 synchronized (mToastQueue) {
1192 N = mToastQueue.size();
1193 if (N > 0) {
1194 pw.println(" Toast Queue:");
1195 for (int i=0; i<N; i++) {
1196 mToastQueue.get(i).dump(pw, " ");
1197 }
1198 pw.println(" ");
1199 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001200
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 }
1202
1203 synchronized (mNotificationList) {
1204 N = mNotificationList.size();
1205 if (N > 0) {
1206 pw.println(" Notification List:");
1207 for (int i=0; i<N; i++) {
1208 mNotificationList.get(i).dump(pw, " ", mContext);
1209 }
1210 pw.println(" ");
1211 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001212
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 N = mLights.size();
1214 if (N > 0) {
1215 pw.println(" Lights List:");
1216 for (int i=0; i<N; i++) {
1217 mLights.get(i).dump(pw, " ", mContext);
1218 }
1219 pw.println(" ");
1220 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001221
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 pw.println(" mSoundNotification=" + mSoundNotification);
1223 pw.println(" mSound=" + mSound);
1224 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001225 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
1226 pw.println(" mSystemReady=" + mSystemReady);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 }
1228 }
1229}