what-the-bank/sources/com/google/firebase/remoteconfig/internal/ConfigCacheClient.java

154 lines
5.8 KiB
Java
Raw Permalink Normal View History

2024-07-27 18:17:47 +07:00
package com.google.firebase.remoteconfig.internal;
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.Task;
import com.google.android.gms.tasks.Tasks;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/* loaded from: classes.dex */
public class ConfigCacheClient {
private static final Executor DIRECT_EXECUTOR;
private static final Map<String, ConfigCacheClient> clientInstances = new HashMap();
private Task<ConfigContainer> cachedContainerTask = null;
private final ExecutorService executorService;
private final ConfigStorageClient storageClient;
static {
Executor executor;
executor = ConfigCacheClient$$Lambda$4.instance;
DIRECT_EXECUTOR = executor;
}
private ConfigCacheClient(ExecutorService executorService, ConfigStorageClient configStorageClient) {
this.executorService = executorService;
this.storageClient = configStorageClient;
}
public ConfigContainer getBlocking() {
return getBlocking(5L);
}
ConfigContainer getBlocking(long j) {
synchronized (this) {
Task<ConfigContainer> task = this.cachedContainerTask;
if (task != null && task.isSuccessful()) {
return this.cachedContainerTask.getResult();
}
try {
return (ConfigContainer) await(get(), j, TimeUnit.SECONDS);
} catch (InterruptedException | ExecutionException | TimeoutException unused) {
return null;
}
}
}
public Task<ConfigContainer> put(ConfigContainer configContainer) {
return put(configContainer, true);
}
public Task<ConfigContainer> put(ConfigContainer configContainer, boolean z) {
return Tasks.call(this.executorService, ConfigCacheClient$$Lambda$1.lambdaFactory$(this, configContainer)).onSuccessTask(this.executorService, ConfigCacheClient$$Lambda$2.lambdaFactory$(this, z, configContainer));
}
public static /* synthetic */ Task lambda$put$1(ConfigCacheClient configCacheClient, boolean z, ConfigContainer configContainer, Void r3) throws Exception {
if (z) {
configCacheClient.updateInMemoryConfigContainer(configContainer);
}
return Tasks.forResult(configContainer);
}
public Task<ConfigContainer> get() {
Task<ConfigContainer> task;
synchronized (this) {
Task<ConfigContainer> task2 = this.cachedContainerTask;
if (task2 == null || (task2.isComplete() && !this.cachedContainerTask.isSuccessful())) {
this.cachedContainerTask = Tasks.call(this.executorService, ConfigCacheClient$$Lambda$3.lambdaFactory$(this.storageClient));
}
task = this.cachedContainerTask;
}
return task;
}
public void clear() {
synchronized (this) {
this.cachedContainerTask = Tasks.forResult(null);
}
this.storageClient.clear();
}
private void updateInMemoryConfigContainer(ConfigContainer configContainer) {
synchronized (this) {
this.cachedContainerTask = Tasks.forResult(configContainer);
}
}
public static ConfigCacheClient getInstance(ExecutorService executorService, ConfigStorageClient configStorageClient) {
ConfigCacheClient configCacheClient;
synchronized (ConfigCacheClient.class) {
String fileName = configStorageClient.getFileName();
Map<String, ConfigCacheClient> map = clientInstances;
if (!map.containsKey(fileName)) {
map.put(fileName, new ConfigCacheClient(executorService, configStorageClient));
}
configCacheClient = map.get(fileName);
}
return configCacheClient;
}
private static <TResult> TResult await(Task<TResult> task, long j, TimeUnit timeUnit) throws ExecutionException, InterruptedException, TimeoutException {
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());
}
/* loaded from: classes2.dex */
public static class AwaitListener<TResult> implements OnSuccessListener<TResult>, OnFailureListener, OnCanceledListener {
private final CountDownLatch latch;
private AwaitListener() {
this.latch = new CountDownLatch(1);
}
/* synthetic */ AwaitListener(AnonymousClass1 anonymousClass1) {
this();
}
@Override // com.google.android.gms.tasks.OnSuccessListener
public void onSuccess(TResult tresult) {
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) throws InterruptedException {
return this.latch.await(j, timeUnit);
}
}
}