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