package com.adobe.marketing.mobile.internal.util;

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import java.io.File;
import wg.t;

/* loaded from: classes4.dex */
public abstract class g {

    /* loaded from: classes4.dex */
    public enum a {
        READ_ONLY(1),
        READ_WRITE(0);

        final int mode;

        a(int i10) {
            this.mode = i10;
        }
    }

    /* JADX WARN: Multi-variable type inference failed */
    public static boolean a(String str, String str2) {
        SQLiteException e10;
        SQLiteDatabase sQLiteDatabase;
        SQLiteDatabase sQLiteDatabase2 = 0;
        try {
            try {
                sQLiteDatabase = e(str, a.READ_WRITE);
                try {
                    sQLiteDatabase.delete(str2, "1", null);
                    b(sQLiteDatabase);
                    return true;
                } catch (SQLiteException e11) {
                    e10 = e11;
                    t.f("MobileCore", "SQLiteDatabaseHelper", String.format("clearTable - Error in clearing table(%s). Returning false. Error: (%s)", str2, e10.getMessage()), new Object[0]);
                    b(sQLiteDatabase);
                    return false;
                }
            } catch (Throwable th2) {
                th = th2;
                sQLiteDatabase2 = str;
                b(sQLiteDatabase2);
                throw th;
            }
        } catch (SQLiteException e12) {
            e10 = e12;
            sQLiteDatabase = null;
        } catch (Throwable th3) {
            th = th3;
            b(sQLiteDatabase2);
            throw th;
        }
    }

    public static void b(SQLiteDatabase sQLiteDatabase) {
        if (sQLiteDatabase == null) {
            t.a("MobileCore", "SQLiteDatabaseHelper", "closeDatabase - Unable to close database, database passed is null.", new Object[0]);
        } else {
            sQLiteDatabase.close();
            t.e("MobileCore", "SQLiteDatabaseHelper", "closeDatabase - Successfully closed the database.", new Object[0]);
        }
    }

    public static boolean c(String str, String str2) {
        SQLiteDatabase sQLiteDatabase = null;
        try {
            sQLiteDatabase = e(str, a.READ_WRITE);
            sQLiteDatabase.execSQL(str2);
            return true;
        } catch (SQLiteException e10) {
            t.f("MobileCore", "SQLiteDatabaseHelper", String.format("createTableIfNotExists - Error in creating/accessing table. Error: (%s)", e10.getMessage()), new Object[0]);
            return false;
        } finally {
            b(sQLiteDatabase);
        }
    }

    public static int d(String str, String str2) {
        SQLiteDatabase sQLiteDatabase = null;
        try {
            sQLiteDatabase = e(str, a.READ_ONLY);
            return (int) sQLiteDatabase.compileStatement("Select Count (*) from " + str2).simpleQueryForLong();
        } catch (SQLiteException e10) {
            t.f("MobileCore", "SQLiteDatabaseHelper", String.format("getTableSize - Error in querying table(%s) size. Returning 0. Error: (%s)", str2, e10.getMessage()), new Object[0]);
            return 0;
        } finally {
            b(sQLiteDatabase);
        }
    }

    public static SQLiteDatabase e(String str, a aVar) {
        if (str == null || str.isEmpty()) {
            t.a("MobileCore", "SQLiteDatabaseHelper", "openDatabase - Failed to open database - filepath is null or empty", new Object[0]);
            throw new SQLiteException("Invalid database path. Database path is null or empty.");
        }
        try {
            File parentFile = new File(str).getParentFile();
            if (parentFile != null && !parentFile.exists()) {
                t.a("MobileCore", "SQLiteDatabaseHelper", "openDatabase - Creating parent directory (%s)", parentFile.getPath());
                parentFile.mkdirs();
            }
            SQLiteDatabase openDatabase = SQLiteDatabase.openDatabase(str, null, aVar.mode | 268435472);
            t.e("MobileCore", "SQLiteDatabaseHelper", String.format("openDatabase - Successfully opened the database at path (%s)", str), new Object[0]);
            return openDatabase;
        } catch (Exception unused) {
            t.a("MobileCore", "SQLiteDatabaseHelper", "openDatabase - Failed to create parent directory for path (%s)", str);
            throw new SQLiteException("Invalid database path. Unable to create parent directory for database.");
        }
    }

    public static boolean f(String str, a aVar, com.adobe.marketing.mobile.internal.util.a aVar2) {
        SQLiteDatabase sQLiteDatabase = null;
        try {
            try {
                sQLiteDatabase = e(str, aVar);
                boolean a10 = aVar2.a(sQLiteDatabase);
                if (sQLiteDatabase != null) {
                    b(sQLiteDatabase);
                }
                return a10;
            } catch (Exception e10) {
                t.f("MobileCore", "SQLiteDatabaseHelper", "Failed to open database -" + e10.getLocalizedMessage(), new Object[0]);
                if (sQLiteDatabase != null) {
                    b(sQLiteDatabase);
                }
                return false;
            }
        } catch (Throwable th2) {
            if (sQLiteDatabase != null) {
                b(sQLiteDatabase);
            }
            throw th2;
        }
    }
}
