package xyz.stream.download;

import android.content.Context;
import android.database.Cursor;
import androidx.annotation.NonNull;
import androidx.room.Database;
import androidx.room.RoomDatabase;
import androidx.room.i0;
import androidx.room.j0;
import com.google.android.gms.measurement.internal.u1;
import java.io.File;
import xyz.stream.download.config.InnerConstant;
import xyz.stream.download.db.DownloadInfoEntity;
import xyz.stream.download.db.FileInfoDao;

/* JADX INFO: loaded from: classes3.dex */
@Database(entities = {DownloadFile.class, DownloadInfoEntity.class}, exportSchema = false, version = 2)
public abstract class DownloadDatabase extends RoomDatabase {
    private static final String LEGACY_DATABASE_ALIAS = "legacy_download_db";
    private static final g3.a MIGRATION_1_2 = new g3.a(1, 2) { // from class: xyz.stream.download.DownloadDatabase.1
        @Override // g3.a
        public void migrate(@NonNull l3.b bVar) {
            bVar.j("CREATE TABLE IF NOT EXISTS `download_info` (`id` TEXT NOT NULL, `downloadUrl` TEXT, `filePath` TEXT, `size` INTEGER NOT NULL, `downloadLocation` INTEGER NOT NULL, `downloadStatus` INTEGER NOT NULL, PRIMARY KEY(`id`))");
        }
    };
    private static final String TAG = "DownloadDatabase";
    private static volatile DownloadDatabase instance;

    public static void closeInstance() {
        synchronized (DownloadDatabase.class) {
            try {
                if (instance != null) {
                    instance.close();
                    instance = null;
                }
            } catch (Throwable th) {
                throw th;
            }
        }
    }

    public static DownloadDatabase createFromFile(Context context) {
        if (instance == null) {
            synchronized (DownloadDatabase.class) {
                try {
                    if (instance == null) {
                        final Context applicationContext = context.getApplicationContext();
                        i0 i0VarD = com.bumptech.glide.g.d(applicationContext, DownloadDatabase.class, DownloadConstant.DATABASE_DOWNLOAD_NAME);
                        i0VarD.a(MIGRATION_1_2);
                        i0VarD.f4095d.add(new j0() { // from class: xyz.stream.download.DownloadDatabase.2
                            @Override // androidx.room.j0
                            public void onOpen(@NonNull l3.b bVar) {
                                DownloadDatabase.migrateLegacyDownloadDatabase(applicationContext, bVar);
                            }
                        });
                        i0VarD.f4100i = true;
                        instance = (DownloadDatabase) i0VarD.b();
                    }
                } finally {
                }
            }
        }
        return instance;
    }

    private static void deleteIfExists(File file) {
        if (!file.exists() || file.delete()) {
            return;
        }
        file.getAbsolutePath();
    }

    private static void deleteLegacyDatabase(Context context, File file) {
        if (context.deleteDatabase(InnerConstant.Db.NAME_DB)) {
            return;
        }
        deleteIfExists(file);
        deleteIfExists(new File(file.getAbsolutePath() + "-journal"));
        deleteIfExists(new File(file.getAbsolutePath() + "-shm"));
        deleteIfExists(new File(file.getAbsolutePath() + "-wal"));
    }

    private static String escapeSql(String str) {
        return str.replace("'", "''");
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static void migrateLegacyDownloadDatabase(Context context, l3.b bVar) {
        File databasePath = context.getDatabasePath(InnerConstant.Db.NAME_DB);
        if (databasePath.exists()) {
            boolean z10 = false;
            try {
                bVar.j("ATTACH DATABASE '" + escapeSql(databasePath.getAbsolutePath()) + "' AS legacy_download_db");
                z10 = true;
                if (tableExists(bVar, LEGACY_DATABASE_ALIAS, InnerConstant.Db.NAME_TABALE)) {
                    bVar.j("INSERT OR REPLACE INTO `download_info` (`id`, `downloadUrl`, `filePath`, `size`, `downloadLocation`, `downloadStatus`) SELECT `id`, `downloadUrl`, `filePath`, COALESCE(`size`, 0), COALESCE(`downloadLocation`, 0), COALESCE(`downloadStatus`, 0) FROM legacy_download_db.`download_info` WHERE `id` IS NOT NULL AND `id` != ''");
                }
                try {
                    bVar.j("DETACH DATABASE legacy_download_db");
                } catch (Exception unused) {
                }
                deleteLegacyDatabase(context, databasePath);
            } catch (Exception unused2) {
                if (z10) {
                    try {
                        bVar.j("DETACH DATABASE legacy_download_db");
                    } catch (Exception unused3) {
                    }
                }
            } catch (Throwable th) {
                if (z10) {
                    try {
                        bVar.j("DETACH DATABASE legacy_download_db");
                    } catch (Exception unused4) {
                    }
                }
                throw th;
            }
        }
    }

    private static boolean tableExists(l3.b bVar, String str, String str2) {
        Cursor cursorH = bVar.h(u1.k("SELECT name FROM ", str, ".sqlite_master WHERE type='table' AND name=?"), new Object[]{str2});
        try {
            return cursorH.moveToFirst();
        } finally {
            cursorH.close();
        }
    }

    public abstract DownloadDao dao();

    public abstract FileInfoDao fileInfoDao();
}
