import com.koushikdutta.superuser.util.Settings;
public class SuDatabaseHelper extends SQLiteOpenHelper {
- private static final int CURRENT_VERSION = 3;
+ private static final int CURRENT_VERSION = 4;
+ Context mContext;
public SuDatabaseHelper(Context context) {
super(context, "su.sqlite", null, CURRENT_VERSION);
+ mContext = context;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion == 0) {
db.execSQL("create table if not exists uid_policy (logging integer, desired_name text, username text, policy text, until integer, command text, uid integer, desired_uid integer, package_name text, name text, primary key(uid, command, desired_uid))");
- db.execSQL("create table if not exists log (id integer primary key autoincrement, desired_name text, username text, uid integer, desired_uid integer, command text, date integer, action text, package_name text, name text)");
- db.execSQL("create index if not exists log_uid_index on log(uid)");
- db.execSQL("create index if not exists log_desired_uid_index on log(desired_uid)");
- db.execSQL("create index if not exists log_command_index on log(command)");
- db.execSQL("create index if not exists log_date_index on log(date)");
- oldVersion = 1;
+ oldVersion = 4;
}
-
+
if (oldVersion == 1 || oldVersion == 2) {
db.execSQL("create table if not exists settings (key TEXT PRIMARY KEY, value TEXT)");
oldVersion = 3;
}
- }
-
- public static void getPackageInfoForUid(Context context, UidCommand cpi) {
- try {
- PackageManager pm = context.getPackageManager();
- PackageInfo pi = context.getPackageManager().getPackageInfo(pm.getPackagesForUid(cpi.uid)[0], 0);
- cpi.name = pi.applicationInfo.loadLabel(pm).toString();
- cpi.packageName = pi.packageName;
- }
- catch (Exception ex) {
+
+ if (oldVersion == 3) {
+ // grab all old logs and migrate
+ SQLiteDatabase superuser = new SuperuserDatabaseHelper(mContext).getWritableDatabase();
+
+ ArrayList<LogEntry> logs = SuperuserDatabaseHelper.getLogs(mContext, db);
+ superuser.beginTransaction();
+ try {
+ for (LogEntry log: logs) {
+ SuperuserDatabaseHelper.addLog(superuser, log);
+ }
+
+ Cursor c = db.query("settings", null, null,null, null, null, null);
+ while (c.moveToNext()) {
+ String key = c.getString(c.getColumnIndex("key"));
+ String value = c.getString(c.getColumnIndex("value"));
+ ContentValues cv = new ContentValues();
+ cv.put("key", key);
+ cv.put("value", value);
+
+ superuser.replace("settings", null, cv);
+ }
+ }
+ catch (Exception e) {
+ e.printStackTrace();
+ }
+ finally {
+ superuser.setTransactionSuccessful();
+ superuser.endTransaction();
+ superuser.close();
+ }
+
+ db.execSQL("drop table if exists log");
+ db.execSQL("drop table if exists settings");
+ oldVersion = 4;
}
}
public static void setPolicy(Context context, UidPolicy policy) {
SQLiteDatabase db = new SuDatabaseHelper(context).getWritableDatabase();
- getPackageInfoForUid(context, policy);
+ policy.getPackageInfo(context);
ContentValues values = new ContentValues();
values.put("logging", policy.logging);
db.replace("uid_policy", null, values);
db.close();
}
-
- private static void getUidCommand(Cursor c, UidCommand u) {
- u.uid = c.getInt(c.getColumnIndex("uid"));
- u.command = c.getString(c.getColumnIndex("command"));
- u.name = c.getString(c.getColumnIndex("name"));
- u.packageName = c.getString(c.getColumnIndex("package_name"));
- u.desiredUid = c.getInt(c.getColumnIndex("desired_uid"));
- u.desiredName = c.getString(c.getColumnIndex("desired_name"));
- u.username = c.getString(c.getColumnIndex("username"));
- }
-
- private static UidPolicy getPolicy(Context context, Cursor c) {
+
+ static UidPolicy getPolicy(Context context, Cursor c) {
UidPolicy u = new UidPolicy();
- getUidCommand(c, u);
+ u.getUidCommand(c);
u.policy = c.getString(c.getColumnIndex("policy"));
u.until = c.getInt(c.getColumnIndex("until"));
u.logging = c.getInt(c.getColumnIndex("logging")) != 0;
- ArrayList<LogEntry> logs = getLogs(context, u, 1);
+ ArrayList<LogEntry> logs = SuperuserDatabaseHelper.getLogs(context, u, 1);
if (logs.size() > 0)
u.last = logs.get(0).date;
return u;
}
return ret;
}
-
- public static ArrayList<LogEntry> getLogs(Context context, UidPolicy policy, int limit) {
- ArrayList<LogEntry> ret = new ArrayList<LogEntry>();
- SQLiteDatabase db = new SuDatabaseHelper(context).getReadableDatabase();
- Cursor c;
- if (policy.command != null)
- c = db.query("log", null, "uid = ? and desired_uid = ? and command = ?", new String[] { String.valueOf(policy.uid), String.valueOf(policy.desiredUid), policy.command }, null, null, "date DESC", limit == -1 ? null : String.valueOf(limit));
- else
- c = db.query("log", null, "uid = ? and desired_uid = ?", new String[] { String.valueOf(policy.uid), String.valueOf(policy.desiredUid) }, null, null, "date DESC", limit == -1 ? null : String.valueOf(limit));
- try {
- while (c.moveToNext()) {
- LogEntry l = new LogEntry();
- ret.add(l);
- getUidCommand(c, l);
- l.id = c.getLong(c.getColumnIndex("id"));
- l.date = c.getInt(c.getColumnIndex("date"));
- l.action = c.getString(c.getColumnIndex("action"));
- }
- }
- catch (Exception ex) {
- }
- finally {
- c.close();
- db.close();
- }
- return ret;
- }
-
- public static ArrayList<LogEntry> getLogs(Context context) {
- ArrayList<LogEntry> ret = new ArrayList<LogEntry>();
- SQLiteDatabase db = new SuDatabaseHelper(context).getReadableDatabase();
- Cursor c = db.query("log", null, null, null, null, null, "date DESC");
- try {
- while (c.moveToNext()) {
- LogEntry l = new LogEntry();
- ret.add(l);
- getUidCommand(c, l);
- l.id = c.getLong(c.getColumnIndex("id"));
- l.date = c.getInt(c.getColumnIndex("date"));
- l.action = c.getString(c.getColumnIndex("action"));
- }
- }
- catch (Exception ex) {
- }
- finally {
- c.close();
- db.close();
- }
- return ret;
- }
public static void delete(Context context, UidPolicy policy) {
SQLiteDatabase db = new SuDatabaseHelper(context).getWritableDatabase();
}
return null;
}
-
- public static void deleteLogs(Context context) {
- SQLiteDatabase db = new SuDatabaseHelper(context).getWritableDatabase();
- db.delete("log", null, null);
- db.close();
- }
-
- public static void addLog(Context context, LogEntry log) {
- if (!Settings.getLogging(context))
- return;
-
- SQLiteDatabase db = new SuDatabaseHelper(context).getWritableDatabase();
- Cursor c = db.query("uid_policy", null, "uid = ? and command = ? and desired_uid = ?", new String[] { String.valueOf(log.uid), log.command, String.valueOf(log.desiredUid) }, null, null, null, null);
- try {
- if (c.moveToNext()) {
- UidPolicy u = getPolicy(context, c);
- if (!u.logging) {
- db.close();
- return;
- }
- }
- }
- finally {
- c.close();
- }
-
- ContentValues values = new ContentValues();
- values.put("uid", log.uid);
- values.put("command", log.command);
- values.put("action", log.action);
- values.put("date", log.date);
- values.put("name", log.name);
- values.put("desired_uid", log.desiredUid);
- values.put("package_name", log.packageName);
- values.put("desired_name", log.desiredName);
- values.put("username", log.username);
- db.insert("log", null, values);
- db.close();
- }
private static final String LOGTAG = "SuReceiver";
}