package P2;

import android.accounts.Account;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.DeadObjectException;
import android.os.IBinder;
import android.os.IInterface;
import android.os.Looper;
import android.os.RemoteException;
import android.util.Log;
import com.google.android.gms.common.api.Scope;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

/* JADX INFO: renamed from: P2.e, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes.dex */
public abstract class AbstractC0248e {

    /* JADX INFO: renamed from: x, reason: collision with root package name */
    public static final M2.d[] f5360x = new M2.d[0];

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public L2.a f5362b;

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

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final M2.f f5365e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final y f5366f;
    public s i;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public InterfaceC0247d f5369j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public IInterface f5370k;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public A f5372m;

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public final InterfaceC0245b f5374o;

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public final InterfaceC0246c f5375p;

    /* JADX INFO: renamed from: q, reason: collision with root package name */
    public final int f5376q;

    /* JADX INFO: renamed from: r, reason: collision with root package name */
    public final String f5377r;

    /* JADX INFO: renamed from: s, reason: collision with root package name */
    public volatile String f5378s;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public volatile String f5361a = null;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final Object f5367g = new Object();

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final Object f5368h = new Object();

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public final ArrayList f5371l = new ArrayList();

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public int f5373n = 1;

    /* JADX INFO: renamed from: t, reason: collision with root package name */
    public M2.b f5379t = null;

    /* JADX INFO: renamed from: u, reason: collision with root package name */
    public boolean f5380u = false;

    /* JADX INFO: renamed from: v, reason: collision with root package name */
    public volatile D f5381v = null;

    /* JADX INFO: renamed from: w, reason: collision with root package name */
    public final AtomicInteger f5382w = new AtomicInteger(0);

    public AbstractC0248e(Context context, Looper looper, H h8, M2.f fVar, int i, InterfaceC0245b interfaceC0245b, InterfaceC0246c interfaceC0246c, String str) {
        w.g(context, "Context must not be null");
        this.f5363c = context;
        w.g(looper, "Looper must not be null");
        w.g(h8, "Supervisor must not be null");
        this.f5364d = h8;
        w.g(fVar, "API availability must not be null");
        this.f5365e = fVar;
        this.f5366f = new y(this, looper);
        this.f5376q = i;
        this.f5374o = interfaceC0245b;
        this.f5375p = interfaceC0246c;
        this.f5377r = str;
    }

    public static /* bridge */ /* synthetic */ void y(AbstractC0248e abstractC0248e) {
        int i;
        int i5;
        synchronized (abstractC0248e.f5367g) {
            i = abstractC0248e.f5373n;
        }
        if (i == 3) {
            abstractC0248e.f5380u = true;
            i5 = 5;
        } else {
            i5 = 4;
        }
        y yVar = abstractC0248e.f5366f;
        yVar.sendMessage(yVar.obtainMessage(i5, abstractC0248e.f5382w.get(), 16));
    }

    public static /* bridge */ /* synthetic */ boolean z(AbstractC0248e abstractC0248e, int i, int i5, IInterface iInterface) {
        synchronized (abstractC0248e.f5367g) {
            try {
                if (abstractC0248e.f5373n != i) {
                    return false;
                }
                abstractC0248e.A(i5, iInterface);
                return true;
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void A(int i, IInterface iInterface) {
        L2.a aVar;
        w.a((i == 4) == (iInterface != null));
        synchronized (this.f5367g) {
            try {
                this.f5373n = i;
                this.f5370k = iInterface;
                Bundle bundle = null;
                if (i == 1) {
                    A a3 = this.f5372m;
                    if (a3 != null) {
                        H h8 = this.f5364d;
                        String str = this.f5362b.f3979c;
                        w.f(str);
                        this.f5362b.getClass();
                        if (this.f5377r == null) {
                            this.f5363c.getClass();
                        }
                        h8.d(str, a3, this.f5362b.f3978b);
                        this.f5372m = null;
                    }
                } else if (i == 2 || i == 3) {
                    A a8 = this.f5372m;
                    if (a8 != null && (aVar = this.f5362b) != null) {
                        Log.e("GmsClient", "Calling connect() while still connected, missing disconnect() for " + aVar.f3979c + " on com.google.android.gms");
                        H h9 = this.f5364d;
                        String str2 = this.f5362b.f3979c;
                        w.f(str2);
                        this.f5362b.getClass();
                        if (this.f5377r == null) {
                            this.f5363c.getClass();
                        }
                        h9.d(str2, a8, this.f5362b.f3978b);
                        this.f5382w.incrementAndGet();
                    }
                    A a9 = new A(this, this.f5382w.get());
                    this.f5372m = a9;
                    String strW = w();
                    boolean zX = x();
                    this.f5362b = new L2.a(2, strW, zX);
                    if (zX && n() < 17895000) {
                        throw new IllegalStateException("Internal Error, the minimum apk version of this BaseGmsClient is too low to support dynamic lookup. Start service action: ".concat(String.valueOf(this.f5362b.f3979c)));
                    }
                    H h10 = this.f5364d;
                    String str3 = this.f5362b.f3979c;
                    w.f(str3);
                    this.f5362b.getClass();
                    String name = this.f5377r;
                    if (name == null) {
                        name = this.f5363c.getClass().getName();
                    }
                    M2.b bVarC = h10.c(new E(str3, this.f5362b.f3978b), a9, name, null);
                    if (!(bVarC.f4155r == 0)) {
                        Log.w("GmsClient", "unable to connect to service: " + this.f5362b.f3979c + " on com.google.android.gms");
                        int i5 = bVarC.f4155r;
                        if (i5 == -1) {
                            i5 = 16;
                        }
                        if (bVarC.f4156s != null) {
                            bundle = new Bundle();
                            bundle.putParcelable("pendingIntent", bVarC.f4156s);
                        }
                        int i8 = this.f5382w.get();
                        C c3 = new C(this, i5, bundle);
                        y yVar = this.f5366f;
                        yVar.sendMessage(yVar.obtainMessage(7, i8, -1, c3));
                    }
                } else if (i == 4) {
                    w.f(iInterface);
                    System.currentTimeMillis();
                }
            } finally {
            }
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    public final void a(InterfaceC0251h interfaceC0251h, Set set) {
        Bundle bundleS = s();
        String str = Build.VERSION.SDK_INT < 31 ? this.f5378s : this.f5378s;
        int i = this.f5376q;
        int i5 = M2.f.f4166a;
        Scope[] scopeArr = C0250g.f5389E;
        Bundle bundle = new Bundle();
        M2.d[] dVarArr = C0250g.f5390F;
        C0250g c0250g = new C0250g(6, i, i5, null, null, scopeArr, bundle, null, dVarArr, dVarArr, true, 0, false, str);
        c0250g.f5397t = this.f5363c.getPackageName();
        c0250g.f5400w = bundleS;
        if (set != null) {
            c0250g.f5399v = (Scope[]) set.toArray(new Scope[0]);
        }
        if (l()) {
            Account accountQ = q();
            if (accountQ == null) {
                accountQ = new Account("<<default account>>", "com.google");
            }
            c0250g.f5401x = accountQ;
            if (interfaceC0251h != 0) {
                c0250g.f5398u = ((Z2.a) interfaceC0251h).f7420f;
            }
        }
        c0250g.f5402y = f5360x;
        c0250g.f5403z = r();
        try {
            synchronized (this.f5368h) {
                try {
                    s sVar = this.i;
                    if (sVar != null) {
                        sVar.c(new z(this, this.f5382w.get()), c0250g);
                    } else {
                        Log.w("GmsClient", "mServiceBroker is null, client disconnected");
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
        } catch (DeadObjectException e8) {
            Log.w("GmsClient", "IGmsServiceBroker.getService failed", e8);
            int i8 = this.f5382w.get();
            y yVar = this.f5366f;
            yVar.sendMessage(yVar.obtainMessage(6, i8, 3));
        } catch (RemoteException e9) {
            e = e9;
            Log.w("GmsClient", "IGmsServiceBroker.getService failed", e);
            int i9 = this.f5382w.get();
            B b8 = new B(this, 8, null, null);
            y yVar2 = this.f5366f;
            yVar2.sendMessage(yVar2.obtainMessage(1, i9, -1, b8));
        } catch (SecurityException e10) {
            throw e10;
        } catch (RuntimeException e11) {
            e = e11;
            Log.w("GmsClient", "IGmsServiceBroker.getService failed", e);
            int i92 = this.f5382w.get();
            B b82 = new B(this, 8, null, null);
            y yVar22 = this.f5366f;
            yVar22.sendMessage(yVar22.obtainMessage(1, i92, -1, b82));
        }
    }

    public final boolean b() {
        boolean z8;
        synchronized (this.f5367g) {
            int i = this.f5373n;
            z8 = true;
            if (i != 2 && i != 3) {
                z8 = false;
            }
        }
        return z8;
    }

    public final M2.d[] c() {
        D d8 = this.f5381v;
        if (d8 == null) {
            return null;
        }
        return d8.f5334r;
    }

    public final boolean d() {
        boolean z8;
        synchronized (this.f5367g) {
            z8 = this.f5373n == 4;
        }
        return z8;
    }

    public final void e() {
        if (!d() || this.f5362b == null) {
            throw new RuntimeException("Failed to connect when checking package");
        }
    }

    public final String f() {
        return this.f5361a;
    }

    public final void h(K4.c cVar) {
        ((O2.i) cVar.f3604r).f5126o.f5105C.post(new M4.k(4, cVar));
    }

    public final void i(InterfaceC0247d interfaceC0247d) {
        this.f5369j = interfaceC0247d;
        A(2, null);
    }

    public final void j() {
        this.f5382w.incrementAndGet();
        synchronized (this.f5371l) {
            try {
                int size = this.f5371l.size();
                for (int i = 0; i < size; i++) {
                    ((q) this.f5371l.get(i)).c();
                }
                this.f5371l.clear();
            } catch (Throwable th) {
                throw th;
            }
        }
        synchronized (this.f5368h) {
            this.i = null;
        }
        A(1, null);
    }

    public final void k(String str) {
        this.f5361a = str;
        j();
    }

    public boolean l() {
        return false;
    }

    public abstract int n();

    public final void o() {
        int iB = this.f5365e.b(this.f5363c, n());
        if (iB == 0) {
            i(new P.g(2, this));
            return;
        }
        A(1, null);
        this.f5369j = new P.g(2, this);
        int i = this.f5382w.get();
        y yVar = this.f5366f;
        yVar.sendMessage(yVar.obtainMessage(3, i, iB, null));
    }

    public abstract IInterface p(IBinder iBinder);

    public Account q() {
        return null;
    }

    public M2.d[] r() {
        return f5360x;
    }

    public Bundle s() {
        return new Bundle();
    }

    public Set t() {
        return Collections.EMPTY_SET;
    }

    public final IInterface u() {
        IInterface iInterface;
        synchronized (this.f5367g) {
            try {
                if (this.f5373n == 5) {
                    throw new DeadObjectException();
                }
                if (!d()) {
                    throw new IllegalStateException("Not connected. Call connect() and wait for onConnected() to be called.");
                }
                iInterface = this.f5370k;
                w.g(iInterface, "Client is connected but service is null");
            } catch (Throwable th) {
                throw th;
            }
        }
        return iInterface;
    }

    public abstract String v();

    public abstract String w();

    public boolean x() {
        return n() >= 211700000;
    }
}
