package d3;

import P2.InterfaceC0245b;
import P2.InterfaceC0246c;
import android.content.ComponentName;
import android.content.Context;
import android.content.ServiceConnection;
import android.os.DeadObjectException;
import android.os.IBinder;
import android.os.IInterface;
import android.os.Looper;
import android.os.RemoteException;

/* JADX INFO: renamed from: d3.s1, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public final class ServiceConnectionC0821s1 implements ServiceConnection, InterfaceC0245b, InterfaceC0246c {

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public volatile boolean f12171q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public volatile U f12172r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public final /* synthetic */ C0824t1 f12173s;

    public ServiceConnectionC0821s1(C0824t1 c0824t1) {
        this.f12173s = c0824t1;
    }

    public final void a() throws Throwable {
        C0824t1 c0824t1 = this.f12173s;
        c0824t1.E();
        Context context = ((C0817r0) c0824t1.f5146q).f12142q;
        synchronized (this) {
            try {
                try {
                    if (this.f12171q) {
                        Z z8 = ((C0817r0) this.f12173s.f5146q).f12150y;
                        C0817r0.k(z8);
                        z8.D.b("Connection attempt already in progress");
                    } else {
                        if (this.f12172r != null && (this.f12172r.b() || this.f12172r.d())) {
                            Z z9 = ((C0817r0) this.f12173s.f5146q).f12150y;
                            C0817r0.k(z9);
                            z9.D.b("Already awaiting connection attempt");
                            return;
                        }
                        this.f12172r = new U(context, Looper.getMainLooper(), P2.H.a(context), M2.f.f4167b, 93, this, this, null);
                        Z z10 = ((C0817r0) this.f12173s.f5146q).f12150y;
                        C0817r0.k(z10);
                        z10.D.b("Connecting to remote service");
                        this.f12171q = true;
                        P2.w.f(this.f12172r);
                        this.f12172r.o();
                    }
                } catch (Throwable th) {
                    th = th;
                    throw th;
                }
            } catch (Throwable th2) {
                th = th2;
            }
        }
    }

    @Override // P2.InterfaceC0245b
    public final void c(int i) {
        C0817r0 c0817r0 = (C0817r0) this.f12173s.f5146q;
        C0815q0 c0815q0 = c0817r0.f12151z;
        C0817r0.k(c0815q0);
        c0815q0.M();
        Z z8 = c0817r0.f12150y;
        C0817r0.k(z8);
        z8.f11871C.b("Service connection suspended");
        C0815q0 c0815q02 = c0817r0.f12151z;
        C0817r0.k(c0815q02);
        c0815q02.O(new M4.k(15, this));
    }

    @Override // P2.InterfaceC0245b
    public final void d() {
        C0815q0 c0815q0 = ((C0817r0) this.f12173s.f5146q).f12151z;
        C0817r0.k(c0815q0);
        c0815q0.M();
        synchronized (this) {
            try {
                P2.w.f(this.f12172r);
                K k8 = (K) this.f12172r.u();
                C0815q0 c0815q02 = ((C0817r0) this.f12173s.f5146q).f12151z;
                C0817r0.k(c0815q02);
                c0815q02.O(new RunnableC0818r1(this, k8, 1));
            } catch (DeadObjectException | IllegalStateException unused) {
                this.f12172r = null;
                this.f12171q = false;
            }
        }
    }

    @Override // P2.InterfaceC0246c
    public final void e(M2.b bVar) {
        C0824t1 c0824t1 = this.f12173s;
        C0815q0 c0815q0 = ((C0817r0) c0824t1.f5146q).f12151z;
        C0817r0.k(c0815q0);
        c0815q0.M();
        Z z8 = ((C0817r0) c0824t1.f5146q).f12150y;
        if (z8 == null || !z8.f11423r) {
            z8 = null;
        }
        if (z8 != null) {
            z8.f11878y.c(bVar, "Service connection failed");
        }
        synchronized (this) {
            this.f12171q = false;
            this.f12172r = null;
        }
        C0815q0 c0815q02 = ((C0817r0) this.f12173s.f5146q).f12151z;
        C0817r0.k(c0815q02);
        c0815q02.O(new G3.b(this, bVar, 19, false));
    }

    @Override // android.content.ServiceConnection
    public final void onServiceConnected(ComponentName componentName, IBinder iBinder) {
        C0815q0 c0815q0 = ((C0817r0) this.f12173s.f5146q).f12151z;
        C0817r0.k(c0815q0);
        c0815q0.M();
        synchronized (this) {
            if (iBinder == null) {
                this.f12171q = false;
                Z z8 = ((C0817r0) this.f12173s.f5146q).f12150y;
                C0817r0.k(z8);
                z8.f11875v.b("Service connected with null binder");
                return;
            }
            K j8 = null;
            try {
                String interfaceDescriptor = iBinder.getInterfaceDescriptor();
                if ("com.google.android.gms.measurement.internal.IMeasurementService".equals(interfaceDescriptor)) {
                    IInterface iInterfaceQueryLocalInterface = iBinder.queryLocalInterface("com.google.android.gms.measurement.internal.IMeasurementService");
                    j8 = iInterfaceQueryLocalInterface instanceof K ? (K) iInterfaceQueryLocalInterface : new J(iBinder);
                    Z z9 = ((C0817r0) this.f12173s.f5146q).f12150y;
                    C0817r0.k(z9);
                    z9.D.b("Bound to IMeasurementService interface");
                } else {
                    Z z10 = ((C0817r0) this.f12173s.f5146q).f12150y;
                    C0817r0.k(z10);
                    z10.f11875v.c(interfaceDescriptor, "Got binder with a wrong descriptor");
                }
            } catch (RemoteException unused) {
                Z z11 = ((C0817r0) this.f12173s.f5146q).f12150y;
                C0817r0.k(z11);
                z11.f11875v.b("Service connect failed to get IMeasurementService");
            }
            if (j8 == null) {
                this.f12171q = false;
                try {
                    S2.a aVarA = S2.a.a();
                    C0824t1 c0824t1 = this.f12173s;
                    aVarA.b(((C0817r0) c0824t1.f5146q).f12142q, c0824t1.f12178s);
                } catch (IllegalArgumentException unused2) {
                }
            } else {
                C0815q0 c0815q02 = ((C0817r0) this.f12173s.f5146q).f12151z;
                C0817r0.k(c0815q02);
                c0815q02.O(new RunnableC0818r1(this, j8, 0));
            }
        }
    }

    @Override // android.content.ServiceConnection
    public final void onServiceDisconnected(ComponentName componentName) {
        C0817r0 c0817r0 = (C0817r0) this.f12173s.f5146q;
        C0815q0 c0815q0 = c0817r0.f12151z;
        C0817r0.k(c0815q0);
        c0815q0.M();
        Z z8 = c0817r0.f12150y;
        C0817r0.k(z8);
        z8.f11871C.b("Service disconnected");
        C0815q0 c0815q02 = c0817r0.f12151z;
        C0817r0.k(c0815q02);
        c0815q02.O(new G3.b(this, componentName, 18, false));
    }
}
