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.database.Cursor;
24 import android.database.sqlite.SQLiteDatabase;
25 import android.database.sqlite.SQLiteOpenHelper;
26 import android.text.TextUtils;
28 import com.koushikdutta.superuser.util.Settings;
30 public class SuperuserDatabaseHelper extends SQLiteOpenHelper {
31 private static final int CURRENT_VERSION = 1;
32 public SuperuserDatabaseHelper(Context context) {
33 super(context, "superuser.sqlite", null, CURRENT_VERSION);
37 public void onCreate(SQLiteDatabase db) {
38 onUpgrade(db, 0, CURRENT_VERSION);
42 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
43 if (oldVersion == 0) {
44 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 not null, date integer, action text, package_name text, name text)");
45 db.execSQL("create index if not exists log_uid_index on log(uid)");
46 db.execSQL("create index if not exists log_desired_uid_index on log(desired_uid)");
47 db.execSQL("create index if not exists log_command_index on log(command)");
48 db.execSQL("create index if not exists log_date_index on log(date)");
49 db.execSQL("create table if not exists settings (key text primary key not null, value text)");
54 public static ArrayList<LogEntry> getLogs(Context context, UidPolicy policy, int limit) {
55 SQLiteDatabase db = new SuperuserDatabaseHelper(context).getReadableDatabase();
57 return getLogs(db, policy, limit);
63 public static ArrayList<LogEntry> getLogs(SQLiteDatabase db, UidPolicy policy, int limit) {
64 ArrayList<LogEntry> ret = new ArrayList<LogEntry>();
66 if (!TextUtils.isEmpty(policy.command))
67 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));
69 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));
71 while (c.moveToNext()) {
72 LogEntry l = new LogEntry();
75 l.id = c.getLong(c.getColumnIndex("id"));
76 l.date = c.getInt(c.getColumnIndex("date"));
77 l.action = c.getString(c.getColumnIndex("action"));
80 catch (Exception ex) {
88 public static ArrayList<LogEntry> getLogs(Context context) {
89 SQLiteDatabase db = new SuperuserDatabaseHelper(context).getReadableDatabase();
91 return getLogs(context, db);
97 public static ArrayList<LogEntry> getLogs(Context context, SQLiteDatabase db) {
98 ArrayList<LogEntry> ret = new ArrayList<LogEntry>();
99 Cursor c = db.query("log", null, null, null, null, null, "date DESC");
101 while (c.moveToNext()) {
102 LogEntry l = new LogEntry();
105 l.id = c.getLong(c.getColumnIndex("id"));
106 l.date = c.getInt(c.getColumnIndex("date"));
107 l.action = c.getString(c.getColumnIndex("action"));
110 catch (Exception ex) {
118 public static void deleteLogs(Context context) {
119 SQLiteDatabase db = new SuperuserDatabaseHelper(context).getWritableDatabase();
120 db.delete("log", null, null);
124 static void addLog(SQLiteDatabase db, LogEntry log) {
125 ContentValues values = new ContentValues();
126 values.put("uid", log.uid);
127 // nulls are considered unique, even from other nulls. blerg.
128 // http://stackoverflow.com/questions/3906811/null-permitted-in-primary-key-why-and-in-which-dbms
129 if (log.command == null)
131 values.put("command", log.command);
132 values.put("action", log.action);
133 values.put("date", log.date);
134 values.put("name", log.name);
135 values.put("desired_uid", log.desiredUid);
136 values.put("package_name", log.packageName);
137 values.put("desired_name", log.desiredName);
138 values.put("username", log.username);
139 db.insert("log", null, values);
142 public static UidPolicy addLog(Context context, LogEntry log) {
143 // nulls are considered unique, even from other nulls. blerg.
144 // http://stackoverflow.com/questions/3906811/null-permitted-in-primary-key-why-and-in-which-dbms
145 if (log.command == null)
148 // grab the policy and add a log
150 SQLiteDatabase su = new SuDatabaseHelper(context).getReadableDatabase();
151 Cursor c = su.query("uid_policy", null, "uid = ? and (command = ? or command = ?) and desired_uid = ?", new String[] { String.valueOf(log.uid), log.command, "", String.valueOf(log.desiredUid) }, null, null, null, null);
153 if (c.moveToNext()) {
154 u = SuDatabaseHelper.getPolicy(c);
162 if (u != null && !u.logging)
165 if (!Settings.getLogging(context))
168 SQLiteDatabase superuser = new SuperuserDatabaseHelper(context).getWritableDatabase();
170 // delete logs over 2 weeks
171 superuser.delete("log", "date < ?", new String[] { String.valueOf((System.currentTimeMillis() - 14L * 24L * 60L * 60L * 1000L) / 1000L) });
172 addLog(superuser, log);