Switch to DB based su configs

This commit is contained in:
topjohnwu 2017-06-01 03:18:41 +08:00
parent 1e4425b30f
commit ff6938280e
3 changed files with 31 additions and 27 deletions

View File

@ -161,18 +161,8 @@ public class MagiskManager extends Application {
isSuClient = suVersion.toUpperCase().contains("MAGISK");
}
if (isSuClient) {
ret = Shell.sh("getprop " + ROOT_ACCESS_PROP);
if (Utils.isValidShellResponse(ret)) {
suAccessState = Integer.parseInt(ret.get(0));
} else {
suAccessState = 0;
}
ret = Shell.sh("getprop " + MULTIUSER_MODE_PROP);
if (Utils.isValidShellResponse(ret)) {
multiuserMode = Integer.parseInt(ret.get(0));
} else {
multiuserMode = 0;
}
suAccessState = suDB.getSettings(SuDatabaseHelper.ROOT_ACCESS, 3);
multiuserMode = suDB.getSettings(SuDatabaseHelper.MULTIUSER_MODE, 0);
}
}

View File

@ -15,6 +15,7 @@ import com.topjohnwu.magisk.asyncs.MagiskHide;
import com.topjohnwu.magisk.asyncs.SerialTask;
import com.topjohnwu.magisk.components.Activity;
import com.topjohnwu.magisk.components.AlertDialogBuilder;
import com.topjohnwu.magisk.database.SuDatabaseHelper;
import com.topjohnwu.magisk.utils.Logger;
import com.topjohnwu.magisk.utils.Shell;
import com.topjohnwu.magisk.utils.Utils;
@ -188,24 +189,12 @@ public class SettingsActivity extends Activity {
}.exec();
break;
case "su_access":
magiskManager.suAccessState = Utils.getPrefsInt(prefs, "su_access", 0);
new SerialTask<Void, Void, Void>(getActivity()) {
@Override
protected Void doInBackground(Void... params) {
Shell.su("setprop " + MagiskManager.ROOT_ACCESS_PROP + " " + magiskManager.suAccessState);
return null;
}
}.exec();
magiskManager.suAccessState = Utils.getPrefsInt(prefs, "su_access", 3);
magiskManager.suDB.setSettings(SuDatabaseHelper.ROOT_ACCESS, magiskManager.suAccessState);
break;
case "multiuser_mode":
magiskManager.multiuserMode = Utils.getPrefsInt(prefs, "multiuser_mode", 0);
new SerialTask<Void, Void, Void>(getActivity()) {
@Override
protected Void doInBackground(Void... params) {
Shell.su("setprop " + MagiskManager.MULTIUSER_MODE_PROP + " " + magiskManager.multiuserMode);
return null;
}
}.exec();
magiskManager.suDB.setSettings(SuDatabaseHelper.MULTIUSER_MODE, magiskManager.multiuserMode);
case "su_request_timeout":
magiskManager.suRequestTimeout = Utils.getPrefsInt(prefs, "su_request_timeout", 10);
break;

View File

@ -1,5 +1,6 @@
package com.topjohnwu.magisk.database;
import android.content.ContentValues;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
@ -18,6 +19,9 @@ import java.util.List;
public class SuDatabaseHelper extends SQLiteOpenHelper {
public static final String ROOT_ACCESS = "root_access";
public static final String MULTIUSER_MODE = "multiuser_mode";
private static final int DATABASE_VER = 2;
private static final String POLICY_TABLE = "policies";
private static final String LOG_TABLE = "logs";
@ -206,4 +210,25 @@ public class SuDatabaseHelper extends SQLiteOpenHelper {
db.delete(LOG_TABLE, null, null);
db.close();
}
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();
}
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()) {
value = c.getInt(c.getColumnIndex("value"));
}
}
db.close();
return value;
}
}