1 package com.koushikdutta.superuser.db;
3 import java.util.ArrayList;
5 import android.content.ContentValues;
6 import android.content.Context;
7 import android.content.pm.PackageInfo;
8 import android.content.pm.PackageManager;
9 import android.database.Cursor;
10 import android.database.sqlite.SQLiteDatabase;
11 import android.database.sqlite.SQLiteOpenHelper;
13 import com.koushikdutta.superuser.util.Settings;
15 public class SuDatabaseHelper extends SQLiteOpenHelper {
16 private static final int CURRENT_VERSION = 2;
17 public SuDatabaseHelper(Context context) {
18 super(context, "su.sqlite", null, CURRENT_VERSION);
22 public void onCreate(SQLiteDatabase db) {
23 onUpgrade(db, 0, CURRENT_VERSION);
27 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
28 if (oldVersion == 0) {
29 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))");
30 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)");
31 db.execSQL("create index if not exists log_uid_index on log(uid)");
32 db.execSQL("create index if not exists log_desired_uid_index on log(desired_uid)");
33 db.execSQL("create index if not exists log_command_index on log(command)");
34 db.execSQL("create index if not exists log_date_index on log(date)");
38 if (oldVersion == 1) {
39 db.execSQL("CREATE TABLE settings (key TEXT PRIMARY KEY, value TEXT);");
44 public static void getPackageInfoForUid(Context context, UidCommand cpi) {
46 PackageManager pm = context.getPackageManager();
47 PackageInfo pi = context.getPackageManager().getPackageInfo(pm.getPackagesForUid(cpi.uid)[0], 0);
48 cpi.name = pi.applicationInfo.loadLabel(pm).toString();
49 cpi.packageName = pi.packageName;
51 catch (Exception ex) {
55 public static void setPolicy(Context context, UidPolicy policy) {
56 SQLiteDatabase db = new SuDatabaseHelper(context).getWritableDatabase();
58 getPackageInfoForUid(context, policy);
60 ContentValues values = new ContentValues();
61 values.put("logging", policy.logging);
62 values.put("uid", policy.uid);
63 values.put("command", policy.command);
64 values.put("policy", policy.policy);
65 values.put("until", policy.until);
66 values.put("name", policy.name);
67 values.put("package_name", policy.packageName);
68 values.put("desired_uid", policy.desiredUid);
69 values.put("desired_name", policy.desiredName);
70 values.put("username", policy.username);
71 db.replace("uid_policy", null, values);
75 private static void getUidCommand(Cursor c, UidCommand u) {
76 u.uid = c.getInt(c.getColumnIndex("uid"));
77 u.command = c.getString(c.getColumnIndex("command"));
78 u.name = c.getString(c.getColumnIndex("name"));
79 u.packageName = c.getString(c.getColumnIndex("package_name"));
80 u.desiredUid = c.getInt(c.getColumnIndex("desired_uid"));
81 u.desiredName = c.getString(c.getColumnIndex("desired_name"));
82 u.username = c.getString(c.getColumnIndex("username"));
85 private static UidPolicy getPolicy(Context context, Cursor c) {
86 UidPolicy u = new UidPolicy();
88 u.policy = c.getString(c.getColumnIndex("policy"));
89 u.until = c.getInt(c.getColumnIndex("until"));
90 u.logging = c.getInt(c.getColumnIndex("logging")) != 0;
92 ArrayList<LogEntry> logs = getLogs(context, u, 1);
94 u.last = logs.get(0).date;
98 public static ArrayList<UidPolicy> getPolicies(Context context) {
99 ArrayList<UidPolicy> ret = new ArrayList<UidPolicy>();
100 SQLiteDatabase db = new SuDatabaseHelper(context).getReadableDatabase();
101 Cursor c = db.query("uid_policy", null, null, null, null, null, null);
103 while (c.moveToNext()) {
104 UidPolicy u = getPolicy(context, c);
108 catch (Exception ex) {
109 ex.printStackTrace();
118 public static ArrayList<LogEntry> getLogs(Context context, UidPolicy policy, int limit) {
119 ArrayList<LogEntry> ret = new ArrayList<LogEntry>();
120 SQLiteDatabase db = new SuDatabaseHelper(context).getReadableDatabase();
122 if (policy.command != null)
123 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));
125 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));
127 while (c.moveToNext()) {
128 LogEntry l = new LogEntry();
131 l.id = c.getLong(c.getColumnIndex("id"));
132 l.date = c.getInt(c.getColumnIndex("date"));
133 l.action = c.getString(c.getColumnIndex("action"));
136 catch (Exception ex) {
145 public static ArrayList<LogEntry> getLogs(Context context) {
146 ArrayList<LogEntry> ret = new ArrayList<LogEntry>();
147 SQLiteDatabase db = new SuDatabaseHelper(context).getReadableDatabase();
148 Cursor c = db.query("log", null, null, null, null, null, "date DESC");
150 while (c.moveToNext()) {
151 LogEntry l = new LogEntry();
154 l.id = c.getLong(c.getColumnIndex("id"));
155 l.date = c.getInt(c.getColumnIndex("date"));
156 l.action = c.getString(c.getColumnIndex("action"));
159 catch (Exception ex) {
168 public static void delete(Context context, UidPolicy policy) {
169 SQLiteDatabase db = new SuDatabaseHelper(context).getWritableDatabase();
170 if (policy.command != null)
171 db.delete("uid_policy", "uid = ? and command = ? and desired_uid = ?", new String[] { String.valueOf(policy.uid), policy.command, String.valueOf(policy.desiredUid) });
173 db.delete("uid_policy", "uid = ? and desired_uid = ?", new String[] { String.valueOf(policy.uid), String.valueOf(policy.desiredUid) });
177 public static UidPolicy get(Context context, int uid, int desiredUid, String command) {
178 SQLiteDatabase db = new SuDatabaseHelper(context).getReadableDatabase();
181 c = db.query("uid_policy", null, "uid = ? and command = ? and desired_uid = ?", new String[] { String.valueOf(uid), command, String.valueOf(desiredUid) }, null, null, null);
183 c = db.query("uid_policy", null, "uid = ? and desired_uid = ?", new String[] { String.valueOf(uid), String.valueOf(desiredUid) }, null, null, null);
185 if (c.moveToNext()) {
186 return getPolicy(context, c);
196 public static void deleteLogs(Context context) {
197 SQLiteDatabase db = new SuDatabaseHelper(context).getWritableDatabase();
198 db.delete("log", null, null);
202 public static void addLog(Context context, LogEntry log) {
203 getPackageInfoForUid(context, log);
205 if (!Settings.getLogging(context))
208 SQLiteDatabase db = new SuDatabaseHelper(context).getWritableDatabase();
209 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);
211 if (c.moveToNext()) {
212 UidPolicy u = getPolicy(context, c);
223 ContentValues values = new ContentValues();
224 values.put("uid", log.uid);
225 values.put("command", log.command);
226 values.put("action", log.action);
227 values.put("date", (int)(System.currentTimeMillis() / 1000));
228 values.put("name", log.name);
229 values.put("desired_uid", log.desiredUid);
230 values.put("package_name", log.packageName);
231 values.put("desired_name", log.desiredName);
232 values.put("username", log.username);
233 db.insert("log", null, values);
236 private static final String LOGTAG = "SuReceiver";