OSDN Git Service

5600361c60ef3aa61766750d4a72af8251292700
[android-x86/external-koush-Superuser.git] / Superuser / src / com / koushikdutta / superuser / db / SuDatabaseHelper.java
1 package com.koushikdutta.superuser.db;
2
3 import java.util.ArrayList;
4
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;
12
13 import com.koushikdutta.superuser.util.Settings;
14
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);
19     }
20
21     @Override
22     public void onCreate(SQLiteDatabase db) {
23         onUpgrade(db, 0, CURRENT_VERSION);
24     }
25
26     @Override
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)");
35             oldVersion = 1;
36         }
37         
38         if (oldVersion == 1) {
39             db.execSQL("CREATE TABLE settings (key TEXT PRIMARY KEY, value TEXT);");
40             oldVersion = 2;
41         }
42     }
43     
44     public static void getPackageInfoForUid(Context context, UidCommand cpi) {
45         try {
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;
50         }
51         catch (Exception ex) {
52         }
53     }
54
55     public static void setPolicy(Context context, UidPolicy policy) {
56         SQLiteDatabase db = new SuDatabaseHelper(context).getWritableDatabase();
57         
58         getPackageInfoForUid(context, policy);
59
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);
72         db.close();
73     }
74     
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"));
83     }
84     
85     private static UidPolicy getPolicy(Context context, Cursor c) {
86         UidPolicy u = new UidPolicy();
87         getUidCommand(c, u);
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;
91         
92         ArrayList<LogEntry> logs = getLogs(context, u, 1);
93         if (logs.size() > 0)
94             u.last = logs.get(0).date;
95         return u;
96     }
97     
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);
102         try {
103             while (c.moveToNext()) {
104                 UidPolicy u = getPolicy(context, c);
105                 ret.add(u);
106             }
107         }
108         catch (Exception ex) {
109             ex.printStackTrace();
110         }
111         finally {
112             c.close();
113             db.close();
114         }
115         return ret;
116     }
117     
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();
121         Cursor c;
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));
124         else
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));
126         try {
127             while (c.moveToNext()) {
128                 LogEntry l = new LogEntry();
129                 ret.add(l);
130                 getUidCommand(c, l);
131                 l.id = c.getLong(c.getColumnIndex("id"));
132                 l.date = c.getInt(c.getColumnIndex("date"));
133                 l.action = c.getString(c.getColumnIndex("action"));
134             }
135         }
136         catch (Exception ex) {
137         }
138         finally {
139             c.close();
140             db.close();
141         }
142         return ret;
143     }
144     
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");
149         try {
150             while (c.moveToNext()) {
151                 LogEntry l = new LogEntry();
152                 ret.add(l);
153                 getUidCommand(c, l);
154                 l.id = c.getLong(c.getColumnIndex("id"));
155                 l.date = c.getInt(c.getColumnIndex("date"));
156                 l.action = c.getString(c.getColumnIndex("action"));
157             }
158         }
159         catch (Exception ex) {
160         }
161         finally {
162             c.close();
163             db.close();
164         }
165         return ret;
166     }
167
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) });
172         else
173             db.delete("uid_policy", "uid = ? and desired_uid = ?", new String[] { String.valueOf(policy.uid), String.valueOf(policy.desiredUid) });
174         db.close();
175     }
176     
177     public static UidPolicy get(Context context, int uid, int desiredUid, String command) {
178         SQLiteDatabase db = new SuDatabaseHelper(context).getReadableDatabase();
179         Cursor c;
180         if (command != null)
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);
182         else
183             c = db.query("uid_policy", null, "uid = ? and desired_uid = ?", new String[] { String.valueOf(uid), String.valueOf(desiredUid) }, null, null, null);
184         try {
185             if (c.moveToNext()) {
186                 return getPolicy(context, c);
187             }
188         }
189         finally {
190             c.close();
191             db.close();
192         }
193         return null;
194     }
195
196     public static void deleteLogs(Context context) {
197         SQLiteDatabase db = new SuDatabaseHelper(context).getWritableDatabase();
198         db.delete("log", null, null);
199         db.close();
200     }
201     
202     public static void addLog(Context context, LogEntry log) {
203         getPackageInfoForUid(context, log);
204
205         if (!Settings.getLogging(context))
206             return;
207         
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);
210         try {
211             if (c.moveToNext()) {
212                 UidPolicy u = getPolicy(context, c);
213                 if (!u.logging) {
214                     db.close();
215                     return;
216                 }
217             }
218         }
219         finally {
220             c.close();
221         }
222
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);
234         db.close();
235     }
236     private static final String LOGTAG = "SuReceiver";
237 }