package androidx.work.impl.workers;

import a6.i;
import android.content.Context;
import android.text.TextUtils;
import androidx.work.ListenableWorker;
import androidx.work.WorkerParameters;
import androidx.work.impl.WorkDatabase;
import d6.c;
import ek.d;
import h6.r;
import java.util.Collections;
import java.util.List;
import z5.k;

/* loaded from: classes.dex */
public class ConstraintTrackingWorker extends ListenableWorker implements c {

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

    /* renamed from: u, reason: collision with root package name */
    private WorkerParameters f6031u;

    /* renamed from: v, reason: collision with root package name */
    final Object f6032v;

    /* renamed from: w, reason: collision with root package name */
    volatile boolean f6033w;

    /* renamed from: x, reason: collision with root package name */
    androidx.work.impl.utils.futures.c f6034x;

    /* renamed from: y, reason: collision with root package name */
    private ListenableWorker f6035y;

    /* loaded from: classes.dex */
    class a implements Runnable {
        a() {
        }

        @Override // java.lang.Runnable
        public void run() {
            ConstraintTrackingWorker.this.u();
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* loaded from: classes.dex */
    public class b implements Runnable {

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

        b(d dVar) {
            this.f6037p = dVar;
        }

        @Override // java.lang.Runnable
        public void run() {
            synchronized (ConstraintTrackingWorker.this.f6032v) {
                if (ConstraintTrackingWorker.this.f6033w) {
                    ConstraintTrackingWorker.this.t();
                } else {
                    ConstraintTrackingWorker.this.f6034x.q(this.f6037p);
                }
            }
        }
    }

    public ConstraintTrackingWorker(Context context, WorkerParameters workerParameters) {
        super(context, workerParameters);
        this.f6031u = workerParameters;
        this.f6032v = new Object();
        this.f6033w = false;
        this.f6034x = androidx.work.impl.utils.futures.c.s();
    }

    @Override // d6.c
    public void a(List list) {
        k.c().a(f6030z, String.format("Constraints changed for %s", list), new Throwable[0]);
        synchronized (this.f6032v) {
            this.f6033w = true;
        }
    }

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

    @Override // androidx.work.ListenableWorker
    public j6.a h() {
        return i.o(c()).u();
    }

    @Override // androidx.work.ListenableWorker
    public boolean j() {
        ListenableWorker listenableWorker = this.f6035y;
        return listenableWorker != null && listenableWorker.j();
    }

    @Override // androidx.work.ListenableWorker
    public void m() {
        super.m();
        ListenableWorker listenableWorker = this.f6035y;
        if (listenableWorker == null || listenableWorker.k()) {
            return;
        }
        this.f6035y.q();
    }

    @Override // androidx.work.ListenableWorker
    public d p() {
        d().execute(new a());
        return this.f6034x;
    }

    public WorkDatabase r() {
        return i.o(c()).s();
    }

    void s() {
        this.f6034x.o(ListenableWorker.a.a());
    }

    void t() {
        this.f6034x.o(ListenableWorker.a.d());
    }

    void u() {
        String o10 = g().o("androidx.work.impl.workers.ConstraintTrackingWorker.ARGUMENT_CLASS_NAME");
        if (TextUtils.isEmpty(o10)) {
            k.c().b(f6030z, "No worker to delegate to.", new Throwable[0]);
            s();
            return;
        }
        ListenableWorker b10 = i().b(c(), o10, this.f6031u);
        this.f6035y = b10;
        if (b10 == null) {
            k.c().a(f6030z, "No worker to delegate to.", new Throwable[0]);
            s();
            return;
        }
        r o11 = r().O().o(f().toString());
        if (o11 == null) {
            s();
            return;
        }
        d6.d dVar = new d6.d(c(), h(), this);
        dVar.d(Collections.singletonList(o11));
        if (!dVar.c(f().toString())) {
            k.c().a(f6030z, String.format("Constraints not met for delegate %s. Requesting retry.", o10), new Throwable[0]);
            t();
            return;
        }
        k.c().a(f6030z, String.format("Constraints met for delegate %s", o10), new Throwable[0]);
        try {
            d p10 = this.f6035y.p();
            p10.addListener(new b(p10), d());
        } catch (Throwable th2) {
            k c10 = k.c();
            String str = f6030z;
            c10.a(str, String.format("Delegated worker %s threw exception in startWork.", o10), th2);
            synchronized (this.f6032v) {
                if (this.f6033w) {
                    k.c().a(str, "Constraints were unmet, Retrying.", new Throwable[0]);
                    t();
                } else {
                    s();
                }
            }
        }
    }
}
