package IMuAHx_6.KQgRr5e_0.KQgRr5e_0.XnFJ5_9.Mqv2r_3;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Build;
import android.util.SparseArray;
import androidx.transition.Transition;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
/* compiled from: BreakpointSQLiteHelper.java */
/* loaded from: classes.dex */
public class X6FRs_4 extends SQLiteOpenHelper {
    public X6FRs_4(Context context) {
        super(context, "okdownload-breakpoint.db", (SQLiteDatabase.CursorFactory) null, 3);
    }

    public void KQgRr5e_0(int i) {
        getWritableDatabase().delete("taskFileDirty", "id = ?", new String[]{String.valueOf(i)});
    }

    public void Mqv2r_3(int i) {
        getWritableDatabase().delete("breakpoint", "id = ?", new String[]{String.valueOf(i)});
        PGPF6hQ_2(i);
    }

    public void PGPF6hQ_2(int i) {
        getWritableDatabase().delete("block", "breakpoint_id = ?", new String[]{String.valueOf(i)});
    }

    public void QxJ9d_1(int i) {
        SQLiteDatabase writableDatabase = getWritableDatabase();
        ContentValues contentValues = new ContentValues(1);
        contentValues.put(Transition.MATCH_ID_STR, Integer.valueOf(i));
        writableDatabase.insert("taskFileDirty", null, contentValues);
    }

    @Override // android.database.sqlite.SQLiteOpenHelper
    public void onCreate(SQLiteDatabase sQLiteDatabase) {
        sQLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS breakpoint( id INTEGER PRIMARY KEY, url VARCHAR NOT NULL, etag VARCHAR, parent_path VARCHAR NOT NULL, filename VARCHAR, task_only_parent_path TINYINT(1) DEFAULT 0, chunked TINYINT(1) DEFAULT 0)");
        sQLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS block( id INTEGER PRIMARY KEY AUTOINCREMENT, breakpoint_id INTEGER, block_index INTEGER, start_offset INTEGER, content_length INTEGER, current_offset INTEGER)");
        sQLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS okdownloadResponseFilename( url VARCHAR NOT NULL PRIMARY KEY, filename VARCHAR NOT NULL)");
        sQLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS taskFileDirty( id INTEGER PRIMARY KEY)");
    }

    @Override // android.database.sqlite.SQLiteOpenHelper
    public void onDowngrade(SQLiteDatabase sQLiteDatabase, int i, int i2) {
    }

    @Override // android.database.sqlite.SQLiteOpenHelper
    public void onOpen(SQLiteDatabase sQLiteDatabase) {
        super.onOpen(sQLiteDatabase);
        if (Build.VERSION.SDK_INT >= 16) {
            setWriteAheadLoggingEnabled(true);
        } else {
            sQLiteDatabase.enableWriteAheadLogging();
        }
    }

    @Override // android.database.sqlite.SQLiteOpenHelper
    public void onUpgrade(SQLiteDatabase sQLiteDatabase, int i, int i2) {
        if (i == 1 && i2 == 2) {
            sQLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS okdownloadResponseFilename( url VARCHAR NOT NULL PRIMARY KEY, filename VARCHAR NOT NULL)");
        }
        if (i <= 2) {
            sQLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS taskFileDirty( id INTEGER PRIMARY KEY)");
        }
    }

    /* renamed from: KQgRr5e_0  reason: collision with other method in class */
    public List<Integer> m99KQgRr5e_0() {
        ArrayList arrayList = new ArrayList();
        Cursor cursor = null;
        try {
            cursor = getWritableDatabase().rawQuery("SELECT * FROM taskFileDirty", null);
            while (cursor.moveToNext()) {
                arrayList.add(Integer.valueOf(cursor.getInt(cursor.getColumnIndex(Transition.MATCH_ID_STR))));
            }
            return arrayList;
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

    public void QxJ9d_1(PGPF6hQ_2 pGPF6hQ_2) throws IOException {
        SQLiteDatabase writableDatabase = getWritableDatabase();
        writableDatabase.beginTransaction();
        Cursor cursor = null;
        try {
            SQLiteDatabase writableDatabase2 = getWritableDatabase();
            cursor = writableDatabase2.rawQuery("SELECT id FROM breakpoint WHERE id =" + pGPF6hQ_2.KQgRr5e_0 + " LIMIT 1", null);
            if (cursor.moveToNext()) {
                Mqv2r_3(pGPF6hQ_2.KQgRr5e_0);
                m100KQgRr5e_0(pGPF6hQ_2);
                writableDatabase.setTransactionSuccessful();
                if (cursor != null) {
                    cursor.close();
                }
                writableDatabase.endTransaction();
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
            writableDatabase.endTransaction();
        }
    }

    public SparseArray<PGPF6hQ_2> KQgRr5e_0() {
        Cursor cursor;
        SQLiteDatabase writableDatabase = getWritableDatabase();
        ArrayList<Mqv2r_3> arrayList = new ArrayList();
        ArrayList arrayList2 = new ArrayList();
        Cursor cursor2 = null;
        try {
            Cursor rawQuery = writableDatabase.rawQuery("SELECT * FROM breakpoint", null);
            while (rawQuery.moveToNext()) {
                try {
                    arrayList.add(new Mqv2r_3(rawQuery));
                } catch (Throwable th) {
                    th = th;
                    cursor = cursor2;
                    cursor2 = rawQuery;
                    if (cursor2 != null) {
                        cursor2.close();
                    }
                    if (cursor != null) {
                        cursor.close();
                    }
                    throw th;
                }
            }
            cursor2 = writableDatabase.rawQuery("SELECT * FROM block", null);
            while (cursor2.moveToNext()) {
                arrayList2.add(new QxJ9d_1(cursor2));
            }
            if (rawQuery != null) {
                rawQuery.close();
            }
            if (cursor2 != null) {
                cursor2.close();
            }
            SparseArray<PGPF6hQ_2> sparseArray = new SparseArray<>();
            for (Mqv2r_3 mqv2r_3 : arrayList) {
                PGPF6hQ_2 KQgRr5e_0 = mqv2r_3.KQgRr5e_0();
                Iterator it = arrayList2.iterator();
                while (it.hasNext()) {
                    QxJ9d_1 qxJ9d_1 = (QxJ9d_1) it.next();
                    if (qxJ9d_1.KQgRr5e_0() == KQgRr5e_0.KQgRr5e_0) {
                        KQgRr5e_0.KQgRr5e_0(qxJ9d_1.m97KQgRr5e_0());
                        it.remove();
                    }
                }
                sparseArray.put(KQgRr5e_0.KQgRr5e_0, KQgRr5e_0);
            }
            return sparseArray;
        } catch (Throwable th2) {
            th = th2;
            cursor = null;
        }
    }

    /* renamed from: KQgRr5e_0  reason: collision with other method in class */
    public HashMap<String, String> m98KQgRr5e_0() {
        SQLiteDatabase writableDatabase = getWritableDatabase();
        HashMap<String, String> hashMap = new HashMap<>();
        Cursor cursor = null;
        try {
            cursor = writableDatabase.rawQuery("SELECT * FROM okdownloadResponseFilename", null);
            while (cursor.moveToNext()) {
                hashMap.put(cursor.getString(cursor.getColumnIndex("url")), cursor.getString(cursor.getColumnIndex("filename")));
            }
            return hashMap;
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

    public void KQgRr5e_0(String str, String str2) {
        Cursor rawQuery;
        SQLiteDatabase writableDatabase = getWritableDatabase();
        ContentValues contentValues = new ContentValues(2);
        contentValues.put("url", str);
        contentValues.put("filename", str2);
        synchronized (str.intern()) {
            Cursor cursor = null;
            try {
                rawQuery = writableDatabase.rawQuery("SELECT filename FROM okdownloadResponseFilename WHERE url = ?", new String[]{str});
            } catch (Throwable th) {
                th = th;
            }
            try {
                if (rawQuery.moveToFirst()) {
                    if (!str2.equals(rawQuery.getString(rawQuery.getColumnIndex("filename")))) {
                        writableDatabase.replace("okdownloadResponseFilename", null, contentValues);
                    }
                } else {
                    writableDatabase.insert("okdownloadResponseFilename", null, contentValues);
                }
                if (rawQuery != null) {
                    rawQuery.close();
                }
            } catch (Throwable th2) {
                th = th2;
                cursor = rawQuery;
                if (cursor != null) {
                    cursor.close();
                }
                throw th;
            }
        }
    }

    /* renamed from: KQgRr5e_0  reason: collision with other method in class */
    public void m100KQgRr5e_0(PGPF6hQ_2 pGPF6hQ_2) throws IOException {
        int KQgRr5e_0 = pGPF6hQ_2.KQgRr5e_0();
        SQLiteDatabase writableDatabase = getWritableDatabase();
        for (int i = 0; i < KQgRr5e_0; i++) {
            KQgRr5e_0 KQgRr5e_02 = pGPF6hQ_2.KQgRr5e_0(i);
            if (writableDatabase.insert("block", null, KQgRr5e_0(pGPF6hQ_2.KQgRr5e_0, i, KQgRr5e_02)) == -1) {
                throw new IMuAHx_6.KQgRr5e_0.KQgRr5e_0.XnFJ5_9.Xgtqvk_8.FSaI4_7("insert block " + KQgRr5e_02 + " failed!");
            }
        }
        if (writableDatabase.insert("breakpoint", null, KQgRr5e_0(pGPF6hQ_2)) != -1) {
            return;
        }
        throw new IMuAHx_6.KQgRr5e_0.KQgRr5e_0.XnFJ5_9.Xgtqvk_8.FSaI4_7("insert info " + pGPF6hQ_2 + " failed!");
    }

    public void KQgRr5e_0(PGPF6hQ_2 pGPF6hQ_2, int i, long j) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("current_offset", Long.valueOf(j));
        getWritableDatabase().update("block", contentValues, "breakpoint_id = ? AND block_index = ?", new String[]{Integer.toString(pGPF6hQ_2.KQgRr5e_0), Integer.toString(i)});
    }

    public static ContentValues KQgRr5e_0(PGPF6hQ_2 pGPF6hQ_2) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(Transition.MATCH_ID_STR, Integer.valueOf(pGPF6hQ_2.KQgRr5e_0));
        contentValues.put("url", pGPF6hQ_2.PGPF6hQ_2());
        contentValues.put("etag", pGPF6hQ_2.m91KQgRr5e_0());
        contentValues.put("parent_path", pGPF6hQ_2.f149KQgRr5e_0.getAbsolutePath());
        contentValues.put("filename", pGPF6hQ_2.m95QxJ9d_1());
        contentValues.put("task_only_parent_path", Integer.valueOf(pGPF6hQ_2.m96QxJ9d_1() ? 1 : 0));
        contentValues.put("chunked", Integer.valueOf(pGPF6hQ_2.m93KQgRr5e_0() ? 1 : 0));
        return contentValues;
    }

    public static ContentValues KQgRr5e_0(int i, int i2, KQgRr5e_0 kQgRr5e_0) {
        ContentValues contentValues = new ContentValues();
        contentValues.put("breakpoint_id", Integer.valueOf(i));
        contentValues.put("block_index", Integer.valueOf(i2));
        contentValues.put("start_offset", Long.valueOf(kQgRr5e_0.X6FRs_4()));
        contentValues.put("content_length", Long.valueOf(kQgRr5e_0.KQgRr5e_0()));
        contentValues.put("current_offset", Long.valueOf(kQgRr5e_0.QxJ9d_1()));
        return contentValues;
    }
}
