package androidx.work.impl;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.PowerManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.work.ListenableWorker;
import androidx.work.impl.foreground.SystemForegroundService;
import androidx.work.l0;
import androidx.work.u;
import com.google.common.util.concurrent.w;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

/* JADX INFO: loaded from: classes.dex */
public final class d implements b, androidx.work.impl.foreground.a {

    /* JADX INFO: renamed from: a, reason: collision with root package name */
    public final Context f4573a;

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final androidx.work.c f4574b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final p4.a f4575c;

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public final WorkDatabase f4576d;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final List f4579g;

    /* JADX INFO: renamed from: f, reason: collision with root package name */
    public final HashMap f4578f = new HashMap();

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final HashMap f4577e = new HashMap();

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public final HashSet f4580h = new HashSet();

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public final ArrayList f4581i = new ArrayList();

    @Nullable
    private PowerManager.WakeLock mForegroundLock = null;

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public final Object f4582j = new Object();

    static {
        u.e("Processor");
    }

    public d(Context context, androidx.work.c cVar, p4.c cVar2, WorkDatabase workDatabase, List list) {
        this.f4573a = context;
        this.f4574b = cVar;
        this.f4575c = cVar2;
        this.f4576d = workDatabase;
        this.f4579g = list;
    }

    private static boolean interrupt(@NonNull String str, @Nullable r rVar) {
        boolean zIsDone;
        if (rVar == null) {
            u uVarC = u.c();
            String.format("WorkerWrapper could not be found for %s", str);
            uVarC.a(new Throwable[0]);
            return false;
        }
        rVar.U = true;
        rVar.i();
        w wVar = rVar.mInnerFuture;
        if (wVar != null) {
            zIsDone = wVar.isDone();
            rVar.mInnerFuture.cancel(true);
        } else {
            zIsDone = false;
        }
        ListenableWorker listenableWorker = rVar.f4649f;
        if (listenableWorker == null || zIsDone) {
            String.format("WorkSpec %s is already done. Not interrupting.", rVar.f4648e);
            u uVarC2 = u.c();
            int i10 = r.V;
            uVarC2.a(new Throwable[0]);
        } else {
            listenableWorker.stop();
        }
        u uVarC3 = u.c();
        String.format("WorkerWrapper interrupted for %s", str);
        uVarC3.a(new Throwable[0]);
        return true;
    }

    public final void a(b bVar) {
        synchronized (this.f4582j) {
            this.f4581i.add(bVar);
        }
    }

    public final boolean b(String str) {
        boolean z10;
        synchronized (this.f4582j) {
            try {
                z10 = this.f4578f.containsKey(str) || this.f4577e.containsKey(str);
            } finally {
            }
        }
        return z10;
    }

    public final void c(b bVar) {
        synchronized (this.f4582j) {
            this.f4581i.remove(bVar);
        }
    }

    public final void d(String str, androidx.work.j jVar) {
        synchronized (this.f4582j) {
            try {
                u uVarC = u.c();
                String.format("Moving WorkSpec (%s) to the foreground", str);
                uVarC.d(new Throwable[0]);
                r rVar = (r) this.f4578f.remove(str);
                if (rVar != null) {
                    if (this.mForegroundLock == null) {
                        PowerManager.WakeLock wakeLockA = o4.m.a(this.f4573a, "ProcessorForegroundLck");
                        this.mForegroundLock = wakeLockA;
                        wakeLockA.acquire();
                    }
                    this.f4577e.put(str, rVar);
                    Intent intentB = androidx.work.impl.foreground.c.b(this.f4573a, str, jVar);
                    Context context = this.f4573a;
                    Object obj = g0.d.f26125a;
                    if (Build.VERSION.SDK_INT >= 26) {
                        g0.a.a(context, intentB);
                    } else {
                        context.startService(intentB);
                    }
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void e(String str) {
        synchronized (this.f4582j) {
            try {
                u uVarC = u.c();
                String.format("Processor cancelling %s", str);
                uVarC.a(new Throwable[0]);
                this.f4580h.add(str);
                r rVar = (r) this.f4577e.remove(str);
                boolean z10 = rVar != null;
                if (rVar == null) {
                    rVar = (r) this.f4578f.remove(str);
                }
                interrupt(str, rVar);
                if (z10) {
                    f();
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void f() {
        synchronized (this.f4582j) {
            try {
                if (!(!this.f4577e.isEmpty())) {
                    Context context = this.f4573a;
                    int i10 = androidx.work.impl.foreground.c.f4584j;
                    Intent intent = new Intent(context, (Class<?>) SystemForegroundService.class);
                    intent.setAction("ACTION_STOP_FOREGROUND");
                    try {
                        this.f4573a.startService(intent);
                    } catch (Throwable th) {
                        u.c().b(th);
                    }
                    PowerManager.WakeLock wakeLock = this.mForegroundLock;
                    if (wakeLock != null) {
                        wakeLock.release();
                        this.mForegroundLock = null;
                    }
                }
            } catch (Throwable th2) {
                throw th2;
            }
        }
    }

    public final boolean g(String str) {
        boolean zInterrupt;
        synchronized (this.f4582j) {
            u uVarC = u.c();
            String.format("Processor stopping foreground work %s", str);
            uVarC.a(new Throwable[0]);
            zInterrupt = interrupt(str, (r) this.f4577e.remove(str));
        }
        return zInterrupt;
    }

    public final boolean h(String str) {
        boolean zInterrupt;
        synchronized (this.f4582j) {
            u uVarC = u.c();
            String.format("Processor stopping background work %s", str);
            uVarC.a(new Throwable[0]);
            zInterrupt = interrupt(str, (r) this.f4578f.remove(str));
        }
        return zInterrupt;
    }

    @Override // androidx.work.impl.b
    public final void onExecuted(String str, boolean z10) {
        synchronized (this.f4582j) {
            try {
                this.f4578f.remove(str);
                u uVarC = u.c();
                String.format("%s %s executed; reschedule = %s", d.class.getSimpleName(), str, Boolean.valueOf(z10));
                uVarC.a(new Throwable[0]);
                Iterator it = this.f4581i.iterator();
                while (it.hasNext()) {
                    ((b) it.next()).onExecuted(str, z10);
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public boolean startWork(@NonNull String str, @Nullable l0 l0Var) {
        synchronized (this.f4582j) {
            try {
                if (b(str)) {
                    u uVarC = u.c();
                    String.format("Work %s is already enqueued for processing", str);
                    uVarC.a(new Throwable[0]);
                    return false;
                }
                Context context = this.f4573a;
                androidx.work.c cVar = this.f4574b;
                p4.a aVar = this.f4575c;
                WorkDatabase workDatabase = this.f4576d;
                q qVar = new q();
                qVar.f4643h = new l0();
                qVar.f4636a = context.getApplicationContext();
                qVar.f4638c = aVar;
                qVar.f4637b = this;
                qVar.f4639d = cVar;
                qVar.f4640e = workDatabase;
                qVar.f4641f = str;
                qVar.f4642g = this.f4579g;
                r rVarA = qVar.withRuntimeExtras(l0Var).a();
                androidx.work.impl.utils.futures.i iVar = rVarA.T;
                iVar.g(new p(this, str, iVar), ((p4.c) this.f4575c).f31625c);
                this.f4578f.put(str, rVarA);
                ((p4.c) this.f4575c).f31623a.execute(rVarA);
                u uVarC2 = u.c();
                String.format("%s: processing %s", d.class.getSimpleName(), str);
                uVarC2.a(new Throwable[0]);
                return true;
            } catch (Throwable th) {
                throw th;
            }
        }
    }
}
