package o;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.PowerManager;
import androidx.work.impl.WorkDatabase;
import androidx.work.impl.foreground.SystemForegroundService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* loaded from: classes.dex */
public final class XY implements WA {
    public final Context b;
    public final C1414hl c;
    public final C2062oc0 d;
    public final WorkDatabase e;
    public final HashMap g = new HashMap();
    public final HashMap f = new HashMap();
    public final HashSet i = new HashSet();
    public final ArrayList j = new ArrayList();
    public PowerManager.WakeLock a = null;
    public final Object k = new Object();
    public final HashMap h = new HashMap();

    static {
        C0669Zu.l("Processor");
    }

    public XY(Context context, C1414hl c1414hl, C2062oc0 c2062oc0, WorkDatabase workDatabase) {
        this.b = context;
        this.c = c1414hl;
        this.d = c2062oc0;
        this.e = workDatabase;
    }

    public static boolean d(Lm0 lm0, int i) {
        if (lm0 == null) {
            C0669Zu.e().getClass();
            return false;
        }
        lm0.B = i;
        lm0.h();
        lm0.A.cancel(true);
        if (lm0.f214o == null || !(lm0.A.l instanceof E)) {
            Objects.toString(lm0.n);
            C0669Zu.e().getClass();
        } else {
            lm0.f214o.e(i);
        }
        C0669Zu.e().getClass();
        return true;
    }

    public final void a(InterfaceC0645Yw interfaceC0645Yw) {
        synchronized (this.k) {
            this.j.add(interfaceC0645Yw);
        }
    }

    public final Lm0 b(String str) {
        Lm0 lm0 = (Lm0) this.f.remove(str);
        boolean z = lm0 != null;
        if (!z) {
            lm0 = (Lm0) this.g.remove(str);
        }
        this.h.remove(str);
        if (z) {
            synchronized (this.k) {
                try {
                    if (this.f.isEmpty()) {
                        Context context = this.b;
                        int i = C1493ic0.u;
                        Intent intent = new Intent(context, (Class<?>) SystemForegroundService.class);
                        intent.setAction("ACTION_STOP_FOREGROUND");
                        try {
                            this.b.startService(intent);
                        } catch (Throwable unused) {
                            C0669Zu.e().getClass();
                        }
                        PowerManager.WakeLock wakeLock = this.a;
                        if (wakeLock != null) {
                            wakeLock.release();
                            this.a = null;
                        }
                    }
                } finally {
                }
            }
        }
        return lm0;
    }

    public final Lm0 c(String str) {
        Lm0 lm0 = (Lm0) this.f.get(str);
        return lm0 == null ? (Lm0) this.g.get(str) : lm0;
    }

    public final void e(InterfaceC0645Yw interfaceC0645Yw) {
        synchronized (this.k) {
            this.j.remove(interfaceC0645Yw);
        }
    }

    public final void f(String str, UA ua) {
        synchronized (this.k) {
            try {
                C0669Zu.e().getClass();
                Lm0 lm0 = (Lm0) this.g.remove(str);
                if (lm0 != null) {
                    if (this.a == null) {
                        PowerManager.WakeLock wakeLockA = Mk0.a(this.b, "ProcessorForegroundLck");
                        this.a = wakeLockA;
                        wakeLockA.acquire();
                    }
                    this.f.put(str, lm0);
                    Intent intentD = C1493ic0.d(this.b, C2887xC.o(lm0.n), ua);
                    Context context = this.b;
                    if (Build.VERSION.SDK_INT >= 26) {
                        AbstractC1456i7.w(context, intentD);
                    } else {
                        context.startService(intentD);
                    }
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final boolean g(C2627ua0 c2627ua0, BP bp) throws Throwable {
        boolean z;
        C2366rm0 c2366rm0 = c2627ua0.a;
        String str = c2366rm0.a;
        ArrayList arrayList = new ArrayList();
        Dm0 dm0 = (Dm0) this.e.m(new CallableC0852br(this, arrayList, str, 1));
        if (dm0 == null) {
            C0669Zu c0669ZuE = C0669Zu.e();
            c2366rm0.toString();
            c0669ZuE.getClass();
            ((ExecutorC1921n20) this.d.d).execute(new O3(17, this, c2366rm0));
            return false;
        }
        synchronized (this.k) {
            try {
                synchronized (this.k) {
                    try {
                        try {
                            z = c(str) != null;
                        } finally {
                            th = th;
                            while (true) {
                                Throwable th = th;
                                try {
                                } catch (Throwable th2) {
                                    th = th2;
                                }
                            }
                        }
                    } catch (Throwable th3) {
                        th = th3;
                        throw th;
                    }
                }
                try {
                    if (z) {
                        Set set = (Set) this.h.get(str);
                        if (((C2627ua0) set.iterator().next()).a.b == c2366rm0.b) {
                            set.add(c2627ua0);
                            C0669Zu c0669ZuE2 = C0669Zu.e();
                            c2366rm0.toString();
                            c0669ZuE2.getClass();
                        } else {
                            ((ExecutorC1921n20) this.d.d).execute(new O3(17, this, c2366rm0));
                        }
                        return false;
                    }
                    if (dm0.t != c2366rm0.b) {
                        ((ExecutorC1921n20) this.d.d).execute(new O3(17, this, c2366rm0));
                        return false;
                    }
                    Lm0 lm0 = new Lm0(new C0450Rj(this.b, this.c, this.d, this, this.e, dm0, arrayList));
                    C2595u70 c2595u70 = lm0.z;
                    c2595u70.a(new RunnableC2970y5(this, c2595u70, lm0, 3), (ExecutorC1921n20) this.d.d);
                    this.g.put(str, lm0);
                    HashSet hashSet = new HashSet();
                    hashSet.add(c2627ua0);
                    this.h.put(str, hashSet);
                    ((E7) this.d.a).execute(lm0);
                    C0669Zu c0669ZuE3 = C0669Zu.e();
                    c2366rm0.toString();
                    c0669ZuE3.getClass();
                    return true;
                } catch (Throwable th4) {
                    Throwable th5 = th4;
                    throw th5;
                }
            } catch (Throwable th6) {
                th = th6;
            }
        }
    }
}
