package com.google.android.gms.common.api.internal;

import android.content.Context;
import android.os.Bundle;
import android.os.DeadObjectException;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.WorkerThread;
import com.google.android.gms.common.api.Status;
import com.google.android.gms.common.api.UnsupportedApiCallException;
import com.google.android.gms.measurement.internal.u1;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;
import java.util.concurrent.atomic.AtomicReference;

/* JADX INFO: loaded from: classes2.dex */
public final class a0 implements com.google.android.gms.common.api.j, com.google.android.gms.common.api.k, z0 {

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final com.google.android.gms.common.api.e f8209b;

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

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

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

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

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final /* synthetic */ g f8218k;

    @Nullable
    private final m0 zai;

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final LinkedList f8208a = new LinkedList();

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

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final HashMap f8213f = new HashMap();

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

    @Nullable
    private e8.b zal = null;

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

    public a0(g gVar, com.google.android.gms.common.api.i iVar) {
        this.f8218k = gVar;
        com.google.android.gms.common.api.e eVarZab = iVar.zab(gVar.f8244k.getLooper(), this);
        this.f8209b = eVarZab;
        this.f8210c = iVar.getApiKey();
        this.f8211d = new w();
        this.f8214g = iVar.zaa();
        if (!eVarZab.requiresSignIn()) {
            this.zai = null;
        } else {
            this.zai = iVar.zac(gVar.f8236c, gVar.f8244k);
        }
    }

    @Nullable
    @WorkerThread
    private final e8.c zaC(@Nullable e8.c[] cVarArr) {
        if (cVarArr != null && cVarArr.length != 0) {
            e8.c[] availableFeatures = this.f8209b.getAvailableFeatures();
            if (availableFeatures == null) {
                availableFeatures = new e8.c[0];
            }
            androidx.collection.f fVar = new androidx.collection.f(availableFeatures.length);
            for (e8.c cVar : availableFeatures) {
                fVar.put(cVar.f25515a, Long.valueOf(cVar.w()));
            }
            for (e8.c cVar2 : cVarArr) {
                Long l10 = (Long) fVar.get(cVar2.f25515a);
                if (l10 == null || l10.longValue() < cVar2.w()) {
                    return cVar2;
                }
            }
        }
        return null;
    }

    @WorkerThread
    private final void zaF(@Nullable Status status, @Nullable Exception exc, boolean z10) {
        com.google.android.gms.common.internal.w.c(this.f8218k.f8244k);
        if ((status == null) == (exc == null)) {
            throw new IllegalArgumentException("Status XOR exception should be null");
        }
        Iterator it = this.f8208a.iterator();
        while (it.hasNext()) {
            s0 s0Var = (s0) it.next();
            if (!z10 || s0Var.f8289a == 2) {
                if (status != null) {
                    s0Var.a(status);
                } else {
                    s0Var.b(exc);
                }
                it.remove();
            }
        }
    }

    public final void a(e8.b bVar) {
        HashSet hashSet = this.f8212e;
        Iterator it = hashSet.iterator();
        if (!it.hasNext()) {
            hashSet.clear();
            return;
        }
        u1.x(it.next());
        if (com.google.android.gms.common.internal.v.equal(bVar, e8.b.f25512c)) {
            this.f8209b.getEndpointPackageName();
        }
        throw null;
    }

    public final void b(Status status) {
        com.google.android.gms.common.internal.w.c(this.f8218k.f8244k);
        zaF(status, null, false);
    }

    public final void c() {
        LinkedList linkedList = this.f8208a;
        ArrayList arrayList = new ArrayList(linkedList);
        int size = arrayList.size();
        for (int i10 = 0; i10 < size; i10++) {
            s0 s0Var = (s0) arrayList.get(i10);
            if (!this.f8209b.isConnected()) {
                return;
            }
            if (g(s0Var)) {
                linkedList.remove(s0Var);
            }
        }
    }

    public final void d() {
        j();
        a(e8.b.f25512c);
        if (this.f8215h) {
            g gVar = this.f8218k;
            o8.e eVar = gVar.f8244k;
            a aVar = this.f8210c;
            eVar.removeMessages(11, aVar);
            gVar.f8244k.removeMessages(9, aVar);
            this.f8215h = false;
        }
        Iterator it = this.f8213f.values().iterator();
        if (it.hasNext()) {
            ((k0) it.next()).getClass();
            throw null;
        }
        c();
        f();
    }

    public final void e(int i10) {
        j();
        this.f8215h = true;
        this.f8211d.zae(i10, this.f8209b.getLastDisconnectMessage());
        g gVar = this.f8218k;
        o8.e eVar = gVar.f8244k;
        a aVar = this.f8210c;
        eVar.sendMessageDelayed(Message.obtain(eVar, 9, aVar), 5000L);
        o8.e eVar2 = gVar.f8244k;
        eVar2.sendMessageDelayed(Message.obtain(eVar2, 11, aVar), 120000L);
        gVar.f8238e.w();
        Iterator it = this.f8213f.values().iterator();
        if (it.hasNext()) {
            ((k0) it.next()).getClass();
            throw null;
        }
    }

    public final void f() {
        g gVar = this.f8218k;
        o8.e eVar = gVar.f8244k;
        a aVar = this.f8210c;
        eVar.removeMessages(12, aVar);
        o8.e eVar2 = gVar.f8244k;
        eVar2.sendMessageDelayed(eVar2.obtainMessage(12, aVar), gVar.f8234a);
    }

    public final boolean g(s0 s0Var) {
        if (!(s0Var instanceof f0)) {
            com.google.android.gms.common.api.e eVar = this.f8209b;
            s0Var.d(this.f8211d, eVar.requiresSignIn());
            try {
                s0Var.c(this);
            } catch (DeadObjectException unused) {
                onConnectionSuspended(1);
                eVar.disconnect("DeadObjectException thrown while running ApiCallRunner.");
            }
            return true;
        }
        f0 f0Var = (f0) s0Var;
        e8.c cVarZaC = zaC(f0Var.zab(this));
        if (cVarZaC == null) {
            com.google.android.gms.common.api.e eVar2 = this.f8209b;
            s0Var.d(this.f8211d, eVar2.requiresSignIn());
            try {
                s0Var.c(this);
            } catch (DeadObjectException unused2) {
                onConnectionSuspended(1);
                eVar2.disconnect("DeadObjectException thrown while running ApiCallRunner.");
            }
            return true;
        }
        this.f8209b.getClass();
        if (!this.f8218k.f8245l || !f0Var.f(this)) {
            f0Var.b(new UnsupportedApiCallException(cVarZaC));
            return true;
        }
        b0 b0Var = new b0(this.f8210c, cVarZaC);
        int iIndexOf = this.f8216i.indexOf(b0Var);
        if (iIndexOf >= 0) {
            b0 b0Var2 = (b0) this.f8216i.get(iIndexOf);
            this.f8218k.f8244k.removeMessages(15, b0Var2);
            o8.e eVar3 = this.f8218k.f8244k;
            eVar3.sendMessageDelayed(Message.obtain(eVar3, 15, b0Var2), 5000L);
            return false;
        }
        this.f8216i.add(b0Var);
        o8.e eVar4 = this.f8218k.f8244k;
        eVar4.sendMessageDelayed(Message.obtain(eVar4, 15, b0Var), 5000L);
        o8.e eVar5 = this.f8218k.f8244k;
        eVar5.sendMessageDelayed(Message.obtain(eVar5, 16, b0Var), 120000L);
        e8.b bVar = new e8.b(2, null);
        if (h(bVar)) {
            return false;
        }
        this.f8218k.d(bVar, this.f8214g);
        return false;
    }

    /* JADX WARN: Code restructure failed: missing block: B:16:0x0044, code lost:
    
        if (r6.get() == null) goto L29;
     */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final boolean h(e8.b bVar) {
        synchronized (g.R) {
            try {
                g gVar = this.f8218k;
                int i10 = 0;
                if (gVar.zao == null || !gVar.f8242i.contains(this.f8210c)) {
                    return false;
                }
                x xVar = this.f8218k.zao;
                int i11 = this.f8214g;
                xVar.getClass();
                t0 t0Var = new t0(bVar, i11);
                loop0: while (true) {
                    AtomicReference atomicReference = xVar.f8300c;
                    while (true) {
                        if (atomicReference.compareAndSet(null, t0Var)) {
                            xVar.f8301d.post(new v0(xVar, t0Var, i10));
                            break loop0;
                        }
                        if (atomicReference.get() != null) {
                            break;
                        }
                    }
                }
                return true;
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final boolean i(boolean z10) {
        com.google.android.gms.common.internal.w.c(this.f8218k.f8244k);
        com.google.android.gms.common.api.e eVar = this.f8209b;
        if (!eVar.isConnected() || !this.f8213f.isEmpty()) {
            return false;
        }
        w wVar = this.f8211d;
        if (wVar.f8297a.isEmpty() && wVar.f8298b.isEmpty()) {
            eVar.disconnect("Timing out service connection.");
            return true;
        }
        if (!z10) {
            return false;
        }
        f();
        return false;
    }

    public final void j() {
        com.google.android.gms.common.internal.w.c(this.f8218k.f8244k);
        this.zal = null;
    }

    /* JADX WARN: Type inference failed for: r5v6, types: [com.google.android.gms.common.api.e, t8.c] */
    /* JADX WARN: Type inference fix 'apply assigned field type' failed
    java.lang.UnsupportedOperationException: ArgType.getObject(), call class: class jadx.core.dex.instructions.args.ArgType$UnknownArg
    	at jadx.core.dex.instructions.args.ArgType.getObject(ArgType.java:593)
    	at jadx.core.dex.attributes.nodes.ClassTypeVarsAttr.getTypeVarsMapFor(ClassTypeVarsAttr.java:35)
    	at jadx.core.dex.nodes.utils.TypeUtils.replaceClassGenerics(TypeUtils.java:177)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.insertExplicitUseCast(FixTypesVisitor.java:397)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.tryFieldTypeWithNewCasts(FixTypesVisitor.java:359)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.applyFieldType(FixTypesVisitor.java:309)
    	at jadx.core.dex.visitors.typeinference.FixTypesVisitor.visit(FixTypesVisitor.java:94)
     */
    public final void k() {
        g gVar = this.f8218k;
        com.google.android.gms.common.internal.w.c(gVar.f8244k);
        com.google.android.gms.common.api.e eVar = this.f8209b;
        if (eVar.isConnected() || eVar.isConnecting()) {
            return;
        }
        try {
            int iV = gVar.f8238e.v(gVar.f8236c, eVar);
            if (iV != 0) {
                e8.b bVar = new e8.b(iV, null);
                bVar.toString();
                zar(bVar, null);
                return;
            }
            c0 c0Var = new c0(gVar, eVar, this.f8210c);
            if (eVar.requiresSignIn()) {
                m0 m0Var = (m0) com.google.android.gms.common.internal.w.checkNotNull(this.zai);
                t8.c cVar = m0Var.f8274f;
                if (cVar != null) {
                    cVar.disconnect();
                }
                Integer numValueOf = Integer.valueOf(System.identityHashCode(m0Var));
                com.google.android.gms.common.internal.i iVar = m0Var.f8273e;
                iVar.f8358i = numValueOf;
                g8.b bVar2 = m0Var.f8271c;
                Context context = m0Var.f8269a;
                Handler handler = m0Var.f8270b;
                m0Var.f8274f = bVar2.buildClient(context, handler.getLooper(), iVar, (Object) iVar.f8357h, (com.google.android.gms.common.api.j) m0Var, (com.google.android.gms.common.api.k) m0Var);
                m0Var.f8275g = c0Var;
                Set set = m0Var.f8272d;
                if (set == null || set.isEmpty()) {
                    handler.post(new i0(m0Var, 1));
                } else {
                    m0Var.f8274f.b();
                }
            }
            try {
                eVar.connect(c0Var);
            } catch (SecurityException e10) {
                zar(new e8.b(10, null, null), e10);
            }
        } catch (IllegalStateException e11) {
            zar(new e8.b(10, null, null), e11);
        }
    }

    public final void l(s0 s0Var) {
        com.google.android.gms.common.internal.w.c(this.f8218k.f8244k);
        boolean zIsConnected = this.f8209b.isConnected();
        LinkedList linkedList = this.f8208a;
        if (zIsConnected) {
            if (g(s0Var)) {
                f();
                return;
            } else {
                linkedList.add(s0Var);
                return;
            }
        }
        linkedList.add(s0Var);
        e8.b bVar = this.zal;
        if (bVar == null || !bVar.w()) {
            k();
        } else {
            zar(this.zal, null);
        }
    }

    public final void m(e8.b bVar) {
        com.google.android.gms.common.internal.w.c(this.f8218k.f8244k);
        com.google.android.gms.common.api.e eVar = this.f8209b;
        eVar.disconnect("onSignInFailed for " + eVar.getClass().getName() + " with " + String.valueOf(bVar));
        zar(bVar, null);
    }

    public final void n() {
        com.google.android.gms.common.internal.w.c(this.f8218k.f8244k);
        Status status = g.f8233m;
        b(status);
        w wVar = this.f8211d;
        wVar.getClass();
        wVar.a(false, status);
        for (j jVar : (j[]) this.f8213f.keySet().toArray(new j[0])) {
            l(new r0(jVar, new w8.i()));
        }
        a(new e8.b(4, null, null));
        com.google.android.gms.common.api.e eVar = this.f8209b;
        if (eVar.isConnected()) {
            eVar.onUserSignOut(new xyz.stream.api.a0(this, 18));
        }
    }

    @Override // com.google.android.gms.common.api.j, com.google.android.gms.common.api.internal.f, com.google.android.gms.common.api.internal.z0
    public final void onConnected(@Nullable Bundle bundle) {
        Looper looperMyLooper = Looper.myLooper();
        g gVar = this.f8218k;
        if (looperMyLooper == gVar.f8244k.getLooper()) {
            d();
        } else {
            gVar.f8244k.post(new e.s0(this, 28));
        }
    }

    @Override // com.google.android.gms.common.api.internal.m
    public final void onConnectionFailed(e8.b bVar) {
        zar(bVar, null);
    }

    @Override // com.google.android.gms.common.api.internal.f
    public final void onConnectionSuspended(int i10) {
        Looper looperMyLooper = Looper.myLooper();
        g gVar = this.f8218k;
        if (looperMyLooper == gVar.f8244k.getLooper()) {
            e(i10);
        } else {
            gVar.f8244k.post(new y3.s(i10, 1, this));
        }
    }

    @Nullable
    @WorkerThread
    public final e8.b zad() {
        com.google.android.gms.common.internal.w.c(this.f8218k.f8244k);
        return this.zal;
    }

    @WorkerThread
    public final void zar(@NonNull e8.b bVar, @Nullable Exception exc) {
        t8.c cVar;
        com.google.android.gms.common.internal.w.c(this.f8218k.f8244k);
        m0 m0Var = this.zai;
        if (m0Var != null && (cVar = m0Var.f8274f) != null) {
            cVar.disconnect();
        }
        j();
        this.f8218k.f8238e.w();
        a(bVar);
        if ((this.f8209b instanceof g8.d) && bVar.f25514b != 24) {
            g gVar = this.f8218k;
            gVar.f8235b = true;
            o8.e eVar = gVar.f8244k;
            eVar.sendMessageDelayed(eVar.obtainMessage(19), 300000L);
        }
        if (bVar.f25514b == 4) {
            b(g.Q);
            return;
        }
        if (this.f8208a.isEmpty()) {
            this.zal = bVar;
            return;
        }
        if (exc != null) {
            com.google.android.gms.common.internal.w.c(this.f8218k.f8244k);
            zaF(null, exc, false);
            return;
        }
        if (!this.f8218k.f8245l) {
            b(g.e(this.f8210c, bVar));
            return;
        }
        zaF(g.e(this.f8210c, bVar), null, true);
        if (this.f8208a.isEmpty() || h(bVar) || this.f8218k.d(bVar, this.f8214g)) {
            return;
        }
        if (bVar.f25514b == 18) {
            this.f8215h = true;
        }
        if (!this.f8215h) {
            b(g.e(this.f8210c, bVar));
            return;
        }
        g gVar2 = this.f8218k;
        a aVar = this.f8210c;
        o8.e eVar2 = gVar2.f8244k;
        eVar2.sendMessageDelayed(Message.obtain(eVar2, 9, aVar), 5000L);
    }
}
