package o;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.work.impl.WorkDatabase;
import androidx.work.impl.WorkDatabase_Impl;
import androidx.work.impl.background.systemalarm.ConstraintProxyUpdateReceiver;
import androidx.work.impl.background.systemalarm.SystemAlarmService;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* renamed from: o.fj, reason: case insensitive filesystem */
/* loaded from: classes.dex */
public final class C1218fj implements InterfaceC0645Yw {
    public static final /* synthetic */ int q = 0;
    public final Context l;
    public final HashMap m = new HashMap();
    public final Object n = new Object();

    /* renamed from: o, reason: collision with root package name */
    public final BP f529o;
    public final C3031ym0 p;

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

    public C1218fj(Context context, BP bp, C3031ym0 c3031ym0) {
        this.l = context;
        this.f529o = bp;
        this.p = c3031ym0;
    }

    public static C2366rm0 c(Intent intent) {
        return new C2366rm0(intent.getStringExtra("KEY_WORKSPEC_ID"), intent.getIntExtra("KEY_WORKSPEC_GENERATION", 0));
    }

    public static void d(Intent intent, C2366rm0 c2366rm0) {
        intent.putExtra("KEY_WORKSPEC_ID", c2366rm0.a);
        intent.putExtra("KEY_WORKSPEC_GENERATION", c2366rm0.b);
    }

    public final void a(Intent intent, int i, C1205fc0 c1205fc0) {
        List<C2627ua0> listC;
        String action = intent.getAction();
        int i2 = 2;
        int i3 = 0;
        if ("ACTION_CONSTRAINTS_CHANGED".equals(action)) {
            C0669Zu c0669ZuE = C0669Zu.e();
            Objects.toString(intent);
            c0669ZuE.getClass();
            Context context = this.l;
            C0746am c0746am = new C0746am(context, this.f529o, i, c1205fc0);
            ArrayList arrayListE = c1205fc0.p.m.s().e();
            int i4 = AbstractC0219Il.a;
            int size = arrayListE.size();
            boolean z = false;
            boolean z2 = false;
            boolean z3 = false;
            boolean z4 = false;
            int i5 = 0;
            while (i5 < size) {
                Object obj = arrayListE.get(i5);
                i5++;
                C0608Xl c0608Xl = ((Dm0) obj).j;
                z |= c0608Xl.d;
                z2 |= c0608Xl.b;
                z3 |= c0608Xl.e;
                z4 |= c0608Xl.a != 1;
                if (z && z2 && z3 && z4) {
                    break;
                }
            }
            int i6 = ConstraintProxyUpdateReceiver.a;
            Intent intent2 = new Intent("androidx.work.impl.background.systemalarm.UpdateProxies");
            intent2.setComponent(new ComponentName(context, (Class<?>) ConstraintProxyUpdateReceiver.class));
            intent2.putExtra("KEY_BATTERY_NOT_LOW_PROXY_ENABLED", z).putExtra("KEY_BATTERY_CHARGING_PROXY_ENABLED", z2).putExtra("KEY_STORAGE_NOT_LOW_PROXY_ENABLED", z3).putExtra("KEY_NETWORK_STATE_PROXY_ENABLED", z4);
            context.sendBroadcast(intent2);
            ArrayList arrayList = new ArrayList(arrayListE.size());
            c0746am.a.getClass();
            long jCurrentTimeMillis = System.currentTimeMillis();
            int size2 = arrayListE.size();
            int i7 = 0;
            while (i7 < size2) {
                Object obj2 = arrayListE.get(i7);
                i7++;
                Dm0 dm0 = (Dm0) obj2;
                if (jCurrentTimeMillis >= dm0.a() && (!dm0.b() || c0746am.c.g(dm0))) {
                    arrayList.add(dm0);
                }
            }
            int size3 = arrayList.size();
            while (i3 < size3) {
                Object obj3 = arrayList.get(i3);
                i3++;
                Dm0 dm02 = (Dm0) obj3;
                String str = dm02.a;
                C2366rm0 c2366rm0O = C2887xC.o(dm02);
                Intent intent3 = new Intent(context, (Class<?>) SystemAlarmService.class);
                intent3.setAction("ACTION_DELAY_MET");
                d(intent3, c2366rm0O);
                C0669Zu.e().getClass();
                ((ExecutorC1921n20) c1205fc0.m.d).execute(new RunnableC2786w8(c0746am.b, i2, c1205fc0, intent3));
            }
            return;
        }
        if ("ACTION_RESCHEDULE".equals(action)) {
            C0669Zu c0669ZuE2 = C0669Zu.e();
            Objects.toString(intent);
            c0669ZuE2.getClass();
            c1205fc0.p.J();
            return;
        }
        Bundle extras = intent.getExtras();
        String[] strArr = {"KEY_WORKSPEC_ID"};
        if (extras == null || extras.isEmpty() || extras.get(strArr[0]) == null) {
            C0669Zu.e().getClass();
            return;
        }
        if ("ACTION_SCHEDULE_WORK".equals(action)) {
            Context context2 = this.l;
            C2366rm0 c2366rm0C = c(intent);
            C0669Zu c0669ZuE3 = C0669Zu.e();
            c2366rm0C.toString();
            c0669ZuE3.getClass();
            WorkDatabase workDatabase = c1205fc0.p.m;
            workDatabase.c();
            try {
                Dm0 dm0H = workDatabase.s().h(c2366rm0C.a);
                if (dm0H == null) {
                    C0669Zu c0669ZuE4 = C0669Zu.e();
                    c2366rm0C.toString();
                    c0669ZuE4.getClass();
                    return;
                }
                if (Ag0.a(dm0H.b)) {
                    C0669Zu c0669ZuE5 = C0669Zu.e();
                    c2366rm0C.toString();
                    c0669ZuE5.getClass();
                    return;
                }
                long jA = dm0H.a();
                if (dm0H.b()) {
                    C0669Zu c0669ZuE6 = C0669Zu.e();
                    c2366rm0C.toString();
                    c0669ZuE6.getClass();
                    AbstractC0680a2.b(context2, workDatabase, c2366rm0C, jA);
                    Intent intent4 = new Intent(context2, (Class<?>) SystemAlarmService.class);
                    intent4.setAction("ACTION_CONSTRAINTS_CHANGED");
                    ((ExecutorC1921n20) c1205fc0.m.d).execute(new RunnableC2786w8(i, i2, c1205fc0, intent4));
                } else {
                    C0669Zu c0669ZuE7 = C0669Zu.e();
                    c2366rm0C.toString();
                    c0669ZuE7.getClass();
                    AbstractC0680a2.b(context2, workDatabase, c2366rm0C, jA);
                }
                workDatabase.n();
                return;
            } finally {
                workDatabase.j();
            }
        }
        if ("ACTION_DELAY_MET".equals(action)) {
            synchronized (this.n) {
                try {
                    C2366rm0 c2366rm0C2 = c(intent);
                    C0669Zu c0669ZuE8 = C0669Zu.e();
                    c2366rm0C2.toString();
                    c0669ZuE8.getClass();
                    if (this.m.containsKey(c2366rm0C2)) {
                        C0669Zu c0669ZuE9 = C0669Zu.e();
                        c2366rm0C2.toString();
                        c0669ZuE9.getClass();
                    } else {
                        C0483Sq c0483Sq = new C0483Sq(this.l, i, c1205fc0, this.p.f(c2366rm0C2));
                        this.m.put(c2366rm0C2, c0483Sq);
                        c0483Sq.e();
                    }
                } finally {
                }
            }
            return;
        }
        if (!"ACTION_STOP_WORK".equals(action)) {
            if (!"ACTION_EXECUTION_COMPLETED".equals(action)) {
                C0669Zu c0669ZuE10 = C0669Zu.e();
                intent.toString();
                c0669ZuE10.getClass();
                return;
            } else {
                C2366rm0 c2366rm0C3 = c(intent);
                boolean z5 = intent.getExtras().getBoolean("KEY_NEEDS_RESCHEDULE");
                C0669Zu c0669ZuE11 = C0669Zu.e();
                intent.toString();
                c0669ZuE11.getClass();
                b(c2366rm0C3, z5);
                return;
            }
        }
        C3031ym0 c3031ym0 = this.p;
        Bundle extras2 = intent.getExtras();
        String string = extras2.getString("KEY_WORKSPEC_ID");
        if (extras2.containsKey("KEY_WORKSPEC_GENERATION")) {
            int i8 = extras2.getInt("KEY_WORKSPEC_GENERATION");
            ArrayList arrayList2 = new ArrayList(1);
            C2627ua0 c2627ua0E = c3031ym0.e(new C2366rm0(string, i8));
            listC = arrayList2;
            if (c2627ua0E != null) {
                arrayList2.add(c2627ua0E);
                listC = arrayList2;
            }
        } else {
            listC = c3031ym0.c(string);
        }
        for (C2627ua0 c2627ua0 : listC) {
            C0669Zu.e().getClass();
            c1205fc0.u.E(c2627ua0, -512);
            C2366rm0 c2366rm0 = c2627ua0.a;
            Context context3 = this.l;
            WorkDatabase workDatabase2 = c1205fc0.p.m;
            int i9 = AbstractC0680a2.a;
            C2062oc0 c2062oc0O = workDatabase2.o();
            C1872mc0 c1872mc0D = c2062oc0O.d(c2366rm0);
            if (c1872mc0D != null) {
                AbstractC0680a2.a(context3, c2366rm0, c1872mc0D.c);
                C0669Zu c0669ZuE12 = C0669Zu.e();
                c2366rm0.toString();
                c0669ZuE12.getClass();
                String str2 = c2366rm0.a;
                int i10 = c2366rm0.b;
                WorkDatabase_Impl workDatabase_Impl = (WorkDatabase_Impl) c2062oc0O.a;
                workDatabase_Impl.b();
                C1967nc0 c1967nc0 = (C1967nc0) c2062oc0O.c;
                LC lcA = c1967nc0.a();
                if (str2 == null) {
                    lcA.r(1);
                } else {
                    lcA.k(1, str2);
                }
                lcA.B(2, i10);
                workDatabase_Impl.c();
                try {
                    lcA.b();
                    workDatabase_Impl.n();
                } finally {
                    workDatabase_Impl.j();
                    c1967nc0.c(lcA);
                }
            }
            c1205fc0.b(c2366rm0, false);
        }
    }

    @Override // o.InterfaceC0645Yw
    public final void b(C2366rm0 c2366rm0, boolean z) {
        synchronized (this.n) {
            try {
                C0483Sq c0483Sq = (C0483Sq) this.m.remove(c2366rm0);
                this.p.e(c2366rm0);
                if (c0483Sq != null) {
                    c0483Sq.f(z);
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }
}
