diff --git a/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java b/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java index d8fe340e5..1768a1d96 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java +++ b/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java @@ -12,6 +12,7 @@ import android.preference.PreferenceManager; import android.text.TextUtils; import android.widget.Toast; +import com.topjohnwu.magisk.database.RepoDatabaseHelper; import com.topjohnwu.magisk.database.SuDatabaseHelper; import com.topjohnwu.magisk.module.Module; import com.topjohnwu.magisk.module.Repo; @@ -89,6 +90,7 @@ public class MagiskManager extends Application { // Global resources public SharedPreferences prefs; public SuDatabaseHelper suDB; + public RepoDatabaseHelper repoDB; public Shell shell; 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 */ prefs = PreferenceManager.getDefaultSharedPreferences(this); shell = Shell.getShell(); + suDB = new SuDatabaseHelper(this); + repoDB = new RepoDatabaseHelper(this); } public void toast(String msg, int duration) { @@ -161,7 +165,6 @@ public class MagiskManager extends Application { } public void initSUConfig() { - suDB = new SuDatabaseHelper(this); suRequestTimeout = Utils.getPrefsInt(prefs, "su_request_timeout", 10); suResponseType = Utils.getPrefsInt(prefs, "su_auto_response", 0); suNotificationType = Utils.getPrefsInt(prefs, "su_notification", 1); diff --git a/app/src/main/java/com/topjohnwu/magisk/asyncs/LoadRepos.java b/app/src/main/java/com/topjohnwu/magisk/asyncs/LoadRepos.java index 37f41435c..2cf2baf33 100644 --- a/app/src/main/java/com/topjohnwu/magisk/asyncs/LoadRepos.java +++ b/app/src/main/java/com/topjohnwu/magisk/asyncs/LoadRepos.java @@ -45,8 +45,8 @@ public class LoadRepos extends ParallelTask { public LoadRepos(Context context) { super(context); prefs = getMagiskManager().prefs; + repoDB = getMagiskManager().repoDB; String prefsPath = context.getApplicationInfo().dataDir + "/shared_prefs"; - repoDB = new RepoDatabaseHelper(context); // Legacy data cleanup File old = new File(prefsPath, "RepoMap.xml"); if (old.exists() || !prefs.getString("repomap", "empty").equals("empty")) { diff --git a/app/src/main/java/com/topjohnwu/magisk/database/RepoDatabaseHelper.java b/app/src/main/java/com/topjohnwu/magisk/database/RepoDatabaseHelper.java index cdfc18bd0..12af9a5d0 100644 --- a/app/src/main/java/com/topjohnwu/magisk/database/RepoDatabaseHelper.java +++ b/app/src/main/java/com/topjohnwu/magisk/database/RepoDatabaseHelper.java @@ -17,8 +17,11 @@ public class RepoDatabaseHelper extends SQLiteOpenHelper { private static final String TABLE_NAME = "repos"; private static final int MIN_TEMPLATE_VER = 3; + private SQLiteDatabase mDb; + public RepoDatabaseHelper(Context context) { super(context, "repo.db", null, DATABASE_VER); + mDb = getWritableDatabase(); } @Override @@ -43,29 +46,23 @@ public class RepoDatabaseHelper extends SQLiteOpenHelper { } public void addRepoMap(ValueSortedMap map) { - SQLiteDatabase db = getWritableDatabase(); Collection list = map.values(); for (Repo repo : list) { 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() { - SQLiteDatabase db = getWritableDatabase(); - db.delete(TABLE_NAME, null, null); - db.close(); + mDb.delete(TABLE_NAME, null, null); } public void removeRepo(ValueSortedMap map) { - SQLiteDatabase db = getWritableDatabase(); Collection list = map.values(); for (Repo repo : list) { 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 getRepoMap() { @@ -74,20 +71,13 @@ public class RepoDatabaseHelper extends SQLiteOpenHelper { public ValueSortedMap getRepoMap(boolean filtered) { ValueSortedMap ret = new ValueSortedMap<>(); - SQLiteDatabase db = getReadableDatabase(); 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()) { repo = new Repo(c); - if (repo.getTemplateVersion() < MIN_TEMPLATE_VER && filtered) { - Logger.dev("Outdated repo: " + repo.getId()); - } else { - // Logger.dev("Load from DB: " + repo.getId()); - ret.put(repo.getId(), repo); - } + ret.put(repo.getId(), repo); } } - db.close(); return ret; } } diff --git a/app/src/main/java/com/topjohnwu/magisk/database/SuDatabaseHelper.java b/app/src/main/java/com/topjohnwu/magisk/database/SuDatabaseHelper.java index 1498131d6..980f46134 100644 --- a/app/src/main/java/com/topjohnwu/magisk/database/SuDatabaseHelper.java +++ b/app/src/main/java/com/topjohnwu/magisk/database/SuDatabaseHelper.java @@ -30,11 +30,13 @@ public class SuDatabaseHelper extends SQLiteOpenHelper { private MagiskManager magiskManager; private PackageManager pm; + private SQLiteDatabase mDb; public SuDatabaseHelper(Context context) { super(context, "su.db", null, DATABASE_VER); magiskManager = Utils.getMagiskManager(context); pm = context.getPackageManager(); + mDb = getWritableDatabase(); cleanup(); } @@ -93,12 +95,11 @@ public class SuDatabaseHelper extends SQLiteOpenHelper { } private void cleanup() { - SQLiteDatabase db = getWritableDatabase(); // 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) }); // 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) }); } @@ -107,25 +108,16 @@ public class SuDatabaseHelper extends SQLiteOpenHelper { } public void deletePolicy(String pkg) { - SQLiteDatabase db = getWritableDatabase(); - db.delete(POLICY_TABLE, "package_name=?", new String[] { pkg }); - db.close(); + mDb.delete(POLICY_TABLE, "package_name=?", new String[] { pkg }); } public void deletePolicy(int uid) { - SQLiteDatabase db = getWritableDatabase(); - deletePolicy(db, uid); - db.close(); - } - - private void deletePolicy(SQLiteDatabase db, int uid) { - db.delete(POLICY_TABLE, "uid=?", new String[]{String.valueOf(uid)}); + mDb.delete(POLICY_TABLE, "uid=?", new String[]{String.valueOf(uid)}); } public Policy getPolicy(int uid) { Policy policy = null; - SQLiteDatabase db = getReadableDatabase(); - try (Cursor c = db.query(POLICY_TABLE, null, "uid=?", new String[] { String.valueOf(uid) }, null, null, null)) { + try (Cursor c = mDb.query(POLICY_TABLE, null, "uid=?", new String[] { String.valueOf(uid) }, null, null, null)) { if (c.moveToNext()) { policy = new Policy(c, pm); } @@ -133,14 +125,12 @@ public class SuDatabaseHelper extends SQLiteOpenHelper { deletePolicy(uid); return null; } - db.close(); return policy; } public Policy getPolicy(String pkg) { Policy policy = null; - SQLiteDatabase db = getReadableDatabase(); - try (Cursor c = db.query(POLICY_TABLE, null, "package_name=?", new String[] { pkg }, null, null, null)) { + try (Cursor c = mDb.query(POLICY_TABLE, null, "package_name=?", new String[] { pkg }, null, null, null)) { if (c.moveToNext()) { policy = new Policy(c, pm); } @@ -148,76 +138,64 @@ public class SuDatabaseHelper extends SQLiteOpenHelper { deletePolicy(pkg); return null; } - db.close(); return policy; } public void addPolicy(Policy policy) { - SQLiteDatabase db = getWritableDatabase(); - db.replace(POLICY_TABLE, null, policy.getContentValues()); - db.close(); + mDb.replace(POLICY_TABLE, null, policy.getContentValues()); } public void updatePolicy(Policy policy) { - SQLiteDatabase db = getWritableDatabase(); - updatePolicy(db, policy); - db.close(); - } - - private void updatePolicy(SQLiteDatabase db, Policy policy) { - db.update(POLICY_TABLE, policy.getContentValues(), "package_name=?", + mDb.update(POLICY_TABLE, policy.getContentValues(), "package_name=?", new String[] { policy.packageName }); } public List getPolicyList(PackageManager pm) { - List ret = new ArrayList<>(); - SQLiteDatabase db = getWritableDatabase(); - Policy policy; - try (Cursor c = db.query(POLICY_TABLE, null, null, null, null, null, null)) { + try (Cursor c = mDb.query(POLICY_TABLE, null, null, null, null, null, null)) { + List ret = new ArrayList<>(c.getCount()); while (c.moveToNext()) { try { - policy = new Policy(c, pm); + Policy policy = new Policy(c, pm); // The application changed UID for some reason, check user config if (policy.info.uid != policy.uid) { if (magiskManager.suReauth) { // Reauth required, remove from DB - deletePolicy(db, policy.uid); + deletePolicy(policy); continue; } else { // No reauth, update to use the new UID policy.uid = policy.info.uid; - updatePolicy(db, policy); + updatePolicy(policy); } } ret.add(policy); } catch (PackageManager.NameNotFoundException e) { // 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 getLogList(SQLiteDatabase db, String selection) { - List ret = new ArrayList<>(); try (Cursor c = db.query(LOG_TABLE, null, selection, null, null, null, "time DESC")) { + List ret = new ArrayList<>(c.getCount()); while (c.moveToNext()) { ret.add(new SuLogEntry(c)); } + return ret; } - db.close(); - return ret; } private void migrateLegacyLogList(File oldDB, SQLiteDatabase newDB) { - SQLiteDatabase db = SQLiteDatabase.openDatabase(oldDB.getPath(), null, SQLiteDatabase.OPEN_READWRITE); - List logs = getLogList(db, null); + SQLiteDatabase oldDb = SQLiteDatabase.openDatabase(oldDB.getPath(), null, SQLiteDatabase.OPEN_READWRITE); + List logs = getLogList(oldDb, null); for (SuLogEntry log : logs) { newDB.insert(LOG_TABLE, null, log.getContentValues()); } + oldDb.close(); } public List getLogList() { @@ -225,39 +203,31 @@ public class SuDatabaseHelper extends SQLiteOpenHelper { } public List getLogList(String selection) { - return getLogList(getReadableDatabase(), selection); + return getLogList(mDb, selection); } public void addLog(SuLogEntry log) { - SQLiteDatabase db = getWritableDatabase(); - db.insert(LOG_TABLE, null, log.getContentValues()); - db.close(); + mDb.insert(LOG_TABLE, null, log.getContentValues()); } public void clearLogs() { - SQLiteDatabase db = getWritableDatabase(); - db.delete(LOG_TABLE, null, null); - db.close(); + mDb.delete(LOG_TABLE, null, null); } public void setSettings(String key, int value) { ContentValues data = new ContentValues(); data.put("key", key); data.put("value", value); - SQLiteDatabase db = getWritableDatabase(); - db.replace(SETTINGS_TABLE, null, data); - db.close(); + mDb.replace(SETTINGS_TABLE, null, data); } public int getSettings(String key, int defaultValue) { - SQLiteDatabase db = getReadableDatabase(); int value = defaultValue; - try (Cursor c = db.query(SETTINGS_TABLE, null, "key=?", new String[] { key }, null, null, null)) { - while (c.moveToNext()) { + try (Cursor c = mDb.query(SETTINGS_TABLE, null, "key=?",new String[] { key }, null, null, null)) { + if (c.moveToNext()) { value = c.getInt(c.getColumnIndex("value")); } } - db.close(); return value; } } diff --git a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java index ead1366af..5e03bec16 100644 --- a/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java +++ b/app/src/main/java/com/topjohnwu/magisk/utils/Utils.java @@ -140,11 +140,11 @@ public class Utils { }.requestTest(); } - public static void clearRepoCache(Activity activity) { - MagiskManager magiskManager = getMagiskManager(activity); + public static void clearRepoCache(Context context) { + MagiskManager magiskManager = getMagiskManager(context); magiskManager.prefs.edit().remove(LoadRepos.ETAG_KEY).apply(); - new RepoDatabaseHelper(activity).clearRepo(); - Toast.makeText(activity, R.string.repo_cache_cleared, Toast.LENGTH_SHORT).show(); + magiskManager.repoDB.clearRepo(); + magiskManager.toast(R.string.repo_cache_cleared, Toast.LENGTH_SHORT); } public static String getNameFromUri(Context context, Uri uri) {