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