| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
| Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 19 | import com.android.internal.statusbar.StatusBarNotification; |
| Joe Onorato | 7a0f36b | 2010-06-07 10:24:36 -0700 | [diff] [blame] | 20 | import com.android.server.StatusBarManagerService; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 21 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | import android.app.ActivityManagerNative; |
| 23 | import android.app.IActivityManager; |
| 24 | import android.app.INotificationManager; |
| 25 | import android.app.ITransientNotification; |
| 26 | import android.app.Notification; |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 27 | import android.app.NotificationManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.app.PendingIntent; |
| 29 | import android.app.StatusBarManager; |
| 30 | import android.content.BroadcastReceiver; |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 31 | import android.content.ComponentName; |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 32 | import android.content.ContentResolver; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.content.Context; |
| 34 | import android.content.Intent; |
| 35 | import android.content.IntentFilter; |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 36 | import android.content.pm.ApplicationInfo; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | import android.content.pm.PackageManager; |
| 38 | import android.content.pm.PackageManager.NameNotFoundException; |
| 39 | import android.content.res.Resources; |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 40 | import android.database.ContentObserver; |
| Mike Lockwood | ff2544c | 2010-06-28 09:17:50 -0400 | [diff] [blame] | 41 | import android.hardware.Usb; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 42 | import android.media.AudioManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import android.net.Uri; |
| 44 | import android.os.BatteryManager; |
| Mike Lockwood | ff2544c | 2010-06-28 09:17:50 -0400 | [diff] [blame] | 45 | import android.os.Bundle; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | import android.os.Binder; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | import android.os.Handler; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | import android.os.IBinder; |
| 49 | import android.os.Message; |
| 50 | import android.os.Power; |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 51 | import android.os.Process; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 52 | import android.os.RemoteException; |
| Mike Lockwood | ed76037 | 2009-07-09 07:07:27 -0400 | [diff] [blame] | 53 | import android.os.SystemProperties; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | import android.os.Vibrator; |
| 55 | import android.provider.Settings; |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 56 | import android.telephony.TelephonyManager; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 57 | import android.text.TextUtils; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | import android.util.EventLog; |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 59 | import android.util.Slog; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | import android.util.Log; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 61 | import android.view.accessibility.AccessibilityEvent; |
| 62 | import android.view.accessibility.AccessibilityManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | import android.widget.Toast; |
| 64 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | import java.io.FileDescriptor; |
| 66 | import java.io.PrintWriter; |
| 67 | import java.util.ArrayList; |
| 68 | import java.util.Arrays; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | |
| Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame^] | 70 | /** {@hide} */ |
| 71 | public class NotificationManagerService extends INotificationManager.Stub |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | { |
| 73 | private static final String TAG = "NotificationService"; |
| 74 | private static final boolean DBG = false; |
| 75 | |
| Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 76 | private static final int MAX_PACKAGE_NOTIFICATIONS = 50; |
| 77 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | // 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 Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 83 | |
| 84 | private static final long[] DEFAULT_VIBRATE_PATTERN = {0, 250, 250, 250}; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | |
| 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 Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 93 | private StatusBarManagerService mStatusBar; |
| Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 94 | private LightsService mLightsService; |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 95 | private LightsService.Light mBatteryLight; |
| 96 | private LightsService.Light mNotificationLight; |
| 97 | private LightsService.Light mAttentionLight; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | |
| Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 99 | private int mDefaultNotificationColor; |
| 100 | private int mDefaultNotificationLedOn; |
| 101 | private int mDefaultNotificationLedOff; |
| 102 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 103 | private NotificationRecord mSoundNotification; |
| Jean-Michel Trivi | 211957f | 2010-03-26 18:19:33 -0700 | [diff] [blame] | 104 | private NotificationPlayer mSound; |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 105 | private boolean mSystemReady; |
| Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 106 | private int mDisabledNotifications; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | |
| 108 | private NotificationRecord mVibrateNotification; |
| 109 | private Vibrator mVibrator = new Vibrator(); |
| 110 | |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 111 | // for enabling and disabling notification pulse behavior |
| 112 | private boolean mScreenOn = true; |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 113 | private boolean mInCall = false; |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 114 | private boolean mNotificationPulseEnabled; |
| 115 | |
| 116 | // for adb connected notifications |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 117 | private boolean mAdbNotificationShown = false; |
| 118 | private Notification mAdbNotification; |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 119 | |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 120 | private final ArrayList<NotificationRecord> mNotificationList = |
| 121 | new ArrayList<NotificationRecord>(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | |
| 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; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 131 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 132 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | 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 Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 165 | final String pkg; |
| 166 | final String tag; |
| 167 | final int id; |
| Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 168 | final int uid; |
| 169 | final int initialPid; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | ITransientNotification callback; |
| 171 | int duration; |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 172 | final Notification notification; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | IBinder statusBarKey; |
| 174 | |
| Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 175 | NotificationRecord(String pkg, String tag, int id, int uid, int initialPid, |
| 176 | Notification notification) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | { |
| 178 | this.pkg = pkg; |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 179 | this.tag = tag; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | this.id = id; |
| Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 181 | this.uid = uid; |
| 182 | this.initialPid = initialPid; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | this.notification = notification; |
| 184 | } |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 185 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | 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 Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 202 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | @Override |
| 204 | public final String toString() |
| 205 | { |
| 206 | return "NotificationRecord{" |
| 207 | + Integer.toHexString(System.identityHashCode(this)) |
| 208 | + " pkg=" + pkg |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 209 | + " id=" + Integer.toHexString(id) |
| 210 | + " tag=" + tag + "}"; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 211 | } |
| 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 Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 232 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 233 | void dump(PrintWriter pw, String prefix) { |
| 234 | pw.println(prefix + this); |
| 235 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 236 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | @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 Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 248 | private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks |
| 249 | = new StatusBarManagerService.NotificationCallbacks() { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 250 | |
| 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 Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 279 | public void onNotificationClick(String pkg, String tag, int id) { |
| 280 | cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL, |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 281 | Notification.FLAG_FOREGROUND_SERVICE); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | } |
| 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 Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 312 | |
| Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 313 | public void onNotificationError(String pkg, String tag, int id, |
| 314 | int uid, int initialPid, String message) { |
| Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame^] | 315 | Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id |
| 316 | + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")"); |
| Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 317 | cancelNotification(pkg, tag, id, 0, 0); |
| Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 318 | 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 Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 326 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | }; |
| 328 | |
| 329 | private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
| 330 | @Override |
| 331 | public void onReceive(Context context, Intent intent) { |
| 332 | String action = intent.getAction(); |
| 333 | |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 334 | boolean queryRestart = false; |
| 335 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 | 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 Lockwood | ff2544c | 2010-06-28 09:17:50 -0400 | [diff] [blame] | 351 | } 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 359 | } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED) |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 360 | || action.equals(Intent.ACTION_PACKAGE_RESTARTED) |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 361 | || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART)) |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 362 | || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 363 | String pkgList[] = null; |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 364 | if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 365 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 366 | } else if (queryRestart) { |
| 367 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 368 | } 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 378 | } |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 379 | if (pkgList != null && (pkgList.length > 0)) { |
| 380 | for (String pkgName : pkgList) { |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 381 | cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 382 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | } |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 384 | } 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 Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 390 | } 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 393 | } |
| 394 | } |
| 395 | }; |
| 396 | |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 397 | class SettingsObserver extends ContentObserver { |
| 398 | SettingsObserver(Handler handler) { |
| 399 | super(handler); |
| 400 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 401 | |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 402 | void observe() { |
| 403 | ContentResolver resolver = mContext.getContentResolver(); |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 404 | resolver.registerContentObserver(Settings.System.getUriFor( |
| 405 | Settings.System.NOTIFICATION_LIGHT_PULSE), false, this); |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 406 | update(); |
| 407 | } |
| 408 | |
| 409 | @Override public void onChange(boolean selfChange) { |
| 410 | update(); |
| 411 | } |
| 412 | |
| 413 | public void update() { |
| 414 | ContentResolver resolver = mContext.getContentResolver(); |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 415 | 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 Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 421 | } |
| 422 | } |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 423 | |
| Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 424 | NotificationManagerService(Context context, StatusBarManagerService statusBar, |
| Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 425 | LightsService lights) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 426 | { |
| 427 | super(); |
| 428 | mContext = context; |
| Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 429 | mLightsService = lights; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | mAm = ActivityManagerNative.getDefault(); |
| Jean-Michel Trivi | 211957f | 2010-03-26 18:19:33 -0700 | [diff] [blame] | 431 | mSound = new NotificationPlayer(TAG); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 432 | mSound.setUsesWakeLock(context); |
| 433 | mToastQueue = new ArrayList<ToastRecord>(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 434 | mHandler = new WorkerHandler(); |
| San Mehat | 3ee1317 | 2010-02-04 20:54:43 -0800 | [diff] [blame] | 435 | |
| Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 436 | mStatusBar = statusBar; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 437 | statusBar.setNotificationCallbacks(mNotificationCallbacks); |
| 438 | |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 439 | 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 Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 443 | 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 Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 451 | // 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 460 | // register for battery changed notifications |
| 461 | IntentFilter filter = new IntentFilter(); |
| 462 | filter.addAction(Intent.ACTION_BATTERY_CHANGED); |
| Mike Lockwood | ff2544c | 2010-06-28 09:17:50 -0400 | [diff] [blame] | 463 | filter.addAction(Usb.ACTION_USB_STATE); |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 464 | filter.addAction(Intent.ACTION_SCREEN_ON); |
| 465 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 466 | filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 467 | mContext.registerReceiver(mIntentReceiver, filter); |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 468 | 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 Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 474 | IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 475 | mContext.registerReceiver(mIntentReceiver, sdFilter); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 476 | |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 477 | SettingsObserver observer = new SettingsObserver(mHandler); |
| 478 | observer.observe(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 479 | } |
| 480 | |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 481 | void systemReady() { |
| 482 | // no beeping until we're basically done booting |
| 483 | mSystemReady = true; |
| 484 | } |
| 485 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 486 | // Toasts |
| 487 | // ============================================================================ |
| 488 | public void enqueueToast(String pkg, ITransientNotification callback, int duration) |
| 489 | { |
| Daniel Sandler | a703590 | 2010-03-30 15:45:31 -0400 | [diff] [blame] | 490 | if (DBG) Slog.i(TAG, "enqueueToast pkg=" + pkg + " callback=" + callback + " duration=" + duration); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 491 | |
| 492 | if (pkg == null || callback == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 493 | Slog.e(TAG, "Not doing toast. pkg=" + pkg + " callback=" + callback); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 | 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 Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 528 | Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 529 | |
| 530 | if (pkg == null || callback == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 531 | Slog.e(TAG, "Not cancelling notification. pkg=" + pkg + " callback=" + callback); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 532 | 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 Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 542 | Slog.w(TAG, "Toast already cancelled. pkg=" + pkg + " callback=" + callback); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 543 | } |
| 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 Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 553 | if (DBG) Slog.d(TAG, "Show pkg=" + record.pkg + " callback=" + record.callback); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 554 | try { |
| 555 | record.callback.show(); |
| 556 | scheduleTimeoutLocked(record, false); |
| 557 | return; |
| 558 | } catch (RemoteException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 559 | Slog.w(TAG, "Object died trying to show notification " + record.callback |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 560 | + " 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 Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 581 | Slog.w(TAG, "Object died trying to hide notification " + record.callback |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 582 | + " 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 Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 606 | if (DBG) Slog.d(TAG, "Timeout pkg=" + record.pkg + " callback=" + record.callback); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 607 | 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 Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 668 | enqueueNotificationWithTag(pkg, null /* tag */, id, notification, idOut); |
| 669 | } |
| 670 | |
| Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame^] | 671 | public void enqueueNotificationWithTag(String pkg, String tag, int id, Notification notification, |
| 672 | int[] idOut) |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 673 | { |
| Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame^] | 674 | 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 Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 685 | |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 686 | checkIncomingCall(pkg); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 687 | |
| Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 688 | // 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 708 | // 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 Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 712 | EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, notification.toString()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 713 | } |
| 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 Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 731 | NotificationRecord r = new NotificationRecord(pkg, tag, id, |
| 732 | callingUid, callingPid, notification); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 733 | NotificationRecord old = null; |
| 734 | |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 735 | int index = indexOfNotificationLocked(pkg, tag, id); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 736 | if (index < 0) { |
| 737 | mNotificationList.add(r); |
| 738 | } else { |
| 739 | old = mNotificationList.remove(index); |
| 740 | mNotificationList.add(index, r); |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 741 | // 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 746 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 747 | |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 748 | // 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 Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 754 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 755 | if (notification.icon != 0) { |
| Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 756 | StatusBarNotification n = new StatusBarNotification(pkg, id, tag, |
| 757 | r.uid, r.initialPid, notification); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 758 | if (old != null && old.statusBarKey != null) { |
| 759 | r.statusBarKey = old.statusBarKey; |
| 760 | long identity = Binder.clearCallingIdentity(); |
| 761 | try { |
| Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 762 | mStatusBar.updateNotification(r.statusBarKey, n); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 763 | } |
| 764 | finally { |
| 765 | Binder.restoreCallingIdentity(identity); |
| 766 | } |
| 767 | } else { |
| 768 | long identity = Binder.clearCallingIdentity(); |
| 769 | try { |
| Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 770 | r.statusBarKey = mStatusBar.addNotification(n); |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 771 | mAttentionLight.pulse(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 772 | } |
| 773 | finally { |
| 774 | Binder.restoreCallingIdentity(identity); |
| 775 | } |
| 776 | } |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 777 | sendAccessibilityEvent(notification, pkg); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 778 | } else { |
| 779 | if (old != null && old.statusBarKey != null) { |
| 780 | long identity = Binder.clearCallingIdentity(); |
| 781 | try { |
| Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 782 | mStatusBar.removeNotification(old.statusBarKey); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 783 | } |
| 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 Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 793 | && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 )) |
| 794 | && mSystemReady) { |
| Eric Laurent | 524dc04 | 2009-11-27 05:07:55 -0800 | [diff] [blame] | 795 | |
| 796 | final AudioManager audioManager = (AudioManager) mContext |
| 797 | .getSystemService(Context.AUDIO_SERVICE); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 798 | // sound |
| 799 | final boolean useDefaultSound = |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 800 | (notification.defaults & Notification.DEFAULT_SOUND) != 0; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 801 | 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 Laurent | 524dc04 | 2009-11-27 05:07:55 -0800 | [diff] [blame] | 816 | // 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 826 | } |
| 827 | } |
| 828 | |
| 829 | // vibrate |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 830 | final boolean useDefaultVibrate = |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 831 | (notification.defaults & Notification.DEFAULT_VIBRATE) != 0; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 832 | if ((useDefaultVibrate || notification.vibrate != null) |
| 833 | && audioManager.shouldVibrate(AudioManager.VIBRATE_TYPE_NOTIFICATION)) { |
| 834 | mVibrateNotification = r; |
| 835 | |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 836 | mVibrator.vibrate(useDefaultVibrate ? DEFAULT_VIBRATE_PATTERN |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 837 | : 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 Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 850 | //Slog.i(TAG, "notification.lights=" |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 851 | // + ((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 Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 866 | private void sendAccessibilityEvent(Notification notification, CharSequence packageName) { |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 867 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 885 | private void cancelNotificationLocked(NotificationRecord r) { |
| 886 | // status bar |
| 887 | if (r.notification.icon != 0) { |
| 888 | long identity = Binder.clearCallingIdentity(); |
| 889 | try { |
| Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 890 | mStatusBar.removeNotification(r.statusBarKey); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 891 | } |
| 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 Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 930 | * Cancels a notification ONLY if it has all of the {@code mustHaveFlags} |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 931 | * and none of the {@code mustNotHaveFlags}. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 932 | */ |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 933 | private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags, |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 934 | int mustNotHaveFlags) { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 935 | EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, mustHaveFlags); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 936 | |
| 937 | synchronized (mNotificationList) { |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 938 | int index = indexOfNotificationLocked(pkg, tag, id); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 939 | if (index >= 0) { |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 940 | NotificationRecord r = mNotificationList.get(index); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 941 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 942 | if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) { |
| 943 | return; |
| 944 | } |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 945 | if ((r.notification.flags & mustNotHaveFlags) != 0) { |
| 946 | return; |
| 947 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 948 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 949 | 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 Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 961 | boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags, |
| 962 | int mustNotHaveFlags, boolean doit) { |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 963 | EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, mustHaveFlags); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 964 | |
| 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 Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 973 | if ((r.notification.flags & mustNotHaveFlags) != 0) { |
| 974 | continue; |
| 975 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 976 | if (!r.pkg.equals(pkg)) { |
| 977 | continue; |
| 978 | } |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 979 | canceledSomething = true; |
| 980 | if (!doit) { |
| 981 | return true; |
| 982 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 983 | mNotificationList.remove(i); |
| 984 | cancelNotificationLocked(r); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 985 | } |
| 986 | if (canceledSomething) { |
| 987 | updateLightsLocked(); |
| 988 | } |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 989 | return canceledSomething; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 990 | } |
| 991 | } |
| 992 | |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 993 | |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 994 | public void cancelNotification(String pkg, int id) { |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 995 | cancelNotificationWithTag(pkg, null /* tag */, id); |
| 996 | } |
| 997 | |
| 998 | public void cancelNotificationWithTag(String pkg, String tag, int id) { |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 999 | checkIncomingCall(pkg); |
| 1000 | // Don't allow client applications to cancel foreground service notis. |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 1001 | cancelNotification(pkg, tag, id, 0, |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1002 | Binder.getCallingUid() == Process.SYSTEM_UID |
| 1003 | ? 0 : Notification.FLAG_FOREGROUND_SERVICE); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1004 | } |
| 1005 | |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1006 | public void cancelAllNotifications(String pkg) { |
| 1007 | checkIncomingCall(pkg); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1008 | |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1009 | // Calling from user space, don't allow the canceling of actively |
| 1010 | // running foreground services. |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1011 | cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1012 | } |
| 1013 | |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1014 | 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 Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1030 | |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1031 | void cancelAll() { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1032 | 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 Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1045 | Slog.w(TAG, "canceled PendingIntent for " + r.pkg, ex); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1046 | } |
| 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 Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1066 | // Battery low always shows, other states only show if charging. |
| 1067 | if (mBatteryLow) { |
| Mike Lockwood | 445f430 | 2009-09-04 11:06:46 -0400 | [diff] [blame] | 1068 | if (mBatteryCharging) { |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 1069 | mBatteryLight.setColor(BATTERY_LOW_ARGB); |
| Mike Lockwood | 445f430 | 2009-09-04 11:06:46 -0400 | [diff] [blame] | 1070 | } else { |
| 1071 | // Flash when battery is low and not charging |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 1072 | mBatteryLight.setFlashing(BATTERY_LOW_ARGB, LightsService.LIGHT_FLASH_TIMED, |
| 1073 | BATTERY_BLINK_ON, BATTERY_BLINK_OFF); |
| Mike Lockwood | 445f430 | 2009-09-04 11:06:46 -0400 | [diff] [blame] | 1074 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1075 | } else if (mBatteryCharging) { |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1076 | if (mBatteryFull) { |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 1077 | mBatteryLight.setColor(BATTERY_FULL_ARGB); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1078 | } else { |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 1079 | mBatteryLight.setColor(BATTERY_MEDIUM_ARGB); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1080 | } |
| 1081 | } else { |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 1082 | mBatteryLight.turnOff(); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1083 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1084 | |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1085 | // 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1091 | } |
| 1092 | } |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 1093 | |
| 1094 | // we only flash if screen is off and persistent pulsing is enabled |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 1095 | // and we are not currently in a call |
| 1096 | if (mLedNotification == null || mScreenOn || mInCall) { |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 1097 | mNotificationLight.turnOff(); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1098 | } else { |
| Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 1099 | 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 Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1115 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | // lock on mNotificationList |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 1119 | private int indexOfNotificationLocked(String pkg, String tag, int id) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1120 | { |
| 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 Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 1125 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1134 | if (r.id == id && r.pkg.equals(pkg)) { |
| 1135 | return i; |
| 1136 | } |
| 1137 | } |
| 1138 | return -1; |
| 1139 | } |
| 1140 | |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1141 | // 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 Lockwood | ff2544c | 2010-06-28 09:17:50 -0400 | [diff] [blame] | 1144 | private void updateAdbNotification(boolean adbEnabled) { |
| 1145 | if (adbEnabled) { |
| Mike Lockwood | ed76037 | 2009-07-09 07:07:27 -0400 | [diff] [blame] | 1146 | if ("0".equals(SystemProperties.get("persist.adb.notify"))) { |
| 1147 | return; |
| 1148 | } |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1149 | 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 Sandler | 39576c8 | 2010-03-25 16:02:33 -0400 | [diff] [blame] | 1161 | mAdbNotification.icon = com.android.internal.R.drawable.stat_sys_adb; |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1162 | mAdbNotification.when = 0; |
| 1163 | mAdbNotification.flags = Notification.FLAG_ONGOING_EVENT; |
| 1164 | mAdbNotification.tickerText = title; |
| Daniel Sandler | 39576c8 | 2010-03-25 16:02:33 -0400 | [diff] [blame] | 1165 | mAdbNotification.defaults = 0; // please be quiet |
| 1166 | mAdbNotification.sound = null; |
| 1167 | mAdbNotification.vibrate = null; |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1168 | } |
| 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 Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1183 | |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1184 | mAdbNotificationShown = true; |
| 1185 | notificationManager.notify( |
| 1186 | com.android.internal.R.string.adb_active_notification_title, |
| 1187 | mAdbNotification); |
| 1188 | } |
| 1189 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1190 | |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 1191 | } 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 Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 1201 | |
| 1202 | private void updateNotificationPulse() { |
| 1203 | synchronized (mNotificationList) { |
| 1204 | updateLightsLocked(); |
| 1205 | } |
| 1206 | } |
| 1207 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1208 | // ====================================================================== |
| 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 Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1218 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1219 | 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 Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1232 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1233 | } |
| 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 Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1244 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1245 | 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 Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1253 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1254 | pw.println(" mSoundNotification=" + mSoundNotification); |
| 1255 | pw.println(" mSound=" + mSound); |
| 1256 | pw.println(" mVibrateNotification=" + mVibrateNotification); |
| Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1257 | pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications)); |
| 1258 | pw.println(" mSystemReady=" + mSystemReady); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1259 | } |
| 1260 | } |
| 1261 | } |