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

import android.content.ComponentName;
import android.content.Context;
import android.content.ServiceConnection;
import android.os.HandlerThread;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import com.google.android.gms.common.annotation.KeepForSdk;
import java.util.HashMap;
import java.util.concurrent.Executor;

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

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

    @Nullable
    @VisibleForTesting
    static HandlerThread zza;

    @Nullable
    private static g1 zzd;

    @Nullable
    private static Executor zze;

    public static g1 a(Context context) {
        synchronized (f8382a) {
            try {
                if (zzd == null) {
                    zzd = new g1(context.getApplicationContext(), context.getMainLooper(), zze);
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        return zzd;
    }

    public static HandlerThread b() {
        synchronized (f8382a) {
            try {
                HandlerThread handlerThread = zza;
                if (handlerThread != null) {
                    return handlerThread;
                }
                HandlerThread handlerThread2 = new HandlerThread("GoogleApiHandler", 9);
                zza = handlerThread2;
                handlerThread2.start();
                return zza;
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    @KeepForSdk
    public static void setDefaultBindExecutor(@Nullable Executor executor) {
        synchronized (f8382a) {
            try {
                g1 g1Var = zzd;
                if (g1Var != null) {
                    g1Var.zze(executor);
                }
                zze = executor;
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    @KeepForSdk
    public boolean bindService(@NonNull ComponentName componentName, @NonNull ServiceConnection serviceConnection, @NonNull String str, @Nullable Executor executor) {
        return zza(new e1(componentName), serviceConnection, str, executor).x();
    }

    public final void c(String str, String str2, z0 z0Var, boolean z10) {
        e1 e1Var = new e1(str, str2, z10);
        g1 g1Var = (g1) this;
        HashMap map = g1Var.f8338b;
        synchronized (map) {
            try {
                f1 f1Var = (f1) map.get(e1Var);
                if (f1Var == null) {
                    String string = e1Var.toString();
                    StringBuilder sb = new StringBuilder(string.length() + 50);
                    sb.append("Nonexistent connection status for service config: ");
                    sb.append(string);
                    throw new IllegalStateException(sb.toString());
                }
                if (!f1Var.f8330a.containsKey(z0Var)) {
                    String string2 = e1Var.toString();
                    StringBuilder sb2 = new StringBuilder(string2.length() + 76);
                    sb2.append("Trying to unbind a GmsServiceConnection  that was not bound before.  config=");
                    sb2.append(string2);
                    throw new IllegalStateException(sb2.toString());
                }
                f1Var.f8330a.remove(z0Var);
                if (f1Var.f8330a.isEmpty()) {
                    g1Var.f8340d.sendMessageDelayed(g1Var.f8340d.obtainMessage(0, e1Var), g1Var.f8342f);
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public abstract e8.b zza(e1 e1Var, ServiceConnection serviceConnection, String str, @Nullable Executor executor);
}
