package d8;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.os.Looper;
import android.os.Messenger;
import android.util.Log;
import android.util.SparseArray;
import androidx.annotation.Nullable;
import com.google.android.gms.cloudmessaging.zzt;
import com.google.android.gms.common.internal.w;
import java.util.ArrayDeque;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public l f25032c;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final /* synthetic */ o f25035f;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public int f25030a = 0;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final Messenger f25031b = new Messenger(new o8.e(Looper.getMainLooper(), new l5.i(this, 1), 1));

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final ArrayDeque f25033d = new ArrayDeque();

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final SparseArray f25034e = new SparseArray();

    public /* synthetic */ k(o oVar) {
        this.f25035f = oVar;
    }

    public final synchronized void a() {
        if (this.f25030a == 2 && this.f25033d.isEmpty() && this.f25034e.size() == 0) {
            this.f25030a = 3;
            h8.a.b().c(this.f25035f.f25041a, this);
        }
    }

    public final synchronized boolean b(m mVar) {
        int i10 = this.f25030a;
        int i11 = 0;
        int i12 = 1;
        if (i10 != 0) {
            if (i10 == 1) {
                this.f25033d.add(mVar);
                return true;
            }
            if (i10 != 2) {
                return false;
            }
            this.f25033d.add(mVar);
            this.f25035f.f25042b.execute(new j(this, i11));
            return true;
        }
        this.f25033d.add(mVar);
        w.g(this.f25030a == 0);
        this.f25030a = 1;
        Intent intent = new Intent("com.google.android.c2dm.intent.REGISTER");
        intent.setPackage("com.google.android.gms");
        try {
            if (h8.a.b().a(this.f25035f.f25041a, intent, this, 1)) {
                this.f25035f.f25042b.schedule(new j(this, i12), 30L, TimeUnit.SECONDS);
            } else {
                zza(0, "Unable to bind to service");
            }
        } catch (SecurityException e10) {
            zzb(0, "Unable to bind to service", e10);
        }
        return true;
    }

    @Override // android.content.ServiceConnection
    public final void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        this.f25035f.f25042b.execute(new l.l(this, iBinder, 25, 0));
    }

    @Override // android.content.ServiceConnection
    public final void onServiceDisconnected(ComponentName componentName) {
        this.f25035f.f25042b.execute(new j(this, 2));
    }

    public final synchronized void zza(int i10, @Nullable String str) {
        zzb(i10, str, null);
    }

    public final synchronized void zzb(int i10, @Nullable String str, @Nullable Throwable th) {
        try {
            if (Log.isLoggable("MessengerIpcClient", 3)) {
                "Disconnected: ".concat(String.valueOf(str));
            }
            int i11 = this.f25030a;
            if (i11 == 0) {
                throw new IllegalStateException();
            }
            if (i11 != 1 && i11 != 2) {
                if (i11 != 3) {
                    return;
                }
                this.f25030a = 4;
                return;
            }
            this.f25030a = 4;
            h8.a.b().c(this.f25035f.f25041a, this);
            zzt zztVar = new zzt(i10, str, th);
            Iterator it = this.f25033d.iterator();
            while (it.hasNext()) {
                ((n) it.next()).a(zztVar);
            }
            this.f25033d.clear();
            for (int i12 = 0; i12 < this.f25034e.size(); i12++) {
                ((n) this.f25034e.valueAt(i12)).a(zztVar);
            }
            this.f25034e.clear();
        } catch (Throwable th2) {
            throw th2;
        }
    }
}
