package androidx.work.impl.utils;

import a6.f;
import a6.h;
import a6.i;
import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.ApplicationExitInfo;
import android.app.PendingIntent;
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.SQLiteTableLockedException;
import android.text.TextUtils;
import androidx.core.os.a;
import androidx.work.impl.WorkDatabase;
import c6.c;
import h6.p;
import h6.r;
import h6.s;
import java.util.List;
import java.util.concurrent.TimeUnit;
import z5.k;
import z5.t;

/* loaded from: classes.dex */
public class ForceStopRunnable implements Runnable {

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

    /* renamed from: t, reason: collision with root package name */
    private static final long f5998t = TimeUnit.DAYS.toMillis(3650);

    /* renamed from: p, reason: collision with root package name */
    private final Context f5999p;

    /* renamed from: q, reason: collision with root package name */
    private final i f6000q;

    /* renamed from: r, reason: collision with root package name */
    private int f6001r = 0;

    /* loaded from: classes.dex */
    public static class BroadcastReceiver extends android.content.BroadcastReceiver {

        /* renamed from: a, reason: collision with root package name */
        private static final String f6002a = k.f("ForceStopRunnable$Rcvr");

        @Override // android.content.BroadcastReceiver
        public void onReceive(Context context, Intent intent) {
            if (intent == null || !"ACTION_FORCE_STOP_RESCHEDULE".equals(intent.getAction())) {
                return;
            }
            k.c().g(f6002a, "Rescheduling alarm that keeps track of force-stops.", new Throwable[0]);
            ForceStopRunnable.g(context);
        }
    }

    public ForceStopRunnable(Context context, i iVar) {
        this.f5999p = context.getApplicationContext();
        this.f6000q = iVar;
    }

    static Intent c(Context context) {
        Intent intent = new Intent();
        intent.setComponent(new ComponentName(context, (Class<?>) BroadcastReceiver.class));
        intent.setAction("ACTION_FORCE_STOP_RESCHEDULE");
        return intent;
    }

    private static PendingIntent d(Context context, int i10) {
        return PendingIntent.getBroadcast(context, -1, c(context), i10);
    }

    static void g(Context context) {
        AlarmManager alarmManager = (AlarmManager) context.getSystemService("alarm");
        PendingIntent d10 = d(context, a.b() ? 167772160 : 134217728);
        long currentTimeMillis = System.currentTimeMillis() + f5998t;
        if (alarmManager != null) {
            alarmManager.setExact(0, currentTimeMillis, d10);
        }
    }

    public boolean a() {
        boolean f10 = c.f(this.f5999p, this.f6000q);
        WorkDatabase s10 = this.f6000q.s();
        s O = s10.O();
        p N = s10.N();
        s10.e();
        try {
            List<r> j10 = O.j();
            boolean z10 = (j10 == null || j10.isEmpty()) ? false : true;
            if (z10) {
                for (r rVar : j10) {
                    O.i(t.a.ENQUEUED, rVar.f20890a);
                    O.b(rVar.f20890a, -1L);
                }
            }
            N.c();
            s10.C();
            return z10 || f10;
        } finally {
            s10.i();
        }
    }

    public void b() {
        boolean a10 = a();
        if (h()) {
            k.c().a(f5997s, "Rescheduling Workers.", new Throwable[0]);
            this.f6000q.x();
            this.f6000q.p().d(false);
        } else if (e()) {
            k.c().a(f5997s, "Application was force-stopped, rescheduling.", new Throwable[0]);
            this.f6000q.x();
        } else if (a10) {
            k.c().a(f5997s, "Found unfinished work, scheduling it.", new Throwable[0]);
            f.b(this.f6000q.m(), this.f6000q.s(), this.f6000q.r());
        }
    }

    public boolean e() {
        try {
            PendingIntent d10 = d(this.f5999p, a.b() ? 570425344 : 536870912);
            if (d10 != null) {
                d10.cancel();
            }
            List<ApplicationExitInfo> historicalProcessExitReasons = ((ActivityManager) this.f5999p.getSystemService("activity")).getHistoricalProcessExitReasons(null, 0, 0);
            if (historicalProcessExitReasons != null && !historicalProcessExitReasons.isEmpty()) {
                for (int i10 = 0; i10 < historicalProcessExitReasons.size(); i10++) {
                    if (historicalProcessExitReasons.get(i10).getReason() == 10) {
                        return true;
                    }
                }
            }
            return false;
        } catch (IllegalArgumentException | SecurityException e10) {
            k.c().h(f5997s, "Ignoring exception", e10);
            return true;
        }
    }

    public boolean f() {
        androidx.work.a m10 = this.f6000q.m();
        if (TextUtils.isEmpty(m10.c())) {
            k.c().a(f5997s, "The default process name was not specified.", new Throwable[0]);
            return true;
        }
        boolean b10 = i6.f.b(this.f5999p, m10);
        k.c().a(f5997s, String.format("Is default app process = %s", Boolean.valueOf(b10)), new Throwable[0]);
        return b10;
    }

    boolean h() {
        return this.f6000q.p().a();
    }

    public void i(long j10) {
        try {
            Thread.sleep(j10);
        } catch (InterruptedException unused) {
        }
    }

    @Override // java.lang.Runnable
    public void run() {
        try {
            if (!f()) {
                return;
            }
            while (true) {
                h.e(this.f5999p);
                k.c().a(f5997s, "Performing cleanup operations.", new Throwable[0]);
                try {
                    b();
                    return;
                } catch (SQLiteAccessPermException | SQLiteCantOpenDatabaseException | SQLiteConstraintException | SQLiteDatabaseCorruptException | SQLiteDatabaseLockedException | SQLiteTableLockedException e10) {
                    int i10 = this.f6001r + 1;
                    this.f6001r = i10;
                    if (i10 >= 3) {
                        k.c().b(f5997s, "The file system on the device is in a bad state. WorkManager cannot access the app's internal data store.", e10);
                        IllegalStateException illegalStateException = new IllegalStateException("The file system on the device is in a bad state. WorkManager cannot access the app's internal data store.", e10);
                        this.f6000q.m().d();
                        throw illegalStateException;
                    }
                    k.c().a(f5997s, String.format("Retrying after %s", Long.valueOf(i10 * 300)), e10);
                    i(this.f6001r * 300);
                }
            }
        } finally {
            this.f6000q.w();
        }
    }
}
