package o4;

import android.app.ActivityManager;
import android.app.AlarmManager;
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.os.Build;
import android.text.TextUtils;
import androidx.media3.common.C;
import androidx.work.WorkInfo$State;
import androidx.work.impl.WorkDatabase;
import androidx.work.impl.utils.ForceStopRunnable$BroadcastReceiver;
import androidx.work.u;
import com.google.android.gms.internal.atv_ads_framework.d1;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

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

    /* JADX INFO: renamed from: d, reason: collision with root package name */
    public static final long f31383d;

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

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

    /* JADX INFO: renamed from: c, reason: collision with root package name */
    public int f31386c = 0;

    static {
        u.e("ForceStopRunnable");
        f31383d = TimeUnit.DAYS.toMillis(3650L);
    }

    public e(Context context, androidx.work.impl.o oVar) {
        this.f31384a = context.getApplicationContext();
        this.f31385b = oVar;
    }

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

    /* JADX WARN: Removed duplicated region for block: B:50:0x0109  */
    /* JADX WARN: Removed duplicated region for block: B:63:? A[RETURN, SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
    */
    public final void a() {
        PendingIntent broadcast;
        Context context = this.f31384a;
        androidx.work.impl.o oVar = this.f31385b;
        boolean zF = j4.c.f(context, oVar);
        WorkDatabase workDatabase = oVar.f4625c;
        n4.p pVarWorkSpecDao = workDatabase.workSpecDao();
        n4.l lVarWorkProgressDao = workDatabase.workProgressDao();
        workDatabase.beginTransaction();
        try {
            n4.r rVar = (n4.r) pVarWorkSpecDao;
            ArrayList<n4.o> arrayListJ = rVar.j();
            boolean z10 = !arrayListJ.isEmpty();
            if (z10) {
                for (n4.o oVar2 : arrayListJ) {
                    rVar.A(WorkInfo$State.f4516a, oVar2.f30818a);
                    rVar.v(-1L, oVar2.f30818a);
                }
            }
            ((u.e) lVarWorkProgressDao).g();
            workDatabase.setTransactionSuccessful();
            workDatabase.endTransaction();
            boolean z11 = z10 || zF;
            Long longValue = ((d1) oVar.f4629g.f31388a.preferenceDao()).getLongValue("reschedule_needed");
            if (longValue != null && longValue.longValue() == 1) {
                u.c().a(new Throwable[0]);
                oVar.e();
                h hVar = oVar.f4629g;
                hVar.getClass();
                ((d1) hVar.f31388a.preferenceDao()).k(new n4.d("reschedule_needed", 0L));
                return;
            }
            try {
                int i10 = m0.a.b() ? 570425344 : C.BUFFER_FLAG_LAST_SAMPLE;
                Intent intent = new Intent();
                intent.setComponent(new ComponentName(context, (Class<?>) ForceStopRunnable$BroadcastReceiver.class));
                intent.setAction("ACTION_FORCE_STOP_RESCHEDULE");
                broadcast = PendingIntent.getBroadcast(context, -1, intent, i10);
            } catch (IllegalArgumentException e10) {
                e = e10;
                u.c().f(e);
            } catch (SecurityException e11) {
                e = e11;
                u.c().f(e);
            }
            if (Build.VERSION.SDK_INT >= 30) {
                if (broadcast != null) {
                    broadcast.cancel();
                }
                List historicalProcessExitReasons = ((ActivityManager) context.getSystemService("activity")).getHistoricalProcessExitReasons(null, 0, 0);
                if (historicalProcessExitReasons != null && !historicalProcessExitReasons.isEmpty()) {
                    for (int i11 = 0; i11 < historicalProcessExitReasons.size(); i11++) {
                        if (io.flutter.plugin.editing.a.b(historicalProcessExitReasons.get(i11)).getReason() == 10) {
                            u.c().a(new Throwable[0]);
                            oVar.e();
                            return;
                        }
                    }
                }
                if (z11) {
                    return;
                }
                u.c().a(new Throwable[0]);
                androidx.work.impl.f.a(oVar.f4624b, oVar.f4625c, oVar.f4627e);
                return;
            }
            if (broadcast == null) {
                c(context);
                u.c().a(new Throwable[0]);
                oVar.e();
                return;
            }
            if (z11) {
            }
        } finally {
            workDatabase.endTransaction();
        }
    }

    public final boolean b() {
        androidx.work.c cVar = this.f31385b.f4624b;
        if (TextUtils.isEmpty(cVar.getDefaultProcessName())) {
            u.c().a(new Throwable[0]);
            return true;
        }
        Context context = this.f31384a;
        String processName = i.getProcessName(context);
        boolean zEquals = !TextUtils.isEmpty(cVar.getDefaultProcessName()) ? TextUtils.equals(processName, cVar.getDefaultProcessName()) : TextUtils.equals(processName, context.getApplicationInfo().processName);
        u uVarC = u.c();
        String.format("Is default app process = %s", Boolean.valueOf(zEquals));
        uVarC.a(new Throwable[0]);
        return zEquals;
    }

    @Override // java.lang.Runnable
    public final void run() {
        androidx.work.impl.o oVar = this.f31385b;
        try {
            if (!b()) {
                return;
            }
            while (true) {
                androidx.work.impl.m.a(this.f31384a);
                u.c().a(new Throwable[0]);
                try {
                    a();
                    return;
                } catch (SQLiteAccessPermException | SQLiteCantOpenDatabaseException | SQLiteConstraintException | SQLiteDatabaseCorruptException | SQLiteDatabaseLockedException | SQLiteTableLockedException e10) {
                    int i10 = this.f31386c + 1;
                    this.f31386c = i10;
                    if (i10 >= 3) {
                        u.c().b(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);
                        oVar.f4624b.getExceptionHandler();
                        throw illegalStateException;
                    }
                    long j10 = ((long) i10) * 300;
                    u uVarC = u.c();
                    String.format("Retrying after %s", Long.valueOf(j10));
                    uVarC.a(e10);
                    try {
                        Thread.sleep(((long) this.f31386c) * 300);
                    } catch (InterruptedException unused) {
                    }
                }
            }
        } finally {
            oVar.d();
        }
    }
}
