blob: d54d2d7d2056a9e6919eedf51aa1ecaebf3f8978 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2006 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
17package com.android.server.am;
18
Jorim Jaggie2ad37f2018-01-22 22:41:22 +010019import static android.app.ActivityManager.START_SUCCESS;
Philip P. Moltmannee295092020-02-10 08:46:26 -080020
Wale Ogunwalee23149f2015-03-06 15:39:44 -080021import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
22import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
23
Philip P. Moltmannee295092020-02-10 08:46:26 -080024import android.annotation.Nullable;
Dianne Hackborna4972e92012-03-14 10:38:05 -070025import android.app.ActivityManager;
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +010026import android.app.ActivityOptions;
Philip P. Moltmanne7421e92020-02-10 16:14:12 +000027import android.app.PendingIntent;
Philip P. Moltmannee295092020-02-10 08:46:26 -080028import android.content.IIntentReceiver;
29import android.content.IIntentSender;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080030import android.content.Intent;
31import android.os.Binder;
Dianne Hackborna4972e92012-03-14 10:38:05 -070032import android.os.Bundle;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070033import android.os.IBinder;
Dianne Hackbornf66adfd2017-04-13 11:01:48 -070034import android.os.RemoteCallbackList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.os.RemoteException;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -070036import android.os.TransactionTooLargeException;
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -070037import android.os.UserHandle;
Dianne Hackborn98305522017-05-05 17:53:53 -070038import android.util.ArrayMap;
Michal Karpinskiac116df2018-12-10 17:51:42 +000039import android.util.ArraySet;
Joe Onorato8a9b2202010-02-26 18:56:32 -080040import android.util.Slog;
Dianne Hackbornbc02a392016-06-02 17:15:08 -070041import android.util.TimeUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
Dianne Hackbornf66adfd2017-04-13 11:01:48 -070043import com.android.internal.os.IResultReceiver;
Wale Ogunwaleee6eca12018-09-19 20:37:53 -070044import com.android.internal.util.function.pooled.PooledLambda;
Wale Ogunwale59507092018-10-29 09:00:30 -070045import com.android.server.wm.SafeActivityOptions;
Craig Mautnerb9168362015-02-26 20:40:19 -080046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import java.io.PrintWriter;
48import java.lang.ref.WeakReference;
Rubin Xuf24d6062016-07-20 17:34:50 +010049import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050
Wale Ogunwaleee6eca12018-09-19 20:37:53 -070051public final class PendingIntentRecord extends IIntentSender.Stub {
Wale Ogunwalee23149f2015-03-06 15:39:44 -080052 private static final String TAG = TAG_WITH_CLASS_NAME ? "PendingIntentRecord" : TAG_AM;
53
Michal Karpinskiac116df2018-12-10 17:51:42 +000054 public static final int FLAG_ACTIVITY_SENDER = 1 << 0;
55 public static final int FLAG_BROADCAST_SENDER = 1 << 1;
Michal Karpinskic8aa91b2019-01-10 16:45:59 +000056 public static final int FLAG_SERVICE_SENDER = 1 << 2;
Michal Karpinskiac116df2018-12-10 17:51:42 +000057
Wale Ogunwaleee6eca12018-09-19 20:37:53 -070058 final PendingIntentController controller;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080059 final Key key;
60 final int uid;
Wale Ogunwale59507092018-10-29 09:00:30 -070061 public final WeakReference<PendingIntentRecord> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080062 boolean sent = false;
63 boolean canceled = false;
Dianne Hackborn98305522017-05-05 17:53:53 -070064 private ArrayMap<IBinder, Long> whitelistDuration;
Dianne Hackbornf66adfd2017-04-13 11:01:48 -070065 private RemoteCallbackList<IResultReceiver> mCancelCallbacks;
Michal Karpinskiac116df2018-12-10 17:51:42 +000066 private ArraySet<IBinder> mAllowBgActivityStartsForActivitySender = new ArraySet<>();
67 private ArraySet<IBinder> mAllowBgActivityStartsForBroadcastSender = new ArraySet<>();
Michal Karpinskic8aa91b2019-01-10 16:45:59 +000068 private ArraySet<IBinder> mAllowBgActivityStartsForServiceSender = new ArraySet<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069
Dianne Hackborn1d442e02009-04-20 18:14:05 -070070 String stringName;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -080071 String lastTagPrefix;
72 String lastTag;
Wale Ogunwalee23149f2015-03-06 15:39:44 -080073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 final static class Key {
75 final int type;
76 final String packageName;
Philip P. Moltmannee295092020-02-10 08:46:26 -080077 final String featureId;
Wale Ogunwaleee6eca12018-09-19 20:37:53 -070078 final IBinder activity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 final String who;
80 final int requestCode;
81 final Intent requestIntent;
82 final String requestResolvedType;
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +010083 final SafeActivityOptions options;
Dianne Hackborn621e17d2010-11-22 15:59:56 -080084 Intent[] allIntents;
85 String[] allResolvedTypes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 final int flags;
87 final int hashCode;
Amith Yamasani4ea60692012-08-28 14:34:53 -070088 final int userId;
Felipe Lemea1b79bf2016-05-24 13:06:54 -070089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 private static final int ODD_PRIME_NUMBER = 37;
Felipe Lemea1b79bf2016-05-24 13:06:54 -070091
Philip P. Moltmannee295092020-02-10 08:46:26 -080092 Key(int _t, String _p, @Nullable String _featureId, IBinder _a, String _w,
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +010093 int _r, Intent[] _i, String[] _it, int _f, SafeActivityOptions _o, int _userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 type = _t;
95 packageName = _p;
Philip P. Moltmannee295092020-02-10 08:46:26 -080096 featureId = _featureId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 activity = _a;
98 who = _w;
99 requestCode = _r;
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800100 requestIntent = _i != null ? _i[_i.length-1] : null;
101 requestResolvedType = _it != null ? _it[_it.length-1] : null;
102 allIntents = _i;
103 allResolvedTypes = _it;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 flags = _f;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -0700105 options = _o;
Amith Yamasani4ea60692012-08-28 14:34:53 -0700106 userId = _userId;
107
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 int hash = 23;
109 hash = (ODD_PRIME_NUMBER*hash) + _f;
110 hash = (ODD_PRIME_NUMBER*hash) + _r;
Amith Yamasani4ea60692012-08-28 14:34:53 -0700111 hash = (ODD_PRIME_NUMBER*hash) + _userId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 if (_w != null) {
113 hash = (ODD_PRIME_NUMBER*hash) + _w.hashCode();
114 }
115 if (_a != null) {
116 hash = (ODD_PRIME_NUMBER*hash) + _a.hashCode();
117 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800118 if (requestIntent != null) {
119 hash = (ODD_PRIME_NUMBER*hash) + requestIntent.filterHashCode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800121 if (requestResolvedType != null) {
122 hash = (ODD_PRIME_NUMBER*hash) + requestResolvedType.hashCode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 }
Rubin Xuf24d6062016-07-20 17:34:50 +0100124 hash = (ODD_PRIME_NUMBER*hash) + (_p != null ? _p.hashCode() : 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 hash = (ODD_PRIME_NUMBER*hash) + _t;
126 hashCode = hash;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800127 //Slog.i(ActivityManagerService.TAG, this + " hashCode=0x"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 // + Integer.toHexString(hashCode));
129 }
Felipe Lemea1b79bf2016-05-24 13:06:54 -0700130
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700131 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 public boolean equals(Object otherObj) {
133 if (otherObj == null) {
134 return false;
135 }
136 try {
137 Key other = (Key)otherObj;
138 if (type != other.type) {
139 return false;
140 }
Amith Yamasani4ea60692012-08-28 14:34:53 -0700141 if (userId != other.userId){
142 return false;
143 }
Rubin Xuf24d6062016-07-20 17:34:50 +0100144 if (!Objects.equals(packageName, other.packageName)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800145 return false;
146 }
Philip P. Moltmannee295092020-02-10 08:46:26 -0800147 if (!Objects.equals(featureId, other.featureId)) {
148 return false;
149 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 if (activity != other.activity) {
151 return false;
152 }
Rubin Xuf24d6062016-07-20 17:34:50 +0100153 if (!Objects.equals(who, other.who)) {
154 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 }
156 if (requestCode != other.requestCode) {
157 return false;
158 }
159 if (requestIntent != other.requestIntent) {
160 if (requestIntent != null) {
161 if (!requestIntent.filterEquals(other.requestIntent)) {
162 return false;
163 }
164 } else if (other.requestIntent != null) {
165 return false;
166 }
167 }
Rubin Xuf24d6062016-07-20 17:34:50 +0100168 if (!Objects.equals(requestResolvedType, other.requestResolvedType)) {
169 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 }
171 if (flags != other.flags) {
172 return false;
173 }
174 return true;
175 } catch (ClassCastException e) {
176 }
177 return false;
178 }
179
180 public int hashCode() {
181 return hashCode;
182 }
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700183
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 public String toString() {
Philip P. Moltmannee295092020-02-10 08:46:26 -0800185 return "Key{" + typeName()
186 + " pkg=" + packageName + (featureId != null ? "/" + featureId : "")
Dianne Hackbornc3b91fd2010-02-23 17:25:30 -0800187 + " intent="
Dianne Hackborn90c52de2011-09-23 12:57:44 -0700188 + (requestIntent != null
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800189 ? requestIntent.toShortString(false, true, false, false) : "<null>")
Amith Yamasani4ea60692012-08-28 14:34:53 -0700190 + " flags=0x" + Integer.toHexString(flags) + " u=" + userId + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800191 }
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700192
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 String typeName() {
194 switch (type) {
Dianne Hackborna4972e92012-03-14 10:38:05 -0700195 case ActivityManager.INTENT_SENDER_ACTIVITY:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 return "startActivity";
Dianne Hackborna4972e92012-03-14 10:38:05 -0700197 case ActivityManager.INTENT_SENDER_BROADCAST:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 return "broadcastIntent";
Dianne Hackborna4972e92012-03-14 10:38:05 -0700199 case ActivityManager.INTENT_SENDER_SERVICE:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 return "startService";
Christopher Tate08992ac2017-03-21 11:37:06 -0700201 case ActivityManager.INTENT_SENDER_FOREGROUND_SERVICE:
202 return "startForegroundService";
Dianne Hackborna4972e92012-03-14 10:38:05 -0700203 case ActivityManager.INTENT_SENDER_ACTIVITY_RESULT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 return "activityResult";
205 }
206 return Integer.toString(type);
207 }
208 }
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700209
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700210 PendingIntentRecord(PendingIntentController _controller, Key _k, int _u) {
211 controller = _controller;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 key = _k;
213 uid = _u;
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700214 ref = new WeakReference<>(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800215 }
216
Dianne Hackborn98305522017-05-05 17:53:53 -0700217 void setWhitelistDurationLocked(IBinder whitelistToken, long duration) {
218 if (duration > 0) {
219 if (whitelistDuration == null) {
220 whitelistDuration = new ArrayMap<>();
221 }
222 whitelistDuration.put(whitelistToken, duration);
223 } else if (whitelistDuration != null) {
224 whitelistDuration.remove(whitelistToken);
225 if (whitelistDuration.size() <= 0) {
226 whitelistDuration = null;
227 }
228
229 }
Felipe Lemea1b79bf2016-05-24 13:06:54 -0700230 this.stringName = null;
231 }
232
Michal Karpinskiac116df2018-12-10 17:51:42 +0000233 void setAllowBgActivityStarts(IBinder token, int flags) {
234 if (token == null) return;
235 if ((flags & FLAG_ACTIVITY_SENDER) != 0) {
236 mAllowBgActivityStartsForActivitySender.add(token);
237 }
238 if ((flags & FLAG_BROADCAST_SENDER) != 0) {
239 mAllowBgActivityStartsForBroadcastSender.add(token);
240 }
Michal Karpinskic8aa91b2019-01-10 16:45:59 +0000241 if ((flags & FLAG_SERVICE_SENDER) != 0) {
242 mAllowBgActivityStartsForServiceSender.add(token);
243 }
Michal Karpinskiac116df2018-12-10 17:51:42 +0000244 }
245
Michal Karpinskid2075892019-04-17 16:01:10 +0100246 void clearAllowBgActivityStarts(IBinder token) {
247 if (token == null) return;
248 mAllowBgActivityStartsForActivitySender.remove(token);
249 mAllowBgActivityStartsForBroadcastSender.remove(token);
250 mAllowBgActivityStartsForServiceSender.remove(token);
251 }
252
Dianne Hackbornf66adfd2017-04-13 11:01:48 -0700253 public void registerCancelListenerLocked(IResultReceiver receiver) {
254 if (mCancelCallbacks == null) {
255 mCancelCallbacks = new RemoteCallbackList<>();
256 }
257 mCancelCallbacks.register(receiver);
258 }
259
260 public void unregisterCancelListenerLocked(IResultReceiver receiver) {
Makoto Onuki8a0319a2018-04-25 16:31:05 -0700261 if (mCancelCallbacks == null) {
262 return; // Already unregistered or detached.
263 }
Dianne Hackbornf66adfd2017-04-13 11:01:48 -0700264 mCancelCallbacks.unregister(receiver);
265 if (mCancelCallbacks.getRegisteredCallbackCount() <= 0) {
266 mCancelCallbacks = null;
267 }
268 }
269
270 public RemoteCallbackList<IResultReceiver> detachCancelListenersLocked() {
271 RemoteCallbackList<IResultReceiver> listeners = mCancelCallbacks;
272 mCancelCallbacks = null;
273 return listeners;
274 }
275
Dianne Hackborn98305522017-05-05 17:53:53 -0700276 public void send(int code, Intent intent, String resolvedType, IBinder whitelistToken,
Dianne Hackborn0c4e6a82016-05-13 17:37:08 -0700277 IIntentReceiver finishedReceiver, String requiredPermission, Bundle options) {
Dianne Hackborn98305522017-05-05 17:53:53 -0700278 sendInner(code, intent, resolvedType, whitelistToken, finishedReceiver,
Andrii Kulianb1cdb102017-07-13 15:33:06 -0700279 requiredPermission, null, null, 0, 0, 0, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -0700280 }
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700281
Dianne Hackborn98305522017-05-05 17:53:53 -0700282 public int sendWithResult(int code, Intent intent, String resolvedType, IBinder whitelistToken,
283 IIntentReceiver finishedReceiver, String requiredPermission, Bundle options) {
284 return sendInner(code, intent, resolvedType, whitelistToken, finishedReceiver,
Andrii Kulianb1cdb102017-07-13 15:33:06 -0700285 requiredPermission, null, null, 0, 0, 0, options);
Dianne Hackborn98305522017-05-05 17:53:53 -0700286 }
287
Wale Ogunwale59507092018-10-29 09:00:30 -0700288 public int sendInner(int code, Intent intent, String resolvedType, IBinder whitelistToken,
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700289 IIntentReceiver finishedReceiver, String requiredPermission, IBinder resultTo,
290 String resultWho, int requestCode, int flagsMask, int flagsValues, Bundle options) {
Jeff Sharkeyf0ec2e02016-03-21 12:37:54 -0600291 if (intent != null) intent.setDefusable(true);
292 if (options != null) options.setDefusable(true);
293
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700294 Long duration = null;
295 Intent finalIntent = null;
296 Intent[] allIntents = null;
297 String[] allResolvedTypes = null;
298 SafeActivityOptions mergedOptions = null;
299 synchronized (controller.mLock) {
300 if (canceled) {
301 return ActivityManager.START_CANCELED;
302 }
Svetoslavb0a78392015-04-10 17:25:35 -0700303
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700304 sent = true;
305 if ((key.flags & PendingIntent.FLAG_ONE_SHOT) != 0) {
306 controller.cancelIntentSender(this, true);
307 }
Svetoslavb0a78392015-04-10 17:25:35 -0700308
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700309 finalIntent = key.requestIntent != null ? new Intent(key.requestIntent) : new Intent();
310
311 final boolean immutable = (key.flags & PendingIntent.FLAG_IMMUTABLE) != 0;
312 if (!immutable) {
313 if (intent != null) {
314 int changes = finalIntent.fillIn(intent, key.flags);
315 if ((changes & Intent.FILL_IN_DATA) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 resolvedType = key.requestResolvedType;
317 }
318 } else {
319 resolvedType = key.requestResolvedType;
320 }
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700321 flagsMask &= ~Intent.IMMUTABLE_FLAGS;
322 flagsValues &= flagsMask;
323 finalIntent.setFlags((finalIntent.getFlags() & ~flagsMask) | flagsValues);
324 } else {
325 resolvedType = key.requestResolvedType;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 }
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700327
Winson Chungd0707752018-09-27 13:55:58 -0700328 // Apply any launch flags from the ActivityOptions. This is to ensure that the caller
329 // can specify a consistent launch mode even if the PendingIntent is immutable
330 final ActivityOptions opts = ActivityOptions.fromBundle(options);
331 if (opts != null) {
332 finalIntent.addFlags(opts.getPendingIntentLaunchFlags());
333 }
334
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700335 // Extract options before clearing calling identity
336 mergedOptions = key.options;
337 if (mergedOptions == null) {
Winson Chungd0707752018-09-27 13:55:58 -0700338 mergedOptions = new SafeActivityOptions(opts);
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700339 } else {
Winson Chungd0707752018-09-27 13:55:58 -0700340 mergedOptions.setCallerOptions(opts);
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700341 }
342
343 if (whitelistDuration != null) {
344 duration = whitelistDuration.get(whitelistToken);
345 }
346
347 if (key.type == ActivityManager.INTENT_SENDER_ACTIVITY
348 && key.allIntents != null && key.allIntents.length > 1) {
349 // Copy all intents and resolved types while we have the controller lock so we can
350 // use it later when the lock isn't held.
351 allIntents = new Intent[key.allIntents.length];
352 allResolvedTypes = new String[key.allIntents.length];
353 System.arraycopy(key.allIntents, 0, allIntents, 0, key.allIntents.length);
354 if (key.allResolvedTypes != null) {
355 System.arraycopy(key.allResolvedTypes, 0, allResolvedTypes, 0,
356 key.allResolvedTypes.length);
357 }
358 allIntents[allIntents.length - 1] = finalIntent;
359 allResolvedTypes[allResolvedTypes.length - 1] = resolvedType;
360 }
361
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 }
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700363 // We don't hold the controller lock beyond this point as we will be calling into AM and WM.
364
365 final int callingUid = Binder.getCallingUid();
366 final int callingPid = Binder.getCallingPid();
367 final long origId = Binder.clearCallingIdentity();
368
369 int res = START_SUCCESS;
370 try {
371 if (duration != null) {
372 int procState = controller.mAmInternal.getUidProcessState(callingUid);
373 if (!ActivityManager.isProcStateBackground(procState)) {
374 StringBuilder tag = new StringBuilder(64);
375 tag.append("pendingintent:");
376 UserHandle.formatUid(tag, callingUid);
377 tag.append(":");
378 if (finalIntent.getAction() != null) {
379 tag.append(finalIntent.getAction());
380 } else if (finalIntent.getComponent() != null) {
381 finalIntent.getComponent().appendShortString(tag);
382 } else if (finalIntent.getData() != null) {
383 tag.append(finalIntent.getData().toSafeString());
384 }
385 controller.mAmInternal.tempWhitelistForPendingIntent(callingPid, callingUid,
386 uid, duration, tag.toString());
387 } else {
388 Slog.w(TAG, "Not doing whitelist " + this + ": caller state=" + procState);
389 }
390 }
391
392 boolean sendFinish = finishedReceiver != null;
393 int userId = key.userId;
394 if (userId == UserHandle.USER_CURRENT) {
395 userId = controller.mUserController.getCurrentOrTargetUserId();
396 }
Michal Karpinskicc88d7e2019-01-24 15:32:12 +0000397 // temporarily allow receivers and services to open activities from background if the
Alan Stokes6ac9efd2019-05-09 12:50:37 +0000398 // PendingIntent.send() caller was foreground at the time of sendInner() call
399 final boolean allowTrampoline = uid != callingUid
Michal Karpinskicc88d7e2019-01-24 15:32:12 +0000400 && controller.mAtmInternal.isUidForeground(callingUid);
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700401
Michal Karpinski9cbb20b2019-02-05 17:31:50 +0000402 // note: we on purpose don't pass in the information about the PendingIntent's creator,
403 // like pid or ProcessRecord, to the ActivityTaskManagerInternal calls below, because
404 // it's not unusual for the creator's process to not be alive at this time
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700405 switch (key.type) {
406 case ActivityManager.INTENT_SENDER_ACTIVITY:
407 try {
408 // Note when someone has a pending intent, even from different
409 // users, then there's no need to ensure the calling user matches
410 // the target user, so validateIncomingUser is always false below.
411
412 if (key.allIntents != null && key.allIntents.length > 1) {
413 res = controller.mAtmInternal.startActivitiesInPackage(
Philip P. Moltmannee295092020-02-10 08:46:26 -0800414 uid, callingPid, callingUid, key.packageName, key.featureId,
415 allIntents, allResolvedTypes, resultTo, mergedOptions, userId,
Michal Karpinski84d9ebd2019-01-17 18:28:59 +0000416 false /* validateIncomingUser */,
Michal Karpinskiac116df2018-12-10 17:51:42 +0000417 this /* originatingPendingIntent */,
418 mAllowBgActivityStartsForActivitySender.contains(whitelistToken));
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700419 } else {
Philip P. Moltmannee295092020-02-10 08:46:26 -0800420 res = controller.mAtmInternal.startActivityInPackage(uid, callingPid,
421 callingUid, key.packageName, key.featureId, finalIntent,
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700422 resolvedType, resultTo, resultWho, requestCode, 0,
423 mergedOptions, userId, null, "PendingIntentRecord",
424 false /* validateIncomingUser */,
Michal Karpinskiac116df2018-12-10 17:51:42 +0000425 this /* originatingPendingIntent */,
Philip P. Moltmannee295092020-02-10 08:46:26 -0800426 mAllowBgActivityStartsForActivitySender.contains(
427 whitelistToken));
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700428 }
429 } catch (RuntimeException e) {
430 Slog.w(TAG, "Unable to send startActivity intent", e);
431 }
432 break;
433 case ActivityManager.INTENT_SENDER_ACTIVITY_RESULT:
434 controller.mAtmInternal.sendActivityResult(-1, key.activity, key.who,
435 key.requestCode, code, finalIntent);
436 break;
437 case ActivityManager.INTENT_SENDER_BROADCAST:
438 try {
439 // If a completion callback has been requested, require
440 // that the broadcast be delivered synchronously
441 int sent = controller.mAmInternal.broadcastIntentInPackage(key.packageName,
Philip P. Moltmannee295092020-02-10 08:46:26 -0800442 key.featureId, uid, callingUid, callingPid, finalIntent,
443 resolvedType, finishedReceiver, code, null, null,
444 requiredPermission, options, (finishedReceiver != null), false,
445 userId,
Michal Karpinskicc88d7e2019-01-24 15:32:12 +0000446 mAllowBgActivityStartsForBroadcastSender.contains(whitelistToken)
Philip P. Moltmannee295092020-02-10 08:46:26 -0800447 || allowTrampoline);
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700448 if (sent == ActivityManager.BROADCAST_SUCCESS) {
449 sendFinish = false;
450 }
451 } catch (RuntimeException e) {
452 Slog.w(TAG, "Unable to send startActivity intent", e);
453 }
454 break;
455 case ActivityManager.INTENT_SENDER_SERVICE:
456 case ActivityManager.INTENT_SENDER_FOREGROUND_SERVICE:
457 try {
458 controller.mAmInternal.startServiceInPackage(uid, finalIntent, resolvedType,
459 key.type == ActivityManager.INTENT_SENDER_FOREGROUND_SERVICE,
Philip P. Moltmannee295092020-02-10 08:46:26 -0800460 key.packageName, key.featureId, userId,
Michal Karpinskicc88d7e2019-01-24 15:32:12 +0000461 mAllowBgActivityStartsForServiceSender.contains(whitelistToken)
462 || allowTrampoline);
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700463 } catch (RuntimeException e) {
464 Slog.w(TAG, "Unable to send startService intent", e);
465 } catch (TransactionTooLargeException e) {
466 res = ActivityManager.START_CANCELED;
467 }
468 break;
469 }
470
471 if (sendFinish && res != ActivityManager.START_CANCELED) {
472 try {
473 finishedReceiver.performReceive(new Intent(finalIntent), 0,
474 null, null, false, false, key.userId);
475 } catch (RemoteException e) {
476 }
477 }
478 } finally {
479 Binder.restoreCallingIdentity(origId);
480 }
481
482 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800483 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800484
485 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486 protected void finalize() throws Throwable {
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800487 try {
488 if (!canceled) {
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700489 controller.mH.sendMessage(PooledLambda.obtainMessage(
490 PendingIntentRecord::completeFinalize, this));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 }
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800492 } finally {
493 super.finalize();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800494 }
495 }
496
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700497 private void completeFinalize() {
498 synchronized(controller.mLock) {
499 WeakReference<PendingIntentRecord> current = controller.mIntentSenderRecords.get(key);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800500 if (current == ref) {
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700501 controller.mIntentSenderRecords.remove(key);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800502 }
503 }
504 }
Felipe Lemea1b79bf2016-05-24 13:06:54 -0700505
Wale Ogunwale59507092018-10-29 09:00:30 -0700506 public void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700507 pw.print(prefix); pw.print("uid="); pw.print(uid);
508 pw.print(" packageName="); pw.print(key.packageName);
Philip P. Moltmannee295092020-02-10 08:46:26 -0800509 pw.print(" featureId="); pw.print(key.featureId);
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700510 pw.print(" type="); pw.print(key.typeName());
511 pw.print(" flags=0x"); pw.println(Integer.toHexString(key.flags));
512 if (key.activity != null || key.who != null) {
513 pw.print(prefix); pw.print("activity="); pw.print(key.activity);
514 pw.print(" who="); pw.println(key.who);
515 }
516 if (key.requestCode != 0 || key.requestResolvedType != null) {
517 pw.print(prefix); pw.print("requestCode="); pw.print(key.requestCode);
518 pw.print(" requestResolvedType="); pw.println(key.requestResolvedType);
519 }
Dianne Hackbornc3b91fd2010-02-23 17:25:30 -0800520 if (key.requestIntent != null) {
521 pw.print(prefix); pw.print("requestIntent=");
Hui Yu6d5c3b92019-10-22 15:35:53 -0700522 pw.println(key.requestIntent.toShortString(false, true, true, false));
Dianne Hackbornc3b91fd2010-02-23 17:25:30 -0800523 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700524 if (sent || canceled) {
525 pw.print(prefix); pw.print("sent="); pw.print(sent);
526 pw.print(" canceled="); pw.println(canceled);
527 }
Dianne Hackborn98305522017-05-05 17:53:53 -0700528 if (whitelistDuration != null) {
Dianne Hackbornbc02a392016-06-02 17:15:08 -0700529 pw.print(prefix);
530 pw.print("whitelistDuration=");
Dianne Hackborn98305522017-05-05 17:53:53 -0700531 for (int i = 0; i < whitelistDuration.size(); i++) {
532 if (i != 0) {
533 pw.print(", ");
534 }
535 pw.print(Integer.toHexString(System.identityHashCode(whitelistDuration.keyAt(i))));
536 pw.print(":");
537 TimeUtils.formatDuration(whitelistDuration.valueAt(i), pw);
538 }
Dianne Hackbornbc02a392016-06-02 17:15:08 -0700539 pw.println();
540 }
Dianne Hackbornf66adfd2017-04-13 11:01:48 -0700541 if (mCancelCallbacks != null) {
542 pw.print(prefix); pw.println("mCancelCallbacks:");
543 for (int i = 0; i < mCancelCallbacks.getRegisteredCallbackCount(); i++) {
544 pw.print(prefix); pw.print(" #"); pw.print(i); pw.print(": ");
545 pw.println(mCancelCallbacks.getRegisteredCallbackItem(i));
546 }
547 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 }
549
550 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700551 if (stringName != null) {
552 return stringName;
553 }
554 StringBuilder sb = new StringBuilder(128);
555 sb.append("PendingIntentRecord{");
556 sb.append(Integer.toHexString(System.identityHashCode(this)));
557 sb.append(' ');
558 sb.append(key.packageName);
Philip P. Moltmannee295092020-02-10 08:46:26 -0800559 if (key.featureId != null) {
560 sb.append('/');
561 sb.append(key.featureId);
562 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700563 sb.append(' ');
564 sb.append(key.typeName());
Dianne Hackborn98305522017-05-05 17:53:53 -0700565 if (whitelistDuration != null) {
Dianne Hackbornbc02a392016-06-02 17:15:08 -0700566 sb.append( " (whitelist: ");
Dianne Hackborn98305522017-05-05 17:53:53 -0700567 for (int i = 0; i < whitelistDuration.size(); i++) {
568 if (i != 0) {
569 sb.append(",");
570 }
571 sb.append(Integer.toHexString(System.identityHashCode(whitelistDuration.keyAt(i))));
572 sb.append(":");
573 TimeUtils.formatDuration(whitelistDuration.valueAt(i), sb);
574 }
Dianne Hackbornbc02a392016-06-02 17:15:08 -0700575 sb.append(")");
Felipe Lemea1b79bf2016-05-24 13:06:54 -0700576 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700577 sb.append('}');
578 return stringName = sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 }
580}