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