package androidx.work.impl.foreground;

import a6.i;
import android.app.Notification;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import androidx.work.impl.WorkDatabase;
import d6.c;
import d6.d;
import h6.r;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import z5.e;
import z5.k;

/* loaded from: classes.dex */
public class a implements c, a6.b {

    /* renamed from: z, reason: collision with root package name */
    static final String f5983z = k.f("SystemFgDispatcher");

    /* renamed from: p, reason: collision with root package name */
    private Context f5984p;

    /* renamed from: q, reason: collision with root package name */
    private i f5985q;

    /* renamed from: r, reason: collision with root package name */
    private final j6.a f5986r;

    /* renamed from: s, reason: collision with root package name */
    final Object f5987s = new Object();

    /* renamed from: t, reason: collision with root package name */
    String f5988t;

    /* renamed from: u, reason: collision with root package name */
    final Map f5989u;

    /* renamed from: v, reason: collision with root package name */
    final Map f5990v;

    /* renamed from: w, reason: collision with root package name */
    final Set f5991w;

    /* renamed from: x, reason: collision with root package name */
    final d f5992x;

    /* renamed from: y, reason: collision with root package name */
    private b f5993y;

    /* JADX INFO: Access modifiers changed from: package-private */
    /* renamed from: androidx.work.impl.foreground.a$a, reason: collision with other inner class name */
    /* loaded from: classes.dex */
    public class RunnableC0150a implements Runnable {

        /* renamed from: p, reason: collision with root package name */
        final /* synthetic */ WorkDatabase f5994p;

        /* renamed from: q, reason: collision with root package name */
        final /* synthetic */ String f5995q;

        RunnableC0150a(WorkDatabase workDatabase, String str) {
            this.f5994p = workDatabase;
            this.f5995q = str;
        }

        @Override // java.lang.Runnable
        public void run() {
            r o10 = this.f5994p.O().o(this.f5995q);
            if (o10 == null || !o10.b()) {
                return;
            }
            synchronized (a.this.f5987s) {
                a.this.f5990v.put(this.f5995q, o10);
                a.this.f5991w.add(o10);
                a aVar = a.this;
                aVar.f5992x.d(aVar.f5991w);
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public interface b {
        void b(int i10, int i11, Notification notification);

        void c(int i10, Notification notification);

        void d(int i10);

        void stop();
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public a(Context context) {
        this.f5984p = context;
        i o10 = i.o(context);
        this.f5985q = o10;
        j6.a u10 = o10.u();
        this.f5986r = u10;
        this.f5988t = null;
        this.f5989u = new LinkedHashMap();
        this.f5991w = new HashSet();
        this.f5990v = new HashMap();
        this.f5992x = new d(this.f5984p, u10, this);
        this.f5985q.q().c(this);
    }

    public static Intent c(Context context, String str, e eVar) {
        Intent intent = new Intent(context, (Class<?>) SystemForegroundService.class);
        intent.setAction("ACTION_NOTIFY");
        intent.putExtra("KEY_NOTIFICATION_ID", eVar.c());
        intent.putExtra("KEY_FOREGROUND_SERVICE_TYPE", eVar.a());
        intent.putExtra("KEY_NOTIFICATION", eVar.b());
        intent.putExtra("KEY_WORKSPEC_ID", str);
        return intent;
    }

    public static Intent d(Context context, String str, e eVar) {
        Intent intent = new Intent(context, (Class<?>) SystemForegroundService.class);
        intent.setAction("ACTION_START_FOREGROUND");
        intent.putExtra("KEY_WORKSPEC_ID", str);
        intent.putExtra("KEY_NOTIFICATION_ID", eVar.c());
        intent.putExtra("KEY_FOREGROUND_SERVICE_TYPE", eVar.a());
        intent.putExtra("KEY_NOTIFICATION", eVar.b());
        intent.putExtra("KEY_WORKSPEC_ID", str);
        return intent;
    }

    public static Intent e(Context context) {
        Intent intent = new Intent(context, (Class<?>) SystemForegroundService.class);
        intent.setAction("ACTION_STOP_FOREGROUND");
        return intent;
    }

    private void f(Intent intent) {
        k.c().d(f5983z, String.format("Stopping foreground work for %s", intent), new Throwable[0]);
        String stringExtra = intent.getStringExtra("KEY_WORKSPEC_ID");
        if (stringExtra == null || TextUtils.isEmpty(stringExtra)) {
            return;
        }
        this.f5985q.j(UUID.fromString(stringExtra));
    }

    private void g(Intent intent) {
        int i10 = 0;
        int intExtra = intent.getIntExtra("KEY_NOTIFICATION_ID", 0);
        int intExtra2 = intent.getIntExtra("KEY_FOREGROUND_SERVICE_TYPE", 0);
        String stringExtra = intent.getStringExtra("KEY_WORKSPEC_ID");
        Notification notification = (Notification) intent.getParcelableExtra("KEY_NOTIFICATION");
        k.c().a(f5983z, String.format("Notifying with (id: %s, workSpecId: %s, notificationType: %s)", Integer.valueOf(intExtra), stringExtra, Integer.valueOf(intExtra2)), new Throwable[0]);
        if (notification == null || this.f5993y == null) {
            return;
        }
        this.f5989u.put(stringExtra, new e(intExtra, notification, intExtra2));
        if (TextUtils.isEmpty(this.f5988t)) {
            this.f5988t = stringExtra;
            this.f5993y.b(intExtra, intExtra2, notification);
            return;
        }
        this.f5993y.c(intExtra, notification);
        if (intExtra2 != 0) {
            Iterator it = this.f5989u.entrySet().iterator();
            while (it.hasNext()) {
                i10 |= ((e) ((Map.Entry) it.next()).getValue()).a();
            }
            e eVar = (e) this.f5989u.get(this.f5988t);
            if (eVar != null) {
                this.f5993y.b(eVar.c(), i10, eVar.b());
            }
        }
    }

    private void h(Intent intent) {
        k.c().d(f5983z, String.format("Started foreground service %s", intent), new Throwable[0]);
        String stringExtra = intent.getStringExtra("KEY_WORKSPEC_ID");
        this.f5986r.b(new RunnableC0150a(this.f5985q.s(), stringExtra));
    }

    @Override // d6.c
    public void a(List list) {
        if (list.isEmpty()) {
            return;
        }
        Iterator it = list.iterator();
        while (it.hasNext()) {
            String str = (String) it.next();
            k.c().a(f5983z, String.format("Constraints unmet for WorkSpec %s", str), new Throwable[0]);
            this.f5985q.B(str);
        }
    }

    @Override // d6.c
    public void b(List list) {
    }

    void i(Intent intent) {
        k.c().d(f5983z, "Stopping foreground service", new Throwable[0]);
        b bVar = this.f5993y;
        if (bVar != null) {
            bVar.stop();
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void j() {
        this.f5993y = null;
        synchronized (this.f5987s) {
            this.f5992x.e();
        }
        this.f5985q.q().h(this);
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void k(Intent intent) {
        String action = intent.getAction();
        if ("ACTION_START_FOREGROUND".equals(action)) {
            h(intent);
            g(intent);
        } else if ("ACTION_NOTIFY".equals(action)) {
            g(intent);
        } else if ("ACTION_CANCEL_WORK".equals(action)) {
            f(intent);
        } else if ("ACTION_STOP_FOREGROUND".equals(action)) {
            i(intent);
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void l(b bVar) {
        if (this.f5993y != null) {
            k.c().b(f5983z, "A callback already exists.", new Throwable[0]);
        } else {
            this.f5993y = bVar;
        }
    }

    @Override // a6.b
    public void onExecuted(String str, boolean z10) {
        Map.Entry entry;
        synchronized (this.f5987s) {
            r rVar = (r) this.f5990v.remove(str);
            if (rVar != null ? this.f5991w.remove(rVar) : false) {
                this.f5992x.d(this.f5991w);
            }
        }
        e eVar = (e) this.f5989u.remove(str);
        if (str.equals(this.f5988t) && this.f5989u.size() > 0) {
            Iterator it = this.f5989u.entrySet().iterator();
            Object next = it.next();
            while (true) {
                entry = (Map.Entry) next;
                if (!it.hasNext()) {
                    break;
                } else {
                    next = it.next();
                }
            }
            this.f5988t = (String) entry.getKey();
            if (this.f5993y != null) {
                e eVar2 = (e) entry.getValue();
                this.f5993y.b(eVar2.c(), eVar2.a(), eVar2.b());
                this.f5993y.d(eVar2.c());
            }
        }
        b bVar = this.f5993y;
        if (eVar == null || bVar == null) {
            return;
        }
        k.c().a(f5983z, String.format("Removing Notification (id: %s, workSpecId: %s ,notificationType: %s)", Integer.valueOf(eVar.c()), str, Integer.valueOf(eVar.a())), new Throwable[0]);
        bVar.d(eVar.c());
    }
}
