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