Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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.am; |
| 18 | |
Dianne Hackborn | b8633f3 | 2017-04-11 17:38:42 -0700 | [diff] [blame] | 19 | import android.content.pm.IPackageManager; |
| 20 | import android.content.pm.PermissionInfo; |
Carmen Jackson | a68e345 | 2017-01-17 14:01:33 -0800 | [diff] [blame] | 21 | import android.os.Trace; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 22 | import java.io.FileDescriptor; |
| 23 | import java.io.PrintWriter; |
Christopher Tate | f278f12 | 2015-04-22 13:12:01 -0700 | [diff] [blame] | 24 | import java.text.SimpleDateFormat; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 25 | import java.util.ArrayList; |
Christopher Tate | f278f12 | 2015-04-22 13:12:01 -0700 | [diff] [blame] | 26 | import java.util.Date; |
Wale Ogunwale | ca1c125 | 2015-05-15 12:49:13 -0700 | [diff] [blame] | 27 | import java.util.Set; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 28 | |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 29 | import android.app.ActivityManager; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 30 | import android.app.AppGlobals; |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 31 | import android.app.AppOpsManager; |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 32 | import android.app.BroadcastOptions; |
Svet Ganov | 9c165d7 | 2015-12-01 19:52:26 -0800 | [diff] [blame] | 33 | import android.app.PendingIntent; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 34 | import android.content.ComponentName; |
| 35 | import android.content.IIntentReceiver; |
Svet Ganov | 9c165d7 | 2015-12-01 19:52:26 -0800 | [diff] [blame] | 36 | import android.content.IIntentSender; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 37 | import android.content.Intent; |
Svet Ganov | 9c165d7 | 2015-12-01 19:52:26 -0800 | [diff] [blame] | 38 | import android.content.IntentSender; |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 39 | import android.content.pm.ActivityInfo; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 40 | import android.content.pm.PackageManager; |
| 41 | import android.content.pm.ResolveInfo; |
| 42 | import android.os.Bundle; |
| 43 | import android.os.Handler; |
| 44 | import android.os.IBinder; |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 45 | import android.os.Looper; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 46 | import android.os.Message; |
| 47 | import android.os.Process; |
| 48 | import android.os.RemoteException; |
| 49 | import android.os.SystemClock; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 50 | import android.os.UserHandle; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 51 | import android.util.EventLog; |
| 52 | import android.util.Slog; |
Dianne Hackborn | 865907d | 2015-10-21 17:12:53 -0700 | [diff] [blame] | 53 | import android.util.TimeUtils; |
Yi Jin | 129fc6c | 2017-09-28 15:48:38 -0700 | [diff] [blame] | 54 | import android.util.proto.ProtoOutputStream; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 55 | |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 56 | import static com.android.server.am.ActivityManagerDebugConfig.*; |
| 57 | |
Yi Jin | 129fc6c | 2017-09-28 15:48:38 -0700 | [diff] [blame] | 58 | import com.android.server.am.proto.BroadcastQueueProto; |
| 59 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 60 | /** |
| 61 | * BROADCASTS |
| 62 | * |
| 63 | * We keep two broadcast queues and associated bookkeeping, one for those at |
| 64 | * foreground priority, and one for normal (background-priority) broadcasts. |
| 65 | */ |
Dianne Hackborn | be4e6aa | 2013-06-07 13:25:29 -0700 | [diff] [blame] | 66 | public final class BroadcastQueue { |
Wale Ogunwale | bfac468 | 2015-04-08 14:33:21 -0700 | [diff] [blame] | 67 | private static final String TAG = "BroadcastQueue"; |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 68 | private static final String TAG_MU = TAG + POSTFIX_MU; |
| 69 | private static final String TAG_BROADCAST = TAG + POSTFIX_BROADCAST; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 70 | |
Dianne Hackborn | 4c51de4 | 2013-10-16 23:34:35 -0700 | [diff] [blame] | 71 | static final int MAX_BROADCAST_HISTORY = ActivityManager.isLowRamDeviceStatic() ? 10 : 50; |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 72 | static final int MAX_BROADCAST_SUMMARY_HISTORY |
Dianne Hackborn | 4c51de4 | 2013-10-16 23:34:35 -0700 | [diff] [blame] | 73 | = ActivityManager.isLowRamDeviceStatic() ? 25 : 300; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 74 | |
| 75 | final ActivityManagerService mService; |
| 76 | |
| 77 | /** |
| 78 | * Recognizable moniker for this queue |
| 79 | */ |
| 80 | final String mQueueName; |
| 81 | |
| 82 | /** |
| 83 | * Timeout period for this queue's broadcasts |
| 84 | */ |
| 85 | final long mTimeoutPeriod; |
| 86 | |
| 87 | /** |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 88 | * If true, we can delay broadcasts while waiting services to finish in the previous |
| 89 | * receiver's process. |
| 90 | */ |
| 91 | final boolean mDelayBehindServices; |
| 92 | |
| 93 | /** |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 94 | * Lists of all active broadcasts that are to be executed immediately |
| 95 | * (without waiting for another broadcast to finish). Currently this only |
| 96 | * contains broadcasts to registered receivers, to avoid spinning up |
| 97 | * a bunch of processes to execute IntentReceiver components. Background- |
| 98 | * and foreground-priority broadcasts are queued separately. |
| 99 | */ |
Wale Ogunwale | 540e123 | 2015-05-01 15:35:39 -0700 | [diff] [blame] | 100 | final ArrayList<BroadcastRecord> mParallelBroadcasts = new ArrayList<>(); |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 101 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 102 | /** |
| 103 | * List of all active broadcasts that are to be executed one at a time. |
| 104 | * The object at the top of the list is the currently activity broadcasts; |
| 105 | * those after it are waiting for the top to finish. As with parallel |
| 106 | * broadcasts, separate background- and foreground-priority queues are |
| 107 | * maintained. |
| 108 | */ |
Wale Ogunwale | 540e123 | 2015-05-01 15:35:39 -0700 | [diff] [blame] | 109 | final ArrayList<BroadcastRecord> mOrderedBroadcasts = new ArrayList<>(); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 110 | |
| 111 | /** |
Christopher Tate | f278f12 | 2015-04-22 13:12:01 -0700 | [diff] [blame] | 112 | * Historical data of past broadcasts, for debugging. This is a ring buffer |
| 113 | * whose last element is at mHistoryNext. |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 114 | */ |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 115 | final BroadcastRecord[] mBroadcastHistory = new BroadcastRecord[MAX_BROADCAST_HISTORY]; |
Christopher Tate | f278f12 | 2015-04-22 13:12:01 -0700 | [diff] [blame] | 116 | int mHistoryNext = 0; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 117 | |
| 118 | /** |
Christopher Tate | f278f12 | 2015-04-22 13:12:01 -0700 | [diff] [blame] | 119 | * Summary of historical data of past broadcasts, for debugging. This is a |
| 120 | * ring buffer whose last element is at mSummaryHistoryNext. |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 121 | */ |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 122 | final Intent[] mBroadcastSummaryHistory = new Intent[MAX_BROADCAST_SUMMARY_HISTORY]; |
Christopher Tate | f278f12 | 2015-04-22 13:12:01 -0700 | [diff] [blame] | 123 | int mSummaryHistoryNext = 0; |
| 124 | |
| 125 | /** |
| 126 | * Various milestone timestamps of entries in the mBroadcastSummaryHistory ring |
| 127 | * buffer, also tracked via the mSummaryHistoryNext index. These are all in wall |
| 128 | * clock time, not elapsed. |
| 129 | */ |
| 130 | final long[] mSummaryHistoryEnqueueTime = new long[MAX_BROADCAST_SUMMARY_HISTORY]; |
| 131 | final long[] mSummaryHistoryDispatchTime = new long[MAX_BROADCAST_SUMMARY_HISTORY]; |
| 132 | final long[] mSummaryHistoryFinishTime = new long[MAX_BROADCAST_SUMMARY_HISTORY]; |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 133 | |
| 134 | /** |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 135 | * Set when we current have a BROADCAST_INTENT_MSG in flight. |
| 136 | */ |
| 137 | boolean mBroadcastsScheduled = false; |
| 138 | |
| 139 | /** |
| 140 | * True if we have a pending unexpired BROADCAST_TIMEOUT_MSG posted to our handler. |
| 141 | */ |
| 142 | boolean mPendingBroadcastTimeoutMessage; |
| 143 | |
| 144 | /** |
| 145 | * Intent broadcasts that we have tried to start, but are |
| 146 | * waiting for the application's process to be created. We only |
| 147 | * need one per scheduling class (instead of a list) because we always |
| 148 | * process broadcasts one at a time, so no others can be started while |
| 149 | * waiting for this one. |
| 150 | */ |
| 151 | BroadcastRecord mPendingBroadcast = null; |
| 152 | |
| 153 | /** |
| 154 | * The receiver index that is pending, to restart the broadcast if needed. |
| 155 | */ |
| 156 | int mPendingBroadcastRecvIndex; |
| 157 | |
| 158 | static final int BROADCAST_INTENT_MSG = ActivityManagerService.FIRST_BROADCAST_QUEUE_MSG; |
| 159 | static final int BROADCAST_TIMEOUT_MSG = ActivityManagerService.FIRST_BROADCAST_QUEUE_MSG + 1; |
| 160 | |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 161 | final BroadcastHandler mHandler; |
| 162 | |
| 163 | private final class BroadcastHandler extends Handler { |
| 164 | public BroadcastHandler(Looper looper) { |
| 165 | super(looper, null, true); |
| 166 | } |
| 167 | |
| 168 | @Override |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 169 | public void handleMessage(Message msg) { |
| 170 | switch (msg.what) { |
| 171 | case BROADCAST_INTENT_MSG: { |
| 172 | if (DEBUG_BROADCAST) Slog.v( |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 173 | TAG_BROADCAST, "Received BROADCAST_INTENT_MSG"); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 174 | processNextBroadcast(true); |
| 175 | } break; |
| 176 | case BROADCAST_TIMEOUT_MSG: { |
| 177 | synchronized (mService) { |
| 178 | broadcastTimeoutLocked(true); |
| 179 | } |
| 180 | } break; |
| 181 | } |
| 182 | } |
Svet Ganov | 9c165d7 | 2015-12-01 19:52:26 -0800 | [diff] [blame] | 183 | } |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 184 | |
| 185 | private final class AppNotResponding implements Runnable { |
| 186 | private final ProcessRecord mApp; |
| 187 | private final String mAnnotation; |
| 188 | |
| 189 | public AppNotResponding(ProcessRecord app, String annotation) { |
| 190 | mApp = app; |
| 191 | mAnnotation = annotation; |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public void run() { |
Adrian Roos | 20d7df3 | 2016-01-12 18:59:43 +0100 | [diff] [blame] | 196 | mService.mAppErrors.appNotResponding(mApp, null, null, false, mAnnotation); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 200 | BroadcastQueue(ActivityManagerService service, Handler handler, |
| 201 | String name, long timeoutPeriod, boolean allowDelayBehindServices) { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 202 | mService = service; |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 203 | mHandler = new BroadcastHandler(handler.getLooper()); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 204 | mQueueName = name; |
| 205 | mTimeoutPeriod = timeoutPeriod; |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 206 | mDelayBehindServices = allowDelayBehindServices; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Jeff Sharkey | fd65813 | 2017-05-03 11:38:01 -0600 | [diff] [blame] | 209 | @Override |
| 210 | public String toString() { |
| 211 | return mQueueName; |
| 212 | } |
| 213 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 214 | public boolean isPendingBroadcastProcessLocked(int pid) { |
| 215 | return mPendingBroadcast != null && mPendingBroadcast.curApp.pid == pid; |
| 216 | } |
| 217 | |
| 218 | public void enqueueParallelBroadcastLocked(BroadcastRecord r) { |
| 219 | mParallelBroadcasts.add(r); |
Carmen Jackson | a68e345 | 2017-01-17 14:01:33 -0800 | [diff] [blame] | 220 | enqueueBroadcastHelper(r); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | public void enqueueOrderedBroadcastLocked(BroadcastRecord r) { |
| 224 | mOrderedBroadcasts.add(r); |
Carmen Jackson | a68e345 | 2017-01-17 14:01:33 -0800 | [diff] [blame] | 225 | enqueueBroadcastHelper(r); |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Don't call this method directly; call enqueueParallelBroadcastLocked or |
| 230 | * enqueueOrderedBroadcastLocked. |
| 231 | */ |
| 232 | private void enqueueBroadcastHelper(BroadcastRecord r) { |
Jeff Brown | 9fb3fd1 | 2014-09-29 15:32:12 -0700 | [diff] [blame] | 233 | r.enqueueClockTime = System.currentTimeMillis(); |
Carmen Jackson | a68e345 | 2017-01-17 14:01:33 -0800 | [diff] [blame] | 234 | |
| 235 | if (Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER)) { |
| 236 | Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, |
| 237 | createBroadcastTraceTitle(r, BroadcastRecord.DELIVERY_PENDING), |
| 238 | System.identityHashCode(r)); |
| 239 | } |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 240 | } |
| 241 | |
Makoto Onuki | 3e7d845 | 2017-03-02 15:33:17 -0800 | [diff] [blame] | 242 | /** |
| 243 | * Find the same intent from queued parallel broadcast, replace with a new one and return |
| 244 | * the old one. |
| 245 | */ |
| 246 | public final BroadcastRecord replaceParallelBroadcastLocked(BroadcastRecord r) { |
| 247 | return replaceBroadcastLocked(mParallelBroadcasts, r, "PARALLEL"); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 248 | } |
| 249 | |
Makoto Onuki | 3e7d845 | 2017-03-02 15:33:17 -0800 | [diff] [blame] | 250 | /** |
| 251 | * Find the same intent from queued ordered broadcast, replace with a new one and return |
| 252 | * the old one. |
| 253 | */ |
| 254 | public final BroadcastRecord replaceOrderedBroadcastLocked(BroadcastRecord r) { |
| 255 | return replaceBroadcastLocked(mOrderedBroadcasts, r, "ORDERED"); |
| 256 | } |
| 257 | |
| 258 | private BroadcastRecord replaceBroadcastLocked(ArrayList<BroadcastRecord> queue, |
| 259 | BroadcastRecord r, String typeForLogging) { |
Erik Wolsheimer | 5d34f00 | 2016-07-28 13:51:39 -0700 | [diff] [blame] | 260 | final Intent intent = r.intent; |
Makoto Onuki | 3e7d845 | 2017-03-02 15:33:17 -0800 | [diff] [blame] | 261 | for (int i = queue.size() - 1; i > 0; i--) { |
| 262 | final BroadcastRecord old = queue.get(i); |
| 263 | if (old.userId == r.userId && intent.filterEquals(old.intent)) { |
| 264 | if (DEBUG_BROADCAST) { |
| 265 | Slog.v(TAG_BROADCAST, "***** DROPPING " |
| 266 | + typeForLogging + " [" + mQueueName + "]: " + intent); |
| 267 | } |
| 268 | queue.set(i, r); |
| 269 | return old; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 270 | } |
| 271 | } |
Makoto Onuki | 3e7d845 | 2017-03-02 15:33:17 -0800 | [diff] [blame] | 272 | return null; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | private final void processCurBroadcastLocked(BroadcastRecord r, |
| 276 | ProcessRecord app) throws RemoteException { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 277 | if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 278 | "Process cur broadcast " + r + " for app " + app); |
| 279 | if (app.thread == null) { |
| 280 | throw new RemoteException(); |
| 281 | } |
Amith Yamasani | b5f3b5c | 2016-05-16 14:23:04 -0700 | [diff] [blame] | 282 | if (app.inFullBackup) { |
| 283 | skipReceiverLocked(r); |
| 284 | return; |
| 285 | } |
| 286 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 287 | r.receiver = app.thread.asBinder(); |
| 288 | r.curApp = app; |
yangzhenyu | d509bc9 | 2016-08-31 18:26:46 +0800 | [diff] [blame] | 289 | app.curReceivers.add(r); |
Dianne Hackborn | a413dc0 | 2013-07-12 12:02:55 -0700 | [diff] [blame] | 290 | app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_RECEIVER); |
Dianne Hackborn | db92608 | 2013-10-31 16:32:44 -0700 | [diff] [blame] | 291 | mService.updateLruProcessLocked(app, false, null); |
| 292 | mService.updateOomAdjLocked(); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 293 | |
| 294 | // Tell the application to launch this receiver. |
| 295 | r.intent.setComponent(r.curComponent); |
| 296 | |
| 297 | boolean started = false; |
| 298 | try { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 299 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 300 | "Delivering to component " + r.curComponent |
| 301 | + ": " + r); |
Brian Carlstrom | ca82e61 | 2016-04-19 23:16:08 -0700 | [diff] [blame] | 302 | mService.notifyPackageUse(r.intent.getComponent().getPackageName(), |
| 303 | PackageManager.NOTIFY_PACKAGE_USE_BROADCAST_RECEIVER); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 304 | app.thread.scheduleReceiver(new Intent(r.intent), r.curReceiver, |
| 305 | mService.compatibilityInfoForPackageLocked(r.curReceiver.applicationInfo), |
Dianne Hackborn | a413dc0 | 2013-07-12 12:02:55 -0700 | [diff] [blame] | 306 | r.resultCode, r.resultData, r.resultExtras, r.ordered, r.userId, |
| 307 | app.repProcState); |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 308 | if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 309 | "Process cur broadcast " + r + " DELIVERED for app " + app); |
| 310 | started = true; |
| 311 | } finally { |
| 312 | if (!started) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 313 | if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 314 | "Process cur broadcast " + r + ": NOT STARTED!"); |
| 315 | r.receiver = null; |
| 316 | r.curApp = null; |
yangzhenyu | d509bc9 | 2016-08-31 18:26:46 +0800 | [diff] [blame] | 317 | app.curReceivers.remove(r); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | public boolean sendPendingBroadcastsLocked(ProcessRecord app) { |
| 323 | boolean didSomething = false; |
| 324 | final BroadcastRecord br = mPendingBroadcast; |
| 325 | if (br != null && br.curApp.pid == app.pid) { |
Amith Yamasani | d86e14e | 2016-08-05 15:25:03 -0700 | [diff] [blame] | 326 | if (br.curApp != app) { |
| 327 | Slog.e(TAG, "App mismatch when sending pending broadcast to " |
| 328 | + app.processName + ", intended target is " + br.curApp.processName); |
| 329 | return false; |
| 330 | } |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 331 | try { |
| 332 | mPendingBroadcast = null; |
| 333 | processCurBroadcastLocked(br, app); |
| 334 | didSomething = true; |
| 335 | } catch (Exception e) { |
| 336 | Slog.w(TAG, "Exception in new application when starting receiver " |
| 337 | + br.curComponent.flattenToShortString(), e); |
| 338 | logBroadcastReceiverDiscardLocked(br); |
| 339 | finishReceiverLocked(br, br.resultCode, br.resultData, |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 340 | br.resultExtras, br.resultAbort, false); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 341 | scheduleBroadcastsLocked(); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 342 | // We need to reset the state if we failed to start the receiver. |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 343 | br.state = BroadcastRecord.IDLE; |
| 344 | throw new RuntimeException(e.getMessage()); |
| 345 | } |
| 346 | } |
| 347 | return didSomething; |
| 348 | } |
| 349 | |
| 350 | public void skipPendingBroadcastLocked(int pid) { |
| 351 | final BroadcastRecord br = mPendingBroadcast; |
| 352 | if (br != null && br.curApp.pid == pid) { |
| 353 | br.state = BroadcastRecord.IDLE; |
| 354 | br.nextReceiver = mPendingBroadcastRecvIndex; |
| 355 | mPendingBroadcast = null; |
| 356 | scheduleBroadcastsLocked(); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | public void skipCurrentReceiverLocked(ProcessRecord app) { |
Wale Ogunwale | 24b243d | 2015-07-17 07:20:57 -0700 | [diff] [blame] | 361 | BroadcastRecord r = null; |
| 362 | if (mOrderedBroadcasts.size() > 0) { |
| 363 | BroadcastRecord br = mOrderedBroadcasts.get(0); |
| 364 | if (br.curApp == app) { |
| 365 | r = br; |
| 366 | } |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 367 | } |
riddle_hsu | 01eb7fa | 2015-03-04 17:27:05 +0800 | [diff] [blame] | 368 | if (r == null && mPendingBroadcast != null && mPendingBroadcast.curApp == app) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 369 | if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 370 | "[" + mQueueName + "] skip & discard pending app " + r); |
riddle_hsu | 01eb7fa | 2015-03-04 17:27:05 +0800 | [diff] [blame] | 371 | r = mPendingBroadcast; |
| 372 | } |
| 373 | |
| 374 | if (r != null) { |
Amith Yamasani | b5f3b5c | 2016-05-16 14:23:04 -0700 | [diff] [blame] | 375 | skipReceiverLocked(r); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
Amith Yamasani | b5f3b5c | 2016-05-16 14:23:04 -0700 | [diff] [blame] | 379 | private void skipReceiverLocked(BroadcastRecord r) { |
| 380 | logBroadcastReceiverDiscardLocked(r); |
| 381 | finishReceiverLocked(r, r.resultCode, r.resultData, |
| 382 | r.resultExtras, r.resultAbort, false); |
| 383 | scheduleBroadcastsLocked(); |
| 384 | } |
| 385 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 386 | public void scheduleBroadcastsLocked() { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 387 | if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Schedule broadcasts [" |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 388 | + mQueueName + "]: current=" |
| 389 | + mBroadcastsScheduled); |
| 390 | |
| 391 | if (mBroadcastsScheduled) { |
| 392 | return; |
| 393 | } |
| 394 | mHandler.sendMessage(mHandler.obtainMessage(BROADCAST_INTENT_MSG, this)); |
| 395 | mBroadcastsScheduled = true; |
| 396 | } |
| 397 | |
| 398 | public BroadcastRecord getMatchingOrderedReceiver(IBinder receiver) { |
| 399 | if (mOrderedBroadcasts.size() > 0) { |
| 400 | final BroadcastRecord r = mOrderedBroadcasts.get(0); |
| 401 | if (r != null && r.receiver == receiver) { |
| 402 | return r; |
| 403 | } |
| 404 | } |
| 405 | return null; |
| 406 | } |
| 407 | |
| 408 | public boolean finishReceiverLocked(BroadcastRecord r, int resultCode, |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 409 | String resultData, Bundle resultExtras, boolean resultAbort, boolean waitForServices) { |
| 410 | final int state = r.state; |
Dianne Hackborn | 3bc8f78d | 2013-09-19 13:34:35 -0700 | [diff] [blame] | 411 | final ActivityInfo receiver = r.curReceiver; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 412 | r.state = BroadcastRecord.IDLE; |
| 413 | if (state == BroadcastRecord.IDLE) { |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 414 | Slog.w(TAG, "finishReceiver [" + mQueueName + "] called but state is IDLE"); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 415 | } |
| 416 | r.receiver = null; |
| 417 | r.intent.setComponent(null); |
yangzhenyu | d509bc9 | 2016-08-31 18:26:46 +0800 | [diff] [blame] | 418 | if (r.curApp != null && r.curApp.curReceivers.contains(r)) { |
| 419 | r.curApp.curReceivers.remove(r); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 420 | } |
| 421 | if (r.curFilter != null) { |
| 422 | r.curFilter.receiverList.curBroadcast = null; |
| 423 | } |
| 424 | r.curFilter = null; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 425 | r.curReceiver = null; |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 426 | r.curApp = null; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 427 | mPendingBroadcast = null; |
| 428 | |
| 429 | r.resultCode = resultCode; |
| 430 | r.resultData = resultData; |
| 431 | r.resultExtras = resultExtras; |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 432 | if (resultAbort && (r.intent.getFlags()&Intent.FLAG_RECEIVER_NO_ABORT) == 0) { |
| 433 | r.resultAbort = resultAbort; |
| 434 | } else { |
| 435 | r.resultAbort = false; |
| 436 | } |
| 437 | |
| 438 | if (waitForServices && r.curComponent != null && r.queue.mDelayBehindServices |
| 439 | && r.queue.mOrderedBroadcasts.size() > 0 |
| 440 | && r.queue.mOrderedBroadcasts.get(0) == r) { |
Dianne Hackborn | 3bc8f78d | 2013-09-19 13:34:35 -0700 | [diff] [blame] | 441 | ActivityInfo nextReceiver; |
| 442 | if (r.nextReceiver < r.receivers.size()) { |
| 443 | Object obj = r.receivers.get(r.nextReceiver); |
| 444 | nextReceiver = (obj instanceof ActivityInfo) ? (ActivityInfo)obj : null; |
| 445 | } else { |
| 446 | nextReceiver = null; |
| 447 | } |
| 448 | // Don't do this if the next receive is in the same process as the current one. |
| 449 | if (receiver == null || nextReceiver == null |
| 450 | || receiver.applicationInfo.uid != nextReceiver.applicationInfo.uid |
| 451 | || !receiver.processName.equals(nextReceiver.processName)) { |
| 452 | // In this case, we are ready to process the next receiver for the current broadcast, |
| 453 | //Â but are on a queue that would like to wait for services to finish before moving |
| 454 | // on. If there are background services currently starting, then we will go into a |
| 455 | // special state where we hold off on continuing this broadcast until they are done. |
Dianne Hackborn | ad51be9 | 2016-08-16 16:27:36 -0700 | [diff] [blame] | 456 | if (mService.mServices.hasBackgroundServicesLocked(r.userId)) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 457 | Slog.i(TAG, "Delay finish: " + r.curComponent.flattenToShortString()); |
Dianne Hackborn | 3bc8f78d | 2013-09-19 13:34:35 -0700 | [diff] [blame] | 458 | r.state = BroadcastRecord.WAITING_SERVICES; |
| 459 | return false; |
| 460 | } |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
| 464 | r.curComponent = null; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 465 | |
| 466 | // We will process the next receiver right now if this is finishing |
| 467 | // an app receiver (which is always asynchronous) or after we have |
| 468 | // come back from calling a receiver. |
| 469 | return state == BroadcastRecord.APP_RECEIVE |
| 470 | || state == BroadcastRecord.CALL_DONE_RECEIVE; |
| 471 | } |
| 472 | |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 473 | public void backgroundServicesFinishedLocked(int userId) { |
| 474 | if (mOrderedBroadcasts.size() > 0) { |
| 475 | BroadcastRecord br = mOrderedBroadcasts.get(0); |
| 476 | if (br.userId == userId && br.state == BroadcastRecord.WAITING_SERVICES) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 477 | Slog.i(TAG, "Resuming delayed broadcast"); |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 478 | br.curComponent = null; |
| 479 | br.state = BroadcastRecord.IDLE; |
| 480 | processNextBroadcast(false); |
| 481 | } |
| 482 | } |
| 483 | } |
| 484 | |
Dianne Hackborn | ea05cd5 | 2016-06-20 11:22:40 -0700 | [diff] [blame] | 485 | void performReceiveLocked(ProcessRecord app, IIntentReceiver receiver, |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 486 | Intent intent, int resultCode, String data, Bundle extras, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 487 | boolean ordered, boolean sticky, int sendingUser) throws RemoteException { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 488 | // Send the intent to the receiver asynchronously using one-way binder calls. |
Craig Mautner | 38c1a5f | 2014-07-21 15:36:37 +0000 | [diff] [blame] | 489 | if (app != null) { |
| 490 | if (app.thread != null) { |
| 491 | // If we have an app thread, do the call through that so it is |
| 492 | // correctly ordered with other one-way calls. |
Joe Onorato | 5869d1c | 2016-04-20 15:38:07 -0700 | [diff] [blame] | 493 | try { |
| 494 | app.thread.scheduleRegisteredReceiver(receiver, intent, resultCode, |
| 495 | data, extras, ordered, sticky, sendingUser, app.repProcState); |
| 496 | // TODO: Uncomment this when (b/28322359) is fixed and we aren't getting |
| 497 | // DeadObjectException when the process isn't actually dead. |
| 498 | //} catch (DeadObjectException ex) { |
| 499 | // Failed to call into the process. It's dying so just let it die and move on. |
| 500 | // throw ex; |
| 501 | } catch (RemoteException ex) { |
| 502 | // Failed to call into the process. It's either dying or wedged. Kill it gently. |
| 503 | synchronized (mService) { |
| 504 | Slog.w(TAG, "Can't deliver broadcast to " + app.processName |
| 505 | + " (pid " + app.pid + "). Crashing it."); |
| 506 | app.scheduleCrash("can't deliver broadcast"); |
| 507 | } |
| 508 | throw ex; |
| 509 | } |
Craig Mautner | 38c1a5f | 2014-07-21 15:36:37 +0000 | [diff] [blame] | 510 | } else { |
| 511 | // Application has died. Receiver doesn't exist. |
| 512 | throw new RemoteException("app.thread must not be null"); |
| 513 | } |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 514 | } else { |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 515 | receiver.performReceive(intent, resultCode, data, extras, ordered, |
| 516 | sticky, sendingUser); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 517 | } |
| 518 | } |
| 519 | |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 520 | private void deliverToRegisteredReceiverLocked(BroadcastRecord r, |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 521 | BroadcastFilter filter, boolean ordered, int index) { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 522 | boolean skip = false; |
Amith Yamasani | 8bf06ed | 2012-08-27 19:30:30 -0700 | [diff] [blame] | 523 | if (filter.requiredPermission != null) { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 524 | int perm = mService.checkComponentPermission(filter.requiredPermission, |
| 525 | r.callingPid, r.callingUid, -1, true); |
| 526 | if (perm != PackageManager.PERMISSION_GRANTED) { |
| 527 | Slog.w(TAG, "Permission Denial: broadcasting " |
| 528 | + r.intent.toString() |
| 529 | + " from " + r.callerPackage + " (pid=" |
| 530 | + r.callingPid + ", uid=" + r.callingUid + ")" |
| 531 | + " requires " + filter.requiredPermission |
| 532 | + " due to registered receiver " + filter); |
| 533 | skip = true; |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 534 | } else { |
| 535 | final int opCode = AppOpsManager.permissionToOpCode(filter.requiredPermission); |
| 536 | if (opCode != AppOpsManager.OP_NONE |
| 537 | && mService.mAppOpsService.noteOperation(opCode, r.callingUid, |
| 538 | r.callerPackage) != AppOpsManager.MODE_ALLOWED) { |
| 539 | Slog.w(TAG, "Appop Denial: broadcasting " |
| 540 | + r.intent.toString() |
| 541 | + " from " + r.callerPackage + " (pid=" |
| 542 | + r.callingPid + ", uid=" + r.callingUid + ")" |
| 543 | + " requires appop " + AppOpsManager.permissionToOp( |
| 544 | filter.requiredPermission) |
| 545 | + " due to registered receiver " + filter); |
| 546 | skip = true; |
| 547 | } |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 548 | } |
| 549 | } |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 550 | if (!skip && r.requiredPermissions != null && r.requiredPermissions.length > 0) { |
| 551 | for (int i = 0; i < r.requiredPermissions.length; i++) { |
| 552 | String requiredPermission = r.requiredPermissions[i]; |
| 553 | int perm = mService.checkComponentPermission(requiredPermission, |
| 554 | filter.receiverList.pid, filter.receiverList.uid, -1, true); |
| 555 | if (perm != PackageManager.PERMISSION_GRANTED) { |
| 556 | Slog.w(TAG, "Permission Denial: receiving " |
| 557 | + r.intent.toString() |
| 558 | + " to " + filter.receiverList.app |
| 559 | + " (pid=" + filter.receiverList.pid |
| 560 | + ", uid=" + filter.receiverList.uid + ")" |
| 561 | + " requires " + requiredPermission |
| 562 | + " due to sender " + r.callerPackage |
| 563 | + " (uid " + r.callingUid + ")"); |
| 564 | skip = true; |
| 565 | break; |
| 566 | } |
| 567 | int appOp = AppOpsManager.permissionToOpCode(requiredPermission); |
Svetoslav | fb9ec50 | 2015-09-01 14:45:18 -0700 | [diff] [blame] | 568 | if (appOp != AppOpsManager.OP_NONE && appOp != r.appOp |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 569 | && mService.mAppOpsService.noteOperation(appOp, |
| 570 | filter.receiverList.uid, filter.packageName) |
| 571 | != AppOpsManager.MODE_ALLOWED) { |
| 572 | Slog.w(TAG, "Appop Denial: receiving " |
| 573 | + r.intent.toString() |
| 574 | + " to " + filter.receiverList.app |
| 575 | + " (pid=" + filter.receiverList.pid |
| 576 | + ", uid=" + filter.receiverList.uid + ")" |
| 577 | + " requires appop " + AppOpsManager.permissionToOp( |
| 578 | requiredPermission) |
| 579 | + " due to sender " + r.callerPackage |
| 580 | + " (uid " + r.callingUid + ")"); |
| 581 | skip = true; |
| 582 | break; |
| 583 | } |
| 584 | } |
| 585 | } |
| 586 | if (!skip && (r.requiredPermissions == null || r.requiredPermissions.length == 0)) { |
| 587 | int perm = mService.checkComponentPermission(null, |
Fyodor Kupolov | e37520b | 2015-07-14 22:29:21 +0000 | [diff] [blame] | 588 | filter.receiverList.pid, filter.receiverList.uid, -1, true); |
| 589 | if (perm != PackageManager.PERMISSION_GRANTED) { |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 590 | Slog.w(TAG, "Permission Denial: security check failed when receiving " |
Fyodor Kupolov | e37520b | 2015-07-14 22:29:21 +0000 | [diff] [blame] | 591 | + r.intent.toString() |
| 592 | + " to " + filter.receiverList.app |
| 593 | + " (pid=" + filter.receiverList.pid |
| 594 | + ", uid=" + filter.receiverList.uid + ")" |
Fyodor Kupolov | e37520b | 2015-07-14 22:29:21 +0000 | [diff] [blame] | 595 | + " due to sender " + r.callerPackage |
| 596 | + " (uid " + r.callingUid + ")"); |
| 597 | skip = true; |
| 598 | } |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 599 | } |
| 600 | if (!skip && r.appOp != AppOpsManager.OP_NONE |
| 601 | && mService.mAppOpsService.noteOperation(r.appOp, |
| 602 | filter.receiverList.uid, filter.packageName) |
| 603 | != AppOpsManager.MODE_ALLOWED) { |
| 604 | Slog.w(TAG, "Appop Denial: receiving " |
| 605 | + r.intent.toString() |
| 606 | + " to " + filter.receiverList.app |
| 607 | + " (pid=" + filter.receiverList.pid |
| 608 | + ", uid=" + filter.receiverList.uid + ")" |
| 609 | + " requires appop " + AppOpsManager.opToName(r.appOp) |
| 610 | + " due to sender " + r.callerPackage |
| 611 | + " (uid " + r.callingUid + ")"); |
| 612 | skip = true; |
Fyodor Kupolov | b4e7283 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 613 | } |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 614 | |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 615 | if (!mService.mIntentFirewall.checkBroadcast(r.intent, r.callingUid, |
| 616 | r.callingPid, r.resolvedType, filter.receiverList.uid)) { |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 617 | skip = true; |
Ben Gruver | 49660c7 | 2013-08-06 19:54:08 -0700 | [diff] [blame] | 618 | } |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 619 | |
Dianne Hackborn | 443d35a | 2017-06-16 17:59:35 -0700 | [diff] [blame] | 620 | if (!skip && (filter.receiverList.app == null || filter.receiverList.app.killed |
| 621 | || filter.receiverList.app.crashing)) { |
Dianne Hackborn | 9357b11 | 2013-10-03 18:27:48 -0700 | [diff] [blame] | 622 | Slog.w(TAG, "Skipping deliver [" + mQueueName + "] " + r |
Dianne Hackborn | 443d35a | 2017-06-16 17:59:35 -0700 | [diff] [blame] | 623 | + " to " + filter.receiverList + ": process gone or crashing"); |
Dianne Hackborn | 9357b11 | 2013-10-03 18:27:48 -0700 | [diff] [blame] | 624 | skip = true; |
| 625 | } |
| 626 | |
Chad Brubaker | b7e34d5 | 2017-02-22 12:36:06 -0800 | [diff] [blame] | 627 | // Ensure that broadcasts are only sent to other Instant Apps if they are marked as |
| 628 | // visible to Instant Apps. |
| 629 | final boolean visibleToInstantApps = |
| 630 | (r.intent.getFlags() & Intent.FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS) != 0; |
| 631 | |
| 632 | if (!skip && !visibleToInstantApps && filter.instantApp |
| 633 | && filter.receiverList.uid != r.callingUid) { |
| 634 | Slog.w(TAG, "Instant App Denial: receiving " |
| 635 | + r.intent.toString() |
| 636 | + " to " + filter.receiverList.app |
| 637 | + " (pid=" + filter.receiverList.pid |
| 638 | + ", uid=" + filter.receiverList.uid + ")" |
| 639 | + " due to sender " + r.callerPackage |
| 640 | + " (uid " + r.callingUid + ")" |
| 641 | + " not specifying FLAG_RECEIVER_VISIBLE_TO_INSTANT_APPS"); |
| 642 | skip = true; |
| 643 | } |
| 644 | |
Chad Brubaker | 816c83b | 2017-03-02 10:27:59 -0800 | [diff] [blame] | 645 | if (!skip && !filter.visibleToInstantApp && r.callerInstantApp |
| 646 | && filter.receiverList.uid != r.callingUid) { |
| 647 | Slog.w(TAG, "Instant App Denial: receiving " |
| 648 | + r.intent.toString() |
| 649 | + " to " + filter.receiverList.app |
| 650 | + " (pid=" + filter.receiverList.pid |
| 651 | + ", uid=" + filter.receiverList.uid + ")" |
| 652 | + " requires receiver be visible to instant apps" |
| 653 | + " due to sender " + r.callerPackage |
| 654 | + " (uid " + r.callingUid + ")"); |
| 655 | skip = true; |
| 656 | } |
| 657 | |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 658 | if (skip) { |
| 659 | r.delivery[index] = BroadcastRecord.DELIVERY_SKIPPED; |
| 660 | return; |
| 661 | } |
Svet Ganov | 9c165d7 | 2015-12-01 19:52:26 -0800 | [diff] [blame] | 662 | |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 663 | // If permissions need a review before any of the app components can run, we drop |
| 664 | // the broadcast and if the calling app is in the foreground and the broadcast is |
| 665 | // explicit we launch the review UI passing it a pending intent to send the skipped |
| 666 | // broadcast. |
Svet Ganov | 77df6f3 | 2016-08-17 11:46:34 -0700 | [diff] [blame] | 667 | if (mService.mPermissionReviewRequired) { |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 668 | if (!requestStartTargetPermissionsReviewIfNeededLocked(r, filter.packageName, |
| 669 | filter.owningUserId)) { |
| 670 | r.delivery[index] = BroadcastRecord.DELIVERY_SKIPPED; |
| 671 | return; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 672 | } |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | r.delivery[index] = BroadcastRecord.DELIVERY_DELIVERED; |
| 676 | |
| 677 | // If this is not being sent as an ordered broadcast, then we |
| 678 | // don't want to touch the fields that keep track of the current |
| 679 | // state of ordered broadcasts. |
| 680 | if (ordered) { |
| 681 | r.receiver = filter.receiverList.receiver.asBinder(); |
| 682 | r.curFilter = filter; |
| 683 | filter.receiverList.curBroadcast = r; |
| 684 | r.state = BroadcastRecord.CALL_IN_RECEIVE; |
| 685 | if (filter.receiverList.app != null) { |
| 686 | // Bump hosting application to no longer be in background |
| 687 | // scheduling class. Note that we can't do that if there |
| 688 | // isn't an app... but we can only be in that case for |
| 689 | // things that directly call the IActivityManager API, which |
| 690 | // are already core system stuff so don't matter for this. |
| 691 | r.curApp = filter.receiverList.app; |
yangzhenyu | d509bc9 | 2016-08-31 18:26:46 +0800 | [diff] [blame] | 692 | filter.receiverList.app.curReceivers.add(r); |
Amith Yamasani | 385c3ad | 2017-05-04 14:27:11 -0700 | [diff] [blame] | 693 | mService.updateOomAdjLocked(r.curApp, true); |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 694 | } |
| 695 | } |
| 696 | try { |
| 697 | if (DEBUG_BROADCAST_LIGHT) Slog.i(TAG_BROADCAST, |
| 698 | "Delivering to " + filter + " : " + r); |
Amith Yamasani | b5f3b5c | 2016-05-16 14:23:04 -0700 | [diff] [blame] | 699 | if (filter.receiverList.app != null && filter.receiverList.app.inFullBackup) { |
| 700 | // Skip delivery if full backup in progress |
| 701 | // If it's an ordered broadcast, we need to continue to the next receiver. |
| 702 | if (ordered) { |
| 703 | skipReceiverLocked(r); |
| 704 | } |
| 705 | } else { |
| 706 | performReceiveLocked(filter.receiverList.app, filter.receiverList.receiver, |
| 707 | new Intent(r.intent), r.resultCode, r.resultData, |
| 708 | r.resultExtras, r.ordered, r.initialSticky, r.userId); |
| 709 | } |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 710 | if (ordered) { |
| 711 | r.state = BroadcastRecord.CALL_DONE_RECEIVE; |
| 712 | } |
| 713 | } catch (RemoteException e) { |
| 714 | Slog.w(TAG, "Failure sending broadcast " + r.intent, e); |
| 715 | if (ordered) { |
| 716 | r.receiver = null; |
| 717 | r.curFilter = null; |
| 718 | filter.receiverList.curBroadcast = null; |
| 719 | if (filter.receiverList.app != null) { |
yangzhenyu | d509bc9 | 2016-08-31 18:26:46 +0800 | [diff] [blame] | 720 | filter.receiverList.app.curReceivers.remove(r); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 721 | } |
| 722 | } |
| 723 | } |
| 724 | } |
| 725 | |
Svet Ganov | 9c165d7 | 2015-12-01 19:52:26 -0800 | [diff] [blame] | 726 | private boolean requestStartTargetPermissionsReviewIfNeededLocked( |
| 727 | BroadcastRecord receiverRecord, String receivingPackageName, |
| 728 | final int receivingUserId) { |
| 729 | if (!mService.getPackageManagerInternalLocked().isPermissionsReviewRequired( |
| 730 | receivingPackageName, receivingUserId)) { |
| 731 | return true; |
| 732 | } |
| 733 | |
| 734 | final boolean callerForeground = receiverRecord.callerApp != null |
Dianne Hackborn | a49ad09 | 2016-03-03 13:39:10 -0800 | [diff] [blame] | 735 | ? receiverRecord.callerApp.setSchedGroup != ProcessList.SCHED_GROUP_BACKGROUND |
Svet Ganov | 9c165d7 | 2015-12-01 19:52:26 -0800 | [diff] [blame] | 736 | : true; |
| 737 | |
| 738 | // Show a permission review UI only for explicit broadcast from a foreground app |
| 739 | if (callerForeground && receiverRecord.intent.getComponent() != null) { |
| 740 | IIntentSender target = mService.getIntentSenderLocked( |
| 741 | ActivityManager.INTENT_SENDER_BROADCAST, receiverRecord.callerPackage, |
| 742 | receiverRecord.callingUid, receiverRecord.userId, null, null, 0, |
| 743 | new Intent[]{receiverRecord.intent}, |
| 744 | new String[]{receiverRecord.intent.resolveType(mService.mContext |
| 745 | .getContentResolver())}, |
| 746 | PendingIntent.FLAG_CANCEL_CURRENT | PendingIntent.FLAG_ONE_SHOT |
| 747 | | PendingIntent.FLAG_IMMUTABLE, null); |
| 748 | |
| 749 | final Intent intent = new Intent(Intent.ACTION_REVIEW_PERMISSIONS); |
| 750 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
| 751 | | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS); |
| 752 | intent.putExtra(Intent.EXTRA_PACKAGE_NAME, receivingPackageName); |
| 753 | intent.putExtra(Intent.EXTRA_INTENT, new IntentSender(target)); |
| 754 | |
| 755 | if (DEBUG_PERMISSIONS_REVIEW) { |
| 756 | Slog.i(TAG, "u" + receivingUserId + " Launching permission review for package " |
| 757 | + receivingPackageName); |
| 758 | } |
| 759 | |
| 760 | mHandler.post(new Runnable() { |
| 761 | @Override |
| 762 | public void run() { |
| 763 | mService.mContext.startActivityAsUser(intent, new UserHandle(receivingUserId)); |
| 764 | } |
| 765 | }); |
| 766 | } else { |
| 767 | Slog.w(TAG, "u" + receivingUserId + " Receiving a broadcast in package" |
| 768 | + receivingPackageName + " requires a permissions review"); |
| 769 | } |
| 770 | |
| 771 | return false; |
| 772 | } |
| 773 | |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 774 | final void scheduleTempWhitelistLocked(int uid, long duration, BroadcastRecord r) { |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 775 | if (duration > Integer.MAX_VALUE) { |
| 776 | duration = Integer.MAX_VALUE; |
| 777 | } |
| 778 | // XXX ideally we should pause the broadcast until everything behind this is done, |
| 779 | // or else we will likely start dispatching the broadcast before we have opened |
| 780 | // access to the app (there is a lot of asynchronicity behind this). It is probably |
| 781 | // not that big a deal, however, because the main purpose here is to allow apps |
| 782 | // to hold wake locks, and they will be able to acquire their wake lock immediately |
| 783 | // it just won't be enabled until we get through this work. |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 784 | StringBuilder b = new StringBuilder(); |
| 785 | b.append("broadcast:"); |
| 786 | UserHandle.formatUid(b, r.callingUid); |
| 787 | b.append(":"); |
| 788 | if (r.intent.getAction() != null) { |
| 789 | b.append(r.intent.getAction()); |
| 790 | } else if (r.intent.getComponent() != null) { |
Dianne Hackborn | e4d1a2e | 2017-04-14 17:57:33 -0700 | [diff] [blame] | 791 | r.intent.getComponent().appendShortString(b); |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 792 | } else if (r.intent.getData() != null) { |
| 793 | b.append(r.intent.getData()); |
| 794 | } |
Dianne Hackborn | e4d1a2e | 2017-04-14 17:57:33 -0700 | [diff] [blame] | 795 | mService.tempWhitelistUidLocked(uid, duration, b.toString()); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 796 | } |
| 797 | |
Dianne Hackborn | b8633f3 | 2017-04-11 17:38:42 -0700 | [diff] [blame] | 798 | /** |
| 799 | * Return true if all given permissions are signature-only perms. |
| 800 | */ |
| 801 | final boolean isSignaturePerm(String[] perms) { |
| 802 | if (perms == null) { |
| 803 | return false; |
| 804 | } |
| 805 | IPackageManager pm = AppGlobals.getPackageManager(); |
| 806 | for (int i = perms.length-1; i >= 0; i--) { |
| 807 | try { |
Svetoslav Ganov | adb8c52 | 2017-07-28 05:46:53 +0000 | [diff] [blame] | 808 | PermissionInfo pi = pm.getPermissionInfo(perms[i], "android", 0); |
Dianne Hackborn | b8633f3 | 2017-04-11 17:38:42 -0700 | [diff] [blame] | 809 | if ((pi.protectionLevel & (PermissionInfo.PROTECTION_MASK_BASE |
| 810 | | PermissionInfo.PROTECTION_FLAG_PRIVILEGED)) |
| 811 | != PermissionInfo.PROTECTION_SIGNATURE) { |
| 812 | // If this a signature permission and NOT allowed for privileged apps, it |
| 813 | // is okay... otherwise, nope! |
| 814 | return false; |
| 815 | } |
| 816 | } catch (RemoteException e) { |
| 817 | return false; |
| 818 | } |
| 819 | } |
| 820 | return true; |
| 821 | } |
| 822 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 823 | final void processNextBroadcast(boolean fromMsg) { |
| 824 | synchronized(mService) { |
| 825 | BroadcastRecord r; |
| 826 | |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 827 | if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "processNextBroadcast [" |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 828 | + mQueueName + "]: " |
Carmen Jackson | a68e345 | 2017-01-17 14:01:33 -0800 | [diff] [blame] | 829 | + mParallelBroadcasts.size() + " parallel broadcasts, " |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 830 | + mOrderedBroadcasts.size() + " ordered broadcasts"); |
| 831 | |
| 832 | mService.updateCpuStats(); |
| 833 | |
| 834 | if (fromMsg) { |
| 835 | mBroadcastsScheduled = false; |
| 836 | } |
| 837 | |
| 838 | // First, deliver any non-serialized broadcasts right away. |
| 839 | while (mParallelBroadcasts.size() > 0) { |
| 840 | r = mParallelBroadcasts.remove(0); |
| 841 | r.dispatchTime = SystemClock.uptimeMillis(); |
| 842 | r.dispatchClockTime = System.currentTimeMillis(); |
Carmen Jackson | a68e345 | 2017-01-17 14:01:33 -0800 | [diff] [blame] | 843 | |
| 844 | if (Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER)) { |
| 845 | Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, |
| 846 | createBroadcastTraceTitle(r, BroadcastRecord.DELIVERY_PENDING), |
| 847 | System.identityHashCode(r)); |
| 848 | Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, |
| 849 | createBroadcastTraceTitle(r, BroadcastRecord.DELIVERY_DELIVERED), |
| 850 | System.identityHashCode(r)); |
| 851 | } |
| 852 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 853 | final int N = r.receivers.size(); |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 854 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, "Processing parallel broadcast [" |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 855 | + mQueueName + "] " + r); |
| 856 | for (int i=0; i<N; i++) { |
| 857 | Object target = r.receivers.get(i); |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 858 | if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 859 | "Delivering non-ordered on [" + mQueueName + "] to registered " |
| 860 | + target + ": " + r); |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 861 | deliverToRegisteredReceiverLocked(r, (BroadcastFilter)target, false, i); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 862 | } |
| 863 | addBroadcastToHistoryLocked(r); |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 864 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, "Done with parallel broadcast [" |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 865 | + mQueueName + "] " + r); |
| 866 | } |
| 867 | |
| 868 | // Now take care of the next serialized one... |
| 869 | |
| 870 | // If we are waiting for a process to come up to handle the next |
| 871 | // broadcast, then do nothing at this point. Just in case, we |
| 872 | // check that the process we're waiting for still exists. |
| 873 | if (mPendingBroadcast != null) { |
Wale Ogunwale | 3c36b8e | 2015-03-09 10:18:51 -0700 | [diff] [blame] | 874 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, |
| 875 | "processNextBroadcast [" + mQueueName + "]: waiting for " |
| 876 | + mPendingBroadcast.curApp); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 877 | |
| 878 | boolean isDead; |
| 879 | synchronized (mService.mPidsSelfLocked) { |
Dianne Hackborn | 9357b11 | 2013-10-03 18:27:48 -0700 | [diff] [blame] | 880 | ProcessRecord proc = mService.mPidsSelfLocked.get(mPendingBroadcast.curApp.pid); |
| 881 | isDead = proc == null || proc.crashing; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 882 | } |
| 883 | if (!isDead) { |
| 884 | // It's still alive, so keep waiting |
| 885 | return; |
| 886 | } else { |
| 887 | Slog.w(TAG, "pending app [" |
| 888 | + mQueueName + "]" + mPendingBroadcast.curApp |
| 889 | + " died before responding to broadcast"); |
| 890 | mPendingBroadcast.state = BroadcastRecord.IDLE; |
| 891 | mPendingBroadcast.nextReceiver = mPendingBroadcastRecvIndex; |
| 892 | mPendingBroadcast = null; |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | boolean looped = false; |
| 897 | |
| 898 | do { |
| 899 | if (mOrderedBroadcasts.size() == 0) { |
| 900 | // No more broadcasts pending, so all done! |
| 901 | mService.scheduleAppGcsLocked(); |
| 902 | if (looped) { |
| 903 | // If we had finished the last ordered broadcast, then |
| 904 | // make sure all processes have correct oom and sched |
| 905 | // adjustments. |
| 906 | mService.updateOomAdjLocked(); |
| 907 | } |
| 908 | return; |
| 909 | } |
| 910 | r = mOrderedBroadcasts.get(0); |
| 911 | boolean forceReceive = false; |
| 912 | |
| 913 | // Ensure that even if something goes awry with the timeout |
| 914 | // detection, we catch "hung" broadcasts here, discard them, |
| 915 | // and continue to make progress. |
| 916 | // |
| 917 | // This is only done if the system is ready so that PRE_BOOT_COMPLETED |
| 918 | // receivers don't get executed with timeouts. They're intended for |
| 919 | // one time heavy lifting after system upgrades and can take |
| 920 | // significant amounts of time. |
| 921 | int numReceivers = (r.receivers != null) ? r.receivers.size() : 0; |
| 922 | if (mService.mProcessesReady && r.dispatchTime > 0) { |
| 923 | long now = SystemClock.uptimeMillis(); |
| 924 | if ((numReceivers > 0) && |
| 925 | (now > r.dispatchTime + (2*mTimeoutPeriod*numReceivers))) { |
| 926 | Slog.w(TAG, "Hung broadcast [" |
| 927 | + mQueueName + "] discarded after timeout failure:" |
| 928 | + " now=" + now |
| 929 | + " dispatchTime=" + r.dispatchTime |
| 930 | + " startTime=" + r.receiverTime |
| 931 | + " intent=" + r.intent |
| 932 | + " numReceivers=" + numReceivers |
| 933 | + " nextReceiver=" + r.nextReceiver |
| 934 | + " state=" + r.state); |
| 935 | broadcastTimeoutLocked(false); // forcibly finish this broadcast |
| 936 | forceReceive = true; |
| 937 | r.state = BroadcastRecord.IDLE; |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | if (r.state != BroadcastRecord.IDLE) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 942 | if (DEBUG_BROADCAST) Slog.d(TAG_BROADCAST, |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 943 | "processNextBroadcast(" |
| 944 | + mQueueName + ") called when not idle (state=" |
| 945 | + r.state + ")"); |
| 946 | return; |
| 947 | } |
| 948 | |
| 949 | if (r.receivers == null || r.nextReceiver >= numReceivers |
| 950 | || r.resultAbort || forceReceive) { |
| 951 | // No more receivers for this broadcast! Send the final |
| 952 | // result if requested... |
| 953 | if (r.resultTo != null) { |
| 954 | try { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 955 | if (DEBUG_BROADCAST) Slog.i(TAG_BROADCAST, |
Todd Kennedy | d2f1511 | 2015-01-21 15:25:56 -0800 | [diff] [blame] | 956 | "Finishing broadcast [" + mQueueName + "] " |
| 957 | + r.intent.getAction() + " app=" + r.callerApp); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 958 | performReceiveLocked(r.callerApp, r.resultTo, |
| 959 | new Intent(r.intent), r.resultCode, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 960 | r.resultData, r.resultExtras, false, false, r.userId); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 961 | // Set this to null so that the reference |
Dianne Hackborn | 9357b11 | 2013-10-03 18:27:48 -0700 | [diff] [blame] | 962 | // (local and remote) isn't kept in the mBroadcastHistory. |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 963 | r.resultTo = null; |
| 964 | } catch (RemoteException e) { |
Craig Mautner | 38c1a5f | 2014-07-21 15:36:37 +0000 | [diff] [blame] | 965 | r.resultTo = null; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 966 | Slog.w(TAG, "Failure [" |
| 967 | + mQueueName + "] sending broadcast result of " |
| 968 | + r.intent, e); |
Joe Onorato | 5869d1c | 2016-04-20 15:38:07 -0700 | [diff] [blame] | 969 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 970 | } |
| 971 | } |
| 972 | |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 973 | if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Cancelling BROADCAST_TIMEOUT_MSG"); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 974 | cancelBroadcastTimeoutLocked(); |
| 975 | |
Wale Ogunwale | 3c36b8e | 2015-03-09 10:18:51 -0700 | [diff] [blame] | 976 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, |
| 977 | "Finished with ordered broadcast " + r); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 978 | |
| 979 | // ... and on to the next... |
| 980 | addBroadcastToHistoryLocked(r); |
Dianne Hackborn | bc02a39 | 2016-06-02 17:15:08 -0700 | [diff] [blame] | 981 | if (r.intent.getComponent() == null && r.intent.getPackage() == null |
| 982 | && (r.intent.getFlags()&Intent.FLAG_RECEIVER_REGISTERED_ONLY) == 0) { |
| 983 | // This was an implicit broadcast... let's record it for posterity. |
| 984 | mService.addBroadcastStatLocked(r.intent.getAction(), r.callerPackage, |
| 985 | r.manifestCount, r.manifestSkipCount, r.finishTime-r.dispatchTime); |
| 986 | } |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 987 | mOrderedBroadcasts.remove(0); |
| 988 | r = null; |
| 989 | looped = true; |
| 990 | continue; |
| 991 | } |
| 992 | } while (r == null); |
| 993 | |
| 994 | // Get the next receiver... |
| 995 | int recIdx = r.nextReceiver++; |
| 996 | |
| 997 | // Keep track of when this receiver started, and make sure there |
| 998 | // is a timeout message pending to kill it if need be. |
| 999 | r.receiverTime = SystemClock.uptimeMillis(); |
| 1000 | if (recIdx == 0) { |
| 1001 | r.dispatchTime = r.receiverTime; |
| 1002 | r.dispatchClockTime = System.currentTimeMillis(); |
Carmen Jackson | a68e345 | 2017-01-17 14:01:33 -0800 | [diff] [blame] | 1003 | if (Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER)) { |
| 1004 | Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, |
| 1005 | createBroadcastTraceTitle(r, BroadcastRecord.DELIVERY_PENDING), |
| 1006 | System.identityHashCode(r)); |
| 1007 | Trace.asyncTraceBegin(Trace.TRACE_TAG_ACTIVITY_MANAGER, |
| 1008 | createBroadcastTraceTitle(r, BroadcastRecord.DELIVERY_DELIVERED), |
| 1009 | System.identityHashCode(r)); |
| 1010 | } |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 1011 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, "Processing ordered broadcast [" |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1012 | + mQueueName + "] " + r); |
| 1013 | } |
| 1014 | if (! mPendingBroadcastTimeoutMessage) { |
| 1015 | long timeoutTime = r.receiverTime + mTimeoutPeriod; |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 1016 | if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1017 | "Submitting BROADCAST_TIMEOUT_MSG [" |
| 1018 | + mQueueName + "] for " + r + " at " + timeoutTime); |
| 1019 | setBroadcastTimeoutLocked(timeoutTime); |
| 1020 | } |
| 1021 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1022 | final BroadcastOptions brOptions = r.options; |
| 1023 | final Object nextReceiver = r.receivers.get(recIdx); |
| 1024 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1025 | if (nextReceiver instanceof BroadcastFilter) { |
| 1026 | // Simple case: this is a registered receiver who gets |
| 1027 | // a direct call. |
| 1028 | BroadcastFilter filter = (BroadcastFilter)nextReceiver; |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 1029 | if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1030 | "Delivering ordered [" |
| 1031 | + mQueueName + "] to registered " |
| 1032 | + filter + ": " + r); |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 1033 | deliverToRegisteredReceiverLocked(r, filter, r.ordered, recIdx); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1034 | if (r.receiver == null || !r.ordered) { |
| 1035 | // The receiver has already finished, so schedule to |
| 1036 | // process the next one. |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 1037 | if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Quick finishing [" |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1038 | + mQueueName + "]: ordered=" |
| 1039 | + r.ordered + " receiver=" + r.receiver); |
| 1040 | r.state = BroadcastRecord.IDLE; |
| 1041 | scheduleBroadcastsLocked(); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1042 | } else { |
| 1043 | if (brOptions != null && brOptions.getTemporaryAppWhitelistDuration() > 0) { |
| 1044 | scheduleTempWhitelistLocked(filter.owningUid, |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1045 | brOptions.getTemporaryAppWhitelistDuration(), r); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1046 | } |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1047 | } |
| 1048 | return; |
| 1049 | } |
| 1050 | |
| 1051 | // Hard case: need to instantiate the receiver, possibly |
| 1052 | // starting its application process to host it. |
| 1053 | |
| 1054 | ResolveInfo info = |
| 1055 | (ResolveInfo)nextReceiver; |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1056 | ComponentName component = new ComponentName( |
| 1057 | info.activityInfo.applicationInfo.packageName, |
| 1058 | info.activityInfo.name); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1059 | |
| 1060 | boolean skip = false; |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 1061 | if (brOptions != null && |
| 1062 | (info.activityInfo.applicationInfo.targetSdkVersion |
| 1063 | < brOptions.getMinManifestReceiverApiLevel() || |
| 1064 | info.activityInfo.applicationInfo.targetSdkVersion |
| 1065 | > brOptions.getMaxManifestReceiverApiLevel())) { |
| 1066 | skip = true; |
| 1067 | } |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1068 | int perm = mService.checkComponentPermission(info.activityInfo.permission, |
| 1069 | r.callingPid, r.callingUid, info.activityInfo.applicationInfo.uid, |
| 1070 | info.activityInfo.exported); |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 1071 | if (!skip && perm != PackageManager.PERMISSION_GRANTED) { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1072 | if (!info.activityInfo.exported) { |
| 1073 | Slog.w(TAG, "Permission Denial: broadcasting " |
| 1074 | + r.intent.toString() |
| 1075 | + " from " + r.callerPackage + " (pid=" + r.callingPid |
| 1076 | + ", uid=" + r.callingUid + ")" |
| 1077 | + " is not exported from uid " + info.activityInfo.applicationInfo.uid |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1078 | + " due to receiver " + component.flattenToShortString()); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1079 | } else { |
| 1080 | Slog.w(TAG, "Permission Denial: broadcasting " |
| 1081 | + r.intent.toString() |
| 1082 | + " from " + r.callerPackage + " (pid=" + r.callingPid |
| 1083 | + ", uid=" + r.callingUid + ")" |
| 1084 | + " requires " + info.activityInfo.permission |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1085 | + " due to receiver " + component.flattenToShortString()); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1086 | } |
| 1087 | skip = true; |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 1088 | } else if (!skip && info.activityInfo.permission != null) { |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 1089 | final int opCode = AppOpsManager.permissionToOpCode(info.activityInfo.permission); |
| 1090 | if (opCode != AppOpsManager.OP_NONE |
| 1091 | && mService.mAppOpsService.noteOperation(opCode, r.callingUid, |
| 1092 | r.callerPackage) != AppOpsManager.MODE_ALLOWED) { |
| 1093 | Slog.w(TAG, "Appop Denial: broadcasting " |
| 1094 | + r.intent.toString() |
| 1095 | + " from " + r.callerPackage + " (pid=" |
| 1096 | + r.callingPid + ", uid=" + r.callingUid + ")" |
| 1097 | + " requires appop " + AppOpsManager.permissionToOp( |
| 1098 | info.activityInfo.permission) |
| 1099 | + " due to registered receiver " |
| 1100 | + component.flattenToShortString()); |
| 1101 | skip = true; |
| 1102 | } |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1103 | } |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 1104 | if (!skip && info.activityInfo.applicationInfo.uid != Process.SYSTEM_UID && |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 1105 | r.requiredPermissions != null && r.requiredPermissions.length > 0) { |
| 1106 | for (int i = 0; i < r.requiredPermissions.length; i++) { |
| 1107 | String requiredPermission = r.requiredPermissions[i]; |
| 1108 | try { |
| 1109 | perm = AppGlobals.getPackageManager(). |
| 1110 | checkPermission(requiredPermission, |
| 1111 | info.activityInfo.applicationInfo.packageName, |
| 1112 | UserHandle |
| 1113 | .getUserId(info.activityInfo.applicationInfo.uid)); |
| 1114 | } catch (RemoteException e) { |
| 1115 | perm = PackageManager.PERMISSION_DENIED; |
| 1116 | } |
| 1117 | if (perm != PackageManager.PERMISSION_GRANTED) { |
| 1118 | Slog.w(TAG, "Permission Denial: receiving " |
| 1119 | + r.intent + " to " |
| 1120 | + component.flattenToShortString() |
| 1121 | + " requires " + requiredPermission |
| 1122 | + " due to sender " + r.callerPackage |
| 1123 | + " (uid " + r.callingUid + ")"); |
| 1124 | skip = true; |
| 1125 | break; |
| 1126 | } |
| 1127 | int appOp = AppOpsManager.permissionToOpCode(requiredPermission); |
| 1128 | if (appOp != AppOpsManager.OP_NONE && appOp != r.appOp |
| 1129 | && mService.mAppOpsService.noteOperation(appOp, |
Fyodor Kupolov | e37520b | 2015-07-14 22:29:21 +0000 | [diff] [blame] | 1130 | info.activityInfo.applicationInfo.uid, info.activityInfo.packageName) |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 1131 | != AppOpsManager.MODE_ALLOWED) { |
| 1132 | Slog.w(TAG, "Appop Denial: receiving " |
| 1133 | + r.intent + " to " |
| 1134 | + component.flattenToShortString() |
| 1135 | + " requires appop " + AppOpsManager.permissionToOp( |
| 1136 | requiredPermission) |
| 1137 | + " due to sender " + r.callerPackage |
| 1138 | + " (uid " + r.callingUid + ")"); |
| 1139 | skip = true; |
| 1140 | break; |
| 1141 | } |
| 1142 | } |
| 1143 | } |
| 1144 | if (!skip && r.appOp != AppOpsManager.OP_NONE |
| 1145 | && mService.mAppOpsService.noteOperation(r.appOp, |
| 1146 | info.activityInfo.applicationInfo.uid, info.activityInfo.packageName) |
| 1147 | != AppOpsManager.MODE_ALLOWED) { |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 1148 | Slog.w(TAG, "Appop Denial: receiving " |
| 1149 | + r.intent + " to " |
| 1150 | + component.flattenToShortString() |
Fyodor Kupolov | d4fd8c7 | 2015-07-13 19:19:25 -0700 | [diff] [blame] | 1151 | + " requires appop " + AppOpsManager.opToName(r.appOp) |
Svet Ganov | 99b6043 | 2015-06-27 13:15:22 -0700 | [diff] [blame] | 1152 | + " due to sender " + r.callerPackage |
| 1153 | + " (uid " + r.callingUid + ")"); |
| 1154 | skip = true; |
| 1155 | } |
Ben Gruver | 49660c7 | 2013-08-06 19:54:08 -0700 | [diff] [blame] | 1156 | if (!skip) { |
| 1157 | skip = !mService.mIntentFirewall.checkBroadcast(r.intent, r.callingUid, |
| 1158 | r.callingPid, r.resolvedType, info.activityInfo.applicationInfo.uid); |
| 1159 | } |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1160 | boolean isSingleton = false; |
| 1161 | try { |
| 1162 | isSingleton = mService.isSingleton(info.activityInfo.processName, |
| 1163 | info.activityInfo.applicationInfo, |
| 1164 | info.activityInfo.name, info.activityInfo.flags); |
| 1165 | } catch (SecurityException e) { |
| 1166 | Slog.w(TAG, e.getMessage()); |
| 1167 | skip = true; |
| 1168 | } |
| 1169 | if ((info.activityInfo.flags&ActivityInfo.FLAG_SINGLE_USER) != 0) { |
| 1170 | if (ActivityManager.checkUidPermission( |
| 1171 | android.Manifest.permission.INTERACT_ACROSS_USERS, |
| 1172 | info.activityInfo.applicationInfo.uid) |
| 1173 | != PackageManager.PERMISSION_GRANTED) { |
| 1174 | Slog.w(TAG, "Permission Denial: Receiver " + component.flattenToShortString() |
| 1175 | + " requests FLAG_SINGLE_USER, but app does not hold " |
| 1176 | + android.Manifest.permission.INTERACT_ACROSS_USERS); |
| 1177 | skip = true; |
| 1178 | } |
| 1179 | } |
Chad Brubaker | b7e34d5 | 2017-02-22 12:36:06 -0800 | [diff] [blame] | 1180 | if (!skip && info.activityInfo.applicationInfo.isInstantApp() |
Chad Brubaker | b7e34d5 | 2017-02-22 12:36:06 -0800 | [diff] [blame] | 1181 | && r.callingUid != info.activityInfo.applicationInfo.uid) { |
| 1182 | Slog.w(TAG, "Instant App Denial: receiving " |
| 1183 | + r.intent |
| 1184 | + " to " + component.flattenToShortString() |
| 1185 | + " due to sender " + r.callerPackage |
| 1186 | + " (uid " + r.callingUid + ")" |
Chad Brubaker | abd2b66 | 2017-03-16 11:24:16 -0700 | [diff] [blame] | 1187 | + " Instant Apps do not support manifest receivers"); |
Chad Brubaker | b7e34d5 | 2017-02-22 12:36:06 -0800 | [diff] [blame] | 1188 | skip = true; |
| 1189 | } |
Chad Brubaker | 816c83b | 2017-03-02 10:27:59 -0800 | [diff] [blame] | 1190 | if (!skip && r.callerInstantApp |
Todd Kennedy | c05f5d1 | 2017-04-25 11:11:40 -0700 | [diff] [blame] | 1191 | && (info.activityInfo.flags & ActivityInfo.FLAG_VISIBLE_TO_INSTANT_APP) == 0 |
Chad Brubaker | 816c83b | 2017-03-02 10:27:59 -0800 | [diff] [blame] | 1192 | && r.callingUid != info.activityInfo.applicationInfo.uid) { |
| 1193 | Slog.w(TAG, "Instant App Denial: receiving " |
| 1194 | + r.intent |
| 1195 | + " to " + component.flattenToShortString() |
| 1196 | + " requires receiver have visibleToInstantApps set" |
| 1197 | + " due to sender " + r.callerPackage |
| 1198 | + " (uid " + r.callingUid + ")"); |
| 1199 | skip = true; |
| 1200 | } |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1201 | if (r.curApp != null && r.curApp.crashing) { |
| 1202 | // If the target process is crashing, just skip it. |
Dianne Hackborn | 9357b11 | 2013-10-03 18:27:48 -0700 | [diff] [blame] | 1203 | Slog.w(TAG, "Skipping deliver ordered [" + mQueueName + "] " + r |
| 1204 | + " to " + r.curApp + ": process crashing"); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1205 | skip = true; |
| 1206 | } |
Christopher Tate | ba629da | 2013-11-13 17:42:28 -0800 | [diff] [blame] | 1207 | if (!skip) { |
| 1208 | boolean isAvailable = false; |
| 1209 | try { |
| 1210 | isAvailable = AppGlobals.getPackageManager().isPackageAvailable( |
| 1211 | info.activityInfo.packageName, |
| 1212 | UserHandle.getUserId(info.activityInfo.applicationInfo.uid)); |
| 1213 | } catch (Exception e) { |
| 1214 | // all such failures mean we skip this receiver |
| 1215 | Slog.w(TAG, "Exception getting recipient info for " |
| 1216 | + info.activityInfo.packageName, e); |
| 1217 | } |
| 1218 | if (!isAvailable) { |
Wale Ogunwale | 3c36b8e | 2015-03-09 10:18:51 -0700 | [diff] [blame] | 1219 | if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, |
| 1220 | "Skipping delivery to " + info.activityInfo.packageName + " / " |
| 1221 | + info.activityInfo.applicationInfo.uid |
| 1222 | + " : package no longer available"); |
Christopher Tate | ba629da | 2013-11-13 17:42:28 -0800 | [diff] [blame] | 1223 | skip = true; |
| 1224 | } |
| 1225 | } |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1226 | |
Svet Ganov | 9c165d7 | 2015-12-01 19:52:26 -0800 | [diff] [blame] | 1227 | // If permissions need a review before any of the app components can run, we drop |
| 1228 | // the broadcast and if the calling app is in the foreground and the broadcast is |
| 1229 | // explicit we launch the review UI passing it a pending intent to send the skipped |
| 1230 | // broadcast. |
Svet Ganov | 77df6f3 | 2016-08-17 11:46:34 -0700 | [diff] [blame] | 1231 | if (mService.mPermissionReviewRequired && !skip) { |
Svet Ganov | 9c165d7 | 2015-12-01 19:52:26 -0800 | [diff] [blame] | 1232 | if (!requestStartTargetPermissionsReviewIfNeededLocked(r, |
| 1233 | info.activityInfo.packageName, UserHandle.getUserId( |
| 1234 | info.activityInfo.applicationInfo.uid))) { |
| 1235 | skip = true; |
| 1236 | } |
| 1237 | } |
| 1238 | |
Dianne Hackborn | 76e8009 | 2015-12-09 14:15:34 -0800 | [diff] [blame] | 1239 | // This is safe to do even if we are skipping the broadcast, and we need |
| 1240 | // this information now to evaluate whether it is going to be allowed to run. |
| 1241 | final int receiverUid = info.activityInfo.applicationInfo.uid; |
| 1242 | // If it's a singleton, it needs to be the same app or a special app |
| 1243 | if (r.callingUid != Process.SYSTEM_UID && isSingleton |
| 1244 | && mService.isValidSingletonCall(r.callingUid, receiverUid)) { |
| 1245 | info.activityInfo = mService.getActivityInfoForUser(info.activityInfo, 0); |
| 1246 | } |
Dianne Hackborn | 2639c4b | 2015-12-04 13:11:09 -0800 | [diff] [blame] | 1247 | String targetProcess = info.activityInfo.processName; |
| 1248 | ProcessRecord app = mService.getProcessRecordLocked(targetProcess, |
| 1249 | info.activityInfo.applicationInfo.uid, false); |
Dianne Hackborn | 76e8009 | 2015-12-09 14:15:34 -0800 | [diff] [blame] | 1250 | |
Dianne Hackborn | 2639c4b | 2015-12-04 13:11:09 -0800 | [diff] [blame] | 1251 | if (!skip) { |
Dianne Hackborn | c3af19a | 2017-01-20 17:00:44 -0800 | [diff] [blame] | 1252 | final int allowed = mService.getAppStartModeLocked( |
| 1253 | info.activityInfo.applicationInfo.uid, info.activityInfo.packageName, |
| 1254 | info.activityInfo.applicationInfo.targetSdkVersion, -1, true, false); |
Dianne Hackborn | 2639c4b | 2015-12-04 13:11:09 -0800 | [diff] [blame] | 1255 | if (allowed != ActivityManager.APP_START_MODE_NORMAL) { |
| 1256 | // We won't allow this receiver to be launched if the app has been |
Dianne Hackborn | e91f3e7 | 2016-03-25 18:48:15 -0700 | [diff] [blame] | 1257 | // completely disabled from launches, or it was not explicitly sent |
| 1258 | // to it and the app is in a state that should not receive it |
Dianne Hackborn | c3af19a | 2017-01-20 17:00:44 -0800 | [diff] [blame] | 1259 | // (depending on how getAppStartModeLocked has determined that). |
Dianne Hackborn | 6ac42ae | 2015-12-08 17:22:10 -0800 | [diff] [blame] | 1260 | if (allowed == ActivityManager.APP_START_MODE_DISABLED) { |
| 1261 | Slog.w(TAG, "Background execution disabled: receiving " |
| 1262 | + r.intent + " to " |
| 1263 | + component.flattenToShortString()); |
| 1264 | skip = true; |
Dianne Hackborn | e91f3e7 | 2016-03-25 18:48:15 -0700 | [diff] [blame] | 1265 | } else if (((r.intent.getFlags()&Intent.FLAG_RECEIVER_EXCLUDE_BACKGROUND) != 0) |
Dianne Hackborn | 2639c4b | 2015-12-04 13:11:09 -0800 | [diff] [blame] | 1266 | || (r.intent.getComponent() == null |
Dianne Hackborn | e91f3e7 | 2016-03-25 18:48:15 -0700 | [diff] [blame] | 1267 | && r.intent.getPackage() == null |
Dianne Hackborn | 6ac42ae | 2015-12-08 17:22:10 -0800 | [diff] [blame] | 1268 | && ((r.intent.getFlags() |
Dianne Hackborn | b8633f3 | 2017-04-11 17:38:42 -0700 | [diff] [blame] | 1269 | & Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND) == 0) |
| 1270 | && !isSignaturePerm(r.requiredPermissions))) { |
Dianne Hackborn | 7fc46d8 | 2017-02-14 15:20:06 -0800 | [diff] [blame] | 1271 | mService.addBackgroundCheckViolationLocked(r.intent.getAction(), |
| 1272 | component.getPackageName()); |
Dianne Hackborn | 2639c4b | 2015-12-04 13:11:09 -0800 | [diff] [blame] | 1273 | Slog.w(TAG, "Background execution not allowed: receiving " |
| 1274 | + r.intent + " to " |
| 1275 | + component.flattenToShortString()); |
| 1276 | skip = true; |
| 1277 | } |
| 1278 | } |
| 1279 | } |
| 1280 | |
Fyodor Kupolov | 099ae88 | 2017-10-23 11:23:51 -0700 | [diff] [blame] | 1281 | if (!skip && !Intent.ACTION_SHUTDOWN.equals(r.intent.getAction()) |
| 1282 | && !mService.mUserController |
| 1283 | .isUserRunning(UserHandle.getUserId(info.activityInfo.applicationInfo.uid), |
| 1284 | 0 /* flags */)) { |
| 1285 | skip = true; |
| 1286 | Slog.w(TAG, |
| 1287 | "Skipping delivery to " + info.activityInfo.packageName + " / " |
| 1288 | + info.activityInfo.applicationInfo.uid + " : user is not running"); |
| 1289 | } |
| 1290 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1291 | if (skip) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 1292 | if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, |
Wale Ogunwale | 3c36b8e | 2015-03-09 10:18:51 -0700 | [diff] [blame] | 1293 | "Skipping delivery of ordered [" + mQueueName + "] " |
| 1294 | + r + " for whatever reason"); |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 1295 | r.delivery[recIdx] = BroadcastRecord.DELIVERY_SKIPPED; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1296 | r.receiver = null; |
| 1297 | r.curFilter = null; |
| 1298 | r.state = BroadcastRecord.IDLE; |
Fyodor Kupolov | 099ae88 | 2017-10-23 11:23:51 -0700 | [diff] [blame] | 1299 | r.manifestSkipCount++; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1300 | scheduleBroadcastsLocked(); |
| 1301 | return; |
| 1302 | } |
Fyodor Kupolov | 099ae88 | 2017-10-23 11:23:51 -0700 | [diff] [blame] | 1303 | r.manifestCount++; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1304 | |
Dianne Hackborn | e0e413e | 2015-12-09 17:22:26 -0800 | [diff] [blame] | 1305 | r.delivery[recIdx] = BroadcastRecord.DELIVERY_DELIVERED; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1306 | r.state = BroadcastRecord.APP_RECEIVE; |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 1307 | r.curComponent = component; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1308 | r.curReceiver = info.activityInfo; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1309 | if (DEBUG_MU && r.callingUid > UserHandle.PER_USER_RANGE) { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1310 | Slog.v(TAG_MU, "Updated broadcast record activity info for secondary user, " |
| 1311 | + info.activityInfo + ", callingUid = " + r.callingUid + ", uid = " |
Fyodor Kupolov | 099ae88 | 2017-10-23 11:23:51 -0700 | [diff] [blame] | 1312 | + receiverUid); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1313 | } |
| 1314 | |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1315 | if (brOptions != null && brOptions.getTemporaryAppWhitelistDuration() > 0) { |
| 1316 | scheduleTempWhitelistLocked(receiverUid, |
Dianne Hackborn | fd854ee | 2015-07-13 18:00:37 -0700 | [diff] [blame] | 1317 | brOptions.getTemporaryAppWhitelistDuration(), r); |
Dianne Hackborn | a750a63 | 2015-06-16 17:18:23 -0700 | [diff] [blame] | 1318 | } |
| 1319 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1320 | // Broadcast is being executed, its package can't be stopped. |
| 1321 | try { |
| 1322 | AppGlobals.getPackageManager().setPackageStoppedState( |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 1323 | r.curComponent.getPackageName(), false, UserHandle.getUserId(r.callingUid)); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1324 | } catch (RemoteException e) { |
| 1325 | } catch (IllegalArgumentException e) { |
| 1326 | Slog.w(TAG, "Failed trying to unstop package " |
| 1327 | + r.curComponent.getPackageName() + ": " + e); |
| 1328 | } |
| 1329 | |
| 1330 | // Is this receiver's application already running? |
Dianne Hackborn | 443d35a | 2017-06-16 17:59:35 -0700 | [diff] [blame] | 1331 | if (app != null && app.thread != null && !app.killed) { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1332 | try { |
Dianne Hackborn | f7097a5 | 2014-05-13 09:56:14 -0700 | [diff] [blame] | 1333 | app.addPackage(info.activityInfo.packageName, |
| 1334 | info.activityInfo.applicationInfo.versionCode, mService.mProcessStats); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1335 | processCurBroadcastLocked(r, app); |
| 1336 | return; |
| 1337 | } catch (RemoteException e) { |
| 1338 | Slog.w(TAG, "Exception when sending broadcast to " |
| 1339 | + r.curComponent, e); |
Dianne Hackborn | 6c5406a | 2012-11-29 16:18:01 -0800 | [diff] [blame] | 1340 | } catch (RuntimeException e) { |
Dianne Hackborn | 8d05172 | 2014-10-01 14:59:58 -0700 | [diff] [blame] | 1341 | Slog.wtf(TAG, "Failed sending broadcast to " |
Dianne Hackborn | 6c5406a | 2012-11-29 16:18:01 -0800 | [diff] [blame] | 1342 | + r.curComponent + " with " + r.intent, e); |
| 1343 | // If some unexpected exception happened, just skip |
| 1344 | // this broadcast. At this point we are not in the call |
| 1345 | // from a client, so throwing an exception out from here |
| 1346 | // will crash the entire system instead of just whoever |
| 1347 | // sent the broadcast. |
| 1348 | logBroadcastReceiverDiscardLocked(r); |
| 1349 | finishReceiverLocked(r, r.resultCode, r.resultData, |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 1350 | r.resultExtras, r.resultAbort, false); |
Dianne Hackborn | 6c5406a | 2012-11-29 16:18:01 -0800 | [diff] [blame] | 1351 | scheduleBroadcastsLocked(); |
| 1352 | // We need to reset the state if we failed to start the receiver. |
| 1353 | r.state = BroadcastRecord.IDLE; |
| 1354 | return; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | // If a dead object exception was thrown -- fall through to |
| 1358 | // restart the application. |
| 1359 | } |
| 1360 | |
| 1361 | // Not running -- get it started, to be executed when the app comes up. |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 1362 | if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1363 | "Need to start app [" |
| 1364 | + mQueueName + "] " + targetProcess + " for broadcast " + r); |
| 1365 | if ((r.curApp=mService.startProcessLocked(targetProcess, |
| 1366 | info.activityInfo.applicationInfo, true, |
| 1367 | r.intent.getFlags() | Intent.FLAG_FROM_BACKGROUND, |
| 1368 | "broadcast", r.curComponent, |
Dianne Hackborn | 3bc8f78d | 2013-09-19 13:34:35 -0700 | [diff] [blame] | 1369 | (r.intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0, false, false)) |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1370 | == null) { |
| 1371 | // Ah, this recipient is unavailable. Finish it if necessary, |
| 1372 | // and mark the broadcast record as ready for the next. |
| 1373 | Slog.w(TAG, "Unable to launch app " |
| 1374 | + info.activityInfo.applicationInfo.packageName + "/" |
Fyodor Kupolov | 099ae88 | 2017-10-23 11:23:51 -0700 | [diff] [blame] | 1375 | + receiverUid + " for broadcast " |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1376 | + r.intent + ": process is bad"); |
| 1377 | logBroadcastReceiverDiscardLocked(r); |
| 1378 | finishReceiverLocked(r, r.resultCode, r.resultData, |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 1379 | r.resultExtras, r.resultAbort, false); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1380 | scheduleBroadcastsLocked(); |
| 1381 | r.state = BroadcastRecord.IDLE; |
| 1382 | return; |
| 1383 | } |
| 1384 | |
| 1385 | mPendingBroadcast = r; |
| 1386 | mPendingBroadcastRecvIndex = recIdx; |
| 1387 | } |
| 1388 | } |
| 1389 | |
| 1390 | final void setBroadcastTimeoutLocked(long timeoutTime) { |
| 1391 | if (! mPendingBroadcastTimeoutMessage) { |
| 1392 | Message msg = mHandler.obtainMessage(BROADCAST_TIMEOUT_MSG, this); |
| 1393 | mHandler.sendMessageAtTime(msg, timeoutTime); |
| 1394 | mPendingBroadcastTimeoutMessage = true; |
| 1395 | } |
| 1396 | } |
| 1397 | |
| 1398 | final void cancelBroadcastTimeoutLocked() { |
| 1399 | if (mPendingBroadcastTimeoutMessage) { |
| 1400 | mHandler.removeMessages(BROADCAST_TIMEOUT_MSG, this); |
| 1401 | mPendingBroadcastTimeoutMessage = false; |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | final void broadcastTimeoutLocked(boolean fromMsg) { |
| 1406 | if (fromMsg) { |
| 1407 | mPendingBroadcastTimeoutMessage = false; |
| 1408 | } |
| 1409 | |
| 1410 | if (mOrderedBroadcasts.size() == 0) { |
| 1411 | return; |
| 1412 | } |
| 1413 | |
| 1414 | long now = SystemClock.uptimeMillis(); |
| 1415 | BroadcastRecord r = mOrderedBroadcasts.get(0); |
| 1416 | if (fromMsg) { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1417 | if (!mService.mProcessesReady) { |
| 1418 | // Only process broadcast timeouts if the system is ready. That way |
| 1419 | // PRE_BOOT_COMPLETED broadcasts can't timeout as they are intended |
| 1420 | // to do heavy lifting for system up. |
| 1421 | return; |
| 1422 | } |
| 1423 | |
| 1424 | long timeoutTime = r.receiverTime + mTimeoutPeriod; |
| 1425 | if (timeoutTime > now) { |
| 1426 | // We can observe premature timeouts because we do not cancel and reset the |
| 1427 | // broadcast timeout message after each receiver finishes. Instead, we set up |
| 1428 | // an initial timeout then kick it down the road a little further as needed |
| 1429 | // when it expires. |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 1430 | if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1431 | "Premature timeout [" |
| 1432 | + mQueueName + "] @ " + now + ": resetting BROADCAST_TIMEOUT_MSG for " |
| 1433 | + timeoutTime); |
| 1434 | setBroadcastTimeoutLocked(timeoutTime); |
| 1435 | return; |
| 1436 | } |
| 1437 | } |
| 1438 | |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 1439 | BroadcastRecord br = mOrderedBroadcasts.get(0); |
| 1440 | if (br.state == BroadcastRecord.WAITING_SERVICES) { |
| 1441 | // In this case the broadcast had already finished, but we had decided to wait |
| 1442 | // for started services to finish as well before going on. So if we have actually |
| 1443 | // waited long enough time timeout the broadcast, let's give up on the whole thing |
| 1444 | // and just move on to the next. |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame] | 1445 | Slog.i(TAG, "Waited long enough for: " + (br.curComponent != null |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 1446 | ? br.curComponent.flattenToShortString() : "(null)")); |
| 1447 | br.curComponent = null; |
| 1448 | br.state = BroadcastRecord.IDLE; |
| 1449 | processNextBroadcast(false); |
| 1450 | return; |
| 1451 | } |
| 1452 | |
| 1453 | Slog.w(TAG, "Timeout of broadcast " + r + " - receiver=" + r. receiver |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1454 | + ", started " + (now - r.receiverTime) + "ms ago"); |
| 1455 | r.receiverTime = now; |
| 1456 | r.anrCount++; |
| 1457 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1458 | ProcessRecord app = null; |
| 1459 | String anrMessage = null; |
| 1460 | |
Christopher Tate | a6b2c88 | 2017-09-01 11:41:39 -0700 | [diff] [blame] | 1461 | Object curReceiver; |
| 1462 | if (r.nextReceiver > 0) { |
| 1463 | curReceiver = r.receivers.get(r.nextReceiver-1); |
| 1464 | r.delivery[r.nextReceiver-1] = BroadcastRecord.DELIVERY_TIMEOUT; |
| 1465 | } else { |
| 1466 | curReceiver = r.curReceiver; |
| 1467 | } |
| 1468 | Slog.w(TAG, "Receiver during timeout of " + r + " : " + curReceiver); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1469 | logBroadcastReceiverDiscardLocked(r); |
Christopher Tate | a6b2c88 | 2017-09-01 11:41:39 -0700 | [diff] [blame] | 1470 | if (curReceiver != null && curReceiver instanceof BroadcastFilter) { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1471 | BroadcastFilter bf = (BroadcastFilter)curReceiver; |
| 1472 | if (bf.receiverList.pid != 0 |
| 1473 | && bf.receiverList.pid != ActivityManagerService.MY_PID) { |
| 1474 | synchronized (mService.mPidsSelfLocked) { |
| 1475 | app = mService.mPidsSelfLocked.get( |
| 1476 | bf.receiverList.pid); |
| 1477 | } |
| 1478 | } |
| 1479 | } else { |
| 1480 | app = r.curApp; |
| 1481 | } |
| 1482 | |
| 1483 | if (app != null) { |
| 1484 | anrMessage = "Broadcast of " + r.intent.toString(); |
| 1485 | } |
| 1486 | |
| 1487 | if (mPendingBroadcast == r) { |
| 1488 | mPendingBroadcast = null; |
| 1489 | } |
| 1490 | |
| 1491 | // Move on to the next receiver. |
| 1492 | finishReceiverLocked(r, r.resultCode, r.resultData, |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 1493 | r.resultExtras, r.resultAbort, false); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1494 | scheduleBroadcastsLocked(); |
| 1495 | |
| 1496 | if (anrMessage != null) { |
| 1497 | // Post the ANR to the handler since we do not want to process ANRs while |
| 1498 | // potentially holding our lock. |
| 1499 | mHandler.post(new AppNotResponding(app, anrMessage)); |
| 1500 | } |
| 1501 | } |
| 1502 | |
Christopher Tate | f278f12 | 2015-04-22 13:12:01 -0700 | [diff] [blame] | 1503 | private final int ringAdvance(int x, final int increment, final int ringSize) { |
| 1504 | x += increment; |
| 1505 | if (x < 0) return (ringSize - 1); |
| 1506 | else if (x >= ringSize) return 0; |
| 1507 | else return x; |
| 1508 | } |
| 1509 | |
Makoto Onuki | 97f82f2 | 2017-05-31 16:20:21 -0700 | [diff] [blame] | 1510 | private final void addBroadcastToHistoryLocked(BroadcastRecord original) { |
| 1511 | if (original.callingUid < 0) { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1512 | // This was from a registerReceiver() call; ignore it. |
| 1513 | return; |
| 1514 | } |
Makoto Onuki | 97f82f2 | 2017-05-31 16:20:21 -0700 | [diff] [blame] | 1515 | original.finishTime = SystemClock.uptimeMillis(); |
Christopher Tate | f278f12 | 2015-04-22 13:12:01 -0700 | [diff] [blame] | 1516 | |
Carmen Jackson | a68e345 | 2017-01-17 14:01:33 -0800 | [diff] [blame] | 1517 | if (Trace.isTagEnabled(Trace.TRACE_TAG_ACTIVITY_MANAGER)) { |
| 1518 | Trace.asyncTraceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER, |
Makoto Onuki | 97f82f2 | 2017-05-31 16:20:21 -0700 | [diff] [blame] | 1519 | createBroadcastTraceTitle(original, BroadcastRecord.DELIVERY_DELIVERED), |
| 1520 | System.identityHashCode(original)); |
Carmen Jackson | a68e345 | 2017-01-17 14:01:33 -0800 | [diff] [blame] | 1521 | } |
| 1522 | |
Makoto Onuki | 97f82f2 | 2017-05-31 16:20:21 -0700 | [diff] [blame] | 1523 | // Note sometimes (only for sticky broadcasts?) we reuse BroadcastRecords, |
| 1524 | // So don't change the incoming record directly. |
| 1525 | final BroadcastRecord historyRecord = original.maybeStripForHistory(); |
| 1526 | |
| 1527 | mBroadcastHistory[mHistoryNext] = historyRecord; |
Christopher Tate | f278f12 | 2015-04-22 13:12:01 -0700 | [diff] [blame] | 1528 | mHistoryNext = ringAdvance(mHistoryNext, 1, MAX_BROADCAST_HISTORY); |
| 1529 | |
Makoto Onuki | 97f82f2 | 2017-05-31 16:20:21 -0700 | [diff] [blame] | 1530 | mBroadcastSummaryHistory[mSummaryHistoryNext] = historyRecord.intent; |
| 1531 | mSummaryHistoryEnqueueTime[mSummaryHistoryNext] = historyRecord.enqueueClockTime; |
| 1532 | mSummaryHistoryDispatchTime[mSummaryHistoryNext] = historyRecord.dispatchClockTime; |
Christopher Tate | f278f12 | 2015-04-22 13:12:01 -0700 | [diff] [blame] | 1533 | mSummaryHistoryFinishTime[mSummaryHistoryNext] = System.currentTimeMillis(); |
| 1534 | mSummaryHistoryNext = ringAdvance(mSummaryHistoryNext, 1, MAX_BROADCAST_SUMMARY_HISTORY); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1535 | } |
| 1536 | |
Wale Ogunwale | ca1c125 | 2015-05-15 12:49:13 -0700 | [diff] [blame] | 1537 | boolean cleanupDisabledPackageReceiversLocked( |
| 1538 | String packageName, Set<String> filterByClasses, int userId, boolean doit) { |
| 1539 | boolean didSomething = false; |
| 1540 | for (int i = mParallelBroadcasts.size() - 1; i >= 0; i--) { |
| 1541 | didSomething |= mParallelBroadcasts.get(i).cleanupDisabledPackageReceiversLocked( |
| 1542 | packageName, filterByClasses, userId, doit); |
| 1543 | if (!doit && didSomething) { |
| 1544 | return true; |
| 1545 | } |
| 1546 | } |
| 1547 | |
| 1548 | for (int i = mOrderedBroadcasts.size() - 1; i >= 0; i--) { |
| 1549 | didSomething |= mOrderedBroadcasts.get(i).cleanupDisabledPackageReceiversLocked( |
| 1550 | packageName, filterByClasses, userId, doit); |
| 1551 | if (!doit && didSomething) { |
| 1552 | return true; |
| 1553 | } |
| 1554 | } |
| 1555 | |
| 1556 | return didSomething; |
| 1557 | } |
| 1558 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1559 | final void logBroadcastReceiverDiscardLocked(BroadcastRecord r) { |
Wale Ogunwale | a22c632 | 2015-06-03 09:59:45 -0700 | [diff] [blame] | 1560 | final int logIndex = r.nextReceiver - 1; |
| 1561 | if (logIndex >= 0 && logIndex < r.receivers.size()) { |
| 1562 | Object curReceiver = r.receivers.get(logIndex); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1563 | if (curReceiver instanceof BroadcastFilter) { |
| 1564 | BroadcastFilter bf = (BroadcastFilter) curReceiver; |
| 1565 | EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_FILTER, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1566 | bf.owningUserId, System.identityHashCode(r), |
Wale Ogunwale | a22c632 | 2015-06-03 09:59:45 -0700 | [diff] [blame] | 1567 | r.intent.getAction(), logIndex, System.identityHashCode(bf)); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1568 | } else { |
Wale Ogunwale | a22c632 | 2015-06-03 09:59:45 -0700 | [diff] [blame] | 1569 | ResolveInfo ri = (ResolveInfo) curReceiver; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1570 | EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1571 | UserHandle.getUserId(ri.activityInfo.applicationInfo.uid), |
Wale Ogunwale | a22c632 | 2015-06-03 09:59:45 -0700 | [diff] [blame] | 1572 | System.identityHashCode(r), r.intent.getAction(), logIndex, ri.toString()); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1573 | } |
| 1574 | } else { |
Wale Ogunwale | a22c632 | 2015-06-03 09:59:45 -0700 | [diff] [blame] | 1575 | if (logIndex < 0) Slog.w(TAG, |
| 1576 | "Discarding broadcast before first receiver is invoked: " + r); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1577 | EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1578 | -1, System.identityHashCode(r), |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1579 | r.intent.getAction(), |
| 1580 | r.nextReceiver, |
| 1581 | "NONE"); |
| 1582 | } |
| 1583 | } |
| 1584 | |
Carmen Jackson | a68e345 | 2017-01-17 14:01:33 -0800 | [diff] [blame] | 1585 | private String createBroadcastTraceTitle(BroadcastRecord record, int state) { |
| 1586 | return String.format("Broadcast %s from %s (%s) %s", |
| 1587 | state == BroadcastRecord.DELIVERY_PENDING ? "in queue" : "dispatched", |
| 1588 | record.callerPackage == null ? "" : record.callerPackage, |
| 1589 | record.callerApp == null ? "process unknown" : record.callerApp.toShortString(), |
| 1590 | record.intent == null ? "" : record.intent.getAction()); |
| 1591 | } |
| 1592 | |
Jeff Sharkey | fd65813 | 2017-05-03 11:38:01 -0600 | [diff] [blame] | 1593 | final boolean isIdle() { |
| 1594 | return mParallelBroadcasts.isEmpty() && mOrderedBroadcasts.isEmpty() |
| 1595 | && (mPendingBroadcast == null); |
| 1596 | } |
| 1597 | |
Yi Jin | 129fc6c | 2017-09-28 15:48:38 -0700 | [diff] [blame] | 1598 | void writeToProto(ProtoOutputStream proto, long fieldId) { |
| 1599 | long token = proto.start(fieldId); |
| 1600 | proto.write(BroadcastQueueProto.QUEUE_NAME, mQueueName); |
| 1601 | int N; |
| 1602 | N = mParallelBroadcasts.size(); |
| 1603 | for (int i = N - 1; i >= 0; i--) { |
| 1604 | mParallelBroadcasts.get(i).writeToProto(proto, BroadcastQueueProto.PARALLEL_BROADCASTS); |
| 1605 | } |
| 1606 | N = mOrderedBroadcasts.size(); |
| 1607 | for (int i = N - 1; i >= 0; i--) { |
| 1608 | mOrderedBroadcasts.get(i).writeToProto(proto, BroadcastQueueProto.ORDERED_BROADCASTS); |
| 1609 | } |
| 1610 | if (mPendingBroadcast != null) { |
| 1611 | mPendingBroadcast.writeToProto(proto, BroadcastQueueProto.PENDING_BROADCAST); |
| 1612 | } |
| 1613 | |
| 1614 | int lastIndex = mHistoryNext; |
| 1615 | int ringIndex = lastIndex; |
| 1616 | do { |
| 1617 | // increasing index = more recent entry, and we want to print the most |
| 1618 | // recent first and work backwards, so we roll through the ring backwards. |
| 1619 | ringIndex = ringAdvance(ringIndex, -1, MAX_BROADCAST_HISTORY); |
| 1620 | BroadcastRecord r = mBroadcastHistory[ringIndex]; |
| 1621 | if (r != null) { |
| 1622 | r.writeToProto(proto, BroadcastQueueProto.HISTORICAL_BROADCASTS); |
| 1623 | } |
| 1624 | } while (ringIndex != lastIndex); |
| 1625 | |
| 1626 | lastIndex = ringIndex = mSummaryHistoryNext; |
| 1627 | do { |
| 1628 | ringIndex = ringAdvance(ringIndex, -1, MAX_BROADCAST_SUMMARY_HISTORY); |
| 1629 | Intent intent = mBroadcastSummaryHistory[ringIndex]; |
| 1630 | if (intent == null) { |
| 1631 | continue; |
| 1632 | } |
| 1633 | long summaryToken = proto.start(BroadcastQueueProto.HISTORICAL_BROADCASTS_SUMMARY); |
| 1634 | intent.writeToProto(proto, BroadcastQueueProto.BroadcastSummary.INTENT, |
| 1635 | false, true, true, false); |
| 1636 | proto.write(BroadcastQueueProto.BroadcastSummary.ENQUEUE_CLOCK_TIME_MS, |
| 1637 | mSummaryHistoryEnqueueTime[ringIndex]); |
| 1638 | proto.write(BroadcastQueueProto.BroadcastSummary.DISPATCH_CLOCK_TIME_MS, |
| 1639 | mSummaryHistoryDispatchTime[ringIndex]); |
| 1640 | proto.write(BroadcastQueueProto.BroadcastSummary.FINISH_CLOCK_TIME_MS, |
| 1641 | mSummaryHistoryFinishTime[ringIndex]); |
| 1642 | proto.end(summaryToken); |
| 1643 | } while (ringIndex != lastIndex); |
| 1644 | proto.end(token); |
| 1645 | } |
| 1646 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1647 | final boolean dumpLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 1648 | int opti, boolean dumpAll, String dumpPackage, boolean needSep) { |
Dianne Hackborn | 865907d | 2015-10-21 17:12:53 -0700 | [diff] [blame] | 1649 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1650 | if (mParallelBroadcasts.size() > 0 || mOrderedBroadcasts.size() > 0 |
| 1651 | || mPendingBroadcast != null) { |
| 1652 | boolean printed = false; |
Wale Ogunwale | ca1c125 | 2015-05-15 12:49:13 -0700 | [diff] [blame] | 1653 | for (int i = mParallelBroadcasts.size() - 1; i >= 0; i--) { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1654 | BroadcastRecord br = mParallelBroadcasts.get(i); |
| 1655 | if (dumpPackage != null && !dumpPackage.equals(br.callerPackage)) { |
| 1656 | continue; |
| 1657 | } |
| 1658 | if (!printed) { |
| 1659 | if (needSep) { |
| 1660 | pw.println(); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1661 | } |
Dianne Hackborn | 6cbd33f | 2012-09-17 18:28:24 -0700 | [diff] [blame] | 1662 | needSep = true; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1663 | printed = true; |
| 1664 | pw.println(" Active broadcasts [" + mQueueName + "]:"); |
| 1665 | } |
Dianne Hackborn | 6cbd33f | 2012-09-17 18:28:24 -0700 | [diff] [blame] | 1666 | pw.println(" Active Broadcast " + mQueueName + " #" + i + ":"); |
Dianne Hackborn | 865907d | 2015-10-21 17:12:53 -0700 | [diff] [blame] | 1667 | br.dump(pw, " ", sdf); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1668 | } |
| 1669 | printed = false; |
| 1670 | needSep = true; |
Wale Ogunwale | ca1c125 | 2015-05-15 12:49:13 -0700 | [diff] [blame] | 1671 | for (int i = mOrderedBroadcasts.size() - 1; i >= 0; i--) { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1672 | BroadcastRecord br = mOrderedBroadcasts.get(i); |
| 1673 | if (dumpPackage != null && !dumpPackage.equals(br.callerPackage)) { |
| 1674 | continue; |
| 1675 | } |
| 1676 | if (!printed) { |
| 1677 | if (needSep) { |
| 1678 | pw.println(); |
| 1679 | } |
| 1680 | needSep = true; |
Dianne Hackborn | 6cbd33f | 2012-09-17 18:28:24 -0700 | [diff] [blame] | 1681 | printed = true; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1682 | pw.println(" Active ordered broadcasts [" + mQueueName + "]:"); |
| 1683 | } |
Dianne Hackborn | 6cbd33f | 2012-09-17 18:28:24 -0700 | [diff] [blame] | 1684 | pw.println(" Active Ordered Broadcast " + mQueueName + " #" + i + ":"); |
Dianne Hackborn | 865907d | 2015-10-21 17:12:53 -0700 | [diff] [blame] | 1685 | mOrderedBroadcasts.get(i).dump(pw, " ", sdf); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1686 | } |
| 1687 | if (dumpPackage == null || (mPendingBroadcast != null |
| 1688 | && dumpPackage.equals(mPendingBroadcast.callerPackage))) { |
| 1689 | if (needSep) { |
| 1690 | pw.println(); |
| 1691 | } |
| 1692 | pw.println(" Pending broadcast [" + mQueueName + "]:"); |
| 1693 | if (mPendingBroadcast != null) { |
Dianne Hackborn | 865907d | 2015-10-21 17:12:53 -0700 | [diff] [blame] | 1694 | mPendingBroadcast.dump(pw, " ", sdf); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1695 | } else { |
| 1696 | pw.println(" (null)"); |
| 1697 | } |
| 1698 | needSep = true; |
| 1699 | } |
| 1700 | } |
| 1701 | |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 1702 | int i; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1703 | boolean printed = false; |
Christopher Tate | f278f12 | 2015-04-22 13:12:01 -0700 | [diff] [blame] | 1704 | |
| 1705 | i = -1; |
| 1706 | int lastIndex = mHistoryNext; |
| 1707 | int ringIndex = lastIndex; |
| 1708 | do { |
| 1709 | // increasing index = more recent entry, and we want to print the most |
| 1710 | // recent first and work backwards, so we roll through the ring backwards. |
| 1711 | ringIndex = ringAdvance(ringIndex, -1, MAX_BROADCAST_HISTORY); |
| 1712 | BroadcastRecord r = mBroadcastHistory[ringIndex]; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1713 | if (r == null) { |
Christopher Tate | f278f12 | 2015-04-22 13:12:01 -0700 | [diff] [blame] | 1714 | continue; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1715 | } |
Christopher Tate | f278f12 | 2015-04-22 13:12:01 -0700 | [diff] [blame] | 1716 | |
| 1717 | i++; // genuine record of some sort even if we're filtering it out |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1718 | if (dumpPackage != null && !dumpPackage.equals(r.callerPackage)) { |
| 1719 | continue; |
| 1720 | } |
| 1721 | if (!printed) { |
| 1722 | if (needSep) { |
| 1723 | pw.println(); |
| 1724 | } |
| 1725 | needSep = true; |
| 1726 | pw.println(" Historical broadcasts [" + mQueueName + "]:"); |
| 1727 | printed = true; |
| 1728 | } |
| 1729 | if (dumpAll) { |
Dianne Hackborn | 6cbd33f | 2012-09-17 18:28:24 -0700 | [diff] [blame] | 1730 | pw.print(" Historical Broadcast " + mQueueName + " #"); |
| 1731 | pw.print(i); pw.println(":"); |
Dianne Hackborn | 865907d | 2015-10-21 17:12:53 -0700 | [diff] [blame] | 1732 | r.dump(pw, " ", sdf); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1733 | } else { |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 1734 | pw.print(" #"); pw.print(i); pw.print(": "); pw.println(r); |
| 1735 | pw.print(" "); |
| 1736 | pw.println(r.intent.toShortString(false, true, true, false)); |
Dianne Hackborn | a40cfeb | 2013-03-25 17:49:36 -0700 | [diff] [blame] | 1737 | if (r.targetComp != null && r.targetComp != r.intent.getComponent()) { |
| 1738 | pw.print(" targetComp: "); pw.println(r.targetComp.toShortString()); |
| 1739 | } |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 1740 | Bundle bundle = r.intent.getExtras(); |
| 1741 | if (bundle != null) { |
| 1742 | pw.print(" extras: "); pw.println(bundle.toString()); |
| 1743 | } |
| 1744 | } |
Christopher Tate | f278f12 | 2015-04-22 13:12:01 -0700 | [diff] [blame] | 1745 | } while (ringIndex != lastIndex); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 1746 | |
| 1747 | if (dumpPackage == null) { |
Christopher Tate | f278f12 | 2015-04-22 13:12:01 -0700 | [diff] [blame] | 1748 | lastIndex = ringIndex = mSummaryHistoryNext; |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 1749 | if (dumpAll) { |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 1750 | printed = false; |
Christopher Tate | f278f12 | 2015-04-22 13:12:01 -0700 | [diff] [blame] | 1751 | i = -1; |
| 1752 | } else { |
| 1753 | // roll over the 'i' full dumps that have already been issued |
| 1754 | for (int j = i; |
| 1755 | j > 0 && ringIndex != lastIndex;) { |
| 1756 | ringIndex = ringAdvance(ringIndex, -1, MAX_BROADCAST_SUMMARY_HISTORY); |
| 1757 | BroadcastRecord r = mBroadcastHistory[ringIndex]; |
| 1758 | if (r == null) { |
| 1759 | continue; |
| 1760 | } |
| 1761 | j--; |
| 1762 | } |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 1763 | } |
Christopher Tate | f278f12 | 2015-04-22 13:12:01 -0700 | [diff] [blame] | 1764 | // done skipping; dump the remainder of the ring. 'i' is still the ordinal within |
| 1765 | // the overall broadcast history. |
Christopher Tate | f278f12 | 2015-04-22 13:12:01 -0700 | [diff] [blame] | 1766 | do { |
| 1767 | ringIndex = ringAdvance(ringIndex, -1, MAX_BROADCAST_SUMMARY_HISTORY); |
| 1768 | Intent intent = mBroadcastSummaryHistory[ringIndex]; |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 1769 | if (intent == null) { |
Christopher Tate | f278f12 | 2015-04-22 13:12:01 -0700 | [diff] [blame] | 1770 | continue; |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 1771 | } |
| 1772 | if (!printed) { |
| 1773 | if (needSep) { |
| 1774 | pw.println(); |
| 1775 | } |
| 1776 | needSep = true; |
| 1777 | pw.println(" Historical broadcasts summary [" + mQueueName + "]:"); |
| 1778 | printed = true; |
| 1779 | } |
| 1780 | if (!dumpAll && i >= 50) { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1781 | pw.println(" ..."); |
| 1782 | break; |
| 1783 | } |
Christopher Tate | f278f12 | 2015-04-22 13:12:01 -0700 | [diff] [blame] | 1784 | i++; |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 1785 | pw.print(" #"); pw.print(i); pw.print(": "); |
| 1786 | pw.println(intent.toShortString(false, true, true, false)); |
Dianne Hackborn | 865907d | 2015-10-21 17:12:53 -0700 | [diff] [blame] | 1787 | pw.print(" "); |
| 1788 | TimeUtils.formatDuration(mSummaryHistoryDispatchTime[ringIndex] |
| 1789 | - mSummaryHistoryEnqueueTime[ringIndex], pw); |
| 1790 | pw.print(" dispatch "); |
| 1791 | TimeUtils.formatDuration(mSummaryHistoryFinishTime[ringIndex] |
| 1792 | - mSummaryHistoryDispatchTime[ringIndex], pw); |
| 1793 | pw.println(" finish"); |
| 1794 | pw.print(" enq="); |
| 1795 | pw.print(sdf.format(new Date(mSummaryHistoryEnqueueTime[ringIndex]))); |
| 1796 | pw.print(" disp="); |
| 1797 | pw.print(sdf.format(new Date(mSummaryHistoryDispatchTime[ringIndex]))); |
| 1798 | pw.print(" fin="); |
| 1799 | pw.println(sdf.format(new Date(mSummaryHistoryFinishTime[ringIndex]))); |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 1800 | Bundle bundle = intent.getExtras(); |
| 1801 | if (bundle != null) { |
| 1802 | pw.print(" extras: "); pw.println(bundle.toString()); |
| 1803 | } |
Christopher Tate | f278f12 | 2015-04-22 13:12:01 -0700 | [diff] [blame] | 1804 | } while (ringIndex != lastIndex); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1805 | } |
| 1806 | |
| 1807 | return needSep; |
| 1808 | } |
| 1809 | } |