package s9; import android.content.Context; import android.os.PowerManager; import android.os.WorkSource; import android.text.TextUtils; import android.util.Log; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import k9.b; import k9.h; import k9.i; import u8.q; import z8.d; import z8.e; import z8.l; import z8.n; /* compiled from: com.google.android.gms:play-services-stats@@17.0.1 */ /* loaded from: classes4.dex */ public class a { /* renamed from: r reason: collision with root package name */ private static final long f22604r = TimeUnit.DAYS.toMillis(366); /* renamed from: s reason: collision with root package name */ private static volatile ScheduledExecutorService f22605s = null; /* renamed from: t reason: collision with root package name */ private static final Object f22606t = new Object(); /* renamed from: u reason: collision with root package name */ private static volatile e f22607u = new c(); /* renamed from: b reason: collision with root package name */ private final PowerManager.WakeLock f22609b; /* renamed from: d reason: collision with root package name */ private Future f22611d; /* renamed from: e reason: collision with root package name */ private long f22612e; /* renamed from: h reason: collision with root package name */ private int f22615h; /* renamed from: k reason: collision with root package name */ private WorkSource f22618k; /* renamed from: l reason: collision with root package name */ private final String f22619l; /* renamed from: m reason: collision with root package name */ private final String f22620m; /* renamed from: n reason: collision with root package name */ private final Context f22621n; /* renamed from: q reason: collision with root package name */ private final ScheduledExecutorService f22624q; /* renamed from: a reason: collision with root package name */ private final Object f22608a = new Object(); /* renamed from: c reason: collision with root package name */ private int f22610c = 0; /* renamed from: f reason: collision with root package name */ private final Set f22613f = new HashSet(); /* renamed from: g reason: collision with root package name */ private boolean f22614g = true; /* renamed from: j reason: collision with root package name */ private d f22617j = e.c(); /* renamed from: o reason: collision with root package name */ private final Map f22622o = new HashMap(); /* renamed from: p reason: collision with root package name */ private AtomicInteger f22623p = new AtomicInteger(0); /* renamed from: i reason: collision with root package name */ b f22616i = null; public a(Context context, int i10, String str) { String packageName = context.getPackageName(); q.k(context, "WakeLock: context must not be null"); q.g(str, "WakeLock: wakeLockName must not be empty"); this.f22621n = context.getApplicationContext(); this.f22620m = str; if (!"com.google.android.gms".equals(context.getPackageName())) { String valueOf = String.valueOf(str); this.f22619l = valueOf.length() != 0 ? "*gcore*:".concat(valueOf) : new String("*gcore*:"); } else { this.f22619l = str; } PowerManager powerManager = (PowerManager) context.getSystemService("power"); if (powerManager != null) { PowerManager.WakeLock newWakeLock = powerManager.newWakeLock(i10, str); this.f22609b = newWakeLock; if (n.c(context)) { WorkSource b10 = n.b(context, l.a(packageName) ? context.getPackageName() : packageName); this.f22618k = b10; if (b10 != null) { i(newWakeLock, b10); } } ScheduledExecutorService scheduledExecutorService = f22605s; if (scheduledExecutorService == null) { synchronized (f22606t) { scheduledExecutorService = f22605s; if (scheduledExecutorService == null) { h.a(); scheduledExecutorService = Executors.unconfigurableScheduledExecutorService(Executors.newScheduledThreadPool(1)); f22605s = scheduledExecutorService; } } } this.f22624q = scheduledExecutorService; return; } StringBuilder sb2 = new StringBuilder(29); sb2.append((CharSequence) "expected a non-null reference", 0, 29); throw new i(sb2.toString()); } public static /* synthetic */ void e(a aVar) { synchronized (aVar.f22608a) { if (aVar.b()) { Log.e("WakeLock", String.valueOf(aVar.f22619l).concat(" ** IS FORCE-RELEASED ON TIMEOUT **")); aVar.g(); if (aVar.b()) { aVar.f22610c = 1; aVar.h(0); } } } } private final String f(String str) { if (this.f22614g) { TextUtils.isEmpty(null); } return null; } private final void g() { if (!this.f22613f.isEmpty()) { ArrayList arrayList = new ArrayList(this.f22613f); this.f22613f.clear(); if (arrayList.size() > 0) { f fVar = (f) arrayList.get(0); throw null; } } } private final void h(int i10) { synchronized (this.f22608a) { if (b()) { if (this.f22614g) { int i11 = this.f22610c - 1; this.f22610c = i11; if (i11 > 0) { return; } } else { this.f22610c = 0; } g(); for (d dVar : this.f22622o.values()) { dVar.f22626a = 0; } this.f22622o.clear(); Future future = this.f22611d; if (future != null) { future.cancel(false); this.f22611d = null; this.f22612e = 0L; } this.f22615h = 0; if (this.f22609b.isHeld()) { try { this.f22609b.release(); if (this.f22616i != null) { this.f22616i = null; } } catch (RuntimeException e10) { if (e10.getClass().equals(RuntimeException.class)) { Log.e("WakeLock", String.valueOf(this.f22619l).concat(" failed to release!"), e10); if (this.f22616i != null) { this.f22616i = null; } } else { throw e10; } } } else { Log.e("WakeLock", String.valueOf(this.f22619l).concat(" should be held!")); } } } } private static void i(PowerManager.WakeLock wakeLock, WorkSource workSource) { try { wakeLock.setWorkSource(workSource); } catch (ArrayIndexOutOfBoundsException | IllegalArgumentException e10) { Log.wtf("WakeLock", e10.toString()); } } public void a(long j10) { this.f22623p.incrementAndGet(); long j11 = Long.MAX_VALUE; long max = Math.max(Math.min(Long.MAX_VALUE, f22604r), 1L); if (j10 > 0) { max = Math.min(j10, max); } synchronized (this.f22608a) { if (!b()) { this.f22616i = b.a(false, null); this.f22609b.acquire(); this.f22617j.b(); } this.f22610c++; this.f22615h++; f(null); d dVar = this.f22622o.get(null); if (dVar == null) { dVar = new d(null); this.f22622o.put(null, dVar); } dVar.f22626a++; long b10 = this.f22617j.b(); if (Long.MAX_VALUE - b10 > max) { j11 = b10 + max; } if (j11 > this.f22612e) { this.f22612e = j11; Future future = this.f22611d; if (future != null) { future.cancel(false); } this.f22611d = this.f22624q.schedule(new b(this), max, TimeUnit.MILLISECONDS); } } } public boolean b() { boolean z10; synchronized (this.f22608a) { z10 = this.f22610c > 0; } return z10; } public void c() { if (this.f22623p.decrementAndGet() < 0) { Log.e("WakeLock", String.valueOf(this.f22619l).concat(" release without a matched acquire!")); } synchronized (this.f22608a) { f(null); if (this.f22622o.containsKey(null)) { d dVar = this.f22622o.get(null); if (dVar != null) { int i10 = dVar.f22626a - 1; dVar.f22626a = i10; if (i10 == 0) { this.f22622o.remove(null); } } } else { Log.w("WakeLock", String.valueOf(this.f22619l).concat(" counter does not exist")); } h(0); } } public void d(boolean z10) { synchronized (this.f22608a) { this.f22614g = z10; } } }