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 androidx.annotation.Nullable;
import java.util.ArrayDeque;
import java.util.Objects;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/* JADX INFO: loaded from: classes2.dex */
public final class p0 implements ServiceConnection {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final Context f24238a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final Intent f24239b;

    @Nullable
    private n0 binder;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final ScheduledExecutorService f24240c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final ArrayDeque f24241d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public boolean f24242e;

    public p0(Context context) {
        ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(0, new m.c("Firebase-FirebaseInstanceIdServiceConnection", 2));
        this.f24241d = new ArrayDeque();
        this.f24242e = false;
        Context applicationContext = context.getApplicationContext();
        this.f24238a = applicationContext;
        this.f24239b = new Intent("com.google.firebase.MESSAGING_EVENT").setPackage(applicationContext.getPackageName());
        this.f24240c = scheduledThreadPoolExecutor;
    }

    public final synchronized void a() {
        while (!this.f24241d.isEmpty()) {
            try {
                n0 n0Var = this.binder;
                if (n0Var == null || !n0Var.isBinderAlive()) {
                    if (!this.f24242e) {
                        this.f24242e = true;
                        if (!h8.a.b().a(this.f24238a, this.f24239b, this, 65)) {
                            this.f24242e = false;
                            while (true) {
                                ArrayDeque arrayDeque = this.f24241d;
                                if (arrayDeque.isEmpty()) {
                                    break;
                                } else {
                                    ((o0) arrayDeque.poll()).f24231b.trySetResult(null);
                                }
                            }
                        }
                    }
                    return;
                }
                this.binder.a((o0) this.f24241d.poll());
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final synchronized w8.t b(Intent intent) {
        o0 o0Var;
        o0Var = new o0(intent);
        ScheduledExecutorService scheduledExecutorService = this.f24240c;
        o0Var.f24231b.f34224a.a(scheduledExecutorService, new androidx.core.app.e(scheduledExecutorService.schedule(new y.a(o0Var, 20), 20L, TimeUnit.SECONDS), 29));
        this.f24241d.add(o0Var);
        a();
        return o0Var.f24231b.f34224a;
    }

    @Override // android.content.ServiceConnection
    public final synchronized void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        try {
            if (Log.isLoggable("FirebaseMessaging", 3)) {
                Objects.toString(componentName);
            }
            this.f24242e = false;
            if (iBinder instanceof n0) {
                this.binder = (n0) iBinder;
                a();
            } else {
                Objects.toString(iBinder);
                while (true) {
                    ArrayDeque arrayDeque = this.f24241d;
                    if (arrayDeque.isEmpty()) {
                        return;
                    } else {
                        ((o0) arrayDeque.poll()).f24231b.trySetResult(null);
                    }
                }
            }
        } catch (Throwable th) {
            throw th;
        }
    }

    @Override // android.content.ServiceConnection
    public final void onServiceDisconnected(ComponentName componentName) {
        if (Log.isLoggable("FirebaseMessaging", 3)) {
            Objects.toString(componentName);
        }
        a();
    }
}
