Proper handle policy changes

This commit is contained in:
topjohnwu 2017-06-20 18:25:18 +08:00
parent dd3b716d85
commit 1eb571b787
4 changed files with 30 additions and 17 deletions

View File

@ -89,7 +89,6 @@ public class MagiskManager extends Application {
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
prefs = PreferenceManager.getDefaultSharedPreferences(this); prefs = PreferenceManager.getDefaultSharedPreferences(this);
suDB = new SuDatabaseHelper(this);
} }
public void toast(String msg, int duration) { public void toast(String msg, int duration) {
@ -146,6 +145,7 @@ 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);

View File

@ -35,6 +35,7 @@ public class SuDatabaseHelper extends SQLiteOpenHelper {
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();
cleanup();
} }
@Override @Override
@ -91,6 +92,16 @@ public class SuDatabaseHelper extends SQLiteOpenHelper {
"(key TEXT, value INT, PRIMARY KEY(key))"); "(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) { public void deletePolicy(Policy policy) {
deletePolicy(policy.packageName); deletePolicy(policy.packageName);
} }
@ -103,10 +114,14 @@ public class SuDatabaseHelper extends SQLiteOpenHelper {
public void deletePolicy(int uid) { public void deletePolicy(int uid) {
SQLiteDatabase db = getWritableDatabase(); SQLiteDatabase db = getWritableDatabase();
db.delete(POLICY_TABLE, "uid=?", new String[]{String.valueOf(uid)}); deletePolicy(db, uid);
db.close(); 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(); SQLiteDatabase db = getReadableDatabase();
@ -145,18 +160,19 @@ public class SuDatabaseHelper extends SQLiteOpenHelper {
public void updatePolicy(Policy policy) { public void updatePolicy(Policy policy) {
SQLiteDatabase db = getWritableDatabase(); SQLiteDatabase db = getWritableDatabase();
updatePolicy(db, policy);
db.close();
}
private void updatePolicy(SQLiteDatabase db, Policy policy) {
db.update(POLICY_TABLE, policy.getContentValues(), "package_name=?", db.update(POLICY_TABLE, policy.getContentValues(), "package_name=?",
new String[] { policy.packageName }); new String[] { policy.packageName });
db.close();
} }
public List<Policy> getPolicyList(PackageManager pm) { public List<Policy> getPolicyList(PackageManager pm) {
List<Policy> ret = new ArrayList<>(); List<Policy> ret = new ArrayList<>();
SQLiteDatabase db = getWritableDatabase(); SQLiteDatabase db = getWritableDatabase();
Policy policy; 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)) { try (Cursor c = db.query(POLICY_TABLE, null, null, null, null, null, null)) {
while (c.moveToNext()) { while (c.moveToNext()) {
try { try {
@ -165,17 +181,18 @@ public class SuDatabaseHelper extends SQLiteOpenHelper {
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(policy.uid); deletePolicy(db, policy.uid);
continue; continue;
} else { } else {
// No reauth, we 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);
} }
} }
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(c.getString(c.getColumnIndex("package_name"))); deletePolicy(db, c.getInt(c.getColumnIndex("uid")));
} }
} }
} }
@ -186,9 +203,6 @@ public class SuDatabaseHelper extends SQLiteOpenHelper {
private List<SuLogEntry> getLogList(SQLiteDatabase db, String selection) { private List<SuLogEntry> getLogList(SQLiteDatabase db, String selection) {
List<SuLogEntry> ret = new ArrayList<>(); List<SuLogEntry> 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")) { try (Cursor c = db.query(LOG_TABLE, null, selection, null, null, null, "time DESC")) {
while (c.moveToNext()) { while (c.moveToNext()) {
ret.add(new SuLogEntry(c)); ret.add(new SuLogEntry(c));
@ -211,7 +225,7 @@ public class SuDatabaseHelper extends SQLiteOpenHelper {
} }
public List<SuLogEntry> getLogList(String selection) { public List<SuLogEntry> getLogList(String selection) {
return getLogList(getWritableDatabase(), selection); return getLogList(getReadableDatabase(), selection);
} }
public void addLog(SuLogEntry log) { public void addLog(SuLogEntry log) {

View File

@ -12,13 +12,13 @@ public class PackageReceiver extends BroadcastReceiver {
@Override @Override
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
MagiskManager magiskManager = Utils.getMagiskManager(context); MagiskManager magiskManager = Utils.getMagiskManager(context);
magiskManager.initSUConfig();
String pkg = intent.getData().getEncodedSchemeSpecificPart(); String pkg = intent.getData().getEncodedSchemeSpecificPart();
Policy policy = magiskManager.suDB.getPolicy(pkg); Policy policy = magiskManager.suDB.getPolicy(pkg);
if (policy == null) if (policy == null)
return; return;
magiskManager.initSUConfig();
switch (intent.getAction()) { switch (intent.getAction()) {
case Intent.ACTION_PACKAGE_ADDED: case Intent.ACTION_PACKAGE_ADDED:
int uid = intent.getIntExtra(Intent.EXTRA_UID, -1); int uid = intent.getIntExtra(Intent.EXTRA_UID, -1);

View File

@ -27,6 +27,7 @@ public class SuReceiver extends BroadcastReceiver {
Policy policy; Policy policy;
MagiskManager magiskManager = (MagiskManager) context.getApplicationContext(); MagiskManager magiskManager = (MagiskManager) context.getApplicationContext();
magiskManager.initSUConfig();
if (intent == null) return; if (intent == null) return;
@ -55,8 +56,6 @@ public class SuReceiver extends BroadcastReceiver {
} }
} }
magiskManager.initSUConfig();
SuLogEntry log = new SuLogEntry(policy); SuLogEntry log = new SuLogEntry(policy);
String message; String message;