2 * Copyright (C) 2013 Koushik Dutta (@koush)
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.koushikdutta.superuser.db;
19 import java.util.ArrayList;
21 import android.content.ContentValues;
22 import android.content.Context;
23 import android.content.pm.PackageInfo;
24 import android.content.pm.PackageManager;
25 import android.database.Cursor;
26 import android.database.sqlite.SQLiteDatabase;
27 import android.database.sqlite.SQLiteOpenHelper;
29 import com.koushikdutta.superuser.util.Settings;
31 public class SuDatabaseHelper extends SQLiteOpenHelper {
32 private static final int CURRENT_VERSION = 2;
33 public SuDatabaseHelper(Context context) {
34 super(context, "su.sqlite", null, CURRENT_VERSION);
38 public void onCreate(SQLiteDatabase db) {
39 onUpgrade(db, 0, CURRENT_VERSION);
43 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
44 if (oldVersion == 0) {
45 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))");
46 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)");
47 db.execSQL("create index if not exists log_uid_index on log(uid)");
48 db.execSQL("create index if not exists log_desired_uid_index on log(desired_uid)");
49 db.execSQL("create index if not exists log_command_index on log(command)");
50 db.execSQL("create index if not exists log_date_index on log(date)");
54 if (oldVersion == 1) {
55 db.execSQL("CREATE TABLE settings (key TEXT PRIMARY KEY, value TEXT);");
60 public static void getPackageInfoForUid(Context context, UidCommand cpi) {
62 PackageManager pm = context.getPackageManager();
63 PackageInfo pi = context.getPackageManager().getPackageInfo(pm.getPackagesForUid(cpi.uid)[0], 0);
64 cpi.name = pi.applicationInfo.loadLabel(pm).toString();
65 cpi.packageName = pi.packageName;
67 catch (Exception ex) {
71 public static void setPolicy(Context context, UidPolicy policy) {
72 SQLiteDatabase db = new SuDatabaseHelper(context).getWritableDatabase();
74 getPackageInfoForUid(context, policy);
76 ContentValues values = new ContentValues();
77 values.put("logging", policy.logging);
78 values.put("uid", policy.uid);
79 values.put("command", policy.command);
80 values.put("policy", policy.policy);
81 values.put("until", policy.until);
82 values.put("name", policy.name);
83 values.put("package_name", policy.packageName);
84 values.put("desired_uid", policy.desiredUid);
85 values.put("desired_name", policy.desiredName);
86 values.put("username", policy.username);
87 db.replace("uid_policy", null, values);
91 private static void getUidCommand(Cursor c, UidCommand u) {
92 u.uid = c.getInt(c.getColumnIndex("uid"));
93 u.command = c.getString(c.getColumnIndex("command"));
94 u.name = c.getString(c.getColumnIndex("name"));
95 u.packageName = c.getString(c.getColumnIndex("package_name"));
96 u.desiredUid = c.getInt(c.getColumnIndex("desired_uid"));
97 u.desiredName = c.getString(c.getColumnIndex("desired_name"));
98 u.username = c.getString(c.getColumnIndex("username"));
101 private static UidPolicy getPolicy(Context context, Cursor c) {
102 UidPolicy u = new UidPolicy();
104 u.policy = c.getString(c.getColumnIndex("policy"));
105 u.until = c.getInt(c.getColumnIndex("until"));
106 u.logging = c.getInt(c.getColumnIndex("logging")) != 0;
108 ArrayList<LogEntry> logs = getLogs(context, u, 1);
110 u.last = logs.get(0).date;
114 public static ArrayList<UidPolicy> getPolicies(Context context) {
115 ArrayList<UidPolicy> ret = new ArrayList<UidPolicy>();
116 SQLiteDatabase db = new SuDatabaseHelper(context).getReadableDatabase();
117 Cursor c = db.query("uid_policy", null, null, null, null, null, null);
119 while (c.moveToNext()) {
120 UidPolicy u = getPolicy(context, c);
124 catch (Exception ex) {
125 ex.printStackTrace();
134 public static ArrayList<LogEntry> getLogs(Context context, UidPolicy policy, int limit) {
135 ArrayList<LogEntry> ret = new ArrayList<LogEntry>();
136 SQLiteDatabase db = new SuDatabaseHelper(context).getReadableDatabase();
138 if (policy.command != null)
139 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));
141 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));
143 while (c.moveToNext()) {
144 LogEntry l = new LogEntry();
147 l.id = c.getLong(c.getColumnIndex("id"));
148 l.date = c.getInt(c.getColumnIndex("date"));
149 l.action = c.getString(c.getColumnIndex("action"));
152 catch (Exception ex) {
161 public static ArrayList<LogEntry> getLogs(Context context) {
162 ArrayList<LogEntry> ret = new ArrayList<LogEntry>();
163 SQLiteDatabase db = new SuDatabaseHelper(context).getReadableDatabase();
164 Cursor c = db.query("log", null, null, null, null, null, "date DESC");
166 while (c.moveToNext()) {
167 LogEntry l = new LogEntry();
170 l.id = c.getLong(c.getColumnIndex("id"));
171 l.date = c.getInt(c.getColumnIndex("date"));
172 l.action = c.getString(c.getColumnIndex("action"));
175 catch (Exception ex) {
184 public static void delete(Context context, UidPolicy policy) {
185 SQLiteDatabase db = new SuDatabaseHelper(context).getWritableDatabase();
186 if (policy.command != null)
187 db.delete("uid_policy", "uid = ? and command = ? and desired_uid = ?", new String[] { String.valueOf(policy.uid), policy.command, String.valueOf(policy.desiredUid) });
189 db.delete("uid_policy", "uid = ? and desired_uid = ?", new String[] { String.valueOf(policy.uid), String.valueOf(policy.desiredUid) });
193 public static UidPolicy get(Context context, int uid, int desiredUid, String command) {
194 SQLiteDatabase db = new SuDatabaseHelper(context).getReadableDatabase();
197 c = db.query("uid_policy", null, "uid = ? and command = ? and desired_uid = ?", new String[] { String.valueOf(uid), command, String.valueOf(desiredUid) }, null, null, null);
199 c = db.query("uid_policy", null, "uid = ? and desired_uid = ?", new String[] { String.valueOf(uid), String.valueOf(desiredUid) }, null, null, null);
201 if (c.moveToNext()) {
202 return getPolicy(context, c);
212 public static void deleteLogs(Context context) {
213 SQLiteDatabase db = new SuDatabaseHelper(context).getWritableDatabase();
214 db.delete("log", null, null);
218 public static void addLog(Context context, LogEntry log) {
219 getPackageInfoForUid(context, log);
221 if (!Settings.getLogging(context))
224 SQLiteDatabase db = new SuDatabaseHelper(context).getWritableDatabase();
225 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);
227 if (c.moveToNext()) {
228 UidPolicy u = getPolicy(context, c);
239 ContentValues values = new ContentValues();
240 values.put("uid", log.uid);
241 values.put("command", log.command);
242 values.put("action", log.action);
243 values.put("date", (int)(System.currentTimeMillis() / 1000));
244 values.put("name", log.name);
245 values.put("desired_uid", log.desiredUid);
246 values.put("package_name", log.packageName);
247 values.put("desired_name", log.desiredName);
248 values.put("username", log.username);
249 db.insert("log", null, values);
252 private static final String LOGTAG = "SuReceiver";