package a6;

import a6.k;
import android.content.Context;
import android.os.PowerManager;
import androidx.work.WorkerParameters;
import androidx.work.impl.WorkDatabase;
import i6.l;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;

/* loaded from: classes.dex */
public class d implements b, g6.a {
    private static final String A = z5.k.f("Processor");

    /* renamed from: q, reason: collision with root package name */
    private Context f193q;

    /* renamed from: r, reason: collision with root package name */
    private androidx.work.a f194r;

    /* renamed from: s, reason: collision with root package name */
    private j6.a f195s;

    /* renamed from: t, reason: collision with root package name */
    private WorkDatabase f196t;

    /* renamed from: w, reason: collision with root package name */
    private List f199w;

    /* renamed from: v, reason: collision with root package name */
    private Map f198v = new HashMap();

    /* renamed from: u, reason: collision with root package name */
    private Map f197u = new HashMap();

    /* renamed from: x, reason: collision with root package name */
    private Set f200x = new HashSet();

    /* renamed from: y, reason: collision with root package name */
    private final List f201y = new ArrayList();

    /* renamed from: p, reason: collision with root package name */
    private PowerManager.WakeLock f192p = null;

    /* renamed from: z, reason: collision with root package name */
    private final Object f202z = new Object();

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public static class a implements Runnable {

        /* renamed from: p, reason: collision with root package name */
        private b f203p;

        /* renamed from: q, reason: collision with root package name */
        private String f204q;

        /* renamed from: r, reason: collision with root package name */
        private ek.d f205r;

        a(b bVar, String str, ek.d dVar) {
            this.f203p = bVar;
            this.f204q = str;
            this.f205r = dVar;
        }

        @Override // java.lang.Runnable
        public void run() {
            boolean z10;
            try {
                z10 = ((Boolean) this.f205r.get()).booleanValue();
            } catch (InterruptedException | ExecutionException unused) {
                z10 = true;
            }
            this.f203p.onExecuted(this.f204q, z10);
        }
    }

    public d(Context context, androidx.work.a aVar, j6.a aVar2, WorkDatabase workDatabase, List list) {
        this.f193q = context;
        this.f194r = aVar;
        this.f195s = aVar2;
        this.f196t = workDatabase;
        this.f199w = list;
    }

    private static boolean d(String str, k kVar) {
        if (kVar == null) {
            z5.k.c().a(A, String.format("WorkerWrapper could not be found for %s", str), new Throwable[0]);
            return false;
        }
        kVar.d();
        z5.k.c().a(A, String.format("WorkerWrapper interrupted for %s", str), new Throwable[0]);
        return true;
    }

    private void l() {
        synchronized (this.f202z) {
            if (!(!this.f197u.isEmpty())) {
                try {
                    this.f193q.startService(androidx.work.impl.foreground.a.e(this.f193q));
                } catch (Throwable th2) {
                    z5.k.c().b(A, "Unable to stop foreground service", th2);
                }
                PowerManager.WakeLock wakeLock = this.f192p;
                if (wakeLock != null) {
                    wakeLock.release();
                    this.f192p = null;
                }
            }
        }
    }

    @Override // g6.a
    public void a(String str) {
        synchronized (this.f202z) {
            this.f197u.remove(str);
            l();
        }
    }

    @Override // g6.a
    public void b(String str, z5.e eVar) {
        synchronized (this.f202z) {
            z5.k.c().d(A, String.format("Moving WorkSpec (%s) to the foreground", str), new Throwable[0]);
            k kVar = (k) this.f198v.remove(str);
            if (kVar != null) {
                if (this.f192p == null) {
                    PowerManager.WakeLock b10 = l.b(this.f193q, "ProcessorForegroundLck");
                    this.f192p = b10;
                    b10.acquire();
                }
                this.f197u.put(str, kVar);
                androidx.core.content.a.startForegroundService(this.f193q, androidx.work.impl.foreground.a.d(this.f193q, str, eVar));
            }
        }
    }

    public void c(b bVar) {
        synchronized (this.f202z) {
            this.f201y.add(bVar);
        }
    }

    public boolean e(String str) {
        boolean contains;
        synchronized (this.f202z) {
            contains = this.f200x.contains(str);
        }
        return contains;
    }

    public boolean f(String str) {
        boolean z10;
        synchronized (this.f202z) {
            z10 = this.f198v.containsKey(str) || this.f197u.containsKey(str);
        }
        return z10;
    }

    public boolean g(String str) {
        boolean containsKey;
        synchronized (this.f202z) {
            containsKey = this.f197u.containsKey(str);
        }
        return containsKey;
    }

    public void h(b bVar) {
        synchronized (this.f202z) {
            this.f201y.remove(bVar);
        }
    }

    public boolean i(String str) {
        return j(str, null);
    }

    public boolean j(String str, WorkerParameters.a aVar) {
        synchronized (this.f202z) {
            if (f(str)) {
                z5.k.c().a(A, String.format("Work %s is already enqueued for processing", str), new Throwable[0]);
                return false;
            }
            k a10 = new k.c(this.f193q, this.f194r, this.f195s, this, this.f196t, str).c(this.f199w).b(aVar).a();
            ek.d b10 = a10.b();
            b10.addListener(new a(this, str, b10), this.f195s.a());
            this.f198v.put(str, a10);
            this.f195s.c().execute(a10);
            z5.k.c().a(A, String.format("%s: processing %s", getClass().getSimpleName(), str), new Throwable[0]);
            return true;
        }
    }

    public boolean k(String str) {
        boolean d10;
        synchronized (this.f202z) {
            z5.k.c().a(A, String.format("Processor cancelling %s", str), new Throwable[0]);
            this.f200x.add(str);
            k kVar = (k) this.f197u.remove(str);
            boolean z10 = kVar != null;
            if (kVar == null) {
                kVar = (k) this.f198v.remove(str);
            }
            d10 = d(str, kVar);
            if (z10) {
                l();
            }
        }
        return d10;
    }

    public boolean m(String str) {
        boolean d10;
        synchronized (this.f202z) {
            z5.k.c().a(A, String.format("Processor stopping foreground work %s", str), new Throwable[0]);
            d10 = d(str, (k) this.f197u.remove(str));
        }
        return d10;
    }

    public boolean n(String str) {
        boolean d10;
        synchronized (this.f202z) {
            z5.k.c().a(A, String.format("Processor stopping background work %s", str), new Throwable[0]);
            d10 = d(str, (k) this.f198v.remove(str));
        }
        return d10;
    }

    @Override // a6.b
    public void onExecuted(String str, boolean z10) {
        synchronized (this.f202z) {
            this.f198v.remove(str);
            z5.k.c().a(A, String.format("%s %s executed; reschedule = %s", getClass().getSimpleName(), str, Boolean.valueOf(z10)), new Throwable[0]);
            Iterator it = this.f201y.iterator();
            while (it.hasNext()) {
                ((b) it.next()).onExecuted(str, z10);
            }
        }
    }
}
