Update database helper
This commit is contained in:
parent
55ecc41d06
commit
8458553b74
@ -12,6 +12,7 @@ import android.preference.PreferenceManager;
|
|||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import com.topjohnwu.magisk.database.RepoDatabaseHelper;
|
||||||
import com.topjohnwu.magisk.database.SuDatabaseHelper;
|
import com.topjohnwu.magisk.database.SuDatabaseHelper;
|
||||||
import com.topjohnwu.magisk.module.Module;
|
import com.topjohnwu.magisk.module.Module;
|
||||||
import com.topjohnwu.magisk.module.Repo;
|
import com.topjohnwu.magisk.module.Repo;
|
||||||
@ -89,6 +90,7 @@ public class MagiskManager extends Application {
|
|||||||
// Global resources
|
// Global resources
|
||||||
public SharedPreferences prefs;
|
public SharedPreferences prefs;
|
||||||
public SuDatabaseHelper suDB;
|
public SuDatabaseHelper suDB;
|
||||||
|
public RepoDatabaseHelper repoDB;
|
||||||
public Shell shell;
|
public Shell shell;
|
||||||
|
|
||||||
private static Handler mHandler = new Handler();
|
private static Handler mHandler = new Handler();
|
||||||
@ -99,6 +101,8 @@ public class MagiskManager extends Application {
|
|||||||
new File(getApplicationInfo().dataDir).mkdirs(); /* Create the app data directory */
|
new File(getApplicationInfo().dataDir).mkdirs(); /* Create the app data directory */
|
||||||
prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
||||||
shell = Shell.getShell();
|
shell = Shell.getShell();
|
||||||
|
suDB = new SuDatabaseHelper(this);
|
||||||
|
repoDB = new RepoDatabaseHelper(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void toast(String msg, int duration) {
|
public void toast(String msg, int duration) {
|
||||||
@ -161,7 +165,6 @@ public class MagiskManager extends Application {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void initSUConfig() {
|
public void initSUConfig() {
|
||||||
suDB = new SuDatabaseHelper(this);
|
|
||||||
suRequestTimeout = Utils.getPrefsInt(prefs, "su_request_timeout", 10);
|
suRequestTimeout = Utils.getPrefsInt(prefs, "su_request_timeout", 10);
|
||||||
suResponseType = Utils.getPrefsInt(prefs, "su_auto_response", 0);
|
suResponseType = Utils.getPrefsInt(prefs, "su_auto_response", 0);
|
||||||
suNotificationType = Utils.getPrefsInt(prefs, "su_notification", 1);
|
suNotificationType = Utils.getPrefsInt(prefs, "su_notification", 1);
|
||||||
|
@ -45,8 +45,8 @@ public class LoadRepos extends ParallelTask<Void, Void, Void> {
|
|||||||
public LoadRepos(Context context) {
|
public LoadRepos(Context context) {
|
||||||
super(context);
|
super(context);
|
||||||
prefs = getMagiskManager().prefs;
|
prefs = getMagiskManager().prefs;
|
||||||
|
repoDB = getMagiskManager().repoDB;
|
||||||
String prefsPath = context.getApplicationInfo().dataDir + "/shared_prefs";
|
String prefsPath = context.getApplicationInfo().dataDir + "/shared_prefs";
|
||||||
repoDB = new RepoDatabaseHelper(context);
|
|
||||||
// Legacy data cleanup
|
// Legacy data cleanup
|
||||||
File old = new File(prefsPath, "RepoMap.xml");
|
File old = new File(prefsPath, "RepoMap.xml");
|
||||||
if (old.exists() || !prefs.getString("repomap", "empty").equals("empty")) {
|
if (old.exists() || !prefs.getString("repomap", "empty").equals("empty")) {
|
||||||
|
@ -17,8 +17,11 @@ public class RepoDatabaseHelper extends SQLiteOpenHelper {
|
|||||||
private static final String TABLE_NAME = "repos";
|
private static final String TABLE_NAME = "repos";
|
||||||
private static final int MIN_TEMPLATE_VER = 3;
|
private static final int MIN_TEMPLATE_VER = 3;
|
||||||
|
|
||||||
|
private SQLiteDatabase mDb;
|
||||||
|
|
||||||
public RepoDatabaseHelper(Context context) {
|
public RepoDatabaseHelper(Context context) {
|
||||||
super(context, "repo.db", null, DATABASE_VER);
|
super(context, "repo.db", null, DATABASE_VER);
|
||||||
|
mDb = getWritableDatabase();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -43,29 +46,23 @@ public class RepoDatabaseHelper extends SQLiteOpenHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void addRepoMap(ValueSortedMap<String, Repo> map) {
|
public void addRepoMap(ValueSortedMap<String, Repo> map) {
|
||||||
SQLiteDatabase db = getWritableDatabase();
|
|
||||||
Collection<Repo> list = map.values();
|
Collection<Repo> list = map.values();
|
||||||
for (Repo repo : list) {
|
for (Repo repo : list) {
|
||||||
Logger.dev("Add to DB: " + repo.getId());
|
Logger.dev("Add to DB: " + repo.getId());
|
||||||
db.replace(TABLE_NAME, null, repo.getContentValues());
|
mDb.replace(TABLE_NAME, null, repo.getContentValues());
|
||||||
}
|
}
|
||||||
db.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clearRepo() {
|
public void clearRepo() {
|
||||||
SQLiteDatabase db = getWritableDatabase();
|
mDb.delete(TABLE_NAME, null, null);
|
||||||
db.delete(TABLE_NAME, null, null);
|
|
||||||
db.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeRepo(ValueSortedMap<String, Repo> map) {
|
public void removeRepo(ValueSortedMap<String, Repo> map) {
|
||||||
SQLiteDatabase db = getWritableDatabase();
|
|
||||||
Collection<Repo> list = map.values();
|
Collection<Repo> list = map.values();
|
||||||
for (Repo repo : list) {
|
for (Repo repo : list) {
|
||||||
Logger.dev("Remove from DB: " + repo.getId());
|
Logger.dev("Remove from DB: " + repo.getId());
|
||||||
db.delete(TABLE_NAME, "id=?", new String[] { repo.getId() });
|
mDb.delete(TABLE_NAME, "id=?", new String[] { repo.getId() });
|
||||||
}
|
}
|
||||||
db.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ValueSortedMap<String, Repo> getRepoMap() {
|
public ValueSortedMap<String, Repo> getRepoMap() {
|
||||||
@ -74,20 +71,13 @@ public class RepoDatabaseHelper extends SQLiteOpenHelper {
|
|||||||
|
|
||||||
public ValueSortedMap<String, Repo> getRepoMap(boolean filtered) {
|
public ValueSortedMap<String, Repo> getRepoMap(boolean filtered) {
|
||||||
ValueSortedMap<String, Repo> ret = new ValueSortedMap<>();
|
ValueSortedMap<String, Repo> ret = new ValueSortedMap<>();
|
||||||
SQLiteDatabase db = getReadableDatabase();
|
|
||||||
Repo repo;
|
Repo repo;
|
||||||
try (Cursor c = db.query(TABLE_NAME, null, null, null, null, null, null)) {
|
try (Cursor c = mDb.query(TABLE_NAME, null, "template>=?", new String[] { filtered ? String.valueOf(MIN_TEMPLATE_VER) : "0" }, null, null, null)) {
|
||||||
while (c.moveToNext()) {
|
while (c.moveToNext()) {
|
||||||
repo = new Repo(c);
|
repo = new Repo(c);
|
||||||
if (repo.getTemplateVersion() < MIN_TEMPLATE_VER && filtered) {
|
ret.put(repo.getId(), repo);
|
||||||
Logger.dev("Outdated repo: " + repo.getId());
|
|
||||||
} else {
|
|
||||||
// Logger.dev("Load from DB: " + repo.getId());
|
|
||||||
ret.put(repo.getId(), repo);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
db.close();
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,11 +30,13 @@ public class SuDatabaseHelper extends SQLiteOpenHelper {
|
|||||||
|
|
||||||
private MagiskManager magiskManager;
|
private MagiskManager magiskManager;
|
||||||
private PackageManager pm;
|
private PackageManager pm;
|
||||||
|
private SQLiteDatabase mDb;
|
||||||
|
|
||||||
public SuDatabaseHelper(Context context) {
|
public SuDatabaseHelper(Context context) {
|
||||||
super(context, "su.db", null, DATABASE_VER);
|
super(context, "su.db", null, DATABASE_VER);
|
||||||
magiskManager = Utils.getMagiskManager(context);
|
magiskManager = Utils.getMagiskManager(context);
|
||||||
pm = context.getPackageManager();
|
pm = context.getPackageManager();
|
||||||
|
mDb = getWritableDatabase();
|
||||||
cleanup();
|
cleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -93,12 +95,11 @@ public class SuDatabaseHelper extends SQLiteOpenHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void cleanup() {
|
private void cleanup() {
|
||||||
SQLiteDatabase db = getWritableDatabase();
|
|
||||||
// Clear outdated policies
|
// Clear outdated policies
|
||||||
db.delete(POLICY_TABLE, "until > 0 AND until < ?",
|
mDb.delete(POLICY_TABLE, "until > 0 AND until < ?",
|
||||||
new String[] { String.valueOf(System.currentTimeMillis() / 1000) });
|
new String[] { String.valueOf(System.currentTimeMillis() / 1000) });
|
||||||
// Clear outdated logs
|
// Clear outdated logs
|
||||||
db.delete(LOG_TABLE, "time < ?", new String[] { String.valueOf(
|
mDb.delete(LOG_TABLE, "time < ?", new String[] { String.valueOf(
|
||||||
System.currentTimeMillis() / 1000 - magiskManager.suLogTimeout * 86400) });
|
System.currentTimeMillis() / 1000 - magiskManager.suLogTimeout * 86400) });
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -107,25 +108,16 @@ public class SuDatabaseHelper extends SQLiteOpenHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void deletePolicy(String pkg) {
|
public void deletePolicy(String pkg) {
|
||||||
SQLiteDatabase db = getWritableDatabase();
|
mDb.delete(POLICY_TABLE, "package_name=?", new String[] { pkg });
|
||||||
db.delete(POLICY_TABLE, "package_name=?", new String[] { pkg });
|
|
||||||
db.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void deletePolicy(int uid) {
|
public void deletePolicy(int uid) {
|
||||||
SQLiteDatabase db = getWritableDatabase();
|
mDb.delete(POLICY_TABLE, "uid=?", new String[]{String.valueOf(uid)});
|
||||||
deletePolicy(db, uid);
|
|
||||||
db.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void deletePolicy(SQLiteDatabase db, int uid) {
|
|
||||||
db.delete(POLICY_TABLE, "uid=?", new String[]{String.valueOf(uid)});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Policy getPolicy(int uid) {
|
public Policy getPolicy(int uid) {
|
||||||
Policy policy = null;
|
Policy policy = null;
|
||||||
SQLiteDatabase db = getReadableDatabase();
|
try (Cursor c = mDb.query(POLICY_TABLE, null, "uid=?", new String[] { String.valueOf(uid) }, null, null, null)) {
|
||||||
try (Cursor c = db.query(POLICY_TABLE, null, "uid=?", new String[] { String.valueOf(uid) }, null, null, null)) {
|
|
||||||
if (c.moveToNext()) {
|
if (c.moveToNext()) {
|
||||||
policy = new Policy(c, pm);
|
policy = new Policy(c, pm);
|
||||||
}
|
}
|
||||||
@ -133,14 +125,12 @@ public class SuDatabaseHelper extends SQLiteOpenHelper {
|
|||||||
deletePolicy(uid);
|
deletePolicy(uid);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
db.close();
|
|
||||||
return policy;
|
return policy;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Policy getPolicy(String pkg) {
|
public Policy getPolicy(String pkg) {
|
||||||
Policy policy = null;
|
Policy policy = null;
|
||||||
SQLiteDatabase db = getReadableDatabase();
|
try (Cursor c = mDb.query(POLICY_TABLE, null, "package_name=?", new String[] { pkg }, null, null, null)) {
|
||||||
try (Cursor c = db.query(POLICY_TABLE, null, "package_name=?", new String[] { pkg }, null, null, null)) {
|
|
||||||
if (c.moveToNext()) {
|
if (c.moveToNext()) {
|
||||||
policy = new Policy(c, pm);
|
policy = new Policy(c, pm);
|
||||||
}
|
}
|
||||||
@ -148,76 +138,64 @@ public class SuDatabaseHelper extends SQLiteOpenHelper {
|
|||||||
deletePolicy(pkg);
|
deletePolicy(pkg);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
db.close();
|
|
||||||
return policy;
|
return policy;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addPolicy(Policy policy) {
|
public void addPolicy(Policy policy) {
|
||||||
SQLiteDatabase db = getWritableDatabase();
|
mDb.replace(POLICY_TABLE, null, policy.getContentValues());
|
||||||
db.replace(POLICY_TABLE, null, policy.getContentValues());
|
|
||||||
db.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updatePolicy(Policy policy) {
|
public void updatePolicy(Policy policy) {
|
||||||
SQLiteDatabase db = getWritableDatabase();
|
mDb.update(POLICY_TABLE, policy.getContentValues(), "package_name=?",
|
||||||
updatePolicy(db, policy);
|
|
||||||
db.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void updatePolicy(SQLiteDatabase db, Policy policy) {
|
|
||||||
db.update(POLICY_TABLE, policy.getContentValues(), "package_name=?",
|
|
||||||
new String[] { policy.packageName });
|
new String[] { policy.packageName });
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Policy> getPolicyList(PackageManager pm) {
|
public List<Policy> getPolicyList(PackageManager pm) {
|
||||||
List<Policy> ret = new ArrayList<>();
|
try (Cursor c = mDb.query(POLICY_TABLE, null, null, null, null, null, null)) {
|
||||||
SQLiteDatabase db = getWritableDatabase();
|
List<Policy> ret = new ArrayList<>(c.getCount());
|
||||||
Policy policy;
|
|
||||||
try (Cursor c = db.query(POLICY_TABLE, null, null, null, null, null, null)) {
|
|
||||||
while (c.moveToNext()) {
|
while (c.moveToNext()) {
|
||||||
try {
|
try {
|
||||||
policy = new Policy(c, pm);
|
Policy policy = new Policy(c, pm);
|
||||||
// The application changed UID for some reason, check user config
|
// The application changed UID for some reason, check user config
|
||||||
if (policy.info.uid != policy.uid) {
|
if (policy.info.uid != policy.uid) {
|
||||||
if (magiskManager.suReauth) {
|
if (magiskManager.suReauth) {
|
||||||
// Reauth required, remove from DB
|
// Reauth required, remove from DB
|
||||||
deletePolicy(db, policy.uid);
|
deletePolicy(policy);
|
||||||
continue;
|
continue;
|
||||||
} else {
|
} else {
|
||||||
// No reauth, update to use the new UID
|
// No reauth, update to use the new UID
|
||||||
policy.uid = policy.info.uid;
|
policy.uid = policy.info.uid;
|
||||||
updatePolicy(db, policy);
|
updatePolicy(policy);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ret.add(policy);
|
ret.add(policy);
|
||||||
} catch (PackageManager.NameNotFoundException e) {
|
} catch (PackageManager.NameNotFoundException e) {
|
||||||
// The app no longer exist, remove from DB
|
// The app no longer exist, remove from DB
|
||||||
deletePolicy(db, c.getInt(c.getColumnIndex("uid")));
|
deletePolicy(c.getInt(c.getColumnIndex("uid")));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Collections.sort(ret);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
db.close();
|
|
||||||
Collections.sort(ret);
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private List<SuLogEntry> getLogList(SQLiteDatabase db, String selection) {
|
private List<SuLogEntry> getLogList(SQLiteDatabase db, String selection) {
|
||||||
List<SuLogEntry> ret = new ArrayList<>();
|
|
||||||
try (Cursor c = db.query(LOG_TABLE, null, selection, null, null, null, "time DESC")) {
|
try (Cursor c = db.query(LOG_TABLE, null, selection, null, null, null, "time DESC")) {
|
||||||
|
List<SuLogEntry> ret = new ArrayList<>(c.getCount());
|
||||||
while (c.moveToNext()) {
|
while (c.moveToNext()) {
|
||||||
ret.add(new SuLogEntry(c));
|
ret.add(new SuLogEntry(c));
|
||||||
}
|
}
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
db.close();
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void migrateLegacyLogList(File oldDB, SQLiteDatabase newDB) {
|
private void migrateLegacyLogList(File oldDB, SQLiteDatabase newDB) {
|
||||||
SQLiteDatabase db = SQLiteDatabase.openDatabase(oldDB.getPath(), null, SQLiteDatabase.OPEN_READWRITE);
|
SQLiteDatabase oldDb = SQLiteDatabase.openDatabase(oldDB.getPath(), null, SQLiteDatabase.OPEN_READWRITE);
|
||||||
List<SuLogEntry> logs = getLogList(db, null);
|
List<SuLogEntry> logs = getLogList(oldDb, null);
|
||||||
for (SuLogEntry log : logs) {
|
for (SuLogEntry log : logs) {
|
||||||
newDB.insert(LOG_TABLE, null, log.getContentValues());
|
newDB.insert(LOG_TABLE, null, log.getContentValues());
|
||||||
}
|
}
|
||||||
|
oldDb.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<SuLogEntry> getLogList() {
|
public List<SuLogEntry> getLogList() {
|
||||||
@ -225,39 +203,31 @@ public class SuDatabaseHelper extends SQLiteOpenHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public List<SuLogEntry> getLogList(String selection) {
|
public List<SuLogEntry> getLogList(String selection) {
|
||||||
return getLogList(getReadableDatabase(), selection);
|
return getLogList(mDb, selection);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void addLog(SuLogEntry log) {
|
public void addLog(SuLogEntry log) {
|
||||||
SQLiteDatabase db = getWritableDatabase();
|
mDb.insert(LOG_TABLE, null, log.getContentValues());
|
||||||
db.insert(LOG_TABLE, null, log.getContentValues());
|
|
||||||
db.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void clearLogs() {
|
public void clearLogs() {
|
||||||
SQLiteDatabase db = getWritableDatabase();
|
mDb.delete(LOG_TABLE, null, null);
|
||||||
db.delete(LOG_TABLE, null, null);
|
|
||||||
db.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setSettings(String key, int value) {
|
public void setSettings(String key, int value) {
|
||||||
ContentValues data = new ContentValues();
|
ContentValues data = new ContentValues();
|
||||||
data.put("key", key);
|
data.put("key", key);
|
||||||
data.put("value", value);
|
data.put("value", value);
|
||||||
SQLiteDatabase db = getWritableDatabase();
|
mDb.replace(SETTINGS_TABLE, null, data);
|
||||||
db.replace(SETTINGS_TABLE, null, data);
|
|
||||||
db.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getSettings(String key, int defaultValue) {
|
public int getSettings(String key, int defaultValue) {
|
||||||
SQLiteDatabase db = getReadableDatabase();
|
|
||||||
int value = defaultValue;
|
int value = defaultValue;
|
||||||
try (Cursor c = db.query(SETTINGS_TABLE, null, "key=?", new String[] { key }, null, null, null)) {
|
try (Cursor c = mDb.query(SETTINGS_TABLE, null, "key=?",new String[] { key }, null, null, null)) {
|
||||||
while (c.moveToNext()) {
|
if (c.moveToNext()) {
|
||||||
value = c.getInt(c.getColumnIndex("value"));
|
value = c.getInt(c.getColumnIndex("value"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
db.close();
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -140,11 +140,11 @@ public class Utils {
|
|||||||
}.requestTest();
|
}.requestTest();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void clearRepoCache(Activity activity) {
|
public static void clearRepoCache(Context context) {
|
||||||
MagiskManager magiskManager = getMagiskManager(activity);
|
MagiskManager magiskManager = getMagiskManager(context);
|
||||||
magiskManager.prefs.edit().remove(LoadRepos.ETAG_KEY).apply();
|
magiskManager.prefs.edit().remove(LoadRepos.ETAG_KEY).apply();
|
||||||
new RepoDatabaseHelper(activity).clearRepo();
|
magiskManager.repoDB.clearRepo();
|
||||||
Toast.makeText(activity, R.string.repo_cache_cleared, Toast.LENGTH_SHORT).show();
|
magiskManager.toast(R.string.repo_cache_cleared, Toast.LENGTH_SHORT);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getNameFromUri(Context context, Uri uri) {
|
public static String getNameFromUri(Context context, Uri uri) {
|
||||||
|
Loading…
Reference in New Issue
Block a user