package com.google.firebase.messaging; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; import android.os.IBinder; import android.util.Log; import com.google.android.gms.common.stats.ConnectionTracker; import com.google.android.gms.common.util.concurrent.NamedThreadFactory; import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.Task; import com.google.android.gms.tasks.TaskCompletionSource; import com.google.firebase.messaging.WithinAppServiceConnection; import java.util.ArrayDeque; import java.util.Queue; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledFuture; import java.util.concurrent.ScheduledThreadPoolExecutor; import java.util.concurrent.TimeUnit; /* loaded from: classes3.dex */ public class WithinAppServiceConnection implements ServiceConnection { public WithinAppServiceBinder binder; public boolean connectionInProgress; public final Intent connectionIntent; public final Context context; public final Queue intentQueue; public final ScheduledExecutorService scheduledExecutorService; public static class BindRequest { public final Intent intent; public final TaskCompletionSource taskCompletionSource = new TaskCompletionSource(); public BindRequest(Intent intent) { this.intent = intent; } public void arrangeTimeout(ScheduledExecutorService scheduledExecutorService) { final ScheduledFuture schedule = scheduledExecutorService.schedule(new Runnable() { // from class: com.google.firebase.messaging.WithinAppServiceConnection$BindRequest$$ExternalSyntheticLambda0 @Override // java.lang.Runnable public final void run() { WithinAppServiceConnection.BindRequest.this.lambda$arrangeTimeout$0(); } }, 20L, TimeUnit.SECONDS); getTask().addOnCompleteListener(scheduledExecutorService, new OnCompleteListener() { // from class: com.google.firebase.messaging.WithinAppServiceConnection$BindRequest$$ExternalSyntheticLambda1 @Override // com.google.android.gms.tasks.OnCompleteListener public final void onComplete(Task task) { schedule.cancel(false); } }); } public final /* synthetic */ void lambda$arrangeTimeout$0() { Log.w("FirebaseMessaging", "Service took too long to process intent: " + this.intent.getAction() + " finishing."); finish(); } public Task getTask() { return this.taskCompletionSource.getTask(); } public void finish() { this.taskCompletionSource.trySetResult(null); } } public WithinAppServiceConnection(Context context, String str) { this(context, str, new ScheduledThreadPoolExecutor(0, new NamedThreadFactory("Firebase-FirebaseInstanceIdServiceConnection"))); } public WithinAppServiceConnection(Context context, String str, ScheduledExecutorService scheduledExecutorService) { this.intentQueue = new ArrayDeque(); this.connectionInProgress = false; Context applicationContext = context.getApplicationContext(); this.context = applicationContext; this.connectionIntent = new Intent(str).setPackage(applicationContext.getPackageName()); this.scheduledExecutorService = scheduledExecutorService; } public synchronized Task sendIntent(Intent intent) { BindRequest bindRequest; Log.isLoggable("FirebaseMessaging", 3); bindRequest = new BindRequest(intent); bindRequest.arrangeTimeout(this.scheduledExecutorService); this.intentQueue.add(bindRequest); flushQueue(); return bindRequest.getTask(); } public final synchronized void flushQueue() { try { Log.isLoggable("FirebaseMessaging", 3); while (!this.intentQueue.isEmpty()) { Log.isLoggable("FirebaseMessaging", 3); WithinAppServiceBinder withinAppServiceBinder = this.binder; if (withinAppServiceBinder != null && withinAppServiceBinder.isBinderAlive()) { Log.isLoggable("FirebaseMessaging", 3); this.binder.send((BindRequest) this.intentQueue.poll()); } else { startConnectionIfNeeded(); return; } } } catch (Throwable th) { throw th; } } public final void startConnectionIfNeeded() { if (Log.isLoggable("FirebaseMessaging", 3)) { StringBuilder sb = new StringBuilder(); sb.append("binder is dead. start connection? "); sb.append(!this.connectionInProgress); } if (this.connectionInProgress) { return; } this.connectionInProgress = true; try { } catch (SecurityException e) { Log.e("FirebaseMessaging", "Exception while binding the service", e); } if (ConnectionTracker.getInstance().bindService(this.context, this.connectionIntent, this, 65)) { return; } Log.e("FirebaseMessaging", "binding to the service failed"); this.connectionInProgress = false; finishAllInQueue(); } public final void finishAllInQueue() { while (!this.intentQueue.isEmpty()) { ((BindRequest) this.intentQueue.poll()).finish(); } } @Override // android.content.ServiceConnection public synchronized void onServiceConnected(ComponentName componentName, IBinder iBinder) { try { if (Log.isLoggable("FirebaseMessaging", 3)) { StringBuilder sb = new StringBuilder(); sb.append("onServiceConnected: "); sb.append(componentName); } this.connectionInProgress = false; if (!(iBinder instanceof WithinAppServiceBinder)) { Log.e("FirebaseMessaging", "Invalid service connection: " + iBinder); finishAllInQueue(); return; } this.binder = (WithinAppServiceBinder) iBinder; flushQueue(); } catch (Throwable th) { throw th; } } @Override // android.content.ServiceConnection public void onServiceDisconnected(ComponentName componentName) { if (Log.isLoggable("FirebaseMessaging", 3)) { StringBuilder sb = new StringBuilder(); sb.append("onServiceDisconnected: "); sb.append(componentName); } flushQueue(); } }