package defpackage;

import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.os.Build;
import android.os.PersistableBundle;
import androidx.work.impl.WorkDatabase;
import androidx.work.impl.background.systemjob.SystemJobService;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.Callable;

/* renamed from: ᵡˋ, reason: contains not printable characters */
/* loaded from: classes3.dex */
public final class C3198 implements InterfaceC3764 {

    /* renamed from: ᵡⁱᵥ, reason: contains not printable characters */
    public static final /* synthetic */ int f12097 = 0;

    /* renamed from: ᵢˇᵭ, reason: contains not printable characters */
    public final JobScheduler f12098;

    /* renamed from: ᵤᵙᵷ, reason: contains not printable characters */
    public final C3019 f12099;

    /* renamed from: ᵧᵾˀˈ, reason: contains not printable characters */
    public final Context f12100;

    /* renamed from: ᵾᵬ, reason: contains not printable characters */
    public final WorkDatabase f12101;

    /* renamed from: ᵿʗᵝʽ, reason: contains not printable characters */
    public final C4757 f12102;

    static {
        ᵼᵫᵧˀ.ˁˑᵰ("SystemJobScheduler");
    }

    public C3198(Context context, WorkDatabase workDatabase, C3019 c3019) {
        JobScheduler jobSchedulerM5628 = AbstractC2042.m5628(context);
        C4757 c4757 = new C4757(context, c3019.f11642, c3019.f11640);
        this.f12100 = context;
        this.f12098 = jobSchedulerM5628;
        this.f12102 = c4757;
        this.f12101 = workDatabase;
        this.f12099 = c3019;
    }

    /* renamed from: ˎˏʜʾ, reason: contains not printable characters */
    public static C3084 m7546(JobInfo jobInfo) {
        PersistableBundle extras = jobInfo.getExtras();
        if (extras == null) {
            return null;
        }
        try {
            if (extras.containsKey("EXTRA_WORK_SPEC_ID")) {
                return new C3084(extras.getInt("EXTRA_WORK_SPEC_GENERATION", 0), extras.getString("EXTRA_WORK_SPEC_ID"));
            }
            return null;
        } catch (NullPointerException unused) {
            return null;
        }
    }

    /* renamed from: ˏˋٴ, reason: contains not printable characters */
    public static ArrayList m7547(Context context, JobScheduler jobScheduler) {
        List<JobInfo> allPendingJobs;
        int i = AbstractC2042.f8182;
        try {
            allPendingJobs = jobScheduler.getAllPendingJobs();
        } catch (Throwable unused) {
            ᵼᵫᵧˀ.ˀᵗᵝᵭ().getClass();
            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;
    }

    /* renamed from: ᵥᵯˎ, reason: contains not printable characters */
    public static void m7548(JobScheduler jobScheduler, int i) {
        try {
            jobScheduler.cancel(i);
        } catch (Throwable unused) {
            ᵼᵫᵧˀ r3 = ᵼᵫᵧˀ.ˀᵗᵝᵭ();
            String.format(Locale.getDefault(), "Exception while trying to cancel job (%d)", Integer.valueOf(i));
            r3.getClass();
        }
    }

    /* renamed from: ᵴᵳ, reason: contains not printable characters */
    public static ArrayList m7549(Context context, JobScheduler jobScheduler, String str) {
        ArrayList arrayListM7547 = m7547(context, jobScheduler);
        if (arrayListM7547 == null) {
            return null;
        }
        ArrayList arrayList = new ArrayList(2);
        int size = arrayListM7547.size();
        int i = 0;
        while (i < size) {
            Object obj = arrayListM7547.get(i);
            i++;
            JobInfo jobInfo = (JobInfo) obj;
            C3084 c3084M7546 = m7546(jobInfo);
            if (c3084M7546 != null && str.equals(c3084M7546.f11772)) {
                arrayList.add(Integer.valueOf(jobInfo.getId()));
            }
        }
        return arrayList;
    }

    /* JADX WARN: Type inference failed for: r5v0 */
    /* JADX WARN: Type inference failed for: r5v1, types: [boolean, int] */
    /* JADX WARN: Type inference failed for: r5v15 */
    @Override // defpackage.InterfaceC3764
    /* renamed from: ˇʼʤˎ */
    public final void mo4856(C3887... c3887Arr) {
        int iIntValue;
        boolean z;
        ArrayList arrayListM7549;
        int iIntValue2;
        WorkDatabase workDatabase = this.f12101;
        final C2852 c2852 = new C2852(workDatabase);
        int length = c3887Arr.length;
        ?? r5 = 0;
        int i = 0;
        while (i < length) {
            C3887 c3887 = c3887Arr[i];
            workDatabase.m683();
            try {
                C3887 c3887M6645 = workDatabase.mo678().m6645(c3887.f14482);
                if (c3887M6645 != null && c3887M6645.f14488 == EnumC4022.f15120) {
                    C3084 c3084M2168 = AbstractC0494.m2168(c3887);
                    int i2 = c3084M2168.f11773;
                    String str = c3084M2168.f11772;
                    C2450 c2450 = (C2450) ˊˉʤﹳ.ˌᵪ(workDatabase.mo685().f6961, true, (boolean) r5, new C4052(i2, r5, str));
                    C3019 c3019 = this.f12099;
                    WorkDatabase workDatabase2 = c2852.f11057;
                    if (c2450 != null) {
                        iIntValue = c2450.f9566;
                    } else {
                        c3019.getClass();
                        final int i3 = c3019.f11641;
                        iIntValue = ((Number) workDatabase2.m682(new Callable() { // from class: ˁᵝᵥᵖ
                            @Override // java.util.concurrent.Callable
                            public final Object call() {
                                WorkDatabase workDatabase3 = c2852.f11057;
                                Long lM7008 = workDatabase3.mo680().m7008("next_job_scheduler_id");
                                int i4 = 0;
                                int iLongValue = lM7008 != null ? (int) lM7008.longValue() : 0;
                                int i5 = iLongValue == Integer.MAX_VALUE ? 0 : iLongValue + 1;
                                C2866 c2866Mo680 = workDatabase3.mo680();
                                ˊˉʤﹳ.ˌᵪ(c2866Mo680.f11112, false, true, new ˈᵾᵣᴵ(c2866Mo680, 6, new C5401("next_job_scheduler_id", Long.valueOf(i5))));
                                if (iLongValue < 0 || iLongValue > i3) {
                                    C2866 c2866Mo6802 = workDatabase3.mo680();
                                    ˊˉʤﹳ.ˌᵪ(c2866Mo6802.f11112, false, true, new ˈᵾᵣᴵ(c2866Mo6802, 6, new C5401("next_job_scheduler_id", 1L)));
                                } else {
                                    i4 = iLongValue;
                                }
                                return Integer.valueOf(i4);
                            }
                        })).intValue();
                    }
                    if (c2450 == null) {
                        C2450 c24502 = new C2450(i2, iIntValue, str);
                        C1746 c1746Mo685 = workDatabase.mo685();
                        ˊˉʤﹳ.ˌᵪ(c1746Mo685.f6961, false, true, new ˈᵾᵣᴵ(c1746Mo685, 9, c24502));
                    }
                    m7550(c3887, iIntValue);
                    if (Build.VERSION.SDK_INT != 23 || (arrayListM7549 = m7549(this.f12100, this.f12098, c3887.f14482)) == null) {
                        z = false;
                    } else {
                        int iIndexOf = arrayListM7549.indexOf(Integer.valueOf(iIntValue));
                        if (iIndexOf >= 0) {
                            arrayListM7549.remove(iIndexOf);
                        }
                        if (arrayListM7549.isEmpty()) {
                            z = false;
                            c3019.getClass();
                            final int i4 = c3019.f11641;
                            iIntValue2 = ((Number) workDatabase2.m682(new Callable() { // from class: ˁᵝᵥᵖ
                                @Override // java.util.concurrent.Callable
                                public final Object call() {
                                    WorkDatabase workDatabase3 = c2852.f11057;
                                    Long lM7008 = workDatabase3.mo680().m7008("next_job_scheduler_id");
                                    int i42 = 0;
                                    int iLongValue = lM7008 != null ? (int) lM7008.longValue() : 0;
                                    int i5 = iLongValue == Integer.MAX_VALUE ? 0 : iLongValue + 1;
                                    C2866 c2866Mo680 = workDatabase3.mo680();
                                    ˊˉʤﹳ.ˌᵪ(c2866Mo680.f11112, false, true, new ˈᵾᵣᴵ(c2866Mo680, 6, new C5401("next_job_scheduler_id", Long.valueOf(i5))));
                                    if (iLongValue < 0 || iLongValue > i4) {
                                        C2866 c2866Mo6802 = workDatabase3.mo680();
                                        ˊˉʤﹳ.ˌᵪ(c2866Mo6802.f11112, false, true, new ˈᵾᵣᴵ(c2866Mo6802, 6, new C5401("next_job_scheduler_id", 1L)));
                                    } else {
                                        i42 = iLongValue;
                                    }
                                    return Integer.valueOf(i42);
                                }
                            })).intValue();
                        } else {
                            z = false;
                            iIntValue2 = ((Integer) arrayListM7549.get(0)).intValue();
                        }
                        m7550(c3887, iIntValue2);
                    }
                    workDatabase.m675();
                    i++;
                    r5 = z;
                } else {
                    ᵼᵫᵧˀ.ˀᵗᵝᵭ().getClass();
                    workDatabase.m675();
                    workDatabase.m668();
                    z = r5;
                    i++;
                    r5 = z;
                }
            } finally {
                workDatabase.m668();
            }
        }
    }

    @Override // defpackage.InterfaceC3764
    /* renamed from: ˏᵙ */
    public final boolean mo4859() {
        return true;
    }

    /* renamed from: ᵹᵝᵶ, reason: contains not printable characters */
    public final void m7550(C3887 c3887, int i) {
        List<JobInfo> allPendingJobs;
        String str;
        JobInfo jobInfoM10041 = this.f12102.m10041(c3887, i);
        ᵼᵫᵧˀ.ˀᵗᵝᵭ().getClass();
        try {
            if (this.f12098.schedule(jobInfoM10041) == 0) {
                ᵼᵫᵧˀ.ˀᵗᵝᵭ().getClass();
                if (c3887.f14480 && c3887.f14490 == 1) {
                    c3887.f14480 = false;
                    ᵼᵫᵧˀ.ˀᵗᵝᵭ().getClass();
                    m7550(c3887, i);
                }
            }
        } catch (IllegalStateException e) {
            int i2 = AbstractC2042.f8182;
            int i3 = Build.VERSION.SDK_INT;
            int i4 = i3 >= 31 ? 150 : 100;
            int size = ((List) ˊˉʤﹳ.ˌᵪ(this.f12101.mo678().f10260, true, false, new ᵭʽᵦ(12))).size();
            Context context = this.f12100;
            String strM6822 = "<faulty JobScheduler failed to getPendingJobs>";
            if (i3 >= 34) {
                JobScheduler jobSchedulerM5628 = AbstractC2042.m5628(context);
                String str2 = null;
                try {
                    allPendingJobs = jobSchedulerM5628.getAllPendingJobs();
                } catch (Throwable unused) {
                    ᵼᵫᵧˀ.ˀᵗᵝᵭ().getClass();
                    allPendingJobs = null;
                }
                if (allPendingJobs != null) {
                    ArrayList arrayListM7547 = m7547(context, jobSchedulerM5628);
                    int size2 = arrayListM7547 != null ? allPendingJobs.size() - arrayListM7547.size() : 0;
                    if (size2 == 0) {
                        str = null;
                    } else {
                        str = size2 + " of which are not owned by WorkManager";
                    }
                    ArrayList arrayListM75472 = m7547(context, (JobScheduler) context.getSystemService("jobscheduler"));
                    int size3 = arrayListM75472 != null ? arrayListM75472.size() : 0;
                    if (size3 != 0) {
                        str2 = size3 + " from WorkManager in the default namespace";
                    }
                    strM6822 = AbstractC2767.m6822(AbstractC1658.m4724(new String[]{allPendingJobs.size() + " jobs in \"androidx.work.systemjobscheduler\" namespace", str, str2}), ",\n", null, null, null, 62);
                }
            } else {
                ArrayList arrayListM75473 = m7547(context, AbstractC2042.m5628(context));
                if (arrayListM75473 != null) {
                    strM6822 = arrayListM75473.size() + " jobs from WorkManager";
                }
            }
            String str3 = "JobScheduler " + i4 + " job limit exceeded.\nIn JobScheduler there are " + strM6822 + ".\nThere are " + size + " jobs tracked by WorkManager's database;\nthe Configuration limit is " + this.f12099.f11639 + '.';
            ᵼᵫᵧˀ.ˀᵗᵝᵭ().getClass();
            C1260.m3810(str3, e);
        } catch (Throwable unused2) {
            ᵼᵫᵧˀ r8 = ᵼᵫᵧˀ.ˀᵗᵝᵭ();
            c3887.toString();
            r8.getClass();
        }
    }

    @Override // defpackage.InterfaceC3764
    /* renamed from: ᵻᵢﹳᵰ */
    public final void mo4862(String str) {
        Context context = this.f12100;
        JobScheduler jobScheduler = this.f12098;
        ArrayList arrayListM7549 = m7549(context, jobScheduler, str);
        if (arrayListM7549 == null || arrayListM7549.isEmpty()) {
            return;
        }
        int size = arrayListM7549.size();
        int i = 0;
        while (i < size) {
            Object obj = arrayListM7549.get(i);
            i++;
            m7548(jobScheduler, ((Integer) obj).intValue());
        }
        ˊˉʤﹳ.ˌᵪ(this.f12101.mo685().f6961, false, true, new C3449(6, str));
    }
}
