diff --git a/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java b/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java index df9be7371..30c41cfaa 100644 --- a/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java +++ b/app/src/main/java/com/topjohnwu/magisk/MagiskManager.java @@ -89,7 +89,6 @@ public class MagiskManager extends Application { public void onCreate() { super.onCreate(); prefs = PreferenceManager.getDefaultSharedPreferences(this); - suDB = new SuDatabaseHelper(this); } public void toast(String msg, int duration) { @@ -146,6 +145,7 @@ 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/database/SuDatabaseHelper.java b/app/src/main/java/com/topjohnwu/magisk/database/SuDatabaseHelper.java index e35435f95..f64dccd95 100644 --- a/app/src/main/java/com/topjohnwu/magisk/database/SuDatabaseHelper.java +++ b/app/src/main/java/com/topjohnwu/magisk/database/SuDatabaseHelper.java @@ -35,6 +35,7 @@ public class SuDatabaseHelper extends SQLiteOpenHelper { super(context, "su.db", null, DATABASE_VER); magiskManager = Utils.getMagiskManager(context); pm = context.getPackageManager(); + cleanup(); } @Override @@ -91,6 +92,16 @@ public class SuDatabaseHelper extends SQLiteOpenHelper { "(key TEXT, value INT, PRIMARY KEY(key))"); } + private void cleanup() { + SQLiteDatabase db = getWritableDatabase(); + // Clear outdated policies + db.delete(POLICY_TABLE, "until > 0 AND until < ?", + new String[] { String.valueOf(System.currentTimeMillis()) }); + // Clear outdated logs + db.delete(LOG_TABLE, "time < ?", new String[] { String.valueOf( + System.currentTimeMillis() / 1000 - magiskManager.suLogTimeout * 86400) }); + } + public void deletePolicy(Policy policy) { deletePolicy(policy.packageName); } @@ -103,10 +114,14 @@ public class SuDatabaseHelper extends SQLiteOpenHelper { public void deletePolicy(int uid) { SQLiteDatabase db = getWritableDatabase(); - db.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) { Policy policy = null; SQLiteDatabase db = getReadableDatabase(); @@ -145,18 +160,19 @@ public class SuDatabaseHelper extends SQLiteOpenHelper { 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=?", new String[] { policy.packageName }); - db.close(); } public List getPolicyList(PackageManager pm) { List ret = new ArrayList<>(); SQLiteDatabase db = getWritableDatabase(); Policy policy; - // Clear outdated policies - db.delete(POLICY_TABLE, "until > 0 AND until < ?", - new String[] { String.valueOf(System.currentTimeMillis()) }); try (Cursor c = db.query(POLICY_TABLE, null, null, null, null, null, null)) { while (c.moveToNext()) { try { @@ -165,17 +181,18 @@ public class SuDatabaseHelper extends SQLiteOpenHelper { if (policy.info.uid != policy.uid) { if (magiskManager.suReauth) { // Reauth required, remove from DB - deletePolicy(policy.uid); + deletePolicy(db, policy.uid); continue; } else { - // No reauth, we use the new UID + // No reauth, update to use the new UID policy.uid = policy.info.uid; + updatePolicy(db, policy); } } ret.add(policy); } catch (PackageManager.NameNotFoundException e) { // The app no longer exist, remove from DB - deletePolicy(c.getString(c.getColumnIndex("package_name"))); + deletePolicy(db, c.getInt(c.getColumnIndex("uid"))); } } } @@ -186,9 +203,6 @@ public class SuDatabaseHelper extends SQLiteOpenHelper { private List getLogList(SQLiteDatabase db, String selection) { List ret = new ArrayList<>(); - // Clear outdated logs - db.delete(LOG_TABLE, "time < ?", new String[] { String.valueOf( - System.currentTimeMillis() / 1000 - magiskManager.suLogTimeout * 86400) }); try (Cursor c = db.query(LOG_TABLE, null, selection, null, null, null, "time DESC")) { while (c.moveToNext()) { ret.add(new SuLogEntry(c)); @@ -211,7 +225,7 @@ public class SuDatabaseHelper extends SQLiteOpenHelper { } public List getLogList(String selection) { - return getLogList(getWritableDatabase(), selection); + return getLogList(getReadableDatabase(), selection); } public void addLog(SuLogEntry log) { diff --git a/app/src/main/java/com/topjohnwu/magisk/receivers/PackageReceiver.java b/app/src/main/java/com/topjohnwu/magisk/receivers/PackageReceiver.java index 9ad4d370c..3711b2730 100644 --- a/app/src/main/java/com/topjohnwu/magisk/receivers/PackageReceiver.java +++ b/app/src/main/java/com/topjohnwu/magisk/receivers/PackageReceiver.java @@ -12,13 +12,13 @@ public class PackageReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { MagiskManager magiskManager = Utils.getMagiskManager(context); + magiskManager.initSUConfig(); + String pkg = intent.getData().getEncodedSchemeSpecificPart(); Policy policy = magiskManager.suDB.getPolicy(pkg); if (policy == null) return; - magiskManager.initSUConfig(); - switch (intent.getAction()) { case Intent.ACTION_PACKAGE_ADDED: int uid = intent.getIntExtra(Intent.EXTRA_UID, -1); diff --git a/app/src/main/java/com/topjohnwu/magisk/superuser/SuReceiver.java b/app/src/main/java/com/topjohnwu/magisk/superuser/SuReceiver.java index deb5f6ef8..cbbf8470d 100644 --- a/app/src/main/java/com/topjohnwu/magisk/superuser/SuReceiver.java +++ b/app/src/main/java/com/topjohnwu/magisk/superuser/SuReceiver.java @@ -27,6 +27,7 @@ public class SuReceiver extends BroadcastReceiver { Policy policy; MagiskManager magiskManager = (MagiskManager) context.getApplicationContext(); + magiskManager.initSUConfig(); if (intent == null) return; @@ -55,8 +56,6 @@ public class SuReceiver extends BroadcastReceiver { } } - magiskManager.initSUConfig(); - SuLogEntry log = new SuLogEntry(policy); String message;