OSDN Git Service

LICENSE
[android-x86/external-koush-Superuser.git] / Superuser / src / com / koushikdutta / superuser / db / SuDatabaseHelper.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.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;
28
29 import com.koushikdutta.superuser.util.Settings;
30
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);
35     }
36
37     @Override
38     public void onCreate(SQLiteDatabase db) {
39         onUpgrade(db, 0, CURRENT_VERSION);
40     }
41
42     @Override
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)");
51             oldVersion = 1;
52         }
53         
54         if (oldVersion == 1) {
55             db.execSQL("CREATE TABLE settings (key TEXT PRIMARY KEY, value TEXT);");
56             oldVersion = 2;
57         }
58     }
59     
60     public static void getPackageInfoForUid(Context context, UidCommand cpi) {
61         try {
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;
66         }
67         catch (Exception ex) {
68         }
69     }
70
71     public static void setPolicy(Context context, UidPolicy policy) {
72         SQLiteDatabase db = new SuDatabaseHelper(context).getWritableDatabase();
73         
74         getPackageInfoForUid(context, policy);
75
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);
88         db.close();
89     }
90     
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"));
99     }
100     
101     private static UidPolicy getPolicy(Context context, Cursor c) {
102         UidPolicy u = new UidPolicy();
103         getUidCommand(c, u);
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;
107         
108         ArrayList<LogEntry> logs = getLogs(context, u, 1);
109         if (logs.size() > 0)
110             u.last = logs.get(0).date;
111         return u;
112     }
113     
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);
118         try {
119             while (c.moveToNext()) {
120                 UidPolicy u = getPolicy(context, c);
121                 ret.add(u);
122             }
123         }
124         catch (Exception ex) {
125             ex.printStackTrace();
126         }
127         finally {
128             c.close();
129             db.close();
130         }
131         return ret;
132     }
133     
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();
137         Cursor c;
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));
140         else
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));
142         try {
143             while (c.moveToNext()) {
144                 LogEntry l = new LogEntry();
145                 ret.add(l);
146                 getUidCommand(c, l);
147                 l.id = c.getLong(c.getColumnIndex("id"));
148                 l.date = c.getInt(c.getColumnIndex("date"));
149                 l.action = c.getString(c.getColumnIndex("action"));
150             }
151         }
152         catch (Exception ex) {
153         }
154         finally {
155             c.close();
156             db.close();
157         }
158         return ret;
159     }
160     
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");
165         try {
166             while (c.moveToNext()) {
167                 LogEntry l = new LogEntry();
168                 ret.add(l);
169                 getUidCommand(c, l);
170                 l.id = c.getLong(c.getColumnIndex("id"));
171                 l.date = c.getInt(c.getColumnIndex("date"));
172                 l.action = c.getString(c.getColumnIndex("action"));
173             }
174         }
175         catch (Exception ex) {
176         }
177         finally {
178             c.close();
179             db.close();
180         }
181         return ret;
182     }
183
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) });
188         else
189             db.delete("uid_policy", "uid = ? and desired_uid = ?", new String[] { String.valueOf(policy.uid), String.valueOf(policy.desiredUid) });
190         db.close();
191     }
192     
193     public static UidPolicy get(Context context, int uid, int desiredUid, String command) {
194         SQLiteDatabase db = new SuDatabaseHelper(context).getReadableDatabase();
195         Cursor c;
196         if (command != null)
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);
198         else
199             c = db.query("uid_policy", null, "uid = ? and desired_uid = ?", new String[] { String.valueOf(uid), String.valueOf(desiredUid) }, null, null, null);
200         try {
201             if (c.moveToNext()) {
202                 return getPolicy(context, c);
203             }
204         }
205         finally {
206             c.close();
207             db.close();
208         }
209         return null;
210     }
211
212     public static void deleteLogs(Context context) {
213         SQLiteDatabase db = new SuDatabaseHelper(context).getWritableDatabase();
214         db.delete("log", null, null);
215         db.close();
216     }
217     
218     public static void addLog(Context context, LogEntry log) {
219         getPackageInfoForUid(context, log);
220
221         if (!Settings.getLogging(context))
222             return;
223         
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);
226         try {
227             if (c.moveToNext()) {
228                 UidPolicy u = getPolicy(context, c);
229                 if (!u.logging) {
230                     db.close();
231                     return;
232                 }
233             }
234         }
235         finally {
236             c.close();
237         }
238
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);
250         db.close();
251     }
252     private static final String LOGTAG = "SuReceiver";
253 }