2017-02-12 23:26:30 +08:00
|
|
|
package com.topjohnwu.magisk.database;
|
2017-01-24 14:19:28 +08:00
|
|
|
|
2017-06-01 03:18:41 +08:00
|
|
|
import android.content.ContentValues;
|
2017-01-24 14:19:28 +08:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.pm.PackageManager;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
import android.database.sqlite.SQLiteOpenHelper;
|
2017-10-20 00:41:44 +08:00
|
|
|
import android.os.Build;
|
2017-07-24 00:34:34 +08:00
|
|
|
import android.text.TextUtils;
|
2017-11-21 02:21:37 +08:00
|
|
|
import android.widget.Toast;
|
2017-01-24 14:19:28 +08:00
|
|
|
|
2017-06-01 00:19:52 +08:00
|
|
|
import com.topjohnwu.magisk.MagiskManager;
|
2017-11-21 02:21:37 +08:00
|
|
|
import com.topjohnwu.magisk.R;
|
2017-09-30 03:04:23 +08:00
|
|
|
import com.topjohnwu.magisk.container.Policy;
|
|
|
|
import com.topjohnwu.magisk.container.SuLogEntry;
|
2017-12-12 02:35:00 +08:00
|
|
|
import com.topjohnwu.magisk.utils.Const;
|
2017-10-20 00:41:44 +08:00
|
|
|
import com.topjohnwu.magisk.utils.Shell;
|
2017-06-01 00:19:52 +08:00
|
|
|
import com.topjohnwu.magisk.utils.Utils;
|
2017-02-12 23:26:30 +08:00
|
|
|
|
2017-06-01 00:19:52 +08:00
|
|
|
import java.io.File;
|
2017-07-24 00:34:34 +08:00
|
|
|
import java.text.DateFormat;
|
2017-01-24 14:19:28 +08:00
|
|
|
import java.util.ArrayList;
|
2017-06-01 00:19:52 +08:00
|
|
|
import java.util.Collections;
|
2017-07-24 00:34:34 +08:00
|
|
|
import java.util.Date;
|
2017-01-24 14:19:28 +08:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class SuDatabaseHelper extends SQLiteOpenHelper {
|
|
|
|
|
2017-09-15 18:03:25 +08:00
|
|
|
public static final String DB_NAME = "su.db";
|
2017-12-20 12:14:46 +08:00
|
|
|
public static boolean verified = false;
|
2017-11-06 04:41:23 +08:00
|
|
|
|
2017-10-28 15:12:29 +08:00
|
|
|
private static final int DATABASE_VER = 5;
|
2017-06-01 00:19:52 +08:00
|
|
|
private static final String POLICY_TABLE = "policies";
|
|
|
|
private static final String LOG_TABLE = "logs";
|
|
|
|
private static final String SETTINGS_TABLE = "settings";
|
2017-10-21 22:54:47 +08:00
|
|
|
private static final String STRINGS_TABLE = "strings";
|
2017-12-12 02:35:00 +08:00
|
|
|
private static final File GLOBAL_DB = new File("/data/adb/magisk.db");
|
2017-10-20 00:41:44 +08:00
|
|
|
|
|
|
|
private Context mContext;
|
2017-06-01 00:19:52 +08:00
|
|
|
private PackageManager pm;
|
2017-07-21 00:46:13 +08:00
|
|
|
private SQLiteDatabase mDb;
|
2017-01-24 14:19:28 +08:00
|
|
|
|
2017-12-25 01:32:17 +08:00
|
|
|
private static void unmntDB() {
|
|
|
|
Shell.su(Utils.fmt("umount -l /data/user*/*/%s/*/*.db", MagiskManager.get().getPackageName()));
|
|
|
|
}
|
|
|
|
|
2017-12-20 12:14:46 +08:00
|
|
|
private static Context initDB(boolean verify) {
|
2017-12-21 23:26:49 +08:00
|
|
|
Context context, de = null;
|
2017-12-12 02:35:00 +08:00
|
|
|
MagiskManager ce = MagiskManager.get();
|
2017-10-20 00:41:44 +08:00
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
2017-12-21 23:26:49 +08:00
|
|
|
de = ce.createDeviceProtectedStorageContext();
|
|
|
|
File ceDB = Utils.getDB(ce, DB_NAME);
|
|
|
|
if (ceDB.exists()) {
|
2017-12-12 02:35:00 +08:00
|
|
|
context = ce;
|
2017-12-21 23:26:49 +08:00
|
|
|
} else {
|
|
|
|
context = de;
|
2017-10-20 00:41:44 +08:00
|
|
|
}
|
|
|
|
} else {
|
2017-12-12 02:35:00 +08:00
|
|
|
context = ce;
|
2017-10-20 00:41:44 +08:00
|
|
|
}
|
|
|
|
|
2017-12-21 23:26:49 +08:00
|
|
|
File db = Utils.getDB(context, DB_NAME);
|
|
|
|
if (!verify) {
|
2017-12-29 04:01:39 +08:00
|
|
|
if (db.exists() && db.length() == 0) {
|
2017-12-21 23:26:49 +08:00
|
|
|
ce.loadMagiskInfo();
|
|
|
|
// Continue verification
|
|
|
|
} else {
|
|
|
|
return context;
|
|
|
|
}
|
2017-10-20 00:41:44 +08:00
|
|
|
}
|
|
|
|
|
2017-12-21 23:26:49 +08:00
|
|
|
// Encryption storage
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
|
|
|
|
if (ce.magiskVersionCode < 1410) {
|
|
|
|
if (context == de) {
|
2017-12-25 01:32:17 +08:00
|
|
|
unmntDB();
|
2017-12-21 23:26:49 +08:00
|
|
|
ce.moveDatabaseFrom(de, DB_NAME);
|
|
|
|
context = ce;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (context == ce) {
|
2017-12-25 01:32:17 +08:00
|
|
|
unmntDB();
|
2017-12-21 23:26:49 +08:00
|
|
|
de.moveDatabaseFrom(ce, DB_NAME);
|
|
|
|
context = de;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-25 01:32:17 +08:00
|
|
|
// Context might be updated
|
|
|
|
db = Utils.getDB(context, DB_NAME);
|
2017-12-21 23:26:49 +08:00
|
|
|
|
|
|
|
if (!Shell.rootAccess())
|
2017-12-12 02:35:00 +08:00
|
|
|
return context;
|
2017-10-20 00:41:44 +08:00
|
|
|
|
2017-12-21 23:26:49 +08:00
|
|
|
// Verify the global db matches the local with the same inode
|
|
|
|
if (ce.magiskVersionCode >= 1450 && ce.magiskVersionCode < 1460) {
|
|
|
|
// v14.5 global su db
|
|
|
|
File OLD_GLOBAL_DB = new File(context.getFilesDir().getParentFile().getParentFile(), "magisk.db");
|
|
|
|
verified = TextUtils.equals(Utils.checkInode(OLD_GLOBAL_DB), Utils.checkInode(db));
|
|
|
|
if (!verified) {
|
|
|
|
context.deleteDatabase(DB_NAME);
|
|
|
|
db.getParentFile().mkdirs();
|
|
|
|
Shell.su(Utils.fmt("magisk --clone-attr %s %s; chmod 600 %s; ln %s %s",
|
|
|
|
context.getFilesDir(), OLD_GLOBAL_DB, OLD_GLOBAL_DB, OLD_GLOBAL_DB, db));
|
|
|
|
verified = TextUtils.equals(Utils.checkInode(OLD_GLOBAL_DB), Utils.checkInode(db));
|
|
|
|
}
|
|
|
|
} else if (ce.magiskVersionCode >= 1464) {
|
|
|
|
// New global su db
|
2017-12-20 12:14:46 +08:00
|
|
|
Shell.su(Utils.fmt("mkdir %s 2>/dev/null; chmod 700 %s", GLOBAL_DB.getParent(), GLOBAL_DB.getParent()));
|
2017-12-12 02:35:00 +08:00
|
|
|
if (!Utils.itemExist(GLOBAL_DB)) {
|
2017-12-29 04:01:39 +08:00
|
|
|
context.openOrCreateDatabase(DB_NAME, 0, null).close();
|
2017-12-12 02:35:00 +08:00
|
|
|
Shell.su(Utils.fmt("cp -af %s %s; rm -f %s*", db, GLOBAL_DB, db));
|
|
|
|
}
|
2017-12-20 12:14:46 +08:00
|
|
|
verified = TextUtils.equals(Utils.checkInode(GLOBAL_DB), Utils.checkInode(db));
|
|
|
|
if (!verified) {
|
2017-12-21 23:26:49 +08:00
|
|
|
context.deleteDatabase(DB_NAME);
|
|
|
|
Utils.javaCreateFile(db);
|
2017-12-12 02:35:00 +08:00
|
|
|
Shell.su(Utils.fmt(
|
|
|
|
"chown 0.0 %s; chmod 666 %s; chcon u:object_r:su_file:s0 %s;" +
|
|
|
|
"mount -o bind %s %s",
|
|
|
|
GLOBAL_DB, GLOBAL_DB, GLOBAL_DB, GLOBAL_DB, db));
|
2017-12-20 12:14:46 +08:00
|
|
|
verified = TextUtils.equals(Utils.checkInode(GLOBAL_DB), Utils.checkInode(db));
|
2017-12-12 02:35:00 +08:00
|
|
|
}
|
2017-10-20 00:41:44 +08:00
|
|
|
}
|
2017-12-12 02:35:00 +08:00
|
|
|
return context;
|
2017-10-20 00:41:44 +08:00
|
|
|
}
|
|
|
|
|
2017-12-29 04:01:39 +08:00
|
|
|
public static SuDatabaseHelper getSuDB(boolean verify) {
|
|
|
|
try {
|
|
|
|
return new SuDatabaseHelper(initDB(verify));
|
|
|
|
} catch(Exception e) {
|
|
|
|
// Try to catch runtime exceptions and remove all db for retry
|
|
|
|
unmntDB();
|
2017-12-31 22:10:28 +08:00
|
|
|
Shell.su(Utils.fmt("rm -rf /data/user*/*/magisk.db /data/adb/magisk.db /data/user*/*/%s/databases",
|
|
|
|
MagiskManager.get().getPackageName()));
|
2017-12-29 04:01:39 +08:00
|
|
|
e.printStackTrace();
|
|
|
|
return new SuDatabaseHelper(initDB(false));
|
|
|
|
}
|
2017-10-20 00:41:44 +08:00
|
|
|
}
|
|
|
|
|
2017-12-12 02:35:00 +08:00
|
|
|
private SuDatabaseHelper(Context context) {
|
2017-09-15 18:03:25 +08:00
|
|
|
super(context, DB_NAME, null, DATABASE_VER);
|
2017-10-20 00:41:44 +08:00
|
|
|
mContext = context;
|
2017-06-01 00:19:52 +08:00
|
|
|
pm = context.getPackageManager();
|
2017-07-21 00:46:13 +08:00
|
|
|
mDb = getWritableDatabase();
|
2017-06-20 18:25:18 +08:00
|
|
|
cleanup();
|
2017-12-20 12:14:46 +08:00
|
|
|
|
|
|
|
if (context.getPackageName().equals(Const.ORIG_PKG_NAME)) {
|
|
|
|
String pkg = getStrings(Const.Key.SU_REQUESTER, null);
|
|
|
|
if (pkg != null) {
|
|
|
|
Utils.uninstallPkg(pkg);
|
|
|
|
setStrings(Const.Key.SU_REQUESTER, null);
|
|
|
|
}
|
|
|
|
}
|
2017-01-24 14:19:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCreate(SQLiteDatabase db) {
|
2017-02-21 03:30:37 +08:00
|
|
|
onUpgrade(db, 0, DATABASE_VER);
|
2017-01-24 14:19:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
2017-11-18 05:03:01 +08:00
|
|
|
try {
|
|
|
|
if (oldVersion == 0) {
|
|
|
|
createTables(db);
|
|
|
|
oldVersion = 3;
|
|
|
|
}
|
|
|
|
if (oldVersion == 1) {
|
|
|
|
// We're dropping column app_name, rename and re-construct table
|
2017-12-12 02:35:00 +08:00
|
|
|
db.execSQL(Utils.fmt("ALTER TABLE %s RENAME TO %s_old", POLICY_TABLE));
|
2017-11-18 05:03:01 +08:00
|
|
|
|
|
|
|
// Create the new tables
|
|
|
|
createTables(db);
|
|
|
|
|
|
|
|
// Migrate old data to new tables
|
2017-12-12 02:35:00 +08:00
|
|
|
db.execSQL(Utils.fmt("INSERT INTO %s SELECT " +
|
|
|
|
"uid, package_name, policy, until, logging, notification FROM %s_old",
|
|
|
|
POLICY_TABLE, POLICY_TABLE));
|
|
|
|
db.execSQL(Utils.fmt("DROP TABLE %s_old", POLICY_TABLE));
|
|
|
|
|
|
|
|
MagiskManager.get().deleteDatabase("sulog.db");
|
2017-11-18 05:03:01 +08:00
|
|
|
++oldVersion;
|
|
|
|
}
|
|
|
|
if (oldVersion == 2) {
|
2017-12-12 02:35:00 +08:00
|
|
|
db.execSQL(Utils.fmt("UPDATE %s SET time=time*1000", LOG_TABLE));
|
2017-11-18 05:03:01 +08:00
|
|
|
++oldVersion;
|
|
|
|
}
|
|
|
|
if (oldVersion == 3) {
|
2017-12-12 02:35:00 +08:00
|
|
|
db.execSQL(Utils.fmt("CREATE TABLE IF NOT EXISTS %s (key TEXT, value TEXT, PRIMARY KEY(key))", STRINGS_TABLE));
|
2017-11-18 05:03:01 +08:00
|
|
|
++oldVersion;
|
|
|
|
}
|
|
|
|
if (oldVersion == 4) {
|
2017-12-12 02:35:00 +08:00
|
|
|
db.execSQL(Utils.fmt("UPDATE %s SET uid=uid%%100000", POLICY_TABLE));
|
2017-11-18 05:03:01 +08:00
|
|
|
++oldVersion;
|
2017-06-01 00:19:52 +08:00
|
|
|
}
|
2017-11-18 05:03:01 +08:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
onDowngrade(db, DATABASE_VER, 0);
|
2017-10-20 00:41:44 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-28 15:12:29 +08:00
|
|
|
@Override
|
|
|
|
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
2017-11-21 02:21:37 +08:00
|
|
|
MagiskManager.toast(R.string.su_db_corrupt, Toast.LENGTH_LONG);
|
2017-10-28 15:12:29 +08:00
|
|
|
// Remove everything, we do not support downgrade
|
2017-11-17 23:34:38 +08:00
|
|
|
db.execSQL("DROP TABLE IF EXISTS " + POLICY_TABLE);
|
|
|
|
db.execSQL("DROP TABLE IF EXISTS " + LOG_TABLE);
|
|
|
|
db.execSQL("DROP TABLE IF EXISTS " + SETTINGS_TABLE);
|
|
|
|
db.execSQL("DROP TABLE IF EXISTS " + STRINGS_TABLE);
|
|
|
|
onUpgrade(db, 0, DATABASE_VER);
|
2017-10-28 15:12:29 +08:00
|
|
|
}
|
|
|
|
|
2017-10-20 00:41:44 +08:00
|
|
|
public File getDbFile() {
|
|
|
|
return mContext.getDatabasePath(DB_NAME);
|
2017-01-24 14:19:28 +08:00
|
|
|
}
|
|
|
|
|
2017-06-01 00:19:52 +08:00
|
|
|
private void createTables(SQLiteDatabase db) {
|
|
|
|
// Policies
|
|
|
|
db.execSQL(
|
|
|
|
"CREATE TABLE IF NOT EXISTS " + POLICY_TABLE + " " +
|
|
|
|
"(uid INT, package_name TEXT, policy INT, " +
|
|
|
|
"until INT, logging INT, notification INT, " +
|
|
|
|
"PRIMARY KEY(uid))");
|
|
|
|
|
|
|
|
// Logs
|
|
|
|
db.execSQL(
|
|
|
|
"CREATE TABLE IF NOT EXISTS " + LOG_TABLE + " " +
|
|
|
|
"(from_uid INT, package_name TEXT, app_name TEXT, from_pid INT, " +
|
|
|
|
"to_uid INT, action INT, time INT, command TEXT)");
|
|
|
|
|
|
|
|
// Settings
|
|
|
|
db.execSQL(
|
|
|
|
"CREATE TABLE IF NOT EXISTS " + SETTINGS_TABLE + " " +
|
|
|
|
"(key TEXT, value INT, PRIMARY KEY(key))");
|
|
|
|
}
|
|
|
|
|
2017-12-22 06:43:55 +08:00
|
|
|
public void cleanup() {
|
2017-06-20 18:25:18 +08:00
|
|
|
// Clear outdated policies
|
2017-12-22 06:43:55 +08:00
|
|
|
mDb.delete(POLICY_TABLE, Utils.fmt("until > 0 AND until < %d", System.currentTimeMillis() / 1000), null);
|
2017-06-20 18:25:18 +08:00
|
|
|
// Clear outdated logs
|
2017-12-22 06:43:55 +08:00
|
|
|
mDb.delete(LOG_TABLE, Utils.fmt("time < %d", System.currentTimeMillis() - MagiskManager.get().suLogTimeout * 86400000), null);
|
2017-06-20 18:25:18 +08:00
|
|
|
}
|
|
|
|
|
2017-06-01 00:19:52 +08:00
|
|
|
public void deletePolicy(Policy policy) {
|
2017-12-12 02:35:00 +08:00
|
|
|
deletePolicy(policy.uid);
|
2017-06-01 00:19:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void deletePolicy(String pkg) {
|
2017-07-21 00:46:13 +08:00
|
|
|
mDb.delete(POLICY_TABLE, "package_name=?", new String[] { pkg });
|
2017-06-01 00:19:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void deletePolicy(int uid) {
|
2017-12-12 02:35:00 +08:00
|
|
|
mDb.delete(POLICY_TABLE, Utils.fmt("uid=%d", uid), null);
|
2017-06-20 18:25:18 +08:00
|
|
|
}
|
|
|
|
|
2017-01-24 14:19:28 +08:00
|
|
|
public Policy getPolicy(int uid) {
|
|
|
|
Policy policy = null;
|
2017-12-12 02:35:00 +08:00
|
|
|
try (Cursor c = mDb.query(POLICY_TABLE, null, Utils.fmt("uid=%d", uid), null, null, null, null)) {
|
2017-02-21 03:30:37 +08:00
|
|
|
if (c.moveToNext()) {
|
2017-06-01 00:19:52 +08:00
|
|
|
policy = new Policy(c, pm);
|
2017-02-21 03:30:37 +08:00
|
|
|
}
|
2017-06-01 00:19:52 +08:00
|
|
|
} catch (PackageManager.NameNotFoundException e) {
|
|
|
|
deletePolicy(uid);
|
|
|
|
return null;
|
2017-01-24 14:19:28 +08:00
|
|
|
}
|
|
|
|
return policy;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addPolicy(Policy policy) {
|
2017-07-21 00:46:13 +08:00
|
|
|
mDb.replace(POLICY_TABLE, null, policy.getContentValues());
|
2017-01-24 14:19:28 +08:00
|
|
|
}
|
|
|
|
|
2017-05-31 16:31:33 +08:00
|
|
|
public void updatePolicy(Policy policy) {
|
2017-12-12 02:35:00 +08:00
|
|
|
mDb.update(POLICY_TABLE, policy.getContentValues(), Utils.fmt("uid=%d", policy.uid), null);
|
2017-05-31 16:31:33 +08:00
|
|
|
}
|
|
|
|
|
2017-01-24 14:19:28 +08:00
|
|
|
public List<Policy> getPolicyList(PackageManager pm) {
|
2017-12-12 02:35:00 +08:00
|
|
|
try (Cursor c = mDb.query(POLICY_TABLE, null, Utils.fmt("uid/100000=%d", Const.USER_ID),
|
|
|
|
null, null, null, null)) {
|
2017-07-21 00:46:13 +08:00
|
|
|
List<Policy> ret = new ArrayList<>(c.getCount());
|
2017-01-24 14:19:28 +08:00
|
|
|
while (c.moveToNext()) {
|
2017-06-20 17:56:47 +08:00
|
|
|
try {
|
2017-07-21 00:46:13 +08:00
|
|
|
Policy policy = new Policy(c, pm);
|
2017-06-20 17:56:47 +08:00
|
|
|
ret.add(policy);
|
|
|
|
} catch (PackageManager.NameNotFoundException e) {
|
|
|
|
// The app no longer exist, remove from DB
|
2017-07-21 00:46:13 +08:00
|
|
|
deletePolicy(c.getInt(c.getColumnIndex("uid")));
|
2017-06-20 17:56:47 +08:00
|
|
|
}
|
2017-01-24 14:19:28 +08:00
|
|
|
}
|
2017-07-21 00:46:13 +08:00
|
|
|
Collections.sort(ret);
|
|
|
|
return ret;
|
2017-06-01 00:19:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-24 00:34:34 +08:00
|
|
|
public List<List<Integer>> getLogStructure() {
|
2017-12-12 02:35:00 +08:00
|
|
|
try (Cursor c = mDb.query(LOG_TABLE, new String[] { "time" }, Utils.fmt("from_uid/100000=%d", Const.USER_ID),
|
|
|
|
null, null, null, "time DESC")) {
|
2017-07-24 00:34:34 +08:00
|
|
|
List<List<Integer>> ret = new ArrayList<>();
|
|
|
|
List<Integer> list = null;
|
|
|
|
String dateString = null, newString;
|
2017-06-01 00:19:52 +08:00
|
|
|
while (c.moveToNext()) {
|
2017-07-24 01:20:03 +08:00
|
|
|
Date date = new Date(c.getLong(c.getColumnIndex("time")));
|
2017-07-24 00:34:34 +08:00
|
|
|
newString = DateFormat.getDateInstance(DateFormat.MEDIUM, MagiskManager.locale).format(date);
|
|
|
|
if (!TextUtils.equals(dateString, newString)) {
|
|
|
|
dateString = newString;
|
|
|
|
list = new ArrayList<>();
|
|
|
|
ret.add(list);
|
|
|
|
}
|
|
|
|
list.add(c.getPosition());
|
2017-06-01 00:19:52 +08:00
|
|
|
}
|
2017-07-21 00:46:13 +08:00
|
|
|
return ret;
|
2017-01-24 14:19:28 +08:00
|
|
|
}
|
|
|
|
}
|
2017-06-01 00:19:52 +08:00
|
|
|
|
2017-07-24 00:34:34 +08:00
|
|
|
public Cursor getLogCursor() {
|
2017-12-12 02:35:00 +08:00
|
|
|
return mDb.query(LOG_TABLE, null, Utils.fmt("from_uid/100000=%d", Const.USER_ID),
|
|
|
|
null, null, null, "time DESC");
|
2017-06-01 00:19:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void addLog(SuLogEntry log) {
|
2017-07-21 00:46:13 +08:00
|
|
|
mDb.insert(LOG_TABLE, null, log.getContentValues());
|
2017-06-01 00:19:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public void clearLogs() {
|
2017-07-21 00:46:13 +08:00
|
|
|
mDb.delete(LOG_TABLE, null, null);
|
2017-06-01 00:19:52 +08:00
|
|
|
}
|
2017-06-01 03:18:41 +08:00
|
|
|
|
|
|
|
public void setSettings(String key, int value) {
|
|
|
|
ContentValues data = new ContentValues();
|
|
|
|
data.put("key", key);
|
|
|
|
data.put("value", value);
|
2017-07-21 00:46:13 +08:00
|
|
|
mDb.replace(SETTINGS_TABLE, null, data);
|
2017-06-01 03:18:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public int getSettings(String key, int defaultValue) {
|
|
|
|
int value = defaultValue;
|
2017-07-21 00:46:13 +08:00
|
|
|
try (Cursor c = mDb.query(SETTINGS_TABLE, null, "key=?",new String[] { key }, null, null, null)) {
|
|
|
|
if (c.moveToNext()) {
|
2017-06-01 03:18:41 +08:00
|
|
|
value = c.getInt(c.getColumnIndex("value"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
2017-10-21 22:54:47 +08:00
|
|
|
|
|
|
|
public void setStrings(String key, String value) {
|
|
|
|
if (value == null) {
|
|
|
|
mDb.delete(STRINGS_TABLE, "key=?", new String[] { key });
|
|
|
|
} else {
|
|
|
|
ContentValues data = new ContentValues();
|
|
|
|
data.put("key", key);
|
|
|
|
data.put("value", value);
|
|
|
|
mDb.replace(STRINGS_TABLE, null, data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getStrings(String key, String defaultValue) {
|
|
|
|
String value = defaultValue;
|
|
|
|
try (Cursor c = mDb.query(STRINGS_TABLE, null, "key=?",new String[] { key }, null, null, null)) {
|
|
|
|
if (c.moveToNext()) {
|
|
|
|
value = c.getString(c.getColumnIndex("value"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
2017-01-24 14:19:28 +08:00
|
|
|
}
|