mirror of
https://github.com/VibedByKaKi/alexandroid.git
synced 2026-11-15 02:17:18 +01:00
259 lines
10 KiB
Java
259 lines
10 KiB
Java
package com.google.firebase.messaging;
|
|
|
|
import android.content.Context;
|
|
import android.util.Log;
|
|
import androidx.annotation.GuardedBy;
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.VisibleForTesting;
|
|
import androidx.annotation.WorkerThread;
|
|
import androidx.collection.ArrayMap;
|
|
import androidx.exifinterface.media.ExifInterface;
|
|
import com.google.android.gms.tasks.Task;
|
|
import com.google.android.gms.tasks.TaskCompletionSource;
|
|
import com.google.android.gms.tasks.Tasks;
|
|
import java.io.IOException;
|
|
import java.util.ArrayDeque;
|
|
import java.util.Map;
|
|
import java.util.concurrent.Callable;
|
|
import java.util.concurrent.ExecutionException;
|
|
import java.util.concurrent.ScheduledExecutorService;
|
|
import java.util.concurrent.TimeUnit;
|
|
import java.util.concurrent.TimeoutException;
|
|
import okio.Buffer$$ExternalSyntheticBUOutline0;
|
|
|
|
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
|
|
/* loaded from: classes8.dex */
|
|
class TopicsSubscriber {
|
|
static final String ERROR_INTERNAL_SERVER_ERROR = "INTERNAL_SERVER_ERROR";
|
|
static final String ERROR_SERVICE_NOT_AVAILABLE = "SERVICE_NOT_AVAILABLE";
|
|
private static final long MAX_DELAY_SEC = 28800;
|
|
private static final long MIN_DELAY_SEC = 30;
|
|
private static final long RPC_TIMEOUT_SEC = 30;
|
|
private final Context context;
|
|
private final FirebaseMessaging firebaseMessaging;
|
|
private final Metadata metadata;
|
|
private final GmsRpc rpc;
|
|
private final TopicsStore store;
|
|
private final ScheduledExecutorService syncExecutor;
|
|
|
|
@GuardedBy("pendingOperations")
|
|
private final Map<String, ArrayDeque<TaskCompletionSource<Void>>> pendingOperations = new ArrayMap();
|
|
|
|
@GuardedBy("this")
|
|
private boolean syncScheduledOrRunning = false;
|
|
|
|
private TopicsSubscriber(FirebaseMessaging firebaseMessaging, Metadata metadata, TopicsStore topicsStore, GmsRpc gmsRpc, Context context, @NonNull ScheduledExecutorService scheduledExecutorService) {
|
|
this.firebaseMessaging = firebaseMessaging;
|
|
this.metadata = metadata;
|
|
this.store = topicsStore;
|
|
this.rpc = gmsRpc;
|
|
this.context = context;
|
|
this.syncExecutor = scheduledExecutorService;
|
|
}
|
|
|
|
private void addToPendingOperations(TopicOperation topicOperation, TaskCompletionSource<Void> taskCompletionSource) {
|
|
ArrayDeque<TaskCompletionSource<Void>> arrayDeque;
|
|
synchronized (this.pendingOperations) {
|
|
try {
|
|
String serialize = topicOperation.serialize();
|
|
if (this.pendingOperations.containsKey(serialize)) {
|
|
arrayDeque = this.pendingOperations.get(serialize);
|
|
} else {
|
|
ArrayDeque<TaskCompletionSource<Void>> arrayDeque2 = new ArrayDeque<>();
|
|
this.pendingOperations.put(serialize, arrayDeque2);
|
|
arrayDeque = arrayDeque2;
|
|
}
|
|
arrayDeque.add(taskCompletionSource);
|
|
} catch (Throwable th) {
|
|
throw th;
|
|
}
|
|
}
|
|
}
|
|
|
|
@WorkerThread
|
|
private static <T> void awaitTask(Task<T> task) throws IOException {
|
|
try {
|
|
Tasks.await(task, 30L, TimeUnit.SECONDS);
|
|
} catch (InterruptedException | TimeoutException e) {
|
|
Buffer$$ExternalSyntheticBUOutline0.m(ERROR_SERVICE_NOT_AVAILABLE, e);
|
|
} catch (ExecutionException e2) {
|
|
Throwable cause = e2.getCause();
|
|
if (cause instanceof IOException) {
|
|
throw ((IOException) cause);
|
|
}
|
|
if (cause instanceof RuntimeException) {
|
|
throw ((RuntimeException) cause);
|
|
}
|
|
Buffer$$ExternalSyntheticBUOutline0.m$2(e2);
|
|
}
|
|
}
|
|
|
|
@WorkerThread
|
|
private void blockingSubscribeToTopic(String str) throws IOException {
|
|
awaitTask(this.rpc.subscribeToTopic(this.firebaseMessaging.blockingGetToken(), str));
|
|
}
|
|
|
|
@WorkerThread
|
|
private void blockingUnsubscribeFromTopic(String str) throws IOException {
|
|
awaitTask(this.rpc.unsubscribeFromTopic(this.firebaseMessaging.blockingGetToken(), str));
|
|
}
|
|
|
|
@VisibleForTesting
|
|
public static Task<TopicsSubscriber> createInstance(final FirebaseMessaging firebaseMessaging, final Metadata metadata, final GmsRpc gmsRpc, final Context context, @NonNull final ScheduledExecutorService scheduledExecutorService) {
|
|
return Tasks.call(scheduledExecutorService, new Callable() { // from class: com.google.firebase.messaging.TopicsSubscriber$$ExternalSyntheticLambda0
|
|
@Override // java.util.concurrent.Callable
|
|
public final Object call() {
|
|
TopicsSubscriber lambda$createInstance$0;
|
|
lambda$createInstance$0 = TopicsSubscriber.lambda$createInstance$0(context, scheduledExecutorService, firebaseMessaging, metadata, gmsRpc);
|
|
return lambda$createInstance$0;
|
|
}
|
|
});
|
|
}
|
|
|
|
public static boolean isDebugLogEnabled() {
|
|
return Log.isLoggable(Constants.TAG, 3);
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: private */
|
|
public static /* synthetic */ TopicsSubscriber lambda$createInstance$0(Context context, ScheduledExecutorService scheduledExecutorService, FirebaseMessaging firebaseMessaging, Metadata metadata, GmsRpc gmsRpc) throws Exception {
|
|
return new TopicsSubscriber(firebaseMessaging, metadata, TopicsStore.getInstance(context, scheduledExecutorService), gmsRpc, context, scheduledExecutorService);
|
|
}
|
|
|
|
private void markCompletePendingOperation(TopicOperation topicOperation) {
|
|
synchronized (this.pendingOperations) {
|
|
try {
|
|
String serialize = topicOperation.serialize();
|
|
if (this.pendingOperations.containsKey(serialize)) {
|
|
ArrayDeque<TaskCompletionSource<Void>> arrayDeque = this.pendingOperations.get(serialize);
|
|
TaskCompletionSource<Void> poll = arrayDeque.poll();
|
|
if (poll != null) {
|
|
poll.setResult(null);
|
|
}
|
|
if (arrayDeque.isEmpty()) {
|
|
this.pendingOperations.remove(serialize);
|
|
}
|
|
}
|
|
} catch (Throwable th) {
|
|
throw th;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void startSync() {
|
|
if (isSyncScheduledOrRunning()) {
|
|
return;
|
|
}
|
|
syncWithDelaySecondsInternal(0L);
|
|
}
|
|
|
|
@VisibleForTesting
|
|
public TopicsStore getStore() {
|
|
return this.store;
|
|
}
|
|
|
|
public boolean hasPendingOperation() {
|
|
return this.store.getNextTopicOperation() != null;
|
|
}
|
|
|
|
public synchronized boolean isSyncScheduledOrRunning() {
|
|
return this.syncScheduledOrRunning;
|
|
}
|
|
|
|
@WorkerThread
|
|
public boolean performTopicOperation(TopicOperation topicOperation) throws IOException {
|
|
try {
|
|
String operation = topicOperation.getOperation();
|
|
int hashCode = operation.hashCode();
|
|
if (hashCode != 83) {
|
|
if (hashCode == 85 && operation.equals("U")) {
|
|
blockingUnsubscribeFromTopic(topicOperation.getTopic());
|
|
if (!isDebugLogEnabled()) {
|
|
return true;
|
|
}
|
|
topicOperation.getTopic();
|
|
return true;
|
|
}
|
|
} else if (operation.equals(ExifInterface.LATITUDE_SOUTH)) {
|
|
blockingSubscribeToTopic(topicOperation.getTopic());
|
|
if (!isDebugLogEnabled()) {
|
|
return true;
|
|
}
|
|
topicOperation.getTopic();
|
|
return true;
|
|
}
|
|
isDebugLogEnabled();
|
|
return true;
|
|
} catch (IOException e) {
|
|
if (!ERROR_SERVICE_NOT_AVAILABLE.equals(e.getMessage()) && !"INTERNAL_SERVER_ERROR".equals(e.getMessage())) {
|
|
if (e.getMessage() != null) {
|
|
throw e;
|
|
}
|
|
Log.e(Constants.TAG, "Topic operation failed without exception message. Will retry Topic operation.");
|
|
return false;
|
|
}
|
|
Log.e(Constants.TAG, "Topic operation failed: " + e.getMessage() + ". Will retry Topic operation.");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public void scheduleSyncTaskWithDelaySeconds(Runnable runnable, long j) {
|
|
this.syncExecutor.schedule(runnable, j, TimeUnit.SECONDS);
|
|
}
|
|
|
|
@VisibleForTesting
|
|
public Task<Void> scheduleTopicOperation(TopicOperation topicOperation) {
|
|
this.store.addTopicOperation(topicOperation);
|
|
TaskCompletionSource<Void> taskCompletionSource = new TaskCompletionSource<>();
|
|
addToPendingOperations(topicOperation, taskCompletionSource);
|
|
return taskCompletionSource.getTask();
|
|
}
|
|
|
|
public synchronized void setSyncScheduledOrRunning(boolean z) {
|
|
this.syncScheduledOrRunning = z;
|
|
}
|
|
|
|
public void startTopicsSyncIfNecessary() {
|
|
if (hasPendingOperation()) {
|
|
startSync();
|
|
}
|
|
}
|
|
|
|
public Task<Void> subscribeToTopic(String str) {
|
|
Task<Void> scheduleTopicOperation = scheduleTopicOperation(TopicOperation.subscribe(str));
|
|
startTopicsSyncIfNecessary();
|
|
return scheduleTopicOperation;
|
|
}
|
|
|
|
@WorkerThread
|
|
public boolean syncTopics() throws IOException {
|
|
while (true) {
|
|
synchronized (this) {
|
|
try {
|
|
TopicOperation nextTopicOperation = this.store.getNextTopicOperation();
|
|
if (nextTopicOperation == null) {
|
|
isDebugLogEnabled();
|
|
return true;
|
|
}
|
|
if (!performTopicOperation(nextTopicOperation)) {
|
|
return false;
|
|
}
|
|
this.store.removeTopicOperation(nextTopicOperation);
|
|
markCompletePendingOperation(nextTopicOperation);
|
|
} catch (Throwable th) {
|
|
throw th;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void syncWithDelaySecondsInternal(long j) {
|
|
scheduleSyncTaskWithDelaySeconds(new TopicsSyncTask(this, this.context, this.metadata, Math.min(Math.max(30L, 2 * j), MAX_DELAY_SEC)), j);
|
|
setSyncScheduledOrRunning(true);
|
|
}
|
|
|
|
public Task<Void> unsubscribeFromTopic(String str) {
|
|
Task<Void> scheduleTopicOperation = scheduleTopicOperation(TopicOperation.unsubscribe(str));
|
|
startTopicsSyncIfNecessary();
|
|
return scheduleTopicOperation;
|
|
}
|
|
}
|