package j4;

import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.database.Cursor;
import android.net.NetworkRequest;
import android.os.Build;
import android.os.PersistableBundle;
import android.text.TextUtils;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.room.RoomDatabase;
import androidx.room.s0;
import androidx.work.BackoffPolicy;
import androidx.work.NetworkType;
import androidx.work.OutOfQuotaPolicy;
import androidx.work.WorkInfo$State;
import androidx.work.impl.WorkDatabase;
import androidx.work.impl.background.systemjob.SystemJobService;
import androidx.work.impl.e;
import androidx.work.impl.o;
import androidx.work.u;
import com.google.android.gms.internal.atv_ads_framework.d1;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import n4.d;
import n4.g;
import n4.p;
import n4.r;
import o4.f;
import xyz.stream.utils.t;

/* JADX INFO: loaded from: classes.dex */
public final class c implements e {

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

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

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

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

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

    public c(Context context, o oVar) {
        JobScheduler jobScheduler = (JobScheduler) context.getSystemService("jobscheduler");
        b bVar = new b(context);
        this.f28629a = context;
        this.f28631c = oVar;
        this.f28630b = jobScheduler;
        this.f28632d = bVar;
    }

    public static void d(Context context) {
        List<JobInfo> pendingJobs;
        JobScheduler jobScheduler = (JobScheduler) context.getSystemService("jobscheduler");
        if (jobScheduler == null || (pendingJobs = getPendingJobs(context, jobScheduler)) == null || pendingJobs.isEmpty()) {
            return;
        }
        Iterator<JobInfo> it = pendingJobs.iterator();
        while (it.hasNext()) {
            e(jobScheduler, it.next().getId());
        }
    }

    public static void e(JobScheduler jobScheduler, int i10) {
        try {
            jobScheduler.cancel(i10);
        } catch (Throwable th) {
            u uVarC = u.c();
            String.format(Locale.getDefault(), "Exception while trying to cancel job (%d)", Integer.valueOf(i10));
            uVarC.b(th);
        }
    }

    public static boolean f(Context context, o oVar) {
        JobScheduler jobScheduler = (JobScheduler) context.getSystemService("jobscheduler");
        List<JobInfo> pendingJobs = getPendingJobs(context, jobScheduler);
        t tVar = (t) oVar.f4625c.systemIdInfoDao();
        tVar.getClass();
        boolean z10 = false;
        s0 s0VarK = s0.k(0, "SELECT DISTINCT work_spec_id FROM SystemIdInfo");
        ((RoomDatabase) tVar.f36172b).assertNotSuspendingTransaction();
        Cursor cursorC = androidx.room.util.b.c((RoomDatabase) tVar.f36172b, s0VarK);
        try {
            ArrayList arrayList = new ArrayList(cursorC.getCount());
            while (cursorC.moveToNext()) {
                arrayList.add(cursorC.getString(0));
            }
            HashSet hashSet = new HashSet(pendingJobs != null ? pendingJobs.size() : 0);
            if (pendingJobs != null && !pendingJobs.isEmpty()) {
                for (JobInfo jobInfo : pendingJobs) {
                    String workSpecIdFromJobInfo = getWorkSpecIdFromJobInfo(jobInfo);
                    if (TextUtils.isEmpty(workSpecIdFromJobInfo)) {
                        e(jobScheduler, jobInfo.getId());
                    } else {
                        hashSet.add(workSpecIdFromJobInfo);
                    }
                }
            }
            Iterator it = arrayList.iterator();
            while (true) {
                if (!it.hasNext()) {
                    break;
                }
                if (!hashSet.contains((String) it.next())) {
                    u.c().a(new Throwable[0]);
                    z10 = true;
                    break;
                }
            }
            if (z10) {
                WorkDatabase workDatabase = oVar.f4625c;
                workDatabase.beginTransaction();
                try {
                    p pVarWorkSpecDao = workDatabase.workSpecDao();
                    Iterator it2 = arrayList.iterator();
                    while (it2.hasNext()) {
                        ((r) pVarWorkSpecDao).v(-1L, (String) it2.next());
                    }
                    workDatabase.setTransactionSuccessful();
                    workDatabase.endTransaction();
                } catch (Throwable th) {
                    workDatabase.endTransaction();
                    throw th;
                }
            }
            return z10;
        } finally {
            cursorC.close();
            s0VarK.m();
        }
    }

    @Nullable
    private static List<Integer> getPendingJobIds(@NonNull Context context, @NonNull JobScheduler jobScheduler, @NonNull String str) {
        List<JobInfo> pendingJobs = getPendingJobs(context, jobScheduler);
        if (pendingJobs == null) {
            return null;
        }
        ArrayList arrayList = new ArrayList(2);
        for (JobInfo jobInfo : pendingJobs) {
            if (str.equals(getWorkSpecIdFromJobInfo(jobInfo))) {
                arrayList.add(Integer.valueOf(jobInfo.getId()));
            }
        }
        return arrayList;
    }

    @Nullable
    private static List<JobInfo> getPendingJobs(@NonNull Context context, @NonNull JobScheduler jobScheduler) {
        List<JobInfo> allPendingJobs;
        try {
            allPendingJobs = jobScheduler.getAllPendingJobs();
        } catch (Throwable th) {
            u.c().b(th);
            allPendingJobs = null;
        }
        if (allPendingJobs == null) {
            return null;
        }
        ArrayList arrayList = new ArrayList(allPendingJobs.size());
        ComponentName componentName = new ComponentName(context, (Class<?>) SystemJobService.class);
        for (JobInfo jobInfo : allPendingJobs) {
            if (componentName.equals(jobInfo.getService())) {
                arrayList.add(jobInfo);
            }
        }
        return arrayList;
    }

    @Nullable
    private static String getWorkSpecIdFromJobInfo(@NonNull JobInfo jobInfo) {
        PersistableBundle extras = jobInfo.getExtras();
        if (extras == null) {
            return null;
        }
        try {
            if (extras.containsKey("EXTRA_WORK_SPEC_ID")) {
                return extras.getString("EXTRA_WORK_SPEC_ID");
            }
            return null;
        } catch (NullPointerException unused) {
            return null;
        }
    }

    @Override // androidx.work.impl.e
    public final void a(n4.o... oVarArr) {
        int i10;
        o oVar = this.f28631c;
        WorkDatabase workDatabase = oVar.f4625c;
        f fVar = new f(workDatabase);
        for (n4.o oVar2 : oVarArr) {
            workDatabase.beginTransaction();
            try {
                n4.o oVarP = ((r) workDatabase.workSpecDao()).p(oVar2.f30818a);
                if (oVarP == null) {
                    u.c().f(new Throwable[0]);
                    workDatabase.setTransactionSuccessful();
                } else if (oVarP.f30819b != WorkInfo$State.f4516a) {
                    u.c().f(new Throwable[0]);
                    workDatabase.setTransactionSuccessful();
                } else {
                    g systemIdInfo = ((t) workDatabase.systemIdInfoDao()).getSystemIdInfo(oVar2.f30818a);
                    if (systemIdInfo != null) {
                        i10 = systemIdInfo.f30809b;
                    } else {
                        oVar.f4624b.getClass();
                        int i11 = oVar.f4624b.f4544g;
                        synchronized (f.class) {
                            int iA = fVar.a("next_job_scheduler_id");
                            if (iA < 0 || iA > i11) {
                                ((d1) fVar.f31387a.preferenceDao()).k(new d("next_job_scheduler_id", 1));
                                i10 = 0;
                            } else {
                                i10 = iA;
                            }
                        }
                    }
                    if (systemIdInfo == null) {
                        ((t) oVar.f4625c.systemIdInfoDao()).s(new g(oVar2.f30818a, i10));
                    }
                    g(oVar2, i10);
                    workDatabase.setTransactionSuccessful();
                }
            } finally {
                workDatabase.endTransaction();
            }
        }
    }

    @Override // androidx.work.impl.e
    public final boolean b() {
        return true;
    }

    @Override // androidx.work.impl.e
    public final void c(String str) {
        Context context = this.f28629a;
        JobScheduler jobScheduler = this.f28630b;
        List<Integer> pendingJobIds = getPendingJobIds(context, jobScheduler, str);
        if (pendingJobIds == null || pendingJobIds.isEmpty()) {
            return;
        }
        Iterator<Integer> it = pendingJobIds.iterator();
        while (it.hasNext()) {
            e(jobScheduler, it.next().intValue());
        }
        ((t) this.f28631c.f4625c.systemIdInfoDao()).v(str);
    }

    /* JADX WARN: Multi-variable type inference failed */
    public final void g(n4.o oVar, int i10) {
        int i11;
        JobScheduler jobScheduler = this.f28630b;
        b bVar = this.f28632d;
        bVar.getClass();
        androidx.work.e eVar = oVar.f30827j;
        PersistableBundle persistableBundle = new PersistableBundle();
        persistableBundle.putString("EXTRA_WORK_SPEC_ID", oVar.f30818a);
        persistableBundle.putBoolean("EXTRA_IS_PERIODIC", oVar.c());
        JobInfo.Builder extras = new JobInfo.Builder(i10, bVar.f28628a).setRequiresCharging(eVar.f4550b).setRequiresDeviceIdle(eVar.f4551c).setExtras(persistableBundle);
        NetworkType networkType = eVar.f4549a;
        int i12 = Build.VERSION.SDK_INT;
        if (i12 < 30 || networkType != NetworkType.f4511f) {
            int iOrdinal = networkType.ordinal();
            if (iOrdinal == 0) {
                i11 = 0;
            } else if (iOrdinal != 1) {
                i11 = 2;
                if (iOrdinal != 2) {
                    i11 = 3;
                    if (iOrdinal != 3) {
                        i11 = 4;
                        if (iOrdinal != 4 || i12 < 26) {
                            u uVarC = u.c();
                            String.format("API version too low. Cannot convert network type value %s", networkType);
                            int i13 = b.f28627b;
                            uVarC.a(new Throwable[0]);
                            i11 = 1;
                        }
                    }
                }
            } else {
                i11 = 1;
            }
            extras.setRequiredNetworkType(i11);
        } else {
            extras.setRequiredNetwork(new NetworkRequest.Builder().addCapability(25).build());
        }
        if (!eVar.f4551c) {
            extras.setBackoffCriteria(oVar.f30830m, oVar.f30829l == BackoffPolicy.f4495b ? 0 : 1);
        }
        long jMax = Math.max(oVar.a() - System.currentTimeMillis(), 0L);
        if (i12 <= 28 || jMax > 0) {
            extras.setMinimumLatency(jMax);
        } else if (!oVar.f30834q) {
            extras.setImportantWhileForeground(true);
        }
        if (eVar.f4556h.f4559a.size() > 0) {
            for (androidx.work.f fVar : eVar.f4556h.f4559a) {
                extras.addTriggerContentUri(new JobInfo.TriggerContentUri(fVar.f4557a, fVar.f4558b ? 1 : 0));
            }
            extras.setTriggerContentUpdateDelay(eVar.f4554f);
            extras.setTriggerContentMaxDelay(eVar.f4555g);
        }
        extras.setPersisted(false);
        if (Build.VERSION.SDK_INT >= 26) {
            extras.setRequiresBatteryNotLow(eVar.f4552d);
            extras.setRequiresStorageNotLow(eVar.f4553e);
        }
        Object[] objArr = oVar.f30828k > 0;
        if (m0.a.b() && oVar.f30834q && objArr == false) {
            extras.setExpedited(true);
        }
        JobInfo jobInfoBuild = extras.build();
        u uVarC2 = u.c();
        String.format("Scheduling work ID %s Job ID %s", oVar.f30818a, Integer.valueOf(i10));
        uVarC2.a(new Throwable[0]);
        try {
            if (jobScheduler.schedule(jobInfoBuild) == 0) {
                u uVarC3 = u.c();
                String.format("Unable to schedule work ID %s", oVar.f30818a);
                uVarC3.f(new Throwable[0]);
                if (oVar.f30834q && oVar.f30835r == OutOfQuotaPolicy.f4513a) {
                    oVar.f30834q = false;
                    String.format("Scheduling a non-expedited job (work ID %s)", oVar.f30818a);
                    u.c().a(new Throwable[0]);
                    g(oVar, i10);
                }
            }
        } catch (IllegalStateException e10) {
            List<JobInfo> pendingJobs = getPendingJobs(this.f28629a, jobScheduler);
            int size = pendingJobs != null ? pendingJobs.size() : 0;
            Locale locale = Locale.getDefault();
            Integer numValueOf = Integer.valueOf(size);
            o oVar2 = this.f28631c;
            String str = String.format(locale, "JobScheduler 100 job limit exceeded.  We count %d WorkManager jobs in JobScheduler; we have %d tracked jobs in our DB; our Configuration limit is %d.", numValueOf, Integer.valueOf(((r) oVar2.f4625c.workSpecDao()).k().size()), Integer.valueOf(oVar2.f4624b.f4545h));
            u.c().b(new Throwable[0]);
            throw new IllegalStateException(str, e10);
        } catch (Throwable th) {
            u uVarC4 = u.c();
            String.format("Unable to schedule %s", oVar);
            uVarC4.b(th);
        }
    }
}
