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