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