package C3;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.IBinder;
import android.os.IInterface;
import android.os.RemoteException;
import com.google.android.gms.tasks.TaskCompletionSource;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicInteger;

/* JADX INFO: renamed from: C3.e, reason: case insensitive filesystem */
/* JADX INFO: loaded from: classes3.dex */
public final class C0129e {

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

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

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

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

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final Intent f723h;

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public final E f724i;

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

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

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

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

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

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public final C f726k = new IBinder.DeathRecipient() { // from class: C3.C
        @Override // android.os.IBinder.DeathRecipient
        public final void binderDied() {
            C0129e c0129e = this.f704a;
            c0129e.f717b.b("reportBinderDeath", new Object[0]);
            if (c0129e.f725j.get() != null) {
                throw new ClassCastException();
            }
            c0129e.f717b.b("%s : Binder has died.", c0129e.f718c);
            Iterator it = c0129e.f719d.iterator();
            while (it.hasNext()) {
                ((B) it.next()).a(new RemoteException(String.valueOf(c0129e.f718c).concat(" : Binder has died.")));
            }
            c0129e.f719d.clear();
            synchronized (c0129e.f721f) {
                c0129e.e();
            }
        }
    };
    public final AtomicInteger l = new AtomicInteger(0);

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final WeakReference f725j = new WeakReference(null);

    /* JADX WARN: Type inference failed for: r0v3, types: [C3.C] */
    public C0129e(Context context, A a6, String str, Intent intent, E e6) {
        this.f716a = context;
        this.f717b = a6;
        this.f718c = str;
        this.f723h = intent;
        this.f724i = e6;
    }

    public static void b(C0129e c0129e, B b5) {
        IInterface iInterface = c0129e.f728n;
        ArrayList arrayList = c0129e.f719d;
        A a6 = c0129e.f717b;
        if (iInterface != null || c0129e.f722g) {
            if (!c0129e.f722g) {
                b5.run();
                return;
            } else {
                a6.b("Waiting to bind to the service.", new Object[0]);
                arrayList.add(b5);
                return;
            }
        }
        a6.b("Initiate binding to the service.", new Object[0]);
        arrayList.add(b5);
        ServiceConnectionC0128d serviceConnectionC0128d = new ServiceConnectionC0128d(c0129e);
        c0129e.f727m = serviceConnectionC0128d;
        c0129e.f722g = true;
        if (c0129e.f716a.bindService(c0129e.f723h, serviceConnectionC0128d, 1)) {
            return;
        }
        a6.b("Failed to bind to the service.", new Object[0]);
        c0129e.f722g = false;
        Iterator it = arrayList.iterator();
        while (it.hasNext()) {
            ((B) it.next()).a(new f("Failed to bind to the service."));
        }
        arrayList.clear();
    }

    public final Handler a() {
        Handler handler;
        HashMap map = f715o;
        synchronized (map) {
            try {
                if (!map.containsKey(this.f718c)) {
                    HandlerThread handlerThread = new HandlerThread(this.f718c, 10);
                    handlerThread.start();
                    map.put(this.f718c, new Handler(handlerThread.getLooper()));
                }
                handler = (Handler) map.get(this.f718c);
            } catch (Throwable th) {
                throw th;
            }
        }
        return handler;
    }

    public final void c(B b5, TaskCompletionSource taskCompletionSource) {
        a().post(new D(this, b5.c(), taskCompletionSource, b5));
    }

    public final void d(TaskCompletionSource taskCompletionSource) {
        synchronized (this.f721f) {
            this.f720e.remove(taskCompletionSource);
        }
        a().post(new C0127c(this, 1));
    }

    public final void e() {
        HashSet hashSet = this.f720e;
        Iterator it = hashSet.iterator();
        while (it.hasNext()) {
            ((TaskCompletionSource) it.next()).trySetException(new RemoteException(String.valueOf(this.f718c).concat(" : Binder has died.")));
        }
        hashSet.clear();
    }
}
