package com.google.firebase.remoteconfig.internal; import androidx.credentials.CredentialManager$$ExternalSyntheticLambda0; import com.google.android.gms.tasks.OnCanceledListener; import com.google.android.gms.tasks.OnFailureListener; import com.google.android.gms.tasks.OnSuccessListener; import com.google.android.gms.tasks.SuccessContinuation; import com.google.android.gms.tasks.Task; import com.google.android.gms.tasks.Tasks; import java.util.HashMap; import java.util.Map; import java.util.Objects; import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; /* loaded from: classes3.dex */ public class ConfigCacheClient { public Task cachedContainerTask = null; public final Executor executor; public final ConfigStorageClient storageClient; public static final Map clientInstances = new HashMap(); public static final Executor DIRECT_EXECUTOR = new CredentialManager$$ExternalSyntheticLambda0(); public ConfigCacheClient(Executor executor, ConfigStorageClient configStorageClient) { this.executor = executor; this.storageClient = configStorageClient; } public ConfigContainer getBlocking() { return getBlocking(5L); } public ConfigContainer getBlocking(long j) { synchronized (this) { try { Task task = this.cachedContainerTask; if (task != null && task.isSuccessful()) { return (ConfigContainer) this.cachedContainerTask.getResult(); } try { return (ConfigContainer) await(get(), j, TimeUnit.SECONDS); } catch (InterruptedException | ExecutionException | TimeoutException unused) { return null; } } catch (Throwable th) { throw th; } } } public Task put(ConfigContainer configContainer) { return put(configContainer, true); } public final /* synthetic */ Void lambda$put$0(ConfigContainer configContainer) { return this.storageClient.write(configContainer); } public Task put(final ConfigContainer configContainer, final boolean z) { return Tasks.call(this.executor, new Callable() { // from class: com.google.firebase.remoteconfig.internal.ConfigCacheClient$$ExternalSyntheticLambda1 @Override // java.util.concurrent.Callable public final Object call() { Void lambda$put$0; lambda$put$0 = ConfigCacheClient.this.lambda$put$0(configContainer); return lambda$put$0; } }).onSuccessTask(this.executor, new SuccessContinuation() { // from class: com.google.firebase.remoteconfig.internal.ConfigCacheClient$$ExternalSyntheticLambda2 @Override // com.google.android.gms.tasks.SuccessContinuation public final Task then(Object obj) { Task lambda$put$1; lambda$put$1 = ConfigCacheClient.this.lambda$put$1(z, configContainer, (Void) obj); return lambda$put$1; } }); } public final /* synthetic */ Task lambda$put$1(boolean z, ConfigContainer configContainer, Void r3) { if (z) { updateInMemoryConfigContainer(configContainer); } return Tasks.forResult(configContainer); } public synchronized Task get() { try { Task task = this.cachedContainerTask; if (task != null) { if (task.isComplete() && !this.cachedContainerTask.isSuccessful()) { } } Executor executor = this.executor; final ConfigStorageClient configStorageClient = this.storageClient; Objects.requireNonNull(configStorageClient); this.cachedContainerTask = Tasks.call(executor, new Callable() { // from class: com.google.firebase.remoteconfig.internal.ConfigCacheClient$$ExternalSyntheticLambda0 @Override // java.util.concurrent.Callable public final Object call() { return ConfigStorageClient.this.read(); } }); } catch (Throwable th) { throw th; } return this.cachedContainerTask; } public void clear() { synchronized (this) { this.cachedContainerTask = Tasks.forResult(null); } this.storageClient.clear(); } public final synchronized void updateInMemoryConfigContainer(ConfigContainer configContainer) { this.cachedContainerTask = Tasks.forResult(configContainer); } public static synchronized ConfigCacheClient getInstance(Executor executor, ConfigStorageClient configStorageClient) { ConfigCacheClient configCacheClient; synchronized (ConfigCacheClient.class) { try { String fileName = configStorageClient.getFileName(); Map map = clientInstances; if (!map.containsKey(fileName)) { map.put(fileName, new ConfigCacheClient(executor, configStorageClient)); } configCacheClient = (ConfigCacheClient) map.get(fileName); } catch (Throwable th) { throw th; } } return configCacheClient; } public static Object await(Task task, long j, TimeUnit timeUnit) { AwaitListener awaitListener = new AwaitListener(); Executor executor = DIRECT_EXECUTOR; task.addOnSuccessListener(executor, awaitListener); task.addOnFailureListener(executor, awaitListener); task.addOnCanceledListener(executor, awaitListener); if (!awaitListener.await(j, timeUnit)) { throw new TimeoutException("Task await timed out."); } if (task.isSuccessful()) { return task.getResult(); } throw new ExecutionException(task.getException()); } public static class AwaitListener implements OnSuccessListener, OnFailureListener, OnCanceledListener { public final CountDownLatch latch; public AwaitListener() { this.latch = new CountDownLatch(1); } @Override // com.google.android.gms.tasks.OnSuccessListener public void onSuccess(Object obj) { this.latch.countDown(); } @Override // com.google.android.gms.tasks.OnFailureListener public void onFailure(Exception exc) { this.latch.countDown(); } @Override // com.google.android.gms.tasks.OnCanceledListener public void onCanceled() { this.latch.countDown(); } public boolean await(long j, TimeUnit timeUnit) { return this.latch.await(j, timeUnit); } } }