OSDN Git Service

Fix several compiler warnings
[android-x86/external-koush-Superuser.git] / Superuser / src / com / koushikdutta / superuser / db / SuperuserDatabaseHelper.java
1 /*
2  * Copyright (C) 2013 Koushik Dutta (@koush)
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.koushikdutta.superuser.db;
18
19 import java.util.ArrayList;
20
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;
27
28 import com.koushikdutta.superuser.util.Settings;
29
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);
34     }
35
36     @Override
37     public void onCreate(SQLiteDatabase db) {
38         onUpgrade(db, 0, CURRENT_VERSION);
39     }
40
41     @Override
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)");
50             oldVersion = 1;
51         }
52     }
53
54     public static ArrayList<LogEntry> getLogs(Context context, UidPolicy policy, int limit) {
55         SQLiteDatabase db = new SuperuserDatabaseHelper(context).getReadableDatabase();
56         try {
57             return getLogs(db, policy, limit);
58         }
59         finally {
60             db.close();
61         }
62     }
63     public static ArrayList<LogEntry> getLogs(SQLiteDatabase db, UidPolicy policy, int limit) {
64         ArrayList<LogEntry> ret = new ArrayList<LogEntry>();
65         Cursor c;
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));
68         else
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));
70         try {
71             while (c.moveToNext()) {
72                 LogEntry l = new LogEntry();
73                 ret.add(l);
74                 l.getUidCommand(c);
75                 l.id = c.getLong(c.getColumnIndex("id"));
76                 l.date = c.getInt(c.getColumnIndex("date"));
77                 l.action = c.getString(c.getColumnIndex("action"));
78             }
79         }
80         catch (Exception ex) {
81         }
82         finally {
83             c.close();
84         }
85         return ret;
86     }
87
88     public static ArrayList<LogEntry> getLogs(Context context) {
89         SQLiteDatabase db = new SuperuserDatabaseHelper(context).getReadableDatabase();
90         try {
91             return getLogs(context, db);
92         }
93         finally {
94             db.close();
95         }
96     }
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");
100         try {
101             while (c.moveToNext()) {
102                 LogEntry l = new LogEntry();
103                 ret.add(l);
104                 l.getUidCommand(c);
105                 l.id = c.getLong(c.getColumnIndex("id"));
106                 l.date = c.getInt(c.getColumnIndex("date"));
107                 l.action = c.getString(c.getColumnIndex("action"));
108             }
109         }
110         catch (Exception ex) {
111         }
112         finally {
113             c.close();
114         }
115         return ret;
116     }
117
118     public static void deleteLogs(Context context) {
119         SQLiteDatabase db = new SuperuserDatabaseHelper(context).getWritableDatabase();
120         db.delete("log", null, null);
121         db.close();
122     }
123
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)
130             log.command = "";
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);
140     }
141
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)
146             log.command = "";
147
148         // grab the policy and add a log
149         UidPolicy u = null;
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);
152         try {
153             if (c.moveToNext()) {
154                 u = SuDatabaseHelper.getPolicy(c);
155             }
156         }
157         finally {
158             c.close();
159             su.close();
160         }
161
162         if (u != null && !u.logging)
163             return u;
164
165         if (!Settings.getLogging(context))
166             return u;
167
168         SQLiteDatabase superuser = new SuperuserDatabaseHelper(context).getWritableDatabase();
169         try {
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);
173         }
174         finally {
175             superuser.close();
176         }
177
178         return u;
179     }
180 }