blob: a51691ca8a5e4b8303b227aed8e9984d32fcb3f1 [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 Onorato089de882010-04-12 08:18:45 -070020import com.android.server.status.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;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 ITransientNotification callback;
168 int duration;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700169 final Notification notification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 IBinder statusBarKey;
171
Fred Quintana6ecaff12009-09-25 14:23:13 -0700172 NotificationRecord(String pkg, String tag, int id, Notification notification)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 {
174 this.pkg = pkg;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700175 this.tag = tag;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 this.id = id;
177 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,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700275 Notification.FLAG_FOREGROUND_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 }
277
278 public void onPanelRevealed() {
279 synchronized (mNotificationList) {
280 // sound
281 mSoundNotification = null;
282 long identity = Binder.clearCallingIdentity();
283 try {
284 mSound.stop();
285 }
286 finally {
287 Binder.restoreCallingIdentity(identity);
288 }
289
290 // vibrate
291 mVibrateNotification = null;
292 identity = Binder.clearCallingIdentity();
293 try {
294 mVibrator.cancel();
295 }
296 finally {
297 Binder.restoreCallingIdentity(identity);
298 }
299
300 // light
301 mLights.clear();
302 mLedNotification = null;
303 updateLightsLocked();
304 }
305 }
Joe Onorato005847b2010-06-04 16:08:02 -0400306
307 public void onNotificationError(String pkg, String tag, int id, String message) {
308 Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id);
309 cancelNotification(pkg, tag, id, 0, 0);
310 // TODO: Tell the activity manager.
311 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 };
313
314 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
315 @Override
316 public void onReceive(Context context, Intent intent) {
317 String action = intent.getAction();
318
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800319 boolean queryRestart = false;
320
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800321 if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
322 boolean batteryCharging = (intent.getIntExtra("plugged", 0) != 0);
323 int level = intent.getIntExtra("level", -1);
324 boolean batteryLow = (level >= 0 && level <= Power.LOW_BATTERY_THRESHOLD);
325 int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
326 boolean batteryFull = (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90);
327
328 if (batteryCharging != mBatteryCharging ||
329 batteryLow != mBatteryLow ||
330 batteryFull != mBatteryFull) {
331 mBatteryCharging = batteryCharging;
332 mBatteryLow = batteryLow;
333 mBatteryFull = batteryFull;
334 updateLights();
335 }
Mike Lockwoodea8b7d52009-08-04 17:03:15 -0400336 } else if (action.equals(Intent.ACTION_UMS_CONNECTED)) {
337 mUsbConnected = true;
338 updateAdbNotification();
339 } else if (action.equals(Intent.ACTION_UMS_DISCONNECTED)) {
340 mUsbConnected = false;
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700341 updateAdbNotification();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800343 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800344 || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800345 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800346 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800347 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800348 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800349 } else if (queryRestart) {
350 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800351 } else {
352 Uri uri = intent.getData();
353 if (uri == null) {
354 return;
355 }
356 String pkgName = uri.getSchemeSpecificPart();
357 if (pkgName == null) {
358 return;
359 }
360 pkgList = new String[]{pkgName};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800362 if (pkgList != null && (pkgList.length > 0)) {
363 for (String pkgName : pkgList) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800364 cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800365 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800366 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500367 } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
368 mScreenOn = true;
369 updateNotificationPulse();
370 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
371 mScreenOn = false;
372 updateNotificationPulse();
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500373 } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
374 mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK));
375 updateNotificationPulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800376 }
377 }
378 };
379
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700380 class SettingsObserver extends ContentObserver {
381 SettingsObserver(Handler handler) {
382 super(handler);
383 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800384
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700385 void observe() {
386 ContentResolver resolver = mContext.getContentResolver();
387 resolver.registerContentObserver(Settings.Secure.getUriFor(
388 Settings.Secure.ADB_ENABLED), false, this);
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 adbEnabled = Settings.Secure.getInt(resolver,
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700401 Settings.Secure.ADB_ENABLED, 0) != 0;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500402 if (mAdbEnabled != adbEnabled) {
403 mAdbEnabled = adbEnabled;
404 updateAdbNotification();
405 }
406 boolean pulseEnabled = Settings.System.getInt(resolver,
407 Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0;
408 if (mNotificationPulseEnabled != pulseEnabled) {
409 mNotificationPulseEnabled = pulseEnabled;
410 updateNotificationPulse();
411 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700412 }
413 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500414
Joe Onorato089de882010-04-12 08:18:45 -0700415 NotificationManagerService(Context context, StatusBarManagerService statusBar,
Mike Lockwood3a322132009-11-24 00:30:52 -0500416 LightsService lights)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 {
418 super();
419 mContext = context;
Mike Lockwood3a322132009-11-24 00:30:52 -0500420 mLightsService = lights;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 mAm = ActivityManagerNative.getDefault();
Jean-Michel Trivi211957f2010-03-26 18:19:33 -0700422 mSound = new NotificationPlayer(TAG);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800423 mSound.setUsesWakeLock(context);
424 mToastQueue = new ArrayList<ToastRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 mHandler = new WorkerHandler();
San Mehat3ee13172010-02-04 20:54:43 -0800426
Joe Onorato089de882010-04-12 08:18:45 -0700427 mStatusBar = statusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800428 statusBar.setNotificationCallbacks(mNotificationCallbacks);
429
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500430 mBatteryLight = lights.getLight(LightsService.LIGHT_ID_BATTERY);
431 mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS);
432 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
433
Mike Lockwood670f9322010-01-20 12:13:36 -0500434 Resources resources = mContext.getResources();
435 mDefaultNotificationColor = resources.getColor(
436 com.android.internal.R.color.config_defaultNotificationColor);
437 mDefaultNotificationLedOn = resources.getInteger(
438 com.android.internal.R.integer.config_defaultNotificationLedOn);
439 mDefaultNotificationLedOff = resources.getInteger(
440 com.android.internal.R.integer.config_defaultNotificationLedOff);
441
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400442 // Don't start allowing notifications until the setup wizard has run once.
443 // After that, including subsequent boots, init with notifications turned on.
444 // This works on the first boot because the setup wizard will toggle this
445 // flag at least once and we'll go back to 0 after that.
446 if (0 == Settings.Secure.getInt(mContext.getContentResolver(),
447 Settings.Secure.DEVICE_PROVISIONED, 0)) {
448 mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
449 }
450
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 // register for battery changed notifications
452 IntentFilter filter = new IntentFilter();
453 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
Mike Lockwoodea8b7d52009-08-04 17:03:15 -0400454 filter.addAction(Intent.ACTION_UMS_CONNECTED);
455 filter.addAction(Intent.ACTION_UMS_DISCONNECTED);
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500456 filter.addAction(Intent.ACTION_SCREEN_ON);
457 filter.addAction(Intent.ACTION_SCREEN_OFF);
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500458 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800460 IntentFilter pkgFilter = new IntentFilter();
461 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
462 pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
463 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
464 pkgFilter.addDataScheme("package");
465 mContext.registerReceiver(mIntentReceiver, pkgFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800466 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800467 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800468
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500469 SettingsObserver observer = new SettingsObserver(mHandler);
470 observer.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 }
472
Joe Onorato30275482009-07-08 17:09:14 -0700473 void systemReady() {
474 // no beeping until we're basically done booting
475 mSystemReady = true;
476 }
477
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 // Toasts
479 // ============================================================================
480 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
481 {
Daniel Sandlera7035902010-03-30 15:45:31 -0400482 if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483
484 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800485 Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 return ;
487 }
488
489 synchronized (mToastQueue) {
490 int callingPid = Binder.getCallingPid();
491 long callingId = Binder.clearCallingIdentity();
492 try {
493 ToastRecord record;
494 int index = indexOfToastLocked(pkg, callback);
495 // If it's already in the queue, we update it in place, we don't
496 // move it to the end of the queue.
497 if (index >= 0) {
498 record = mToastQueue.get(index);
499 record.update(duration);
500 } else {
501 record = new ToastRecord(callingPid, pkg, callback, duration);
502 mToastQueue.add(record);
503 index = mToastQueue.size() - 1;
504 keepProcessAliveLocked(callingPid);
505 }
506 // If it's at index 0, it's the current toast. It doesn't matter if it's
507 // new or just been updated. Call back and tell it to show itself.
508 // If the callback fails, this will remove it from the list, so don't
509 // assume that it's valid after this.
510 if (index == 0) {
511 showNextToastLocked();
512 }
513 } finally {
514 Binder.restoreCallingIdentity(callingId);
515 }
516 }
517 }
518
519 public void cancelToast(String pkg, ITransientNotification callback) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800520 Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521
522 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800523 Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 return ;
525 }
526
527 synchronized (mToastQueue) {
528 long callingId = Binder.clearCallingIdentity();
529 try {
530 int index = indexOfToastLocked(pkg, callback);
531 if (index >= 0) {
532 cancelToastLocked(index);
533 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800534 Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800535 }
536 } finally {
537 Binder.restoreCallingIdentity(callingId);
538 }
539 }
540 }
541
542 private void showNextToastLocked() {
543 ToastRecord record = mToastQueue.get(0);
544 while (record != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800545 if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 try {
547 record.callback.show();
548 scheduleTimeoutLocked(record, false);
549 return;
550 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800551 Slog.w(TAG, "Object died trying to show notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 + " in package " + record.pkg);
553 // remove it from the list and let the process die
554 int index = mToastQueue.indexOf(record);
555 if (index >= 0) {
556 mToastQueue.remove(index);
557 }
558 keepProcessAliveLocked(record.pid);
559 if (mToastQueue.size() > 0) {
560 record = mToastQueue.get(0);
561 } else {
562 record = null;
563 }
564 }
565 }
566 }
567
568 private void cancelToastLocked(int index) {
569 ToastRecord record = mToastQueue.get(index);
570 try {
571 record.callback.hide();
572 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800573 Slog.w(TAG, "Object died trying to hide notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800574 + " in package " + record.pkg);
575 // don't worry about this, we're about to remove it from
576 // the list anyway
577 }
578 mToastQueue.remove(index);
579 keepProcessAliveLocked(record.pid);
580 if (mToastQueue.size() > 0) {
581 // Show the next one. If the callback fails, this will remove
582 // it from the list, so don't assume that the list hasn't changed
583 // after this point.
584 showNextToastLocked();
585 }
586 }
587
588 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
589 {
590 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
591 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
592 mHandler.removeCallbacksAndMessages(r);
593 mHandler.sendMessageDelayed(m, delay);
594 }
595
596 private void handleTimeout(ToastRecord record)
597 {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800598 if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 synchronized (mToastQueue) {
600 int index = indexOfToastLocked(record.pkg, record.callback);
601 if (index >= 0) {
602 cancelToastLocked(index);
603 }
604 }
605 }
606
607 // lock on mToastQueue
608 private int indexOfToastLocked(String pkg, ITransientNotification callback)
609 {
610 IBinder cbak = callback.asBinder();
611 ArrayList<ToastRecord> list = mToastQueue;
612 int len = list.size();
613 for (int i=0; i<len; i++) {
614 ToastRecord r = list.get(i);
615 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
616 return i;
617 }
618 }
619 return -1;
620 }
621
622 // lock on mToastQueue
623 private void keepProcessAliveLocked(int pid)
624 {
625 int toastCount = 0; // toasts from this pid
626 ArrayList<ToastRecord> list = mToastQueue;
627 int N = list.size();
628 for (int i=0; i<N; i++) {
629 ToastRecord r = list.get(i);
630 if (r.pid == pid) {
631 toastCount++;
632 }
633 }
634 try {
635 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
636 } catch (RemoteException e) {
637 // Shouldn't happen.
638 }
639 }
640
641 private final class WorkerHandler extends Handler
642 {
643 @Override
644 public void handleMessage(Message msg)
645 {
646 switch (msg.what)
647 {
648 case MESSAGE_TIMEOUT:
649 handleTimeout((ToastRecord)msg.obj);
650 break;
651 }
652 }
653 }
654
655
656 // Notifications
657 // ============================================================================
658 public void enqueueNotification(String pkg, int id, Notification notification, int[] idOut)
659 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700660 enqueueNotificationWithTag(pkg, null /* tag */, id, notification, idOut);
661 }
662
663 public void enqueueNotificationWithTag(String pkg, String tag, int id,
664 Notification notification, int[] idOut)
665 {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700666 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800667
Joe Onoratobd73d012010-06-04 11:44:54 -0700668 // Limit the number of notifications that any given package except the android
669 // package can enqueue. Prevents DOS attacks and deals with leaks.
670 if (!"android".equals(pkg)) {
671 synchronized (mNotificationList) {
672 int count = 0;
673 final int N = mNotificationList.size();
674 for (int i=0; i<N; i++) {
675 final NotificationRecord r = mNotificationList.get(i);
676 if (r.pkg.equals(pkg)) {
677 count++;
678 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
679 Slog.e(TAG, "Package has already posted " + count
680 + " notifications. Not showing more. package=" + pkg);
681 return;
682 }
683 }
684 }
685 }
686 }
687
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800688 // This conditional is a dirty hack to limit the logging done on
689 // behalf of the download manager without affecting other apps.
690 if (!pkg.equals("com.android.providers.downloads")
691 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800692 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 }
694
695 if (pkg == null || notification == null) {
696 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
697 + " id=" + id + " notification=" + notification);
698 }
699 if (notification.icon != 0) {
700 if (notification.contentView == null) {
701 throw new IllegalArgumentException("contentView required: pkg=" + pkg
702 + " id=" + id + " notification=" + notification);
703 }
704 if (notification.contentIntent == null) {
705 throw new IllegalArgumentException("contentIntent required: pkg=" + pkg
706 + " id=" + id + " notification=" + notification);
707 }
708 }
709
710 synchronized (mNotificationList) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700711 NotificationRecord r = new NotificationRecord(pkg, tag, id, notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 NotificationRecord old = null;
713
Fred Quintana6ecaff12009-09-25 14:23:13 -0700714 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800715 if (index < 0) {
716 mNotificationList.add(r);
717 } else {
718 old = mNotificationList.remove(index);
719 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700720 // Make sure we don't lose the foreground service state.
721 if (old != null) {
722 notification.flags |=
723 old.notification.flags&Notification.FLAG_FOREGROUND_SERVICE;
724 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800726
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700727 // Ensure if this is a foreground service that the proper additional
728 // flags are set.
729 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
730 notification.flags |= Notification.FLAG_ONGOING_EVENT
731 | Notification.FLAG_NO_CLEAR;
732 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800733
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 if (notification.icon != 0) {
Joe Onorato18e69df2010-05-17 22:26:12 -0700735 StatusBarNotification n = new StatusBarNotification(pkg, id, tag, notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 if (old != null && old.statusBarKey != null) {
737 r.statusBarKey = old.statusBarKey;
738 long identity = Binder.clearCallingIdentity();
739 try {
Joe Onorato18e69df2010-05-17 22:26:12 -0700740 mStatusBar.updateNotification(r.statusBarKey, n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 }
742 finally {
743 Binder.restoreCallingIdentity(identity);
744 }
745 } else {
746 long identity = Binder.clearCallingIdentity();
747 try {
Joe Onorato18e69df2010-05-17 22:26:12 -0700748 r.statusBarKey = mStatusBar.addNotification(n);
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500749 mAttentionLight.pulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 }
751 finally {
752 Binder.restoreCallingIdentity(identity);
753 }
754 }
Joe Onorato30275482009-07-08 17:09:14 -0700755 sendAccessibilityEvent(notification, pkg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800756 } else {
757 if (old != null && old.statusBarKey != null) {
758 long identity = Binder.clearCallingIdentity();
759 try {
Joe Onorato0cbda992010-05-02 16:28:15 -0700760 mStatusBar.removeNotification(old.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 }
762 finally {
763 Binder.restoreCallingIdentity(identity);
764 }
765 }
766 }
767
768 // If we're not supposed to beep, vibrate, etc. then don't.
769 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
770 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -0700771 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
772 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -0800773
774 final AudioManager audioManager = (AudioManager) mContext
775 .getSystemService(Context.AUDIO_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 // sound
777 final boolean useDefaultSound =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800778 (notification.defaults & Notification.DEFAULT_SOUND) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800779 if (useDefaultSound || notification.sound != null) {
780 Uri uri;
781 if (useDefaultSound) {
782 uri = Settings.System.DEFAULT_NOTIFICATION_URI;
783 } else {
784 uri = notification.sound;
785 }
786 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
787 int audioStreamType;
788 if (notification.audioStreamType >= 0) {
789 audioStreamType = notification.audioStreamType;
790 } else {
791 audioStreamType = DEFAULT_STREAM_TYPE;
792 }
793 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -0800794 // do not play notifications if stream volume is 0
795 // (typically because ringer mode is silent).
796 if (audioManager.getStreamVolume(audioStreamType) != 0) {
797 long identity = Binder.clearCallingIdentity();
798 try {
799 mSound.play(mContext, uri, looping, audioStreamType);
800 }
801 finally {
802 Binder.restoreCallingIdentity(identity);
803 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 }
805 }
806
807 // vibrate
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 final boolean useDefaultVibrate =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800809 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800810 if ((useDefaultVibrate || notification.vibrate != null)
811 && audioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_NOTIFICATION)) {
812 mVibrateNotification = r;
813
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800814 mVibrator.vibrate(useDefaultVibrate ? DEFAULT_VIBRATE_PATTERN
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 : notification.vibrate,
816 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
817 }
818 }
819
820 // this option doesn't shut off the lights
821
822 // light
823 // the most recent thing gets the light
824 mLights.remove(old);
825 if (mLedNotification == old) {
826 mLedNotification = null;
827 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800828 //Slog.i(TAG, "notification.lights="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
830 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
831 mLights.add(r);
832 updateLightsLocked();
833 } else {
834 if (old != null
835 && ((old.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0)) {
836 updateLightsLocked();
837 }
838 }
839 }
840
841 idOut[0] = id;
842 }
843
Joe Onorato30275482009-07-08 17:09:14 -0700844 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700845 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
846 if (!manager.isEnabled()) {
847 return;
848 }
849
850 AccessibilityEvent event =
851 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
852 event.setPackageName(packageName);
853 event.setClassName(Notification.class.getName());
854 event.setParcelableData(notification);
855 CharSequence tickerText = notification.tickerText;
856 if (!TextUtils.isEmpty(tickerText)) {
857 event.getText().add(tickerText);
858 }
859
860 manager.sendAccessibilityEvent(event);
861 }
862
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 private void cancelNotificationLocked(NotificationRecord r) {
864 // status bar
865 if (r.notification.icon != 0) {
866 long identity = Binder.clearCallingIdentity();
867 try {
Joe Onorato0cbda992010-05-02 16:28:15 -0700868 mStatusBar.removeNotification(r.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800869 }
870 finally {
871 Binder.restoreCallingIdentity(identity);
872 }
873 r.statusBarKey = null;
874 }
875
876 // sound
877 if (mSoundNotification == r) {
878 mSoundNotification = null;
879 long identity = Binder.clearCallingIdentity();
880 try {
881 mSound.stop();
882 }
883 finally {
884 Binder.restoreCallingIdentity(identity);
885 }
886 }
887
888 // vibrate
889 if (mVibrateNotification == r) {
890 mVibrateNotification = null;
891 long identity = Binder.clearCallingIdentity();
892 try {
893 mVibrator.cancel();
894 }
895 finally {
896 Binder.restoreCallingIdentity(identity);
897 }
898 }
899
900 // light
901 mLights.remove(r);
902 if (mLedNotification == r) {
903 mLedNotification = null;
904 }
905 }
906
907 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700908 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800909 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 */
Fred Quintana6ecaff12009-09-25 14:23:13 -0700911 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700912 int mustNotHaveFlags) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800913 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914
915 synchronized (mNotificationList) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700916 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700918 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800919
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
921 return;
922 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700923 if ((r.notification.flags & mustNotHaveFlags) != 0) {
924 return;
925 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800926
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927 mNotificationList.remove(index);
928
929 cancelNotificationLocked(r);
930 updateLightsLocked();
931 }
932 }
933 }
934
935 /**
936 * Cancels all notifications from a given package that have all of the
937 * {@code mustHaveFlags}.
938 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800939 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
940 int mustNotHaveFlags, boolean doit) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800941 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942
943 synchronized (mNotificationList) {
944 final int N = mNotificationList.size();
945 boolean canceledSomething = false;
946 for (int i = N-1; i >= 0; --i) {
947 NotificationRecord r = mNotificationList.get(i);
948 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
949 continue;
950 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700951 if ((r.notification.flags & mustNotHaveFlags) != 0) {
952 continue;
953 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 if (!r.pkg.equals(pkg)) {
955 continue;
956 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800957 canceledSomething = true;
958 if (!doit) {
959 return true;
960 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800961 mNotificationList.remove(i);
962 cancelNotificationLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 }
964 if (canceledSomething) {
965 updateLightsLocked();
966 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800967 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800968 }
969 }
970
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800971
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700972 public void cancelNotification(String pkg, int id) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700973 cancelNotificationWithTag(pkg, null /* tag */, id);
974 }
975
976 public void cancelNotificationWithTag(String pkg, String tag, int id) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700977 checkIncomingCall(pkg);
978 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700979 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700980 Binder.getCallingUid() == Process.SYSTEM_UID
981 ? 0 : Notification.FLAG_FOREGROUND_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 }
983
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700984 public void cancelAllNotifications(String pkg) {
985 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800986
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700987 // Calling from user space, don't allow the canceling of actively
988 // running foreground services.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800989 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 }
991
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700992 void checkIncomingCall(String pkg) {
993 int uid = Binder.getCallingUid();
994 if (uid == Process.SYSTEM_UID || uid == 0) {
995 return;
996 }
997 try {
998 ApplicationInfo ai = mContext.getPackageManager().getApplicationInfo(
999 pkg, 0);
1000 if (ai.uid != uid) {
1001 throw new SecurityException("Calling uid " + uid + " gave package"
1002 + pkg + " which is owned by uid " + ai.uid);
1003 }
1004 } catch (PackageManager.NameNotFoundException e) {
1005 throw new SecurityException("Unknown package " + pkg);
1006 }
1007 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001008
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001009 void cancelAll() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 synchronized (mNotificationList) {
1011 final int N = mNotificationList.size();
1012 for (int i=N-1; i>=0; i--) {
1013 NotificationRecord r = mNotificationList.get(i);
1014
1015 if ((r.notification.flags & (Notification.FLAG_ONGOING_EVENT
1016 | Notification.FLAG_NO_CLEAR)) == 0) {
1017 if (r.notification.deleteIntent != null) {
1018 try {
1019 r.notification.deleteIntent.send();
1020 } catch (PendingIntent.CanceledException ex) {
1021 // do nothing - there's no relevant way to recover, and
1022 // no reason to let this propagate
Joe Onorato8a9b2202010-02-26 18:56:32 -08001023 Slog.w(TAG, "canceled PendingIntent for " + r.pkg, ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 }
1025 }
1026 mNotificationList.remove(i);
1027 cancelNotificationLocked(r);
1028 }
1029 }
1030
1031 updateLightsLocked();
1032 }
1033 }
1034
1035 private void updateLights() {
1036 synchronized (mNotificationList) {
1037 updateLightsLocked();
1038 }
1039 }
1040
1041 // lock on mNotificationList
1042 private void updateLightsLocked()
1043 {
The Android Open Source Project10592532009-03-18 17:39:46 -07001044 // Battery low always shows, other states only show if charging.
1045 if (mBatteryLow) {
Mike Lockwood445f4302009-09-04 11:06:46 -04001046 if (mBatteryCharging) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001047 mBatteryLight.setColor(BATTERY_LOW_ARGB);
Mike Lockwood445f4302009-09-04 11:06:46 -04001048 } else {
1049 // Flash when battery is low and not charging
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001050 mBatteryLight.setFlashing(BATTERY_LOW_ARGB, LightsService.LIGHT_FLASH_TIMED,
1051 BATTERY_BLINK_ON, BATTERY_BLINK_OFF);
Mike Lockwood445f4302009-09-04 11:06:46 -04001052 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001053 } else if (mBatteryCharging) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001054 if (mBatteryFull) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001055 mBatteryLight.setColor(BATTERY_FULL_ARGB);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001056 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001057 mBatteryLight.setColor(BATTERY_MEDIUM_ARGB);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001058 }
1059 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001060 mBatteryLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001061 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001062
The Android Open Source Project10592532009-03-18 17:39:46 -07001063 // handle notification lights
1064 if (mLedNotification == null) {
1065 // get next notification, if any
1066 int n = mLights.size();
1067 if (n > 0) {
1068 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 }
1070 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001071
1072 // we only flash if screen is off and persistent pulsing is enabled
Daniel Sandlere96ffb12010-03-11 13:38:06 -05001073 // and we are not currently in a call
1074 if (mLedNotification == null || mScreenOn || mInCall) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001075 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001076 } else {
Mike Lockwood670f9322010-01-20 12:13:36 -05001077 int ledARGB = mLedNotification.notification.ledARGB;
1078 int ledOnMS = mLedNotification.notification.ledOnMS;
1079 int ledOffMS = mLedNotification.notification.ledOffMS;
1080 if ((mLedNotification.notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
1081 ledARGB = mDefaultNotificationColor;
1082 ledOnMS = mDefaultNotificationLedOn;
1083 ledOffMS = mDefaultNotificationLedOff;
1084 }
1085 if (mNotificationPulseEnabled) {
1086 // pulse repeatedly
1087 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
1088 ledOnMS, ledOffMS);
1089 } else {
1090 // pulse only once
1091 mNotificationLight.pulse(ledARGB, ledOnMS);
1092 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001093 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001094 }
1095
1096 // lock on mNotificationList
Fred Quintana6ecaff12009-09-25 14:23:13 -07001097 private int indexOfNotificationLocked(String pkg, String tag, int id)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001098 {
1099 ArrayList<NotificationRecord> list = mNotificationList;
1100 final int len = list.size();
1101 for (int i=0; i<len; i++) {
1102 NotificationRecord r = list.get(i);
Fred Quintana6ecaff12009-09-25 14:23:13 -07001103 if (tag == null) {
1104 if (r.tag != null) {
1105 continue;
1106 }
1107 } else {
1108 if (!tag.equals(r.tag)) {
1109 continue;
1110 }
1111 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001112 if (r.id == id && r.pkg.equals(pkg)) {
1113 return i;
1114 }
1115 }
1116 return -1;
1117 }
1118
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001119 // This is here instead of StatusBarPolicy because it is an important
1120 // security feature that we don't want people customizing the platform
1121 // to accidentally lose.
1122 private void updateAdbNotification() {
Mike Lockwoodea8b7d52009-08-04 17:03:15 -04001123 if (mAdbEnabled && mUsbConnected) {
Mike Lockwooded760372009-07-09 07:07:27 -04001124 if ("0".equals(SystemProperties.get("persist.adb.notify"))) {
1125 return;
1126 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001127 if (!mAdbNotificationShown) {
1128 NotificationManager notificationManager = (NotificationManager) mContext
1129 .getSystemService(Context.NOTIFICATION_SERVICE);
1130 if (notificationManager != null) {
1131 Resources r = mContext.getResources();
1132 CharSequence title = r.getText(
1133 com.android.internal.R.string.adb_active_notification_title);
1134 CharSequence message = r.getText(
1135 com.android.internal.R.string.adb_active_notification_message);
1136
1137 if (mAdbNotification == null) {
1138 mAdbNotification = new Notification();
Daniel Sandler39576c82010-03-25 16:02:33 -04001139 mAdbNotification.icon = com.android.internal.R.drawable.stat_sys_adb;
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001140 mAdbNotification.when = 0;
1141 mAdbNotification.flags = Notification.FLAG_ONGOING_EVENT;
1142 mAdbNotification.tickerText = title;
Daniel Sandler39576c82010-03-25 16:02:33 -04001143 mAdbNotification.defaults = 0; // please be quiet
1144 mAdbNotification.sound = null;
1145 mAdbNotification.vibrate = null;
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001146 }
1147
1148 Intent intent = new Intent(
1149 Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS);
1150 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1151 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1152 // Note: we are hard-coding the component because this is
1153 // an important security UI that we don't want anyone
1154 // intercepting.
1155 intent.setComponent(new ComponentName("com.android.settings",
1156 "com.android.settings.DevelopmentSettings"));
1157 PendingIntent pi = PendingIntent.getActivity(mContext, 0,
1158 intent, 0);
1159
1160 mAdbNotification.setLatestEventInfo(mContext, title, message, pi);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001161
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001162 mAdbNotificationShown = true;
1163 notificationManager.notify(
1164 com.android.internal.R.string.adb_active_notification_title,
1165 mAdbNotification);
1166 }
1167 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001168
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001169 } else if (mAdbNotificationShown) {
1170 NotificationManager notificationManager = (NotificationManager) mContext
1171 .getSystemService(Context.NOTIFICATION_SERVICE);
1172 if (notificationManager != null) {
1173 mAdbNotificationShown = false;
1174 notificationManager.cancel(
1175 com.android.internal.R.string.adb_active_notification_title);
1176 }
1177 }
1178 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001179
1180 private void updateNotificationPulse() {
1181 synchronized (mNotificationList) {
1182 updateLightsLocked();
1183 }
1184 }
1185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 // ======================================================================
1187 @Override
1188 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1189 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1190 != PackageManager.PERMISSION_GRANTED) {
1191 pw.println("Permission Denial: can't dump NotificationManager from from pid="
1192 + Binder.getCallingPid()
1193 + ", uid=" + Binder.getCallingUid());
1194 return;
1195 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001196
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 pw.println("Current Notification Manager state:");
1198
1199 int N;
1200
1201 synchronized (mToastQueue) {
1202 N = mToastQueue.size();
1203 if (N > 0) {
1204 pw.println(" Toast Queue:");
1205 for (int i=0; i<N; i++) {
1206 mToastQueue.get(i).dump(pw, " ");
1207 }
1208 pw.println(" ");
1209 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001210
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001211 }
1212
1213 synchronized (mNotificationList) {
1214 N = mNotificationList.size();
1215 if (N > 0) {
1216 pw.println(" Notification List:");
1217 for (int i=0; i<N; i++) {
1218 mNotificationList.get(i).dump(pw, " ", mContext);
1219 }
1220 pw.println(" ");
1221 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001222
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 N = mLights.size();
1224 if (N > 0) {
1225 pw.println(" Lights List:");
1226 for (int i=0; i<N; i++) {
1227 mLights.get(i).dump(pw, " ", mContext);
1228 }
1229 pw.println(" ");
1230 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001232 pw.println(" mSoundNotification=" + mSoundNotification);
1233 pw.println(" mSound=" + mSound);
1234 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001235 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
1236 pw.println(" mSystemReady=" + mSystemReady);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 }
1238 }
1239}