The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.am; |
| 18 | |
Jorim Jaggi | e2ad37f | 2018-01-22 22:41:22 +0100 | [diff] [blame] | 19 | import static android.app.ActivityManager.START_SUCCESS; |
Wale Ogunwale | e23149f | 2015-03-06 15:39:44 -0800 | [diff] [blame] | 20 | import static com.android.server.am.ActivityManagerDebugConfig.TAG_AM; |
| 21 | import static com.android.server.am.ActivityManagerDebugConfig.TAG_WITH_CLASS_NAME; |
| 22 | |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 23 | import android.app.ActivityManager; |
Jorim Jaggi | 4d8d32c | 2018-01-19 15:57:41 +0100 | [diff] [blame] | 24 | import android.app.ActivityOptions; |
Suchi Amalapurapu | 1ccac75 | 2009-06-12 10:09:58 -0700 | [diff] [blame] | 25 | import android.content.IIntentSender; |
| 26 | import android.content.IIntentReceiver; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.app.PendingIntent; |
| 28 | import android.content.Intent; |
| 29 | import android.os.Binder; |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 30 | import android.os.Bundle; |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 31 | import android.os.IBinder; |
Dianne Hackborn | f66adfd | 2017-04-13 11:01:48 -0700 | [diff] [blame] | 32 | import android.os.RemoteCallbackList; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.os.RemoteException; |
Craig Mautner | 5f2bb4c | 2015-03-12 16:10:27 -0700 | [diff] [blame] | 34 | import android.os.TransactionTooLargeException; |
Dianne Hackborn | 50cdf7c3 | 2012-09-23 17:08:57 -0700 | [diff] [blame] | 35 | import android.os.UserHandle; |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 36 | import android.util.ArrayMap; |
Michal Karpinski | ac116df | 2018-12-10 17:51:42 +0000 | [diff] [blame] | 37 | import android.util.ArraySet; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 38 | import android.util.Slog; |
Dianne Hackborn | bc02a39 | 2016-06-02 17:15:08 -0700 | [diff] [blame] | 39 | import android.util.TimeUtils; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | |
Dianne Hackborn | f66adfd | 2017-04-13 11:01:48 -0700 | [diff] [blame] | 41 | import com.android.internal.os.IResultReceiver; |
Wale Ogunwale | ee6eca1 | 2018-09-19 20:37:53 -0700 | [diff] [blame] | 42 | import com.android.internal.util.function.pooled.PooledLambda; |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 43 | import com.android.server.wm.SafeActivityOptions; |
Craig Mautner | b916836 | 2015-02-26 20:40:19 -0800 | [diff] [blame] | 44 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import java.io.PrintWriter; |
| 46 | import java.lang.ref.WeakReference; |
Rubin Xu | f24d606 | 2016-07-20 17:34:50 +0100 | [diff] [blame] | 47 | import java.util.Objects; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | |
Wale Ogunwale | ee6eca1 | 2018-09-19 20:37:53 -0700 | [diff] [blame] | 49 | public final class PendingIntentRecord extends IIntentSender.Stub { |
Wale Ogunwale | e23149f | 2015-03-06 15:39:44 -0800 | [diff] [blame] | 50 | private static final String TAG = TAG_WITH_CLASS_NAME ? "PendingIntentRecord" : TAG_AM; |
| 51 | |
Michal Karpinski | ac116df | 2018-12-10 17:51:42 +0000 | [diff] [blame] | 52 | public static final int FLAG_ACTIVITY_SENDER = 1 << 0; |
| 53 | public static final int FLAG_BROADCAST_SENDER = 1 << 1; |
Michal Karpinski | c8aa91b | 2019-01-10 16:45:59 +0000 | [diff] [blame] | 54 | public static final int FLAG_SERVICE_SENDER = 1 << 2; |
Michal Karpinski | ac116df | 2018-12-10 17:51:42 +0000 | [diff] [blame] | 55 | |
Wale Ogunwale | ee6eca1 | 2018-09-19 20:37:53 -0700 | [diff] [blame] | 56 | final PendingIntentController controller; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | final Key key; |
| 58 | final int uid; |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 59 | public final WeakReference<PendingIntentRecord> ref; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | boolean sent = false; |
| 61 | boolean canceled = false; |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 62 | private ArrayMap<IBinder, Long> whitelistDuration; |
Dianne Hackborn | f66adfd | 2017-04-13 11:01:48 -0700 | [diff] [blame] | 63 | private RemoteCallbackList<IResultReceiver> mCancelCallbacks; |
Michal Karpinski | ac116df | 2018-12-10 17:51:42 +0000 | [diff] [blame] | 64 | private ArraySet<IBinder> mAllowBgActivityStartsForActivitySender = new ArraySet<>(); |
| 65 | private ArraySet<IBinder> mAllowBgActivityStartsForBroadcastSender = new ArraySet<>(); |
Michal Karpinski | c8aa91b | 2019-01-10 16:45:59 +0000 | [diff] [blame] | 66 | private ArraySet<IBinder> mAllowBgActivityStartsForServiceSender = new ArraySet<>(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 68 | String stringName; |
Dianne Hackborn | a1f1a3c | 2014-02-24 18:12:28 -0800 | [diff] [blame] | 69 | String lastTagPrefix; |
| 70 | String lastTag; |
Wale Ogunwale | e23149f | 2015-03-06 15:39:44 -0800 | [diff] [blame] | 71 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | final static class Key { |
| 73 | final int type; |
| 74 | final String packageName; |
Wale Ogunwale | ee6eca1 | 2018-09-19 20:37:53 -0700 | [diff] [blame] | 75 | final IBinder activity; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | final String who; |
| 77 | final int requestCode; |
| 78 | final Intent requestIntent; |
| 79 | final String requestResolvedType; |
Jorim Jaggi | 4d8d32c | 2018-01-19 15:57:41 +0100 | [diff] [blame] | 80 | final SafeActivityOptions options; |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 81 | Intent[] allIntents; |
| 82 | String[] allResolvedTypes; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 83 | final int flags; |
| 84 | final int hashCode; |
Amith Yamasani | 4ea6069 | 2012-08-28 14:34:53 -0700 | [diff] [blame] | 85 | final int userId; |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 86 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | private static final int ODD_PRIME_NUMBER = 37; |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 88 | |
Wale Ogunwale | ee6eca1 | 2018-09-19 20:37:53 -0700 | [diff] [blame] | 89 | Key(int _t, String _p, IBinder _a, String _w, |
Jorim Jaggi | 4d8d32c | 2018-01-19 15:57:41 +0100 | [diff] [blame] | 90 | int _r, Intent[] _i, String[] _it, int _f, SafeActivityOptions _o, int _userId) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | type = _t; |
| 92 | packageName = _p; |
| 93 | activity = _a; |
| 94 | who = _w; |
| 95 | requestCode = _r; |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 96 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | flags = _f; |
Dianne Hackborn | 7a2195c | 2012-03-19 17:38:00 -0700 | [diff] [blame] | 101 | options = _o; |
Amith Yamasani | 4ea6069 | 2012-08-28 14:34:53 -0700 | [diff] [blame] | 102 | userId = _userId; |
| 103 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | int hash = 23; |
| 105 | hash = (ODD_PRIME_NUMBER*hash) + _f; |
| 106 | hash = (ODD_PRIME_NUMBER*hash) + _r; |
Amith Yamasani | 4ea6069 | 2012-08-28 14:34:53 -0700 | [diff] [blame] | 107 | hash = (ODD_PRIME_NUMBER*hash) + _userId; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | 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 Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 114 | if (requestIntent != null) { |
| 115 | hash = (ODD_PRIME_NUMBER*hash) + requestIntent.filterHashCode(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | } |
Dianne Hackborn | 621e17d | 2010-11-22 15:59:56 -0800 | [diff] [blame] | 117 | if (requestResolvedType != null) { |
| 118 | hash = (ODD_PRIME_NUMBER*hash) + requestResolvedType.hashCode(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | } |
Rubin Xu | f24d606 | 2016-07-20 17:34:50 +0100 | [diff] [blame] | 120 | hash = (ODD_PRIME_NUMBER*hash) + (_p != null ? _p.hashCode() : 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | hash = (ODD_PRIME_NUMBER*hash) + _t; |
| 122 | hashCode = hash; |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 123 | //Slog.i(ActivityManagerService.TAG, this + " hashCode=0x" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | // + Integer.toHexString(hashCode)); |
| 125 | } |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 126 | |
Wale Ogunwale | ee6eca1 | 2018-09-19 20:37:53 -0700 | [diff] [blame] | 127 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | 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 Yamasani | 4ea6069 | 2012-08-28 14:34:53 -0700 | [diff] [blame] | 137 | if (userId != other.userId){ |
| 138 | return false; |
| 139 | } |
Rubin Xu | f24d606 | 2016-07-20 17:34:50 +0100 | [diff] [blame] | 140 | if (!Objects.equals(packageName, other.packageName)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | return false; |
| 142 | } |
| 143 | if (activity != other.activity) { |
| 144 | return false; |
| 145 | } |
Rubin Xu | f24d606 | 2016-07-20 17:34:50 +0100 | [diff] [blame] | 146 | if (!Objects.equals(who, other.who)) { |
| 147 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | } |
| 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 Xu | f24d606 | 2016-07-20 17:34:50 +0100 | [diff] [blame] | 161 | if (!Objects.equals(requestResolvedType, other.requestResolvedType)) { |
| 162 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | } |
| 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 Mautner | 5f2bb4c | 2015-03-12 16:10:27 -0700 | [diff] [blame] | 176 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | public String toString() { |
| 178 | return "Key{" + typeName() + " pkg=" + packageName |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 179 | + " intent=" |
Dianne Hackborn | 90c52de | 2011-09-23 12:57:44 -0700 | [diff] [blame] | 180 | + (requestIntent != null |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 181 | ? requestIntent.toShortString(false, true, false, false) : "<null>") |
Amith Yamasani | 4ea6069 | 2012-08-28 14:34:53 -0700 | [diff] [blame] | 182 | + " flags=0x" + Integer.toHexString(flags) + " u=" + userId + "}"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | } |
Craig Mautner | 5f2bb4c | 2015-03-12 16:10:27 -0700 | [diff] [blame] | 184 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | String typeName() { |
| 186 | switch (type) { |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 187 | case ActivityManager.INTENT_SENDER_ACTIVITY: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | return "startActivity"; |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 189 | case ActivityManager.INTENT_SENDER_BROADCAST: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | return "broadcastIntent"; |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 191 | case ActivityManager.INTENT_SENDER_SERVICE: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | return "startService"; |
Christopher Tate | 08992ac | 2017-03-21 11:37:06 -0700 | [diff] [blame] | 193 | case ActivityManager.INTENT_SENDER_FOREGROUND_SERVICE: |
| 194 | return "startForegroundService"; |
Dianne Hackborn | a4972e9 | 2012-03-14 10:38:05 -0700 | [diff] [blame] | 195 | case ActivityManager.INTENT_SENDER_ACTIVITY_RESULT: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | return "activityResult"; |
| 197 | } |
| 198 | return Integer.toString(type); |
| 199 | } |
| 200 | } |
Craig Mautner | 5f2bb4c | 2015-03-12 16:10:27 -0700 | [diff] [blame] | 201 | |
Wale Ogunwale | ee6eca1 | 2018-09-19 20:37:53 -0700 | [diff] [blame] | 202 | PendingIntentRecord(PendingIntentController _controller, Key _k, int _u) { |
| 203 | controller = _controller; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | key = _k; |
| 205 | uid = _u; |
Wale Ogunwale | ee6eca1 | 2018-09-19 20:37:53 -0700 | [diff] [blame] | 206 | ref = new WeakReference<>(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | } |
| 208 | |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 209 | 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 Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 222 | this.stringName = null; |
| 223 | } |
| 224 | |
Michal Karpinski | ac116df | 2018-12-10 17:51:42 +0000 | [diff] [blame] | 225 | 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 Karpinski | c8aa91b | 2019-01-10 16:45:59 +0000 | [diff] [blame] | 233 | if ((flags & FLAG_SERVICE_SENDER) != 0) { |
| 234 | mAllowBgActivityStartsForServiceSender.add(token); |
| 235 | } |
Michal Karpinski | ac116df | 2018-12-10 17:51:42 +0000 | [diff] [blame] | 236 | } |
| 237 | |
Dianne Hackborn | f66adfd | 2017-04-13 11:01:48 -0700 | [diff] [blame] | 238 | 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 Onuki | 8a0319a | 2018-04-25 16:31:05 -0700 | [diff] [blame] | 246 | if (mCancelCallbacks == null) { |
| 247 | return; // Already unregistered or detached. |
| 248 | } |
Dianne Hackborn | f66adfd | 2017-04-13 11:01:48 -0700 | [diff] [blame] | 249 | 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 Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 261 | public void send(int code, Intent intent, String resolvedType, IBinder whitelistToken, |
Dianne Hackborn | 0c4e6a8 | 2016-05-13 17:37:08 -0700 | [diff] [blame] | 262 | IIntentReceiver finishedReceiver, String requiredPermission, Bundle options) { |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 263 | sendInner(code, intent, resolvedType, whitelistToken, finishedReceiver, |
Andrii Kulian | b1cdb10 | 2017-07-13 15:33:06 -0700 | [diff] [blame] | 264 | requiredPermission, null, null, 0, 0, 0, options); |
Dianne Hackborn | bcbcaa7 | 2009-09-10 10:54:46 -0700 | [diff] [blame] | 265 | } |
Craig Mautner | 5f2bb4c | 2015-03-12 16:10:27 -0700 | [diff] [blame] | 266 | |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 267 | 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 Kulian | b1cdb10 | 2017-07-13 15:33:06 -0700 | [diff] [blame] | 270 | requiredPermission, null, null, 0, 0, 0, options); |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 271 | } |
| 272 | |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 273 | public int sendInner(int code, Intent intent, String resolvedType, IBinder whitelistToken, |
Wale Ogunwale | ee6eca1 | 2018-09-19 20:37:53 -0700 | [diff] [blame] | 274 | IIntentReceiver finishedReceiver, String requiredPermission, IBinder resultTo, |
| 275 | String resultWho, int requestCode, int flagsMask, int flagsValues, Bundle options) { |
Jeff Sharkey | f0ec2e0 | 2016-03-21 12:37:54 -0600 | [diff] [blame] | 276 | if (intent != null) intent.setDefusable(true); |
| 277 | if (options != null) options.setDefusable(true); |
| 278 | |
Wale Ogunwale | ee6eca1 | 2018-09-19 20:37:53 -0700 | [diff] [blame] | 279 | 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 | } |
Svetoslav | b0a7839 | 2015-04-10 17:25:35 -0700 | [diff] [blame] | 288 | |
Wale Ogunwale | ee6eca1 | 2018-09-19 20:37:53 -0700 | [diff] [blame] | 289 | sent = true; |
| 290 | if ((key.flags & PendingIntent.FLAG_ONE_SHOT) != 0) { |
| 291 | controller.cancelIntentSender(this, true); |
| 292 | } |
Svetoslav | b0a7839 | 2015-04-10 17:25:35 -0700 | [diff] [blame] | 293 | |
Wale Ogunwale | ee6eca1 | 2018-09-19 20:37:53 -0700 | [diff] [blame] | 294 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | resolvedType = key.requestResolvedType; |
| 302 | } |
| 303 | } else { |
| 304 | resolvedType = key.requestResolvedType; |
| 305 | } |
Wale Ogunwale | ee6eca1 | 2018-09-19 20:37:53 -0700 | [diff] [blame] | 306 | flagsMask &= ~Intent.IMMUTABLE_FLAGS; |
| 307 | flagsValues &= flagsMask; |
| 308 | finalIntent.setFlags((finalIntent.getFlags() & ~flagsMask) | flagsValues); |
| 309 | } else { |
| 310 | resolvedType = key.requestResolvedType; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | } |
Wale Ogunwale | ee6eca1 | 2018-09-19 20:37:53 -0700 | [diff] [blame] | 312 | |
Winson Chung | d070775 | 2018-09-27 13:55:58 -0700 | [diff] [blame] | 313 | // 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 Ogunwale | ee6eca1 | 2018-09-19 20:37:53 -0700 | [diff] [blame] | 320 | // Extract options before clearing calling identity |
| 321 | mergedOptions = key.options; |
| 322 | if (mergedOptions == null) { |
Winson Chung | d070775 | 2018-09-27 13:55:58 -0700 | [diff] [blame] | 323 | mergedOptions = new SafeActivityOptions(opts); |
Wale Ogunwale | ee6eca1 | 2018-09-19 20:37:53 -0700 | [diff] [blame] | 324 | } else { |
Winson Chung | d070775 | 2018-09-27 13:55:58 -0700 | [diff] [blame] | 325 | mergedOptions.setCallerOptions(opts); |
Wale Ogunwale | ee6eca1 | 2018-09-19 20:37:53 -0700 | [diff] [blame] | 326 | } |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 347 | } |
Wale Ogunwale | ee6eca1 | 2018-09-19 20:37:53 -0700 | [diff] [blame] | 348 | // 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 Karpinski | cc88d7e | 2019-01-24 15:32:12 +0000 | [diff] [blame] | 382 | // 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 Ogunwale | ee6eca1 | 2018-09-19 20:37:53 -0700 | [diff] [blame] | 386 | |
Michal Karpinski | 9cbb20b | 2019-02-05 17:31:50 +0000 | [diff] [blame] | 387 | // 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 Ogunwale | ee6eca1 | 2018-09-19 20:37:53 -0700 | [diff] [blame] | 390 | 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 Karpinski | 84d9ebd | 2019-01-17 18:28:59 +0000 | [diff] [blame] | 399 | uid, callingPid, callingUid, key.packageName, allIntents, |
| 400 | allResolvedTypes, resultTo, mergedOptions, userId, |
| 401 | false /* validateIncomingUser */, |
Michal Karpinski | ac116df | 2018-12-10 17:51:42 +0000 | [diff] [blame] | 402 | this /* originatingPendingIntent */, |
| 403 | mAllowBgActivityStartsForActivitySender.contains(whitelistToken)); |
Wale Ogunwale | ee6eca1 | 2018-09-19 20:37:53 -0700 | [diff] [blame] | 404 | } 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 Karpinski | ac116df | 2018-12-10 17:51:42 +0000 | [diff] [blame] | 410 | this /* originatingPendingIntent */, |
| 411 | mAllowBgActivityStartsForActivitySender.contains(whitelistToken)); |
Wale Ogunwale | ee6eca1 | 2018-09-19 20:37:53 -0700 | [diff] [blame] | 412 | } |
| 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 Karpinski | c99d718 | 2019-02-17 13:15:23 +0000 | [diff] [blame] | 426 | uid, callingUid, callingPid, finalIntent, resolvedType, |
| 427 | finishedReceiver, code, null, null, requiredPermission, options, |
| 428 | (finishedReceiver != null), false, userId, |
Michal Karpinski | cc88d7e | 2019-01-24 15:32:12 +0000 | [diff] [blame] | 429 | mAllowBgActivityStartsForBroadcastSender.contains(whitelistToken) |
| 430 | || allowTrampoline); |
Wale Ogunwale | ee6eca1 | 2018-09-19 20:37:53 -0700 | [diff] [blame] | 431 | 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 Karpinski | c8aa91b | 2019-01-10 16:45:59 +0000 | [diff] [blame] | 443 | key.packageName, userId, |
Michal Karpinski | cc88d7e | 2019-01-24 15:32:12 +0000 | [diff] [blame] | 444 | mAllowBgActivityStartsForServiceSender.contains(whitelistToken) |
| 445 | || allowTrampoline); |
Wale Ogunwale | ee6eca1 | 2018-09-19 20:37:53 -0700 | [diff] [blame] | 446 | } 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 466 | } |
Craig Mautner | df88d73 | 2014-01-27 09:21:32 -0800 | [diff] [blame] | 467 | |
| 468 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 469 | protected void finalize() throws Throwable { |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 470 | try { |
| 471 | if (!canceled) { |
Wale Ogunwale | ee6eca1 | 2018-09-19 20:37:53 -0700 | [diff] [blame] | 472 | controller.mH.sendMessage(PooledLambda.obtainMessage( |
| 473 | PendingIntentRecord::completeFinalize, this)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 474 | } |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 475 | } finally { |
| 476 | super.finalize(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
Wale Ogunwale | ee6eca1 | 2018-09-19 20:37:53 -0700 | [diff] [blame] | 480 | private void completeFinalize() { |
| 481 | synchronized(controller.mLock) { |
| 482 | WeakReference<PendingIntentRecord> current = controller.mIntentSenderRecords.get(key); |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 483 | if (current == ref) { |
Wale Ogunwale | ee6eca1 | 2018-09-19 20:37:53 -0700 | [diff] [blame] | 484 | controller.mIntentSenderRecords.remove(key); |
Dianne Hackborn | 9e0f5d9 | 2010-02-22 15:05:42 -0800 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | } |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 488 | |
Wale Ogunwale | 5950709 | 2018-10-29 09:00:30 -0700 | [diff] [blame] | 489 | public void dump(PrintWriter pw, String prefix) { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 490 | 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 Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 502 | if (key.requestIntent != null) { |
| 503 | pw.print(prefix); pw.print("requestIntent="); |
Dianne Hackborn | 21c241e | 2012-03-08 13:57:23 -0800 | [diff] [blame] | 504 | pw.println(key.requestIntent.toShortString(false, true, true, true)); |
Dianne Hackborn | c3b91fd | 2010-02-23 17:25:30 -0800 | [diff] [blame] | 505 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 506 | if (sent || canceled) { |
| 507 | pw.print(prefix); pw.print("sent="); pw.print(sent); |
| 508 | pw.print(" canceled="); pw.println(canceled); |
| 509 | } |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 510 | if (whitelistDuration != null) { |
Dianne Hackborn | bc02a39 | 2016-06-02 17:15:08 -0700 | [diff] [blame] | 511 | pw.print(prefix); |
| 512 | pw.print("whitelistDuration="); |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 513 | 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 Hackborn | bc02a39 | 2016-06-02 17:15:08 -0700 | [diff] [blame] | 521 | pw.println(); |
| 522 | } |
Dianne Hackborn | f66adfd | 2017-04-13 11:01:48 -0700 | [diff] [blame] | 523 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 530 | } |
| 531 | |
| 532 | public String toString() { |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 533 | 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 Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 543 | if (whitelistDuration != null) { |
Dianne Hackborn | bc02a39 | 2016-06-02 17:15:08 -0700 | [diff] [blame] | 544 | sb.append( " (whitelist: "); |
Dianne Hackborn | 9830552 | 2017-05-05 17:53:53 -0700 | [diff] [blame] | 545 | 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 Hackborn | bc02a39 | 2016-06-02 17:15:08 -0700 | [diff] [blame] | 553 | sb.append(")"); |
Felipe Leme | a1b79bf | 2016-05-24 13:06:54 -0700 | [diff] [blame] | 554 | } |
Dianne Hackborn | 1d442e0 | 2009-04-20 18:14:05 -0700 | [diff] [blame] | 555 | sb.append('}'); |
| 556 | return stringName = sb.toString(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 557 | } |
| 558 | } |