package v8;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.PowerManager;
import android.os.SystemClock;
import android.os.WorkSource;
import android.text.TextUtils;
import android.util.Log;
import com.google.android.gms.common.api.internal.i0;
import com.google.android.gms.common.internal.w;
import com.google.android.gms.internal.stats.zzi;
import com.google.android.gms.measurement.internal.u1;
import i8.d;
import i8.h;
import i8.i;
import j8.e;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/* JADX INFO: loaded from: classes2.dex */
public final class a {

    /* JADX INFO: renamed from: o, reason: collision with root package name */
    public static final long f33743o = TimeUnit.DAYS.toMillis(366);

    /* JADX INFO: renamed from: p, reason: collision with root package name */
    public static volatile ScheduledExecutorService f33744p = null;

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

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final PowerManager.WakeLock f33747b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public int f33748c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public ScheduledFuture f33749d;

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

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

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

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public q8.a f33753h;

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

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

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

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public final HashMap f33757l;

    /* JADX INFO: renamed from: m, reason: collision with root package name */
    public final AtomicInteger f33758m;

    /* JADX INFO: renamed from: n, reason: collision with root package name */
    public final ScheduledExecutorService f33759n;

    public a(Context context) {
        boolean zBooleanValue;
        String packageName = context.getPackageName();
        this.f33746a = new Object();
        this.f33748c = 0;
        this.f33751f = new HashSet();
        this.f33752g = true;
        this.f33754i = d.f26577a;
        this.f33757l = new HashMap();
        this.f33758m = new AtomicInteger(0);
        w.checkNotEmpty("wake:com.google.firebase.iid.WakeLockHolder", "WakeLock: wakeLockName must not be empty");
        this.f33756k = context.getApplicationContext();
        WorkSource workSource = null;
        this.f33753h = null;
        if ("com.google.android.gms".equals(context.getPackageName())) {
            this.f33755j = "wake:com.google.firebase.iid.WakeLockHolder";
        } else {
            this.f33755j = "wake:com.google.firebase.iid.WakeLockHolder".length() != 0 ? "*gcore*:".concat("wake:com.google.firebase.iid.WakeLockHolder") : new String("*gcore*:");
        }
        PowerManager powerManager = (PowerManager) context.getSystemService("power");
        if (powerManager == null) {
            StringBuilder sb = new StringBuilder(29);
            sb.append((CharSequence) "expected a non-null reference", 0, 29);
            throw new zzi(sb.toString());
        }
        this.f33747b = powerManager.newWakeLock(1, "wake:com.google.firebase.iid.WakeLockHolder");
        Method method = i.f26583a;
        synchronized (i.class) {
            Boolean bool = i.f26585c;
            if (bool != null) {
                zBooleanValue = bool.booleanValue();
            } else {
                Boolean boolValueOf = Boolean.valueOf(g0.d.a(context, "android.permission.UPDATE_DEVICE_STATS") == 0);
                i.f26585c = boolValueOf;
                zBooleanValue = boolValueOf.booleanValue();
            }
        }
        if (zBooleanValue) {
            packageName = h.isEmptyOrWhitespace(packageName) ? context.getPackageName() : packageName;
            if (context.getPackageManager() != null && packageName != null) {
                try {
                    ApplicationInfo applicationInfoA = e.a(context).a(0, packageName);
                    if (applicationInfoA == null) {
                        "Could not get applicationInfo from package: ".concat(packageName);
                    } else {
                        int i10 = applicationInfoA.uid;
                        workSource = new WorkSource();
                        Method method2 = i.f26584b;
                        if (method2 != null) {
                            try {
                                method2.invoke(workSource, Integer.valueOf(i10), packageName);
                            } catch (Exception e10) {
                                Log.wtf("WorkSourceUtil", "Unable to assign blame through WorkSource", e10);
                            }
                        } else {
                            Method method3 = i.f26583a;
                            if (method3 != null) {
                                try {
                                    method3.invoke(workSource, Integer.valueOf(i10));
                                } catch (Exception e11) {
                                    Log.wtf("WorkSourceUtil", "Unable to assign blame through WorkSource", e11);
                                }
                            }
                        }
                    }
                } catch (PackageManager.NameNotFoundException unused) {
                    "Could not find package: ".concat(packageName);
                }
            }
            if (workSource != null) {
                try {
                    this.f33747b.setWorkSource(workSource);
                } catch (ArrayIndexOutOfBoundsException | IllegalArgumentException e12) {
                    Log.wtf("WakeLock", e12.toString());
                }
            }
        }
        ScheduledExecutorService scheduledExecutorServiceUnconfigurableScheduledExecutorService = f33744p;
        if (scheduledExecutorServiceUnconfigurableScheduledExecutorService == null) {
            synchronized (f33745q) {
                try {
                    scheduledExecutorServiceUnconfigurableScheduledExecutorService = f33744p;
                    if (scheduledExecutorServiceUnconfigurableScheduledExecutorService == null) {
                        scheduledExecutorServiceUnconfigurableScheduledExecutorService = Executors.unconfigurableScheduledExecutorService(Executors.newScheduledThreadPool(1));
                        f33744p = scheduledExecutorServiceUnconfigurableScheduledExecutorService;
                    }
                } finally {
                }
            }
        }
        this.f33759n = scheduledExecutorServiceUnconfigurableScheduledExecutorService;
    }

    public final void a(long j10) {
        this.f33758m.incrementAndGet();
        long jMax = Math.max(Math.min(Long.MAX_VALUE, f33743o), 1L);
        if (j10 > 0) {
            jMax = Math.min(j10, jMax);
        }
        synchronized (this.f33746a) {
            try {
                if (!b()) {
                    this.f33753h = q8.a.zza(false, null);
                    this.f33747b.acquire();
                    this.f33754i.getClass();
                    SystemClock.elapsedRealtime();
                }
                this.f33748c++;
                if (this.f33752g) {
                    TextUtils.isEmpty(null);
                }
                b bVar = (b) this.f33757l.get(null);
                if (bVar == null) {
                    bVar = new b();
                    this.f33757l.put(null, bVar);
                }
                bVar.f33760a++;
                this.f33754i.getClass();
                long jElapsedRealtime = SystemClock.elapsedRealtime();
                long j11 = Long.MAX_VALUE - jElapsedRealtime > jMax ? jElapsedRealtime + jMax : Long.MAX_VALUE;
                if (j11 > this.f33750e) {
                    this.f33750e = j11;
                    ScheduledFuture scheduledFuture = this.f33749d;
                    if (scheduledFuture != null) {
                        scheduledFuture.cancel(false);
                    }
                    this.f33749d = this.f33759n.schedule(new i0(this, 3), jMax, TimeUnit.MILLISECONDS);
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final boolean b() {
        boolean z10;
        synchronized (this.f33746a) {
            z10 = this.f33748c > 0;
        }
        return z10;
    }

    public final void c() {
        if (this.f33758m.decrementAndGet() < 0) {
            String.valueOf(this.f33755j).concat(" release without a matched acquire!");
        }
        synchronized (this.f33746a) {
            try {
                if (this.f33752g) {
                    TextUtils.isEmpty(null);
                }
                if (this.f33757l.containsKey(null)) {
                    b bVar = (b) this.f33757l.get(null);
                    if (bVar != null) {
                        int i10 = bVar.f33760a - 1;
                        bVar.f33760a = i10;
                        if (i10 == 0) {
                            this.f33757l.remove(null);
                        }
                    }
                } else {
                    String.valueOf(this.f33755j).concat(" counter does not exist");
                }
                e();
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void d() {
        HashSet hashSet = this.f33751f;
        if (hashSet.isEmpty()) {
            return;
        }
        ArrayList arrayList = new ArrayList(hashSet);
        hashSet.clear();
        if (arrayList.size() <= 0) {
            return;
        }
        u1.x(arrayList.get(0));
        throw null;
    }

    public final void e() {
        synchronized (this.f33746a) {
            try {
                if (b()) {
                    if (this.f33752g) {
                        int i10 = this.f33748c - 1;
                        this.f33748c = i10;
                        if (i10 > 0) {
                            return;
                        }
                    } else {
                        this.f33748c = 0;
                    }
                    d();
                    Iterator it = this.f33757l.values().iterator();
                    while (it.hasNext()) {
                        ((b) it.next()).f33760a = 0;
                    }
                    this.f33757l.clear();
                    ScheduledFuture scheduledFuture = this.f33749d;
                    if (scheduledFuture != null) {
                        scheduledFuture.cancel(false);
                        this.f33749d = null;
                        this.f33750e = 0L;
                    }
                    if (this.f33747b.isHeld()) {
                        try {
                            try {
                                this.f33747b.release();
                                if (this.f33753h != null) {
                                    this.f33753h = null;
                                }
                            } catch (RuntimeException e10) {
                                if (!e10.getClass().equals(RuntimeException.class)) {
                                    throw e10;
                                }
                                String.valueOf(this.f33755j).concat(" failed to release!");
                                if (this.f33753h != null) {
                                    this.f33753h = null;
                                }
                            }
                        } catch (Throwable th) {
                            if (this.f33753h != null) {
                                this.f33753h = null;
                            }
                            throw th;
                        }
                    } else {
                        String.valueOf(this.f33755j).concat(" should be held!");
                    }
                }
            } catch (Throwable th2) {
                throw th2;
            }
        }
    }
}
