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