blob: ac3b23bb86c00779617e131d8e6732069e102a47 [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
73 // message codes
74 private static final int MESSAGE_TIMEOUT = 2;
75
76 private static final int LONG_DELAY = 3500; // 3.5 seconds
77 private static final int SHORT_DELAY = 2000; // 2 seconds
Doug Zongkerab5c49c2009-12-04 10:31:43 -080078
79 private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080
81 private static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION;
82
83 final Context mContext;
84 final IActivityManager mAm;
85 final IBinder mForegroundToken = new Binder();
86
87 private WorkerHandler mHandler;
Joe Onorato089de882010-04-12 08:18:45 -070088 private StatusBarManagerService mStatusBar;
Mike Lockwood3a322132009-11-24 00:30:52 -050089 private LightsService mLightsService;
Mike Lockwood3cb67a32009-11-27 14:25:58 -050090 private LightsService.Light mBatteryLight;
91 private LightsService.Light mNotificationLight;
92 private LightsService.Light mAttentionLight;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093
Mike Lockwood670f9322010-01-20 12:13:36 -050094 private int mDefaultNotificationColor;
95 private int mDefaultNotificationLedOn;
96 private int mDefaultNotificationLedOff;
97
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 private NotificationRecord mSoundNotification;
Jean-Michel Trivi211957f2010-03-26 18:19:33 -070099 private NotificationPlayer mSound;
Joe Onorato30275482009-07-08 17:09:14 -0700100 private boolean mSystemReady;
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400101 private int mDisabledNotifications;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102
103 private NotificationRecord mVibrateNotification;
104 private Vibrator mVibrator = new Vibrator();
105
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500106 // for enabling and disabling notification pulse behavior
107 private boolean mScreenOn = true;
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500108 private boolean mInCall = false;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500109 private boolean mNotificationPulseEnabled;
110
111 // for adb connected notifications
Mike Lockwoodea8b7d52009-08-04 17:03:15 -0400112 private boolean mUsbConnected;
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700113 private boolean mAdbEnabled = false;
114 private boolean mAdbNotificationShown = false;
115 private Notification mAdbNotification;
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800116
Fred Quintana6ecaff12009-09-25 14:23:13 -0700117 private final ArrayList<NotificationRecord> mNotificationList =
118 new ArrayList<NotificationRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119
120 private ArrayList<ToastRecord> mToastQueue;
121
122 private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>();
123
124 private boolean mBatteryCharging;
125 private boolean mBatteryLow;
126 private boolean mBatteryFull;
127 private NotificationRecord mLedNotification;
svetoslavganov75986cf2009-05-14 22:28:01 -0700128
The Android Open Source Project10592532009-03-18 17:39:46 -0700129 private static final int BATTERY_LOW_ARGB = 0xFFFF0000; // Charging Low - red solid on
130 private static final int BATTERY_MEDIUM_ARGB = 0xFFFFFF00; // Charging - orange solid on
131 private static final int BATTERY_FULL_ARGB = 0xFF00FF00; // Charging Full - green solid on
132 private static final int BATTERY_BLINK_ON = 125;
133 private static final int BATTERY_BLINK_OFF = 2875;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 private static String idDebugString(Context baseContext, String packageName, int id) {
136 Context c = null;
137
138 if (packageName != null) {
139 try {
140 c = baseContext.createPackageContext(packageName, 0);
141 } catch (NameNotFoundException e) {
142 c = baseContext;
143 }
144 } else {
145 c = baseContext;
146 }
147
148 String pkg;
149 String type;
150 String name;
151
152 Resources r = c.getResources();
153 try {
154 return r.getResourceName(id);
155 } catch (Resources.NotFoundException e) {
156 return "<name unknown>";
157 }
158 }
159
160 private static final class NotificationRecord
161 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700162 final String pkg;
163 final String tag;
164 final int id;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 ITransientNotification callback;
166 int duration;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700167 final Notification notification;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 IBinder statusBarKey;
169
Fred Quintana6ecaff12009-09-25 14:23:13 -0700170 NotificationRecord(String pkg, String tag, int id, Notification notification)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171 {
172 this.pkg = pkg;
Fred Quintana6ecaff12009-09-25 14:23:13 -0700173 this.tag = tag;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 this.id = id;
175 this.notification = notification;
176 }
Fred Quintana6ecaff12009-09-25 14:23:13 -0700177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 void dump(PrintWriter pw, String prefix, Context baseContext) {
179 pw.println(prefix + this);
180 pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon)
181 + " / " + idDebugString(baseContext, this.pkg, notification.icon));
182 pw.println(prefix + " contentIntent=" + notification.contentIntent);
183 pw.println(prefix + " deleteIntent=" + notification.deleteIntent);
184 pw.println(prefix + " tickerText=" + notification.tickerText);
185 pw.println(prefix + " contentView=" + notification.contentView);
186 pw.println(prefix + " defaults=0x" + Integer.toHexString(notification.defaults));
187 pw.println(prefix + " flags=0x" + Integer.toHexString(notification.flags));
188 pw.println(prefix + " sound=" + notification.sound);
189 pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate));
190 pw.println(prefix + " ledARGB=0x" + Integer.toHexString(notification.ledARGB)
191 + " ledOnMS=" + notification.ledOnMS
192 + " ledOffMS=" + notification.ledOffMS);
193 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 @Override
196 public final String toString()
197 {
198 return "NotificationRecord{"
199 + Integer.toHexString(System.identityHashCode(this))
200 + " pkg=" + pkg
Fred Quintana6ecaff12009-09-25 14:23:13 -0700201 + " id=" + Integer.toHexString(id)
202 + " tag=" + tag + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 }
204 }
205
206 private static final class ToastRecord
207 {
208 final int pid;
209 final String pkg;
210 final ITransientNotification callback;
211 int duration;
212
213 ToastRecord(int pid, String pkg, ITransientNotification callback, int duration)
214 {
215 this.pid = pid;
216 this.pkg = pkg;
217 this.callback = callback;
218 this.duration = duration;
219 }
220
221 void update(int duration) {
222 this.duration = duration;
223 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800224
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 void dump(PrintWriter pw, String prefix) {
226 pw.println(prefix + this);
227 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800228
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 @Override
230 public final String toString()
231 {
232 return "ToastRecord{"
233 + Integer.toHexString(System.identityHashCode(this))
234 + " pkg=" + pkg
235 + " callback=" + callback
236 + " duration=" + duration;
237 }
238 }
239
Joe Onorato089de882010-04-12 08:18:45 -0700240 private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks
241 = new StatusBarManagerService.NotificationCallbacks() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242
243 public void onSetDisabled(int status) {
244 synchronized (mNotificationList) {
245 mDisabledNotifications = status;
246 if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) {
247 // cancel whatever's going on
248 long identity = Binder.clearCallingIdentity();
249 try {
250 mSound.stop();
251 }
252 finally {
253 Binder.restoreCallingIdentity(identity);
254 }
255
256 identity = Binder.clearCallingIdentity();
257 try {
258 mVibrator.cancel();
259 }
260 finally {
261 Binder.restoreCallingIdentity(identity);
262 }
263 }
264 }
265 }
266
267 public void onClearAll() {
268 cancelAll();
269 }
270
Fred Quintana6ecaff12009-09-25 14:23:13 -0700271 public void onNotificationClick(String pkg, String tag, int id) {
272 cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700273 Notification.FLAG_FOREGROUND_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 }
275
276 public void onPanelRevealed() {
277 synchronized (mNotificationList) {
278 // sound
279 mSoundNotification = null;
280 long identity = Binder.clearCallingIdentity();
281 try {
282 mSound.stop();
283 }
284 finally {
285 Binder.restoreCallingIdentity(identity);
286 }
287
288 // vibrate
289 mVibrateNotification = null;
290 identity = Binder.clearCallingIdentity();
291 try {
292 mVibrator.cancel();
293 }
294 finally {
295 Binder.restoreCallingIdentity(identity);
296 }
297
298 // light
299 mLights.clear();
300 mLedNotification = null;
301 updateLightsLocked();
302 }
303 }
304 };
305
306 private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
307 @Override
308 public void onReceive(Context context, Intent intent) {
309 String action = intent.getAction();
310
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800311 boolean queryRestart = false;
312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
314 boolean batteryCharging = (intent.getIntExtra("plugged", 0) != 0);
315 int level = intent.getIntExtra("level", -1);
316 boolean batteryLow = (level >= 0 && level <= Power.LOW_BATTERY_THRESHOLD);
317 int status = intent.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
318 boolean batteryFull = (status == BatteryManager.BATTERY_STATUS_FULL || level >= 90);
319
320 if (batteryCharging != mBatteryCharging ||
321 batteryLow != mBatteryLow ||
322 batteryFull != mBatteryFull) {
323 mBatteryCharging = batteryCharging;
324 mBatteryLow = batteryLow;
325 mBatteryFull = batteryFull;
326 updateLights();
327 }
Mike Lockwoodea8b7d52009-08-04 17:03:15 -0400328 } else if (action.equals(Intent.ACTION_UMS_CONNECTED)) {
329 mUsbConnected = true;
330 updateAdbNotification();
331 } else if (action.equals(Intent.ACTION_UMS_DISCONNECTED)) {
332 mUsbConnected = false;
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700333 updateAdbNotification();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800335 || action.equals(Intent.ACTION_PACKAGE_RESTARTED)
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800336 || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART))
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800337 || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800338 String pkgList[] = null;
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800339 if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) {
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800340 pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800341 } else if (queryRestart) {
342 pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800343 } else {
344 Uri uri = intent.getData();
345 if (uri == null) {
346 return;
347 }
348 String pkgName = uri.getSchemeSpecificPart();
349 if (pkgName == null) {
350 return;
351 }
352 pkgList = new String[]{pkgName};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800353 }
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800354 if (pkgList != null && (pkgList.length > 0)) {
355 for (String pkgName : pkgList) {
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800356 cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800357 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500359 } else if (action.equals(Intent.ACTION_SCREEN_ON)) {
360 mScreenOn = true;
361 updateNotificationPulse();
362 } else if (action.equals(Intent.ACTION_SCREEN_OFF)) {
363 mScreenOn = false;
364 updateNotificationPulse();
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500365 } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
366 mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals(TelephonyManager.EXTRA_STATE_OFFHOOK));
367 updateNotificationPulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 }
369 }
370 };
371
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700372 class SettingsObserver extends ContentObserver {
373 SettingsObserver(Handler handler) {
374 super(handler);
375 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800376
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700377 void observe() {
378 ContentResolver resolver = mContext.getContentResolver();
379 resolver.registerContentObserver(Settings.Secure.getUriFor(
380 Settings.Secure.ADB_ENABLED), false, this);
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500381 resolver.registerContentObserver(Settings.System.getUriFor(
382 Settings.System.NOTIFICATION_LIGHT_PULSE), false, this);
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700383 update();
384 }
385
386 @Override public void onChange(boolean selfChange) {
387 update();
388 }
389
390 public void update() {
391 ContentResolver resolver = mContext.getContentResolver();
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500392 boolean adbEnabled = Settings.Secure.getInt(resolver,
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700393 Settings.Secure.ADB_ENABLED, 0) != 0;
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500394 if (mAdbEnabled != adbEnabled) {
395 mAdbEnabled = adbEnabled;
396 updateAdbNotification();
397 }
398 boolean pulseEnabled = Settings.System.getInt(resolver,
399 Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0;
400 if (mNotificationPulseEnabled != pulseEnabled) {
401 mNotificationPulseEnabled = pulseEnabled;
402 updateNotificationPulse();
403 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -0700404 }
405 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500406
Joe Onorato089de882010-04-12 08:18:45 -0700407 NotificationManagerService(Context context, StatusBarManagerService statusBar,
Mike Lockwood3a322132009-11-24 00:30:52 -0500408 LightsService lights)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 {
410 super();
411 mContext = context;
Mike Lockwood3a322132009-11-24 00:30:52 -0500412 mLightsService = lights;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 mAm = ActivityManagerNative.getDefault();
Jean-Michel Trivi211957f2010-03-26 18:19:33 -0700414 mSound = new NotificationPlayer(TAG);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800415 mSound.setUsesWakeLock(context);
416 mToastQueue = new ArrayList<ToastRecord>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800417 mHandler = new WorkerHandler();
San Mehat3ee13172010-02-04 20:54:43 -0800418
Joe Onorato089de882010-04-12 08:18:45 -0700419 mStatusBar = statusBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 statusBar.setNotificationCallbacks(mNotificationCallbacks);
421
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500422 mBatteryLight = lights.getLight(LightsService.LIGHT_ID_BATTERY);
423 mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS);
424 mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION);
425
Mike Lockwood670f9322010-01-20 12:13:36 -0500426 Resources resources = mContext.getResources();
427 mDefaultNotificationColor = resources.getColor(
428 com.android.internal.R.color.config_defaultNotificationColor);
429 mDefaultNotificationLedOn = resources.getInteger(
430 com.android.internal.R.integer.config_defaultNotificationLedOn);
431 mDefaultNotificationLedOff = resources.getInteger(
432 com.android.internal.R.integer.config_defaultNotificationLedOff);
433
Joe Onorato39f5b6a2009-07-23 12:29:19 -0400434 // Don't start allowing notifications until the setup wizard has run once.
435 // After that, including subsequent boots, init with notifications turned on.
436 // This works on the first boot because the setup wizard will toggle this
437 // flag at least once and we'll go back to 0 after that.
438 if (0 == Settings.Secure.getInt(mContext.getContentResolver(),
439 Settings.Secure.DEVICE_PROVISIONED, 0)) {
440 mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS;
441 }
442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 // register for battery changed notifications
444 IntentFilter filter = new IntentFilter();
445 filter.addAction(Intent.ACTION_BATTERY_CHANGED);
Mike Lockwoodea8b7d52009-08-04 17:03:15 -0400446 filter.addAction(Intent.ACTION_UMS_CONNECTED);
447 filter.addAction(Intent.ACTION_UMS_DISCONNECTED);
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500448 filter.addAction(Intent.ACTION_SCREEN_ON);
449 filter.addAction(Intent.ACTION_SCREEN_OFF);
Daniel Sandlere96ffb12010-03-11 13:38:06 -0500450 filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 mContext.registerReceiver(mIntentReceiver, filter);
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800452 IntentFilter pkgFilter = new IntentFilter();
453 pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
454 pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED);
455 pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART);
456 pkgFilter.addDataScheme("package");
457 mContext.registerReceiver(mIntentReceiver, pkgFilter);
Suchi Amalapurapub56ae202010-02-04 22:51:07 -0800458 IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
Suchi Amalapurapu08675a32010-01-28 09:57:30 -0800459 mContext.registerReceiver(mIntentReceiver, sdFilter);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800460
Mike Lockwoodc22404a2009-12-02 11:15:02 -0500461 SettingsObserver observer = new SettingsObserver(mHandler);
462 observer.observe();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800463 }
464
Joe Onorato30275482009-07-08 17:09:14 -0700465 void systemReady() {
466 // no beeping until we're basically done booting
467 mSystemReady = true;
468 }
469
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 // Toasts
471 // ============================================================================
472 public void enqueueToast(String pkg, ITransientNotification callback, int duration)
473 {
Daniel Sandlera7035902010-03-30 15:45:31 -0400474 if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800475
476 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800477 Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800478 return ;
479 }
480
481 synchronized (mToastQueue) {
482 int callingPid = Binder.getCallingPid();
483 long callingId = Binder.clearCallingIdentity();
484 try {
485 ToastRecord record;
486 int index = indexOfToastLocked(pkg, callback);
487 // If it's already in the queue, we update it in place, we don't
488 // move it to the end of the queue.
489 if (index >= 0) {
490 record = mToastQueue.get(index);
491 record.update(duration);
492 } else {
493 record = new ToastRecord(callingPid, pkg, callback, duration);
494 mToastQueue.add(record);
495 index = mToastQueue.size() - 1;
496 keepProcessAliveLocked(callingPid);
497 }
498 // If it's at index 0, it's the current toast. It doesn't matter if it's
499 // new or just been updated. Call back and tell it to show itself.
500 // If the callback fails, this will remove it from the list, so don't
501 // assume that it's valid after this.
502 if (index == 0) {
503 showNextToastLocked();
504 }
505 } finally {
506 Binder.restoreCallingIdentity(callingId);
507 }
508 }
509 }
510
511 public void cancelToast(String pkg, ITransientNotification callback) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800512 Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513
514 if (pkg == null || callback == null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800515 Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 return ;
517 }
518
519 synchronized (mToastQueue) {
520 long callingId = Binder.clearCallingIdentity();
521 try {
522 int index = indexOfToastLocked(pkg, callback);
523 if (index >= 0) {
524 cancelToastLocked(index);
525 } else {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800526 Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 }
528 } finally {
529 Binder.restoreCallingIdentity(callingId);
530 }
531 }
532 }
533
534 private void showNextToastLocked() {
535 ToastRecord record = mToastQueue.get(0);
536 while (record != null) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800537 if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800538 try {
539 record.callback.show();
540 scheduleTimeoutLocked(record, false);
541 return;
542 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800543 Slog.w(TAG, "Object died trying to show notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 + " in package " + record.pkg);
545 // remove it from the list and let the process die
546 int index = mToastQueue.indexOf(record);
547 if (index >= 0) {
548 mToastQueue.remove(index);
549 }
550 keepProcessAliveLocked(record.pid);
551 if (mToastQueue.size() > 0) {
552 record = mToastQueue.get(0);
553 } else {
554 record = null;
555 }
556 }
557 }
558 }
559
560 private void cancelToastLocked(int index) {
561 ToastRecord record = mToastQueue.get(index);
562 try {
563 record.callback.hide();
564 } catch (RemoteException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800565 Slog.w(TAG, "Object died trying to hide notification " + record.callback
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 + " in package " + record.pkg);
567 // don't worry about this, we're about to remove it from
568 // the list anyway
569 }
570 mToastQueue.remove(index);
571 keepProcessAliveLocked(record.pid);
572 if (mToastQueue.size() > 0) {
573 // Show the next one. If the callback fails, this will remove
574 // it from the list, so don't assume that the list hasn't changed
575 // after this point.
576 showNextToastLocked();
577 }
578 }
579
580 private void scheduleTimeoutLocked(ToastRecord r, boolean immediate)
581 {
582 Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r);
583 long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY);
584 mHandler.removeCallbacksAndMessages(r);
585 mHandler.sendMessageDelayed(m, delay);
586 }
587
588 private void handleTimeout(ToastRecord record)
589 {
Joe Onorato8a9b2202010-02-26 18:56:32 -0800590 if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800591 synchronized (mToastQueue) {
592 int index = indexOfToastLocked(record.pkg, record.callback);
593 if (index >= 0) {
594 cancelToastLocked(index);
595 }
596 }
597 }
598
599 // lock on mToastQueue
600 private int indexOfToastLocked(String pkg, ITransientNotification callback)
601 {
602 IBinder cbak = callback.asBinder();
603 ArrayList<ToastRecord> list = mToastQueue;
604 int len = list.size();
605 for (int i=0; i<len; i++) {
606 ToastRecord r = list.get(i);
607 if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) {
608 return i;
609 }
610 }
611 return -1;
612 }
613
614 // lock on mToastQueue
615 private void keepProcessAliveLocked(int pid)
616 {
617 int toastCount = 0; // toasts from this pid
618 ArrayList<ToastRecord> list = mToastQueue;
619 int N = list.size();
620 for (int i=0; i<N; i++) {
621 ToastRecord r = list.get(i);
622 if (r.pid == pid) {
623 toastCount++;
624 }
625 }
626 try {
627 mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0);
628 } catch (RemoteException e) {
629 // Shouldn't happen.
630 }
631 }
632
633 private final class WorkerHandler extends Handler
634 {
635 @Override
636 public void handleMessage(Message msg)
637 {
638 switch (msg.what)
639 {
640 case MESSAGE_TIMEOUT:
641 handleTimeout((ToastRecord)msg.obj);
642 break;
643 }
644 }
645 }
646
647
648 // Notifications
649 // ============================================================================
650 public void enqueueNotification(String pkg, int id, Notification notification, int[] idOut)
651 {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700652 enqueueNotificationWithTag(pkg, null /* tag */, id, notification, idOut);
653 }
654
655 public void enqueueNotificationWithTag(String pkg, String tag, int id,
656 Notification notification, int[] idOut)
657 {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700658 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800659
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 // This conditional is a dirty hack to limit the logging done on
661 // behalf of the download manager without affecting other apps.
662 if (!pkg.equals("com.android.providers.downloads")
663 || Log.isLoggable("DownloadManager", Log.VERBOSE)) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800664 EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, notification.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800665 }
666
667 if (pkg == null || notification == null) {
668 throw new IllegalArgumentException("null not allowed: pkg=" + pkg
669 + " id=" + id + " notification=" + notification);
670 }
671 if (notification.icon != 0) {
672 if (notification.contentView == null) {
673 throw new IllegalArgumentException("contentView required: pkg=" + pkg
674 + " id=" + id + " notification=" + notification);
675 }
676 if (notification.contentIntent == null) {
677 throw new IllegalArgumentException("contentIntent required: pkg=" + pkg
678 + " id=" + id + " notification=" + notification);
679 }
680 }
681
682 synchronized (mNotificationList) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700683 NotificationRecord r = new NotificationRecord(pkg, tag, id, notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 NotificationRecord old = null;
685
Fred Quintana6ecaff12009-09-25 14:23:13 -0700686 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800687 if (index < 0) {
688 mNotificationList.add(r);
689 } else {
690 old = mNotificationList.remove(index);
691 mNotificationList.add(index, r);
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700692 // Make sure we don't lose the foreground service state.
693 if (old != null) {
694 notification.flags |=
695 old.notification.flags&Notification.FLAG_FOREGROUND_SERVICE;
696 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800697 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800698
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700699 // Ensure if this is a foreground service that the proper additional
700 // flags are set.
701 if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) {
702 notification.flags |= Notification.FLAG_ONGOING_EVENT
703 | Notification.FLAG_NO_CLEAR;
704 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800705
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800706 if (notification.icon != 0) {
Joe Onorato18e69df2010-05-17 22:26:12 -0700707 StatusBarNotification n = new StatusBarNotification(pkg, id, tag, notification);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 if (old != null && old.statusBarKey != null) {
709 r.statusBarKey = old.statusBarKey;
710 long identity = Binder.clearCallingIdentity();
711 try {
Joe Onorato18e69df2010-05-17 22:26:12 -0700712 mStatusBar.updateNotification(r.statusBarKey, n);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 }
714 finally {
715 Binder.restoreCallingIdentity(identity);
716 }
717 } else {
718 long identity = Binder.clearCallingIdentity();
719 try {
Joe Onorato18e69df2010-05-17 22:26:12 -0700720 r.statusBarKey = mStatusBar.addNotification(n);
Mike Lockwood3cb67a32009-11-27 14:25:58 -0500721 mAttentionLight.pulse();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800722 }
723 finally {
724 Binder.restoreCallingIdentity(identity);
725 }
726 }
Joe Onorato30275482009-07-08 17:09:14 -0700727 sendAccessibilityEvent(notification, pkg);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800728 } else {
729 if (old != null && old.statusBarKey != null) {
730 long identity = Binder.clearCallingIdentity();
731 try {
Joe Onorato0cbda992010-05-02 16:28:15 -0700732 mStatusBar.removeNotification(old.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733 }
734 finally {
735 Binder.restoreCallingIdentity(identity);
736 }
737 }
738 }
739
740 // If we're not supposed to beep, vibrate, etc. then don't.
741 if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0)
742 && (!(old != null
Joe Onorato30275482009-07-08 17:09:14 -0700743 && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 ))
744 && mSystemReady) {
Eric Laurent524dc042009-11-27 05:07:55 -0800745
746 final AudioManager audioManager = (AudioManager) mContext
747 .getSystemService(Context.AUDIO_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 // sound
749 final boolean useDefaultSound =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800750 (notification.defaults & Notification.DEFAULT_SOUND) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 if (useDefaultSound || notification.sound != null) {
752 Uri uri;
753 if (useDefaultSound) {
754 uri = Settings.System.DEFAULT_NOTIFICATION_URI;
755 } else {
756 uri = notification.sound;
757 }
758 boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
759 int audioStreamType;
760 if (notification.audioStreamType >= 0) {
761 audioStreamType = notification.audioStreamType;
762 } else {
763 audioStreamType = DEFAULT_STREAM_TYPE;
764 }
765 mSoundNotification = r;
Eric Laurent524dc042009-11-27 05:07:55 -0800766 // do not play notifications if stream volume is 0
767 // (typically because ringer mode is silent).
768 if (audioManager.getStreamVolume(audioStreamType) != 0) {
769 long identity = Binder.clearCallingIdentity();
770 try {
771 mSound.play(mContext, uri, looping, audioStreamType);
772 }
773 finally {
774 Binder.restoreCallingIdentity(identity);
775 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800776 }
777 }
778
779 // vibrate
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780 final boolean useDefaultVibrate =
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800781 (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800782 if ((useDefaultVibrate || notification.vibrate != null)
783 && audioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_NOTIFICATION)) {
784 mVibrateNotification = r;
785
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800786 mVibrator.vibrate(useDefaultVibrate ? DEFAULT_VIBRATE_PATTERN
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787 : notification.vibrate,
788 ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1);
789 }
790 }
791
792 // this option doesn't shut off the lights
793
794 // light
795 // the most recent thing gets the light
796 mLights.remove(old);
797 if (mLedNotification == old) {
798 mLedNotification = null;
799 }
Joe Onorato8a9b2202010-02-26 18:56:32 -0800800 //Slog.i(TAG, "notification.lights="
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0));
802 if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) {
803 mLights.add(r);
804 updateLightsLocked();
805 } else {
806 if (old != null
807 && ((old.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0)) {
808 updateLightsLocked();
809 }
810 }
811 }
812
813 idOut[0] = id;
814 }
815
Joe Onorato30275482009-07-08 17:09:14 -0700816 private void sendAccessibilityEvent(Notification notification, CharSequence packageName) {
svetoslavganov75986cf2009-05-14 22:28:01 -0700817 AccessibilityManager manager = AccessibilityManager.getInstance(mContext);
818 if (!manager.isEnabled()) {
819 return;
820 }
821
822 AccessibilityEvent event =
823 AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED);
824 event.setPackageName(packageName);
825 event.setClassName(Notification.class.getName());
826 event.setParcelableData(notification);
827 CharSequence tickerText = notification.tickerText;
828 if (!TextUtils.isEmpty(tickerText)) {
829 event.getText().add(tickerText);
830 }
831
832 manager.sendAccessibilityEvent(event);
833 }
834
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800835 private void cancelNotificationLocked(NotificationRecord r) {
836 // status bar
837 if (r.notification.icon != 0) {
838 long identity = Binder.clearCallingIdentity();
839 try {
Joe Onorato0cbda992010-05-02 16:28:15 -0700840 mStatusBar.removeNotification(r.statusBarKey);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800841 }
842 finally {
843 Binder.restoreCallingIdentity(identity);
844 }
845 r.statusBarKey = null;
846 }
847
848 // sound
849 if (mSoundNotification == r) {
850 mSoundNotification = null;
851 long identity = Binder.clearCallingIdentity();
852 try {
853 mSound.stop();
854 }
855 finally {
856 Binder.restoreCallingIdentity(identity);
857 }
858 }
859
860 // vibrate
861 if (mVibrateNotification == r) {
862 mVibrateNotification = null;
863 long identity = Binder.clearCallingIdentity();
864 try {
865 mVibrator.cancel();
866 }
867 finally {
868 Binder.restoreCallingIdentity(identity);
869 }
870 }
871
872 // light
873 mLights.remove(r);
874 if (mLedNotification == r) {
875 mLedNotification = null;
876 }
877 }
878
879 /**
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700880 * Cancels a notification ONLY if it has all of the {@code mustHaveFlags}
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800881 * and none of the {@code mustNotHaveFlags}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 */
Fred Quintana6ecaff12009-09-25 14:23:13 -0700883 private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700884 int mustNotHaveFlags) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800885 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886
887 synchronized (mNotificationList) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700888 int index = indexOfNotificationLocked(pkg, tag, id);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800889 if (index >= 0) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700890 NotificationRecord r = mNotificationList.get(index);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800891
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
893 return;
894 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700895 if ((r.notification.flags & mustNotHaveFlags) != 0) {
896 return;
897 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800898
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 mNotificationList.remove(index);
900
901 cancelNotificationLocked(r);
902 updateLightsLocked();
903 }
904 }
905 }
906
907 /**
908 * Cancels all notifications from a given package that have all of the
909 * {@code mustHaveFlags}.
910 */
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800911 boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags,
912 int mustNotHaveFlags, boolean doit) {
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800913 EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, mustHaveFlags);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914
915 synchronized (mNotificationList) {
916 final int N = mNotificationList.size();
917 boolean canceledSomething = false;
918 for (int i = N-1; i >= 0; --i) {
919 NotificationRecord r = mNotificationList.get(i);
920 if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) {
921 continue;
922 }
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700923 if ((r.notification.flags & mustNotHaveFlags) != 0) {
924 continue;
925 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 if (!r.pkg.equals(pkg)) {
927 continue;
928 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800929 canceledSomething = true;
930 if (!doit) {
931 return true;
932 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 mNotificationList.remove(i);
934 cancelNotificationLocked(r);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 }
936 if (canceledSomething) {
937 updateLightsLocked();
938 }
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800939 return canceledSomething;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800940 }
941 }
942
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800943
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700944 public void cancelNotification(String pkg, int id) {
Fred Quintana6ecaff12009-09-25 14:23:13 -0700945 cancelNotificationWithTag(pkg, null /* tag */, id);
946 }
947
948 public void cancelNotificationWithTag(String pkg, String tag, int id) {
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700949 checkIncomingCall(pkg);
950 // Don't allow client applications to cancel foreground service notis.
Fred Quintana6ecaff12009-09-25 14:23:13 -0700951 cancelNotification(pkg, tag, id, 0,
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700952 Binder.getCallingUid() == Process.SYSTEM_UID
953 ? 0 : Notification.FLAG_FOREGROUND_SERVICE);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800954 }
955
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700956 public void cancelAllNotifications(String pkg) {
957 checkIncomingCall(pkg);
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800958
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700959 // Calling from user space, don't allow the canceling of actively
960 // running foreground services.
Dianne Hackborn21f1bd12010-02-19 17:02:21 -0800961 cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 }
963
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700964 void checkIncomingCall(String pkg) {
965 int uid = Binder.getCallingUid();
966 if (uid == Process.SYSTEM_UID || uid == 0) {
967 return;
968 }
969 try {
970 ApplicationInfo ai = mContext.getPackageManager().getApplicationInfo(
971 pkg, 0);
972 if (ai.uid != uid) {
973 throw new SecurityException("Calling uid " + uid + " gave package"
974 + pkg + " which is owned by uid " + ai.uid);
975 }
976 } catch (PackageManager.NameNotFoundException e) {
977 throw new SecurityException("Unknown package " + pkg);
978 }
979 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -0800980
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700981 void cancelAll() {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800982 synchronized (mNotificationList) {
983 final int N = mNotificationList.size();
984 for (int i=N-1; i>=0; i--) {
985 NotificationRecord r = mNotificationList.get(i);
986
987 if ((r.notification.flags & (Notification.FLAG_ONGOING_EVENT
988 | Notification.FLAG_NO_CLEAR)) == 0) {
989 if (r.notification.deleteIntent != null) {
990 try {
991 r.notification.deleteIntent.send();
992 } catch (PendingIntent.CanceledException ex) {
993 // do nothing - there's no relevant way to recover, and
994 // no reason to let this propagate
Joe Onorato8a9b2202010-02-26 18:56:32 -0800995 Slog.w(TAG, "canceled PendingIntent for " + r.pkg, ex);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800996 }
997 }
998 mNotificationList.remove(i);
999 cancelNotificationLocked(r);
1000 }
1001 }
1002
1003 updateLightsLocked();
1004 }
1005 }
1006
1007 private void updateLights() {
1008 synchronized (mNotificationList) {
1009 updateLightsLocked();
1010 }
1011 }
1012
1013 // lock on mNotificationList
1014 private void updateLightsLocked()
1015 {
The Android Open Source Project10592532009-03-18 17:39:46 -07001016 // Battery low always shows, other states only show if charging.
1017 if (mBatteryLow) {
Mike Lockwood445f4302009-09-04 11:06:46 -04001018 if (mBatteryCharging) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001019 mBatteryLight.setColor(BATTERY_LOW_ARGB);
Mike Lockwood445f4302009-09-04 11:06:46 -04001020 } else {
1021 // Flash when battery is low and not charging
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001022 mBatteryLight.setFlashing(BATTERY_LOW_ARGB, LightsService.LIGHT_FLASH_TIMED,
1023 BATTERY_BLINK_ON, BATTERY_BLINK_OFF);
Mike Lockwood445f4302009-09-04 11:06:46 -04001024 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001025 } else if (mBatteryCharging) {
The Android Open Source Project10592532009-03-18 17:39:46 -07001026 if (mBatteryFull) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001027 mBatteryLight.setColor(BATTERY_FULL_ARGB);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001028 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001029 mBatteryLight.setColor(BATTERY_MEDIUM_ARGB);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001030 }
1031 } else {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001032 mBatteryLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001033 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001034
The Android Open Source Project10592532009-03-18 17:39:46 -07001035 // handle notification lights
1036 if (mLedNotification == null) {
1037 // get next notification, if any
1038 int n = mLights.size();
1039 if (n > 0) {
1040 mLedNotification = mLights.get(n-1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 }
1042 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001043
1044 // we only flash if screen is off and persistent pulsing is enabled
Daniel Sandlere96ffb12010-03-11 13:38:06 -05001045 // and we are not currently in a call
1046 if (mLedNotification == null || mScreenOn || mInCall) {
Mike Lockwood3cb67a32009-11-27 14:25:58 -05001047 mNotificationLight.turnOff();
The Android Open Source Project10592532009-03-18 17:39:46 -07001048 } else {
Mike Lockwood670f9322010-01-20 12:13:36 -05001049 int ledARGB = mLedNotification.notification.ledARGB;
1050 int ledOnMS = mLedNotification.notification.ledOnMS;
1051 int ledOffMS = mLedNotification.notification.ledOffMS;
1052 if ((mLedNotification.notification.defaults & Notification.DEFAULT_LIGHTS) != 0) {
1053 ledARGB = mDefaultNotificationColor;
1054 ledOnMS = mDefaultNotificationLedOn;
1055 ledOffMS = mDefaultNotificationLedOff;
1056 }
1057 if (mNotificationPulseEnabled) {
1058 // pulse repeatedly
1059 mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED,
1060 ledOnMS, ledOffMS);
1061 } else {
1062 // pulse only once
1063 mNotificationLight.pulse(ledARGB, ledOnMS);
1064 }
The Android Open Source Project10592532009-03-18 17:39:46 -07001065 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 }
1067
1068 // lock on mNotificationList
Fred Quintana6ecaff12009-09-25 14:23:13 -07001069 private int indexOfNotificationLocked(String pkg, String tag, int id)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 {
1071 ArrayList<NotificationRecord> list = mNotificationList;
1072 final int len = list.size();
1073 for (int i=0; i<len; i++) {
1074 NotificationRecord r = list.get(i);
Fred Quintana6ecaff12009-09-25 14:23:13 -07001075 if (tag == null) {
1076 if (r.tag != null) {
1077 continue;
1078 }
1079 } else {
1080 if (!tag.equals(r.tag)) {
1081 continue;
1082 }
1083 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001084 if (r.id == id && r.pkg.equals(pkg)) {
1085 return i;
1086 }
1087 }
1088 return -1;
1089 }
1090
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001091 // This is here instead of StatusBarPolicy because it is an important
1092 // security feature that we don't want people customizing the platform
1093 // to accidentally lose.
1094 private void updateAdbNotification() {
Mike Lockwoodea8b7d52009-08-04 17:03:15 -04001095 if (mAdbEnabled && mUsbConnected) {
Mike Lockwooded760372009-07-09 07:07:27 -04001096 if ("0".equals(SystemProperties.get("persist.adb.notify"))) {
1097 return;
1098 }
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001099 if (!mAdbNotificationShown) {
1100 NotificationManager notificationManager = (NotificationManager) mContext
1101 .getSystemService(Context.NOTIFICATION_SERVICE);
1102 if (notificationManager != null) {
1103 Resources r = mContext.getResources();
1104 CharSequence title = r.getText(
1105 com.android.internal.R.string.adb_active_notification_title);
1106 CharSequence message = r.getText(
1107 com.android.internal.R.string.adb_active_notification_message);
1108
1109 if (mAdbNotification == null) {
1110 mAdbNotification = new Notification();
Daniel Sandler39576c82010-03-25 16:02:33 -04001111 mAdbNotification.icon = com.android.internal.R.drawable.stat_sys_adb;
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001112 mAdbNotification.when = 0;
1113 mAdbNotification.flags = Notification.FLAG_ONGOING_EVENT;
1114 mAdbNotification.tickerText = title;
Daniel Sandler39576c82010-03-25 16:02:33 -04001115 mAdbNotification.defaults = 0; // please be quiet
1116 mAdbNotification.sound = null;
1117 mAdbNotification.vibrate = null;
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001118 }
1119
1120 Intent intent = new Intent(
1121 Settings.ACTION_APPLICATION_DEVELOPMENT_SETTINGS);
1122 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1123 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
1124 // Note: we are hard-coding the component because this is
1125 // an important security UI that we don't want anyone
1126 // intercepting.
1127 intent.setComponent(new ComponentName("com.android.settings",
1128 "com.android.settings.DevelopmentSettings"));
1129 PendingIntent pi = PendingIntent.getActivity(mContext, 0,
1130 intent, 0);
1131
1132 mAdbNotification.setLatestEventInfo(mContext, title, message, pi);
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001133
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001134 mAdbNotificationShown = true;
1135 notificationManager.notify(
1136 com.android.internal.R.string.adb_active_notification_title,
1137 mAdbNotification);
1138 }
1139 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001140
Dianne Hackborn1dac2772009-06-26 18:16:48 -07001141 } else if (mAdbNotificationShown) {
1142 NotificationManager notificationManager = (NotificationManager) mContext
1143 .getSystemService(Context.NOTIFICATION_SERVICE);
1144 if (notificationManager != null) {
1145 mAdbNotificationShown = false;
1146 notificationManager.cancel(
1147 com.android.internal.R.string.adb_active_notification_title);
1148 }
1149 }
1150 }
Mike Lockwoodc22404a2009-12-02 11:15:02 -05001151
1152 private void updateNotificationPulse() {
1153 synchronized (mNotificationList) {
1154 updateLightsLocked();
1155 }
1156 }
1157
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 // ======================================================================
1159 @Override
1160 protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
1161 if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP)
1162 != PackageManager.PERMISSION_GRANTED) {
1163 pw.println("Permission Denial: can't dump NotificationManager from from pid="
1164 + Binder.getCallingPid()
1165 + ", uid=" + Binder.getCallingUid());
1166 return;
1167 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001168
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001169 pw.println("Current Notification Manager state:");
1170
1171 int N;
1172
1173 synchronized (mToastQueue) {
1174 N = mToastQueue.size();
1175 if (N > 0) {
1176 pw.println(" Toast Queue:");
1177 for (int i=0; i<N; i++) {
1178 mToastQueue.get(i).dump(pw, " ");
1179 }
1180 pw.println(" ");
1181 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001182
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001183 }
1184
1185 synchronized (mNotificationList) {
1186 N = mNotificationList.size();
1187 if (N > 0) {
1188 pw.println(" Notification List:");
1189 for (int i=0; i<N; i++) {
1190 mNotificationList.get(i).dump(pw, " ", mContext);
1191 }
1192 pw.println(" ");
1193 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 N = mLights.size();
1196 if (N > 0) {
1197 pw.println(" Lights List:");
1198 for (int i=0; i<N; i++) {
1199 mLights.get(i).dump(pw, " ", mContext);
1200 }
1201 pw.println(" ");
1202 }
Doug Zongkerab5c49c2009-12-04 10:31:43 -08001203
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 pw.println(" mSoundNotification=" + mSoundNotification);
1205 pw.println(" mSound=" + mSound);
1206 pw.println(" mVibrateNotification=" + mVibrateNotification);
Joe Onorato39f5b6a2009-07-23 12:29:19 -04001207 pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications));
1208 pw.println(" mSystemReady=" + mSystemReady);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001209 }
1210 }
1211}