Magisk/src/main/java/com/topjohnwu/magisk/database/RepoDatabaseHelper.java

113 lines
3.4 KiB
Java
Raw Normal View History

2017-02-12 16:26:30 +01:00
package com.topjohnwu.magisk.database;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
2017-09-06 16:32:40 +02:00
import com.topjohnwu.magisk.MagiskManager;
2017-09-29 21:04:23 +02:00
import com.topjohnwu.magisk.container.Repo;
2017-11-05 21:41:23 +01:00
import com.topjohnwu.magisk.utils.Const;
2017-09-06 16:32:40 +02:00
import com.topjohnwu.magisk.utils.Utils;
2017-02-12 16:26:30 +01:00
import java.util.LinkedList;
import java.util.List;
2017-02-12 16:26:30 +01:00
public class RepoDatabaseHelper extends SQLiteOpenHelper {
2017-11-17 16:34:38 +01:00
private static final int DATABASE_VER = 3;
2017-02-12 16:26:30 +01:00
private static final String TABLE_NAME = "repos";
2017-07-20 18:46:13 +02:00
private SQLiteDatabase mDb;
2017-09-06 16:32:40 +02:00
private MagiskManager mm;
2017-07-20 18:46:13 +02:00
2017-02-12 16:26:30 +01:00
public RepoDatabaseHelper(Context context) {
super(context, "repo.db", null, DATABASE_VER);
2017-09-06 16:32:40 +02:00
mm = Utils.getMagiskManager(context);
2017-11-17 16:34:38 +01:00
mDb = getWritableDatabase();
// Clear bad repos
2017-11-17 16:34:38 +01:00
mDb.delete(TABLE_NAME, "minMagisk<?",
new String[] { String.valueOf(Const.MIN_MODULE_VER) });
2017-02-12 16:26:30 +01:00
}
@Override
public void onCreate(SQLiteDatabase db) {
onUpgrade(db, 0, DATABASE_VER);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try {
if (oldVersion < 3) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
db.execSQL(
"CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " " +
"(id TEXT, name TEXT, version TEXT, versionCode INT, minMagisk INT, " +
"author TEXT, description TEXT, repo_name TEXT, last_update INT, " +
"PRIMARY KEY(id))");
mm.prefs.edit().remove(Const.Key.ETAG_KEY).apply();
oldVersion = 3;
}
} catch (Exception e) {
e.printStackTrace();
// Reset database
onDowngrade(db, DATABASE_VER, 0);
2017-02-12 16:26:30 +01:00
}
}
2017-11-17 16:34:38 +01:00
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, 0, DATABASE_VER);
}
public void clearRepo() {
mDb.delete(TABLE_NAME, null, null);
}
public void removeRepo(String id) {
mDb.delete(TABLE_NAME, "id=?", new String[] { id });
}
public void removeRepo(Repo repo) {
mDb.delete(TABLE_NAME, "repo_name=?", new String[] { repo.getRepoName() });
}
public void removeRepo(List<String> list) {
for (String id : list) {
if (id == null) continue;
mDb.delete(TABLE_NAME, "id=?", new String[] { id });
}
2017-02-12 16:26:30 +01:00
}
public void addRepo(Repo repo) {
mDb.replace(TABLE_NAME, null, repo.getContentValues());
2017-02-12 16:26:30 +01:00
}
public Repo getRepo(String id) {
try (Cursor c = mDb.query(TABLE_NAME, null, "id=?", new String[] { id }, null, null, null)) {
if (c.moveToNext()) {
return new Repo(c);
}
}
return null;
2017-02-12 16:26:30 +01:00
}
public Cursor getRepoCursor() {
2017-11-17 16:34:38 +01:00
return mDb.query(TABLE_NAME, null, "minMagisk<=?",
new String[] { String.valueOf(mm.magiskVersionCode) },
null, null, "name COLLATE NOCASE");
2017-03-30 00:52:18 +02:00
}
public List<String> getRepoIDList() {
LinkedList<String> ret = new LinkedList<>();
try (Cursor c = mDb.query(TABLE_NAME, null, null, null, null, null, null)) {
2017-02-12 16:26:30 +01:00
while (c.moveToNext()) {
ret.add(c.getString(c.getColumnIndex("id")));
2017-02-12 16:26:30 +01:00
}
}
return ret;
}
}