blob: 63325dd040c8811ce9ba69a6f2b23e6b4544a71b [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Joe Onorato18e69df2010-05-17 22:26:12 -070019import com.android.internal.statusbar.StatusBarNotification;
Joe Onorato7a0f36b2010-06-07 10:24:36 -070020import com.android.server.StatusBarManagerService;
svetoslavganov75986cf2009-05-14 22:28:01 -070021
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.app.ActivityManagerNative;
23import android.app.IActivityManager;
24import android.app.INotificationManager;
25import android.app.ITransientNotification;
26import android.app.Notification;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070027import android.app.NotificationManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080028import android.app.PendingIntent;
29import android.app.StatusBarManager;
30import android.content.BroadcastReceiver;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070031import android.content.ComponentName;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070032import android.content.ContentResolver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.content.Context;
34import android.content.Intent;
35import android.content.IntentFilter;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070036import android.content.pm.ApplicationInfo;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.content.pm.PackageManager;
38import android.content.pm.PackageManager.NameNotFoundException;
39import android.content.res.Resources;
Dianne Hackborn1dac2772009-06-26 18:16:48 -070040import android.database.ContentObserver;
Mike Lockwoodff2544c2010-06-28 09:17:50 -040041import android.hardware.Usb;
svetoslavganov75986cf2009-05-14 22:28:01 -070042import android.media.AudioManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.net.Uri;
44import android.os.BatteryManager;
Mike Lockwoodff2544c2010-06-28 09:17:50 -040045import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.os.Binder;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.os.Handler;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048import android.os.IBinder;
49import android.os.Message;
50import android.os.Power;
Dianne Hackbornd8a43f62009-08-17 23:33:56 -070051import android.os.Process;
svetoslavganov75986cf2009-05-14 22:28:01 -070052import android.os.RemoteException;
Mike Lockwooded760372009-07-09 07:07:27 -040053import android.os.SystemProperties;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054import android.os.Vibrator;
55import android.provider.Settings;
Daniel Sandlere96ffb12010-03-11 13:38:06 -050056import android.telephony.TelephonyManager;
svetoslavganov75986cf2009-05-14 22:28:01 -070057import android.text.TextUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058import android.util.EventLog;
Joe Onorato8a9b2202010-02-26 18:56:32 -080059import android.util.Slog;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060import android.util.Log;
svetoslavganov75986cf2009-05-14 22:28:01 -070061import android.view.accessibility.AccessibilityEvent;
62import android.view.accessibility.AccessibilityManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.widget.Toast;
64
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065import java.io.FileDescriptor;
66import java.io.PrintWriter;
67import java.util.ArrayList;
68import java.util.Arrays;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069
Daniel Sandlerd0a2f862010-08-03 15:29:31 -040070/** {@hide} */
71public class NotificationManagerService extends INotificationManager.Stub
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072{
73 private static final String TAG = "NotificationService";
74 private static final boolean DBG = false;
75
Joe Onoratobd73d012010-06-04 11:44:54 -070076 private static final int MAX_PACKAGE_NOTIFICATIONS = 50;
77
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 // message codes
79 private static final int MESSAGE_TIMEOUT = 2;
80
81 private static final int LONG_DELAY = 3500; // 3.5 seconds
82 private static final int SHORT_DELAY = 2000; // 2 seconds
Doug Zongkerab5c49c2009-12-04 10:31:43 -080083
84 private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085
86 private static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION;
87
88 final Context mContext;
89 final IActivityManager mAm;
90 final IBinder mForegroundToken = new Binder();
91
92 private WorkerHandler mHandler;
Joe Onorato089de882010-04-12 08:18:45 -070093 private StatusBarManagerService mStatusBar;
Mike Lockwood3a322132009-11-24 00:30:52 -050094 private LightsService mLightsService;
Mike Lockwood3cb67a32009-11-27 14:25:58 -050095 private LightsService.Light mBatteryLight;
96 private LightsService.Light mNotificationLight;
97 private LightsService.Light mAttentionLight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098
Mike Lockwood670f9322010-01-20 12:13:36 -050099 private int mDefaultNotificationColor;
100 private int mDefaultNotificationLedOn;
101 private int mDefaultNotificationLedOff;
102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 private NotificationRecord mSoundNotification;
Jean-Michel Trivi211957f2010-03-26 18:19:33 -0700104 private NotificationPlayer mSound;
Joe Onorato30275482009-07-08 17:09:14 -0700105 private boolean mSystemReady;
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400106 private int mDisabledNotifications;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107
108 private NotificationRecord mVibrateNotification;
109 private Vibrator mVibrator = new Vibrator();
110
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500111 // for enabling and disabling notification pulse behavior
112 private boolean mScreenOn = true;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500113 private boolean mInCall = false;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500114 private boolean mNotificationPulseEnabled;
115
116 // for adb connected notifications
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700117 private boolean mAdbNotificationShown = false;
118 private Notification mAdbNotification;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800119
Fred Quintana6ecaff12009-09-25 14:23:13 -0700120 private final ArrayList<NotificationRecord> mNotificationList =
121 new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122
123 private ArrayList<ToastRecord> mToastQueue;
124
125 private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>();
126
127 private boolean mBatteryCharging;
128 private boolean mBatteryLow;
129 private boolean mBatteryFull;
130 private NotificationRecord mLedNotification;
svetoslavganov75986cf2009-05-14 22:28:01 -0700131
The Android Open Source Project10592532009-03-18 17:39:46 -0700132 private static final int BATTERY_LOW_ARGB = 0xFFFF0000; // Charging Low - red solid on
133 private static final int BATTERY_MEDIUM_ARGB = 0xFFFFFF00; // Charging - orange solid on
134 private static final int BATTERY_FULL_ARGB = 0xFF00FF00; // Charging Full - green solid on
135 private static final int BATTERY_BLINK_ON = 125;
136 private static final int BATTERY_BLINK_OFF = 2875;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 private static String idDebugString(Context baseContext, String packageName, int id) {
139 Context c = null;
140
141 if (packageName != null) {
142 try {
143 c = baseContext.createPackageContext(packageName, 0);
144 } catch (NameNotFoundException e) {
145 c = baseContext;
146 }
147 } else {
148 c = baseContext;
149 }
150
151 String pkg;
152 String type;
153 String name;
154
155 Resources r = c.getResources();
156 try {
157 return r.getResourceName(id);
158 } catch (Resources.NotFoundException e) {
159 return "<name unknown>";
160 }
161 }
162
163 private static final class NotificationRecord
164 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700165 final String pkg;
166 final String tag;
167 final int id;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700168 final int uid;
169 final int initialPid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 ITransientNotification callback;
171 int duration;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700172 final Notification notification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 IBinder statusBarKey;
174
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700175 NotificationRecord(String pkg, String tag, int id, int uid, int initialPid,
176 Notification notification)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 {
178 this.pkg = pkg;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700179 this.tag = tag;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 this.id = id;
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700181 this.uid = uid;
182 this.initialPid = initialPid;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 this.notification = notification;
184 }
Fred Quintana6ecaff12009-09-25 14:23:13 -0700185
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 void dump(PrintWriter pw, String prefix, Context baseContext) {
187 pw.println(prefix + this);
188 pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
189 + " / " + idDebugString(baseContext, this.pkg, notification.icon));
190 pw.println(prefix + " contentIntent=" + notification.contentIntent);
191 pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
192 pw.println(prefix + " tickerText=" + notification.tickerText);
193 pw.println(prefix + " contentView=" + notification.contentView);
194 pw.println(prefix + " defaults=0x" + Integer.toHexString(notification.defaults));
195 pw.println(prefix + " flags=0x" + Integer.toHexString(notification.flags));
196 pw.println(prefix + " sound=" + notification.sound);
197 pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate));
198 pw.println(prefix + " ledARGB=0x" + Integer.toHexString(notification.ledARGB)
199 + " ledOnMS=" + notification.ledOnMS
200 + " ledOffMS=" + notification.ledOffMS);
201 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800202
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 @Override
204 public final String toString()
205 {
206 return "NotificationRecord{"
207 + Integer.toHexString(System.identityHashCode(this))
208 + " pkg=" + pkg
Fred Quintana6ecaff12009-09-25 14:23:13 -0700209 + " id=" + Integer.toHexString(id)
210 + " tag=" + tag + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800211 }
212 }
213
214 private static final class ToastRecord
215 {
216 final int pid;
217 final String pkg;
218 final ITransientNotification callback;
219 int duration;
220
221 ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
222 {
223 this.pid = pid;
224 this.pkg = pkg;
225 this.callback = callback;
226 this.duration = duration;
227 }
228
229 void update(int duration) {
230 this.duration = duration;
231 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800233 void dump(PrintWriter pw, String prefix) {
234 pw.println(prefix + this);
235 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800236
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 @Override
238 public final String toString()
239 {
240 return "ToastRecord{"
241 + Integer.toHexString(System.identityHashCode(this))
242 + " pkg=" + pkg
243 + " callback=" + callback
244 + " duration=" + duration;
245 }
246 }
247
Joe Onorato089de882010-04-12 08:18:45 -0700248 private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks
249 = new StatusBarManagerService.NotificationCallbacks() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250
251 public void onSetDisabled(int status) {
252 synchronized (mNotificationList) {
253 mDisabledNotifications = status;
254 if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) {
255 // cancel whatever's going on
256 long identity = Binder.clearCallingIdentity();
257 try {
258 mSound.stop();
259 }
260 finally {
261 Binder.restoreCallingIdentity(identity);
262 }
263
264 identity = Binder.clearCallingIdentity();
265 try {
266 mVibrator.cancel();
267 }
268 finally {
269 Binder.restoreCallingIdentity(identity);
270 }
271 }
272 }
273 }
274
275 public void onClearAll() {
276 cancelAll();
277 }
278
Fred Quintana6ecaff12009-09-25 14:23:13 -0700279 public void onNotificationClick(String pkg, String tag, int id) {
280 cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700281 Notification.FLAG_FOREGROUND_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800282 }
283
284 public void onPanelRevealed() {
285 synchronized (mNotificationList) {
286 // sound
287 mSoundNotification = null;
288 long identity = Binder.clearCallingIdentity();
289 try {
290 mSound.stop();
291 }
292 finally {
293 Binder.restoreCallingIdentity(identity);
294 }
295
296 // vibrate
297 mVibrateNotification = null;
298 identity = Binder.clearCallingIdentity();
299 try {
300 mVibrator.cancel();
301 }
302 finally {
303 Binder.restoreCallingIdentity(identity);
304 }
305
306 // light
307 mLights.clear();
308 mLedNotification = null;
309 updateLightsLocked();
310 }
311 }
Joe Onorato005847b2010-06-04 16:08:02 -0400312
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700313 public void onNotificationError(String pkg, String tag, int id,
314 int uid, int initialPid, String message) {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400315 Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id
316 + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")");
Joe Onorato005847b2010-06-04 16:08:02 -0400317 cancelNotification(pkg, tag, id, 0, 0);
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700318 long ident = Binder.clearCallingIdentity();
319 try {
320 ActivityManagerNative.getDefault().crashApplication(uid, initialPid, pkg,
321 "Bad notification posted from package " + pkg
322 + ": " + message);
323 } catch (RemoteException e) {
324 }
325 Binder.restoreCallingIdentity(ident);
Joe Onorato005847b2010-06-04 16:08:02 -0400326 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 };
328
329 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
330 @Override
331 public void onReceive(Context context, Intent intent) {
332 String action = intent.getAction();
333
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800334 boolean queryRestart = false;
335
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
337 boolean batteryCharging = (intent.getIntExtra("plugged", 0) != 0);
338 int level = intent.getIntExtra("level", -1);
339 boolean batteryLow = (level >= 0 && level <= Power.LOW_BATTERY_THRESHOLD);
340 int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
341 boolean batteryFull = (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90);
342
343 if (batteryCharging != mBatteryCharging ||
344 batteryLow != mBatteryLow ||
345 batteryFull != mBatteryFull) {
346 mBatteryCharging = batteryCharging;
347 mBatteryLow = batteryLow;
348 mBatteryFull = batteryFull;
349 updateLights();
350 }
Mike Lockwoodff2544c2010-06-28 09:17:50 -0400351 } else if (action.equals(Usb.ACTION_USB_STATE)) {
352 Bundle extras = intent.getExtras();
353 boolean usbConnected = extras.getBoolean(Usb.USB_CONNECTED);
354 boolean adbEnabled = (Usb.USB_FUNCTION_ENABLED.equals(
355 extras.getString(Usb.USB_FUNCTION_ADB)));
356 updateAdbNotification(usbConnected && adbEnabled);
357 } else if (action.equals(Usb.ACTION_USB_DISCONNECTED)) {
358 updateAdbNotification(false);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800360 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800361 || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800362 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800363 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800364 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800365 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800366 } else if (queryRestart) {
367 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800368 } else {
369 Uri uri = intent.getData();
370 if (uri == null) {
371 return;
372 }
373 String pkgName = uri.getSchemeSpecificPart();
374 if (pkgName == null) {
375 return;
376 }
377 pkgList = new String[]{pkgName};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800378 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800379 if (pkgList != null && (pkgList.length > 0)) {
380 for (String pkgName : pkgList) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800381 cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800382 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500384 } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
385 mScreenOn = true;
386 updateNotificationPulse();
387 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
388 mScreenOn = false;
389 updateNotificationPulse();
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500390 } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
391 mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK));
392 updateNotificationPulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 }
394 }
395 };
396
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700397 class SettingsObserver extends ContentObserver {
398 SettingsObserver(Handler handler) {
399 super(handler);
400 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800401
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700402 void observe() {
403 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500404 resolver.registerContentObserver(Settings.System.getUriFor(
405 Settings.System.NOTIFICATION_LIGHT_PULSE), false, this);
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700406 update();
407 }
408
409 @Override public void onChange(boolean selfChange) {
410 update();
411 }
412
413 public void update() {
414 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500415 boolean pulseEnabled = Settings.System.getInt(resolver,
416 Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0;
417 if (mNotificationPulseEnabled != pulseEnabled) {
418 mNotificationPulseEnabled = pulseEnabled;
419 updateNotificationPulse();
420 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700421 }
422 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500423
Joe Onorato089de882010-04-12 08:18:45 -0700424 NotificationManagerService(Context context, StatusBarManagerService statusBar,
Mike Lockwood3a322132009-11-24 00:30:52 -0500425 LightsService lights)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 {
427 super();
428 mContext = context;
Mike Lockwood3a322132009-11-24 00:30:52 -0500429 mLightsService = lights;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 mAm = ActivityManagerNative.getDefault();
Jean-Michel Trivi211957f2010-03-26 18:19:33 -0700431 mSound = new NotificationPlayer(TAG);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432 mSound.setUsesWakeLock(context);
433 mToastQueue = new ArrayList<ToastRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800434 mHandler = new WorkerHandler();
San Mehat3ee13172010-02-04 20:54:43 -0800435
Joe Onorato089de882010-04-12 08:18:45 -0700436 mStatusBar = statusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 statusBar.setNotificationCallbacks(mNotificationCallbacks);
438
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500439 mBatteryLight = lights.getLight(LightsService.LIGHT_ID_BATTERY);
440 mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS);
441 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
442
Mike Lockwood670f9322010-01-20 12:13:36 -0500443 Resources resources = mContext.getResources();
444 mDefaultNotificationColor = resources.getColor(
445 com.android.internal.R.color.config_defaultNotificationColor);
446 mDefaultNotificationLedOn = resources.getInteger(
447 com.android.internal.R.integer.config_defaultNotificationLedOn);
448 mDefaultNotificationLedOff = resources.getInteger(
449 com.android.internal.R.integer.config_defaultNotificationLedOff);
450
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400451 // Don't start allowing notifications until the setup wizard has run once.
452 // After that, including subsequent boots, init with notifications turned on.
453 // This works on the first boot because the setup wizard will toggle this
454 // flag at least once and we'll go back to 0 after that.
455 if (0 == Settings.Secure.getInt(mContext.getContentResolver(),
456 Settings.Secure.DEVICE_PROVISIONED, 0)) {
457 mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
458 }
459
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 // register for battery changed notifications
461 IntentFilter filter = new IntentFilter();
462 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
Mike Lockwoodff2544c2010-06-28 09:17:50 -0400463 filter.addAction(Usb.ACTION_USB_STATE);
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500464 filter.addAction(Intent.ACTION_SCREEN_ON);
465 filter.addAction(Intent.ACTION_SCREEN_OFF);
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500466 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800467 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800468 IntentFilter pkgFilter = new IntentFilter();
469 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
470 pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
471 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
472 pkgFilter.addDataScheme("package");
473 mContext.registerReceiver(mIntentReceiver, pkgFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800474 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800475 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800476
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500477 SettingsObserver observer = new SettingsObserver(mHandler);
478 observer.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800479 }
480
Joe Onorato30275482009-07-08 17:09:14 -0700481 void systemReady() {
482 // no beeping until we're basically done booting
483 mSystemReady = true;
484 }
485
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 // Toasts
487 // ============================================================================
488 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
489 {
Daniel Sandlera7035902010-03-30 15:45:31 -0400490 if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491
492 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800493 Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 return ;
495 }
496
497 synchronized (mToastQueue) {
498 int callingPid = Binder.getCallingPid();
499 long callingId = Binder.clearCallingIdentity();
500 try {
501 ToastRecord record;
502 int index = indexOfToastLocked(pkg, callback);
503 // If it's already in the queue, we update it in place, we don't
504 // move it to the end of the queue.
505 if (index >= 0) {
506 record = mToastQueue.get(index);
507 record.update(duration);
508 } else {
509 record = new ToastRecord(callingPid, pkg, callback, duration);
510 mToastQueue.add(record);
511 index = mToastQueue.size() - 1;
512 keepProcessAliveLocked(callingPid);
513 }
514 // If it's at index 0, it's the current toast. It doesn't matter if it's
515 // new or just been updated. Call back and tell it to show itself.
516 // If the callback fails, this will remove it from the list, so don't
517 // assume that it's valid after this.
518 if (index == 0) {
519 showNextToastLocked();
520 }
521 } finally {
522 Binder.restoreCallingIdentity(callingId);
523 }
524 }
525 }
526
527 public void cancelToast(String pkg, ITransientNotification callback) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800528 Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800529
530 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800531 Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 return ;
533 }
534
535 synchronized (mToastQueue) {
536 long callingId = Binder.clearCallingIdentity();
537 try {
538 int index = indexOfToastLocked(pkg, callback);
539 if (index >= 0) {
540 cancelToastLocked(index);
541 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800542 Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800543 }
544 } finally {
545 Binder.restoreCallingIdentity(callingId);
546 }
547 }
548 }
549
550 private void showNextToastLocked() {
551 ToastRecord record = mToastQueue.get(0);
552 while (record != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800553 if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800554 try {
555 record.callback.show();
556 scheduleTimeoutLocked(record, false);
557 return;
558 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800559 Slog.w(TAG, "Object died trying to show notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800560 + " in package " + record.pkg);
561 // remove it from the list and let the process die
562 int index = mToastQueue.indexOf(record);
563 if (index >= 0) {
564 mToastQueue.remove(index);
565 }
566 keepProcessAliveLocked(record.pid);
567 if (mToastQueue.size() > 0) {
568 record = mToastQueue.get(0);
569 } else {
570 record = null;
571 }
572 }
573 }
574 }
575
576 private void cancelToastLocked(int index) {
577 ToastRecord record = mToastQueue.get(index);
578 try {
579 record.callback.hide();
580 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800581 Slog.w(TAG, "Object died trying to hide notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 + " in package " + record.pkg);
583 // don't worry about this, we're about to remove it from
584 // the list anyway
585 }
586 mToastQueue.remove(index);
587 keepProcessAliveLocked(record.pid);
588 if (mToastQueue.size() > 0) {
589 // Show the next one. If the callback fails, this will remove
590 // it from the list, so don't assume that the list hasn't changed
591 // after this point.
592 showNextToastLocked();
593 }
594 }
595
596 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
597 {
598 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
599 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
600 mHandler.removeCallbacksAndMessages(r);
601 mHandler.sendMessageDelayed(m, delay);
602 }
603
604 private void handleTimeout(ToastRecord record)
605 {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800606 if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 synchronized (mToastQueue) {
608 int index = indexOfToastLocked(record.pkg, record.callback);
609 if (index >= 0) {
610 cancelToastLocked(index);
611 }
612 }
613 }
614
615 // lock on mToastQueue
616 private int indexOfToastLocked(String pkg, ITransientNotification callback)
617 {
618 IBinder cbak = callback.asBinder();
619 ArrayList<ToastRecord> list = mToastQueue;
620 int len = list.size();
621 for (int i=0; i<len; i++) {
622 ToastRecord r = list.get(i);
623 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
624 return i;
625 }
626 }
627 return -1;
628 }
629
630 // lock on mToastQueue
631 private void keepProcessAliveLocked(int pid)
632 {
633 int toastCount = 0; // toasts from this pid
634 ArrayList<ToastRecord> list = mToastQueue;
635 int N = list.size();
636 for (int i=0; i<N; i++) {
637 ToastRecord r = list.get(i);
638 if (r.pid == pid) {
639 toastCount++;
640 }
641 }
642 try {
643 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
644 } catch (RemoteException e) {
645 // Shouldn't happen.
646 }
647 }
648
649 private final class WorkerHandler extends Handler
650 {
651 @Override
652 public void handleMessage(Message msg)
653 {
654 switch (msg.what)
655 {
656 case MESSAGE_TIMEOUT:
657 handleTimeout((ToastRecord)msg.obj);
658 break;
659 }
660 }
661 }
662
663
664 // Notifications
665 // ============================================================================
666 public void enqueueNotification(String pkg, int id, Notification notification, int[] idOut)
667 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700668 enqueueNotificationWithTag(pkg, null /* tag */, id, notification, idOut);
669 }
670
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400671 public void enqueueNotificationWithTag(String pkg, String tag, int id, Notification notification,
672 int[] idOut)
Fred Quintana6ecaff12009-09-25 14:23:13 -0700673 {
Daniel Sandlerd0a2f862010-08-03 15:29:31 -0400674 enqueueNotificationInternal(pkg, Binder.getCallingUid(), Binder.getCallingPid(),
675 tag, id, notification, idOut);
676 }
677
678 // Not exposed via Binder; for system use only (otherwise malicious apps could spoof the
679 // uid/pid of another application)
680 public void enqueueNotificationInternal(String pkg, int callingUid, int callingPid,
681 String tag, int id, Notification notification, int[] idOut)
682 {
683 Slog.d(TAG, "enqueueNotificationWithTag: calling uid=" + callingUid
684 + ", pid=" + callingPid);
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700685
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700686 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800687
Joe Onoratobd73d012010-06-04 11:44:54 -0700688 // Limit the number of notifications that any given package except the android
689 // package can enqueue. Prevents DOS attacks and deals with leaks.
690 if (!"android".equals(pkg)) {
691 synchronized (mNotificationList) {
692 int count = 0;
693 final int N = mNotificationList.size();
694 for (int i=0; i<N; i++) {
695 final NotificationRecord r = mNotificationList.get(i);
696 if (r.pkg.equals(pkg)) {
697 count++;
698 if (count >= MAX_PACKAGE_NOTIFICATIONS) {
699 Slog.e(TAG, "Package has already posted " + count
700 + " notifications. Not showing more. package=" + pkg);
701 return;
702 }
703 }
704 }
705 }
706 }
707
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 // This conditional is a dirty hack to limit the logging done on
709 // behalf of the download manager without affecting other apps.
710 if (!pkg.equals("com.android.providers.downloads")
711 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800712 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 }
714
715 if (pkg == null || notification == null) {
716 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
717 + " id=" + id + " notification=" + notification);
718 }
719 if (notification.icon != 0) {
720 if (notification.contentView == null) {
721 throw new IllegalArgumentException("contentView required: pkg=" + pkg
722 + " id=" + id + " notification=" + notification);
723 }
724 if (notification.contentIntent == null) {
725 throw new IllegalArgumentException("contentIntent required: pkg=" + pkg
726 + " id=" + id + " notification=" + notification);
727 }
728 }
729
730 synchronized (mNotificationList) {
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700731 NotificationRecord r = new NotificationRecord(pkg, tag, id,
732 callingUid, callingPid, notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 NotificationRecord old = null;
734
Fred Quintana6ecaff12009-09-25 14:23:13 -0700735 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800736 if (index < 0) {
737 mNotificationList.add(r);
738 } else {
739 old = mNotificationList.remove(index);
740 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700741 // Make sure we don't lose the foreground service state.
742 if (old != null) {
743 notification.flags |=
744 old.notification.flags&Notification.FLAG_FOREGROUND_SERVICE;
745 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800747
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700748 // Ensure if this is a foreground service that the proper additional
749 // flags are set.
750 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
751 notification.flags |= Notification.FLAG_ONGOING_EVENT
752 | Notification.FLAG_NO_CLEAR;
753 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800754
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800755 if (notification.icon != 0) {
Dianne Hackborn9d39d0c2010-06-24 15:57:42 -0700756 StatusBarNotification n = new StatusBarNotification(pkg, id, tag,
757 r.uid, r.initialPid, notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800758 if (old != null && old.statusBarKey != null) {
759 r.statusBarKey = old.statusBarKey;
760 long identity = Binder.clearCallingIdentity();
761 try {
Joe Onorato18e69df2010-05-17 22:26:12 -0700762 mStatusBar.updateNotification(r.statusBarKey, n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 }
764 finally {
765 Binder.restoreCallingIdentity(identity);
766 }
767 } else {
768 long identity = Binder.clearCallingIdentity();
769 try {
Joe Onorato18e69df2010-05-17 22:26:12 -0700770 r.statusBarKey = mStatusBar.addNotification(n);
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500771 mAttentionLight.pulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800772 }
773 finally {
774 Binder.restoreCallingIdentity(identity);
775 }
776 }
Joe Onorato30275482009-07-08 17:09:14 -0700777 sendAccessibilityEvent(notification, pkg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800778 } else {
779 if (old != null && old.statusBarKey != null) {
780 long identity = Binder.clearCallingIdentity();
781 try {
Joe Onorato0cbda992010-05-02 16:28:15 -0700782 mStatusBar.removeNotification(old.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800783 }
784 finally {
785 Binder.restoreCallingIdentity(identity);
786 }
787 }
788 }
789
790 // If we're not supposed to beep, vibrate, etc. then don't.
791 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
792 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -0700793 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
794 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -0800795
796 final AudioManager audioManager = (AudioManager) mContext
797 .getSystemService(Context.AUDIO_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 // sound
799 final boolean useDefaultSound =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800800 (notification.defaults & Notification.DEFAULT_SOUND) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 if (useDefaultSound || notification.sound != null) {
802 Uri uri;
803 if (useDefaultSound) {
804 uri = Settings.System.DEFAULT_NOTIFICATION_URI;
805 } else {
806 uri = notification.sound;
807 }
808 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
809 int audioStreamType;
810 if (notification.audioStreamType >= 0) {
811 audioStreamType = notification.audioStreamType;
812 } else {
813 audioStreamType = DEFAULT_STREAM_TYPE;
814 }
815 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -0800816 // do not play notifications if stream volume is 0
817 // (typically because ringer mode is silent).
818 if (audioManager.getStreamVolume(audioStreamType) != 0) {
819 long identity = Binder.clearCallingIdentity();
820 try {
821 mSound.play(mContext, uri, looping, audioStreamType);
822 }
823 finally {
824 Binder.restoreCallingIdentity(identity);
825 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800826 }
827 }
828
829 // vibrate
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 final boolean useDefaultVibrate =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800831 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800832 if ((useDefaultVibrate || notification.vibrate != null)
833 && audioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_NOTIFICATION)) {
834 mVibrateNotification = r;
835
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800836 mVibrator.vibrate(useDefaultVibrate ? DEFAULT_VIBRATE_PATTERN
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800837 : notification.vibrate,
838 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
839 }
840 }
841
842 // this option doesn't shut off the lights
843
844 // light
845 // the most recent thing gets the light
846 mLights.remove(old);
847 if (mLedNotification == old) {
848 mLedNotification = null;
849 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800850 //Slog.i(TAG, "notification.lights="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800851 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
852 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
853 mLights.add(r);
854 updateLightsLocked();
855 } else {
856 if (old != null
857 && ((old.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0)) {
858 updateLightsLocked();
859 }
860 }
861 }
862
863 idOut[0] = id;
864 }
865
Joe Onorato30275482009-07-08 17:09:14 -0700866 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700867 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
868 if (!manager.isEnabled()) {
869 return;
870 }
871
872 AccessibilityEvent event =
873 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
874 event.setPackageName(packageName);
875 event.setClassName(Notification.class.getName());
876 event.setParcelableData(notification);
877 CharSequence tickerText = notification.tickerText;
878 if (!TextUtils.isEmpty(tickerText)) {
879 event.getText().add(tickerText);
880 }
881
882 manager.sendAccessibilityEvent(event);
883 }
884
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 private void cancelNotificationLocked(NotificationRecord r) {
886 // status bar
887 if (r.notification.icon != 0) {
888 long identity = Binder.clearCallingIdentity();
889 try {
Joe Onorato0cbda992010-05-02 16:28:15 -0700890 mStatusBar.removeNotification(r.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800891 }
892 finally {
893 Binder.restoreCallingIdentity(identity);
894 }
895 r.statusBarKey = null;
896 }
897
898 // sound
899 if (mSoundNotification == r) {
900 mSoundNotification = null;
901 long identity = Binder.clearCallingIdentity();
902 try {
903 mSound.stop();
904 }
905 finally {
906 Binder.restoreCallingIdentity(identity);
907 }
908 }
909
910 // vibrate
911 if (mVibrateNotification == r) {
912 mVibrateNotification = null;
913 long identity = Binder.clearCallingIdentity();
914 try {
915 mVibrator.cancel();
916 }
917 finally {
918 Binder.restoreCallingIdentity(identity);
919 }
920 }
921
922 // light
923 mLights.remove(r);
924 if (mLedNotification == r) {
925 mLedNotification = null;
926 }
927 }
928
929 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700930 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800931 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800932 */
Fred Quintana6ecaff12009-09-25 14:23:13 -0700933 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700934 int mustNotHaveFlags) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800935 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800936
937 synchronized (mNotificationList) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700938 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700940 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800941
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
943 return;
944 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700945 if ((r.notification.flags & mustNotHaveFlags) != 0) {
946 return;
947 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800949 mNotificationList.remove(index);
950
951 cancelNotificationLocked(r);
952 updateLightsLocked();
953 }
954 }
955 }
956
957 /**
958 * Cancels all notifications from a given package that have all of the
959 * {@code mustHaveFlags}.
960 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800961 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
962 int mustNotHaveFlags, boolean doit) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800963 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800964
965 synchronized (mNotificationList) {
966 final int N = mNotificationList.size();
967 boolean canceledSomething = false;
968 for (int i = N-1; i >= 0; --i) {
969 NotificationRecord r = mNotificationList.get(i);
970 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
971 continue;
972 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700973 if ((r.notification.flags & mustNotHaveFlags) != 0) {
974 continue;
975 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800976 if (!r.pkg.equals(pkg)) {
977 continue;
978 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800979 canceledSomething = true;
980 if (!doit) {
981 return true;
982 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800983 mNotificationList.remove(i);
984 cancelNotificationLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800985 }
986 if (canceledSomething) {
987 updateLightsLocked();
988 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800989 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800990 }
991 }
992
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800993
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700994 public void cancelNotification(String pkg, int id) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700995 cancelNotificationWithTag(pkg, null /* tag */, id);
996 }
997
998 public void cancelNotificationWithTag(String pkg, String tag, int id) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700999 checkIncomingCall(pkg);
1000 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -07001001 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001002 Binder.getCallingUid() == Process.SYSTEM_UID
1003 ? 0 : Notification.FLAG_FOREGROUND_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001004 }
1005
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001006 public void cancelAllNotifications(String pkg) {
1007 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001008
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001009 // Calling from user space, don't allow the canceling of actively
1010 // running foreground services.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -08001011 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001012 }
1013
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001014 void checkIncomingCall(String pkg) {
1015 int uid = Binder.getCallingUid();
1016 if (uid == Process.SYSTEM_UID || uid == 0) {
1017 return;
1018 }
1019 try {
1020 ApplicationInfo ai = mContext.getPackageManager().getApplicationInfo(
1021 pkg, 0);
1022 if (ai.uid != uid) {
1023 throw new SecurityException("Calling uid " + uid + " gave package"
1024 + pkg + " which is owned by uid " + ai.uid);
1025 }
1026 } catch (PackageManager.NameNotFoundException e) {
1027 throw new SecurityException("Unknown package " + pkg);
1028 }
1029 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001030
Dianne Hackbornd8a43f62009-08-17 23:33:56 -07001031 void cancelAll() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001032 synchronized (mNotificationList) {
1033 final int N = mNotificationList.size();
1034 for (int i=N-1; i>=0; i--) {
1035 NotificationRecord r = mNotificationList.get(i);
1036
1037 if ((r.notification.flags & (Notification.FLAG_ONGOING_EVENT
1038 | Notification.FLAG_NO_CLEAR)) == 0) {
1039 if (r.notification.deleteIntent != null) {
1040 try {
1041 r.notification.deleteIntent.send();
1042 } catch (PendingIntent.CanceledException ex) {
1043 // do nothing - there's no relevant way to recover, and
1044 // no reason to let this propagate
Joe Onorato8a9b2202010-02-26 18:56:32 -08001045 Slog.w(TAG, "canceled PendingIntent for " + r.pkg, ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 }
1047 }
1048 mNotificationList.remove(i);
1049 cancelNotificationLocked(r);
1050 }
1051 }
1052
1053 updateLightsLocked();
1054 }
1055 }
1056
1057 private void updateLights() {
1058 synchronized (mNotificationList) {
1059 updateLightsLocked();
1060 }
1061 }
1062
1063 // lock on mNotificationList
1064 private void updateLightsLocked()
1065 {
The Android Open Source Project10592532009-03-18 17:39:46 -07001066 // Battery low always shows, other states only show if charging.
1067 if (mBatteryLow) {
Mike Lockwood445f4302009-09-04 11:06:46 -04001068 if (mBatteryCharging) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001069 mBatteryLight.setColor(BATTERY_LOW_ARGB);
Mike Lockwood445f4302009-09-04 11:06:46 -04001070 } else {
1071 // Flash when battery is low and not charging
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001072 mBatteryLight.setFlashing(BATTERY_LOW_ARGB, LightsService.LIGHT_FLASH_TIMED,
1073 BATTERY_BLINK_ON, BATTERY_BLINK_OFF);
Mike Lockwood445f4302009-09-04 11:06:46 -04001074 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 } else if (mBatteryCharging) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001076 if (mBatteryFull) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001077 mBatteryLight.setColor(BATTERY_FULL_ARGB);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001078 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001079 mBatteryLight.setColor(BATTERY_MEDIUM_ARGB);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001080 }
1081 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001082 mBatteryLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001083 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084
The Android Open Source Project10592532009-03-18 17:39:46 -07001085 // handle notification lights
1086 if (mLedNotification == null) {
1087 // get next notification, if any
1088 int n = mLights.size();
1089 if (n > 0) {
1090 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 }
1092 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001093
1094 // we only flash if screen is off and persistent pulsing is enabled
Daniel Sandlere96ffb12010-03-11 13:38:06 -05001095 // and we are not currently in a call
1096 if (mLedNotification == null || mScreenOn || mInCall) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001097 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001098 } else {
Mike Lockwood670f9322010-01-20 12:13:36 -05001099 int ledARGB = mLedNotification.notification.ledARGB;
1100 int ledOnMS = mLedNotification.notification.ledOnMS;
1101 int ledOffMS = mLedNotification.notification.ledOffMS;
1102 if ((mLedNotification.notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
1103 ledARGB = mDefaultNotificationColor;
1104 ledOnMS = mDefaultNotificationLedOn;
1105 ledOffMS = mDefaultNotificationLedOff;
1106 }
1107 if (mNotificationPulseEnabled) {
1108 // pulse repeatedly
1109 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
1110 ledOnMS, ledOffMS);
1111 } else {
1112 // pulse only once
1113 mNotificationLight.pulse(ledARGB, ledOnMS);
1114 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001115 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 }
1117
1118 // lock on mNotificationList
Fred Quintana6ecaff12009-09-25 14:23:13 -07001119 private int indexOfNotificationLocked(String pkg, String tag, int id)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001120 {
1121 ArrayList<NotificationRecord> list = mNotificationList;
1122 final int len = list.size();
1123 for (int i=0; i<len; i++) {
1124 NotificationRecord r = list.get(i);
Fred Quintana6ecaff12009-09-25 14:23:13 -07001125 if (tag == null) {
1126 if (r.tag != null) {
1127 continue;
1128 }
1129 } else {
1130 if (!tag.equals(r.tag)) {
1131 continue;
1132 }
1133 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001134 if (r.id == id && r.pkg.equals(pkg)) {
1135 return i;
1136 }
1137 }
1138 return -1;
1139 }
1140
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001141 // This is here instead of StatusBarPolicy because it is an important
1142 // security feature that we don't want people customizing the platform
1143 // to accidentally lose.
Mike Lockwoodff2544c2010-06-28 09:17:50 -04001144 private void updateAdbNotification(boolean adbEnabled) {
1145 if (adbEnabled) {
Mike Lockwooded760372009-07-09 07:07:27 -04001146 if ("0".equals(SystemProperties.get("persist.adb.notify"))) {
1147 return;
1148 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001149 if (!mAdbNotificationShown) {
1150 NotificationManager notificationManager = (NotificationManager) mContext
1151 .getSystemService(Context.NOTIFICATION_SERVICE);
1152 if (notificationManager != null) {
1153 Resources r = mContext.getResources();
1154 CharSequence title = r.getText(
1155 com.android.internal.R.string.adb_active_notification_title);
1156 CharSequence message = r.getText(
1157 com.android.internal.R.string.adb_active_notification_message);
1158
1159 if (mAdbNotification == null) {
1160 mAdbNotification = new Notification();
Daniel Sandler39576c82010-03-25 16:02:33 -04001161 mAdbNotification.icon = com.android.internal.R.drawable.stat_sys_adb;
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001162 mAdbNotification.when = 0;
1163 mAdbNotification.flags = Notification.FLAG_ONGOING_EVENT;
1164 mAdbNotification.tickerText = title;
Daniel Sandler39576c82010-03-25 16:02:33 -04001165 mAdbNotification.defaults = 0; // please be quiet
1166 mAdbNotification.sound = null;
1167 mAdbNotification.vibrate = null;
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001168 }
1169
1170 Intent intent = new Intent(
1171 Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS);
1172 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1173 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1174 // Note: we are hard-coding the component because this is
1175 // an important security UI that we don't want anyone
1176 // intercepting.
1177 intent.setComponent(new ComponentName("com.android.settings",
1178 "com.android.settings.DevelopmentSettings"));
1179 PendingIntent pi = PendingIntent.getActivity(mContext, 0,
1180 intent, 0);
1181
1182 mAdbNotification.setLatestEventInfo(mContext, title, message, pi);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001183
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001184 mAdbNotificationShown = true;
1185 notificationManager.notify(
1186 com.android.internal.R.string.adb_active_notification_title,
1187 mAdbNotification);
1188 }
1189 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001190
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001191 } else if (mAdbNotificationShown) {
1192 NotificationManager notificationManager = (NotificationManager) mContext
1193 .getSystemService(Context.NOTIFICATION_SERVICE);
1194 if (notificationManager != null) {
1195 mAdbNotificationShown = false;
1196 notificationManager.cancel(
1197 com.android.internal.R.string.adb_active_notification_title);
1198 }
1199 }
1200 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001201
1202 private void updateNotificationPulse() {
1203 synchronized (mNotificationList) {
1204 updateLightsLocked();
1205 }
1206 }
1207
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001208 // ======================================================================
1209 @Override
1210 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1211 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1212 != PackageManager.PERMISSION_GRANTED) {
1213 pw.println("Permission Denial: can't dump NotificationManager from from pid="
1214 + Binder.getCallingPid()
1215 + ", uid=" + Binder.getCallingUid());
1216 return;
1217 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001218
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 pw.println("Current Notification Manager state:");
1220
1221 int N;
1222
1223 synchronized (mToastQueue) {
1224 N = mToastQueue.size();
1225 if (N > 0) {
1226 pw.println(" Toast Queue:");
1227 for (int i=0; i<N; i++) {
1228 mToastQueue.get(i).dump(pw, " ");
1229 }
1230 pw.println(" ");
1231 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001232
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001233 }
1234
1235 synchronized (mNotificationList) {
1236 N = mNotificationList.size();
1237 if (N > 0) {
1238 pw.println(" Notification List:");
1239 for (int i=0; i<N; i++) {
1240 mNotificationList.get(i).dump(pw, " ", mContext);
1241 }
1242 pw.println(" ");
1243 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001244
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 N = mLights.size();
1246 if (N > 0) {
1247 pw.println(" Lights List:");
1248 for (int i=0; i<N; i++) {
1249 mLights.get(i).dump(pw, " ", mContext);
1250 }
1251 pw.println(" ");
1252 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001253
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 pw.println(" mSoundNotification=" + mSoundNotification);
1255 pw.println(" mSound=" + mSound);
1256 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001257 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
1258 pw.println(" mSystemReady=" + mSystemReady);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001259 }
1260 }
1261}