package P2;

import android.content.Context;
import android.content.ServiceConnection;
import android.os.HandlerThread;
import android.os.Looper;
import java.util.HashMap;
import java.util.concurrent.Executor;

/* JADX INFO: loaded from: classes.dex */
public final class H {

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public static H f5351h;
    public static HandlerThread i;

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

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public volatile Y2.e f5354c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final S2.a f5355d;

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final long f5356e;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final long f5357f;

    public H(Context context, Looper looper) {
        G g8 = new G(0, this);
        this.f5353b = context.getApplicationContext();
        Y2.e eVar = new Y2.e(looper, g8);
        Looper.getMainLooper();
        this.f5354c = eVar;
        this.f5355d = S2.a.a();
        this.f5356e = 5000L;
        this.f5357f = 300000L;
    }

    public static H a(Context context) {
        synchronized (f5350g) {
            try {
                if (f5351h == null) {
                    f5351h = new H(context.getApplicationContext(), context.getMainLooper());
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        return f5351h;
    }

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

    public final M2.b c(E e8, A a3, String str, Executor executor) {
        synchronized (this.f5352a) {
            try {
                F f3 = (F) this.f5352a.get(e8);
                M2.b bVarA = null;
                if (executor == null) {
                    executor = null;
                }
                if (f3 == null) {
                    f3 = new F(this, e8);
                    f3.f5341q.put(a3, a3);
                    bVarA = F.a(f3, str, executor);
                    this.f5352a.put(e8, f3);
                } else {
                    this.f5354c.removeMessages(0, e8);
                    if (f3.f5341q.containsKey(a3)) {
                        throw new IllegalStateException("Trying to bind a GmsServiceConnection that was already connected before.  config=".concat(e8.toString()));
                    }
                    f3.f5341q.put(a3, a3);
                    int i5 = f3.f5342r;
                    if (i5 == 1) {
                        a3.onServiceConnected(f3.f5346v, f3.f5344t);
                    } else if (i5 == 2) {
                        bVarA = F.a(f3, str, executor);
                    }
                }
                if (f3.f5343s) {
                    return M2.b.f4153u;
                }
                if (bVarA == null) {
                    bVarA = new M2.b(-1);
                }
                return bVarA;
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void d(String str, ServiceConnection serviceConnection, boolean z8) {
        E e8 = new E(str, z8);
        w.g(serviceConnection, "ServiceConnection must not be null");
        synchronized (this.f5352a) {
            try {
                F f3 = (F) this.f5352a.get(e8);
                if (f3 == null) {
                    throw new IllegalStateException("Nonexistent connection status for service config: ".concat(e8.toString()));
                }
                if (!f3.f5341q.containsKey(serviceConnection)) {
                    throw new IllegalStateException("Trying to unbind a GmsServiceConnection  that was not bound before.  config=".concat(e8.toString()));
                }
                f3.f5341q.remove(serviceConnection);
                if (f3.f5341q.isEmpty()) {
                    this.f5354c.sendMessageDelayed(this.f5354c.obtainMessage(0, e8), this.f5356e);
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }
}
