| 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 | |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 19 | import static org.xmlpull.v1.XmlPullParser.END_DOCUMENT; |
| 20 | import static org.xmlpull.v1.XmlPullParser.END_TAG; |
| 21 | import static org.xmlpull.v1.XmlPullParser.START_TAG; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 22 | |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 23 | import android.app.ActivityManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.app.ActivityManagerNative; |
| Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 25 | import android.app.AppGlobals; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.app.IActivityManager; |
| 27 | import android.app.INotificationManager; |
| 28 | import android.app.ITransientNotification; |
| 29 | import android.app.Notification; |
| 30 | import android.app.PendingIntent; |
| 31 | import android.app.StatusBarManager; |
| 32 | import android.content.BroadcastReceiver; |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 33 | import android.content.ContentResolver; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.content.Context; |
| 35 | import android.content.Intent; |
| 36 | import android.content.IntentFilter; |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 37 | import android.content.pm.ApplicationInfo; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import android.content.pm.PackageManager; |
| 39 | import android.content.pm.PackageManager.NameNotFoundException; |
| 40 | import android.content.res.Resources; |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 41 | import android.database.ContentObserver; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 42 | import android.media.AudioManager; |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 43 | import android.media.IAudioService; |
| 44 | import android.media.IRingtonePlayer; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import android.net.Uri; |
| 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; |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 50 | import android.os.Process; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 51 | import android.os.RemoteException; |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 52 | import android.os.ServiceManager; |
| Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 53 | import android.os.UserHandle; |
| 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; |
| John Spurlock | 7be1a3d | 2012-08-13 16:45:12 -0400 | [diff] [blame] | 56 | import android.service.dreams.IDreamManager; |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 57 | import android.telephony.TelephonyManager; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 58 | import android.text.TextUtils; |
| Dianne Hackborn | 39606a0 | 2012-07-31 17:54:35 -0700 | [diff] [blame] | 59 | import android.util.AtomicFile; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | import android.util.EventLog; |
| 61 | import android.util.Log; |
| Andy Stadler | 110988c | 2010-12-03 14:29:16 -0800 | [diff] [blame] | 62 | import android.util.Slog; |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 63 | import android.util.Xml; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 64 | import android.view.accessibility.AccessibilityEvent; |
| 65 | import android.view.accessibility.AccessibilityManager; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | import android.widget.Toast; |
| 67 | |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 68 | import com.android.internal.statusbar.StatusBarNotification; |
| 69 | import com.android.internal.util.FastXmlSerializer; |
| 70 | |
| 71 | import org.xmlpull.v1.XmlPullParser; |
| 72 | import org.xmlpull.v1.XmlPullParserException; |
| 73 | import org.xmlpull.v1.XmlSerializer; |
| 74 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 75 | import java.io.File; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | import java.io.FileDescriptor; |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 77 | import java.io.FileInputStream; |
| 78 | import java.io.FileNotFoundException; |
| 79 | import java.io.FileOutputStream; |
| 80 | import java.io.IOException; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | import java.io.PrintWriter; |
| 82 | import java.util.ArrayList; |
| 83 | import java.util.Arrays; |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 84 | import java.util.HashSet; |
| 85 | |
| 86 | import libcore.io.IoUtils; |
| 87 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | |
| Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 89 | /** {@hide} */ |
| 90 | public class NotificationManagerService extends INotificationManager.Stub |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | { |
| 92 | private static final String TAG = "NotificationService"; |
| 93 | private static final boolean DBG = false; |
| 94 | |
| Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 95 | private static final int MAX_PACKAGE_NOTIFICATIONS = 50; |
| 96 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | // message codes |
| 98 | private static final int MESSAGE_TIMEOUT = 2; |
| 99 | |
| 100 | private static final int LONG_DELAY = 3500; // 3.5 seconds |
| 101 | private static final int SHORT_DELAY = 2000; // 2 seconds |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 102 | |
| 103 | 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] | 104 | |
| 105 | private static final int DEFAULT_STREAM_TYPE = AudioManager.STREAM_NOTIFICATION; |
| Daniel Sandler | 49a2ad1 | 2012-03-28 15:46:39 -0400 | [diff] [blame] | 106 | private static final boolean SCORE_ONGOING_HIGHER = false; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 107 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 108 | private static final int JUNK_SCORE = -1000; |
| 109 | private static final int NOTIFICATION_PRIORITY_MULTIPLIER = 10; |
| 110 | private static final int SCORE_DISPLAY_THRESHOLD = Notification.PRIORITY_MIN * NOTIFICATION_PRIORITY_MULTIPLIER; |
| 111 | |
| 112 | private static final boolean ENABLE_BLOCKED_NOTIFICATIONS = true; |
| 113 | private static final boolean ENABLE_BLOCKED_TOASTS = true; |
| 114 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | final Context mContext; |
| 116 | final IActivityManager mAm; |
| 117 | final IBinder mForegroundToken = new Binder(); |
| 118 | |
| 119 | private WorkerHandler mHandler; |
| Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 120 | private StatusBarManagerService mStatusBar; |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 121 | private LightsService.Light mNotificationLight; |
| 122 | private LightsService.Light mAttentionLight; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | |
| Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 124 | private int mDefaultNotificationColor; |
| 125 | private int mDefaultNotificationLedOn; |
| 126 | private int mDefaultNotificationLedOff; |
| 127 | |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 128 | private boolean mSystemReady; |
| Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 129 | private int mDisabledNotifications; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 131 | private NotificationRecord mSoundNotification; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | private NotificationRecord mVibrateNotification; |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 133 | |
| 134 | private IAudioService mAudioService; |
| Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 135 | private Vibrator mVibrator; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 137 | // for enabling and disabling notification pulse behavior |
| Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 138 | private boolean mScreenOn = true; |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 139 | private boolean mInCall = false; |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 140 | private boolean mNotificationPulseEnabled; |
| 141 | |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 142 | private final ArrayList<NotificationRecord> mNotificationList = |
| 143 | new ArrayList<NotificationRecord>(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | |
| 145 | private ArrayList<ToastRecord> mToastQueue; |
| 146 | |
| 147 | private ArrayList<NotificationRecord> mLights = new ArrayList<NotificationRecord>(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | private NotificationRecord mLedNotification; |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 149 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 150 | // Notification control database. For now just contains disabled packages. |
| 151 | private AtomicFile mPolicyFile; |
| 152 | private HashSet<String> mBlockedPackages = new HashSet<String>(); |
| 153 | |
| John Spurlock | 7be1a3d | 2012-08-13 16:45:12 -0400 | [diff] [blame] | 154 | private IDreamManager mSandman; |
| 155 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 156 | private static final int DB_VERSION = 1; |
| 157 | |
| 158 | private static final String TAG_BODY = "notification-policy"; |
| 159 | private static final String ATTR_VERSION = "version"; |
| 160 | |
| 161 | private static final String TAG_BLOCKED_PKGS = "blocked-packages"; |
| 162 | private static final String TAG_PACKAGE = "package"; |
| 163 | private static final String ATTR_NAME = "name"; |
| 164 | |
| 165 | private void loadBlockDb() { |
| 166 | synchronized(mBlockedPackages) { |
| 167 | if (mPolicyFile == null) { |
| 168 | File dir = new File("/data/system"); |
| 169 | mPolicyFile = new AtomicFile(new File(dir, "notification_policy.xml")); |
| 170 | |
| 171 | mBlockedPackages.clear(); |
| 172 | |
| 173 | FileInputStream infile = null; |
| 174 | try { |
| 175 | infile = mPolicyFile.openRead(); |
| 176 | final XmlPullParser parser = Xml.newPullParser(); |
| 177 | parser.setInput(infile, null); |
| 178 | |
| 179 | int type; |
| 180 | String tag; |
| 181 | int version = DB_VERSION; |
| 182 | while ((type = parser.next()) != END_DOCUMENT) { |
| 183 | tag = parser.getName(); |
| 184 | if (type == START_TAG) { |
| 185 | if (TAG_BODY.equals(tag)) { |
| 186 | version = Integer.parseInt(parser.getAttributeValue(null, ATTR_VERSION)); |
| 187 | } else if (TAG_BLOCKED_PKGS.equals(tag)) { |
| 188 | while ((type = parser.next()) != END_DOCUMENT) { |
| 189 | tag = parser.getName(); |
| 190 | if (TAG_PACKAGE.equals(tag)) { |
| 191 | mBlockedPackages.add(parser.getAttributeValue(null, ATTR_NAME)); |
| 192 | } else if (TAG_BLOCKED_PKGS.equals(tag) && type == END_TAG) { |
| 193 | break; |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | } catch (FileNotFoundException e) { |
| 200 | // No data yet |
| 201 | } catch (IOException e) { |
| 202 | Log.wtf(TAG, "Unable to read blocked notifications database", e); |
| 203 | } catch (NumberFormatException e) { |
| 204 | Log.wtf(TAG, "Unable to parse blocked notifications database", e); |
| 205 | } catch (XmlPullParserException e) { |
| 206 | Log.wtf(TAG, "Unable to parse blocked notifications database", e); |
| 207 | } finally { |
| 208 | IoUtils.closeQuietly(infile); |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | private void writeBlockDb() { |
| 215 | synchronized(mBlockedPackages) { |
| 216 | FileOutputStream outfile = null; |
| 217 | try { |
| 218 | outfile = mPolicyFile.startWrite(); |
| 219 | |
| 220 | XmlSerializer out = new FastXmlSerializer(); |
| 221 | out.setOutput(outfile, "utf-8"); |
| 222 | |
| 223 | out.startDocument(null, true); |
| 224 | |
| 225 | out.startTag(null, TAG_BODY); { |
| 226 | out.attribute(null, ATTR_VERSION, String.valueOf(DB_VERSION)); |
| 227 | out.startTag(null, TAG_BLOCKED_PKGS); { |
| 228 | // write all known network policies |
| 229 | for (String pkg : mBlockedPackages) { |
| 230 | out.startTag(null, TAG_PACKAGE); { |
| 231 | out.attribute(null, ATTR_NAME, pkg); |
| 232 | } out.endTag(null, TAG_PACKAGE); |
| 233 | } |
| 234 | } out.endTag(null, TAG_BLOCKED_PKGS); |
| 235 | } out.endTag(null, TAG_BODY); |
| 236 | |
| 237 | out.endDocument(); |
| 238 | |
| 239 | mPolicyFile.finishWrite(outfile); |
| 240 | } catch (IOException e) { |
| 241 | if (outfile != null) { |
| 242 | mPolicyFile.failWrite(outfile); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | public boolean areNotificationsEnabledForPackage(String pkg) { |
| 249 | checkCallerIsSystem(); |
| 250 | return areNotificationsEnabledForPackageInt(pkg); |
| 251 | } |
| 252 | |
| 253 | // Unchecked. Not exposed via Binder, but can be called in the course of enqueue*(). |
| 254 | private boolean areNotificationsEnabledForPackageInt(String pkg) { |
| 255 | final boolean enabled = !mBlockedPackages.contains(pkg); |
| 256 | if (DBG) { |
| 257 | Slog.v(TAG, "notifications are " + (enabled?"en":"dis") + "abled for " + pkg); |
| 258 | } |
| 259 | return enabled; |
| 260 | } |
| 261 | |
| 262 | public void setNotificationsEnabledForPackage(String pkg, boolean enabled) { |
| 263 | checkCallerIsSystem(); |
| 264 | if (DBG) { |
| 265 | Slog.v(TAG, (enabled?"en":"dis") + "abling notifications for " + pkg); |
| 266 | } |
| 267 | if (enabled) { |
| 268 | mBlockedPackages.remove(pkg); |
| 269 | } else { |
| 270 | mBlockedPackages.add(pkg); |
| 271 | |
| 272 | // Now, cancel any outstanding notifications that are part of a just-disabled app |
| 273 | if (ENABLE_BLOCKED_NOTIFICATIONS) { |
| 274 | synchronized (mNotificationList) { |
| 275 | final int N = mNotificationList.size(); |
| 276 | for (int i=0; i<N; i++) { |
| 277 | final NotificationRecord r = mNotificationList.get(i); |
| 278 | if (r.pkg.equals(pkg)) { |
| 279 | cancelNotificationLocked(r, false); |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | // Don't bother canceling toasts, they'll go away soon enough. |
| 285 | } |
| 286 | writeBlockDb(); |
| 287 | } |
| 288 | |
| 289 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 | private static String idDebugString(Context baseContext, String packageName, int id) { |
| 291 | Context c = null; |
| 292 | |
| 293 | if (packageName != null) { |
| 294 | try { |
| 295 | c = baseContext.createPackageContext(packageName, 0); |
| 296 | } catch (NameNotFoundException e) { |
| 297 | c = baseContext; |
| 298 | } |
| 299 | } else { |
| 300 | c = baseContext; |
| 301 | } |
| 302 | |
| 303 | String pkg; |
| 304 | String type; |
| 305 | String name; |
| 306 | |
| 307 | Resources r = c.getResources(); |
| 308 | try { |
| 309 | return r.getResourceName(id); |
| 310 | } catch (Resources.NotFoundException e) { |
| 311 | return "<name unknown>"; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | private static final class NotificationRecord |
| 316 | { |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 317 | final String pkg; |
| 318 | final String tag; |
| 319 | final int id; |
| Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 320 | final int uid; |
| 321 | final int initialPid; |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 322 | final int userId; |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 323 | final Notification notification; |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 324 | final int score; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | IBinder statusBarKey; |
| 326 | |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 327 | NotificationRecord(String pkg, String tag, int id, int uid, int initialPid, |
| 328 | int userId, int score, Notification notification) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 | { |
| 330 | this.pkg = pkg; |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 331 | this.tag = tag; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 332 | this.id = id; |
| Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 333 | this.uid = uid; |
| 334 | this.initialPid = initialPid; |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 335 | this.userId = userId; |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 336 | this.score = score; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | this.notification = notification; |
| 338 | } |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 339 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | void dump(PrintWriter pw, String prefix, Context baseContext) { |
| 341 | pw.println(prefix + this); |
| 342 | pw.println(prefix + " icon=0x" + Integer.toHexString(notification.icon) |
| 343 | + " / " + idDebugString(baseContext, this.pkg, notification.icon)); |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 344 | pw.println(prefix + " pri=" + notification.priority); |
| 345 | pw.println(prefix + " score=" + this.score); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 346 | pw.println(prefix + " contentIntent=" + notification.contentIntent); |
| 347 | pw.println(prefix + " deleteIntent=" + notification.deleteIntent); |
| 348 | pw.println(prefix + " tickerText=" + notification.tickerText); |
| 349 | pw.println(prefix + " contentView=" + notification.contentView); |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 350 | pw.println(prefix + " uid=" + uid + " userId=" + userId); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | pw.println(prefix + " defaults=0x" + Integer.toHexString(notification.defaults)); |
| 352 | pw.println(prefix + " flags=0x" + Integer.toHexString(notification.flags)); |
| 353 | pw.println(prefix + " sound=" + notification.sound); |
| 354 | pw.println(prefix + " vibrate=" + Arrays.toString(notification.vibrate)); |
| 355 | pw.println(prefix + " ledARGB=0x" + Integer.toHexString(notification.ledARGB) |
| 356 | + " ledOnMS=" + notification.ledOnMS |
| 357 | + " ledOffMS=" + notification.ledOffMS); |
| 358 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 359 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 360 | @Override |
| 361 | public final String toString() |
| 362 | { |
| 363 | return "NotificationRecord{" |
| 364 | + Integer.toHexString(System.identityHashCode(this)) |
| 365 | + " pkg=" + pkg |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 366 | + " id=" + Integer.toHexString(id) |
| Daniel Sandler | e40451a | 2011-02-03 14:51:35 -0500 | [diff] [blame] | 367 | + " tag=" + tag |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 368 | + " score=" + score |
| Daniel Sandler | e40451a | 2011-02-03 14:51:35 -0500 | [diff] [blame] | 369 | + "}"; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 370 | } |
| 371 | } |
| 372 | |
| 373 | private static final class ToastRecord |
| 374 | { |
| 375 | final int pid; |
| 376 | final String pkg; |
| 377 | final ITransientNotification callback; |
| 378 | int duration; |
| 379 | |
| 380 | ToastRecord(int pid, String pkg, ITransientNotification callback, int duration) |
| 381 | { |
| 382 | this.pid = pid; |
| 383 | this.pkg = pkg; |
| 384 | this.callback = callback; |
| 385 | this.duration = duration; |
| 386 | } |
| 387 | |
| 388 | void update(int duration) { |
| 389 | this.duration = duration; |
| 390 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 391 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 392 | void dump(PrintWriter pw, String prefix) { |
| 393 | pw.println(prefix + this); |
| 394 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 395 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 396 | @Override |
| 397 | public final String toString() |
| 398 | { |
| 399 | return "ToastRecord{" |
| 400 | + Integer.toHexString(System.identityHashCode(this)) |
| 401 | + " pkg=" + pkg |
| 402 | + " callback=" + callback |
| 403 | + " duration=" + duration; |
| 404 | } |
| 405 | } |
| 406 | |
| Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 407 | private StatusBarManagerService.NotificationCallbacks mNotificationCallbacks |
| 408 | = new StatusBarManagerService.NotificationCallbacks() { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 409 | |
| 410 | public void onSetDisabled(int status) { |
| 411 | synchronized (mNotificationList) { |
| 412 | mDisabledNotifications = status; |
| 413 | if ((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) != 0) { |
| 414 | // cancel whatever's going on |
| 415 | long identity = Binder.clearCallingIdentity(); |
| 416 | try { |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 417 | final IRingtonePlayer player = mAudioService.getRingtonePlayer(); |
| 418 | if (player != null) { |
| 419 | player.stopAsync(); |
| 420 | } |
| 421 | } catch (RemoteException e) { |
| 422 | } finally { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 423 | Binder.restoreCallingIdentity(identity); |
| 424 | } |
| 425 | |
| 426 | identity = Binder.clearCallingIdentity(); |
| 427 | try { |
| 428 | mVibrator.cancel(); |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 429 | } finally { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 430 | Binder.restoreCallingIdentity(identity); |
| 431 | } |
| 432 | } |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | public void onClearAll() { |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 437 | // XXX to be totally correct, the caller should tell us which user |
| 438 | // this is for. |
| 439 | cancelAll(ActivityManager.getCurrentUser()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 440 | } |
| 441 | |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 442 | public void onNotificationClick(String pkg, String tag, int id) { |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 443 | // XXX to be totally correct, the caller should tell us which user |
| 444 | // this is for. |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 445 | cancelNotification(pkg, tag, id, Notification.FLAG_AUTO_CANCEL, |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 446 | Notification.FLAG_FOREGROUND_SERVICE, false, |
| 447 | ActivityManager.getCurrentUser()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 448 | } |
| 449 | |
| Daniel Sandler | 0f0b11c | 2010-08-04 15:54:58 -0400 | [diff] [blame] | 450 | public void onNotificationClear(String pkg, String tag, int id) { |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 451 | // XXX to be totally correct, the caller should tell us which user |
| 452 | // this is for. |
| Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 453 | cancelNotification(pkg, tag, id, 0, |
| 454 | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_FOREGROUND_SERVICE, |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 455 | true, ActivityManager.getCurrentUser()); |
| Daniel Sandler | 0f0b11c | 2010-08-04 15:54:58 -0400 | [diff] [blame] | 456 | } |
| 457 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 458 | public void onPanelRevealed() { |
| 459 | synchronized (mNotificationList) { |
| 460 | // sound |
| 461 | mSoundNotification = null; |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 462 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | long identity = Binder.clearCallingIdentity(); |
| 464 | try { |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 465 | final IRingtonePlayer player = mAudioService.getRingtonePlayer(); |
| 466 | if (player != null) { |
| 467 | player.stopAsync(); |
| 468 | } |
| 469 | } catch (RemoteException e) { |
| 470 | } finally { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 471 | Binder.restoreCallingIdentity(identity); |
| 472 | } |
| 473 | |
| 474 | // vibrate |
| 475 | mVibrateNotification = null; |
| 476 | identity = Binder.clearCallingIdentity(); |
| 477 | try { |
| 478 | mVibrator.cancel(); |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 479 | } finally { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 480 | Binder.restoreCallingIdentity(identity); |
| 481 | } |
| 482 | |
| 483 | // light |
| 484 | mLights.clear(); |
| 485 | mLedNotification = null; |
| 486 | updateLightsLocked(); |
| 487 | } |
| 488 | } |
| Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 489 | |
| Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 490 | public void onNotificationError(String pkg, String tag, int id, |
| 491 | int uid, int initialPid, String message) { |
| Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 492 | Slog.d(TAG, "onNotification error pkg=" + pkg + " tag=" + tag + " id=" + id |
| 493 | + "; will crashApplication(uid=" + uid + ", pid=" + initialPid + ")"); |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 494 | // XXX to be totally correct, the caller should tell us which user |
| 495 | // this is for. |
| 496 | cancelNotification(pkg, tag, id, 0, 0, false, UserHandle.getUserId(uid)); |
| Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 497 | long ident = Binder.clearCallingIdentity(); |
| 498 | try { |
| 499 | ActivityManagerNative.getDefault().crashApplication(uid, initialPid, pkg, |
| 500 | "Bad notification posted from package " + pkg |
| 501 | + ": " + message); |
| 502 | } catch (RemoteException e) { |
| 503 | } |
| 504 | Binder.restoreCallingIdentity(ident); |
| Joe Onorato | 005847b | 2010-06-04 16:08:02 -0400 | [diff] [blame] | 505 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 506 | }; |
| 507 | |
| 508 | private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() { |
| 509 | @Override |
| 510 | public void onReceive(Context context, Intent intent) { |
| 511 | String action = intent.getAction(); |
| 512 | |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 513 | boolean queryRestart = false; |
| Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 514 | boolean packageChanged = false; |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 515 | |
| Mike Lockwood | 541c994 | 2011-06-12 19:35:45 -0400 | [diff] [blame] | 516 | if (action.equals(Intent.ACTION_PACKAGE_REMOVED) |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 517 | || action.equals(Intent.ACTION_PACKAGE_RESTARTED) |
| Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 518 | || (packageChanged=action.equals(Intent.ACTION_PACKAGE_CHANGED)) |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 519 | || (queryRestart=action.equals(Intent.ACTION_QUERY_PACKAGE_RESTART)) |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 520 | || action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 521 | String pkgList[] = null; |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 522 | if (action.equals(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE)) { |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 523 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_CHANGED_PACKAGE_LIST); |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 524 | } else if (queryRestart) { |
| 525 | pkgList = intent.getStringArrayExtra(Intent.EXTRA_PACKAGES); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 526 | } else { |
| 527 | Uri uri = intent.getData(); |
| 528 | if (uri == null) { |
| 529 | return; |
| 530 | } |
| 531 | String pkgName = uri.getSchemeSpecificPart(); |
| 532 | if (pkgName == null) { |
| 533 | return; |
| 534 | } |
| Daniel Sandler | 26ece57 | 2012-06-01 15:38:46 -0400 | [diff] [blame] | 535 | if (packageChanged) { |
| 536 | // We cancel notifications for packages which have just been disabled |
| 537 | final int enabled = mContext.getPackageManager() |
| 538 | .getApplicationEnabledSetting(pkgName); |
| 539 | if (enabled == PackageManager.COMPONENT_ENABLED_STATE_ENABLED |
| 540 | || enabled == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) { |
| 541 | return; |
| 542 | } |
| 543 | } |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 544 | pkgList = new String[]{pkgName}; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 545 | } |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 546 | if (pkgList != null && (pkgList.length > 0)) { |
| 547 | for (String pkgName : pkgList) { |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 548 | cancelAllNotificationsInt(pkgName, 0, 0, !queryRestart, |
| 549 | UserHandle.USER_ALL); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 550 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 551 | } |
| Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 552 | } else if (action.equals(Intent.ACTION_SCREEN_ON)) { |
| 553 | // Keep track of screen on/off state, but do not turn off the notification light |
| 554 | // until user passes through the lock screen or views the notification. |
| 555 | mScreenOn = true; |
| 556 | } else if (action.equals(Intent.ACTION_SCREEN_OFF)) { |
| 557 | mScreenOn = false; |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 558 | } else if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) { |
| Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 559 | mInCall = (intent.getStringExtra(TelephonyManager.EXTRA_STATE).equals( |
| 560 | TelephonyManager.EXTRA_STATE_OFFHOOK)); |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 561 | updateNotificationPulse(); |
| Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 562 | } else if (action.equals(Intent.ACTION_USER_STOPPED)) { |
| 563 | int userHandle = intent.getIntExtra(Intent.EXTRA_USER_HANDLE, -1); |
| 564 | if (userHandle >= 0) { |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 565 | cancelAllNotificationsInt(null, 0, 0, true, userHandle); |
| Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 566 | } |
| Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 567 | } else if (action.equals(Intent.ACTION_USER_PRESENT)) { |
| 568 | // turn off LED when user passes through lock screen |
| 569 | mNotificationLight.turnOff(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | }; |
| 573 | |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 574 | class SettingsObserver extends ContentObserver { |
| 575 | SettingsObserver(Handler handler) { |
| 576 | super(handler); |
| 577 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 578 | |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 579 | void observe() { |
| 580 | ContentResolver resolver = mContext.getContentResolver(); |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 581 | resolver.registerContentObserver(Settings.System.getUriFor( |
| 582 | Settings.System.NOTIFICATION_LIGHT_PULSE), false, this); |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 583 | update(); |
| 584 | } |
| 585 | |
| 586 | @Override public void onChange(boolean selfChange) { |
| 587 | update(); |
| 588 | } |
| 589 | |
| 590 | public void update() { |
| 591 | ContentResolver resolver = mContext.getContentResolver(); |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 592 | boolean pulseEnabled = Settings.System.getInt(resolver, |
| 593 | Settings.System.NOTIFICATION_LIGHT_PULSE, 0) != 0; |
| 594 | if (mNotificationPulseEnabled != pulseEnabled) { |
| 595 | mNotificationPulseEnabled = pulseEnabled; |
| 596 | updateNotificationPulse(); |
| 597 | } |
| Dianne Hackborn | 1dac277 | 2009-06-26 18:16:48 -0700 | [diff] [blame] | 598 | } |
| 599 | } |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 600 | |
| Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 601 | NotificationManagerService(Context context, StatusBarManagerService statusBar, |
| Mike Lockwood | 3a32213 | 2009-11-24 00:30:52 -0500 | [diff] [blame] | 602 | LightsService lights) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 603 | { |
| 604 | super(); |
| 605 | mContext = context; |
| Jeff Brown | c234613 | 2012-04-13 01:55:38 -0700 | [diff] [blame] | 606 | mVibrator = (Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 607 | mAm = ActivityManagerNative.getDefault(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 608 | mToastQueue = new ArrayList<ToastRecord>(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 609 | mHandler = new WorkerHandler(); |
| San Mehat | 3ee1317 | 2010-02-04 20:54:43 -0800 | [diff] [blame] | 610 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 611 | loadBlockDb(); |
| 612 | |
| Joe Onorato | 089de88 | 2010-04-12 08:18:45 -0700 | [diff] [blame] | 613 | mStatusBar = statusBar; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 614 | statusBar.setNotificationCallbacks(mNotificationCallbacks); |
| 615 | |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 616 | mNotificationLight = lights.getLight(LightsService.LIGHT_ID_NOTIFICATIONS); |
| 617 | mAttentionLight = lights.getLight(LightsService.LIGHT_ID_ATTENTION); |
| 618 | |
| Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 619 | Resources resources = mContext.getResources(); |
| 620 | mDefaultNotificationColor = resources.getColor( |
| 621 | com.android.internal.R.color.config_defaultNotificationColor); |
| 622 | mDefaultNotificationLedOn = resources.getInteger( |
| 623 | com.android.internal.R.integer.config_defaultNotificationLedOn); |
| 624 | mDefaultNotificationLedOff = resources.getInteger( |
| 625 | com.android.internal.R.integer.config_defaultNotificationLedOff); |
| 626 | |
| Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 627 | // Don't start allowing notifications until the setup wizard has run once. |
| 628 | // After that, including subsequent boots, init with notifications turned on. |
| 629 | // This works on the first boot because the setup wizard will toggle this |
| 630 | // flag at least once and we'll go back to 0 after that. |
| 631 | if (0 == Settings.Secure.getInt(mContext.getContentResolver(), |
| 632 | Settings.Secure.DEVICE_PROVISIONED, 0)) { |
| 633 | mDisabledNotifications = StatusBarManager.DISABLE_NOTIFICATION_ALERTS; |
| 634 | } |
| 635 | |
| Mike Lockwood | 35e16bf | 2010-11-30 19:53:36 -0500 | [diff] [blame] | 636 | // register for various Intents |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 637 | IntentFilter filter = new IntentFilter(); |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 638 | filter.addAction(Intent.ACTION_SCREEN_ON); |
| 639 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
| Daniel Sandler | e96ffb1 | 2010-03-11 13:38:06 -0500 | [diff] [blame] | 640 | filter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED); |
| Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 641 | filter.addAction(Intent.ACTION_USER_PRESENT); |
| Dianne Hackborn | 80a4af2 | 2012-08-27 19:18:31 -0700 | [diff] [blame] | 642 | filter.addAction(Intent.ACTION_USER_STOPPED); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 643 | mContext.registerReceiver(mIntentReceiver, filter); |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 644 | IntentFilter pkgFilter = new IntentFilter(); |
| 645 | pkgFilter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| Daniel Sandler | aac0eb0 | 2011-08-06 22:51:56 -0400 | [diff] [blame] | 646 | pkgFilter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 647 | pkgFilter.addAction(Intent.ACTION_PACKAGE_RESTARTED); |
| 648 | pkgFilter.addAction(Intent.ACTION_QUERY_PACKAGE_RESTART); |
| 649 | pkgFilter.addDataScheme("package"); |
| 650 | mContext.registerReceiver(mIntentReceiver, pkgFilter); |
| Suchi Amalapurapu | b56ae20 | 2010-02-04 22:51:07 -0800 | [diff] [blame] | 651 | IntentFilter sdFilter = new IntentFilter(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE); |
| Suchi Amalapurapu | 08675a3 | 2010-01-28 09:57:30 -0800 | [diff] [blame] | 652 | mContext.registerReceiver(mIntentReceiver, sdFilter); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 653 | |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 654 | SettingsObserver observer = new SettingsObserver(mHandler); |
| 655 | observer.observe(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 656 | } |
| 657 | |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 658 | void systemReady() { |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 659 | mAudioService = IAudioService.Stub.asInterface( |
| 660 | ServiceManager.getService(Context.AUDIO_SERVICE)); |
| John Spurlock | 7be1a3d | 2012-08-13 16:45:12 -0400 | [diff] [blame] | 661 | mSandman = IDreamManager.Stub.asInterface( |
| 662 | ServiceManager.getService("dreams")); |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 663 | |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 664 | // no beeping until we're basically done booting |
| 665 | mSystemReady = true; |
| 666 | } |
| 667 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 668 | // Toasts |
| 669 | // ============================================================================ |
| 670 | public void enqueueToast(String pkg, ITransientNotification callback, int duration) |
| 671 | { |
| Daniel Sandler | a703590 | 2010-03-30 15:45:31 -0400 | [diff] [blame] | 672 | 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] | 673 | |
| 674 | if (pkg == null || callback == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 675 | 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] | 676 | return ; |
| 677 | } |
| 678 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 679 | final boolean isSystemToast = ("android".equals(pkg)); |
| 680 | |
| 681 | if (ENABLE_BLOCKED_TOASTS && !isSystemToast && !areNotificationsEnabledForPackageInt(pkg)) { |
| 682 | Slog.e(TAG, "Suppressing toast from package " + pkg + " by user request."); |
| 683 | return; |
| 684 | } |
| 685 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 686 | synchronized (mToastQueue) { |
| 687 | int callingPid = Binder.getCallingPid(); |
| 688 | long callingId = Binder.clearCallingIdentity(); |
| 689 | try { |
| 690 | ToastRecord record; |
| 691 | int index = indexOfToastLocked(pkg, callback); |
| 692 | // If it's already in the queue, we update it in place, we don't |
| 693 | // move it to the end of the queue. |
| 694 | if (index >= 0) { |
| 695 | record = mToastQueue.get(index); |
| 696 | record.update(duration); |
| 697 | } else { |
| Vairavan Srinivasan | f9eb06c | 2011-01-21 18:08:36 -0800 | [diff] [blame] | 698 | // Limit the number of toasts that any given package except the android |
| 699 | // package can enqueue. Prevents DOS attacks and deals with leaks. |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 700 | if (!isSystemToast) { |
| Vairavan Srinivasan | f9eb06c | 2011-01-21 18:08:36 -0800 | [diff] [blame] | 701 | int count = 0; |
| 702 | final int N = mToastQueue.size(); |
| 703 | for (int i=0; i<N; i++) { |
| 704 | final ToastRecord r = mToastQueue.get(i); |
| 705 | if (r.pkg.equals(pkg)) { |
| 706 | count++; |
| 707 | if (count >= MAX_PACKAGE_NOTIFICATIONS) { |
| 708 | Slog.e(TAG, "Package has already posted " + count |
| 709 | + " toasts. Not showing more. Package=" + pkg); |
| 710 | return; |
| 711 | } |
| 712 | } |
| 713 | } |
| 714 | } |
| 715 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 716 | record = new ToastRecord(callingPid, pkg, callback, duration); |
| 717 | mToastQueue.add(record); |
| 718 | index = mToastQueue.size() - 1; |
| 719 | keepProcessAliveLocked(callingPid); |
| 720 | } |
| 721 | // If it's at index 0, it's the current toast. It doesn't matter if it's |
| 722 | // new or just been updated. Call back and tell it to show itself. |
| 723 | // If the callback fails, this will remove it from the list, so don't |
| 724 | // assume that it's valid after this. |
| 725 | if (index == 0) { |
| 726 | showNextToastLocked(); |
| 727 | } |
| 728 | } finally { |
| 729 | Binder.restoreCallingIdentity(callingId); |
| 730 | } |
| 731 | } |
| 732 | } |
| 733 | |
| 734 | public void cancelToast(String pkg, ITransientNotification callback) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 735 | Slog.i(TAG, "cancelToast pkg=" + pkg + " callback=" + callback); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 736 | |
| 737 | if (pkg == null || callback == null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 738 | 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] | 739 | return ; |
| 740 | } |
| 741 | |
| 742 | synchronized (mToastQueue) { |
| 743 | long callingId = Binder.clearCallingIdentity(); |
| 744 | try { |
| 745 | int index = indexOfToastLocked(pkg, callback); |
| 746 | if (index >= 0) { |
| 747 | cancelToastLocked(index); |
| 748 | } else { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 749 | 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] | 750 | } |
| 751 | } finally { |
| 752 | Binder.restoreCallingIdentity(callingId); |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | private void showNextToastLocked() { |
| 758 | ToastRecord record = mToastQueue.get(0); |
| 759 | while (record != null) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 760 | 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] | 761 | try { |
| 762 | record.callback.show(); |
| 763 | scheduleTimeoutLocked(record, false); |
| 764 | return; |
| 765 | } catch (RemoteException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 766 | 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] | 767 | + " in package " + record.pkg); |
| 768 | // remove it from the list and let the process die |
| 769 | int index = mToastQueue.indexOf(record); |
| 770 | if (index >= 0) { |
| 771 | mToastQueue.remove(index); |
| 772 | } |
| 773 | keepProcessAliveLocked(record.pid); |
| 774 | if (mToastQueue.size() > 0) { |
| 775 | record = mToastQueue.get(0); |
| 776 | } else { |
| 777 | record = null; |
| 778 | } |
| 779 | } |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | private void cancelToastLocked(int index) { |
| 784 | ToastRecord record = mToastQueue.get(index); |
| 785 | try { |
| 786 | record.callback.hide(); |
| 787 | } catch (RemoteException e) { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 788 | 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] | 789 | + " in package " + record.pkg); |
| 790 | // don't worry about this, we're about to remove it from |
| 791 | // the list anyway |
| 792 | } |
| 793 | mToastQueue.remove(index); |
| 794 | keepProcessAliveLocked(record.pid); |
| 795 | if (mToastQueue.size() > 0) { |
| 796 | // Show the next one. If the callback fails, this will remove |
| 797 | // it from the list, so don't assume that the list hasn't changed |
| 798 | // after this point. |
| 799 | showNextToastLocked(); |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | private void scheduleTimeoutLocked(ToastRecord r, boolean immediate) |
| 804 | { |
| 805 | Message m = Message.obtain(mHandler, MESSAGE_TIMEOUT, r); |
| 806 | long delay = immediate ? 0 : (r.duration == Toast.LENGTH_LONG ? LONG_DELAY : SHORT_DELAY); |
| 807 | mHandler.removeCallbacksAndMessages(r); |
| 808 | mHandler.sendMessageDelayed(m, delay); |
| 809 | } |
| 810 | |
| 811 | private void handleTimeout(ToastRecord record) |
| 812 | { |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 813 | 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] | 814 | synchronized (mToastQueue) { |
| 815 | int index = indexOfToastLocked(record.pkg, record.callback); |
| 816 | if (index >= 0) { |
| 817 | cancelToastLocked(index); |
| 818 | } |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | // lock on mToastQueue |
| 823 | private int indexOfToastLocked(String pkg, ITransientNotification callback) |
| 824 | { |
| 825 | IBinder cbak = callback.asBinder(); |
| 826 | ArrayList<ToastRecord> list = mToastQueue; |
| 827 | int len = list.size(); |
| 828 | for (int i=0; i<len; i++) { |
| 829 | ToastRecord r = list.get(i); |
| 830 | if (r.pkg.equals(pkg) && r.callback.asBinder() == cbak) { |
| 831 | return i; |
| 832 | } |
| 833 | } |
| 834 | return -1; |
| 835 | } |
| 836 | |
| 837 | // lock on mToastQueue |
| 838 | private void keepProcessAliveLocked(int pid) |
| 839 | { |
| 840 | int toastCount = 0; // toasts from this pid |
| 841 | ArrayList<ToastRecord> list = mToastQueue; |
| 842 | int N = list.size(); |
| 843 | for (int i=0; i<N; i++) { |
| 844 | ToastRecord r = list.get(i); |
| 845 | if (r.pid == pid) { |
| 846 | toastCount++; |
| 847 | } |
| 848 | } |
| 849 | try { |
| 850 | mAm.setProcessForeground(mForegroundToken, pid, toastCount > 0); |
| 851 | } catch (RemoteException e) { |
| 852 | // Shouldn't happen. |
| 853 | } |
| 854 | } |
| 855 | |
| 856 | private final class WorkerHandler extends Handler |
| 857 | { |
| 858 | @Override |
| 859 | public void handleMessage(Message msg) |
| 860 | { |
| 861 | switch (msg.what) |
| 862 | { |
| 863 | case MESSAGE_TIMEOUT: |
| 864 | handleTimeout((ToastRecord)msg.obj); |
| 865 | break; |
| 866 | } |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | |
| 871 | // Notifications |
| 872 | // ============================================================================ |
| Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 873 | public void enqueueNotificationWithTag(String pkg, String tag, int id, Notification notification, |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 874 | int[] idOut, int userId) |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 875 | { |
| Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 876 | enqueueNotificationInternal(pkg, Binder.getCallingUid(), Binder.getCallingPid(), |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 877 | tag, id, notification, idOut, userId); |
| Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 878 | } |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 879 | |
| 880 | private final static int clamp(int x, int low, int high) { |
| 881 | return (x < low) ? low : ((x > high) ? high : x); |
| Daniel Sandler | e40451a | 2011-02-03 14:51:35 -0500 | [diff] [blame] | 882 | } |
| 883 | |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 884 | |
| Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 885 | // Not exposed via Binder; for system use only (otherwise malicious apps could spoof the |
| 886 | // uid/pid of another application) |
| 887 | public void enqueueNotificationInternal(String pkg, int callingUid, int callingPid, |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 888 | String tag, int id, Notification notification, int[] idOut, int userId) |
| Daniel Sandler | d0a2f86 | 2010-08-03 15:29:31 -0400 | [diff] [blame] | 889 | { |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 890 | if (DBG) { |
| 891 | Slog.v(TAG, "enqueueNotificationInternal: pkg=" + pkg + " id=" + id + " notification=" + notification); |
| 892 | } |
| 893 | checkCallerIsSystemOrSameApp(pkg); |
| 894 | final boolean isSystemNotification = ("android".equals(pkg)); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 895 | |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 896 | userId = ActivityManager.handleIncomingUser(callingPid, |
| 897 | callingUid, userId, false, true, "enqueueNotification", pkg); |
| 898 | |
| Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 899 | // Limit the number of notifications that any given package except the android |
| 900 | // package can enqueue. Prevents DOS attacks and deals with leaks. |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 901 | if (!isSystemNotification) { |
| Joe Onorato | bd73d01 | 2010-06-04 11:44:54 -0700 | [diff] [blame] | 902 | synchronized (mNotificationList) { |
| 903 | int count = 0; |
| 904 | final int N = mNotificationList.size(); |
| 905 | for (int i=0; i<N; i++) { |
| 906 | final NotificationRecord r = mNotificationList.get(i); |
| 907 | if (r.pkg.equals(pkg)) { |
| 908 | count++; |
| 909 | if (count >= MAX_PACKAGE_NOTIFICATIONS) { |
| 910 | Slog.e(TAG, "Package has already posted " + count |
| 911 | + " notifications. Not showing more. package=" + pkg); |
| 912 | return; |
| 913 | } |
| 914 | } |
| 915 | } |
| 916 | } |
| 917 | } |
| 918 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 919 | // This conditional is a dirty hack to limit the logging done on |
| 920 | // behalf of the download manager without affecting other apps. |
| 921 | if (!pkg.equals("com.android.providers.downloads") |
| 922 | || Log.isLoggable("DownloadManager", Log.VERBOSE)) { |
| Daniel Sandler | b64cb88 | 2011-11-29 23:48:29 -0500 | [diff] [blame] | 923 | EventLog.writeEvent(EventLogTags.NOTIFICATION_ENQUEUE, pkg, id, tag, |
| 924 | notification.toString()); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 925 | } |
| 926 | |
| 927 | if (pkg == null || notification == null) { |
| 928 | throw new IllegalArgumentException("null not allowed: pkg=" + pkg |
| 929 | + " id=" + id + " notification=" + notification); |
| 930 | } |
| 931 | if (notification.icon != 0) { |
| 932 | if (notification.contentView == null) { |
| 933 | throw new IllegalArgumentException("contentView required: pkg=" + pkg |
| 934 | + " id=" + id + " notification=" + notification); |
| 935 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 936 | } |
| 937 | |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 938 | // === Scoring === |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 939 | |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 940 | // 0. Sanitize inputs |
| 941 | notification.priority = clamp(notification.priority, Notification.PRIORITY_MIN, Notification.PRIORITY_MAX); |
| 942 | // Migrate notification flags to scores |
| 943 | if (0 != (notification.flags & Notification.FLAG_HIGH_PRIORITY)) { |
| 944 | if (notification.priority < Notification.PRIORITY_MAX) notification.priority = Notification.PRIORITY_MAX; |
| Daniel Sandler | 49a2ad1 | 2012-03-28 15:46:39 -0400 | [diff] [blame] | 945 | } else if (SCORE_ONGOING_HIGHER && 0 != (notification.flags & Notification.FLAG_ONGOING_EVENT)) { |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 946 | if (notification.priority < Notification.PRIORITY_HIGH) notification.priority = Notification.PRIORITY_HIGH; |
| 947 | } |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 948 | |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 949 | // 1. initial score: buckets of 10, around the app |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 950 | int score = notification.priority * NOTIFICATION_PRIORITY_MULTIPLIER; //[-20..20] |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 951 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 952 | // 2. Consult external heuristics (TBD) |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 953 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 954 | // 3. Apply local rules |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 955 | |
| 956 | // blocked apps |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 957 | if (ENABLE_BLOCKED_NOTIFICATIONS && !isSystemNotification && !areNotificationsEnabledForPackageInt(pkg)) { |
| 958 | score = JUNK_SCORE; |
| 959 | Slog.e(TAG, "Suppressing notification from package " + pkg + " by user request."); |
| 960 | } |
| 961 | |
| 962 | if (DBG) { |
| 963 | Slog.v(TAG, "Assigned score=" + score + " to " + notification); |
| 964 | } |
| 965 | |
| 966 | if (score < SCORE_DISPLAY_THRESHOLD) { |
| 967 | // Notification will be blocked because the score is too low. |
| 968 | return; |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 969 | } |
| 970 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 971 | synchronized (mNotificationList) { |
| Daniel Sandler | e40451a | 2011-02-03 14:51:35 -0500 | [diff] [blame] | 972 | NotificationRecord r = new NotificationRecord(pkg, tag, id, |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 973 | callingUid, callingPid, userId, |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 974 | score, |
| Daniel Sandler | e40451a | 2011-02-03 14:51:35 -0500 | [diff] [blame] | 975 | notification); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 976 | NotificationRecord old = null; |
| 977 | |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 978 | int index = indexOfNotificationLocked(pkg, tag, id, userId); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 979 | if (index < 0) { |
| 980 | mNotificationList.add(r); |
| 981 | } else { |
| 982 | old = mNotificationList.remove(index); |
| 983 | mNotificationList.add(index, r); |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 984 | // Make sure we don't lose the foreground service state. |
| 985 | if (old != null) { |
| 986 | notification.flags |= |
| 987 | old.notification.flags&Notification.FLAG_FOREGROUND_SERVICE; |
| 988 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 989 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 990 | |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 991 | // Ensure if this is a foreground service that the proper additional |
| 992 | // flags are set. |
| 993 | if ((notification.flags&Notification.FLAG_FOREGROUND_SERVICE) != 0) { |
| 994 | notification.flags |= Notification.FLAG_ONGOING_EVENT |
| 995 | | Notification.FLAG_NO_CLEAR; |
| 996 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 997 | |
| John Spurlock | 7be1a3d | 2012-08-13 16:45:12 -0400 | [diff] [blame] | 998 | // Stop screensaver if the notification has a full-screen intent. |
| 999 | // (like an incoming phone call) |
| 1000 | if (notification.fullScreenIntent != null && mSandman != null) { |
| 1001 | try { |
| 1002 | mSandman.awaken(); |
| 1003 | } catch (RemoteException e) { |
| 1004 | // noop |
| 1005 | } |
| 1006 | } |
| 1007 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1008 | if (notification.icon != 0) { |
| Dianne Hackborn | 9d39d0c | 2010-06-24 15:57:42 -0700 | [diff] [blame] | 1009 | StatusBarNotification n = new StatusBarNotification(pkg, id, tag, |
| Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1010 | r.uid, r.initialPid, score, notification); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1011 | if (old != null && old.statusBarKey != null) { |
| 1012 | r.statusBarKey = old.statusBarKey; |
| 1013 | long identity = Binder.clearCallingIdentity(); |
| 1014 | try { |
| Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 1015 | mStatusBar.updateNotification(r.statusBarKey, n); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1016 | } |
| 1017 | finally { |
| 1018 | Binder.restoreCallingIdentity(identity); |
| 1019 | } |
| 1020 | } else { |
| 1021 | long identity = Binder.clearCallingIdentity(); |
| 1022 | try { |
| Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 1023 | r.statusBarKey = mStatusBar.addNotification(n); |
| Mike Lockwood | ece18ef | 2012-02-13 20:42:19 -0800 | [diff] [blame] | 1024 | if ((n.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) { |
| 1025 | mAttentionLight.pulse(); |
| 1026 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1027 | } |
| 1028 | finally { |
| 1029 | Binder.restoreCallingIdentity(identity); |
| 1030 | } |
| 1031 | } |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 1032 | sendAccessibilityEvent(notification, pkg); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1033 | } else { |
| Daniel Sandler | e40451a | 2011-02-03 14:51:35 -0500 | [diff] [blame] | 1034 | Slog.e(TAG, "Ignoring notification with icon==0: " + notification); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1035 | if (old != null && old.statusBarKey != null) { |
| 1036 | long identity = Binder.clearCallingIdentity(); |
| 1037 | try { |
| Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 1038 | mStatusBar.removeNotification(old.statusBarKey); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1039 | } |
| 1040 | finally { |
| 1041 | Binder.restoreCallingIdentity(identity); |
| 1042 | } |
| 1043 | } |
| 1044 | } |
| 1045 | |
| 1046 | // If we're not supposed to beep, vibrate, etc. then don't. |
| 1047 | if (((mDisabledNotifications & StatusBarManager.DISABLE_NOTIFICATION_ALERTS) == 0) |
| 1048 | && (!(old != null |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 1049 | && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0 )) |
| Daniel Sandler | 24518e4 | 2012-09-04 13:23:01 -0400 | [diff] [blame^] | 1050 | && (r.userId == UserHandle.USER_ALL || r.userId == userId) |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 1051 | && mSystemReady) { |
| Eric Laurent | 524dc04 | 2009-11-27 05:07:55 -0800 | [diff] [blame] | 1052 | |
| 1053 | final AudioManager audioManager = (AudioManager) mContext |
| 1054 | .getSystemService(Context.AUDIO_SERVICE); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1055 | // sound |
| 1056 | final boolean useDefaultSound = |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1057 | (notification.defaults & Notification.DEFAULT_SOUND) != 0; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1058 | if (useDefaultSound || notification.sound != null) { |
| 1059 | Uri uri; |
| 1060 | if (useDefaultSound) { |
| 1061 | uri = Settings.System.DEFAULT_NOTIFICATION_URI; |
| 1062 | } else { |
| 1063 | uri = notification.sound; |
| 1064 | } |
| 1065 | boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0; |
| 1066 | int audioStreamType; |
| 1067 | if (notification.audioStreamType >= 0) { |
| 1068 | audioStreamType = notification.audioStreamType; |
| 1069 | } else { |
| 1070 | audioStreamType = DEFAULT_STREAM_TYPE; |
| 1071 | } |
| 1072 | mSoundNotification = r; |
| Eric Laurent | 524dc04 | 2009-11-27 05:07:55 -0800 | [diff] [blame] | 1073 | // do not play notifications if stream volume is 0 |
| 1074 | // (typically because ringer mode is silent). |
| 1075 | if (audioManager.getStreamVolume(audioStreamType) != 0) { |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1076 | final long identity = Binder.clearCallingIdentity(); |
| Eric Laurent | 524dc04 | 2009-11-27 05:07:55 -0800 | [diff] [blame] | 1077 | try { |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1078 | final IRingtonePlayer player = mAudioService.getRingtonePlayer(); |
| 1079 | if (player != null) { |
| 1080 | player.playAsync(uri, looping, audioStreamType); |
| 1081 | } |
| 1082 | } catch (RemoteException e) { |
| 1083 | } finally { |
| Eric Laurent | 524dc04 | 2009-11-27 05:07:55 -0800 | [diff] [blame] | 1084 | Binder.restoreCallingIdentity(identity); |
| 1085 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | // vibrate |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1090 | final boolean useDefaultVibrate = |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1091 | (notification.defaults & Notification.DEFAULT_VIBRATE) != 0; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1092 | if ((useDefaultVibrate || notification.vibrate != null) |
| Eric Laurent | bffc3d1 | 2012-05-07 17:43:49 -0700 | [diff] [blame] | 1093 | && !(audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1094 | mVibrateNotification = r; |
| 1095 | |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1096 | mVibrator.vibrate(useDefaultVibrate ? DEFAULT_VIBRATE_PATTERN |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1097 | : notification.vibrate, |
| 1098 | ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0: -1); |
| 1099 | } |
| 1100 | } |
| 1101 | |
| 1102 | // this option doesn't shut off the lights |
| 1103 | |
| 1104 | // light |
| 1105 | // the most recent thing gets the light |
| 1106 | mLights.remove(old); |
| 1107 | if (mLedNotification == old) { |
| 1108 | mLedNotification = null; |
| 1109 | } |
| Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 1110 | //Slog.i(TAG, "notification.lights=" |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1111 | // + ((old.notification.lights.flags & Notification.FLAG_SHOW_LIGHTS) != 0)); |
| 1112 | if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0) { |
| 1113 | mLights.add(r); |
| 1114 | updateLightsLocked(); |
| 1115 | } else { |
| 1116 | if (old != null |
| 1117 | && ((old.notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0)) { |
| 1118 | updateLightsLocked(); |
| 1119 | } |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | idOut[0] = id; |
| 1124 | } |
| 1125 | |
| Joe Onorato | 3027548 | 2009-07-08 17:09:14 -0700 | [diff] [blame] | 1126 | private void sendAccessibilityEvent(Notification notification, CharSequence packageName) { |
| svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1127 | AccessibilityManager manager = AccessibilityManager.getInstance(mContext); |
| 1128 | if (!manager.isEnabled()) { |
| 1129 | return; |
| 1130 | } |
| 1131 | |
| 1132 | AccessibilityEvent event = |
| 1133 | AccessibilityEvent.obtain(AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED); |
| 1134 | event.setPackageName(packageName); |
| 1135 | event.setClassName(Notification.class.getName()); |
| 1136 | event.setParcelableData(notification); |
| 1137 | CharSequence tickerText = notification.tickerText; |
| 1138 | if (!TextUtils.isEmpty(tickerText)) { |
| 1139 | event.getText().add(tickerText); |
| 1140 | } |
| 1141 | |
| 1142 | manager.sendAccessibilityEvent(event); |
| 1143 | } |
| 1144 | |
| Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1145 | private void cancelNotificationLocked(NotificationRecord r, boolean sendDelete) { |
| 1146 | // tell the app |
| 1147 | if (sendDelete) { |
| 1148 | if (r.notification.deleteIntent != null) { |
| 1149 | try { |
| 1150 | r.notification.deleteIntent.send(); |
| 1151 | } catch (PendingIntent.CanceledException ex) { |
| 1152 | // do nothing - there's no relevant way to recover, and |
| 1153 | // no reason to let this propagate |
| 1154 | Slog.w(TAG, "canceled PendingIntent for " + r.pkg, ex); |
| 1155 | } |
| 1156 | } |
| 1157 | } |
| 1158 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1159 | // status bar |
| 1160 | if (r.notification.icon != 0) { |
| 1161 | long identity = Binder.clearCallingIdentity(); |
| 1162 | try { |
| Joe Onorato | 0cbda99 | 2010-05-02 16:28:15 -0700 | [diff] [blame] | 1163 | mStatusBar.removeNotification(r.statusBarKey); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1164 | } |
| 1165 | finally { |
| 1166 | Binder.restoreCallingIdentity(identity); |
| 1167 | } |
| 1168 | r.statusBarKey = null; |
| 1169 | } |
| 1170 | |
| 1171 | // sound |
| 1172 | if (mSoundNotification == r) { |
| 1173 | mSoundNotification = null; |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1174 | final long identity = Binder.clearCallingIdentity(); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1175 | try { |
| Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 1176 | final IRingtonePlayer player = mAudioService.getRingtonePlayer(); |
| 1177 | if (player != null) { |
| 1178 | player.stopAsync(); |
| 1179 | } |
| 1180 | } catch (RemoteException e) { |
| 1181 | } finally { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1182 | Binder.restoreCallingIdentity(identity); |
| 1183 | } |
| 1184 | } |
| 1185 | |
| 1186 | // vibrate |
| 1187 | if (mVibrateNotification == r) { |
| 1188 | mVibrateNotification = null; |
| 1189 | long identity = Binder.clearCallingIdentity(); |
| 1190 | try { |
| 1191 | mVibrator.cancel(); |
| 1192 | } |
| 1193 | finally { |
| 1194 | Binder.restoreCallingIdentity(identity); |
| 1195 | } |
| 1196 | } |
| 1197 | |
| 1198 | // light |
| 1199 | mLights.remove(r); |
| 1200 | if (mLedNotification == r) { |
| 1201 | mLedNotification = null; |
| 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | /** |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1206 | * Cancels a notification ONLY if it has all of the {@code mustHaveFlags} |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1207 | * and none of the {@code mustNotHaveFlags}. |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1208 | */ |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 1209 | private void cancelNotification(String pkg, String tag, int id, int mustHaveFlags, |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1210 | int mustNotHaveFlags, boolean sendDelete, int userId) { |
| Daniel Sandler | b64cb88 | 2011-11-29 23:48:29 -0500 | [diff] [blame] | 1211 | EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL, pkg, id, tag, |
| 1212 | mustHaveFlags, mustNotHaveFlags); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1213 | |
| 1214 | synchronized (mNotificationList) { |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1215 | int index = indexOfNotificationLocked(pkg, tag, id, userId); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1216 | if (index >= 0) { |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 1217 | NotificationRecord r = mNotificationList.get(index); |
| 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 | if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) { |
| 1220 | return; |
| 1221 | } |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1222 | if ((r.notification.flags & mustNotHaveFlags) != 0) { |
| 1223 | return; |
| 1224 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1225 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1226 | mNotificationList.remove(index); |
| 1227 | |
| Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1228 | cancelNotificationLocked(r, sendDelete); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1229 | updateLightsLocked(); |
| 1230 | } |
| 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | /** |
| 1235 | * Cancels all notifications from a given package that have all of the |
| 1236 | * {@code mustHaveFlags}. |
| 1237 | */ |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1238 | boolean cancelAllNotificationsInt(String pkg, int mustHaveFlags, |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1239 | int mustNotHaveFlags, boolean doit, int userId) { |
| Daniel Sandler | b64cb88 | 2011-11-29 23:48:29 -0500 | [diff] [blame] | 1240 | EventLog.writeEvent(EventLogTags.NOTIFICATION_CANCEL_ALL, pkg, mustHaveFlags, |
| 1241 | mustNotHaveFlags); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1242 | |
| 1243 | synchronized (mNotificationList) { |
| 1244 | final int N = mNotificationList.size(); |
| 1245 | boolean canceledSomething = false; |
| 1246 | for (int i = N-1; i >= 0; --i) { |
| 1247 | NotificationRecord r = mNotificationList.get(i); |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1248 | if (userId != UserHandle.USER_ALL && r.userId != userId) { |
| 1249 | continue; |
| 1250 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1251 | if ((r.notification.flags & mustHaveFlags) != mustHaveFlags) { |
| 1252 | continue; |
| 1253 | } |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1254 | if ((r.notification.flags & mustNotHaveFlags) != 0) { |
| 1255 | continue; |
| 1256 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1257 | if (!r.pkg.equals(pkg)) { |
| 1258 | continue; |
| 1259 | } |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1260 | canceledSomething = true; |
| 1261 | if (!doit) { |
| 1262 | return true; |
| 1263 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1264 | mNotificationList.remove(i); |
| Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1265 | cancelNotificationLocked(r, false); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1266 | } |
| 1267 | if (canceledSomething) { |
| 1268 | updateLightsLocked(); |
| 1269 | } |
| Dianne Hackborn | 21f1bd1 | 2010-02-19 17:02:21 -0800 | [diff] [blame] | 1270 | return canceledSomething; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1271 | } |
| 1272 | } |
| 1273 | |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1274 | public void cancelNotificationWithTag(String pkg, String tag, int id, int userId) { |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1275 | checkCallerIsSystemOrSameApp(pkg); |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1276 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 1277 | Binder.getCallingUid(), userId, false, true, "cancelNotificationWithTag", pkg); |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1278 | // Don't allow client applications to cancel foreground service notis. |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 1279 | cancelNotification(pkg, tag, id, 0, |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1280 | Binder.getCallingUid() == Process.SYSTEM_UID |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1281 | ? 0 : Notification.FLAG_FOREGROUND_SERVICE, false, userId); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1282 | } |
| 1283 | |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1284 | public void cancelAllNotifications(String pkg, int userId) { |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1285 | checkCallerIsSystemOrSameApp(pkg); |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1286 | |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1287 | userId = ActivityManager.handleIncomingUser(Binder.getCallingPid(), |
| 1288 | Binder.getCallingUid(), userId, true, true, "cancelAllNotifications", pkg); |
| 1289 | |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1290 | // Calling from user space, don't allow the canceling of actively |
| 1291 | // running foreground services. |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1292 | cancelAllNotificationsInt(pkg, 0, Notification.FLAG_FOREGROUND_SERVICE, true, userId); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1293 | } |
| 1294 | |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1295 | void checkCallerIsSystem() { |
| 1296 | int uid = Binder.getCallingUid(); |
| Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 1297 | if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) { |
| Daniel Sandler | 0da673f | 2012-04-11 12:33:16 -0400 | [diff] [blame] | 1298 | return; |
| 1299 | } |
| 1300 | throw new SecurityException("Disallowed call for uid " + uid); |
| 1301 | } |
| 1302 | |
| 1303 | void checkCallerIsSystemOrSameApp(String pkg) { |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1304 | int uid = Binder.getCallingUid(); |
| Dianne Hackborn | 0c38049 | 2012-08-20 17:23:30 -0700 | [diff] [blame] | 1305 | if (UserHandle.getAppId(uid) == Process.SYSTEM_UID || uid == 0) { |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1306 | return; |
| 1307 | } |
| 1308 | try { |
| Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 1309 | ApplicationInfo ai = AppGlobals.getPackageManager().getApplicationInfo( |
| 1310 | pkg, 0, UserHandle.getCallingUserId()); |
| Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1311 | if (!UserHandle.isSameApp(ai.uid, uid)) { |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1312 | throw new SecurityException("Calling uid " + uid + " gave package" |
| 1313 | + pkg + " which is owned by uid " + ai.uid); |
| 1314 | } |
| Amith Yamasani | f203aee | 2012-08-29 18:41:53 -0700 | [diff] [blame] | 1315 | } catch (RemoteException re) { |
| 1316 | throw new SecurityException("Unknown package " + pkg + "\n" + re); |
| Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 1317 | } |
| 1318 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1319 | |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1320 | void cancelAll(int userId) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1321 | synchronized (mNotificationList) { |
| 1322 | final int N = mNotificationList.size(); |
| 1323 | for (int i=N-1; i>=0; i--) { |
| 1324 | NotificationRecord r = mNotificationList.get(i); |
| 1325 | |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1326 | if (r.userId != userId) { |
| 1327 | continue; |
| 1328 | } |
| 1329 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1330 | if ((r.notification.flags & (Notification.FLAG_ONGOING_EVENT |
| 1331 | | Notification.FLAG_NO_CLEAR)) == 0) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1332 | mNotificationList.remove(i); |
| Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1333 | cancelNotificationLocked(r, true); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | updateLightsLocked(); |
| 1338 | } |
| 1339 | } |
| 1340 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1341 | // lock on mNotificationList |
| 1342 | private void updateLightsLocked() |
| 1343 | { |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1344 | // handle notification lights |
| 1345 | if (mLedNotification == null) { |
| 1346 | // get next notification, if any |
| 1347 | int n = mLights.size(); |
| 1348 | if (n > 0) { |
| 1349 | mLedNotification = mLights.get(n-1); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1350 | } |
| 1351 | } |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 1352 | |
| Mike Lockwood | 63b5ad9 | 2011-08-30 09:55:30 -0400 | [diff] [blame] | 1353 | // Don't flash while we are in a call or screen is on |
| 1354 | if (mLedNotification == null || mInCall || mScreenOn) { |
| Mike Lockwood | 3cb67a3 | 2009-11-27 14:25:58 -0500 | [diff] [blame] | 1355 | mNotificationLight.turnOff(); |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1356 | } else { |
| Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 1357 | int ledARGB = mLedNotification.notification.ledARGB; |
| 1358 | int ledOnMS = mLedNotification.notification.ledOnMS; |
| 1359 | int ledOffMS = mLedNotification.notification.ledOffMS; |
| 1360 | if ((mLedNotification.notification.defaults & Notification.DEFAULT_LIGHTS) != 0) { |
| 1361 | ledARGB = mDefaultNotificationColor; |
| 1362 | ledOnMS = mDefaultNotificationLedOn; |
| 1363 | ledOffMS = mDefaultNotificationLedOff; |
| 1364 | } |
| 1365 | if (mNotificationPulseEnabled) { |
| 1366 | // pulse repeatedly |
| 1367 | mNotificationLight.setFlashing(ledARGB, LightsService.LIGHT_FLASH_TIMED, |
| 1368 | ledOnMS, ledOffMS); |
| Mike Lockwood | 670f932 | 2010-01-20 12:13:36 -0500 | [diff] [blame] | 1369 | } |
| The Android Open Source Project | 1059253 | 2009-03-18 17:39:46 -0700 | [diff] [blame] | 1370 | } |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1371 | } |
| 1372 | |
| 1373 | // lock on mNotificationList |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1374 | private int indexOfNotificationLocked(String pkg, String tag, int id, int userId) |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1375 | { |
| 1376 | ArrayList<NotificationRecord> list = mNotificationList; |
| 1377 | final int len = list.size(); |
| 1378 | for (int i=0; i<len; i++) { |
| 1379 | NotificationRecord r = list.get(i); |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1380 | if (r.userId != userId || r.id != id) { |
| 1381 | continue; |
| 1382 | } |
| Fred Quintana | 6ecaff1 | 2009-09-25 14:23:13 -0700 | [diff] [blame] | 1383 | if (tag == null) { |
| 1384 | if (r.tag != null) { |
| 1385 | continue; |
| 1386 | } |
| 1387 | } else { |
| 1388 | if (!tag.equals(r.tag)) { |
| 1389 | continue; |
| 1390 | } |
| 1391 | } |
| Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 1392 | if (r.pkg.equals(pkg)) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1393 | return i; |
| 1394 | } |
| 1395 | } |
| 1396 | return -1; |
| 1397 | } |
| 1398 | |
| Mike Lockwood | c22404a | 2009-12-02 11:15:02 -0500 | [diff] [blame] | 1399 | private void updateNotificationPulse() { |
| 1400 | synchronized (mNotificationList) { |
| 1401 | updateLightsLocked(); |
| 1402 | } |
| 1403 | } |
| 1404 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1405 | // ====================================================================== |
| 1406 | @Override |
| 1407 | protected void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 1408 | if (mContext.checkCallingOrSelfPermission(android.Manifest.permission.DUMP) |
| 1409 | != PackageManager.PERMISSION_GRANTED) { |
| 1410 | pw.println("Permission Denial: can't dump NotificationManager from from pid=" |
| 1411 | + Binder.getCallingPid() |
| 1412 | + ", uid=" + Binder.getCallingUid()); |
| 1413 | return; |
| 1414 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1415 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1416 | pw.println("Current Notification Manager state:"); |
| 1417 | |
| 1418 | int N; |
| 1419 | |
| 1420 | synchronized (mToastQueue) { |
| 1421 | N = mToastQueue.size(); |
| 1422 | if (N > 0) { |
| 1423 | pw.println(" Toast Queue:"); |
| 1424 | for (int i=0; i<N; i++) { |
| 1425 | mToastQueue.get(i).dump(pw, " "); |
| 1426 | } |
| 1427 | pw.println(" "); |
| 1428 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1429 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | synchronized (mNotificationList) { |
| 1433 | N = mNotificationList.size(); |
| 1434 | if (N > 0) { |
| 1435 | pw.println(" Notification List:"); |
| 1436 | for (int i=0; i<N; i++) { |
| 1437 | mNotificationList.get(i).dump(pw, " ", mContext); |
| 1438 | } |
| 1439 | pw.println(" "); |
| 1440 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1441 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1442 | N = mLights.size(); |
| 1443 | if (N > 0) { |
| 1444 | pw.println(" Lights List:"); |
| 1445 | for (int i=0; i<N; i++) { |
| 1446 | mLights.get(i).dump(pw, " ", mContext); |
| 1447 | } |
| 1448 | pw.println(" "); |
| 1449 | } |
| Doug Zongker | ab5c49c | 2009-12-04 10:31:43 -0800 | [diff] [blame] | 1450 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1451 | pw.println(" mSoundNotification=" + mSoundNotification); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1452 | pw.println(" mVibrateNotification=" + mVibrateNotification); |
| Joe Onorato | 39f5b6a | 2009-07-23 12:29:19 -0400 | [diff] [blame] | 1453 | pw.println(" mDisabledNotifications=0x" + Integer.toHexString(mDisabledNotifications)); |
| 1454 | pw.println(" mSystemReady=" + mSystemReady); |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1455 | } |
| 1456 | } |
| 1457 | } |