package o;

import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import androidx.work.impl.foreground.SystemForegroundService;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

/* compiled from: r8-map-id-3b7cc9102578c509342fa2f0fabed2bfc72239cd258d579fc679bfcbc198b048 */
/* renamed from: o.ic0, reason: case insensitive filesystem */
/* loaded from: classes.dex */
public final class C1493ic0 implements JT, InterfaceC0645Yw {
    public static final /* synthetic */ int u = 0;
    public final C2651um0 l;
    public final C2062oc0 m;
    public final Object n = new Object();

    /* renamed from: o, reason: collision with root package name */
    public C2366rm0 f583o;
    public final LinkedHashMap p;
    public final HashMap q;
    public final HashMap r;
    public final C1696kj0 s;
    public SystemForegroundService t;

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

    public C1493ic0(Context context) {
        C2651um0 c2651um0H = C2651um0.H(context);
        this.l = c2651um0H;
        this.m = c2651um0H.n;
        this.f583o = null;
        this.p = new LinkedHashMap();
        this.r = new HashMap();
        this.q = new HashMap();
        this.s = new C1696kj0(c2651um0H.t);
        c2651um0H.p.a(this);
    }

    public static Intent a(Context context, C2366rm0 c2366rm0, UA ua) {
        Intent intent = new Intent(context, (Class<?>) SystemForegroundService.class);
        intent.setAction("ACTION_NOTIFY");
        intent.putExtra("KEY_NOTIFICATION_ID", ua.a);
        intent.putExtra("KEY_FOREGROUND_SERVICE_TYPE", ua.b);
        intent.putExtra("KEY_NOTIFICATION", ua.c);
        intent.putExtra("KEY_WORKSPEC_ID", c2366rm0.a);
        intent.putExtra("KEY_GENERATION", c2366rm0.b);
        return intent;
    }

    public static Intent d(Context context, C2366rm0 c2366rm0, UA ua) {
        Intent intent = new Intent(context, (Class<?>) SystemForegroundService.class);
        intent.setAction("ACTION_START_FOREGROUND");
        intent.putExtra("KEY_WORKSPEC_ID", c2366rm0.a);
        intent.putExtra("KEY_GENERATION", c2366rm0.b);
        intent.putExtra("KEY_NOTIFICATION_ID", ua.a);
        intent.putExtra("KEY_FOREGROUND_SERVICE_TYPE", ua.b);
        intent.putExtra("KEY_NOTIFICATION", ua.c);
        return intent;
    }

    @Override // o.InterfaceC0645Yw
    public final void b(C2366rm0 c2366rm0, boolean z) {
        Map.Entry entry;
        synchronized (this.n) {
            try {
                TI ti = ((Dm0) this.q.remove(c2366rm0)) != null ? (TI) this.r.remove(c2366rm0) : null;
                if (ti != null) {
                    ti.c(null);
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        UA ua = (UA) this.p.remove(c2366rm0);
        if (c2366rm0.equals(this.f583o)) {
            if (this.p.size() > 0) {
                Iterator it = this.p.entrySet().iterator();
                Object next = it.next();
                while (true) {
                    entry = (Map.Entry) next;
                    if (!it.hasNext()) {
                        break;
                    } else {
                        next = it.next();
                    }
                }
                this.f583o = (C2366rm0) entry.getKey();
                if (this.t != null) {
                    UA ua2 = (UA) entry.getValue();
                    SystemForegroundService systemForegroundService = this.t;
                    systemForegroundService.m.post(new RunnableC1587jc0(systemForegroundService, ua2.a, ua2.c, ua2.b));
                    SystemForegroundService systemForegroundService2 = this.t;
                    systemForegroundService2.m.post(new RunnableC1212fg(ua2.a, 3, systemForegroundService2));
                }
            } else {
                this.f583o = null;
            }
        }
        SystemForegroundService systemForegroundService3 = this.t;
        if (ua == null || systemForegroundService3 == null) {
            return;
        }
        C0669Zu c0669ZuE = C0669Zu.e();
        c2366rm0.toString();
        c0669ZuE.getClass();
        systemForegroundService3.m.post(new RunnableC1212fg(ua.a, 3, systemForegroundService3));
    }

    @Override // o.JT
    public final void c(Dm0 dm0, AbstractC1320gm abstractC1320gm) {
        if (abstractC1320gm instanceof C1224fm) {
            C0669Zu.e().getClass();
            C2366rm0 c2366rm0O = C2887xC.o(dm0);
            C2651um0 c2651um0 = this.l;
            c2651um0.n.c(new Oa0(c2651um0.p, new C2627ua0(c2366rm0O), true, -512));
        }
    }

    public final void e(Intent intent) {
        int i = 0;
        int intExtra = intent.getIntExtra("KEY_NOTIFICATION_ID", 0);
        int intExtra2 = intent.getIntExtra("KEY_FOREGROUND_SERVICE_TYPE", 0);
        C2366rm0 c2366rm0 = new C2366rm0(intent.getStringExtra("KEY_WORKSPEC_ID"), intent.getIntExtra("KEY_GENERATION", 0));
        Notification notification = (Notification) intent.getParcelableExtra("KEY_NOTIFICATION");
        C0669Zu.e().getClass();
        if (notification == null || this.t == null) {
            return;
        }
        UA ua = new UA(intExtra, notification, intExtra2);
        LinkedHashMap linkedHashMap = this.p;
        linkedHashMap.put(c2366rm0, ua);
        if (this.f583o == null) {
            this.f583o = c2366rm0;
            SystemForegroundService systemForegroundService = this.t;
            systemForegroundService.m.post(new RunnableC1587jc0(systemForegroundService, intExtra, notification, intExtra2));
            return;
        }
        SystemForegroundService systemForegroundService2 = this.t;
        systemForegroundService2.m.post(new RunnableC2786w8(systemForegroundService2, intExtra, notification));
        if (intExtra2 == 0 || Build.VERSION.SDK_INT < 29) {
            return;
        }
        Iterator it = linkedHashMap.entrySet().iterator();
        while (it.hasNext()) {
            i |= ((UA) ((Map.Entry) it.next()).getValue()).b;
        }
        UA ua2 = (UA) linkedHashMap.get(this.f583o);
        if (ua2 != null) {
            SystemForegroundService systemForegroundService3 = this.t;
            systemForegroundService3.m.post(new RunnableC1587jc0(systemForegroundService3, ua2.a, ua2.c, i));
        }
    }

    public final void f() {
        this.t = null;
        synchronized (this.n) {
            try {
                Iterator it = this.r.values().iterator();
                while (it.hasNext()) {
                    ((TI) it.next()).c(null);
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        this.l.p.e(this);
    }
}
