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

138 lines
4.0 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;
2018-07-31 11:42:35 +02:00
import com.topjohnwu.magisk.Const;
2018-07-31 11:41:54 +02:00
import com.topjohnwu.magisk.Data;
2017-09-06 16:32:40 +02:00
import com.topjohnwu.magisk.MagiskManager;
2018-08-03 17:04:35 +02:00
import com.topjohnwu.magisk.adapters.ReposAdapter;
2017-09-29 21:04:23 +02:00
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-09-06 16:32:40 +02:00
private MagiskManager mm;
2018-08-03 17:04:35 +02:00
private ReposAdapter adapter;
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);
2018-08-01 08:30:59 +02:00
mm = Data.MM();
2017-11-17 16:34:38 +01:00
mDb = getWritableDatabase();
2018-04-07 19:05:01 +02:00
// Remove outdated repos
2017-11-17 16:34:38 +01:00
mDb.delete(TABLE_NAME, "minMagisk<?",
2018-04-07 19:05:01 +02:00
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))");
mm.prefs.edit().remove(Const.Key.ETAG_KEY).apply();
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);
2018-08-03 17:04:35 +02:00
notifyAdapter();
}
public void removeRepo(String id) {
mDb.delete(TABLE_NAME, "id=?", new String[] { id });
2018-08-03 17:04:35 +02:00
notifyAdapter();
}
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 });
}
2018-08-03 17:04:35 +02:00
notifyAdapter();
2017-02-12 16:26:30 +01:00
}
public void addRepo(Repo repo) {
mDb.replace(TABLE_NAME, null, repo.getContentValues());
2018-08-03 17:04:35 +02:00
notifyAdapter();
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;
2018-07-31 11:41:54 +02:00
switch (Data.repoOrder) {
2017-12-26 18:07:33 +01:00
case Const.Value.ORDER_NAME:
orderBy = "name COLLATE NOCASE";
break;
case Const.Value.ORDER_DATE:
orderBy = "last_update DESC";
}
2018-05-05 20:51:23 +02:00
return mDb.query(TABLE_NAME, null, "minMagisk<=? AND minMagisk>=?",
2018-07-31 11:41:54 +02:00
new String[] { String.valueOf(Data.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
}
2018-08-03 17:04:35 +02:00
public void registerAdapter(ReposAdapter a) {
adapter = a;
}
public void unregisterAdapter() {
adapter = null;
}
private void notifyAdapter() {
if (adapter != null) {
Data.mainHandler.post(adapter::notifyDBChanged);
}
}
2017-02-12 16:26:30 +01:00
}