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