2 * Copyright (C) 2006 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.server.am;
19 import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM;
20 import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME;
22 import android.app.ActivityManager;
23 import android.content.IIntentSender;
24 import android.content.IIntentReceiver;
25 import android.app.PendingIntent;
26 import android.content.Intent;
27 import android.os.Binder;
28 import android.os.Bundle;
29 import android.os.IBinder;
30 import android.os.RemoteCallbackList;
31 import android.os.RemoteException;
32 import android.os.TransactionTooLargeException;
33 import android.os.UserHandle;
34 import android.util.ArrayMap;
35 import android.util.Slog;
36 import android.util.TimeUtils;
38 import com.android.internal.os.IResultReceiver;
40 import java.io.PrintWriter;
41 import java.lang.ref.WeakReference;
42 import java.util.Objects;
44 final class PendingIntentRecord extends IIntentSender.Stub {
45 private static final String TAG = TAG_WITH_CLASS_NAME ? "PendingIntentRecord" : TAG_AM;
47 final ActivityManagerService owner;
50 final WeakReference<PendingIntentRecord> ref;
52 boolean canceled = false;
53 private ArrayMap<IBinder, Long> whitelistDuration;
54 private RemoteCallbackList<IResultReceiver> mCancelCallbacks;
60 final static class Key {
62 final String packageName;
63 final ActivityRecord activity;
65 final int requestCode;
66 final Intent requestIntent;
67 final String requestResolvedType;
70 String[] allResolvedTypes;
75 private static final int ODD_PRIME_NUMBER = 37;
77 Key(int _t, String _p, ActivityRecord _a, String _w,
78 int _r, Intent[] _i, String[] _it, int _f, Bundle _o, int _userId) {
84 requestIntent = _i != null ? _i[_i.length-1] : null;
85 requestResolvedType = _it != null ? _it[_it.length-1] : null;
87 allResolvedTypes = _it;
93 hash = (ODD_PRIME_NUMBER*hash) + _f;
94 hash = (ODD_PRIME_NUMBER*hash) + _r;
95 hash = (ODD_PRIME_NUMBER*hash) + _userId;
97 hash = (ODD_PRIME_NUMBER*hash) + _w.hashCode();
100 hash = (ODD_PRIME_NUMBER*hash) + _a.hashCode();
102 if (requestIntent != null) {
103 hash = (ODD_PRIME_NUMBER*hash) + requestIntent.filterHashCode();
105 if (requestResolvedType != null) {
106 hash = (ODD_PRIME_NUMBER*hash) + requestResolvedType.hashCode();
108 hash = (ODD_PRIME_NUMBER*hash) + (_p != null ? _p.hashCode() : 0);
109 hash = (ODD_PRIME_NUMBER*hash) + _t;
111 //Slog.i(ActivityManagerService.TAG, this + " hashCode=0x"
112 // + Integer.toHexString(hashCode));
115 public boolean equals(Object otherObj) {
116 if (otherObj == null) {
120 Key other = (Key)otherObj;
121 if (type != other.type) {
124 if (userId != other.userId){
127 if (!Objects.equals(packageName, other.packageName)) {
130 if (activity != other.activity) {
133 if (!Objects.equals(who, other.who)) {
136 if (requestCode != other.requestCode) {
139 if (requestIntent != other.requestIntent) {
140 if (requestIntent != null) {
141 if (!requestIntent.filterEquals(other.requestIntent)) {
144 } else if (other.requestIntent != null) {
148 if (!Objects.equals(requestResolvedType, other.requestResolvedType)) {
151 if (flags != other.flags) {
155 } catch (ClassCastException e) {
160 public int hashCode() {
164 public String toString() {
165 return "Key{" + typeName() + " pkg=" + packageName
167 + (requestIntent != null
168 ? requestIntent.toShortString(false, true, false, false) : "<null>")
169 + " flags=0x" + Integer.toHexString(flags) + " u=" + userId + "}";
174 case ActivityManager.INTENT_SENDER_ACTIVITY:
175 return "startActivity";
176 case ActivityManager.INTENT_SENDER_BROADCAST:
177 return "broadcastIntent";
178 case ActivityManager.INTENT_SENDER_SERVICE:
179 return "startService";
180 case ActivityManager.INTENT_SENDER_FOREGROUND_SERVICE:
181 return "startForegroundService";
182 case ActivityManager.INTENT_SENDER_ACTIVITY_RESULT:
183 return "activityResult";
185 return Integer.toString(type);
189 PendingIntentRecord(ActivityManagerService _owner, Key _k, int _u) {
193 ref = new WeakReference<PendingIntentRecord>(this);
196 void setWhitelistDurationLocked(IBinder whitelistToken, long duration) {
198 if (whitelistDuration == null) {
199 whitelistDuration = new ArrayMap<>();
201 whitelistDuration.put(whitelistToken, duration);
202 } else if (whitelistDuration != null) {
203 whitelistDuration.remove(whitelistToken);
204 if (whitelistDuration.size() <= 0) {
205 whitelistDuration = null;
209 this.stringName = null;
212 public void registerCancelListenerLocked(IResultReceiver receiver) {
213 if (mCancelCallbacks == null) {
214 mCancelCallbacks = new RemoteCallbackList<>();
216 mCancelCallbacks.register(receiver);
219 public void unregisterCancelListenerLocked(IResultReceiver receiver) {
220 mCancelCallbacks.unregister(receiver);
221 if (mCancelCallbacks.getRegisteredCallbackCount() <= 0) {
222 mCancelCallbacks = null;
226 public RemoteCallbackList<IResultReceiver> detachCancelListenersLocked() {
227 RemoteCallbackList<IResultReceiver> listeners = mCancelCallbacks;
228 mCancelCallbacks = null;
232 public void send(int code, Intent intent, String resolvedType, IBinder whitelistToken,
233 IIntentReceiver finishedReceiver, String requiredPermission, Bundle options) {
234 sendInner(code, intent, resolvedType, whitelistToken, finishedReceiver,
235 requiredPermission, null, null, 0, 0, 0, options);
238 public int sendWithResult(int code, Intent intent, String resolvedType, IBinder whitelistToken,
239 IIntentReceiver finishedReceiver, String requiredPermission, Bundle options) {
240 return sendInner(code, intent, resolvedType, whitelistToken, finishedReceiver,
241 requiredPermission, null, null, 0, 0, 0, options);
244 int sendInner(int code, Intent intent, String resolvedType, IBinder whitelistToken,
245 IIntentReceiver finishedReceiver,
246 String requiredPermission, IBinder resultTo, String resultWho, int requestCode,
247 int flagsMask, int flagsValues, Bundle options) {
248 if (intent != null) intent.setDefusable(true);
249 if (options != null) options.setDefusable(true);
251 synchronized (owner) {
254 if ((key.flags&PendingIntent.FLAG_ONE_SHOT) != 0) {
255 owner.cancelIntentSenderLocked(this, true);
258 Intent finalIntent = key.requestIntent != null
259 ? new Intent(key.requestIntent) : new Intent();
261 final boolean immutable = (key.flags & PendingIntent.FLAG_IMMUTABLE) != 0;
263 if (intent != null) {
264 int changes = finalIntent.fillIn(intent, key.flags);
265 if ((changes & Intent.FILL_IN_DATA) == 0) {
266 resolvedType = key.requestResolvedType;
269 resolvedType = key.requestResolvedType;
271 flagsMask &= ~Intent.IMMUTABLE_FLAGS;
272 flagsValues &= flagsMask;
273 finalIntent.setFlags((finalIntent.getFlags() & ~flagsMask) | flagsValues);
275 resolvedType = key.requestResolvedType;
278 final int callingUid = Binder.getCallingUid();
279 final int callingPid = Binder.getCallingPid();
281 final long origId = Binder.clearCallingIdentity();
283 if (whitelistDuration != null) {
284 Long duration = whitelistDuration.get(whitelistToken);
285 if (duration != null) {
286 int procState = owner.getUidState(callingUid);
287 if (!ActivityManager.isProcStateBackground(procState)) {
288 StringBuilder tag = new StringBuilder(64);
289 tag.append("pendingintent:");
290 UserHandle.formatUid(tag, callingUid);
292 if (finalIntent.getAction() != null) {
293 tag.append(finalIntent.getAction());
294 } else if (finalIntent.getComponent() != null) {
295 finalIntent.getComponent().appendShortString(tag);
296 } else if (finalIntent.getData() != null) {
297 tag.append(finalIntent.getData());
299 owner.tempWhitelistForPendingIntentLocked(callingPid,
300 callingUid, uid, duration, tag.toString());
302 Slog.w(TAG, "Not doing whitelist " + this + ": caller state="
308 boolean sendFinish = finishedReceiver != null;
309 int userId = key.userId;
310 if (userId == UserHandle.USER_CURRENT) {
311 userId = owner.mUserController.getCurrentOrTargetUserIdLocked();
315 case ActivityManager.INTENT_SENDER_ACTIVITY:
316 if (options == null) {
317 options = key.options;
318 } else if (key.options != null) {
319 Bundle opts = new Bundle(key.options);
320 opts.putAll(options);
324 if (key.allIntents != null && key.allIntents.length > 1) {
325 Intent[] allIntents = new Intent[key.allIntents.length];
326 String[] allResolvedTypes = new String[key.allIntents.length];
327 System.arraycopy(key.allIntents, 0, allIntents, 0,
328 key.allIntents.length);
329 if (key.allResolvedTypes != null) {
330 System.arraycopy(key.allResolvedTypes, 0, allResolvedTypes, 0,
331 key.allResolvedTypes.length);
333 allIntents[allIntents.length-1] = finalIntent;
334 allResolvedTypes[allResolvedTypes.length-1] = resolvedType;
335 owner.startActivitiesInPackage(uid, key.packageName, allIntents,
336 allResolvedTypes, resultTo, options, userId);
338 owner.startActivityInPackage(uid, key.packageName, finalIntent,
339 resolvedType, resultTo, resultWho, requestCode, 0,
340 options, userId, null, "PendingIntentRecord");
342 } catch (RuntimeException e) {
343 Slog.w(TAG, "Unable to send startActivity intent", e);
346 case ActivityManager.INTENT_SENDER_ACTIVITY_RESULT:
347 final ActivityStack stack = key.activity.getStack();
349 stack.sendActivityResultLocked(-1, key.activity, key.who,
350 key.requestCode, code, finalIntent);
353 case ActivityManager.INTENT_SENDER_BROADCAST:
355 // If a completion callback has been requested, require
356 // that the broadcast be delivered synchronously
357 int sent = owner.broadcastIntentInPackage(key.packageName, uid,
358 finalIntent, resolvedType, finishedReceiver, code, null, null,
359 requiredPermission, options, (finishedReceiver != null),
361 if (sent == ActivityManager.BROADCAST_SUCCESS) {
364 } catch (RuntimeException e) {
365 Slog.w(TAG, "Unable to send startActivity intent", e);
368 case ActivityManager.INTENT_SENDER_SERVICE:
369 case ActivityManager.INTENT_SENDER_FOREGROUND_SERVICE:
371 owner.startServiceInPackage(uid, finalIntent, resolvedType,
372 key.type == ActivityManager.INTENT_SENDER_FOREGROUND_SERVICE,
373 key.packageName, userId);
374 } catch (RuntimeException e) {
375 Slog.w(TAG, "Unable to send startService intent", e);
376 } catch (TransactionTooLargeException e) {
377 res = ActivityManager.START_CANCELED;
382 if (sendFinish && res != ActivityManager.START_CANCELED) {
384 finishedReceiver.performReceive(new Intent(finalIntent), 0,
385 null, null, false, false, key.userId);
386 } catch (RemoteException e) {
390 Binder.restoreCallingIdentity(origId);
395 return ActivityManager.START_CANCELED;
399 protected void finalize() throws Throwable {
402 owner.mHandler.sendMessage(owner.mHandler.obtainMessage(
403 ActivityManagerService.FINALIZE_PENDING_INTENT_MSG, this));
410 public void completeFinalize() {
411 synchronized(owner) {
412 WeakReference<PendingIntentRecord> current =
413 owner.mIntentSenderRecords.get(key);
414 if (current == ref) {
415 owner.mIntentSenderRecords.remove(key);
420 void dump(PrintWriter pw, String prefix) {
421 pw.print(prefix); pw.print("uid="); pw.print(uid);
422 pw.print(" packageName="); pw.print(key.packageName);
423 pw.print(" type="); pw.print(key.typeName());
424 pw.print(" flags=0x"); pw.println(Integer.toHexString(key.flags));
425 if (key.activity != null || key.who != null) {
426 pw.print(prefix); pw.print("activity="); pw.print(key.activity);
427 pw.print(" who="); pw.println(key.who);
429 if (key.requestCode != 0 || key.requestResolvedType != null) {
430 pw.print(prefix); pw.print("requestCode="); pw.print(key.requestCode);
431 pw.print(" requestResolvedType="); pw.println(key.requestResolvedType);
433 if (key.requestIntent != null) {
434 pw.print(prefix); pw.print("requestIntent=");
435 pw.println(key.requestIntent.toShortString(false, true, true, true));
437 if (sent || canceled) {
438 pw.print(prefix); pw.print("sent="); pw.print(sent);
439 pw.print(" canceled="); pw.println(canceled);
441 if (whitelistDuration != null) {
443 pw.print("whitelistDuration=");
444 for (int i = 0; i < whitelistDuration.size(); i++) {
448 pw.print(Integer.toHexString(System.identityHashCode(whitelistDuration.keyAt(i))));
450 TimeUtils.formatDuration(whitelistDuration.valueAt(i), pw);
454 if (mCancelCallbacks != null) {
455 pw.print(prefix); pw.println("mCancelCallbacks:");
456 for (int i = 0; i < mCancelCallbacks.getRegisteredCallbackCount(); i++) {
457 pw.print(prefix); pw.print(" #"); pw.print(i); pw.print(": ");
458 pw.println(mCancelCallbacks.getRegisteredCallbackItem(i));
463 public String toString() {
464 if (stringName != null) {
467 StringBuilder sb = new StringBuilder(128);
468 sb.append("PendingIntentRecord{");
469 sb.append(Integer.toHexString(System.identityHashCode(this)));
471 sb.append(key.packageName);
473 sb.append(key.typeName());
474 if (whitelistDuration != null) {
475 sb.append( " (whitelist: ");
476 for (int i = 0; i < whitelistDuration.size(); i++) {
480 sb.append(Integer.toHexString(System.identityHashCode(whitelistDuration.keyAt(i))));
482 TimeUtils.formatDuration(whitelistDuration.valueAt(i), sb);
487 return stringName = sb.toString();