blob: a08c829f05767147d212236358a146f64421b972 [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;
Wale Ogunwalee23149f2015-03-06 15:39:44 -080020import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
21import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
22
Dianne Hackborna4972e92012-03-14 10:38:05 -070023import android.app.ActivityManager;
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +010024import android.app.ActivityOptions;
Suchi Amalapurapu1ccac752009-06-12 10:09:58 -070025import android.content.IIntentSender;
26import android.content.IIntentReceiver;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.app.PendingIntent;
28import android.content.Intent;
29import android.os.Binder;
Dianne Hackborna4972e92012-03-14 10:38:05 -070030import android.os.Bundle;
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -070031import android.os.IBinder;
Dianne Hackbornf66adfd2017-04-13 11:01:48 -070032import android.os.RemoteCallbackList;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.os.RemoteException;
Craig Mautner5f2bb4c2015-03-12 16:10:27 -070034import android.os.TransactionTooLargeException;
Dianne Hackborn50cdf7c32012-09-23 17:08:57 -070035import android.os.UserHandle;
Dianne Hackborn98305522017-05-05 17:53:53 -070036import android.util.ArrayMap;
Michal Karpinskiac116df2018-12-10 17:51:42 +000037import android.util.ArraySet;
Joe Onorato8a9b2202010-02-26 18:56:32 -080038import android.util.Slog;
Dianne Hackbornbc02a392016-06-02 17:15:08 -070039import android.util.TimeUtils;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
Dianne Hackbornf66adfd2017-04-13 11:01:48 -070041import com.android.internal.os.IResultReceiver;
Wale Ogunwaleee6eca12018-09-19 20:37:53 -070042import com.android.internal.util.function.pooled.PooledLambda;
Wale Ogunwale59507092018-10-29 09:00:30 -070043import com.android.server.wm.SafeActivityOptions;
Craig Mautnerb9168362015-02-26 20:40:19 -080044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import java.io.PrintWriter;
46import java.lang.ref.WeakReference;
Rubin Xuf24d6062016-07-20 17:34:50 +010047import java.util.Objects;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080048
Wale Ogunwaleee6eca12018-09-19 20:37:53 -070049public final class PendingIntentRecord extends IIntentSender.Stub {
Wale Ogunwalee23149f2015-03-06 15:39:44 -080050 private static final String TAG = TAG_WITH_CLASS_NAME ? "PendingIntentRecord" : TAG_AM;
51
Michal Karpinskiac116df2018-12-10 17:51:42 +000052 public static final int FLAG_ACTIVITY_SENDER = 1 << 0;
53 public static final int FLAG_BROADCAST_SENDER = 1 << 1;
Michal Karpinskic8aa91b2019-01-10 16:45:59 +000054 public static final int FLAG_SERVICE_SENDER = 1 << 2;
Michal Karpinskiac116df2018-12-10 17:51:42 +000055
Wale Ogunwaleee6eca12018-09-19 20:37:53 -070056 final PendingIntentController controller;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057 final Key key;
58 final int uid;
Wale Ogunwale59507092018-10-29 09:00:30 -070059 public final WeakReference<PendingIntentRecord> ref;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 boolean sent = false;
61 boolean canceled = false;
Dianne Hackborn98305522017-05-05 17:53:53 -070062 private ArrayMap<IBinder, Long> whitelistDuration;
Dianne Hackbornf66adfd2017-04-13 11:01:48 -070063 private RemoteCallbackList<IResultReceiver> mCancelCallbacks;
Michal Karpinskiac116df2018-12-10 17:51:42 +000064 private ArraySet<IBinder> mAllowBgActivityStartsForActivitySender = new ArraySet<>();
65 private ArraySet<IBinder> mAllowBgActivityStartsForBroadcastSender = new ArraySet<>();
Michal Karpinskic8aa91b2019-01-10 16:45:59 +000066 private ArraySet<IBinder> mAllowBgActivityStartsForServiceSender = new ArraySet<>();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067
Dianne Hackborn1d442e02009-04-20 18:14:05 -070068 String stringName;
Dianne Hackborna1f1a3c2014-02-24 18:12:28 -080069 String lastTagPrefix;
70 String lastTag;
Wale Ogunwalee23149f2015-03-06 15:39:44 -080071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 final static class Key {
73 final int type;
74 final String packageName;
Wale Ogunwaleee6eca12018-09-19 20:37:53 -070075 final IBinder activity;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 final String who;
77 final int requestCode;
78 final Intent requestIntent;
79 final String requestResolvedType;
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +010080 final SafeActivityOptions options;
Dianne Hackborn621e17d2010-11-22 15:59:56 -080081 Intent[] allIntents;
82 String[] allResolvedTypes;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 final int flags;
84 final int hashCode;
Amith Yamasani4ea60692012-08-28 14:34:53 -070085 final int userId;
Felipe Lemea1b79bf2016-05-24 13:06:54 -070086
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 private static final int ODD_PRIME_NUMBER = 37;
Felipe Lemea1b79bf2016-05-24 13:06:54 -070088
Wale Ogunwaleee6eca12018-09-19 20:37:53 -070089 Key(int _t, String _p, IBinder _a, String _w,
Jorim Jaggi4d8d32c2018-01-19 15:57:41 +010090 int _r, Intent[] _i, String[] _it, int _f, SafeActivityOptions _o, int _userId) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 type = _t;
92 packageName = _p;
93 activity = _a;
94 who = _w;
95 requestCode = _r;
Dianne Hackborn621e17d2010-11-22 15:59:56 -080096 requestIntent = _i != null ? _i[_i.length-1] : null;
97 requestResolvedType = _it != null ? _it[_it.length-1] : null;
98 allIntents = _i;
99 allResolvedTypes = _it;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 flags = _f;
Dianne Hackborn7a2195c2012-03-19 17:38:00 -0700101 options = _o;
Amith Yamasani4ea60692012-08-28 14:34:53 -0700102 userId = _userId;
103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 int hash = 23;
105 hash = (ODD_PRIME_NUMBER*hash) + _f;
106 hash = (ODD_PRIME_NUMBER*hash) + _r;
Amith Yamasani4ea60692012-08-28 14:34:53 -0700107 hash = (ODD_PRIME_NUMBER*hash) + _userId;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 if (_w != null) {
109 hash = (ODD_PRIME_NUMBER*hash) + _w.hashCode();
110 }
111 if (_a != null) {
112 hash = (ODD_PRIME_NUMBER*hash) + _a.hashCode();
113 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800114 if (requestIntent != null) {
115 hash = (ODD_PRIME_NUMBER*hash) + requestIntent.filterHashCode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 }
Dianne Hackborn621e17d2010-11-22 15:59:56 -0800117 if (requestResolvedType != null) {
118 hash = (ODD_PRIME_NUMBER*hash) + requestResolvedType.hashCode();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800119 }
Rubin Xuf24d6062016-07-20 17:34:50 +0100120 hash = (ODD_PRIME_NUMBER*hash) + (_p != null ? _p.hashCode() : 0);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 hash = (ODD_PRIME_NUMBER*hash) + _t;
122 hashCode = hash;
Joe Onorato8a9b2202010-02-26 18:56:32 -0800123 //Slog.i(ActivityManagerService.TAG, this + " hashCode=0x"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 // + Integer.toHexString(hashCode));
125 }
Felipe Lemea1b79bf2016-05-24 13:06:54 -0700126
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700127 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 public boolean equals(Object otherObj) {
129 if (otherObj == null) {
130 return false;
131 }
132 try {
133 Key other = (Key)otherObj;
134 if (type != other.type) {
135 return false;
136 }
Amith Yamasani4ea60692012-08-28 14:34:53 -0700137 if (userId != other.userId){
138 return false;
139 }
Rubin Xuf24d6062016-07-20 17:34:50 +0100140 if (!Objects.equals(packageName, other.packageName)) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 return false;
142 }
143 if (activity != other.activity) {
144 return false;
145 }
Rubin Xuf24d6062016-07-20 17:34:50 +0100146 if (!Objects.equals(who, other.who)) {
147 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 }
149 if (requestCode != other.requestCode) {
150 return false;
151 }
152 if (requestIntent != other.requestIntent) {
153 if (requestIntent != null) {
154 if (!requestIntent.filterEquals(other.requestIntent)) {
155 return false;
156 }
157 } else if (other.requestIntent != null) {
158 return false;
159 }
160 }
Rubin Xuf24d6062016-07-20 17:34:50 +0100161 if (!Objects.equals(requestResolvedType, other.requestResolvedType)) {
162 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 }
164 if (flags != other.flags) {
165 return false;
166 }
167 return true;
168 } catch (ClassCastException e) {
169 }
170 return false;
171 }
172
173 public int hashCode() {
174 return hashCode;
175 }
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 public String toString() {
178 return "Key{" + typeName() + " pkg=" + packageName
Dianne Hackbornc3b91fd2010-02-23 17:25:30 -0800179 + " intent="
Dianne Hackborn90c52de2011-09-23 12:57:44 -0700180 + (requestIntent != null
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800181 ? requestIntent.toShortString(false, true, false, false) : "<null>")
Amith Yamasani4ea60692012-08-28 14:34:53 -0700182 + " flags=0x" + Integer.toHexString(flags) + " u=" + userId + "}";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 }
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 String typeName() {
186 switch (type) {
Dianne Hackborna4972e92012-03-14 10:38:05 -0700187 case ActivityManager.INTENT_SENDER_ACTIVITY:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 return "startActivity";
Dianne Hackborna4972e92012-03-14 10:38:05 -0700189 case ActivityManager.INTENT_SENDER_BROADCAST:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 return "broadcastIntent";
Dianne Hackborna4972e92012-03-14 10:38:05 -0700191 case ActivityManager.INTENT_SENDER_SERVICE:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 return "startService";
Christopher Tate08992ac2017-03-21 11:37:06 -0700193 case ActivityManager.INTENT_SENDER_FOREGROUND_SERVICE:
194 return "startForegroundService";
Dianne Hackborna4972e92012-03-14 10:38:05 -0700195 case ActivityManager.INTENT_SENDER_ACTIVITY_RESULT:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 return "activityResult";
197 }
198 return Integer.toString(type);
199 }
200 }
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700201
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700202 PendingIntentRecord(PendingIntentController _controller, Key _k, int _u) {
203 controller = _controller;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800204 key = _k;
205 uid = _u;
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700206 ref = new WeakReference<>(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 }
208
Dianne Hackborn98305522017-05-05 17:53:53 -0700209 void setWhitelistDurationLocked(IBinder whitelistToken, long duration) {
210 if (duration > 0) {
211 if (whitelistDuration == null) {
212 whitelistDuration = new ArrayMap<>();
213 }
214 whitelistDuration.put(whitelistToken, duration);
215 } else if (whitelistDuration != null) {
216 whitelistDuration.remove(whitelistToken);
217 if (whitelistDuration.size() <= 0) {
218 whitelistDuration = null;
219 }
220
221 }
Felipe Lemea1b79bf2016-05-24 13:06:54 -0700222 this.stringName = null;
223 }
224
Michal Karpinskiac116df2018-12-10 17:51:42 +0000225 void setAllowBgActivityStarts(IBinder token, int flags) {
226 if (token == null) return;
227 if ((flags & FLAG_ACTIVITY_SENDER) != 0) {
228 mAllowBgActivityStartsForActivitySender.add(token);
229 }
230 if ((flags & FLAG_BROADCAST_SENDER) != 0) {
231 mAllowBgActivityStartsForBroadcastSender.add(token);
232 }
Michal Karpinskic8aa91b2019-01-10 16:45:59 +0000233 if ((flags & FLAG_SERVICE_SENDER) != 0) {
234 mAllowBgActivityStartsForServiceSender.add(token);
235 }
Michal Karpinskiac116df2018-12-10 17:51:42 +0000236 }
237
Dianne Hackbornf66adfd2017-04-13 11:01:48 -0700238 public void registerCancelListenerLocked(IResultReceiver receiver) {
239 if (mCancelCallbacks == null) {
240 mCancelCallbacks = new RemoteCallbackList<>();
241 }
242 mCancelCallbacks.register(receiver);
243 }
244
245 public void unregisterCancelListenerLocked(IResultReceiver receiver) {
Makoto Onuki8a0319a2018-04-25 16:31:05 -0700246 if (mCancelCallbacks == null) {
247 return; // Already unregistered or detached.
248 }
Dianne Hackbornf66adfd2017-04-13 11:01:48 -0700249 mCancelCallbacks.unregister(receiver);
250 if (mCancelCallbacks.getRegisteredCallbackCount() <= 0) {
251 mCancelCallbacks = null;
252 }
253 }
254
255 public RemoteCallbackList<IResultReceiver> detachCancelListenersLocked() {
256 RemoteCallbackList<IResultReceiver> listeners = mCancelCallbacks;
257 mCancelCallbacks = null;
258 return listeners;
259 }
260
Dianne Hackborn98305522017-05-05 17:53:53 -0700261 public void send(int code, Intent intent, String resolvedType, IBinder whitelistToken,
Dianne Hackborn0c4e6a82016-05-13 17:37:08 -0700262 IIntentReceiver finishedReceiver, String requiredPermission, Bundle options) {
Dianne Hackborn98305522017-05-05 17:53:53 -0700263 sendInner(code, intent, resolvedType, whitelistToken, finishedReceiver,
Andrii Kulianb1cdb102017-07-13 15:33:06 -0700264 requiredPermission, null, null, 0, 0, 0, options);
Dianne Hackbornbcbcaa72009-09-10 10:54:46 -0700265 }
Craig Mautner5f2bb4c2015-03-12 16:10:27 -0700266
Dianne Hackborn98305522017-05-05 17:53:53 -0700267 public int sendWithResult(int code, Intent intent, String resolvedType, IBinder whitelistToken,
268 IIntentReceiver finishedReceiver, String requiredPermission, Bundle options) {
269 return sendInner(code, intent, resolvedType, whitelistToken, finishedReceiver,
Andrii Kulianb1cdb102017-07-13 15:33:06 -0700270 requiredPermission, null, null, 0, 0, 0, options);
Dianne Hackborn98305522017-05-05 17:53:53 -0700271 }
272
Wale Ogunwale59507092018-10-29 09:00:30 -0700273 public int sendInner(int code, Intent intent, String resolvedType, IBinder whitelistToken,
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700274 IIntentReceiver finishedReceiver, String requiredPermission, IBinder resultTo,
275 String resultWho, int requestCode, int flagsMask, int flagsValues, Bundle options) {
Jeff Sharkeyf0ec2e02016-03-21 12:37:54 -0600276 if (intent != null) intent.setDefusable(true);
277 if (options != null) options.setDefusable(true);
278
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700279 Long duration = null;
280 Intent finalIntent = null;
281 Intent[] allIntents = null;
282 String[] allResolvedTypes = null;
283 SafeActivityOptions mergedOptions = null;
284 synchronized (controller.mLock) {
285 if (canceled) {
286 return ActivityManager.START_CANCELED;
287 }
Svetoslavb0a78392015-04-10 17:25:35 -0700288
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700289 sent = true;
290 if ((key.flags & PendingIntent.FLAG_ONE_SHOT) != 0) {
291 controller.cancelIntentSender(this, true);
292 }
Svetoslavb0a78392015-04-10 17:25:35 -0700293
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700294 finalIntent = key.requestIntent != null ? new Intent(key.requestIntent) : new Intent();
295
296 final boolean immutable = (key.flags & PendingIntent.FLAG_IMMUTABLE) != 0;
297 if (!immutable) {
298 if (intent != null) {
299 int changes = finalIntent.fillIn(intent, key.flags);
300 if ((changes & Intent.FILL_IN_DATA) == 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 resolvedType = key.requestResolvedType;
302 }
303 } else {
304 resolvedType = key.requestResolvedType;
305 }
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700306 flagsMask &= ~Intent.IMMUTABLE_FLAGS;
307 flagsValues &= flagsMask;
308 finalIntent.setFlags((finalIntent.getFlags() & ~flagsMask) | flagsValues);
309 } else {
310 resolvedType = key.requestResolvedType;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 }
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700312
Winson Chungd0707752018-09-27 13:55:58 -0700313 // Apply any launch flags from the ActivityOptions. This is to ensure that the caller
314 // can specify a consistent launch mode even if the PendingIntent is immutable
315 final ActivityOptions opts = ActivityOptions.fromBundle(options);
316 if (opts != null) {
317 finalIntent.addFlags(opts.getPendingIntentLaunchFlags());
318 }
319
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700320 // Extract options before clearing calling identity
321 mergedOptions = key.options;
322 if (mergedOptions == null) {
Winson Chungd0707752018-09-27 13:55:58 -0700323 mergedOptions = new SafeActivityOptions(opts);
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700324 } else {
Winson Chungd0707752018-09-27 13:55:58 -0700325 mergedOptions.setCallerOptions(opts);
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700326 }
327
328 if (whitelistDuration != null) {
329 duration = whitelistDuration.get(whitelistToken);
330 }
331
332 if (key.type == ActivityManager.INTENT_SENDER_ACTIVITY
333 && key.allIntents != null && key.allIntents.length > 1) {
334 // Copy all intents and resolved types while we have the controller lock so we can
335 // use it later when the lock isn't held.
336 allIntents = new Intent[key.allIntents.length];
337 allResolvedTypes = new String[key.allIntents.length];
338 System.arraycopy(key.allIntents, 0, allIntents, 0, key.allIntents.length);
339 if (key.allResolvedTypes != null) {
340 System.arraycopy(key.allResolvedTypes, 0, allResolvedTypes, 0,
341 key.allResolvedTypes.length);
342 }
343 allIntents[allIntents.length - 1] = finalIntent;
344 allResolvedTypes[allResolvedTypes.length - 1] = resolvedType;
345 }
346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 }
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700348 // We don't hold the controller lock beyond this point as we will be calling into AM and WM.
349
350 final int callingUid = Binder.getCallingUid();
351 final int callingPid = Binder.getCallingPid();
352 final long origId = Binder.clearCallingIdentity();
353
354 int res = START_SUCCESS;
355 try {
356 if (duration != null) {
357 int procState = controller.mAmInternal.getUidProcessState(callingUid);
358 if (!ActivityManager.isProcStateBackground(procState)) {
359 StringBuilder tag = new StringBuilder(64);
360 tag.append("pendingintent:");
361 UserHandle.formatUid(tag, callingUid);
362 tag.append(":");
363 if (finalIntent.getAction() != null) {
364 tag.append(finalIntent.getAction());
365 } else if (finalIntent.getComponent() != null) {
366 finalIntent.getComponent().appendShortString(tag);
367 } else if (finalIntent.getData() != null) {
368 tag.append(finalIntent.getData().toSafeString());
369 }
370 controller.mAmInternal.tempWhitelistForPendingIntent(callingPid, callingUid,
371 uid, duration, tag.toString());
372 } else {
373 Slog.w(TAG, "Not doing whitelist " + this + ": caller state=" + procState);
374 }
375 }
376
377 boolean sendFinish = finishedReceiver != null;
378 int userId = key.userId;
379 if (userId == UserHandle.USER_CURRENT) {
380 userId = controller.mUserController.getCurrentOrTargetUserId();
381 }
Michal Karpinskicc88d7e2019-01-24 15:32:12 +0000382 // temporarily allow receivers and services to open activities from background if the
383 // PendingIntent.send() caller was foreground at the time of sendInner() call
384 final boolean allowTrampoline = uid != callingUid
385 && controller.mAtmInternal.isUidForeground(callingUid);
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700386
Michal Karpinski9cbb20b2019-02-05 17:31:50 +0000387 // note: we on purpose don't pass in the information about the PendingIntent's creator,
388 // like pid or ProcessRecord, to the ActivityTaskManagerInternal calls below, because
389 // it's not unusual for the creator's process to not be alive at this time
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700390 switch (key.type) {
391 case ActivityManager.INTENT_SENDER_ACTIVITY:
392 try {
393 // Note when someone has a pending intent, even from different
394 // users, then there's no need to ensure the calling user matches
395 // the target user, so validateIncomingUser is always false below.
396
397 if (key.allIntents != null && key.allIntents.length > 1) {
398 res = controller.mAtmInternal.startActivitiesInPackage(
Michal Karpinski84d9ebd2019-01-17 18:28:59 +0000399 uid, callingPid, callingUid, key.packageName, allIntents,
400 allResolvedTypes, resultTo, mergedOptions, userId,
401 false /* validateIncomingUser */,
Michal Karpinskiac116df2018-12-10 17:51:42 +0000402 this /* originatingPendingIntent */,
403 mAllowBgActivityStartsForActivitySender.contains(whitelistToken));
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700404 } else {
405 res = controller.mAtmInternal.startActivityInPackage(
406 uid, callingPid, callingUid, key.packageName, finalIntent,
407 resolvedType, resultTo, resultWho, requestCode, 0,
408 mergedOptions, userId, null, "PendingIntentRecord",
409 false /* validateIncomingUser */,
Michal Karpinskiac116df2018-12-10 17:51:42 +0000410 this /* originatingPendingIntent */,
411 mAllowBgActivityStartsForActivitySender.contains(whitelistToken));
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700412 }
413 } catch (RuntimeException e) {
414 Slog.w(TAG, "Unable to send startActivity intent", e);
415 }
416 break;
417 case ActivityManager.INTENT_SENDER_ACTIVITY_RESULT:
418 controller.mAtmInternal.sendActivityResult(-1, key.activity, key.who,
419 key.requestCode, code, finalIntent);
420 break;
421 case ActivityManager.INTENT_SENDER_BROADCAST:
422 try {
423 // If a completion callback has been requested, require
424 // that the broadcast be delivered synchronously
425 int sent = controller.mAmInternal.broadcastIntentInPackage(key.packageName,
Michal Karpinskic99d7182019-02-17 13:15:23 +0000426 uid, callingUid, callingPid, finalIntent, resolvedType,
427 finishedReceiver, code, null, null, requiredPermission, options,
428 (finishedReceiver != null), false, userId,
Michal Karpinskicc88d7e2019-01-24 15:32:12 +0000429 mAllowBgActivityStartsForBroadcastSender.contains(whitelistToken)
430 || allowTrampoline);
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700431 if (sent == ActivityManager.BROADCAST_SUCCESS) {
432 sendFinish = false;
433 }
434 } catch (RuntimeException e) {
435 Slog.w(TAG, "Unable to send startActivity intent", e);
436 }
437 break;
438 case ActivityManager.INTENT_SENDER_SERVICE:
439 case ActivityManager.INTENT_SENDER_FOREGROUND_SERVICE:
440 try {
441 controller.mAmInternal.startServiceInPackage(uid, finalIntent, resolvedType,
442 key.type == ActivityManager.INTENT_SENDER_FOREGROUND_SERVICE,
Michal Karpinskic8aa91b2019-01-10 16:45:59 +0000443 key.packageName, userId,
Michal Karpinskicc88d7e2019-01-24 15:32:12 +0000444 mAllowBgActivityStartsForServiceSender.contains(whitelistToken)
445 || allowTrampoline);
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700446 } catch (RuntimeException e) {
447 Slog.w(TAG, "Unable to send startService intent", e);
448 } catch (TransactionTooLargeException e) {
449 res = ActivityManager.START_CANCELED;
450 }
451 break;
452 }
453
454 if (sendFinish && res != ActivityManager.START_CANCELED) {
455 try {
456 finishedReceiver.performReceive(new Intent(finalIntent), 0,
457 null, null, false, false, key.userId);
458 } catch (RemoteException e) {
459 }
460 }
461 } finally {
462 Binder.restoreCallingIdentity(origId);
463 }
464
465 return res;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 }
Craig Mautnerdf88d732014-01-27 09:21:32 -0800467
468 @Override
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 protected void finalize() throws Throwable {
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800470 try {
471 if (!canceled) {
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700472 controller.mH.sendMessage(PooledLambda.obtainMessage(
473 PendingIntentRecord::completeFinalize, this));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800474 }
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800475 } finally {
476 super.finalize();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800477 }
478 }
479
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700480 private void completeFinalize() {
481 synchronized(controller.mLock) {
482 WeakReference<PendingIntentRecord> current = controller.mIntentSenderRecords.get(key);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800483 if (current == ref) {
Wale Ogunwaleee6eca12018-09-19 20:37:53 -0700484 controller.mIntentSenderRecords.remove(key);
Dianne Hackborn9e0f5d92010-02-22 15:05:42 -0800485 }
486 }
487 }
Felipe Lemea1b79bf2016-05-24 13:06:54 -0700488
Wale Ogunwale59507092018-10-29 09:00:30 -0700489 public void dump(PrintWriter pw, String prefix) {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700490 pw.print(prefix); pw.print("uid="); pw.print(uid);
491 pw.print(" packageName="); pw.print(key.packageName);
492 pw.print(" type="); pw.print(key.typeName());
493 pw.print(" flags=0x"); pw.println(Integer.toHexString(key.flags));
494 if (key.activity != null || key.who != null) {
495 pw.print(prefix); pw.print("activity="); pw.print(key.activity);
496 pw.print(" who="); pw.println(key.who);
497 }
498 if (key.requestCode != 0 || key.requestResolvedType != null) {
499 pw.print(prefix); pw.print("requestCode="); pw.print(key.requestCode);
500 pw.print(" requestResolvedType="); pw.println(key.requestResolvedType);
501 }
Dianne Hackbornc3b91fd2010-02-23 17:25:30 -0800502 if (key.requestIntent != null) {
503 pw.print(prefix); pw.print("requestIntent=");
Dianne Hackborn21c241e2012-03-08 13:57:23 -0800504 pw.println(key.requestIntent.toShortString(false, true, true, true));
Dianne Hackbornc3b91fd2010-02-23 17:25:30 -0800505 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700506 if (sent || canceled) {
507 pw.print(prefix); pw.print("sent="); pw.print(sent);
508 pw.print(" canceled="); pw.println(canceled);
509 }
Dianne Hackborn98305522017-05-05 17:53:53 -0700510 if (whitelistDuration != null) {
Dianne Hackbornbc02a392016-06-02 17:15:08 -0700511 pw.print(prefix);
512 pw.print("whitelistDuration=");
Dianne Hackborn98305522017-05-05 17:53:53 -0700513 for (int i = 0; i < whitelistDuration.size(); i++) {
514 if (i != 0) {
515 pw.print(", ");
516 }
517 pw.print(Integer.toHexString(System.identityHashCode(whitelistDuration.keyAt(i))));
518 pw.print(":");
519 TimeUtils.formatDuration(whitelistDuration.valueAt(i), pw);
520 }
Dianne Hackbornbc02a392016-06-02 17:15:08 -0700521 pw.println();
522 }
Dianne Hackbornf66adfd2017-04-13 11:01:48 -0700523 if (mCancelCallbacks != null) {
524 pw.print(prefix); pw.println("mCancelCallbacks:");
525 for (int i = 0; i < mCancelCallbacks.getRegisteredCallbackCount(); i++) {
526 pw.print(prefix); pw.print(" #"); pw.print(i); pw.print(": ");
527 pw.println(mCancelCallbacks.getRegisteredCallbackItem(i));
528 }
529 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800530 }
531
532 public String toString() {
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700533 if (stringName != null) {
534 return stringName;
535 }
536 StringBuilder sb = new StringBuilder(128);
537 sb.append("PendingIntentRecord{");
538 sb.append(Integer.toHexString(System.identityHashCode(this)));
539 sb.append(' ');
540 sb.append(key.packageName);
541 sb.append(' ');
542 sb.append(key.typeName());
Dianne Hackborn98305522017-05-05 17:53:53 -0700543 if (whitelistDuration != null) {
Dianne Hackbornbc02a392016-06-02 17:15:08 -0700544 sb.append( " (whitelist: ");
Dianne Hackborn98305522017-05-05 17:53:53 -0700545 for (int i = 0; i < whitelistDuration.size(); i++) {
546 if (i != 0) {
547 sb.append(",");
548 }
549 sb.append(Integer.toHexString(System.identityHashCode(whitelistDuration.keyAt(i))));
550 sb.append(":");
551 TimeUtils.formatDuration(whitelistDuration.valueAt(i), sb);
552 }
Dianne Hackbornbc02a392016-06-02 17:15:08 -0700553 sb.append(")");
Felipe Lemea1b79bf2016-05-24 13:06:54 -0700554 }
Dianne Hackborn1d442e02009-04-20 18:14:05 -0700555 sb.append('}');
556 return stringName = sb.toString();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 }
558}