package com.google.firebase.firestore.remote; import android.content.Context; import com.google.android.gms.common.GooglePlayServicesNotAvailableException; import com.google.android.gms.common.GooglePlayServicesRepairableException; import com.google.android.gms.security.ProviderInstaller; import com.google.android.gms.tasks.Continuation; import com.google.android.gms.tasks.Task; import com.google.android.gms.tasks.Tasks; import com.google.firebase.firestore.core.DatabaseInfo; import com.google.firebase.firestore.util.AsyncQueue; import com.google.firebase.firestore.util.Executors; import com.google.firebase.firestore.util.Logger; import com.google.firebase.firestore.util.Supplier; import io.grpc.CallCredentials; import io.grpc.CallOptions; import io.grpc.ClientCall; import io.grpc.ConnectivityState; import io.grpc.ManagedChannel; import io.grpc.ManagedChannelBuilder; import io.grpc.MethodDescriptor; import io.grpc.android.AndroidChannelBuilder; import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; import o.GMq; /* loaded from: classes2.dex */ public class GrpcCallProvider { private static Supplier> overrideChannelBuilderSupplier; private final AsyncQueue asyncQueue; private CallOptions callOptions; private Task channelTask; private AsyncQueue.DelayedTask connectivityAttemptTimer; private final Context context; private final DatabaseInfo databaseInfo; private final CallCredentials firestoreHeaders; /* JADX INFO: Access modifiers changed from: package-private */ public GrpcCallProvider(AsyncQueue asyncQueue, Context context, DatabaseInfo databaseInfo, CallCredentials callCredentials) { this.asyncQueue = asyncQueue; this.context = context; this.databaseInfo = databaseInfo; this.firestoreHeaders = callCredentials; initChannelTask(); } private ManagedChannel initChannel(Context context, DatabaseInfo databaseInfo) { ManagedChannelBuilder managedChannelBuilder; try { ProviderInstaller.installIfNeeded(context); } catch (GooglePlayServicesNotAvailableException | GooglePlayServicesRepairableException | IllegalStateException e) { Logger.warn("GrpcCallProvider", "Failed to update ssl context: %s", e); } Supplier> supplier = overrideChannelBuilderSupplier; if (supplier != null) { managedChannelBuilder = supplier.get(); } else { ManagedChannelBuilder forTarget = ManagedChannelBuilder.forTarget(databaseInfo.getHost()); if (!databaseInfo.isSslEnabled()) { forTarget.usePlaintext(); } managedChannelBuilder = forTarget; } managedChannelBuilder.keepAliveTime(30L, TimeUnit.SECONDS); return AndroidChannelBuilder.usingBuilder(managedChannelBuilder).context(context).build(); } /* JADX INFO: Access modifiers changed from: package-private */ public Task> createClientCall(final MethodDescriptor methodDescriptor) { return (Task>) this.channelTask.continueWithTask(this.asyncQueue.getExecutor(), new Continuation(this, methodDescriptor) { // from class: com.google.firebase.firestore.remote.GrpcCallProvider$$ExternalSyntheticLambda6 public final GrpcCallProvider f$0; public final MethodDescriptor f$1; @Override // com.google.android.gms.tasks.Continuation public final Object then(Task task) { return this.f$0.m245x1673c39e(this.f$1, task); } { this.f$0 = this; this.f$1 = methodDescriptor; } }); } /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: lambda$createClientCall$0$com-google-firebase-firestore-remote-GrpcCallProvider, reason: not valid java name */ public /* synthetic */ Task m245x1673c39e(MethodDescriptor methodDescriptor, Task task) throws Exception { return Tasks.forResult(((ManagedChannel) task.getResult()).newCall(methodDescriptor, this.callOptions)); } /* JADX INFO: Access modifiers changed from: private */ /* renamed from: onConnectivityStateChange, reason: merged with bridge method [inline-methods] and merged with bridge method [inline-methods] */ public void m249x2cfc29ec(final ManagedChannel managedChannel) { ConnectivityState state = managedChannel.getState(true); Logger.debug("GrpcCallProvider", "Current gRPC connectivity state: ".concat(String.valueOf(state)), new Object[0]); clearConnectivityAttemptTimer(); if (state == ConnectivityState.CONNECTING) { Logger.debug("GrpcCallProvider", "Setting the connectivityAttemptTimer", new Object[0]); this.connectivityAttemptTimer = this.asyncQueue.enqueueAfterDelay(AsyncQueue.TimerId.CONNECTIVITY_ATTEMPT_TIMER, 15000L, new Runnable(this, managedChannel) { // from class: com.google.firebase.firestore.remote.GrpcCallProvider$$ExternalSyntheticLambda3 public final GrpcCallProvider f$0; public final ManagedChannel f$1; @Override // java.lang.Runnable public final void run() { this.f$0.m248x3a7d4ab(this.f$1); } { this.f$0 = this; this.f$1 = managedChannel; } }); } managedChannel.notifyWhenStateChanged(state, new Runnable(this, managedChannel) { // from class: com.google.firebase.firestore.remote.GrpcCallProvider$$ExternalSyntheticLambda4 public final GrpcCallProvider f$0; public final ManagedChannel f$1; @Override // java.lang.Runnable public final void run() { this.f$0.m250x56507f2d(this.f$1); } { this.f$0 = this; this.f$1 = managedChannel; } }); } /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: lambda$onConnectivityStateChange$1$com-google-firebase-firestore-remote-GrpcCallProvider, reason: not valid java name */ public /* synthetic */ void m248x3a7d4ab(ManagedChannel managedChannel) { Logger.debug("GrpcCallProvider", "connectivityAttemptTimer elapsed. Resetting the channel.", new Object[0]); clearConnectivityAttemptTimer(); resetChannel(managedChannel); } /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: lambda$onConnectivityStateChange$3$com-google-firebase-firestore-remote-GrpcCallProvider, reason: not valid java name */ public /* synthetic */ void m250x56507f2d(final ManagedChannel managedChannel) { this.asyncQueue.enqueueAndForget(new Runnable(this, managedChannel) { // from class: com.google.firebase.firestore.remote.GrpcCallProvider$$ExternalSyntheticLambda5 public final GrpcCallProvider f$0; public final ManagedChannel f$1; @Override // java.lang.Runnable public final void run() { this.f$0.m249x2cfc29ec(this.f$1); } { this.f$0 = this; this.f$1 = managedChannel; } }); } private void resetChannel(final ManagedChannel managedChannel) { this.asyncQueue.enqueueAndForget(new Runnable(this, managedChannel) { // from class: com.google.firebase.firestore.remote.GrpcCallProvider$$ExternalSyntheticLambda0 public final GrpcCallProvider f$0; public final ManagedChannel f$1; @Override // java.lang.Runnable public final void run() { this.f$0.m251xfb8fbff1(this.f$1); } { this.f$0 = this; this.f$1 = managedChannel; } }); } /* JADX INFO: Access modifiers changed from: package-private */ /* renamed from: lambda$resetChannel$4$com-google-firebase-firestore-remote-GrpcCallProvider, reason: not valid java name */ public /* synthetic */ void m251xfb8fbff1(ManagedChannel managedChannel) { managedChannel.shutdownNow(); initChannelTask(); } private void initChannelTask() { this.channelTask = Tasks.call(Executors.BACKGROUND_EXECUTOR, new Callable(this) { // from class: com.google.firebase.firestore.remote.GrpcCallProvider$$ExternalSyntheticLambda1 public final GrpcCallProvider f$0; @Override // java.util.concurrent.Callable public final Object call() { return this.f$0.m247xa80a811d(); } { this.f$0 = this; } }); } /* JADX INFO: Access modifiers changed from: package-private */ /* JADX WARN: Multi-variable type inference failed */ /* renamed from: lambda$initChannelTask$6$com-google-firebase-firestore-remote-GrpcCallProvider, reason: not valid java name */ public /* synthetic */ ManagedChannel m247xa80a811d() throws Exception { final ManagedChannel initChannel = initChannel(this.context, this.databaseInfo); this.asyncQueue.enqueueAndForget(new Runnable(this, initChannel) { // from class: com.google.firebase.firestore.remote.GrpcCallProvider$$ExternalSyntheticLambda2 public final GrpcCallProvider f$0; public final ManagedChannel f$1; @Override // java.lang.Runnable public final void run() { this.f$0.m246x7eb62bdc(this.f$1); } { this.f$0 = this; this.f$1 = initChannel; } }); this.callOptions = ((GMq.RVV) ((GMq.RVV) GMq.e(initChannel).withCallCredentials(this.firestoreHeaders)).withExecutor(this.asyncQueue.getExecutor())).getCallOptions(); Logger.debug("GrpcCallProvider", "Channel successfully reset.", new Object[0]); return initChannel; } private void clearConnectivityAttemptTimer() { if (this.connectivityAttemptTimer != null) { Logger.debug("GrpcCallProvider", "Clearing the connectivityAttemptTimer", new Object[0]); this.connectivityAttemptTimer.cancel(); this.connectivityAttemptTimer = null; } } }