package androidx.work.impl.utils;

import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.ApplicationExitInfo;
import android.app.PendingIntent;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteAccessPermException;
import android.database.sqlite.SQLiteCantOpenDatabaseException;
import android.database.sqlite.SQLiteConstraintException;
import android.database.sqlite.SQLiteDatabaseCorruptException;
import android.database.sqlite.SQLiteDatabaseLockedException;
import android.database.sqlite.SQLiteDiskIOException;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteTableLockedException;
import android.os.Build;
import android.text.TextUtils;
import androidx.media3.common.C;
import androidx.work.a;
import androidx.work.impl.WorkDatabase;
import defpackage.eh5;
import defpackage.f3;
import defpackage.gsa;
import defpackage.hsa;
import defpackage.jg7;
import defpackage.lba;
import defpackage.lta;
import defpackage.m38;
import defpackage.mi9;
import defpackage.mta;
import defpackage.osa;
import defpackage.si7;
import defpackage.vf7;
import defpackage.yb8;
import defpackage.ysa;
import defpackage.zra;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.TimeUnit;

/* JADX INFO: loaded from: classes.dex */
@m38
public class ForceStopRunnable implements Runnable {
    public static final String e = eh5.d("ForceStopRunnable");
    public static final long f = TimeUnit.DAYS.toMillis(3650);
    public final Context a;
    public final osa b;
    public final jg7 c;
    public int d = 0;

    @m38
    public static class BroadcastReceiver extends android.content.BroadcastReceiver {
        static {
            eh5.d("ForceStopRunnable$Rcvr");
        }

        @Override // android.content.BroadcastReceiver
        public final void onReceive(Context context, Intent intent) {
            if (intent == null || !"ACTION_FORCE_STOP_RESCHEDULE".equals(intent.getAction())) {
                return;
            }
            eh5.c().getClass();
            ForceStopRunnable.b(context);
        }
    }

    public ForceStopRunnable(Context context, osa osaVar) {
        this.a = context.getApplicationContext();
        this.b = osaVar;
        this.c = osaVar.g;
    }

    public static void b(Context context) {
        AlarmManager alarmManager = (AlarmManager) context.getSystemService("alarm");
        int i = Build.VERSION.SDK_INT >= 31 ? 167772160 : C.BUFFER_FLAG_FIRST_SAMPLE;
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(context, (Class<?>) BroadcastReceiver.class));
        intent.setAction("ACTION_FORCE_STOP_RESCHEDULE");
        PendingIntent broadcast = PendingIntent.getBroadcast(context, -1, intent, i);
        long jCurrentTimeMillis = System.currentTimeMillis() + f;
        if (alarmManager != null) {
            alarmManager.setExact(0, jCurrentTimeMillis, broadcast);
        }
    }

    /* JADX WARN: Finally extract failed */
    /* JADX WARN: Removed duplicated region for block: B:118:? A[RETURN, SYNTHETIC] */
    /* JADX WARN: Removed duplicated region for block: B:91:0x01b9  */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void a() {
        boolean z;
        int i;
        PendingIntent broadcast;
        jg7 jg7Var = this.c;
        osa osaVar = this.b;
        WorkDatabase workDatabase = osaVar.c;
        a aVar = osaVar.b;
        jg7 jg7Var2 = osaVar.g;
        workDatabase = osaVar.c;
        String str = mi9.f;
        Context context = this.a;
        JobScheduler jobScheduler = (JobScheduler) context.getSystemService("jobscheduler");
        ArrayList<JobInfo> arrayListD = mi9.d(context, jobScheduler);
        ArrayList arrayListC = workDatabase.c().c();
        HashSet hashSet = new HashSet(arrayListD != null ? arrayListD.size() : 0);
        if (arrayListD != null && !arrayListD.isEmpty()) {
            for (JobInfo jobInfo : arrayListD) {
                gsa gsaVarF = mi9.f(jobInfo);
                if (gsaVarF != null) {
                    hashSet.add(gsaVarF.a);
                } else {
                    mi9.a(jobScheduler, jobInfo.getId());
                }
            }
        }
        Iterator it = arrayListC.iterator();
        while (true) {
            if (it.hasNext()) {
                if (!hashSet.contains((String) it.next())) {
                    eh5 eh5VarC = eh5.c();
                    String str2 = mi9.f;
                    eh5VarC.getClass();
                    z = true;
                    break;
                }
            } else {
                z = false;
                break;
            }
        }
        if (z) {
            workDatabase.beginTransaction();
            try {
                mta mtaVarF = workDatabase.f();
                Iterator it2 = arrayListC.iterator();
                while (it2.hasNext()) {
                    mtaVarF.c(-1L, (String) it2.next());
                }
                workDatabase.setTransactionSuccessful();
                workDatabase.endTransaction();
            } catch (Throwable th) {
                throw th;
            }
        }
        mta mtaVarF2 = workDatabase.f();
        ysa ysaVarE = workDatabase.e();
        workDatabase.beginTransaction();
        try {
            ArrayList<lta> arrayListU = mtaVarF2.u();
            boolean zIsEmpty = arrayListU.isEmpty();
            if (!zIsEmpty) {
                for (lta ltaVar : arrayListU) {
                    hsa.c cVar = hsa.c.a;
                    String str3 = ltaVar.a;
                    mtaVarF2.e(cVar, str3);
                    mtaVarF2.setStopReason(str3, -512);
                    mtaVarF2.c(-1L, str3);
                }
            }
            ysaVarE.c();
            workDatabase.setTransactionSuccessful();
            workDatabase.endTransaction();
            boolean z2 = !zIsEmpty || z;
            Long lA = jg7Var2.a.b().a("reschedule_needed");
            if (lA != null && lA.longValue() == 1) {
                eh5.c().getClass();
                osaVar.g();
                jg7Var2.getClass();
                jg7Var2.a.b().b(new vf7("reschedule_needed", 0L));
                return;
            }
            try {
                i = Build.VERSION.SDK_INT;
                int i2 = i >= 31 ? 570425344 : 536870912;
                Intent intent = new Intent();
                intent.setComponent(new ComponentName(context, (Class<?>) BroadcastReceiver.class));
                intent.setAction("ACTION_FORCE_STOP_RESCHEDULE");
                broadcast = PendingIntent.getBroadcast(context, -1, intent, i2);
            } catch (IllegalArgumentException e2) {
                e = e2;
                eh5.c().f(e, "Ignoring exception", e);
            } catch (SecurityException e3) {
                e = e3;
                eh5.c().f(e, "Ignoring exception", e);
            }
            if (i >= 30) {
                if (broadcast != null) {
                    broadcast.cancel();
                }
                List historicalProcessExitReasons = ((ActivityManager) context.getSystemService("activity")).getHistoricalProcessExitReasons(null, 0, 0);
                if (historicalProcessExitReasons != null && !historicalProcessExitReasons.isEmpty()) {
                    Long lA2 = jg7Var.a.b().a("last_force_stop_ms");
                    long jLongValue = lA2 != null ? lA2.longValue() : 0L;
                    for (int i3 = 0; i3 < historicalProcessExitReasons.size(); i3++) {
                        ApplicationExitInfo applicationExitInfoC = f3.c(historicalProcessExitReasons.get(i3));
                        if (applicationExitInfoC.getReason() == 10 && applicationExitInfoC.getTimestamp() >= jLongValue) {
                            eh5.c().getClass();
                            osaVar.g();
                            aVar.c.getClass();
                            long jCurrentTimeMillis = System.currentTimeMillis();
                            jg7Var.getClass();
                            jg7Var.a.b().b(new vf7("last_force_stop_ms", Long.valueOf(jCurrentTimeMillis)));
                            return;
                        }
                    }
                }
                if (z2) {
                    return;
                }
                eh5.c().getClass();
                yb8.b(aVar, workDatabase, osaVar.e);
                return;
            }
            if (broadcast == null) {
                b(context);
                eh5.c().getClass();
                osaVar.g();
                aVar.c.getClass();
                long jCurrentTimeMillis2 = System.currentTimeMillis();
                jg7Var.getClass();
                jg7Var.a.b().b(new vf7("last_force_stop_ms", Long.valueOf(jCurrentTimeMillis2)));
                return;
            }
            if (z2) {
            }
        } finally {
            workDatabase.endTransaction();
        }
    }

    @Override // java.lang.Runnable
    public final void run() {
        boolean zA;
        String str = e;
        osa osaVar = this.b;
        try {
            a aVar = osaVar.b;
            aVar.getClass();
            boolean zIsEmpty = TextUtils.isEmpty(null);
            Context context = this.a;
            if (zIsEmpty) {
                eh5.c().getClass();
                zA = true;
            } else {
                zA = si7.a(context, aVar);
                eh5.c().getClass();
            }
            if (!zA) {
                return;
            }
            while (true) {
                try {
                    zra.a(context);
                    eh5.c().getClass();
                    try {
                        a();
                        return;
                    } catch (SQLiteAccessPermException | SQLiteCantOpenDatabaseException | SQLiteConstraintException | SQLiteDatabaseCorruptException | SQLiteDatabaseLockedException | SQLiteDiskIOException | SQLiteTableLockedException e2) {
                        int i = this.d + 1;
                        this.d = i;
                        if (i >= 3) {
                            String str2 = lba.a(context) ? "The file system on the device is in a bad state. WorkManager cannot access the app's internal data store." : "WorkManager can't be accessed from direct boot, because credential encrypted storage isn't accessible.\nDon't access or initialise WorkManager from directAware components. See https://developer.android.com/training/articles/direct-boot";
                            eh5.c().b(str, str2, e2);
                            IllegalStateException illegalStateException = new IllegalStateException(str2, e2);
                            osaVar.b.getClass();
                            throw illegalStateException;
                        }
                        eh5.c().getClass();
                        try {
                            Thread.sleep(((long) this.d) * 300);
                        } catch (InterruptedException unused) {
                        }
                    }
                } catch (SQLiteException e3) {
                    eh5.c().a(str, "Unexpected SQLite exception during migrations");
                    IllegalStateException illegalStateException2 = new IllegalStateException("Unexpected SQLite exception during migrations", e3);
                    osaVar.b.getClass();
                    throw illegalStateException2;
                }
            }
        } finally {
            osaVar.f();
        }
    }
}
