package com.google.android.play.integrity.internal; 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: classes2.dex */ public final class y { /* renamed from: o, reason: collision with root package name */ private static final Map f5251o = new HashMap(); /* renamed from: a, reason: collision with root package name */ private final Context f5252a; /* renamed from: b, reason: collision with root package name */ private final n f5253b; /* renamed from: g, reason: collision with root package name */ private boolean f5258g; /* renamed from: h, reason: collision with root package name */ private final Intent f5259h; /* renamed from: l, reason: collision with root package name */ private ServiceConnection f5263l; /* renamed from: m, reason: collision with root package name */ private IInterface f5264m; /* renamed from: n, reason: collision with root package name */ private final com.google.android.play.core.integrity.q f5265n; /* renamed from: d, reason: collision with root package name */ private final List f5255d = new ArrayList(); /* renamed from: e, reason: collision with root package name */ private final Set f5256e = new HashSet(); /* renamed from: f, reason: collision with root package name */ private final Object f5257f = new Object(); /* renamed from: j, reason: collision with root package name */ private final IBinder.DeathRecipient f5261j = new IBinder.DeathRecipient() { // from class: com.google.android.play.integrity.internal.q @Override // android.os.IBinder.DeathRecipient public final void binderDied() { y.h(y.this); } }; /* renamed from: k, reason: collision with root package name */ private final AtomicInteger f5262k = new AtomicInteger(0); /* renamed from: c, reason: collision with root package name */ private final String f5254c = "IntegrityService"; /* renamed from: i, reason: collision with root package name */ private final WeakReference f5260i = new WeakReference(null); public y(Context context, n nVar, String str, Intent intent, com.google.android.play.core.integrity.q qVar, t tVar, byte[] bArr) { this.f5252a = context; this.f5253b = nVar; this.f5259h = intent; this.f5265n = qVar; } public static /* synthetic */ void h(y yVar) { yVar.f5253b.d("reportBinderDeath", new Object[0]); androidx.appcompat.app.m.a(yVar.f5260i.get()); yVar.f5253b.d("%s : Binder has died.", yVar.f5254c); Iterator it = yVar.f5255d.iterator(); while (it.hasNext()) { ((o) it.next()).a(yVar.s()); } yVar.f5255d.clear(); yVar.t(); } static /* bridge */ /* synthetic */ void m(y yVar, o oVar) { if (yVar.f5264m != null || yVar.f5258g) { if (!yVar.f5258g) { oVar.run(); return; } else { yVar.f5253b.d("Waiting to bind to the service.", new Object[0]); yVar.f5255d.add(oVar); return; } } yVar.f5253b.d("Initiate binding to the service.", new Object[0]); yVar.f5255d.add(oVar); x xVar = new x(yVar, null); yVar.f5263l = xVar; yVar.f5258g = true; if (yVar.f5252a.bindService(yVar.f5259h, xVar, 1)) { return; } yVar.f5253b.d("Failed to bind to the service.", new Object[0]); yVar.f5258g = false; Iterator it = yVar.f5255d.iterator(); while (it.hasNext()) { ((o) it.next()).a(new z()); } yVar.f5255d.clear(); } static /* bridge */ /* synthetic */ void n(y yVar) { yVar.f5253b.d("linkToDeath", new Object[0]); try { yVar.f5264m.asBinder().linkToDeath(yVar.f5261j, 0); } catch (RemoteException e10) { yVar.f5253b.c(e10, "linkToDeath failed", new Object[0]); } } static /* bridge */ /* synthetic */ void o(y yVar) { yVar.f5253b.d("unlinkToDeath", new Object[0]); yVar.f5264m.asBinder().unlinkToDeath(yVar.f5261j, 0); } private final RemoteException s() { return new RemoteException(String.valueOf(this.f5254c).concat(" : Binder has died.")); } /* JADX INFO: Access modifiers changed from: private */ public final void t() { synchronized (this.f5257f) { Iterator it = this.f5256e.iterator(); while (it.hasNext()) { ((TaskCompletionSource) it.next()).trySetException(s()); } this.f5256e.clear(); } } public final Handler c() { Handler handler; Map map = f5251o; synchronized (map) { if (!map.containsKey(this.f5254c)) { HandlerThread handlerThread = new HandlerThread(this.f5254c, 10); handlerThread.start(); map.put(this.f5254c, new Handler(handlerThread.getLooper())); } handler = (Handler) map.get(this.f5254c); } return handler; } public final IInterface e() { return this.f5264m; } public final void p(o oVar, final TaskCompletionSource taskCompletionSource) { synchronized (this.f5257f) { this.f5256e.add(taskCompletionSource); taskCompletionSource.getTask().addOnCompleteListener(new OnCompleteListener() { // from class: com.google.android.play.integrity.internal.p @Override // com.google.android.gms.tasks.OnCompleteListener public final void onComplete(Task task) { y.this.q(taskCompletionSource, task); } }); } synchronized (this.f5257f) { if (this.f5262k.getAndIncrement() > 0) { this.f5253b.a("Already connected to the service.", new Object[0]); } } c().post(new r(this, oVar.c(), oVar)); } final /* synthetic */ void q(TaskCompletionSource taskCompletionSource, Task task) { synchronized (this.f5257f) { this.f5256e.remove(taskCompletionSource); } } public final void r(TaskCompletionSource taskCompletionSource) { synchronized (this.f5257f) { this.f5256e.remove(taskCompletionSource); } synchronized (this.f5257f) { if (this.f5262k.get() > 0 && this.f5262k.decrementAndGet() > 0) { this.f5253b.d("Leaving the connection open for other ongoing calls.", new Object[0]); } else { c().post(new s(this)); } } } }