package androidx.work.impl;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RestrictTo;
import androidx.room.RoomDatabase;
import androidx.room.t0;
import androidx.work.d0;
import androidx.work.g0;
import androidx.work.impl.background.systemjob.SystemJobService;
import androidx.work.l0;
import androidx.work.t;
import androidx.work.u;
import com.google.android.gms.measurement.internal.u1;
import java.util.Arrays;
import java.util.List;

/* JADX INFO: loaded from: classes.dex */
public final class o extends g0 {

    /* JADX INFO: renamed from: j, reason: collision with root package name */
    public static o f4620j;

    /* JADX INFO: renamed from: k, reason: collision with root package name */
    public static o f4621k;

    /* JADX INFO: renamed from: l, reason: collision with root package name */
    public static final Object f4622l;

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

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

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

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

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

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

    /* JADX INFO: renamed from: g, reason: collision with root package name */
    public final o4.h f4629g;

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

    /* JADX INFO: renamed from: i, reason: collision with root package name */
    public BroadcastReceiver.PendingResult f4631i;

    static {
        u.e("WorkManagerImpl");
        f4620j = null;
        f4621k = null;
        f4622l = new Object();
    }

    public o(Context context, androidx.work.c cVar, p4.c cVar2) {
        WorkDatabase workDatabaseCreate = WorkDatabase.create(context.getApplicationContext(), cVar2.f31623a, context.getResources().getBoolean(d0.workmanager_test_configuration));
        Context applicationContext = context.getApplicationContext();
        t tVar = new t(cVar.f4543f);
        synchronized (u.class) {
            u.f4694a = tVar;
        }
        int i10 = f.f4583a;
        j4.c cVar3 = new j4.c(applicationContext, this);
        o4.g.a(applicationContext, SystemJobService.class, true);
        u.c().a(new Throwable[0]);
        List listAsList = Arrays.asList(cVar3, new h4.b(applicationContext, cVar, cVar2, this));
        d dVar = new d(context, cVar, cVar2, workDatabaseCreate, listAsList);
        Context applicationContext2 = context.getApplicationContext();
        this.f4623a = applicationContext2;
        this.f4624b = cVar;
        this.f4626d = cVar2;
        this.f4625c = workDatabaseCreate;
        this.f4627e = listAsList;
        this.f4628f = dVar;
        this.f4629g = new o4.h(workDatabaseCreate);
        this.f4630h = false;
        if (applicationContext2.isDeviceProtectedStorage()) {
            throw new IllegalStateException("Cannot initialize WorkManager in direct boot mode");
        }
        ((p4.c) this.f4626d).a(new o4.e(applicationContext2, this));
    }

    public static o b(Context context) {
        o oVar;
        synchronized (f4622l) {
            try {
                oVar = getInstance();
                if (oVar == null) {
                    context.getApplicationContext();
                    throw new IllegalStateException("WorkManager is not initialized properly.  You have explicitly disabled WorkManagerInitializer in your manifest, have not manually called WorkManager#initialize at this point, and your Application does not implement Configuration.Provider.");
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        return oVar;
    }

    public static void c(Context context, androidx.work.c cVar) {
        synchronized (f4622l) {
            try {
                o oVar = f4620j;
                if (oVar != null && f4621k != null) {
                    throw new IllegalStateException("WorkManager is already initialized.  Did you try to initialize it manually without disabling WorkManagerInitializer? See WorkManager#initialize(Context, Configuration) or the class level Javadoc for more information.");
                }
                if (oVar == null) {
                    Context applicationContext = context.getApplicationContext();
                    if (f4621k == null) {
                        f4621k = new o(applicationContext, cVar, new p4.c(cVar.f4539b));
                    }
                    f4620j = f4621k;
                }
            } finally {
            }
        }
    }

    @Nullable
    @RestrictTo({RestrictTo.Scope.f556b})
    @Deprecated
    public static o getInstance() {
        synchronized (f4622l) {
            try {
                o oVar = f4620j;
                if (oVar != null) {
                    return oVar;
                }
                return f4621k;
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    @RestrictTo({RestrictTo.Scope.f556b})
    public static void setDelegate(@Nullable o oVar) {
        synchronized (f4622l) {
            f4620j = oVar;
        }
    }

    public final void d() {
        synchronized (f4622l) {
            try {
                this.f4630h = true;
                BroadcastReceiver.PendingResult pendingResult = this.f4631i;
                if (pendingResult != null) {
                    pendingResult.finish();
                    this.f4631i = null;
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public final void e() {
        j4.c.d(this.f4623a);
        n4.r rVar = (n4.r) this.f4625c.workSpecDao();
        Object obj = rVar.f30838b;
        RoomDatabase roomDatabase = (RoomDatabase) obj;
        roomDatabase.assertNotSuspendingTransaction();
        l3.j jVarA = ((t0) rVar.f30846j).a();
        roomDatabase.beginTransaction();
        try {
            jVarA.n();
            ((RoomDatabase) obj).setTransactionSuccessful();
            roomDatabase.endTransaction();
            ((t0) rVar.f30846j).c(jVarA);
            f.a(this.f4624b, this.f4625c, this.f4627e);
        } catch (Throwable th) {
            roomDatabase.endTransaction();
            ((t0) rVar.f30846j).c(jVarA);
            throw th;
        }
    }

    public final void f(String str) {
        ((p4.c) this.f4626d).a(new o4.l(this, str, false));
    }

    public final void g() {
        try {
            u1.x(Class.forName("androidx.work.multiprocess.RemoteWorkManagerClient").getConstructor(Context.class, o.class).newInstance(this.f4623a, this));
        } catch (Throwable th) {
            u.c().a(th);
        }
    }

    @Nullable
    @RestrictTo({RestrictTo.Scope.f556b})
    public q4.a getRemoteWorkManager() {
        synchronized (f4622l) {
            try {
                g();
                if (!TextUtils.isEmpty(this.f4624b.getDefaultProcessName())) {
                    throw new IllegalStateException("Invalid multiprocess configuration. Define an `implementation` dependency on :work:work-multiprocess library");
                }
            } catch (Throwable th) {
                throw th;
            }
        }
        return null;
    }

    @RestrictTo({RestrictTo.Scope.f556b})
    public void startWork(@NonNull String str, @Nullable l0 l0Var) {
        p4.a aVar = this.f4626d;
        o4.k kVar = new o4.k();
        kVar.f31393a = this;
        kVar.f31394b = str;
        kVar.f31395c = l0Var;
        ((p4.c) aVar).a(kVar);
    }
}
