package o;

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

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* renamed from: o.im0, reason: case insensitive filesystem */
/* loaded from: classes.dex */
public final class ServiceConnectionC1512im0 implements ServiceConnection {
    public final Context a;
    public final Intent b;
    public final ScheduledThreadPoolExecutor c;
    public final ArrayDeque d;
    public BinderC1321gm0 e;
    public boolean f;

    public ServiceConnectionC1512im0(Context context) {
        ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(1, new NamedThreadFactory("Firebase-FirebaseInstanceIdServiceConnection"));
        scheduledThreadPoolExecutor.setKeepAliveTime(40L, TimeUnit.SECONDS);
        scheduledThreadPoolExecutor.allowCoreThreadTimeOut(true);
        this.d = new ArrayDeque();
        this.f = false;
        Context applicationContext = context.getApplicationContext();
        this.a = applicationContext;
        this.b = new Intent("com.google.firebase.MESSAGING_EVENT").setPackage(applicationContext.getPackageName());
        this.c = scheduledThreadPoolExecutor;
    }

    public final synchronized void a() {
        try {
            Log.isLoggable("FirebaseMessaging", 3);
            while (!this.d.isEmpty()) {
                Log.isLoggable("FirebaseMessaging", 3);
                BinderC1321gm0 binderC1321gm0 = this.e;
                if (binderC1321gm0 == null || !binderC1321gm0.isBinderAlive()) {
                    Log.isLoggable("FirebaseMessaging", 3);
                    if (!this.f) {
                        this.f = true;
                        if (!ConnectionTracker.getInstance().bindService(this.a, this.b, this, 65)) {
                            this.f = false;
                            ArrayDeque arrayDeque = this.d;
                            while (!arrayDeque.isEmpty()) {
                                ((C1417hm0) arrayDeque.poll()).b.d(null);
                            }
                        }
                    }
                    return;
                }
                Log.isLoggable("FirebaseMessaging", 3);
                this.e.a((C1417hm0) this.d.poll());
            }
        } finally {
        }
    }

    public final synchronized Hw0 b(Intent intent) {
        C1417hm0 c1417hm0;
        Log.isLoggable("FirebaseMessaging", 3);
        c1417hm0 = new C1417hm0(intent);
        ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = this.c;
        c1417hm0.b.a.a(scheduledThreadPoolExecutor, new G1(20, scheduledThreadPoolExecutor.schedule(new RunnableC2108p1(25, c1417hm0), 20L, TimeUnit.SECONDS)));
        this.d.add(c1417hm0);
        a();
        return c1417hm0.b.a;
    }

    @Override // android.content.ServiceConnection
    public final synchronized void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        try {
            if (Log.isLoggable("FirebaseMessaging", 3)) {
                Objects.toString(componentName);
            }
            this.f = false;
            if (iBinder instanceof BinderC1321gm0) {
                this.e = (BinderC1321gm0) iBinder;
                a();
            } else {
                Objects.toString(iBinder);
                ArrayDeque arrayDeque = this.d;
                while (!arrayDeque.isEmpty()) {
                    ((C1417hm0) arrayDeque.poll()).b.d(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();
    }
}
