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

114 lines
3.5 KiB
Java
Raw Normal View History

2019-01-30 09:10:12 +01:00
package com.topjohnwu.magisk.database;
2017-02-12 16:26:30 +01:00
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
2019-01-30 09:10:12 +01:00
import com.topjohnwu.magisk.Config;
import com.topjohnwu.magisk.Const;
import com.topjohnwu.magisk.container.Repo;
2017-02-12 16:26:30 +01:00
2018-07-04 14:13:12 +02:00
import java.util.HashSet;
import java.util.Set;
2017-02-12 16:26:30 +01:00
public class RepoDatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VER = 4;
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-02-12 16:26:30 +01:00
public RepoDatabaseHelper(Context context) {
super(context, "repo.db", null, DATABASE_VER);
2017-11-17 16:34:38 +01:00
mDb = getWritableDatabase();
2018-04-07 19:05:01 +02:00
// Remove outdated repos
2018-10-28 05:54:56 +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) {
if (oldVersion != newVersion) {
// Nuke old DB and create new table
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, last_update INT, PRIMARY KEY(id))");
2019-01-21 21:49:03 +01:00
Config.remove(Config.Key.ETAG_KEY);
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) {
removeRepo(repo.getId());
}
2018-07-04 14:13:12 +02:00
public void removeRepo(Iterable<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
}
2018-07-04 14:13:12 +02:00
public Cursor getRawCursor() {
return mDb.query(TABLE_NAME, null, null, null, null, null, null);
}
public Cursor getRepoCursor() {
2017-12-26 18:07:33 +01:00
String orderBy = null;
2019-01-21 21:49:03 +01:00
switch ((int) Config.get(Config.Key.REPO_ORDER)) {
case Config.Value.ORDER_NAME:
2017-12-26 18:07:33 +01:00
orderBy = "name COLLATE NOCASE";
break;
2019-01-21 21:49:03 +01:00
case Config.Value.ORDER_DATE:
2017-12-26 18:07:33 +01:00
orderBy = "last_update DESC";
}
2018-05-05 20:51:23 +02:00
return mDb.query(TABLE_NAME, null, "minMagisk<=? AND minMagisk>=?",
2019-01-21 21:49:03 +01:00
new String[] { String.valueOf(Config.magiskVersionCode), String.valueOf(Const.MIN_MODULE_VER) },
2017-12-26 18:07:33 +01:00
null, null, orderBy);
2017-03-30 00:52:18 +02:00
}
2018-07-04 14:13:12 +02:00
public Set<String> getRepoIDSet() {
HashSet<String> set = new HashSet<>(300);
try (Cursor c = mDb.query(TABLE_NAME, null, null, null, null, null, null)) {
2017-02-12 16:26:30 +01:00
while (c.moveToNext()) {
2018-07-04 14:13:12 +02:00
set.add(c.getString(c.getColumnIndex("id")));
2017-02-12 16:26:30 +01:00
}
}
2018-07-04 14:13:12 +02:00
return set;
2017-02-12 16:26:30 +01:00
}
}