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