Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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.pm; |
| 18 | |
Patrick Baumann | 9c4aa5c | 2018-02-28 14:08:29 -0800 | [diff] [blame] | 19 | import android.annotation.AnyThread; |
| 20 | import android.annotation.WorkerThread; |
Todd Kennedy | 1fb3404 | 2017-03-01 13:56:58 -0800 | [diff] [blame] | 21 | import android.app.IInstantAppResolver; |
| 22 | import android.app.InstantAppResolverService; |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 23 | import android.content.ComponentName; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.content.ServiceConnection; |
Todd Kennedy | 1fb3404 | 2017-03-01 13:56:58 -0800 | [diff] [blame] | 27 | import android.content.pm.InstantAppResolveInfo; |
Tony Mak | f264540 | 2017-05-12 16:11:43 +0100 | [diff] [blame] | 28 | import android.os.Binder; |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 29 | import android.os.Build; |
| 30 | import android.os.Bundle; |
Todd Kennedy | 01ad0c7 | 2016-11-11 15:33:12 -0800 | [diff] [blame] | 31 | import android.os.Handler; |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 32 | import android.os.IBinder; |
Todd Kennedy | 16c3a3e | 2017-04-03 12:03:35 -0700 | [diff] [blame] | 33 | import android.os.IBinder.DeathRecipient; |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 34 | import android.os.IRemoteCallback; |
| 35 | import android.os.RemoteException; |
| 36 | import android.os.SystemClock; |
| 37 | import android.os.UserHandle; |
Todd Kennedy | 7cf918e | 2017-04-10 15:10:56 -0700 | [diff] [blame] | 38 | import android.util.Slog; |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 39 | import android.util.TimedRemoteCaller; |
| 40 | |
Todd Kennedy | 46b4f2b | 2017-04-21 12:20:03 -0700 | [diff] [blame] | 41 | import com.android.internal.annotations.GuardedBy; |
Patrick Baumann | 9c4aa5c | 2018-02-28 14:08:29 -0800 | [diff] [blame] | 42 | import com.android.internal.os.BackgroundThread; |
Jeff Sharkey | 850c83e | 2016-11-09 12:25:44 -0700 | [diff] [blame] | 43 | |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 44 | import java.util.ArrayList; |
| 45 | import java.util.List; |
Todd Kennedy | 34f5b42 | 2017-05-03 16:38:44 -0700 | [diff] [blame] | 46 | import java.util.NoSuchElementException; |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 47 | import java.util.concurrent.TimeoutException; |
| 48 | |
| 49 | /** |
Patrick Baumann | 43c97a0 | 2018-01-31 20:09:03 +0000 | [diff] [blame] | 50 | * Represents a remote instant app resolver. It is responsible for binding to the remote |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 51 | * service and handling all interactions in a timely manner. |
| 52 | * @hide |
| 53 | */ |
Patrick Baumann | 43c97a0 | 2018-01-31 20:09:03 +0000 | [diff] [blame] | 54 | final class InstantAppResolverConnection implements DeathRecipient { |
Todd Kennedy | 7cf918e | 2017-04-10 15:10:56 -0700 | [diff] [blame] | 55 | private static final String TAG = "PackageManager"; |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 56 | // This is running in a critical section and the timeout must be sufficiently low |
| 57 | private static final long BIND_SERVICE_TIMEOUT_MS = |
Jeff Sharkey | 5ab0243 | 2017-06-27 11:01:36 -0600 | [diff] [blame] | 58 | Build.IS_ENG ? 500 : 300; |
Todd Kennedy | 46b4f2b | 2017-04-21 12:20:03 -0700 | [diff] [blame] | 59 | private static final long CALL_SERVICE_TIMEOUT_MS = |
Jeff Sharkey | 5ab0243 | 2017-06-27 11:01:36 -0600 | [diff] [blame] | 60 | Build.IS_ENG ? 200 : 100; |
Patrick Baumann | 43c97a0 | 2018-01-31 20:09:03 +0000 | [diff] [blame] | 61 | private static final boolean DEBUG_INSTANT = Build.IS_DEBUGGABLE; |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 62 | |
| 63 | private final Object mLock = new Object(); |
Patrick Baumann | 43c97a0 | 2018-01-31 20:09:03 +0000 | [diff] [blame] | 64 | private final GetInstantAppResolveInfoCaller mGetInstantAppResolveInfoCaller = |
| 65 | new GetInstantAppResolveInfoCaller(); |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 66 | private final ServiceConnection mServiceConnection = new MyServiceConnection(); |
| 67 | private final Context mContext; |
| 68 | /** Intent used to bind to the service */ |
| 69 | private final Intent mIntent; |
| 70 | |
Snild Dolkow | e2612eb | 2017-06-14 09:57:36 +0200 | [diff] [blame] | 71 | private static final int STATE_IDLE = 0; // no bind operation is ongoing |
| 72 | private static final int STATE_BINDING = 1; // someone is binding and waiting |
| 73 | private static final int STATE_PENDING = 2; // a bind is pending, but the caller is not waiting |
Patrick Baumann | 9c4aa5c | 2018-02-28 14:08:29 -0800 | [diff] [blame] | 74 | private final Handler mBgHandler; |
Snild Dolkow | e2612eb | 2017-06-14 09:57:36 +0200 | [diff] [blame] | 75 | |
Todd Kennedy | 46b4f2b | 2017-04-21 12:20:03 -0700 | [diff] [blame] | 76 | @GuardedBy("mLock") |
Snild Dolkow | e2612eb | 2017-06-14 09:57:36 +0200 | [diff] [blame] | 77 | private int mBindState = STATE_IDLE; |
Todd Kennedy | 46b4f2b | 2017-04-21 12:20:03 -0700 | [diff] [blame] | 78 | @GuardedBy("mLock") |
Todd Kennedy | 1fb3404 | 2017-03-01 13:56:58 -0800 | [diff] [blame] | 79 | private IInstantAppResolver mRemoteInstance; |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 80 | |
Patrick Baumann | 43c97a0 | 2018-01-31 20:09:03 +0000 | [diff] [blame] | 81 | public InstantAppResolverConnection( |
Todd Kennedy | 02a6b73 | 2017-04-05 14:24:58 -0700 | [diff] [blame] | 82 | Context context, ComponentName componentName, String action) { |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 83 | mContext = context; |
Todd Kennedy | 02a6b73 | 2017-04-05 14:24:58 -0700 | [diff] [blame] | 84 | mIntent = new Intent(action).setComponent(componentName); |
Patrick Baumann | 9c4aa5c | 2018-02-28 14:08:29 -0800 | [diff] [blame] | 85 | mBgHandler = BackgroundThread.getHandler(); |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 86 | } |
| 87 | |
Hai Zhang | bfa4f90 | 2018-08-21 11:33:33 -0700 | [diff] [blame] | 88 | public List<InstantAppResolveInfo> getInstantAppResolveInfoList(Intent sanitizedIntent, |
| 89 | int[] hashPrefix, int userId, String token) throws ConnectionException { |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 90 | throwIfCalledOnMainThread(); |
Todd Kennedy | 46b4f2b | 2017-04-21 12:20:03 -0700 | [diff] [blame] | 91 | IInstantAppResolver target = null; |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 92 | try { |
Todd Kennedy | bdf2a80 | 2017-05-08 16:09:42 -0700 | [diff] [blame] | 93 | try { |
| 94 | target = getRemoteInstanceLazy(token); |
| 95 | } catch (TimeoutException e) { |
| 96 | throw new ConnectionException(ConnectionException.FAILURE_BIND); |
| 97 | } catch (InterruptedException e) { |
| 98 | throw new ConnectionException(ConnectionException.FAILURE_INTERRUPTED); |
| 99 | } |
| 100 | try { |
Patrick Baumann | 43c97a0 | 2018-01-31 20:09:03 +0000 | [diff] [blame] | 101 | return mGetInstantAppResolveInfoCaller |
Hai Zhang | bfa4f90 | 2018-08-21 11:33:33 -0700 | [diff] [blame] | 102 | .getInstantAppResolveInfoList(target, sanitizedIntent, hashPrefix, userId, |
| 103 | token); |
Todd Kennedy | bdf2a80 | 2017-05-08 16:09:42 -0700 | [diff] [blame] | 104 | } catch (TimeoutException e) { |
Todd Kennedy | e6393c9 | 2017-05-16 15:47:01 -0700 | [diff] [blame] | 105 | throw new ConnectionException(ConnectionException.FAILURE_CALL); |
Todd Kennedy | bdf2a80 | 2017-05-08 16:09:42 -0700 | [diff] [blame] | 106 | } catch (RemoteException ignore) { |
Todd Kennedy | 46b4f2b | 2017-04-21 12:20:03 -0700 | [diff] [blame] | 107 | } |
Todd Kennedy | e5195dd | 2016-10-19 15:29:19 -0700 | [diff] [blame] | 108 | } finally { |
| 109 | synchronized (mLock) { |
| 110 | mLock.notifyAll(); |
| 111 | } |
| 112 | } |
| 113 | return null; |
| 114 | } |
| 115 | |
Hai Zhang | bfa4f90 | 2018-08-21 11:33:33 -0700 | [diff] [blame] | 116 | public void getInstantAppIntentFilterList(Intent sanitizedIntent, int[] hashPrefix, int userId, |
Patrick Baumann | 577d402 | 2018-01-31 16:55:10 +0000 | [diff] [blame] | 117 | String token, PhaseTwoCallback callback, Handler callbackHandler, final long startTime) |
| 118 | throws ConnectionException { |
Todd Kennedy | 01ad0c7 | 2016-11-11 15:33:12 -0800 | [diff] [blame] | 119 | final IRemoteCallback remoteCallback = new IRemoteCallback.Stub() { |
| 120 | @Override |
| 121 | public void sendResult(Bundle data) throws RemoteException { |
Todd Kennedy | 1fb3404 | 2017-03-01 13:56:58 -0800 | [diff] [blame] | 122 | final ArrayList<InstantAppResolveInfo> resolveList = |
| 123 | data.getParcelableArrayList( |
| 124 | InstantAppResolverService.EXTRA_RESOLVE_INFO); |
Patrick Baumann | 577d402 | 2018-01-31 16:55:10 +0000 | [diff] [blame] | 125 | callbackHandler.post(() -> callback.onPhaseTwoResolved(resolveList, startTime)); |
Todd Kennedy | 01ad0c7 | 2016-11-11 15:33:12 -0800 | [diff] [blame] | 126 | } |
| 127 | }; |
Todd Kennedy | e5195dd | 2016-10-19 15:29:19 -0700 | [diff] [blame] | 128 | try { |
Todd Kennedy | 46b4f2b | 2017-04-21 12:20:03 -0700 | [diff] [blame] | 129 | getRemoteInstanceLazy(token) |
Hai Zhang | bfa4f90 | 2018-08-21 11:33:33 -0700 | [diff] [blame] | 130 | .getInstantAppIntentFilterList(sanitizedIntent, hashPrefix, userId, token, |
Patrick Baumann | 577d402 | 2018-01-31 16:55:10 +0000 | [diff] [blame] | 131 | remoteCallback); |
Todd Kennedy | bdf2a80 | 2017-05-08 16:09:42 -0700 | [diff] [blame] | 132 | } catch (TimeoutException e) { |
| 133 | throw new ConnectionException(ConnectionException.FAILURE_BIND); |
| 134 | } catch (InterruptedException e) { |
| 135 | throw new ConnectionException(ConnectionException.FAILURE_INTERRUPTED); |
| 136 | } catch (RemoteException ignore) { |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 137 | } |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Patrick Baumann | 9c4aa5c | 2018-02-28 14:08:29 -0800 | [diff] [blame] | 140 | @WorkerThread |
Todd Kennedy | 46b4f2b | 2017-04-21 12:20:03 -0700 | [diff] [blame] | 141 | private IInstantAppResolver getRemoteInstanceLazy(String token) |
Todd Kennedy | bdf2a80 | 2017-05-08 16:09:42 -0700 | [diff] [blame] | 142 | throws ConnectionException, TimeoutException, InterruptedException { |
Snild Dolkow | e2612eb | 2017-06-14 09:57:36 +0200 | [diff] [blame] | 143 | long binderToken = Binder.clearCallingIdentity(); |
| 144 | try { |
| 145 | return bind(token); |
| 146 | } finally { |
| 147 | Binder.restoreCallingIdentity(binderToken); |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 151 | @GuardedBy("mLock") |
Todd Kennedy | bdf2a80 | 2017-05-08 16:09:42 -0700 | [diff] [blame] | 152 | private void waitForBindLocked(String token) throws TimeoutException, InterruptedException { |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 153 | final long startMillis = SystemClock.uptimeMillis(); |
Snild Dolkow | e2612eb | 2017-06-14 09:57:36 +0200 | [diff] [blame] | 154 | while (mBindState != STATE_IDLE) { |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 155 | if (mRemoteInstance != null) { |
| 156 | break; |
| 157 | } |
| 158 | final long elapsedMillis = SystemClock.uptimeMillis() - startMillis; |
| 159 | final long remainingMillis = BIND_SERVICE_TIMEOUT_MS - elapsedMillis; |
| 160 | if (remainingMillis <= 0) { |
Todd Kennedy | 46b4f2b | 2017-04-21 12:20:03 -0700 | [diff] [blame] | 161 | throw new TimeoutException("[" + token + "] Didn't bind to resolver in time!"); |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 162 | } |
Todd Kennedy | 46b4f2b | 2017-04-21 12:20:03 -0700 | [diff] [blame] | 163 | mLock.wait(remainingMillis); |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 164 | } |
Todd Kennedy | 46b4f2b | 2017-04-21 12:20:03 -0700 | [diff] [blame] | 165 | } |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 166 | |
Patrick Baumann | 9c4aa5c | 2018-02-28 14:08:29 -0800 | [diff] [blame] | 167 | @WorkerThread |
Snild Dolkow | e2612eb | 2017-06-14 09:57:36 +0200 | [diff] [blame] | 168 | private IInstantAppResolver bind(String token) |
Todd Kennedy | bdf2a80 | 2017-05-08 16:09:42 -0700 | [diff] [blame] | 169 | throws ConnectionException, TimeoutException, InterruptedException { |
Snild Dolkow | e2612eb | 2017-06-14 09:57:36 +0200 | [diff] [blame] | 170 | boolean doUnbind = false; |
| 171 | synchronized (mLock) { |
| 172 | if (mRemoteInstance != null) { |
| 173 | return mRemoteInstance; |
Todd Kennedy | 46b4f2b | 2017-04-21 12:20:03 -0700 | [diff] [blame] | 174 | } |
Snild Dolkow | e2612eb | 2017-06-14 09:57:36 +0200 | [diff] [blame] | 175 | |
| 176 | if (mBindState == STATE_PENDING) { |
| 177 | // there is a pending bind, let's see if we can use it. |
Patrick Baumann | 43c97a0 | 2018-01-31 20:09:03 +0000 | [diff] [blame] | 178 | if (DEBUG_INSTANT) { |
Snild Dolkow | e2612eb | 2017-06-14 09:57:36 +0200 | [diff] [blame] | 179 | Slog.i(TAG, "[" + token + "] Previous bind timed out; waiting for connection"); |
| 180 | } |
| 181 | try { |
| 182 | waitForBindLocked(token); |
| 183 | if (mRemoteInstance != null) { |
| 184 | return mRemoteInstance; |
| 185 | } |
| 186 | } catch (TimeoutException e) { |
| 187 | // nope, we might have to try a rebind. |
| 188 | doUnbind = true; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | if (mBindState == STATE_BINDING) { |
| 193 | // someone was binding when we called bind(), or they raced ahead while we were |
| 194 | // waiting in the PENDING case; wait for their result instead. Last chance! |
Patrick Baumann | 43c97a0 | 2018-01-31 20:09:03 +0000 | [diff] [blame] | 195 | if (DEBUG_INSTANT) { |
Snild Dolkow | e2612eb | 2017-06-14 09:57:36 +0200 | [diff] [blame] | 196 | Slog.i(TAG, "[" + token + "] Another thread is binding; waiting for connection"); |
| 197 | } |
| 198 | waitForBindLocked(token); |
| 199 | // if the other thread's bindService() returned false, we could still have null. |
| 200 | if (mRemoteInstance != null) { |
| 201 | return mRemoteInstance; |
| 202 | } |
| 203 | throw new ConnectionException(ConnectionException.FAILURE_BIND); |
| 204 | } |
| 205 | mBindState = STATE_BINDING; // our time to shine! :) |
Todd Kennedy | 46b4f2b | 2017-04-21 12:20:03 -0700 | [diff] [blame] | 206 | } |
Snild Dolkow | e2612eb | 2017-06-14 09:57:36 +0200 | [diff] [blame] | 207 | |
| 208 | // only one thread can be here at a time (the one that set STATE_BINDING) |
Todd Kennedy | 46b4f2b | 2017-04-21 12:20:03 -0700 | [diff] [blame] | 209 | boolean wasBound = false; |
Snild Dolkow | e2612eb | 2017-06-14 09:57:36 +0200 | [diff] [blame] | 210 | IInstantAppResolver instance = null; |
Todd Kennedy | 46b4f2b | 2017-04-21 12:20:03 -0700 | [diff] [blame] | 211 | try { |
Snild Dolkow | e2612eb | 2017-06-14 09:57:36 +0200 | [diff] [blame] | 212 | if (doUnbind) { |
Patrick Baumann | 43c97a0 | 2018-01-31 20:09:03 +0000 | [diff] [blame] | 213 | if (DEBUG_INSTANT) { |
Snild Dolkow | e2612eb | 2017-06-14 09:57:36 +0200 | [diff] [blame] | 214 | Slog.i(TAG, "[" + token + "] Previous connection never established; rebinding"); |
| 215 | } |
| 216 | mContext.unbindService(mServiceConnection); |
| 217 | } |
Patrick Baumann | 43c97a0 | 2018-01-31 20:09:03 +0000 | [diff] [blame] | 218 | if (DEBUG_INSTANT) { |
Snild Dolkow | e2612eb | 2017-06-14 09:57:36 +0200 | [diff] [blame] | 219 | Slog.v(TAG, "[" + token + "] Binding to instant app resolver"); |
| 220 | } |
Todd Kennedy | 46b4f2b | 2017-04-21 12:20:03 -0700 | [diff] [blame] | 221 | final int flags = Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE; |
| 222 | wasBound = mContext |
| 223 | .bindServiceAsUser(mIntent, mServiceConnection, flags, UserHandle.SYSTEM); |
| 224 | if (wasBound) { |
Snild Dolkow | e2612eb | 2017-06-14 09:57:36 +0200 | [diff] [blame] | 225 | synchronized (mLock) { |
| 226 | waitForBindLocked(token); |
| 227 | instance = mRemoteInstance; |
| 228 | return instance; |
| 229 | } |
Todd Kennedy | 46b4f2b | 2017-04-21 12:20:03 -0700 | [diff] [blame] | 230 | } else { |
| 231 | Slog.w(TAG, "[" + token + "] Failed to bind to: " + mIntent); |
Todd Kennedy | bdf2a80 | 2017-05-08 16:09:42 -0700 | [diff] [blame] | 232 | throw new ConnectionException(ConnectionException.FAILURE_BIND); |
Todd Kennedy | 46b4f2b | 2017-04-21 12:20:03 -0700 | [diff] [blame] | 233 | } |
| 234 | } finally { |
Snild Dolkow | e2612eb | 2017-06-14 09:57:36 +0200 | [diff] [blame] | 235 | synchronized (mLock) { |
| 236 | if (wasBound && instance == null) { |
| 237 | mBindState = STATE_PENDING; |
| 238 | } else { |
| 239 | mBindState = STATE_IDLE; |
| 240 | } |
| 241 | mLock.notifyAll(); |
| 242 | } |
Todd Kennedy | 46b4f2b | 2017-04-21 12:20:03 -0700 | [diff] [blame] | 243 | } |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 244 | } |
| 245 | |
| 246 | private void throwIfCalledOnMainThread() { |
| 247 | if (Thread.currentThread() == mContext.getMainLooper().getThread()) { |
| 248 | throw new RuntimeException("Cannot invoke on the main thread"); |
| 249 | } |
| 250 | } |
| 251 | |
Patrick Baumann | 9c4aa5c | 2018-02-28 14:08:29 -0800 | [diff] [blame] | 252 | @AnyThread |
| 253 | void optimisticBind() { |
| 254 | mBgHandler.post(() -> { |
| 255 | try { |
| 256 | if (bind("Optimistic Bind") != null && DEBUG_INSTANT) { |
| 257 | Slog.i(TAG, "Optimistic bind succeeded."); |
| 258 | } |
| 259 | } catch (ConnectionException | TimeoutException | InterruptedException e) { |
| 260 | Slog.e(TAG, "Optimistic bind failed.", e); |
| 261 | } |
| 262 | }); |
| 263 | } |
| 264 | |
Todd Kennedy | 16c3a3e | 2017-04-03 12:03:35 -0700 | [diff] [blame] | 265 | @Override |
| 266 | public void binderDied() { |
Patrick Baumann | 43c97a0 | 2018-01-31 20:09:03 +0000 | [diff] [blame] | 267 | if (DEBUG_INSTANT) { |
Todd Kennedy | 46b4f2b | 2017-04-21 12:20:03 -0700 | [diff] [blame] | 268 | Slog.d(TAG, "Binder to instant app resolver died"); |
Todd Kennedy | 7cf918e | 2017-04-10 15:10:56 -0700 | [diff] [blame] | 269 | } |
Todd Kennedy | 46b4f2b | 2017-04-21 12:20:03 -0700 | [diff] [blame] | 270 | synchronized (mLock) { |
| 271 | handleBinderDiedLocked(); |
| 272 | } |
Patrick Baumann | 9c4aa5c | 2018-02-28 14:08:29 -0800 | [diff] [blame] | 273 | optimisticBind(); |
Todd Kennedy | 46b4f2b | 2017-04-21 12:20:03 -0700 | [diff] [blame] | 274 | } |
| 275 | |
Andreas Gampe | a36dc62 | 2018-02-05 17:19:22 -0800 | [diff] [blame] | 276 | @GuardedBy("mLock") |
Todd Kennedy | 46b4f2b | 2017-04-21 12:20:03 -0700 | [diff] [blame] | 277 | private void handleBinderDiedLocked() { |
Todd Kennedy | 16c3a3e | 2017-04-03 12:03:35 -0700 | [diff] [blame] | 278 | if (mRemoteInstance != null) { |
Todd Kennedy | 34f5b42 | 2017-05-03 16:38:44 -0700 | [diff] [blame] | 279 | try { |
| 280 | mRemoteInstance.asBinder().unlinkToDeath(this, 0 /*flags*/); |
| 281 | } catch (NoSuchElementException ignore) { } |
Todd Kennedy | 16c3a3e | 2017-04-03 12:03:35 -0700 | [diff] [blame] | 282 | } |
| 283 | mRemoteInstance = null; |
Todd Kennedy | 16c3a3e | 2017-04-03 12:03:35 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Todd Kennedy | 01ad0c7 | 2016-11-11 15:33:12 -0800 | [diff] [blame] | 286 | /** |
| 287 | * Asynchronous callback when results come back from ephemeral resolution phase two. |
| 288 | */ |
| 289 | public abstract static class PhaseTwoCallback { |
Todd Kennedy | 1fb3404 | 2017-03-01 13:56:58 -0800 | [diff] [blame] | 290 | abstract void onPhaseTwoResolved( |
Todd Kennedy | 50d946c1 | 2017-03-17 13:55:38 -0700 | [diff] [blame] | 291 | List<InstantAppResolveInfo> instantAppResolveInfoList, long startTime); |
Todd Kennedy | 01ad0c7 | 2016-11-11 15:33:12 -0800 | [diff] [blame] | 292 | } |
| 293 | |
Todd Kennedy | bdf2a80 | 2017-05-08 16:09:42 -0700 | [diff] [blame] | 294 | public static class ConnectionException extends Exception { |
| 295 | public static final int FAILURE_BIND = 1; |
| 296 | public static final int FAILURE_CALL = 2; |
| 297 | public static final int FAILURE_INTERRUPTED = 3; |
| 298 | |
| 299 | public final int failure; |
| 300 | public ConnectionException(int _failure) { |
| 301 | failure = _failure; |
| 302 | } |
| 303 | } |
| 304 | |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 305 | private final class MyServiceConnection implements ServiceConnection { |
| 306 | @Override |
| 307 | public void onServiceConnected(ComponentName name, IBinder service) { |
Patrick Baumann | 43c97a0 | 2018-01-31 20:09:03 +0000 | [diff] [blame] | 308 | if (DEBUG_INSTANT) { |
Todd Kennedy | 46b4f2b | 2017-04-21 12:20:03 -0700 | [diff] [blame] | 309 | Slog.d(TAG, "Connected to instant app resolver"); |
Todd Kennedy | 7cf918e | 2017-04-10 15:10:56 -0700 | [diff] [blame] | 310 | } |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 311 | synchronized (mLock) { |
Todd Kennedy | 46b4f2b | 2017-04-21 12:20:03 -0700 | [diff] [blame] | 312 | mRemoteInstance = IInstantAppResolver.Stub.asInterface(service); |
Snild Dolkow | e2612eb | 2017-06-14 09:57:36 +0200 | [diff] [blame] | 313 | if (mBindState == STATE_PENDING) { |
| 314 | mBindState = STATE_IDLE; |
| 315 | } |
Todd Kennedy | 16c3a3e | 2017-04-03 12:03:35 -0700 | [diff] [blame] | 316 | try { |
Patrick Baumann | 43c97a0 | 2018-01-31 20:09:03 +0000 | [diff] [blame] | 317 | service.linkToDeath(InstantAppResolverConnection.this, 0 /*flags*/); |
Todd Kennedy | 16c3a3e | 2017-04-03 12:03:35 -0700 | [diff] [blame] | 318 | } catch (RemoteException e) { |
Todd Kennedy | 46b4f2b | 2017-04-21 12:20:03 -0700 | [diff] [blame] | 319 | handleBinderDiedLocked(); |
Todd Kennedy | 16c3a3e | 2017-04-03 12:03:35 -0700 | [diff] [blame] | 320 | } |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 321 | mLock.notifyAll(); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | @Override |
| 326 | public void onServiceDisconnected(ComponentName name) { |
Patrick Baumann | 43c97a0 | 2018-01-31 20:09:03 +0000 | [diff] [blame] | 327 | if (DEBUG_INSTANT) { |
Todd Kennedy | 46b4f2b | 2017-04-21 12:20:03 -0700 | [diff] [blame] | 328 | Slog.d(TAG, "Disconnected from instant app resolver"); |
Todd Kennedy | 7cf918e | 2017-04-10 15:10:56 -0700 | [diff] [blame] | 329 | } |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 330 | synchronized (mLock) { |
Todd Kennedy | 46b4f2b | 2017-04-21 12:20:03 -0700 | [diff] [blame] | 331 | handleBinderDiedLocked(); |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
Patrick Baumann | 43c97a0 | 2018-01-31 20:09:03 +0000 | [diff] [blame] | 336 | private static final class GetInstantAppResolveInfoCaller |
Todd Kennedy | 1fb3404 | 2017-03-01 13:56:58 -0800 | [diff] [blame] | 337 | extends TimedRemoteCaller<List<InstantAppResolveInfo>> { |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 338 | private final IRemoteCallback mCallback; |
| 339 | |
Patrick Baumann | 43c97a0 | 2018-01-31 20:09:03 +0000 | [diff] [blame] | 340 | public GetInstantAppResolveInfoCaller() { |
Todd Kennedy | 46b4f2b | 2017-04-21 12:20:03 -0700 | [diff] [blame] | 341 | super(CALL_SERVICE_TIMEOUT_MS); |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 342 | mCallback = new IRemoteCallback.Stub() { |
| 343 | @Override |
| 344 | public void sendResult(Bundle data) throws RemoteException { |
Todd Kennedy | 1fb3404 | 2017-03-01 13:56:58 -0800 | [diff] [blame] | 345 | final ArrayList<InstantAppResolveInfo> resolveList = |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 346 | data.getParcelableArrayList( |
Todd Kennedy | 1fb3404 | 2017-03-01 13:56:58 -0800 | [diff] [blame] | 347 | InstantAppResolverService.EXTRA_RESOLVE_INFO); |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 348 | int sequence = |
Todd Kennedy | 1fb3404 | 2017-03-01 13:56:58 -0800 | [diff] [blame] | 349 | data.getInt(InstantAppResolverService.EXTRA_SEQUENCE, -1); |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 350 | onRemoteMethodResult(resolveList, sequence); |
| 351 | } |
| 352 | }; |
| 353 | } |
| 354 | |
Patrick Baumann | 43c97a0 | 2018-01-31 20:09:03 +0000 | [diff] [blame] | 355 | public List<InstantAppResolveInfo> getInstantAppResolveInfoList( |
Hai Zhang | bfa4f90 | 2018-08-21 11:33:33 -0700 | [diff] [blame] | 356 | IInstantAppResolver target, Intent sanitizedIntent, int[] hashPrefix, int userId, |
| 357 | String token) throws RemoteException, TimeoutException { |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 358 | final int sequence = onBeforeRemoteCall(); |
Hai Zhang | bfa4f90 | 2018-08-21 11:33:33 -0700 | [diff] [blame] | 359 | target.getInstantAppResolveInfoList(sanitizedIntent, hashPrefix, userId, token, |
| 360 | sequence, mCallback); |
Todd Kennedy | e5195dd | 2016-10-19 15:29:19 -0700 | [diff] [blame] | 361 | return getResultTimed(sequence); |
| 362 | } |
| 363 | } |
Todd Kennedy | b8a279e | 2015-11-18 09:59:47 -0800 | [diff] [blame] | 364 | } |