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

import android.accounts.Account;
import android.app.PendingIntent;
import android.content.AttributionSource;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.DeadObjectException;
import android.os.Handler;
import android.os.IBinder;
import android.os.IInterface;
import android.os.Looper;
import android.os.RemoteException;
import android.text.TextUtils;
import androidx.annotation.CallSuper;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.annotation.WorkerThread;
import com.google.android.gms.common.annotation.KeepForSdk;
import com.google.android.gms.common.api.Scope;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicInteger;

/* JADX INFO: loaded from: classes2.dex */
public abstract class g {

    @KeepForSdk
    public static final int CONNECT_STATE_CONNECTED = 4;

    @KeepForSdk
    public static final int CONNECT_STATE_DISCONNECTED = 1;

    @KeepForSdk
    public static final int CONNECT_STATE_DISCONNECTING = 5;

    @NonNull
    @KeepForSdk
    public static final String DEFAULT_ACCOUNT = "<<default account>>";

    @NonNull
    @KeepForSdk
    public static final String KEY_PENDING_INTENT = "pendingIntent";

    @Nullable
    private volatile String zzA;

    @Nullable
    private volatile j8.a zzB;

    @Nullable
    private e8.b zzC;
    private boolean zzD;

    @Nullable
    private volatile c1 zzE;

    @VisibleForTesting
    h1 zza;
    final Handler zzb;

    @NonNull
    @VisibleForTesting
    protected d zzc;

    @NonNull
    @VisibleForTesting
    protected AtomicInteger zzd;
    private int zzf;
    private long zzg;
    private long zzh;
    private int zzi;
    private long zzj;

    @Nullable
    private volatile String zzk;
    private final Context zzl;
    private final Looper zzm;
    private final m zzn;
    private final e8.e zzo;
    private final Object zzp;
    private final Object zzq;

    @Nullable
    private r zzr;

    @Nullable
    private IInterface zzs;
    private final ArrayList zzt;

    @Nullable
    private z0 zzu;
    private int zzv;

    @Nullable
    private final b zzw;

    @Nullable
    private final c zzx;
    private final int zzy;

    @Nullable
    private final String zzz;
    private static final e8.c[] zze = new e8.c[0];

    @NonNull
    @KeepForSdk
    public static final String[] GOOGLE_PLUS_REQUIRED_FEATURES = {"service_esmobile", "service_googleme"};

    @KeepForSdk
    @VisibleForTesting
    public g(@NonNull Context context, @NonNull Handler handler, @NonNull m mVar, @NonNull e8.e eVar, int i10, @Nullable b bVar, @Nullable c cVar) {
        this.zzk = null;
        this.zzp = new Object();
        this.zzq = new Object();
        this.zzt = new ArrayList();
        this.zzv = 1;
        this.zzC = null;
        this.zzD = false;
        this.zzE = null;
        this.zzd = new AtomicInteger(0);
        if (context == null) {
            throw new NullPointerException("Context must not be null");
        }
        this.zzl = context;
        if (handler == null) {
            throw new NullPointerException("Handler must not be null");
        }
        this.zzb = handler;
        this.zzm = handler.getLooper();
        if (mVar == null) {
            throw new NullPointerException("Supervisor must not be null");
        }
        this.zzn = mVar;
        if (eVar == null) {
            throw new NullPointerException("API availability must not be null");
        }
        this.zzo = eVar;
        this.zzy = i10;
        this.zzw = bVar;
        this.zzx = cVar;
        this.zzz = null;
    }

    private final void zzp(int i10, @Nullable IInterface iInterface) {
        h1 h1Var;
        w.a((i10 == 4) == (iInterface != null));
        synchronized (this.zzp) {
            try {
                this.zzv = i10;
                this.zzs = iInterface;
                Bundle bundle = null;
                if (i10 == 1) {
                    z0 z0Var = this.zzu;
                    if (z0Var != null) {
                        m mVar = this.zzn;
                        String strZza = this.zza.zza();
                        w.checkNotNull(strZza);
                        String str = this.zza.f8348a;
                        zza();
                        mVar.c(strZza, str, z0Var, this.zza.f8349b);
                        this.zzu = null;
                    }
                } else if (i10 == 2 || i10 == 3) {
                    z0 z0Var2 = this.zzu;
                    if (z0Var2 != null && (h1Var = this.zza) != null) {
                        new StringBuilder(String.valueOf(h1Var.zza()).length() + 70 + String.valueOf(h1Var.f8348a).length());
                        m mVar2 = this.zzn;
                        String strZza2 = this.zza.zza();
                        w.checkNotNull(strZza2);
                        String str2 = this.zza.f8348a;
                        zza();
                        mVar2.c(strZza2, str2, z0Var2, this.zza.f8349b);
                        this.zzd.incrementAndGet();
                    }
                    z0 z0Var3 = new z0(this, this.zzd.get());
                    this.zzu = z0Var3;
                    h1 h1Var2 = (this.zzv != 3 || getLocalStartServiceAction() == null) ? new h1(getStartServicePackage(), getStartServiceAction(), false, 4225, getUseDynamicLookup()) : new h1(getContext().getPackageName(), getLocalStartServiceAction(), true, 4225, false);
                    this.zza = h1Var2;
                    if (h1Var2.f8349b && getMinApkVersion() < 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.zza.zza())));
                    }
                    m mVar3 = this.zzn;
                    String strZza3 = this.zza.zza();
                    w.checkNotNull(strZza3);
                    e8.b bVarZza = mVar3.zza(new e1(strZza3, this.zza.f8348a, this.zza.f8349b), z0Var3, zza(), getBindServiceExecutor());
                    if (!bVarZza.x()) {
                        new StringBuilder(String.valueOf(this.zza.zza()).length() + 34 + String.valueOf(this.zza.f8348a).length());
                        int i11 = bVarZza.f25514b;
                        if (i11 == -1) {
                            i11 = 16;
                        }
                        if (bVarZza.getResolution() != null) {
                            bundle = new Bundle();
                            bundle.putParcelable(KEY_PENDING_INTENT, bVarZza.getResolution());
                        }
                        zzb(i11, bundle, this.zzd.get());
                    }
                } else if (i10 == 4) {
                    w.checkNotNull(iInterface);
                    onConnectedLocked(iInterface);
                }
            } finally {
            }
        }
    }

    @KeepForSdk
    public void checkAvailabilityAndConnect() {
        int iB = this.zzo.b(this.zzl, getMinApkVersion());
        if (iB == 0) {
            connect(new e(this));
        } else {
            zzp(1, null);
            triggerNotAvailable(new e(this), iB, null);
        }
    }

    @KeepForSdk
    public final void checkConnected() {
        if (!isConnected()) {
            throw new IllegalStateException("Not connected. Call connect() and wait for onConnected() to be called.");
        }
    }

    @KeepForSdk
    public void connect(@NonNull d dVar) {
        if (dVar == null) {
            throw new NullPointerException("Connection progress callbacks cannot be null.");
        }
        this.zzc = dVar;
        zzp(2, null);
    }

    @Nullable
    @KeepForSdk
    public abstract IInterface createServiceInterface(@NonNull IBinder iBinder);

    @KeepForSdk
    public void disconnect() {
        this.zzd.incrementAndGet();
        ArrayList arrayList = this.zzt;
        synchronized (arrayList) {
            try {
                int size = arrayList.size();
                for (int i10 = 0; i10 < size; i10++) {
                    ((x0) arrayList.get(i10)).c();
                }
                arrayList.clear();
            } catch (Throwable th) {
                throw th;
            }
        }
        synchronized (this.zzq) {
            this.zzr = null;
        }
        zzp(1, null);
    }

    @KeepForSdk
    public void dump(@NonNull String str, @NonNull FileDescriptor fileDescriptor, @NonNull PrintWriter printWriter, @NonNull String[] strArr) {
        int i10;
        IInterface iInterface;
        r rVar;
        synchronized (this.zzp) {
            i10 = this.zzv;
            iInterface = this.zzs;
        }
        synchronized (this.zzq) {
            rVar = this.zzr;
        }
        printWriter.append((CharSequence) str).append("mConnectState=");
        if (i10 == 1) {
            printWriter.print("DISCONNECTED");
        } else if (i10 == 2) {
            printWriter.print("REMOTE_CONNECTING");
        } else if (i10 == 3) {
            printWriter.print("LOCAL_CONNECTING");
        } else if (i10 == 4) {
            printWriter.print("CONNECTED");
        } else if (i10 != 5) {
            printWriter.print("UNKNOWN");
        } else {
            printWriter.print("DISCONNECTING");
        }
        printWriter.append(" mService=");
        if (iInterface == null) {
            printWriter.append("null");
        } else {
            printWriter.append((CharSequence) getServiceDescriptor()).append("@").append((CharSequence) Integer.toHexString(System.identityHashCode(iInterface.asBinder())));
        }
        printWriter.append(" mServiceBroker=");
        if (rVar == null) {
            printWriter.println("null");
        } else {
            printWriter.append("IGmsServiceBroker@").println(Integer.toHexString(System.identityHashCode(rVar.asBinder())));
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
        if (this.zzh > 0) {
            PrintWriter printWriterAppend = printWriter.append((CharSequence) str).append("lastConnectedTime=");
            long j10 = this.zzh;
            String str2 = simpleDateFormat.format(new Date(j10));
            StringBuilder sb = new StringBuilder(String.valueOf(j10).length() + 1 + String.valueOf(str2).length());
            sb.append(j10);
            sb.append(" ");
            sb.append(str2);
            printWriterAppend.println(sb.toString());
        }
        if (this.zzg > 0) {
            printWriter.append((CharSequence) str).append("lastSuspendedCause=");
            int i11 = this.zzf;
            if (i11 == 1) {
                printWriter.append("CAUSE_SERVICE_DISCONNECTED");
            } else if (i11 == 2) {
                printWriter.append("CAUSE_NETWORK_LOST");
            } else if (i11 != 3) {
                printWriter.append((CharSequence) String.valueOf(i11));
            } else {
                printWriter.append("CAUSE_DEAD_OBJECT_EXCEPTION");
            }
            PrintWriter printWriterAppend2 = printWriter.append(" lastSuspendedTime=");
            long j11 = this.zzg;
            String str3 = simpleDateFormat.format(new Date(j11));
            StringBuilder sb2 = new StringBuilder(String.valueOf(j11).length() + 1 + String.valueOf(str3).length());
            sb2.append(j11);
            sb2.append(" ");
            sb2.append(str3);
            printWriterAppend2.println(sb2.toString());
        }
        if (this.zzj > 0) {
            printWriter.append((CharSequence) str).append("lastFailedStatus=").append((CharSequence) hb.a.e0(this.zzi));
            PrintWriter printWriterAppend3 = printWriter.append(" lastFailedTime=");
            long j12 = this.zzj;
            String str4 = simpleDateFormat.format(new Date(j12));
            StringBuilder sb3 = new StringBuilder(String.valueOf(j12).length() + 1 + String.valueOf(str4).length());
            sb3.append(j12);
            sb3.append(" ");
            sb3.append(str4);
            printWriterAppend3.println(sb3.toString());
        }
    }

    @KeepForSdk
    public boolean enableLocalFallback() {
        return false;
    }

    @Nullable
    @KeepForSdk
    public Account getAccount() {
        return null;
    }

    @NonNull
    @KeepForSdk
    public e8.c[] getApiFeatures() {
        return zze;
    }

    @Nullable
    @KeepForSdk
    public j8.a getAttributionSourceWrapper() {
        return this.zzB;
    }

    @Nullable
    @KeepForSdk
    public final e8.c[] getAvailableFeatures() {
        c1 c1Var = this.zzE;
        if (c1Var == null) {
            return null;
        }
        return c1Var.f8319b;
    }

    @Nullable
    @KeepForSdk
    public Executor getBindServiceExecutor() {
        return null;
    }

    @Nullable
    @KeepForSdk
    public Bundle getConnectionHint() {
        return null;
    }

    @NonNull
    @KeepForSdk
    public final Context getContext() {
        return this.zzl;
    }

    @NonNull
    @KeepForSdk
    public String getEndpointPackageName() {
        h1 h1Var;
        if (!isConnected() || (h1Var = this.zza) == null) {
            throw new RuntimeException("Failed to connect when checking package");
        }
        return h1Var.f8348a;
    }

    @KeepForSdk
    public int getGCoreServiceId() {
        return this.zzy;
    }

    @NonNull
    @KeepForSdk
    public Bundle getGetServiceRequestExtraArgs() {
        return new Bundle();
    }

    @Nullable
    @KeepForSdk
    public String getLastDisconnectMessage() {
        return this.zzk;
    }

    @Nullable
    @KeepForSdk
    public String getLocalStartServiceAction() {
        return null;
    }

    @NonNull
    @KeepForSdk
    public final Looper getLooper() {
        return this.zzm;
    }

    @KeepForSdk
    public int getMinApkVersion() {
        return e8.e.f25521a;
    }

    @KeepForSdk
    @WorkerThread
    public void getRemoteService(@Nullable o oVar, @NonNull Set<Scope> set) {
        AttributionSource attributionSource;
        Bundle getServiceRequestExtraArgs = getGetServiceRequestExtraArgs();
        String attributionTag = (Build.VERSION.SDK_INT < 31 || this.zzB == null || (attributionSource = this.zzB.getAttributionSource()) == null || attributionSource.getAttributionTag() == null) ? this.zzA : attributionSource.getAttributionTag();
        String str = attributionTag;
        int i10 = this.zzy;
        int i11 = e8.e.f25521a;
        Scope[] scopeArr = k.f8365l;
        Bundle bundle = new Bundle();
        e8.c[] cVarArr = k.f8366m;
        k kVar = new k(6, i10, i11, null, null, scopeArr, bundle, null, cVarArr, cVarArr, true, 0, false, str);
        kVar.f8370d = this.zzl.getPackageName();
        kVar.f8372f = getServiceRequestExtraArgs;
        if (set != null) {
            kVar.f8371e = (Scope[]) set.toArray(new Scope[0]);
        }
        if (requiresSignIn()) {
            Account account = getAccount();
            if (account == null) {
                account = new Account(DEFAULT_ACCOUNT, "com.google");
            }
            kVar.zzj = account;
            if (oVar != null) {
                kVar.zzg = oVar.asBinder();
            }
        } else if (requiresAccount()) {
            kVar.zzj = getAccount();
        }
        kVar.f8373g = zze;
        kVar.f8374h = getApiFeatures();
        if (usesClientTelemetry()) {
            kVar.f8377k = true;
        }
        try {
            try {
                synchronized (this.zzq) {
                    try {
                        r rVar = this.zzr;
                        if (rVar != null) {
                            ((q0) rVar).getService(new y0(this, this.zzd.get()), kVar);
                        }
                    } finally {
                    }
                }
            } catch (RemoteException | RuntimeException unused) {
                onPostInitHandler(8, null, null, this.zzd.get());
            }
        } catch (DeadObjectException unused2) {
            triggerConnectionSuspended(3);
        } catch (SecurityException e10) {
            throw e10;
        }
    }

    @NonNull
    @KeepForSdk
    public Set<Scope> getScopes() {
        return Collections.emptySet();
    }

    @NonNull
    @KeepForSdk
    public final IInterface getService() throws DeadObjectException {
        IInterface iInterface;
        synchronized (this.zzp) {
            try {
                if (this.zzv == 5) {
                    throw new DeadObjectException();
                }
                checkConnected();
                IInterface iInterface2 = this.zzs;
                w.f(iInterface2, "Client is connected but service is null");
                iInterface = iInterface2;
            } catch (Throwable th) {
                throw th;
            }
        }
        return iInterface;
    }

    @Nullable
    @KeepForSdk
    public IBinder getServiceBrokerBinder() {
        synchronized (this.zzq) {
            try {
                r rVar = this.zzr;
                if (rVar == null) {
                    return null;
                }
                return rVar.asBinder();
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    @NonNull
    @KeepForSdk
    public abstract String getServiceDescriptor();

    @NonNull
    @KeepForSdk
    public Intent getSignInIntent() {
        throw new UnsupportedOperationException("Not a sign in API");
    }

    @NonNull
    @KeepForSdk
    public abstract String getStartServiceAction();

    @NonNull
    @KeepForSdk
    public String getStartServicePackage() {
        return "com.google.android.gms";
    }

    @Nullable
    @KeepForSdk
    public j getTelemetryConfiguration() {
        c1 c1Var = this.zzE;
        if (c1Var == null) {
            return null;
        }
        return c1Var.zzd;
    }

    @KeepForSdk
    public boolean getUseDynamicLookup() {
        return getMinApkVersion() >= 211700000;
    }

    @KeepForSdk
    public boolean hasConnectionInfo() {
        return this.zzE != null;
    }

    @KeepForSdk
    public boolean isConnected() {
        boolean z10;
        synchronized (this.zzp) {
            z10 = this.zzv == 4;
        }
        return z10;
    }

    @KeepForSdk
    public boolean isConnecting() {
        boolean z10;
        synchronized (this.zzp) {
            int i10 = this.zzv;
            z10 = true;
            if (i10 != 2 && i10 != 3) {
                z10 = false;
            }
        }
        return z10;
    }

    @KeepForSdk
    @CallSuper
    public void onConnectedLocked(@NonNull IInterface iInterface) {
        this.zzh = System.currentTimeMillis();
    }

    @KeepForSdk
    @CallSuper
    public void onConnectionFailed(@NonNull e8.b bVar) {
        this.zzi = bVar.f25514b;
        this.zzj = System.currentTimeMillis();
    }

    @KeepForSdk
    @CallSuper
    public void onConnectionSuspended(int i10) {
        this.zzf = i10;
        this.zzg = System.currentTimeMillis();
    }

    @KeepForSdk
    public void onPostInitHandler(int i10, @Nullable IBinder iBinder, @Nullable Bundle bundle, int i11) {
        a1 a1Var = new a1(this, i10, iBinder, bundle);
        Handler handler = this.zzb;
        handler.sendMessage(handler.obtainMessage(1, i11, -1, a1Var));
    }

    @KeepForSdk
    public void onUserSignOut(@NonNull f fVar) {
        xyz.stream.api.a0 a0Var = (xyz.stream.api.a0) fVar;
        ((com.google.android.gms.common.api.internal.a0) a0Var.f35758b).f8218k.f8244k.post(new e.s0(a0Var, 29));
    }

    @KeepForSdk
    public boolean providesSignIn() {
        return false;
    }

    @KeepForSdk
    public boolean requiresAccount() {
        return false;
    }

    @KeepForSdk
    public boolean requiresGooglePlayServices() {
        return true;
    }

    @KeepForSdk
    public boolean requiresSignIn() {
        return false;
    }

    @KeepForSdk
    public void setAttributionSourceWrapper(@NonNull j8.a aVar) {
        this.zzB = aVar;
    }

    @KeepForSdk
    public void setAttributionTag(@NonNull String str) {
        this.zzA = str;
    }

    @KeepForSdk
    public void triggerConnectionSuspended(int i10) {
        int i11 = this.zzd.get();
        Handler handler = this.zzb;
        handler.sendMessage(handler.obtainMessage(6, i11, i10));
    }

    @KeepForSdk
    @VisibleForTesting
    public void triggerNotAvailable(@NonNull d dVar, int i10, @Nullable PendingIntent pendingIntent) {
        if (dVar == null) {
            throw new NullPointerException("Connection progress callbacks cannot be null.");
        }
        this.zzc = dVar;
        int i11 = this.zzd.get();
        Handler handler = this.zzb;
        handler.sendMessage(handler.obtainMessage(3, i11, i10, pendingIntent));
    }

    @KeepForSdk
    public boolean usesClientTelemetry() {
        return false;
    }

    @NonNull
    public final String zza() {
        String str = this.zzz;
        return str == null ? this.zzl.getClass().getName() : str;
    }

    public final void zzb(int i10, @Nullable Bundle bundle, int i11) {
        b1 b1Var = new b1(this, i10, bundle);
        Handler handler = this.zzb;
        handler.sendMessage(handler.obtainMessage(7, i11, -1, b1Var));
    }

    public final void zzc(c1 c1Var) {
        this.zzE = c1Var;
        if (usesClientTelemetry()) {
            j jVar = c1Var.zzd;
            x.a().zza(jVar == null ? null : jVar.f8360a);
        }
    }

    public final /* synthetic */ void zzd(int i10, IInterface iInterface) {
        zzp(i10, null);
    }

    public final /* synthetic */ boolean zze(int i10, int i11, IInterface iInterface) {
        synchronized (this.zzp) {
            try {
                if (this.zzv != i10) {
                    return false;
                }
                zzp(i11, iInterface);
                return true;
            } finally {
            }
        }
    }

    public final /* synthetic */ void zzf(int i10) {
        int i11;
        int i12;
        synchronized (this.zzp) {
            i11 = this.zzv;
        }
        if (i11 == 3) {
            this.zzD = true;
            i12 = 5;
        } else {
            i12 = 4;
        }
        Handler handler = this.zzb;
        handler.sendMessage(handler.obtainMessage(i12, this.zzd.get(), 16));
    }

    public final /* synthetic */ boolean zzg() {
        if (this.zzD || TextUtils.isEmpty(getServiceDescriptor()) || TextUtils.isEmpty(getLocalStartServiceAction())) {
            return false;
        }
        try {
            Class.forName(getServiceDescriptor());
            return true;
        } catch (ClassNotFoundException unused) {
            return false;
        }
    }

    public final /* synthetic */ Object zzh() {
        return this.zzq;
    }

    public final /* synthetic */ void zzi(r rVar) {
        this.zzr = rVar;
    }

    public final /* synthetic */ ArrayList zzj() {
        return this.zzt;
    }

    public final /* synthetic */ b zzk() {
        return this.zzw;
    }

    public final /* synthetic */ c zzl() {
        return this.zzx;
    }

    public final /* synthetic */ e8.b zzm() {
        return this.zzC;
    }

    public final /* synthetic */ void zzn(e8.b bVar) {
        this.zzC = bVar;
    }

    public final /* synthetic */ boolean zzo() {
        return this.zzD;
    }

    /* JADX WARN: Illegal instructions before constructor call */
    @KeepForSdk
    public g(@NonNull Context context, @NonNull Looper looper, int i10, @Nullable b bVar, @Nullable c cVar, @Nullable String str) {
        g1 g1VarA = m.a(context);
        e8.e eVar = e8.e.f25522b;
        w.checkNotNull(bVar);
        w.checkNotNull(cVar);
        this(context, looper, g1VarA, eVar, i10, bVar, cVar, str);
    }

    @KeepForSdk
    public void disconnect(@NonNull String str) {
        this.zzk = str;
        disconnect();
    }

    @KeepForSdk
    @VisibleForTesting
    public g(@NonNull Context context, @NonNull Looper looper, @NonNull m mVar, @NonNull e8.e eVar, int i10, @Nullable b bVar, @Nullable c cVar, @Nullable String str) {
        this.zzk = null;
        this.zzp = new Object();
        this.zzq = new Object();
        this.zzt = new ArrayList();
        this.zzv = 1;
        this.zzC = null;
        this.zzD = false;
        this.zzE = null;
        this.zzd = new AtomicInteger(0);
        if (context == null) {
            throw new NullPointerException("Context must not be null");
        }
        this.zzl = context;
        if (looper == null) {
            throw new NullPointerException("Looper must not be null");
        }
        this.zzm = looper;
        if (mVar == null) {
            throw new NullPointerException("Supervisor must not be null");
        }
        this.zzn = mVar;
        if (eVar != null) {
            this.zzo = eVar;
            this.zzb = new w0(this, looper);
            this.zzy = i10;
            this.zzw = bVar;
            this.zzx = cVar;
            this.zzz = str;
            return;
        }
        throw new NullPointerException("API availability must not be null");
    }
}
