package i4;

import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import androidx.annotation.Nullable;
import androidx.work.impl.background.systemalarm.SystemAlarmService;
import androidx.work.u;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import n4.o;
import o4.m;
import o4.r;

/* JADX INFO: loaded from: classes.dex */
public final class e implements k4.c, androidx.work.impl.b, r {

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public static final /* synthetic */ int f26547i = 0;

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

    /* JADX INFO: renamed from: b, reason: collision with root package name */
    public final int f26549b;

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public final String f26550c;

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

    /* JADX INFO: renamed from: e, reason: collision with root package name */
    public final k4.d f26552e;

    @Nullable
    private PowerManager.WakeLock mWakeLock;

    /* JADX INFO: renamed from: h, reason: collision with root package name */
    public boolean f26555h = false;

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public int f26554g = 0;

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

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

    public e(Context context, int i10, String str, h hVar) {
        this.f26548a = context;
        this.f26549b = i10;
        this.f26551d = hVar;
        this.f26550c = str;
        this.f26552e = new k4.d(context, hVar.f26560b, this);
    }

    public final void a() {
        synchronized (this.f26553f) {
            try {
                this.f26552e.e();
                this.f26551d.f26561c.b(this.f26550c);
                PowerManager.WakeLock wakeLock = this.mWakeLock;
                if (wakeLock != null && wakeLock.isHeld()) {
                    u uVarC = u.c();
                    String.format("Releasing wakelock %s for WorkSpec %s", this.mWakeLock, this.f26550c);
                    uVarC.a(new Throwable[0]);
                    this.mWakeLock.release();
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void b() {
        Integer numValueOf = Integer.valueOf(this.f26549b);
        String str = this.f26550c;
        this.mWakeLock = m.a(this.f26548a, String.format("%s (%s)", str, numValueOf));
        u uVarC = u.c();
        String.format("Acquiring wakelock %s for WorkSpec %s", this.mWakeLock, str);
        uVarC.a(new Throwable[0]);
        this.mWakeLock.acquire();
        o oVarP = ((n4.r) this.f26551d.f26563e.f4625c.workSpecDao()).p(str);
        if (oVarP == null) {
            c();
            return;
        }
        boolean zB = oVarP.b();
        this.f26555h = zB;
        if (zB) {
            this.f26552e.d(Collections.singletonList(oVarP));
        } else {
            u uVarC2 = u.c();
            String.format("No constraints for %s", str);
            uVarC2.a(new Throwable[0]);
            e(Collections.singletonList(str));
        }
    }

    public final void c() {
        synchronized (this.f26553f) {
            try {
                if (this.f26554g < 2) {
                    this.f26554g = 2;
                    u uVarC = u.c();
                    String.format("Stopping work for WorkSpec %s", this.f26550c);
                    uVarC.a(new Throwable[0]);
                    Context context = this.f26548a;
                    String str = this.f26550c;
                    Intent intent = new Intent(context, (Class<?>) SystemAlarmService.class);
                    intent.setAction("ACTION_STOP_WORK");
                    intent.putExtra("KEY_WORKSPEC_ID", str);
                    h hVar = this.f26551d;
                    hVar.e(new androidx.browser.customtabs.f(hVar, intent, this.f26549b));
                    if (this.f26551d.f26562d.b(this.f26550c)) {
                        u uVarC2 = u.c();
                        String.format("WorkSpec %s needs to be rescheduled", this.f26550c);
                        uVarC2.a(new Throwable[0]);
                        Intent intentB = b.b(this.f26548a, this.f26550c);
                        h hVar2 = this.f26551d;
                        hVar2.e(new androidx.browser.customtabs.f(hVar2, intentB, this.f26549b));
                    } else {
                        u uVarC3 = u.c();
                        String.format("Processor does not have WorkSpec %s. No need to reschedule ", this.f26550c);
                        uVarC3.a(new Throwable[0]);
                    }
                } else {
                    u uVarC4 = u.c();
                    String.format("Already stopped work for %s", this.f26550c);
                    uVarC4.a(new Throwable[0]);
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    @Override // k4.c
    public final void d(ArrayList arrayList) {
        c();
    }

    @Override // k4.c
    public final void e(List list) {
        if (list.contains(this.f26550c)) {
            synchronized (this.f26553f) {
                try {
                    if (this.f26554g == 0) {
                        this.f26554g = 1;
                        u uVarC = u.c();
                        String.format("onAllConstraintsMet for %s", this.f26550c);
                        uVarC.a(new Throwable[0]);
                        if (this.f26551d.f26562d.startWork(this.f26550c, null)) {
                            this.f26551d.f26561c.a(this.f26550c, this);
                        } else {
                            a();
                        }
                    } else {
                        u uVarC2 = u.c();
                        String.format("Already started work for %s", this.f26550c);
                        uVarC2.a(new Throwable[0]);
                    }
                } catch (Throwable th) {
                    throw th;
                }
            }
        }
    }

    @Override // androidx.work.impl.b
    public final void onExecuted(String str, boolean z10) {
        u uVarC = u.c();
        String.format("onExecuted %s, %s", str, Boolean.valueOf(z10));
        uVarC.a(new Throwable[0]);
        a();
        int i10 = this.f26549b;
        h hVar = this.f26551d;
        Context context = this.f26548a;
        if (z10) {
            hVar.e(new androidx.browser.customtabs.f(hVar, b.b(context, this.f26550c), i10));
        }
        if (this.f26555h) {
            Intent intent = new Intent(context, (Class<?>) SystemAlarmService.class);
            intent.setAction("ACTION_CONSTRAINTS_CHANGED");
            hVar.e(new androidx.browser.customtabs.f(hVar, intent, i10));
        }
    }
}
