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 | |
| 19 | import java.io.FileDescriptor; |
| 20 | import java.io.PrintWriter; |
| 21 | import java.util.ArrayList; |
| 22 | |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 23 | import android.app.ActivityManager; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 24 | import android.app.AppGlobals; |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 25 | import android.app.AppOpsManager; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 26 | import android.content.ComponentName; |
| 27 | import android.content.IIntentReceiver; |
| 28 | import android.content.Intent; |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 29 | import android.content.pm.ActivityInfo; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 30 | import android.content.pm.PackageManager; |
| 31 | import android.content.pm.ResolveInfo; |
| 32 | import android.os.Bundle; |
| 33 | import android.os.Handler; |
| 34 | import android.os.IBinder; |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 35 | import android.os.Looper; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 36 | import android.os.Message; |
| 37 | import android.os.Process; |
| 38 | import android.os.RemoteException; |
| 39 | import android.os.SystemClock; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 40 | import android.os.UserHandle; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 41 | import android.util.EventLog; |
| 42 | import android.util.Slog; |
| 43 | |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame^] | 44 | import static com.android.server.am.ActivityManagerDebugConfig.*; |
| 45 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 46 | /** |
| 47 | * BROADCASTS |
| 48 | * |
| 49 | * We keep two broadcast queues and associated bookkeeping, one for those at |
| 50 | * foreground priority, and one for normal (background-priority) broadcasts. |
| 51 | */ |
Dianne Hackborn | be4e6aa | 2013-06-07 13:25:29 -0700 | [diff] [blame] | 52 | public final class BroadcastQueue { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame^] | 53 | private static final String TAG = TAG_WITH_CLASS_NAME ? "BroadcastQueue" : TAG_AM; |
| 54 | private static final String TAG_MU = TAG + POSTFIX_MU; |
| 55 | private static final String TAG_BROADCAST = TAG + POSTFIX_BROADCAST; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 56 | |
Dianne Hackborn | 4c51de4 | 2013-10-16 23:34:35 -0700 | [diff] [blame] | 57 | static final int MAX_BROADCAST_HISTORY = ActivityManager.isLowRamDeviceStatic() ? 10 : 50; |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 58 | static final int MAX_BROADCAST_SUMMARY_HISTORY |
Dianne Hackborn | 4c51de4 | 2013-10-16 23:34:35 -0700 | [diff] [blame] | 59 | = ActivityManager.isLowRamDeviceStatic() ? 25 : 300; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 60 | |
| 61 | final ActivityManagerService mService; |
| 62 | |
| 63 | /** |
| 64 | * Recognizable moniker for this queue |
| 65 | */ |
| 66 | final String mQueueName; |
| 67 | |
| 68 | /** |
| 69 | * Timeout period for this queue's broadcasts |
| 70 | */ |
| 71 | final long mTimeoutPeriod; |
| 72 | |
| 73 | /** |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 74 | * If true, we can delay broadcasts while waiting services to finish in the previous |
| 75 | * receiver's process. |
| 76 | */ |
| 77 | final boolean mDelayBehindServices; |
| 78 | |
| 79 | /** |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 80 | * Lists of all active broadcasts that are to be executed immediately |
| 81 | * (without waiting for another broadcast to finish). Currently this only |
| 82 | * contains broadcasts to registered receivers, to avoid spinning up |
| 83 | * a bunch of processes to execute IntentReceiver components. Background- |
| 84 | * and foreground-priority broadcasts are queued separately. |
| 85 | */ |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 86 | final ArrayList<BroadcastRecord> mParallelBroadcasts = new ArrayList<BroadcastRecord>(); |
| 87 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 88 | /** |
| 89 | * List of all active broadcasts that are to be executed one at a time. |
| 90 | * The object at the top of the list is the currently activity broadcasts; |
| 91 | * those after it are waiting for the top to finish. As with parallel |
| 92 | * broadcasts, separate background- and foreground-priority queues are |
| 93 | * maintained. |
| 94 | */ |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 95 | final ArrayList<BroadcastRecord> mOrderedBroadcasts = new ArrayList<BroadcastRecord>(); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 96 | |
| 97 | /** |
| 98 | * Historical data of past broadcasts, for debugging. |
| 99 | */ |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 100 | final BroadcastRecord[] mBroadcastHistory = new BroadcastRecord[MAX_BROADCAST_HISTORY]; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 101 | |
| 102 | /** |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 103 | * Summary of historical data of past broadcasts, for debugging. |
| 104 | */ |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 105 | final Intent[] mBroadcastSummaryHistory = new Intent[MAX_BROADCAST_SUMMARY_HISTORY]; |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 106 | |
| 107 | /** |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 108 | * Set when we current have a BROADCAST_INTENT_MSG in flight. |
| 109 | */ |
| 110 | boolean mBroadcastsScheduled = false; |
| 111 | |
| 112 | /** |
| 113 | * True if we have a pending unexpired BROADCAST_TIMEOUT_MSG posted to our handler. |
| 114 | */ |
| 115 | boolean mPendingBroadcastTimeoutMessage; |
| 116 | |
| 117 | /** |
| 118 | * Intent broadcasts that we have tried to start, but are |
| 119 | * waiting for the application's process to be created. We only |
| 120 | * need one per scheduling class (instead of a list) because we always |
| 121 | * process broadcasts one at a time, so no others can be started while |
| 122 | * waiting for this one. |
| 123 | */ |
| 124 | BroadcastRecord mPendingBroadcast = null; |
| 125 | |
| 126 | /** |
| 127 | * The receiver index that is pending, to restart the broadcast if needed. |
| 128 | */ |
| 129 | int mPendingBroadcastRecvIndex; |
| 130 | |
| 131 | static final int BROADCAST_INTENT_MSG = ActivityManagerService.FIRST_BROADCAST_QUEUE_MSG; |
| 132 | static final int BROADCAST_TIMEOUT_MSG = ActivityManagerService.FIRST_BROADCAST_QUEUE_MSG + 1; |
| 133 | |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 134 | final BroadcastHandler mHandler; |
| 135 | |
| 136 | private final class BroadcastHandler extends Handler { |
| 137 | public BroadcastHandler(Looper looper) { |
| 138 | super(looper, null, true); |
| 139 | } |
| 140 | |
| 141 | @Override |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 142 | public void handleMessage(Message msg) { |
| 143 | switch (msg.what) { |
| 144 | case BROADCAST_INTENT_MSG: { |
| 145 | if (DEBUG_BROADCAST) Slog.v( |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame^] | 146 | TAG_BROADCAST, "Received BROADCAST_INTENT_MSG"); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 147 | processNextBroadcast(true); |
| 148 | } break; |
| 149 | case BROADCAST_TIMEOUT_MSG: { |
| 150 | synchronized (mService) { |
| 151 | broadcastTimeoutLocked(true); |
| 152 | } |
| 153 | } break; |
| 154 | } |
| 155 | } |
| 156 | }; |
| 157 | |
| 158 | private final class AppNotResponding implements Runnable { |
| 159 | private final ProcessRecord mApp; |
| 160 | private final String mAnnotation; |
| 161 | |
| 162 | public AppNotResponding(ProcessRecord app, String annotation) { |
| 163 | mApp = app; |
| 164 | mAnnotation = annotation; |
| 165 | } |
| 166 | |
| 167 | @Override |
| 168 | public void run() { |
Dianne Hackborn | 5fe7e2a | 2012-10-04 11:58:16 -0700 | [diff] [blame] | 169 | mService.appNotResponding(mApp, null, null, false, mAnnotation); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 173 | BroadcastQueue(ActivityManagerService service, Handler handler, |
| 174 | String name, long timeoutPeriod, boolean allowDelayBehindServices) { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 175 | mService = service; |
Jeff Brown | 6f357d3 | 2014-01-15 20:40:55 -0800 | [diff] [blame] | 176 | mHandler = new BroadcastHandler(handler.getLooper()); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 177 | mQueueName = name; |
| 178 | mTimeoutPeriod = timeoutPeriod; |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 179 | mDelayBehindServices = allowDelayBehindServices; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | public boolean isPendingBroadcastProcessLocked(int pid) { |
| 183 | return mPendingBroadcast != null && mPendingBroadcast.curApp.pid == pid; |
| 184 | } |
| 185 | |
| 186 | public void enqueueParallelBroadcastLocked(BroadcastRecord r) { |
| 187 | mParallelBroadcasts.add(r); |
Jeff Brown | 9fb3fd1 | 2014-09-29 15:32:12 -0700 | [diff] [blame] | 188 | r.enqueueClockTime = System.currentTimeMillis(); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | public void enqueueOrderedBroadcastLocked(BroadcastRecord r) { |
| 192 | mOrderedBroadcasts.add(r); |
Jeff Brown | 9fb3fd1 | 2014-09-29 15:32:12 -0700 | [diff] [blame] | 193 | r.enqueueClockTime = System.currentTimeMillis(); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | public final boolean replaceParallelBroadcastLocked(BroadcastRecord r) { |
| 197 | for (int i=mParallelBroadcasts.size()-1; i>=0; i--) { |
| 198 | if (r.intent.filterEquals(mParallelBroadcasts.get(i).intent)) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame^] | 199 | if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 200 | "***** DROPPING PARALLEL [" |
| 201 | + mQueueName + "]: " + r.intent); |
| 202 | mParallelBroadcasts.set(i, r); |
| 203 | return true; |
| 204 | } |
| 205 | } |
| 206 | return false; |
| 207 | } |
| 208 | |
| 209 | public final boolean replaceOrderedBroadcastLocked(BroadcastRecord r) { |
| 210 | for (int i=mOrderedBroadcasts.size()-1; i>0; i--) { |
| 211 | if (r.intent.filterEquals(mOrderedBroadcasts.get(i).intent)) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame^] | 212 | if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 213 | "***** DROPPING ORDERED [" |
| 214 | + mQueueName + "]: " + r.intent); |
| 215 | mOrderedBroadcasts.set(i, r); |
| 216 | return true; |
| 217 | } |
| 218 | } |
| 219 | return false; |
| 220 | } |
| 221 | |
| 222 | private final void processCurBroadcastLocked(BroadcastRecord r, |
| 223 | ProcessRecord app) throws RemoteException { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame^] | 224 | if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 225 | "Process cur broadcast " + r + " for app " + app); |
| 226 | if (app.thread == null) { |
| 227 | throw new RemoteException(); |
| 228 | } |
| 229 | r.receiver = app.thread.asBinder(); |
| 230 | r.curApp = app; |
| 231 | app.curReceiver = r; |
Dianne Hackborn | a413dc0 | 2013-07-12 12:02:55 -0700 | [diff] [blame] | 232 | app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_RECEIVER); |
Dianne Hackborn | db92608 | 2013-10-31 16:32:44 -0700 | [diff] [blame] | 233 | mService.updateLruProcessLocked(app, false, null); |
| 234 | mService.updateOomAdjLocked(); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 235 | |
| 236 | // Tell the application to launch this receiver. |
| 237 | r.intent.setComponent(r.curComponent); |
| 238 | |
| 239 | boolean started = false; |
| 240 | try { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame^] | 241 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 242 | "Delivering to component " + r.curComponent |
| 243 | + ": " + r); |
| 244 | mService.ensurePackageDexOpt(r.intent.getComponent().getPackageName()); |
| 245 | app.thread.scheduleReceiver(new Intent(r.intent), r.curReceiver, |
| 246 | mService.compatibilityInfoForPackageLocked(r.curReceiver.applicationInfo), |
Dianne Hackborn | a413dc0 | 2013-07-12 12:02:55 -0700 | [diff] [blame] | 247 | r.resultCode, r.resultData, r.resultExtras, r.ordered, r.userId, |
| 248 | app.repProcState); |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame^] | 249 | if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 250 | "Process cur broadcast " + r + " DELIVERED for app " + app); |
| 251 | started = true; |
| 252 | } finally { |
| 253 | if (!started) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame^] | 254 | if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 255 | "Process cur broadcast " + r + ": NOT STARTED!"); |
| 256 | r.receiver = null; |
| 257 | r.curApp = null; |
| 258 | app.curReceiver = null; |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | public boolean sendPendingBroadcastsLocked(ProcessRecord app) { |
| 264 | boolean didSomething = false; |
| 265 | final BroadcastRecord br = mPendingBroadcast; |
| 266 | if (br != null && br.curApp.pid == app.pid) { |
| 267 | try { |
| 268 | mPendingBroadcast = null; |
| 269 | processCurBroadcastLocked(br, app); |
| 270 | didSomething = true; |
| 271 | } catch (Exception e) { |
| 272 | Slog.w(TAG, "Exception in new application when starting receiver " |
| 273 | + br.curComponent.flattenToShortString(), e); |
| 274 | logBroadcastReceiverDiscardLocked(br); |
| 275 | finishReceiverLocked(br, br.resultCode, br.resultData, |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 276 | br.resultExtras, br.resultAbort, false); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 277 | scheduleBroadcastsLocked(); |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 278 | // 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] | 279 | br.state = BroadcastRecord.IDLE; |
| 280 | throw new RuntimeException(e.getMessage()); |
| 281 | } |
| 282 | } |
| 283 | return didSomething; |
| 284 | } |
| 285 | |
| 286 | public void skipPendingBroadcastLocked(int pid) { |
| 287 | final BroadcastRecord br = mPendingBroadcast; |
| 288 | if (br != null && br.curApp.pid == pid) { |
| 289 | br.state = BroadcastRecord.IDLE; |
| 290 | br.nextReceiver = mPendingBroadcastRecvIndex; |
| 291 | mPendingBroadcast = null; |
| 292 | scheduleBroadcastsLocked(); |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | public void skipCurrentReceiverLocked(ProcessRecord app) { |
| 297 | boolean reschedule = false; |
| 298 | BroadcastRecord r = app.curReceiver; |
Kenji Sugimoto | 4472fa97 | 2014-07-17 14:50:41 +0900 | [diff] [blame] | 299 | if (r != null && r.queue == this) { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 300 | // The current broadcast is waiting for this app's receiver |
| 301 | // to be finished. Looks like that's not going to happen, so |
| 302 | // let the broadcast continue. |
| 303 | logBroadcastReceiverDiscardLocked(r); |
| 304 | finishReceiverLocked(r, r.resultCode, r.resultData, |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 305 | r.resultExtras, r.resultAbort, false); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 306 | reschedule = true; |
| 307 | } |
| 308 | |
| 309 | r = mPendingBroadcast; |
| 310 | if (r != null && r.curApp == app) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame^] | 311 | if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 312 | "[" + mQueueName + "] skip & discard pending app " + r); |
| 313 | logBroadcastReceiverDiscardLocked(r); |
| 314 | finishReceiverLocked(r, r.resultCode, r.resultData, |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 315 | r.resultExtras, r.resultAbort, false); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 316 | reschedule = true; |
| 317 | } |
| 318 | if (reschedule) { |
| 319 | scheduleBroadcastsLocked(); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | public void scheduleBroadcastsLocked() { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame^] | 324 | if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Schedule broadcasts [" |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 325 | + mQueueName + "]: current=" |
| 326 | + mBroadcastsScheduled); |
| 327 | |
| 328 | if (mBroadcastsScheduled) { |
| 329 | return; |
| 330 | } |
| 331 | mHandler.sendMessage(mHandler.obtainMessage(BROADCAST_INTENT_MSG, this)); |
| 332 | mBroadcastsScheduled = true; |
| 333 | } |
| 334 | |
| 335 | public BroadcastRecord getMatchingOrderedReceiver(IBinder receiver) { |
| 336 | if (mOrderedBroadcasts.size() > 0) { |
| 337 | final BroadcastRecord r = mOrderedBroadcasts.get(0); |
| 338 | if (r != null && r.receiver == receiver) { |
| 339 | return r; |
| 340 | } |
| 341 | } |
| 342 | return null; |
| 343 | } |
| 344 | |
| 345 | public boolean finishReceiverLocked(BroadcastRecord r, int resultCode, |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 346 | String resultData, Bundle resultExtras, boolean resultAbort, boolean waitForServices) { |
| 347 | final int state = r.state; |
Dianne Hackborn | 3bc8f78d | 2013-09-19 13:34:35 -0700 | [diff] [blame] | 348 | final ActivityInfo receiver = r.curReceiver; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 349 | r.state = BroadcastRecord.IDLE; |
| 350 | if (state == BroadcastRecord.IDLE) { |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 351 | Slog.w(TAG, "finishReceiver [" + mQueueName + "] called but state is IDLE"); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 352 | } |
| 353 | r.receiver = null; |
| 354 | r.intent.setComponent(null); |
Guobin Zhang | 04d0bb6 | 2014-03-07 17:47:10 +0800 | [diff] [blame] | 355 | if (r.curApp != null && r.curApp.curReceiver == r) { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 356 | r.curApp.curReceiver = null; |
| 357 | } |
| 358 | if (r.curFilter != null) { |
| 359 | r.curFilter.receiverList.curBroadcast = null; |
| 360 | } |
| 361 | r.curFilter = null; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 362 | r.curReceiver = null; |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 363 | r.curApp = null; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 364 | mPendingBroadcast = null; |
| 365 | |
| 366 | r.resultCode = resultCode; |
| 367 | r.resultData = resultData; |
| 368 | r.resultExtras = resultExtras; |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 369 | if (resultAbort && (r.intent.getFlags()&Intent.FLAG_RECEIVER_NO_ABORT) == 0) { |
| 370 | r.resultAbort = resultAbort; |
| 371 | } else { |
| 372 | r.resultAbort = false; |
| 373 | } |
| 374 | |
| 375 | if (waitForServices && r.curComponent != null && r.queue.mDelayBehindServices |
| 376 | && r.queue.mOrderedBroadcasts.size() > 0 |
| 377 | && r.queue.mOrderedBroadcasts.get(0) == r) { |
Dianne Hackborn | 3bc8f78d | 2013-09-19 13:34:35 -0700 | [diff] [blame] | 378 | ActivityInfo nextReceiver; |
| 379 | if (r.nextReceiver < r.receivers.size()) { |
| 380 | Object obj = r.receivers.get(r.nextReceiver); |
| 381 | nextReceiver = (obj instanceof ActivityInfo) ? (ActivityInfo)obj : null; |
| 382 | } else { |
| 383 | nextReceiver = null; |
| 384 | } |
| 385 | // Don't do this if the next receive is in the same process as the current one. |
| 386 | if (receiver == null || nextReceiver == null |
| 387 | || receiver.applicationInfo.uid != nextReceiver.applicationInfo.uid |
| 388 | || !receiver.processName.equals(nextReceiver.processName)) { |
| 389 | // In this case, we are ready to process the next receiver for the current broadcast, |
| 390 | //Â but are on a queue that would like to wait for services to finish before moving |
| 391 | // on. If there are background services currently starting, then we will go into a |
| 392 | // special state where we hold off on continuing this broadcast until they are done. |
| 393 | if (mService.mServices.hasBackgroundServices(r.userId)) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame^] | 394 | Slog.i(TAG, "Delay finish: " + r.curComponent.flattenToShortString()); |
Dianne Hackborn | 3bc8f78d | 2013-09-19 13:34:35 -0700 | [diff] [blame] | 395 | r.state = BroadcastRecord.WAITING_SERVICES; |
| 396 | return false; |
| 397 | } |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 398 | } |
| 399 | } |
| 400 | |
| 401 | r.curComponent = null; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 402 | |
| 403 | // We will process the next receiver right now if this is finishing |
| 404 | // an app receiver (which is always asynchronous) or after we have |
| 405 | // come back from calling a receiver. |
| 406 | return state == BroadcastRecord.APP_RECEIVE |
| 407 | || state == BroadcastRecord.CALL_DONE_RECEIVE; |
| 408 | } |
| 409 | |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 410 | public void backgroundServicesFinishedLocked(int userId) { |
| 411 | if (mOrderedBroadcasts.size() > 0) { |
| 412 | BroadcastRecord br = mOrderedBroadcasts.get(0); |
| 413 | if (br.userId == userId && br.state == BroadcastRecord.WAITING_SERVICES) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame^] | 414 | Slog.i(TAG, "Resuming delayed broadcast"); |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 415 | br.curComponent = null; |
| 416 | br.state = BroadcastRecord.IDLE; |
| 417 | processNextBroadcast(false); |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 422 | private static void performReceiveLocked(ProcessRecord app, IIntentReceiver receiver, |
| 423 | Intent intent, int resultCode, String data, Bundle extras, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 424 | boolean ordered, boolean sticky, int sendingUser) throws RemoteException { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 425 | // Send the intent to the receiver asynchronously using one-way binder calls. |
Craig Mautner | 38c1a5f | 2014-07-21 15:36:37 +0000 | [diff] [blame] | 426 | if (app != null) { |
| 427 | if (app.thread != null) { |
| 428 | // If we have an app thread, do the call through that so it is |
| 429 | // correctly ordered with other one-way calls. |
| 430 | app.thread.scheduleRegisteredReceiver(receiver, intent, resultCode, |
| 431 | data, extras, ordered, sticky, sendingUser, app.repProcState); |
| 432 | } else { |
| 433 | // Application has died. Receiver doesn't exist. |
| 434 | throw new RemoteException("app.thread must not be null"); |
| 435 | } |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 436 | } else { |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 437 | receiver.performReceive(intent, resultCode, data, extras, ordered, |
| 438 | sticky, sendingUser); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 439 | } |
| 440 | } |
| 441 | |
| 442 | private final void deliverToRegisteredReceiverLocked(BroadcastRecord r, |
| 443 | BroadcastFilter filter, boolean ordered) { |
| 444 | boolean skip = false; |
Amith Yamasani | 8bf06ed | 2012-08-27 19:30:30 -0700 | [diff] [blame] | 445 | if (filter.requiredPermission != null) { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 446 | int perm = mService.checkComponentPermission(filter.requiredPermission, |
| 447 | r.callingPid, r.callingUid, -1, true); |
| 448 | if (perm != PackageManager.PERMISSION_GRANTED) { |
| 449 | Slog.w(TAG, "Permission Denial: broadcasting " |
| 450 | + r.intent.toString() |
| 451 | + " from " + r.callerPackage + " (pid=" |
| 452 | + r.callingPid + ", uid=" + r.callingUid + ")" |
| 453 | + " requires " + filter.requiredPermission |
| 454 | + " due to registered receiver " + filter); |
| 455 | skip = true; |
| 456 | } |
| 457 | } |
Dianne Hackborn | b4163a6 | 2012-08-02 18:31:26 -0700 | [diff] [blame] | 458 | if (!skip && r.requiredPermission != null) { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 459 | int perm = mService.checkComponentPermission(r.requiredPermission, |
| 460 | filter.receiverList.pid, filter.receiverList.uid, -1, true); |
| 461 | if (perm != PackageManager.PERMISSION_GRANTED) { |
| 462 | Slog.w(TAG, "Permission Denial: receiving " |
| 463 | + r.intent.toString() |
| 464 | + " to " + filter.receiverList.app |
| 465 | + " (pid=" + filter.receiverList.pid |
| 466 | + ", uid=" + filter.receiverList.uid + ")" |
| 467 | + " requires " + r.requiredPermission |
| 468 | + " due to sender " + r.callerPackage |
| 469 | + " (uid " + r.callingUid + ")"); |
| 470 | skip = true; |
| 471 | } |
| 472 | } |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 473 | if (r.appOp != AppOpsManager.OP_NONE) { |
Dianne Hackborn | 1304f4a | 2013-07-09 18:17:27 -0700 | [diff] [blame] | 474 | int mode = mService.mAppOpsService.noteOperation(r.appOp, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 475 | filter.receiverList.uid, filter.packageName); |
| 476 | if (mode != AppOpsManager.MODE_ALLOWED) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame^] | 477 | if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 478 | "App op " + r.appOp + " not allowed for broadcast to uid " |
| 479 | + filter.receiverList.uid + " pkg " + filter.packageName); |
| 480 | skip = true; |
| 481 | } |
| 482 | } |
Ben Gruver | 49660c7 | 2013-08-06 19:54:08 -0700 | [diff] [blame] | 483 | if (!skip) { |
| 484 | skip = !mService.mIntentFirewall.checkBroadcast(r.intent, r.callingUid, |
| 485 | r.callingPid, r.resolvedType, filter.receiverList.uid); |
| 486 | } |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 487 | |
Dianne Hackborn | 9357b11 | 2013-10-03 18:27:48 -0700 | [diff] [blame] | 488 | if (filter.receiverList.app == null || filter.receiverList.app.crashing) { |
| 489 | Slog.w(TAG, "Skipping deliver [" + mQueueName + "] " + r |
| 490 | + " to " + filter.receiverList + ": process crashing"); |
| 491 | skip = true; |
| 492 | } |
| 493 | |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 494 | if (!skip) { |
| 495 | // If this is not being sent as an ordered broadcast, then we |
| 496 | // don't want to touch the fields that keep track of the current |
| 497 | // state of ordered broadcasts. |
| 498 | if (ordered) { |
| 499 | r.receiver = filter.receiverList.receiver.asBinder(); |
| 500 | r.curFilter = filter; |
| 501 | filter.receiverList.curBroadcast = r; |
| 502 | r.state = BroadcastRecord.CALL_IN_RECEIVE; |
| 503 | if (filter.receiverList.app != null) { |
| 504 | // Bump hosting application to no longer be in background |
| 505 | // scheduling class. Note that we can't do that if there |
| 506 | // isn't an app... but we can only be in that case for |
| 507 | // things that directly call the IActivityManager API, which |
| 508 | // are already core system stuff so don't matter for this. |
| 509 | r.curApp = filter.receiverList.app; |
| 510 | filter.receiverList.app.curReceiver = r; |
Dianne Hackborn | 684bf34 | 2014-04-29 17:56:57 -0700 | [diff] [blame] | 511 | mService.updateOomAdjLocked(r.curApp); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 512 | } |
| 513 | } |
| 514 | try { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame^] | 515 | if (DEBUG_BROADCAST_LIGHT) Slog.i(TAG_BROADCAST, "Delivering to " + filter + " : " + r); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 516 | performReceiveLocked(filter.receiverList.app, filter.receiverList.receiver, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 517 | new Intent(r.intent), r.resultCode, r.resultData, |
| 518 | r.resultExtras, r.ordered, r.initialSticky, r.userId); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 519 | if (ordered) { |
| 520 | r.state = BroadcastRecord.CALL_DONE_RECEIVE; |
| 521 | } |
| 522 | } catch (RemoteException e) { |
| 523 | Slog.w(TAG, "Failure sending broadcast " + r.intent, e); |
| 524 | if (ordered) { |
| 525 | r.receiver = null; |
| 526 | r.curFilter = null; |
| 527 | filter.receiverList.curBroadcast = null; |
| 528 | if (filter.receiverList.app != null) { |
| 529 | filter.receiverList.app.curReceiver = null; |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | final void processNextBroadcast(boolean fromMsg) { |
| 537 | synchronized(mService) { |
| 538 | BroadcastRecord r; |
| 539 | |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame^] | 540 | if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "processNextBroadcast [" |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 541 | + mQueueName + "]: " |
| 542 | + mParallelBroadcasts.size() + " broadcasts, " |
| 543 | + mOrderedBroadcasts.size() + " ordered broadcasts"); |
| 544 | |
| 545 | mService.updateCpuStats(); |
| 546 | |
| 547 | if (fromMsg) { |
| 548 | mBroadcastsScheduled = false; |
| 549 | } |
| 550 | |
| 551 | // First, deliver any non-serialized broadcasts right away. |
| 552 | while (mParallelBroadcasts.size() > 0) { |
| 553 | r = mParallelBroadcasts.remove(0); |
| 554 | r.dispatchTime = SystemClock.uptimeMillis(); |
| 555 | r.dispatchClockTime = System.currentTimeMillis(); |
| 556 | final int N = r.receivers.size(); |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame^] | 557 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, "Processing parallel broadcast [" |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 558 | + mQueueName + "] " + r); |
| 559 | for (int i=0; i<N; i++) { |
| 560 | Object target = r.receivers.get(i); |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame^] | 561 | if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 562 | "Delivering non-ordered on [" + mQueueName + "] to registered " |
| 563 | + target + ": " + r); |
| 564 | deliverToRegisteredReceiverLocked(r, (BroadcastFilter)target, false); |
| 565 | } |
| 566 | addBroadcastToHistoryLocked(r); |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame^] | 567 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, "Done with parallel broadcast [" |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 568 | + mQueueName + "] " + r); |
| 569 | } |
| 570 | |
| 571 | // Now take care of the next serialized one... |
| 572 | |
| 573 | // If we are waiting for a process to come up to handle the next |
| 574 | // broadcast, then do nothing at this point. Just in case, we |
| 575 | // check that the process we're waiting for still exists. |
| 576 | if (mPendingBroadcast != null) { |
| 577 | if (DEBUG_BROADCAST_LIGHT) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame^] | 578 | Slog.v(TAG_BROADCAST, "processNextBroadcast [" |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 579 | + mQueueName + "]: waiting for " |
| 580 | + mPendingBroadcast.curApp); |
| 581 | } |
| 582 | |
| 583 | boolean isDead; |
| 584 | synchronized (mService.mPidsSelfLocked) { |
Dianne Hackborn | 9357b11 | 2013-10-03 18:27:48 -0700 | [diff] [blame] | 585 | ProcessRecord proc = mService.mPidsSelfLocked.get(mPendingBroadcast.curApp.pid); |
| 586 | isDead = proc == null || proc.crashing; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 587 | } |
| 588 | if (!isDead) { |
| 589 | // It's still alive, so keep waiting |
| 590 | return; |
| 591 | } else { |
| 592 | Slog.w(TAG, "pending app [" |
| 593 | + mQueueName + "]" + mPendingBroadcast.curApp |
| 594 | + " died before responding to broadcast"); |
| 595 | mPendingBroadcast.state = BroadcastRecord.IDLE; |
| 596 | mPendingBroadcast.nextReceiver = mPendingBroadcastRecvIndex; |
| 597 | mPendingBroadcast = null; |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | boolean looped = false; |
| 602 | |
| 603 | do { |
| 604 | if (mOrderedBroadcasts.size() == 0) { |
| 605 | // No more broadcasts pending, so all done! |
| 606 | mService.scheduleAppGcsLocked(); |
| 607 | if (looped) { |
| 608 | // If we had finished the last ordered broadcast, then |
| 609 | // make sure all processes have correct oom and sched |
| 610 | // adjustments. |
| 611 | mService.updateOomAdjLocked(); |
| 612 | } |
| 613 | return; |
| 614 | } |
| 615 | r = mOrderedBroadcasts.get(0); |
| 616 | boolean forceReceive = false; |
| 617 | |
| 618 | // Ensure that even if something goes awry with the timeout |
| 619 | // detection, we catch "hung" broadcasts here, discard them, |
| 620 | // and continue to make progress. |
| 621 | // |
| 622 | // This is only done if the system is ready so that PRE_BOOT_COMPLETED |
| 623 | // receivers don't get executed with timeouts. They're intended for |
| 624 | // one time heavy lifting after system upgrades and can take |
| 625 | // significant amounts of time. |
| 626 | int numReceivers = (r.receivers != null) ? r.receivers.size() : 0; |
| 627 | if (mService.mProcessesReady && r.dispatchTime > 0) { |
| 628 | long now = SystemClock.uptimeMillis(); |
| 629 | if ((numReceivers > 0) && |
| 630 | (now > r.dispatchTime + (2*mTimeoutPeriod*numReceivers))) { |
| 631 | Slog.w(TAG, "Hung broadcast [" |
| 632 | + mQueueName + "] discarded after timeout failure:" |
| 633 | + " now=" + now |
| 634 | + " dispatchTime=" + r.dispatchTime |
| 635 | + " startTime=" + r.receiverTime |
| 636 | + " intent=" + r.intent |
| 637 | + " numReceivers=" + numReceivers |
| 638 | + " nextReceiver=" + r.nextReceiver |
| 639 | + " state=" + r.state); |
| 640 | broadcastTimeoutLocked(false); // forcibly finish this broadcast |
| 641 | forceReceive = true; |
| 642 | r.state = BroadcastRecord.IDLE; |
| 643 | } |
| 644 | } |
| 645 | |
| 646 | if (r.state != BroadcastRecord.IDLE) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame^] | 647 | if (DEBUG_BROADCAST) Slog.d(TAG_BROADCAST, |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 648 | "processNextBroadcast(" |
| 649 | + mQueueName + ") called when not idle (state=" |
| 650 | + r.state + ")"); |
| 651 | return; |
| 652 | } |
| 653 | |
| 654 | if (r.receivers == null || r.nextReceiver >= numReceivers |
| 655 | || r.resultAbort || forceReceive) { |
| 656 | // No more receivers for this broadcast! Send the final |
| 657 | // result if requested... |
| 658 | if (r.resultTo != null) { |
| 659 | try { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame^] | 660 | if (DEBUG_BROADCAST) Slog.i(TAG_BROADCAST, |
Todd Kennedy | d2f1511 | 2015-01-21 15:25:56 -0800 | [diff] [blame] | 661 | "Finishing broadcast [" + mQueueName + "] " |
| 662 | + r.intent.getAction() + " app=" + r.callerApp); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 663 | performReceiveLocked(r.callerApp, r.resultTo, |
| 664 | new Intent(r.intent), r.resultCode, |
Dianne Hackborn | 20e8098 | 2012-08-31 19:00:44 -0700 | [diff] [blame] | 665 | r.resultData, r.resultExtras, false, false, r.userId); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 666 | // Set this to null so that the reference |
Dianne Hackborn | 9357b11 | 2013-10-03 18:27:48 -0700 | [diff] [blame] | 667 | // (local and remote) isn't kept in the mBroadcastHistory. |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 668 | r.resultTo = null; |
| 669 | } catch (RemoteException e) { |
Craig Mautner | 38c1a5f | 2014-07-21 15:36:37 +0000 | [diff] [blame] | 670 | r.resultTo = null; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 671 | Slog.w(TAG, "Failure [" |
| 672 | + mQueueName + "] sending broadcast result of " |
| 673 | + r.intent, e); |
| 674 | } |
| 675 | } |
| 676 | |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame^] | 677 | if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Cancelling BROADCAST_TIMEOUT_MSG"); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 678 | cancelBroadcastTimeoutLocked(); |
| 679 | |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame^] | 680 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, "Finished with ordered broadcast " |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 681 | + r); |
| 682 | |
| 683 | // ... and on to the next... |
| 684 | addBroadcastToHistoryLocked(r); |
| 685 | mOrderedBroadcasts.remove(0); |
| 686 | r = null; |
| 687 | looped = true; |
| 688 | continue; |
| 689 | } |
| 690 | } while (r == null); |
| 691 | |
| 692 | // Get the next receiver... |
| 693 | int recIdx = r.nextReceiver++; |
| 694 | |
| 695 | // Keep track of when this receiver started, and make sure there |
| 696 | // is a timeout message pending to kill it if need be. |
| 697 | r.receiverTime = SystemClock.uptimeMillis(); |
| 698 | if (recIdx == 0) { |
| 699 | r.dispatchTime = r.receiverTime; |
| 700 | r.dispatchClockTime = System.currentTimeMillis(); |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame^] | 701 | if (DEBUG_BROADCAST_LIGHT) Slog.v(TAG_BROADCAST, "Processing ordered broadcast [" |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 702 | + mQueueName + "] " + r); |
| 703 | } |
| 704 | if (! mPendingBroadcastTimeoutMessage) { |
| 705 | long timeoutTime = r.receiverTime + mTimeoutPeriod; |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame^] | 706 | if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 707 | "Submitting BROADCAST_TIMEOUT_MSG [" |
| 708 | + mQueueName + "] for " + r + " at " + timeoutTime); |
| 709 | setBroadcastTimeoutLocked(timeoutTime); |
| 710 | } |
| 711 | |
| 712 | Object nextReceiver = r.receivers.get(recIdx); |
| 713 | if (nextReceiver instanceof BroadcastFilter) { |
| 714 | // Simple case: this is a registered receiver who gets |
| 715 | // a direct call. |
| 716 | BroadcastFilter filter = (BroadcastFilter)nextReceiver; |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame^] | 717 | if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 718 | "Delivering ordered [" |
| 719 | + mQueueName + "] to registered " |
| 720 | + filter + ": " + r); |
| 721 | deliverToRegisteredReceiverLocked(r, filter, r.ordered); |
| 722 | if (r.receiver == null || !r.ordered) { |
| 723 | // The receiver has already finished, so schedule to |
| 724 | // process the next one. |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame^] | 725 | if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, "Quick finishing [" |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 726 | + mQueueName + "]: ordered=" |
| 727 | + r.ordered + " receiver=" + r.receiver); |
| 728 | r.state = BroadcastRecord.IDLE; |
| 729 | scheduleBroadcastsLocked(); |
| 730 | } |
| 731 | return; |
| 732 | } |
| 733 | |
| 734 | // Hard case: need to instantiate the receiver, possibly |
| 735 | // starting its application process to host it. |
| 736 | |
| 737 | ResolveInfo info = |
| 738 | (ResolveInfo)nextReceiver; |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 739 | ComponentName component = new ComponentName( |
| 740 | info.activityInfo.applicationInfo.packageName, |
| 741 | info.activityInfo.name); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 742 | |
| 743 | boolean skip = false; |
| 744 | int perm = mService.checkComponentPermission(info.activityInfo.permission, |
| 745 | r.callingPid, r.callingUid, info.activityInfo.applicationInfo.uid, |
| 746 | info.activityInfo.exported); |
| 747 | if (perm != PackageManager.PERMISSION_GRANTED) { |
| 748 | if (!info.activityInfo.exported) { |
| 749 | Slog.w(TAG, "Permission Denial: broadcasting " |
| 750 | + r.intent.toString() |
| 751 | + " from " + r.callerPackage + " (pid=" + r.callingPid |
| 752 | + ", uid=" + r.callingUid + ")" |
| 753 | + " is not exported from uid " + info.activityInfo.applicationInfo.uid |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 754 | + " due to receiver " + component.flattenToShortString()); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 755 | } else { |
| 756 | Slog.w(TAG, "Permission Denial: broadcasting " |
| 757 | + r.intent.toString() |
| 758 | + " from " + r.callerPackage + " (pid=" + r.callingPid |
| 759 | + ", uid=" + r.callingUid + ")" |
| 760 | + " requires " + info.activityInfo.permission |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 761 | + " due to receiver " + component.flattenToShortString()); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 762 | } |
| 763 | skip = true; |
| 764 | } |
| 765 | if (info.activityInfo.applicationInfo.uid != Process.SYSTEM_UID && |
| 766 | r.requiredPermission != null) { |
| 767 | try { |
| 768 | perm = AppGlobals.getPackageManager(). |
| 769 | checkPermission(r.requiredPermission, |
| 770 | info.activityInfo.applicationInfo.packageName); |
| 771 | } catch (RemoteException e) { |
| 772 | perm = PackageManager.PERMISSION_DENIED; |
| 773 | } |
| 774 | if (perm != PackageManager.PERMISSION_GRANTED) { |
| 775 | Slog.w(TAG, "Permission Denial: receiving " |
| 776 | + r.intent + " to " |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 777 | + component.flattenToShortString() |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 778 | + " requires " + r.requiredPermission |
| 779 | + " due to sender " + r.callerPackage |
| 780 | + " (uid " + r.callingUid + ")"); |
| 781 | skip = true; |
| 782 | } |
| 783 | } |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 784 | if (r.appOp != AppOpsManager.OP_NONE) { |
Dianne Hackborn | 1304f4a | 2013-07-09 18:17:27 -0700 | [diff] [blame] | 785 | int mode = mService.mAppOpsService.noteOperation(r.appOp, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 786 | info.activityInfo.applicationInfo.uid, info.activityInfo.packageName); |
| 787 | if (mode != AppOpsManager.MODE_ALLOWED) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame^] | 788 | if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, |
Dianne Hackborn | f51f612 | 2013-02-04 18:23:34 -0800 | [diff] [blame] | 789 | "App op " + r.appOp + " not allowed for broadcast to uid " |
| 790 | + info.activityInfo.applicationInfo.uid + " pkg " |
| 791 | + info.activityInfo.packageName); |
| 792 | skip = true; |
| 793 | } |
| 794 | } |
Ben Gruver | 49660c7 | 2013-08-06 19:54:08 -0700 | [diff] [blame] | 795 | if (!skip) { |
| 796 | skip = !mService.mIntentFirewall.checkBroadcast(r.intent, r.callingUid, |
| 797 | r.callingPid, r.resolvedType, info.activityInfo.applicationInfo.uid); |
| 798 | } |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 799 | boolean isSingleton = false; |
| 800 | try { |
| 801 | isSingleton = mService.isSingleton(info.activityInfo.processName, |
| 802 | info.activityInfo.applicationInfo, |
| 803 | info.activityInfo.name, info.activityInfo.flags); |
| 804 | } catch (SecurityException e) { |
| 805 | Slog.w(TAG, e.getMessage()); |
| 806 | skip = true; |
| 807 | } |
| 808 | if ((info.activityInfo.flags&ActivityInfo.FLAG_SINGLE_USER) != 0) { |
| 809 | if (ActivityManager.checkUidPermission( |
| 810 | android.Manifest.permission.INTERACT_ACROSS_USERS, |
| 811 | info.activityInfo.applicationInfo.uid) |
| 812 | != PackageManager.PERMISSION_GRANTED) { |
| 813 | Slog.w(TAG, "Permission Denial: Receiver " + component.flattenToShortString() |
| 814 | + " requests FLAG_SINGLE_USER, but app does not hold " |
| 815 | + android.Manifest.permission.INTERACT_ACROSS_USERS); |
| 816 | skip = true; |
| 817 | } |
| 818 | } |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 819 | if (r.curApp != null && r.curApp.crashing) { |
| 820 | // If the target process is crashing, just skip it. |
Dianne Hackborn | 9357b11 | 2013-10-03 18:27:48 -0700 | [diff] [blame] | 821 | Slog.w(TAG, "Skipping deliver ordered [" + mQueueName + "] " + r |
| 822 | + " to " + r.curApp + ": process crashing"); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 823 | skip = true; |
| 824 | } |
Christopher Tate | ba629da | 2013-11-13 17:42:28 -0800 | [diff] [blame] | 825 | if (!skip) { |
| 826 | boolean isAvailable = false; |
| 827 | try { |
| 828 | isAvailable = AppGlobals.getPackageManager().isPackageAvailable( |
| 829 | info.activityInfo.packageName, |
| 830 | UserHandle.getUserId(info.activityInfo.applicationInfo.uid)); |
| 831 | } catch (Exception e) { |
| 832 | // all such failures mean we skip this receiver |
| 833 | Slog.w(TAG, "Exception getting recipient info for " |
| 834 | + info.activityInfo.packageName, e); |
| 835 | } |
| 836 | if (!isAvailable) { |
| 837 | if (DEBUG_BROADCAST) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame^] | 838 | Slog.v(TAG_BROADCAST, "Skipping delivery to " + info.activityInfo.packageName |
Christopher Tate | ba629da | 2013-11-13 17:42:28 -0800 | [diff] [blame] | 839 | + " / " + info.activityInfo.applicationInfo.uid |
| 840 | + " : package no longer available"); |
| 841 | } |
| 842 | skip = true; |
| 843 | } |
| 844 | } |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 845 | |
| 846 | if (skip) { |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame^] | 847 | if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 848 | "Skipping delivery of ordered [" |
| 849 | + mQueueName + "] " + r + " for whatever reason"); |
| 850 | r.receiver = null; |
| 851 | r.curFilter = null; |
| 852 | r.state = BroadcastRecord.IDLE; |
| 853 | scheduleBroadcastsLocked(); |
| 854 | return; |
| 855 | } |
| 856 | |
| 857 | r.state = BroadcastRecord.APP_RECEIVE; |
| 858 | String targetProcess = info.activityInfo.processName; |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 859 | r.curComponent = component; |
Amith Yamasani | 4b9d79c | 2014-05-21 19:14:21 -0700 | [diff] [blame] | 860 | final int receiverUid = info.activityInfo.applicationInfo.uid; |
| 861 | // If it's a singleton, it needs to be the same app or a special app |
| 862 | if (r.callingUid != Process.SYSTEM_UID && isSingleton |
| 863 | && mService.isValidSingletonCall(r.callingUid, receiverUid)) { |
Dianne Hackborn | 7d19e02 | 2012-08-07 19:12:33 -0700 | [diff] [blame] | 864 | info.activityInfo = mService.getActivityInfoForUser(info.activityInfo, 0); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 865 | } |
| 866 | r.curReceiver = info.activityInfo; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 867 | if (DEBUG_MU && r.callingUid > UserHandle.PER_USER_RANGE) { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 868 | Slog.v(TAG_MU, "Updated broadcast record activity info for secondary user, " |
| 869 | + info.activityInfo + ", callingUid = " + r.callingUid + ", uid = " |
| 870 | + info.activityInfo.applicationInfo.uid); |
| 871 | } |
| 872 | |
| 873 | // Broadcast is being executed, its package can't be stopped. |
| 874 | try { |
| 875 | AppGlobals.getPackageManager().setPackageStoppedState( |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 876 | r.curComponent.getPackageName(), false, UserHandle.getUserId(r.callingUid)); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 877 | } catch (RemoteException e) { |
| 878 | } catch (IllegalArgumentException e) { |
| 879 | Slog.w(TAG, "Failed trying to unstop package " |
| 880 | + r.curComponent.getPackageName() + ": " + e); |
| 881 | } |
| 882 | |
| 883 | // Is this receiver's application already running? |
| 884 | ProcessRecord app = mService.getProcessRecordLocked(targetProcess, |
Dianne Hackborn | 3bc8f78d | 2013-09-19 13:34:35 -0700 | [diff] [blame] | 885 | info.activityInfo.applicationInfo.uid, false); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 886 | if (app != null && app.thread != null) { |
| 887 | try { |
Dianne Hackborn | f7097a5 | 2014-05-13 09:56:14 -0700 | [diff] [blame] | 888 | app.addPackage(info.activityInfo.packageName, |
| 889 | info.activityInfo.applicationInfo.versionCode, mService.mProcessStats); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 890 | processCurBroadcastLocked(r, app); |
| 891 | return; |
| 892 | } catch (RemoteException e) { |
| 893 | Slog.w(TAG, "Exception when sending broadcast to " |
| 894 | + r.curComponent, e); |
Dianne Hackborn | 6c5406a | 2012-11-29 16:18:01 -0800 | [diff] [blame] | 895 | } catch (RuntimeException e) { |
Dianne Hackborn | 8d05172 | 2014-10-01 14:59:58 -0700 | [diff] [blame] | 896 | Slog.wtf(TAG, "Failed sending broadcast to " |
Dianne Hackborn | 6c5406a | 2012-11-29 16:18:01 -0800 | [diff] [blame] | 897 | + r.curComponent + " with " + r.intent, e); |
| 898 | // If some unexpected exception happened, just skip |
| 899 | // this broadcast. At this point we are not in the call |
| 900 | // from a client, so throwing an exception out from here |
| 901 | // will crash the entire system instead of just whoever |
| 902 | // sent the broadcast. |
| 903 | logBroadcastReceiverDiscardLocked(r); |
| 904 | finishReceiverLocked(r, r.resultCode, r.resultData, |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 905 | r.resultExtras, r.resultAbort, false); |
Dianne Hackborn | 6c5406a | 2012-11-29 16:18:01 -0800 | [diff] [blame] | 906 | scheduleBroadcastsLocked(); |
| 907 | // We need to reset the state if we failed to start the receiver. |
| 908 | r.state = BroadcastRecord.IDLE; |
| 909 | return; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | // If a dead object exception was thrown -- fall through to |
| 913 | // restart the application. |
| 914 | } |
| 915 | |
| 916 | // 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^] | 917 | if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 918 | "Need to start app [" |
| 919 | + mQueueName + "] " + targetProcess + " for broadcast " + r); |
| 920 | if ((r.curApp=mService.startProcessLocked(targetProcess, |
| 921 | info.activityInfo.applicationInfo, true, |
| 922 | r.intent.getFlags() | Intent.FLAG_FROM_BACKGROUND, |
| 923 | "broadcast", r.curComponent, |
Dianne Hackborn | 3bc8f78d | 2013-09-19 13:34:35 -0700 | [diff] [blame] | 924 | (r.intent.getFlags()&Intent.FLAG_RECEIVER_BOOT_UPGRADE) != 0, false, false)) |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 925 | == null) { |
| 926 | // Ah, this recipient is unavailable. Finish it if necessary, |
| 927 | // and mark the broadcast record as ready for the next. |
| 928 | Slog.w(TAG, "Unable to launch app " |
| 929 | + info.activityInfo.applicationInfo.packageName + "/" |
| 930 | + info.activityInfo.applicationInfo.uid + " for broadcast " |
| 931 | + r.intent + ": process is bad"); |
| 932 | logBroadcastReceiverDiscardLocked(r); |
| 933 | finishReceiverLocked(r, r.resultCode, r.resultData, |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 934 | r.resultExtras, r.resultAbort, false); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 935 | scheduleBroadcastsLocked(); |
| 936 | r.state = BroadcastRecord.IDLE; |
| 937 | return; |
| 938 | } |
| 939 | |
| 940 | mPendingBroadcast = r; |
| 941 | mPendingBroadcastRecvIndex = recIdx; |
| 942 | } |
| 943 | } |
| 944 | |
| 945 | final void setBroadcastTimeoutLocked(long timeoutTime) { |
| 946 | if (! mPendingBroadcastTimeoutMessage) { |
| 947 | Message msg = mHandler.obtainMessage(BROADCAST_TIMEOUT_MSG, this); |
| 948 | mHandler.sendMessageAtTime(msg, timeoutTime); |
| 949 | mPendingBroadcastTimeoutMessage = true; |
| 950 | } |
| 951 | } |
| 952 | |
| 953 | final void cancelBroadcastTimeoutLocked() { |
| 954 | if (mPendingBroadcastTimeoutMessage) { |
| 955 | mHandler.removeMessages(BROADCAST_TIMEOUT_MSG, this); |
| 956 | mPendingBroadcastTimeoutMessage = false; |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | final void broadcastTimeoutLocked(boolean fromMsg) { |
| 961 | if (fromMsg) { |
| 962 | mPendingBroadcastTimeoutMessage = false; |
| 963 | } |
| 964 | |
| 965 | if (mOrderedBroadcasts.size() == 0) { |
| 966 | return; |
| 967 | } |
| 968 | |
| 969 | long now = SystemClock.uptimeMillis(); |
| 970 | BroadcastRecord r = mOrderedBroadcasts.get(0); |
| 971 | if (fromMsg) { |
| 972 | if (mService.mDidDexOpt) { |
| 973 | // Delay timeouts until dexopt finishes. |
| 974 | mService.mDidDexOpt = false; |
| 975 | long timeoutTime = SystemClock.uptimeMillis() + mTimeoutPeriod; |
| 976 | setBroadcastTimeoutLocked(timeoutTime); |
| 977 | return; |
| 978 | } |
| 979 | if (!mService.mProcessesReady) { |
| 980 | // Only process broadcast timeouts if the system is ready. That way |
| 981 | // PRE_BOOT_COMPLETED broadcasts can't timeout as they are intended |
| 982 | // to do heavy lifting for system up. |
| 983 | return; |
| 984 | } |
| 985 | |
| 986 | long timeoutTime = r.receiverTime + mTimeoutPeriod; |
| 987 | if (timeoutTime > now) { |
| 988 | // We can observe premature timeouts because we do not cancel and reset the |
| 989 | // broadcast timeout message after each receiver finishes. Instead, we set up |
| 990 | // an initial timeout then kick it down the road a little further as needed |
| 991 | // when it expires. |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame^] | 992 | if (DEBUG_BROADCAST) Slog.v(TAG_BROADCAST, |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 993 | "Premature timeout [" |
| 994 | + mQueueName + "] @ " + now + ": resetting BROADCAST_TIMEOUT_MSG for " |
| 995 | + timeoutTime); |
| 996 | setBroadcastTimeoutLocked(timeoutTime); |
| 997 | return; |
| 998 | } |
| 999 | } |
| 1000 | |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 1001 | BroadcastRecord br = mOrderedBroadcasts.get(0); |
| 1002 | if (br.state == BroadcastRecord.WAITING_SERVICES) { |
| 1003 | // In this case the broadcast had already finished, but we had decided to wait |
| 1004 | // for started services to finish as well before going on. So if we have actually |
| 1005 | // waited long enough time timeout the broadcast, let's give up on the whole thing |
| 1006 | // and just move on to the next. |
Wale Ogunwale | d57969f | 2014-11-15 19:37:29 -0800 | [diff] [blame^] | 1007 | Slog.i(TAG, "Waited long enough for: " + (br.curComponent != null |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 1008 | ? br.curComponent.flattenToShortString() : "(null)")); |
| 1009 | br.curComponent = null; |
| 1010 | br.state = BroadcastRecord.IDLE; |
| 1011 | processNextBroadcast(false); |
| 1012 | return; |
| 1013 | } |
| 1014 | |
| 1015 | Slog.w(TAG, "Timeout of broadcast " + r + " - receiver=" + r. receiver |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1016 | + ", started " + (now - r.receiverTime) + "ms ago"); |
| 1017 | r.receiverTime = now; |
| 1018 | r.anrCount++; |
| 1019 | |
| 1020 | // Current receiver has passed its expiration date. |
| 1021 | if (r.nextReceiver <= 0) { |
| 1022 | Slog.w(TAG, "Timeout on receiver with nextReceiver <= 0"); |
| 1023 | return; |
| 1024 | } |
| 1025 | |
| 1026 | ProcessRecord app = null; |
| 1027 | String anrMessage = null; |
| 1028 | |
| 1029 | Object curReceiver = r.receivers.get(r.nextReceiver-1); |
| 1030 | Slog.w(TAG, "Receiver during timeout: " + curReceiver); |
| 1031 | logBroadcastReceiverDiscardLocked(r); |
| 1032 | if (curReceiver instanceof BroadcastFilter) { |
| 1033 | BroadcastFilter bf = (BroadcastFilter)curReceiver; |
| 1034 | if (bf.receiverList.pid != 0 |
| 1035 | && bf.receiverList.pid != ActivityManagerService.MY_PID) { |
| 1036 | synchronized (mService.mPidsSelfLocked) { |
| 1037 | app = mService.mPidsSelfLocked.get( |
| 1038 | bf.receiverList.pid); |
| 1039 | } |
| 1040 | } |
| 1041 | } else { |
| 1042 | app = r.curApp; |
| 1043 | } |
| 1044 | |
| 1045 | if (app != null) { |
| 1046 | anrMessage = "Broadcast of " + r.intent.toString(); |
| 1047 | } |
| 1048 | |
| 1049 | if (mPendingBroadcast == r) { |
| 1050 | mPendingBroadcast = null; |
| 1051 | } |
| 1052 | |
| 1053 | // Move on to the next receiver. |
| 1054 | finishReceiverLocked(r, r.resultCode, r.resultData, |
Dianne Hackborn | 6285a32 | 2013-09-18 12:09:47 -0700 | [diff] [blame] | 1055 | r.resultExtras, r.resultAbort, false); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1056 | scheduleBroadcastsLocked(); |
| 1057 | |
| 1058 | if (anrMessage != null) { |
| 1059 | // Post the ANR to the handler since we do not want to process ANRs while |
| 1060 | // potentially holding our lock. |
| 1061 | mHandler.post(new AppNotResponding(app, anrMessage)); |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | private final void addBroadcastToHistoryLocked(BroadcastRecord r) { |
| 1066 | if (r.callingUid < 0) { |
| 1067 | // This was from a registerReceiver() call; ignore it. |
| 1068 | return; |
| 1069 | } |
| 1070 | System.arraycopy(mBroadcastHistory, 0, mBroadcastHistory, 1, |
| 1071 | MAX_BROADCAST_HISTORY-1); |
| 1072 | r.finishTime = SystemClock.uptimeMillis(); |
| 1073 | mBroadcastHistory[0] = r; |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 1074 | System.arraycopy(mBroadcastSummaryHistory, 0, mBroadcastSummaryHistory, 1, |
| 1075 | MAX_BROADCAST_SUMMARY_HISTORY-1); |
| 1076 | mBroadcastSummaryHistory[0] = r.intent; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1077 | } |
| 1078 | |
| 1079 | final void logBroadcastReceiverDiscardLocked(BroadcastRecord r) { |
| 1080 | if (r.nextReceiver > 0) { |
| 1081 | Object curReceiver = r.receivers.get(r.nextReceiver-1); |
| 1082 | if (curReceiver instanceof BroadcastFilter) { |
| 1083 | BroadcastFilter bf = (BroadcastFilter) curReceiver; |
| 1084 | EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_FILTER, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1085 | bf.owningUserId, System.identityHashCode(r), |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1086 | r.intent.getAction(), |
| 1087 | r.nextReceiver - 1, |
| 1088 | System.identityHashCode(bf)); |
| 1089 | } else { |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1090 | ResolveInfo ri = (ResolveInfo)curReceiver; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1091 | EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1092 | UserHandle.getUserId(ri.activityInfo.applicationInfo.uid), |
| 1093 | System.identityHashCode(r), r.intent.getAction(), |
| 1094 | r.nextReceiver - 1, ri.toString()); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1095 | } |
| 1096 | } else { |
| 1097 | Slog.w(TAG, "Discarding broadcast before first receiver is invoked: " |
| 1098 | + r); |
| 1099 | EventLog.writeEvent(EventLogTags.AM_BROADCAST_DISCARD_APP, |
Dianne Hackborn | b12e135 | 2012-09-26 11:39:20 -0700 | [diff] [blame] | 1100 | -1, System.identityHashCode(r), |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1101 | r.intent.getAction(), |
| 1102 | r.nextReceiver, |
| 1103 | "NONE"); |
| 1104 | } |
| 1105 | } |
| 1106 | |
| 1107 | final boolean dumpLocked(FileDescriptor fd, PrintWriter pw, String[] args, |
| 1108 | int opti, boolean dumpAll, String dumpPackage, boolean needSep) { |
| 1109 | if (mParallelBroadcasts.size() > 0 || mOrderedBroadcasts.size() > 0 |
| 1110 | || mPendingBroadcast != null) { |
| 1111 | boolean printed = false; |
| 1112 | for (int i=mParallelBroadcasts.size()-1; i>=0; i--) { |
| 1113 | BroadcastRecord br = mParallelBroadcasts.get(i); |
| 1114 | if (dumpPackage != null && !dumpPackage.equals(br.callerPackage)) { |
| 1115 | continue; |
| 1116 | } |
| 1117 | if (!printed) { |
| 1118 | if (needSep) { |
| 1119 | pw.println(); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1120 | } |
Dianne Hackborn | 6cbd33f | 2012-09-17 18:28:24 -0700 | [diff] [blame] | 1121 | needSep = true; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1122 | printed = true; |
| 1123 | pw.println(" Active broadcasts [" + mQueueName + "]:"); |
| 1124 | } |
Dianne Hackborn | 6cbd33f | 2012-09-17 18:28:24 -0700 | [diff] [blame] | 1125 | pw.println(" Active Broadcast " + mQueueName + " #" + i + ":"); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1126 | br.dump(pw, " "); |
| 1127 | } |
| 1128 | printed = false; |
| 1129 | needSep = true; |
| 1130 | for (int i=mOrderedBroadcasts.size()-1; i>=0; i--) { |
| 1131 | BroadcastRecord br = mOrderedBroadcasts.get(i); |
| 1132 | if (dumpPackage != null && !dumpPackage.equals(br.callerPackage)) { |
| 1133 | continue; |
| 1134 | } |
| 1135 | if (!printed) { |
| 1136 | if (needSep) { |
| 1137 | pw.println(); |
| 1138 | } |
| 1139 | needSep = true; |
Dianne Hackborn | 6cbd33f | 2012-09-17 18:28:24 -0700 | [diff] [blame] | 1140 | printed = true; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1141 | pw.println(" Active ordered broadcasts [" + mQueueName + "]:"); |
| 1142 | } |
Dianne Hackborn | 6cbd33f | 2012-09-17 18:28:24 -0700 | [diff] [blame] | 1143 | pw.println(" Active Ordered Broadcast " + mQueueName + " #" + i + ":"); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1144 | mOrderedBroadcasts.get(i).dump(pw, " "); |
| 1145 | } |
| 1146 | if (dumpPackage == null || (mPendingBroadcast != null |
| 1147 | && dumpPackage.equals(mPendingBroadcast.callerPackage))) { |
| 1148 | if (needSep) { |
| 1149 | pw.println(); |
| 1150 | } |
| 1151 | pw.println(" Pending broadcast [" + mQueueName + "]:"); |
| 1152 | if (mPendingBroadcast != null) { |
| 1153 | mPendingBroadcast.dump(pw, " "); |
| 1154 | } else { |
| 1155 | pw.println(" (null)"); |
| 1156 | } |
| 1157 | needSep = true; |
| 1158 | } |
| 1159 | } |
| 1160 | |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 1161 | int i; |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1162 | boolean printed = false; |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 1163 | for (i=0; i<MAX_BROADCAST_HISTORY; i++) { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1164 | BroadcastRecord r = mBroadcastHistory[i]; |
| 1165 | if (r == null) { |
| 1166 | break; |
| 1167 | } |
| 1168 | if (dumpPackage != null && !dumpPackage.equals(r.callerPackage)) { |
| 1169 | continue; |
| 1170 | } |
| 1171 | if (!printed) { |
| 1172 | if (needSep) { |
| 1173 | pw.println(); |
| 1174 | } |
| 1175 | needSep = true; |
| 1176 | pw.println(" Historical broadcasts [" + mQueueName + "]:"); |
| 1177 | printed = true; |
| 1178 | } |
| 1179 | if (dumpAll) { |
Dianne Hackborn | 6cbd33f | 2012-09-17 18:28:24 -0700 | [diff] [blame] | 1180 | pw.print(" Historical Broadcast " + mQueueName + " #"); |
| 1181 | pw.print(i); pw.println(":"); |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1182 | r.dump(pw, " "); |
| 1183 | } else { |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 1184 | pw.print(" #"); pw.print(i); pw.print(": "); pw.println(r); |
| 1185 | pw.print(" "); |
| 1186 | pw.println(r.intent.toShortString(false, true, true, false)); |
Dianne Hackborn | a40cfeb | 2013-03-25 17:49:36 -0700 | [diff] [blame] | 1187 | if (r.targetComp != null && r.targetComp != r.intent.getComponent()) { |
| 1188 | pw.print(" targetComp: "); pw.println(r.targetComp.toShortString()); |
| 1189 | } |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 1190 | Bundle bundle = r.intent.getExtras(); |
| 1191 | if (bundle != null) { |
| 1192 | pw.print(" extras: "); pw.println(bundle.toString()); |
| 1193 | } |
| 1194 | } |
| 1195 | } |
| 1196 | |
| 1197 | if (dumpPackage == null) { |
| 1198 | if (dumpAll) { |
| 1199 | i = 0; |
| 1200 | printed = false; |
| 1201 | } |
| 1202 | for (; i<MAX_BROADCAST_SUMMARY_HISTORY; i++) { |
| 1203 | Intent intent = mBroadcastSummaryHistory[i]; |
| 1204 | if (intent == null) { |
| 1205 | break; |
| 1206 | } |
| 1207 | if (!printed) { |
| 1208 | if (needSep) { |
| 1209 | pw.println(); |
| 1210 | } |
| 1211 | needSep = true; |
| 1212 | pw.println(" Historical broadcasts summary [" + mQueueName + "]:"); |
| 1213 | printed = true; |
| 1214 | } |
| 1215 | if (!dumpAll && i >= 50) { |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1216 | pw.println(" ..."); |
| 1217 | break; |
| 1218 | } |
Dianne Hackborn | c0bd747 | 2012-10-09 14:00:30 -0700 | [diff] [blame] | 1219 | pw.print(" #"); pw.print(i); pw.print(": "); |
| 1220 | pw.println(intent.toShortString(false, true, true, false)); |
| 1221 | Bundle bundle = intent.getExtras(); |
| 1222 | if (bundle != null) { |
| 1223 | pw.print(" extras: "); pw.println(bundle.toString()); |
| 1224 | } |
Dianne Hackborn | 40c8db5 | 2012-02-10 18:59:48 -0800 | [diff] [blame] | 1225 | } |
| 1226 | } |
| 1227 | |
| 1228 | return needSep; |
| 1229 | } |
| 1230 | } |