package zj;

import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
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.OnCompleteListener;
import com.google.android.gms.tasks.Task;
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.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

/* loaded from: classes3.dex */
public final class t {

    /* renamed from: o */
    private static final Map f53489o = new HashMap();

    /* renamed from: a */
    private final Context f53490a;

    /* renamed from: b */
    private final i f53491b;

    /* renamed from: g */
    private boolean f53496g;

    /* renamed from: h */
    private final Intent f53497h;

    /* renamed from: l */
    private ServiceConnection f53501l;

    /* renamed from: m */
    private IInterface f53502m;

    /* renamed from: n */
    private final yj.i f53503n;

    /* renamed from: d */
    private final List f53493d = new ArrayList();

    /* renamed from: e */
    private final Set f53494e = new HashSet();

    /* renamed from: f */
    private final Object f53495f = new Object();

    /* renamed from: j */
    private final IBinder.DeathRecipient f53499j = new IBinder.DeathRecipient() { // from class: zj.l
        @Override // android.os.IBinder.DeathRecipient
        public final void binderDied() {
            t.h(t.this);
        }
    };

    /* renamed from: k */
    private final AtomicInteger f53500k = new AtomicInteger(0);

    /* renamed from: c */
    private final String f53492c = "com.google.android.finsky.inappreviewservice.InAppReviewService";

    /* renamed from: i */
    private final WeakReference f53498i = new WeakReference(null);

    public t(Context context, i iVar, String str, Intent intent, yj.i iVar2, o oVar, byte[] bArr) {
        this.f53490a = context;
        this.f53491b = iVar;
        this.f53497h = intent;
        this.f53503n = iVar2;
    }

    public static /* synthetic */ void h(t tVar) {
        tVar.f53491b.d("reportBinderDeath", new Object[0]);
        android.support.v4.media.session.b.a(tVar.f53498i.get());
        tVar.f53491b.d("%s : Binder has died.", tVar.f53492c);
        Iterator it = tVar.f53493d.iterator();
        while (it.hasNext()) {
            ((j) it.next()).c(tVar.s());
        }
        tVar.f53493d.clear();
        tVar.t();
    }

    public static /* bridge */ /* synthetic */ void m(t tVar, j jVar) {
        if (tVar.f53502m != null || tVar.f53496g) {
            if (!tVar.f53496g) {
                jVar.run();
                return;
            } else {
                tVar.f53491b.d("Waiting to bind to the service.", new Object[0]);
                tVar.f53493d.add(jVar);
                return;
            }
        }
        tVar.f53491b.d("Initiate binding to the service.", new Object[0]);
        tVar.f53493d.add(jVar);
        s sVar = new s(tVar, null);
        tVar.f53501l = sVar;
        tVar.f53496g = true;
        if (tVar.f53490a.bindService(tVar.f53497h, sVar, 1)) {
            return;
        }
        tVar.f53491b.d("Failed to bind to the service.", new Object[0]);
        tVar.f53496g = false;
        Iterator it = tVar.f53493d.iterator();
        while (it.hasNext()) {
            ((j) it.next()).c(new u());
        }
        tVar.f53493d.clear();
    }

    public static /* bridge */ /* synthetic */ void n(t tVar) {
        tVar.f53491b.d("linkToDeath", new Object[0]);
        try {
            tVar.f53502m.asBinder().linkToDeath(tVar.f53499j, 0);
        } catch (RemoteException e10) {
            tVar.f53491b.c(e10, "linkToDeath failed", new Object[0]);
        }
    }

    public static /* bridge */ /* synthetic */ void o(t tVar) {
        tVar.f53491b.d("unlinkToDeath", new Object[0]);
        tVar.f53502m.asBinder().unlinkToDeath(tVar.f53499j, 0);
    }

    private final RemoteException s() {
        return new RemoteException(String.valueOf(this.f53492c).concat(" : Binder has died."));
    }

    public final void t() {
        synchronized (this.f53495f) {
            Iterator it = this.f53494e.iterator();
            while (it.hasNext()) {
                ((TaskCompletionSource) it.next()).trySetException(s());
            }
            this.f53494e.clear();
        }
    }

    public final Handler c() {
        Handler handler;
        Map map = f53489o;
        synchronized (map) {
            if (!map.containsKey(this.f53492c)) {
                HandlerThread handlerThread = new HandlerThread(this.f53492c, 10);
                handlerThread.start();
                map.put(this.f53492c, new Handler(handlerThread.getLooper()));
            }
            handler = (Handler) map.get(this.f53492c);
        }
        return handler;
    }

    public final IInterface e() {
        return this.f53502m;
    }

    public final void p(j jVar, final TaskCompletionSource taskCompletionSource) {
        synchronized (this.f53495f) {
            this.f53494e.add(taskCompletionSource);
            taskCompletionSource.getTask().addOnCompleteListener(new OnCompleteListener() { // from class: zj.k
                @Override // com.google.android.gms.tasks.OnCompleteListener
                public final void onComplete(Task task) {
                    t.this.q(taskCompletionSource, task);
                }
            });
        }
        synchronized (this.f53495f) {
            if (this.f53500k.getAndIncrement() > 0) {
                this.f53491b.a("Already connected to the service.", new Object[0]);
            }
        }
        c().post(new m(this, jVar.b(), jVar));
    }

    public final /* synthetic */ void q(TaskCompletionSource taskCompletionSource, Task task) {
        synchronized (this.f53495f) {
            this.f53494e.remove(taskCompletionSource);
        }
    }

    public final void r(TaskCompletionSource taskCompletionSource) {
        synchronized (this.f53495f) {
            this.f53494e.remove(taskCompletionSource);
        }
        synchronized (this.f53495f) {
            if (this.f53500k.get() > 0 && this.f53500k.decrementAndGet() > 0) {
                this.f53491b.d("Leaving the connection open for other ongoing calls.", new Object[0]);
            } else {
                c().post(new n(this));
            }
        }
    }
}
