package androidx.browser.trusted; import android.content.ComponentName; import android.content.ServiceConnection; import android.os.IBinder; import android.support.customtabs.trusted.ITrustedWebActivityService; import androidx.annotation.MainThread; import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.concurrent.futures.CallbackToFutureAdapter; import com.google.common.util.concurrent.ListenableFuture; import java.util.ArrayList; import java.util.Iterator; import java.util.List; /* loaded from: classes.dex */ class ConnectionHolder implements ServiceConnection { private static final int STATE_AWAITING_CONNECTION = 0; private static final int STATE_CANCELLED = 3; private static final int STATE_CONNECTED = 1; private static final int STATE_DISCONNECTED = 2; @Nullable private Exception mCancellationException; @NonNull private final Runnable mCloseRunnable; @NonNull private List> mCompleters; @Nullable private TrustedWebActivityServiceConnection mService; private int mState; @NonNull private final WrapperFactory mWrapperFactory; public static class WrapperFactory { @NonNull public TrustedWebActivityServiceConnection create(ComponentName componentName, IBinder iBinder) { return new TrustedWebActivityServiceConnection(ITrustedWebActivityService.Stub.asInterface(iBinder), componentName); } } @MainThread public ConnectionHolder(@NonNull Runnable runnable) { this(runnable, new WrapperFactory()); } @MainThread public ConnectionHolder(@NonNull Runnable runnable, @NonNull WrapperFactory wrapperFactory) { this.mState = 0; this.mCompleters = new ArrayList(); this.mCloseRunnable = runnable; this.mWrapperFactory = wrapperFactory; } @Override // android.content.ServiceConnection @MainThread public void onServiceConnected(ComponentName componentName, IBinder iBinder) { this.mService = this.mWrapperFactory.create(componentName, iBinder); Iterator> it = this.mCompleters.iterator(); while (it.hasNext()) { it.next().set(this.mService); } this.mCompleters.clear(); this.mState = 1; } @Override // android.content.ServiceConnection @MainThread public void onServiceDisconnected(ComponentName componentName) { this.mService = null; this.mCloseRunnable.run(); this.mState = 2; } @MainThread public void cancel(@NonNull Exception exc) { Iterator> it = this.mCompleters.iterator(); while (it.hasNext()) { it.next().setException(exc); } this.mCompleters.clear(); this.mCloseRunnable.run(); this.mState = 3; this.mCancellationException = exc; } @NonNull @MainThread public ListenableFuture getServiceWrapper() { return CallbackToFutureAdapter.getFuture(new CallbackToFutureAdapter.Resolver() { // from class: androidx.browser.trusted.ConnectionHolder$$ExternalSyntheticLambda0 @Override // androidx.concurrent.futures.CallbackToFutureAdapter.Resolver public final Object attachCompleter(CallbackToFutureAdapter.Completer completer) { Object lambda$getServiceWrapper$0; lambda$getServiceWrapper$0 = ConnectionHolder.this.lambda$getServiceWrapper$0(completer); return lambda$getServiceWrapper$0; } }); } /* JADX INFO: Access modifiers changed from: private */ public /* synthetic */ Object lambda$getServiceWrapper$0(CallbackToFutureAdapter.Completer completer) throws Exception { int i = this.mState; if (i == 0) { this.mCompleters.add(completer); } else { if (i != 1) { if (i == 2) { throw new IllegalStateException("Service has been disconnected."); } if (i == 3) { throw this.mCancellationException; } throw new IllegalStateException("Connection state is invalid"); } TrustedWebActivityServiceConnection trustedWebActivityServiceConnection = this.mService; if (trustedWebActivityServiceConnection == null) { throw new IllegalStateException("ConnectionHolder state is incorrect."); } completer.set(trustedWebActivityServiceConnection); } return "ConnectionHolder, state = " + this.mState; } }