package com.koushikdutta.superuser;
import java.util.ArrayList;
+import java.util.Date;
import android.content.Context;
import android.content.res.Configuration;
+import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.ImageView;
+import com.koushikdutta.superuser.db.LogEntry;
import com.koushikdutta.superuser.db.SuDatabaseHelper;
+import com.koushikdutta.superuser.db.SuperuserDatabaseHelper;
import com.koushikdutta.superuser.db.UidPolicy;
import com.koushikdutta.widgets.FragmentInterfaceWrapper;
import com.koushikdutta.widgets.ListContentFragmentInternal;
clear();
final ArrayList<UidPolicy> policies = SuDatabaseHelper.getPolicies(getActivity());
- for (UidPolicy up: policies) {
- addPolicy(up);
+ SQLiteDatabase db = new SuperuserDatabaseHelper(getActivity()).getReadableDatabase();
+ try {
+ for (UidPolicy up: policies) {
+ int last = 0;
+ ArrayList<LogEntry> logs = SuperuserDatabaseHelper.getLogs(db, up, 1);
+ if (logs.size() > 0)
+ last = logs.get(0).date;
+ addPolicy(up, last);
+ }
+ }
+ finally {
+ db.close();
}
}
showAllLogs();
}
+ public Date getLastDate(int last) {
+ return new Date((long)last * 1000);
+ }
- void addPolicy(final UidPolicy up) {
+ void addPolicy(final UidPolicy up, final int last) {
java.text.DateFormat df = DateFormat.getLongDateFormat(getActivity());
- String date = df.format(up.getLastDate());
- if (up.last == 0)
+ String date;
+ if (last == 0)
date = null;
+ else
+ date = df.format(getLastDate(last));
ListItem li = addItem(up.getPolicyResource(), new ListItem(this, up.name, date) {
public void onClick(View view) {
super.onClick(view);
le.getPackageInfo(context);
// wait a bit before logging... lots of concurrent su requests at the same time
// cause a db lock
+ // TODO: this hack should no longer be necessary
new Handler().postDelayed(new Runnable() {
public void run() {
try {
import android.content.ContentValues;
import android.content.Context;
-import android.content.pm.PackageInfo;
-import android.content.pm.PackageManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
-import com.koushikdutta.superuser.util.Settings;
-
public class SuDatabaseHelper extends SQLiteOpenHelper {
private static final int CURRENT_VERSION = 4;
Context mContext;
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (oldVersion == 0) {
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))");
+ // skip past to v4, as the next migrations have legacy tables, which were moved
oldVersion = 4;
}
oldVersion = 3;
}
+ // migrate the logs and settings outta this db. fix for db locking issues by su, which
+ // only needs a readonly db.
if (oldVersion == 3) {
- // grab all old logs and migrate
SQLiteDatabase superuser = new SuperuserDatabaseHelper(mContext).getWritableDatabase();
ArrayList<LogEntry> logs = SuperuserDatabaseHelper.getLogs(mContext, db);
u.policy = c.getString(c.getColumnIndex("policy"));
u.until = c.getInt(c.getColumnIndex("until"));
u.logging = c.getInt(c.getColumnIndex("logging")) != 0;
-
- ArrayList<LogEntry> logs = SuperuserDatabaseHelper.getLogs(context, u, 1);
- if (logs.size() > 0)
- u.last = logs.get(0).date;
return u;
}
}
public static ArrayList<LogEntry> getLogs(Context context, UidPolicy policy, int limit) {
- ArrayList<LogEntry> ret = new ArrayList<LogEntry>();
SQLiteDatabase db = new SuperuserDatabaseHelper(context).getReadableDatabase();
+ try {
+ return getLogs(db, policy, limit);
+ }
+ finally {
+ db.close();
+ }
+ }
+ public static ArrayList<LogEntry> getLogs(SQLiteDatabase db, UidPolicy policy, int limit) {
+ ArrayList<LogEntry> ret = new ArrayList<LogEntry>();
Cursor c;
if (policy.command != null)
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));
}
finally {
c.close();
- db.close();
}
return ret;
}
db.close();
}
+ db = new SuperuserDatabaseHelper(context).getReadableDatabase();
try {
addLog(db, log);
}
return R.string.interactive;
return R.string.deny;
}
-
- public int last;
- public Date getLastDate() {
- return new Date((long)last * 1000);
- }
}