blob: 3e2c1229e53e14a3f1bdff967a0823c2d5ca0f37 [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;
svetoslavganov75986cf2009-05-14 22:28:01 -070041import android.media.AudioManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.net.Uri;
43import android.os.BatteryManager;
44import android.os.Binder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.Handler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.os.IBinder;
47import android.os.Message;
48import android.os.Power;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070049import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070050import android.os.RemoteException;
Mike Lockwooded760372009-07-09 07:07:27 -040051import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.os.Vibrator;
53import android.provider.Settings;
Daniel Sandlere96ffb12010-03-11 13:38:06 -050054import android.telephony.TelephonyManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070055import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080057import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.util.Log;
svetoslavganov75986cf2009-05-14 22:28:01 -070059import android.view.accessibility.AccessibilityEvent;
60import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061import android.widget.Toast;
62
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import java.io.FileDescriptor;
64import java.io.PrintWriter;
65import java.util.ArrayList;
66import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067
68class NotificationManagerService extends INotificationManager.Stub
69{
70 private static final String TAG = "NotificationService";
71 private static final boolean DBG = false;
72
Joe Onoratobd73d012010-06-04 11:44:54 -070073 private static final int MAX_PACKAGE_NOTIFICATIONS = 50;
74
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 // message codes
76 private static final int MESSAGE_TIMEOUT = 2;
77
78 private static final int LONG_DELAY = 3500; // 3.5 seconds
79 private static final int SHORT_DELAY = 2000; // 2 seconds
Doug Zongkerab5c49c2009-12-04 10:31:43 -080080
81 private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082
83 private static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION;
84
85 final Context mContext;
86 final IActivityManager mAm;
87 final IBinder mForegroundToken = new Binder();
88
89 private WorkerHandler mHandler;
Joe Onorato089de882010-04-12 08:18:45 -070090 private StatusBarManagerService mStatusBar;
Mike Lockwood3a322132009-11-24 00:30:52 -050091 private LightsService mLightsService;
Mike Lockwood3cb67a32009-11-27 14:25:58 -050092 private LightsService.Light mBatteryLight;
93 private LightsService.Light mNotificationLight;
94 private LightsService.Light mAttentionLight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095
Mike Lockwood670f9322010-01-20 12:13:36 -050096 private int mDefaultNotificationColor;
97 private int mDefaultNotificationLedOn;
98 private int mDefaultNotificationLedOff;
99
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 private NotificationRecord mSoundNotification;
Jean-Michel Trivi211957f2010-03-26 18:19:33 -0700101 private NotificationPlayer mSound;
Joe Onorato30275482009-07-08 17:09:14 -0700102 private boolean mSystemReady;
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400103 private int mDisabledNotifications;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104
105 private NotificationRecord mVibrateNotification;
106 private Vibrator mVibrator = new Vibrator();
107
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500108 // for enabling and disabling notification pulse behavior
109 private boolean mScreenOn = true;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500110 private boolean mInCall = false;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500111 private boolean mNotificationPulseEnabled;
112
113 // for adb connected notifications
Mike Lockwoodea8b7d52009-08-04 17:03:15 -0400114 private boolean mUsbConnected;
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700115 private boolean mAdbEnabled = false;
116 private boolean mAdbNotificationShown = false;
117 private Notification mAdbNotification;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800118
Fred Quintana6ecaff12009-09-25 14:23:13 -0700119 private final ArrayList<NotificationRecord> mNotificationList =
120 new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121
122 private ArrayList<ToastRecord> mToastQueue;
123
124 private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>();
125
126 private boolean mBatteryCharging;
127 private boolean mBatteryLow;
128 private boolean mBatteryFull;
129 private NotificationRecord mLedNotification;
svetoslavganov75986cf2009-05-14 22:28:01 -0700130
The Android Open Source Project10592532009-03-18 17:39:46 -0700131 private static final int BATTERY_LOW_ARGB = 0xFFFF0000; // Charging Low - red solid on
132 private static final int BATTERY_MEDIUM_ARGB = 0xFFFFFF00; // Charging - orange solid on
133 private static final int BATTERY_FULL_ARGB = 0xFF00FF00; // Charging Full - green solid on
134 private static final int BATTERY_BLINK_ON = 125;
135 private static final int BATTERY_BLINK_OFF = 2875;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 private static String idDebugString(Context baseContext, String packageName, int id) {
138 Context c = null;
139
140 if (packageName != null) {
141 try {
142 c = baseContext.createPackageContext(packageName, 0);
143 } catch (NameNotFoundException e) {
144 c = baseContext;
145 }
146 } else {
147 c = baseContext;
148 }
149
150 String pkg;
151 String type;
152 String name;
153
154 Resources r = c.getResources();
155 try {
156 return r.getResourceName(id);
157 } catch (Resources.NotFoundException e) {
158 return "<name unknown>";
159 }
160 }
161
162 private static final class NotificationRecord
163 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700164 final String pkg;
165 final String tag;
166 final int id;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700167 final int uid;
168 final int initialPid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 ITransientNotification callback;
170 int duration;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700171 final Notification notification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 IBinder statusBarKey;
173
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700174 NotificationRecord(String pkg, String tag, int id, int uid, int initialPid,
175 Notification notification)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 {
177 this.pkg = pkg;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700178 this.tag = tag;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 this.id = id;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700180 this.uid = uid;
181 this.initialPid = initialPid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800182 this.notification = notification;
183 }
Fred Quintana6ecaff12009-09-25 14:23:13 -0700184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 void dump(PrintWriter pw, String prefix, Context baseContext) {
186 pw.println(prefix + this);
187 pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
188 + " / " + idDebugString(baseContext, this.pkg, notification.icon));
189 pw.println(prefix + " contentIntent=" + notification.contentIntent);
190 pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
191 pw.println(prefix + " tickerText=" + notification.tickerText);
192 pw.println(prefix + " contentView=" + notification.contentView);
193 pw.println(prefix + " defaults=0x" + Integer.toHexString(notification.defaults));
194 pw.println(prefix + " flags=0x" + Integer.toHexString(notification.flags));
195 pw.println(prefix + " sound=" + notification.sound);
196 pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate));
197 pw.println(prefix + " ledARGB=0x" + Integer.toHexString(notification.ledARGB)
198 + " ledOnMS=" + notification.ledOnMS
199 + " ledOffMS=" + notification.ledOffMS);
200 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800201
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 @Override
203 public final String toString()
204 {
205 return "NotificationRecord{"
206 + Integer.toHexString(System.identityHashCode(this))
207 + " pkg=" + pkg
Fred Quintana6ecaff12009-09-25 14:23:13 -0700208 + " id=" + Integer.toHexString(id)
209 + " tag=" + tag + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 }
211 }
212
213 private static final class ToastRecord
214 {
215 final int pid;
216 final String pkg;
217 final ITransientNotification callback;
218 int duration;
219
220 ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
221 {
222 this.pid = pid;
223 this.pkg = pkg;
224 this.callback = callback;
225 this.duration = duration;
226 }
227
228 void update(int duration) {
229 this.duration = duration;
230 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 void dump(PrintWriter pw, String prefix) {
233 pw.println(prefix + this);
234 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800235
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 @Override
237 public final String toString()
238 {
239 return "ToastRecord{"
240 + Integer.toHexString(System.identityHashCode(this))
241 + " pkg=" + pkg
242 + " callback=" + callback
243 + " duration=" + duration;
244 }
245 }
246
Joe Onorato089de882010-04-12 08:18:45 -0700247 private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks
248 = new StatusBarManagerService.NotificationCallbacks() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249
250 public void onSetDisabled(int status) {
251 synchronized (mNotificationList) {
252 mDisabledNotifications = status;
253 if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) {
254 // cancel whatever's going on
255 long identity = Binder.clearCallingIdentity();
256 try {
257 mSound.stop();
258 }
259 finally {
260 Binder.restoreCallingIdentity(identity);
261 }
262
263 identity = Binder.clearCallingIdentity();
264 try {
265 mVibrator.cancel();
266 }
267 finally {
268 Binder.restoreCallingIdentity(identity);
269 }
270 }
271 }
272 }
273
274 public void onClearAll() {
275 cancelAll();
276 }
277
Fred Quintana6ecaff12009-09-25 14:23:13 -0700278 public void onNotificationClick(String pkg, String tag, int id) {
279 cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700280 Notification.FLAG_FOREGROUND_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 }
282
283 public void onPanelRevealed() {
284 synchronized (mNotificationList) {
285 // sound
286 mSoundNotification = null;
287 long identity = Binder.clearCallingIdentity();
288 try {
289 mSound.stop();
290 }
291 finally {
292 Binder.restoreCallingIdentity(identity);
293 }
294
295 // vibrate
296 mVibrateNotification = null;
297 identity = Binder.clearCallingIdentity();
298 try {
299 mVibrator.cancel();
300 }
301 finally {
302 Binder.restoreCallingIdentity(identity);
303 }
304
305 // light
306 mLights.clear();
307 mLedNotification = null;
308 updateLightsLocked();
309 }
310 }
Joe Onorato005847b2010-06-04 16:08:02 -0400311
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700312 public void onNotificationError(String pkg, String tag, int id,
313 int uid, int initialPid, String message) {
Joe Onorato005847b2010-06-04 16:08:02 -0400314 Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id);
315 cancelNotification(pkg, tag, id, 0, 0);
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700316 long ident = Binder.clearCallingIdentity();
317 try {
318 ActivityManagerNative.getDefault().crashApplication(uid, initialPid, pkg,
319 "Bad notification posted from package " + pkg
320 + ": " + message);
321 } catch (RemoteException e) {
322 }
323 Binder.restoreCallingIdentity(ident);
Joe Onorato005847b2010-06-04 16:08:02 -0400324 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 };
326
327 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
328 @Override
329 public void onReceive(Context context, Intent intent) {
330 String action = intent.getAction();
331
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800332 boolean queryRestart = false;
333
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
335 boolean batteryCharging = (intent.getIntExtra("plugged", 0) != 0);
336 int level = intent.getIntExtra("level", -1);
337 boolean batteryLow = (level >= 0 && level <= Power.LOW_BATTERY_THRESHOLD);
338 int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
339 boolean batteryFull = (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90);
340
341 if (batteryCharging != mBatteryCharging ||
342 batteryLow != mBatteryLow ||
343 batteryFull != mBatteryFull) {
344 mBatteryCharging = batteryCharging;
345 mBatteryLow = batteryLow;
346 mBatteryFull = batteryFull;
347 updateLights();
348 }
Mike Lockwoodea8b7d52009-08-04 17:03:15 -0400349 } else if (action.equals(Intent.ACTION_UMS_CONNECTED)) {
350 mUsbConnected = true;
351 updateAdbNotification();
352 } else if (action.equals(Intent.ACTION_UMS_DISCONNECTED)) {
353 mUsbConnected = false;
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700354 updateAdbNotification();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800356 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800357 || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800358 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800359 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800360 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800361 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800362 } else if (queryRestart) {
363 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800364 } else {
365 Uri uri = intent.getData();
366 if (uri == null) {
367 return;
368 }
369 String pkgName = uri.getSchemeSpecificPart();
370 if (pkgName == null) {
371 return;
372 }
373 pkgList = new String[]{pkgName};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800375 if (pkgList != null && (pkgList.length > 0)) {
376 for (String pkgName : pkgList) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800377 cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800378 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500380 } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
381 mScreenOn = true;
382 updateNotificationPulse();
383 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
384 mScreenOn = false;
385 updateNotificationPulse();
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500386 } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
387 mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK));
388 updateNotificationPulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800389 }
390 }
391 };
392
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700393 class SettingsObserver extends ContentObserver {
394 SettingsObserver(Handler handler) {
395 super(handler);
396 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800397
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700398 void observe() {
399 ContentResolver resolver = mContext.getContentResolver();
400 resolver.registerContentObserver(Settings.Secure.getUriFor(
401 Settings.Secure.ADB_ENABLED), false, this);
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500402 resolver.registerContentObserver(Settings.System.getUriFor(
403 Settings.System.NOTIFICATION_LIGHT_PULSE), false, this);
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700404 update();
405 }
406
407 @Override public void onChange(boolean selfChange) {
408 update();
409 }
410
411 public void update() {
412 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500413 boolean adbEnabled = Settings.Secure.getInt(resolver,
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700414 Settings.Secure.ADB_ENABLED, 0) != 0;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500415 if (mAdbEnabled != adbEnabled) {
416 mAdbEnabled = adbEnabled;
417 updateAdbNotification();
418 }
419 boolean pulseEnabled = Settings.System.getInt(resolver,
420 Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0;
421 if (mNotificationPulseEnabled != pulseEnabled) {
422 mNotificationPulseEnabled = pulseEnabled;
423 updateNotificationPulse();
424 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700425 }
426 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500427
Joe Onorato089de882010-04-12 08:18:45 -0700428 NotificationManagerService(Context context, StatusBarManagerService statusBar,
Mike Lockwood3a322132009-11-24 00:30:52 -0500429 LightsService lights)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 {
431 super();
432 mContext = context;
Mike Lockwood3a322132009-11-24 00:30:52 -0500433 mLightsService = lights;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 mAm = ActivityManagerNative.getDefault();
Jean-Michel Trivi211957f2010-03-26 18:19:33 -0700435 mSound = new NotificationPlayer(TAG);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 mSound.setUsesWakeLock(context);
437 mToastQueue = new ArrayList<ToastRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 mHandler = new WorkerHandler();
San Mehat3ee13172010-02-04 20:54:43 -0800439
Joe Onorato089de882010-04-12 08:18:45 -0700440 mStatusBar = statusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 statusBar.setNotificationCallbacks(mNotificationCallbacks);
442
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500443 mBatteryLight = lights.getLight(LightsService.LIGHT_ID_BATTERY);
444 mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS);
445 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
446
Mike Lockwood670f9322010-01-20 12:13:36 -0500447 Resources resources = mContext.getResources();
448 mDefaultNotificationColor = resources.getColor(
449 com.android.internal.R.color.config_defaultNotificationColor);
450 mDefaultNotificationLedOn = resources.getInteger(
451 com.android.internal.R.integer.config_defaultNotificationLedOn);
452 mDefaultNotificationLedOff = resources.getInteger(
453 com.android.internal.R.integer.config_defaultNotificationLedOff);
454
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400455 // Don't start allowing notifications until the setup wizard has run once.
456 // After that, including subsequent boots, init with notifications turned on.
457 // This works on the first boot because the setup wizard will toggle this
458 // flag at least once and we'll go back to 0 after that.
459 if (0 == Settings.Secure.getInt(mContext.getContentResolver(),
460 Settings.Secure.DEVICE_PROVISIONED, 0)) {
461 mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
462 }
463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 // register for battery changed notifications
465 IntentFilter filter = new IntentFilter();
466 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
Mike Lockwoodea8b7d52009-08-04 17:03:15 -0400467 filter.addAction(Intent.ACTION_UMS_CONNECTED);
468 filter.addAction(Intent.ACTION_UMS_DISCONNECTED);
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500469 filter.addAction(Intent.ACTION_SCREEN_ON);
470 filter.addAction(Intent.ACTION_SCREEN_OFF);
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500471 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800472 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800473 IntentFilter pkgFilter = new IntentFilter();
474 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
475 pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
476 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
477 pkgFilter.addDataScheme("package");
478 mContext.registerReceiver(mIntentReceiver, pkgFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800479 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800480 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800481
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500482 SettingsObserver observer = new SettingsObserver(mHandler);
483 observer.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 }
485
Joe Onorato30275482009-07-08 17:09:14 -0700486 void systemReady() {
487 // no beeping until we're basically done booting
488 mSystemReady = true;
489 }
490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 // Toasts
492 // ============================================================================
493 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
494 {
Daniel Sandlera7035902010-03-30 15:45:31 -0400495 if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496
497 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800498 Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 return ;
500 }
501
502 synchronized (mToastQueue) {
503 int callingPid = Binder.getCallingPid();
504 long callingId = Binder.clearCallingIdentity();
505 try {
506 ToastRecord record;
507 int index = indexOfToastLocked(pkg, callback);
508 // If it's already in the queue, we update it in place, we don't
509 // move it to the end of the queue.
510 if (index >= 0) {
511 record = mToastQueue.get(index);
512 record.update(duration);
513 } else {
514 record = new ToastRecord(callingPid, pkg, callback, duration);
515 mToastQueue.add(record);
516 index = mToastQueue.size() - 1;
517 keepProcessAliveLocked(callingPid);
518 }
519 // If it's at index 0, it's the current toast. It doesn't matter if it's
520 // new or just been updated. Call back and tell it to show itself.
521 // If the callback fails, this will remove it from the list, so don't
522 // assume that it's valid after this.
523 if (index == 0) {
524 showNextToastLocked();
525 }
526 } finally {
527 Binder.restoreCallingIdentity(callingId);
528 }
529 }
530 }
531
532 public void cancelToast(String pkg, ITransientNotification callback) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800533 Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534
535 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800536 Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800537 return ;
538 }
539
540 synchronized (mToastQueue) {
541 long callingId = Binder.clearCallingIdentity();
542 try {
543 int index = indexOfToastLocked(pkg, callback);
544 if (index >= 0) {
545 cancelToastLocked(index);
546 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800547 Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 }
549 } finally {
550 Binder.restoreCallingIdentity(callingId);
551 }
552 }
553 }
554
555 private void showNextToastLocked() {
556 ToastRecord record = mToastQueue.get(0);
557 while (record != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800558 if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 try {
560 record.callback.show();
561 scheduleTimeoutLocked(record, false);
562 return;
563 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800564 Slog.w(TAG, "Object died trying to show notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 + " in package " + record.pkg);
566 // remove it from the list and let the process die
567 int index = mToastQueue.indexOf(record);
568 if (index >= 0) {
569 mToastQueue.remove(index);
570 }
571 keepProcessAliveLocked(record.pid);
572 if (mToastQueue.size() > 0) {
573 record = mToastQueue.get(0);
574 } else {
575 record = null;
576 }
577 }
578 }
579 }
580
581 private void cancelToastLocked(int index) {
582 ToastRecord record = mToastQueue.get(index);
583 try {
584 record.callback.hide();
585 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800586 Slog.w(TAG, "Object died trying to hide notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 + " in package " + record.pkg);
588 // don't worry about this, we're about to remove it from
589 // the list anyway
590 }
591 mToastQueue.remove(index);
592 keepProcessAliveLocked(record.pid);
593 if (mToastQueue.size() > 0) {
594 // Show the next one. If the callback fails, this will remove
595 // it from the list, so don't assume that the list hasn't changed
596 // after this point.
597 showNextToastLocked();
598 }
599 }
600
601 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
602 {
603 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
604 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
605 mHandler.removeCallbacksAndMessages(r);
606 mHandler.sendMessageDelayed(m, delay);
607 }
608
609 private void handleTimeout(ToastRecord record)
610 {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800611 if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800612 synchronized (mToastQueue) {
613 int index = indexOfToastLocked(record.pkg, record.callback);
614 if (index >= 0) {
615 cancelToastLocked(index);
616 }
617 }
618 }
619
620 // lock on mToastQueue
621 private int indexOfToastLocked(String pkg, ITransientNotification callback)
622 {
623 IBinder cbak = callback.asBinder();
624 ArrayList<ToastRecord> list = mToastQueue;
625 int len = list.size();
626 for (int i=0; i<len; i++) {
627 ToastRecord r = list.get(i);
628 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
629 return i;
630 }
631 }
632 return -1;
633 }
634
635 // lock on mToastQueue
636 private void keepProcessAliveLocked(int pid)
637 {
638 int toastCount = 0; // toasts from this pid
639 ArrayList<ToastRecord> list = mToastQueue;
640 int N = list.size();
641 for (int i=0; i<N; i++) {
642 ToastRecord r = list.get(i);
643 if (r.pid == pid) {
644 toastCount++;
645 }
646 }
647 try {
648 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
649 } catch (RemoteException e) {
650 // Shouldn't happen.
651 }
652 }
653
654 private final class WorkerHandler extends Handler
655 {
656 @Override
657 public void handleMessage(Message msg)
658 {
659 switch (msg.what)
660 {
661 case MESSAGE_TIMEOUT:
662 handleTimeout((ToastRecord)msg.obj);
663 break;
664 }
665 }
666 }
667
668
669 // Notifications
670 // ============================================================================
671 public void enqueueNotification(String pkg, int id, Notification notification, int[] idOut)
672 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700673 enqueueNotificationWithTag(pkg, null /* tag */, id, notification, idOut);
674 }
675
676 public void enqueueNotificationWithTag(String pkg, String tag, int id,
677 Notification notification, int[] idOut)
678 {
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700679 final int callingUid = Binder.getCallingUid();
680 final int callingPid = Binder.getCallingPid();
681
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700682 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800683
Joe Onoratobd73d012010-06-04 11:44:54 -0700684 // Limit the number of notifications that any given package except the android
685 // package can enqueue. Prevents DOS attacks and deals with leaks.
686 if (!"android".equals(pkg)) {
687 synchronized (mNotificationList) {
688 int count = 0;
689 final int N = mNotificationList.size();
690 for (int i=0; i<N; i++) {
691 final NotificationRecord r = mNotificationList.get(i);
692 if (r.pkg.equals(pkg)) {
693 count++;
694 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
695 Slog.e(TAG, "Package has already posted " + count
696 + " notifications. Not showing more. package=" + pkg);
697 return;
698 }
699 }
700 }
701 }
702 }
703
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800704 // This conditional is a dirty hack to limit the logging done on
705 // behalf of the download manager without affecting other apps.
706 if (!pkg.equals("com.android.providers.downloads")
707 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800708 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 }
710
711 if (pkg == null || notification == null) {
712 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
713 + " id=" + id + " notification=" + notification);
714 }
715 if (notification.icon != 0) {
716 if (notification.contentView == null) {
717 throw new IllegalArgumentException("contentView required: pkg=" + pkg
718 + " id=" + id + " notification=" + notification);
719 }
720 if (notification.contentIntent == null) {
721 throw new IllegalArgumentException("contentIntent required: pkg=" + pkg
722 + " id=" + id + " notification=" + notification);
723 }
724 }
725
726 synchronized (mNotificationList) {
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700727 NotificationRecord r = new NotificationRecord(pkg, tag, id,
728 callingUid, callingPid, notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 NotificationRecord old = null;
730
Fred Quintana6ecaff12009-09-25 14:23:13 -0700731 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800732 if (index < 0) {
733 mNotificationList.add(r);
734 } else {
735 old = mNotificationList.remove(index);
736 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700737 // Make sure we don't lose the foreground service state.
738 if (old != null) {
739 notification.flags |=
740 old.notification.flags&Notification.FLAG_FOREGROUND_SERVICE;
741 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800742 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800743
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700744 // Ensure if this is a foreground service that the proper additional
745 // flags are set.
746 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
747 notification.flags |= Notification.FLAG_ONGOING_EVENT
748 | Notification.FLAG_NO_CLEAR;
749 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800750
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 if (notification.icon != 0) {
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700752 StatusBarNotification n = new StatusBarNotification(pkg, id, tag,
753 r.uid, r.initialPid, notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754 if (old != null && old.statusBarKey != null) {
755 r.statusBarKey = old.statusBarKey;
756 long identity = Binder.clearCallingIdentity();
757 try {
Joe Onorato18e69df2010-05-17 22:26:12 -0700758 mStatusBar.updateNotification(r.statusBarKey, n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 }
760 finally {
761 Binder.restoreCallingIdentity(identity);
762 }
763 } else {
764 long identity = Binder.clearCallingIdentity();
765 try {
Joe Onorato18e69df2010-05-17 22:26:12 -0700766 r.statusBarKey = mStatusBar.addNotification(n);
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500767 mAttentionLight.pulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800768 }
769 finally {
770 Binder.restoreCallingIdentity(identity);
771 }
772 }
Joe Onorato30275482009-07-08 17:09:14 -0700773 sendAccessibilityEvent(notification, pkg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800774 } else {
775 if (old != null && old.statusBarKey != null) {
776 long identity = Binder.clearCallingIdentity();
777 try {
Joe Onorato0cbda992010-05-02 16:28:15 -0700778 mStatusBar.removeNotification(old.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 }
780 finally {
781 Binder.restoreCallingIdentity(identity);
782 }
783 }
784 }
785
786 // If we're not supposed to beep, vibrate, etc. then don't.
787 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
788 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -0700789 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
790 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -0800791
792 final AudioManager audioManager = (AudioManager) mContext
793 .getSystemService(Context.AUDIO_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794 // sound
795 final boolean useDefaultSound =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800796 (notification.defaults & Notification.DEFAULT_SOUND) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800797 if (useDefaultSound || notification.sound != null) {
798 Uri uri;
799 if (useDefaultSound) {
800 uri = Settings.System.DEFAULT_NOTIFICATION_URI;
801 } else {
802 uri = notification.sound;
803 }
804 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
805 int audioStreamType;
806 if (notification.audioStreamType >= 0) {
807 audioStreamType = notification.audioStreamType;
808 } else {
809 audioStreamType = DEFAULT_STREAM_TYPE;
810 }
811 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -0800812 // do not play notifications if stream volume is 0
813 // (typically because ringer mode is silent).
814 if (audioManager.getStreamVolume(audioStreamType) != 0) {
815 long identity = Binder.clearCallingIdentity();
816 try {
817 mSound.play(mContext, uri, looping, audioStreamType);
818 }
819 finally {
820 Binder.restoreCallingIdentity(identity);
821 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 }
823 }
824
825 // vibrate
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 final boolean useDefaultVibrate =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800827 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800828 if ((useDefaultVibrate || notification.vibrate != null)
829 && audioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_NOTIFICATION)) {
830 mVibrateNotification = r;
831
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800832 mVibrator.vibrate(useDefaultVibrate ? DEFAULT_VIBRATE_PATTERN
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800833 : notification.vibrate,
834 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
835 }
836 }
837
838 // this option doesn't shut off the lights
839
840 // light
841 // the most recent thing gets the light
842 mLights.remove(old);
843 if (mLedNotification == old) {
844 mLedNotification = null;
845 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800846 //Slog.i(TAG, "notification.lights="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
848 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
849 mLights.add(r);
850 updateLightsLocked();
851 } else {
852 if (old != null
853 && ((old.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0)) {
854 updateLightsLocked();
855 }
856 }
857 }
858
859 idOut[0] = id;
860 }
861
Joe Onorato30275482009-07-08 17:09:14 -0700862 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700863 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
864 if (!manager.isEnabled()) {
865 return;
866 }
867
868 AccessibilityEvent event =
869 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
870 event.setPackageName(packageName);
871 event.setClassName(Notification.class.getName());
872 event.setParcelableData(notification);
873 CharSequence tickerText = notification.tickerText;
874 if (!TextUtils.isEmpty(tickerText)) {
875 event.getText().add(tickerText);
876 }
877
878 manager.sendAccessibilityEvent(event);
879 }
880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881 private void cancelNotificationLocked(NotificationRecord r) {
882 // status bar
883 if (r.notification.icon != 0) {
884 long identity = Binder.clearCallingIdentity();
885 try {
Joe Onorato0cbda992010-05-02 16:28:15 -0700886 mStatusBar.removeNotification(r.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 }
888 finally {
889 Binder.restoreCallingIdentity(identity);
890 }
891 r.statusBarKey = null;
892 }
893
894 // sound
895 if (mSoundNotification == r) {
896 mSoundNotification = null;
897 long identity = Binder.clearCallingIdentity();
898 try {
899 mSound.stop();
900 }
901 finally {
902 Binder.restoreCallingIdentity(identity);
903 }
904 }
905
906 // vibrate
907 if (mVibrateNotification == r) {
908 mVibrateNotification = null;
909 long identity = Binder.clearCallingIdentity();
910 try {
911 mVibrator.cancel();
912 }
913 finally {
914 Binder.restoreCallingIdentity(identity);
915 }
916 }
917
918 // light
919 mLights.remove(r);
920 if (mLedNotification == r) {
921 mLedNotification = null;
922 }
923 }
924
925 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700926 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800927 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 */
Fred Quintana6ecaff12009-09-25 14:23:13 -0700929 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700930 int mustNotHaveFlags) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800931 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932
933 synchronized (mNotificationList) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700934 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700936 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800938 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
939 return;
940 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700941 if ((r.notification.flags & mustNotHaveFlags) != 0) {
942 return;
943 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800944
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800945 mNotificationList.remove(index);
946
947 cancelNotificationLocked(r);
948 updateLightsLocked();
949 }
950 }
951 }
952
953 /**
954 * Cancels all notifications from a given package that have all of the
955 * {@code mustHaveFlags}.
956 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800957 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
958 int mustNotHaveFlags, boolean doit) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800959 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800960
961 synchronized (mNotificationList) {
962 final int N = mNotificationList.size();
963 boolean canceledSomething = false;
964 for (int i = N-1; i >= 0; --i) {
965 NotificationRecord r = mNotificationList.get(i);
966 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
967 continue;
968 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700969 if ((r.notification.flags & mustNotHaveFlags) != 0) {
970 continue;
971 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800972 if (!r.pkg.equals(pkg)) {
973 continue;
974 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800975 canceledSomething = true;
976 if (!doit) {
977 return true;
978 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800979 mNotificationList.remove(i);
980 cancelNotificationLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800981 }
982 if (canceledSomething) {
983 updateLightsLocked();
984 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800985 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800986 }
987 }
988
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800989
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700990 public void cancelNotification(String pkg, int id) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700991 cancelNotificationWithTag(pkg, null /* tag */, id);
992 }
993
994 public void cancelNotificationWithTag(String pkg, String tag, int id) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700995 checkIncomingCall(pkg);
996 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700997 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700998 Binder.getCallingUid() == Process.SYSTEM_UID
999 ? 0 : Notification.FLAG_FOREGROUND_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001000 }
1001
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001002 public void cancelAllNotifications(String pkg) {
1003 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001004
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001005 // Calling from user space, don't allow the canceling of actively
1006 // running foreground services.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001007 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001008 }
1009
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001010 void checkIncomingCall(String pkg) {
1011 int uid = Binder.getCallingUid();
1012 if (uid == Process.SYSTEM_UID || uid == 0) {
1013 return;
1014 }
1015 try {
1016 ApplicationInfo ai = mContext.getPackageManager().getApplicationInfo(
1017 pkg, 0);
1018 if (ai.uid != uid) {
1019 throw new SecurityException("Calling uid " + uid + " gave package"
1020 + pkg + " which is owned by uid " + ai.uid);
1021 }
1022 } catch (PackageManager.NameNotFoundException e) {
1023 throw new SecurityException("Unknown package " + pkg);
1024 }
1025 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001026
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001027 void cancelAll() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 synchronized (mNotificationList) {
1029 final int N = mNotificationList.size();
1030 for (int i=N-1; i>=0; i--) {
1031 NotificationRecord r = mNotificationList.get(i);
1032
1033 if ((r.notification.flags & (Notification.FLAG_ONGOING_EVENT
1034 | Notification.FLAG_NO_CLEAR)) == 0) {
1035 if (r.notification.deleteIntent != null) {
1036 try {
1037 r.notification.deleteIntent.send();
1038 } catch (PendingIntent.CanceledException ex) {
1039 // do nothing - there's no relevant way to recover, and
1040 // no reason to let this propagate
Joe Onorato8a9b2202010-02-26 18:56:32 -08001041 Slog.w(TAG, "canceled PendingIntent for " + r.pkg, ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 }
1043 }
1044 mNotificationList.remove(i);
1045 cancelNotificationLocked(r);
1046 }
1047 }
1048
1049 updateLightsLocked();
1050 }
1051 }
1052
1053 private void updateLights() {
1054 synchronized (mNotificationList) {
1055 updateLightsLocked();
1056 }
1057 }
1058
1059 // lock on mNotificationList
1060 private void updateLightsLocked()
1061 {
The Android Open Source Project10592532009-03-18 17:39:46 -07001062 // Battery low always shows, other states only show if charging.
1063 if (mBatteryLow) {
Mike Lockwood445f4302009-09-04 11:06:46 -04001064 if (mBatteryCharging) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001065 mBatteryLight.setColor(BATTERY_LOW_ARGB);
Mike Lockwood445f4302009-09-04 11:06:46 -04001066 } else {
1067 // Flash when battery is low and not charging
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001068 mBatteryLight.setFlashing(BATTERY_LOW_ARGB, LightsService.LIGHT_FLASH_TIMED,
1069 BATTERY_BLINK_ON, BATTERY_BLINK_OFF);
Mike Lockwood445f4302009-09-04 11:06:46 -04001070 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 } else if (mBatteryCharging) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001072 if (mBatteryFull) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001073 mBatteryLight.setColor(BATTERY_FULL_ARGB);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001074 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001075 mBatteryLight.setColor(BATTERY_MEDIUM_ARGB);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 }
1077 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001078 mBatteryLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001079 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080
The Android Open Source Project10592532009-03-18 17:39:46 -07001081 // handle notification lights
1082 if (mLedNotification == null) {
1083 // get next notification, if any
1084 int n = mLights.size();
1085 if (n > 0) {
1086 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 }
1088 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001089
1090 // we only flash if screen is off and persistent pulsing is enabled
Daniel Sandlere96ffb12010-03-11 13:38:06 -05001091 // and we are not currently in a call
1092 if (mLedNotification == null || mScreenOn || mInCall) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001093 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001094 } else {
Mike Lockwood670f9322010-01-20 12:13:36 -05001095 int ledARGB = mLedNotification.notification.ledARGB;
1096 int ledOnMS = mLedNotification.notification.ledOnMS;
1097 int ledOffMS = mLedNotification.notification.ledOffMS;
1098 if ((mLedNotification.notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
1099 ledARGB = mDefaultNotificationColor;
1100 ledOnMS = mDefaultNotificationLedOn;
1101 ledOffMS = mDefaultNotificationLedOff;
1102 }
1103 if (mNotificationPulseEnabled) {
1104 // pulse repeatedly
1105 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
1106 ledOnMS, ledOffMS);
1107 } else {
1108 // pulse only once
1109 mNotificationLight.pulse(ledARGB, ledOnMS);
1110 }
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
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001137 // This is here instead of StatusBarPolicy because it is an important
1138 // security feature that we don't want people customizing the platform
1139 // to accidentally lose.
1140 private void updateAdbNotification() {
Mike Lockwoodea8b7d52009-08-04 17:03:15 -04001141 if (mAdbEnabled && mUsbConnected) {
Mike Lockwooded760372009-07-09 07:07:27 -04001142 if ("0".equals(SystemProperties.get("persist.adb.notify"))) {
1143 return;
1144 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001145 if (!mAdbNotificationShown) {
1146 NotificationManager notificationManager = (NotificationManager) mContext
1147 .getSystemService(Context.NOTIFICATION_SERVICE);
1148 if (notificationManager != null) {
1149 Resources r = mContext.getResources();
1150 CharSequence title = r.getText(
1151 com.android.internal.R.string.adb_active_notification_title);
1152 CharSequence message = r.getText(
1153 com.android.internal.R.string.adb_active_notification_message);
1154
1155 if (mAdbNotification == null) {
1156 mAdbNotification = new Notification();
Daniel Sandler39576c82010-03-25 16:02:33 -04001157 mAdbNotification.icon = com.android.internal.R.drawable.stat_sys_adb;
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001158 mAdbNotification.when = 0;
1159 mAdbNotification.flags = Notification.FLAG_ONGOING_EVENT;
1160 mAdbNotification.tickerText = title;
Daniel Sandler39576c82010-03-25 16:02:33 -04001161 mAdbNotification.defaults = 0; // please be quiet
1162 mAdbNotification.sound = null;
1163 mAdbNotification.vibrate = null;
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001164 }
1165
1166 Intent intent = new Intent(
1167 Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS);
1168 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1169 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1170 // Note: we are hard-coding the component because this is
1171 // an important security UI that we don't want anyone
1172 // intercepting.
1173 intent.setComponent(new ComponentName("com.android.settings",
1174 "com.android.settings.DevelopmentSettings"));
1175 PendingIntent pi = PendingIntent.getActivity(mContext, 0,
1176 intent, 0);
1177
1178 mAdbNotification.setLatestEventInfo(mContext, title, message, pi);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001179
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001180 mAdbNotificationShown = true;
1181 notificationManager.notify(
1182 com.android.internal.R.string.adb_active_notification_title,
1183 mAdbNotification);
1184 }
1185 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001186
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001187 } else if (mAdbNotificationShown) {
1188 NotificationManager notificationManager = (NotificationManager) mContext
1189 .getSystemService(Context.NOTIFICATION_SERVICE);
1190 if (notificationManager != null) {
1191 mAdbNotificationShown = false;
1192 notificationManager.cancel(
1193 com.android.internal.R.string.adb_active_notification_title);
1194 }
1195 }
1196 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001197
1198 private void updateNotificationPulse() {
1199 synchronized (mNotificationList) {
1200 updateLightsLocked();
1201 }
1202 }
1203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 // ======================================================================
1205 @Override
1206 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1207 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1208 != PackageManager.PERMISSION_GRANTED) {
1209 pw.println("Permission Denial: can't dump NotificationManager from from pid="
1210 + Binder.getCallingPid()
1211 + ", uid=" + Binder.getCallingUid());
1212 return;
1213 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001214
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001215 pw.println("Current Notification Manager state:");
1216
1217 int N;
1218
1219 synchronized (mToastQueue) {
1220 N = mToastQueue.size();
1221 if (N > 0) {
1222 pw.println(" Toast Queue:");
1223 for (int i=0; i<N; i++) {
1224 mToastQueue.get(i).dump(pw, " ");
1225 }
1226 pw.println(" ");
1227 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 }
1230
1231 synchronized (mNotificationList) {
1232 N = mNotificationList.size();
1233 if (N > 0) {
1234 pw.println(" Notification List:");
1235 for (int i=0; i<N; i++) {
1236 mNotificationList.get(i).dump(pw, " ", mContext);
1237 }
1238 pw.println(" ");
1239 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001240
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 N = mLights.size();
1242 if (N > 0) {
1243 pw.println(" Lights List:");
1244 for (int i=0; i<N; i++) {
1245 mLights.get(i).dump(pw, " ", mContext);
1246 }
1247 pw.println(" ");
1248 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001249
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001250 pw.println(" mSoundNotification=" + mSoundNotification);
1251 pw.println(" mSound=" + mSound);
1252 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001253 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
1254 pw.println(" mSystemReady=" + mSystemReady);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 }
1256 }
1257}