From 1bd25fcb2fdf045457f747f46b3d671e9f1f2e22 Mon Sep 17 00:00:00 2001 From: Koushik Dutta Date: Tue, 5 Mar 2013 14:06:08 -0800 Subject: [PATCH] fix up some db churn by reusing a single db connection Change-Id: I2fd61d43331cd5014cde888a18b0c14d47fcc149 --- .../superuser/PolicyFragmentInternal.java | 29 ++++++++++++++++++---- .../src/com/koushikdutta/superuser/SuReceiver.java | 1 + .../superuser/db/SuDatabaseHelper.java | 12 +++------ .../superuser/db/SuperuserDatabaseHelper.java | 12 +++++++-- .../com/koushikdutta/superuser/db/UidPolicy.java | 5 ---- 5 files changed, 38 insertions(+), 21 deletions(-) diff --git a/Superuser/src/com/koushikdutta/superuser/PolicyFragmentInternal.java b/Superuser/src/com/koushikdutta/superuser/PolicyFragmentInternal.java index 9332c85..d16baff 100644 --- a/Superuser/src/com/koushikdutta/superuser/PolicyFragmentInternal.java +++ b/Superuser/src/com/koushikdutta/superuser/PolicyFragmentInternal.java @@ -17,9 +17,11 @@ 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; @@ -32,7 +34,9 @@ import android.view.MenuItem.OnMenuItemClickListener; 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; @@ -61,8 +65,18 @@ public class PolicyFragmentInternal extends ListContentFragmentInternal { clear(); final ArrayList 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 logs = SuperuserDatabaseHelper.getLogs(db, up, 1); + if (logs.size() > 0) + last = logs.get(0).date; + addPolicy(up, last); + } + } + finally { + db.close(); } } @@ -93,12 +107,17 @@ public class PolicyFragmentInternal extends ListContentFragmentInternal { 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); diff --git a/Superuser/src/com/koushikdutta/superuser/SuReceiver.java b/Superuser/src/com/koushikdutta/superuser/SuReceiver.java index f4ab638..b44b803 100644 --- a/Superuser/src/com/koushikdutta/superuser/SuReceiver.java +++ b/Superuser/src/com/koushikdutta/superuser/SuReceiver.java @@ -61,6 +61,7 @@ public class SuReceiver extends BroadcastReceiver { 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 { diff --git a/Superuser/src/com/koushikdutta/superuser/db/SuDatabaseHelper.java b/Superuser/src/com/koushikdutta/superuser/db/SuDatabaseHelper.java index f3bcee6..7626283 100644 --- a/Superuser/src/com/koushikdutta/superuser/db/SuDatabaseHelper.java +++ b/Superuser/src/com/koushikdutta/superuser/db/SuDatabaseHelper.java @@ -20,14 +20,10 @@ import java.util.ArrayList; 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; @@ -45,6 +41,7 @@ public class SuDatabaseHelper extends SQLiteOpenHelper { 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; } @@ -53,8 +50,9 @@ public class SuDatabaseHelper extends SQLiteOpenHelper { 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 logs = SuperuserDatabaseHelper.getLogs(mContext, db); @@ -116,10 +114,6 @@ public class SuDatabaseHelper extends SQLiteOpenHelper { u.policy = c.getString(c.getColumnIndex("policy")); u.until = c.getInt(c.getColumnIndex("until")); u.logging = c.getInt(c.getColumnIndex("logging")) != 0; - - ArrayList logs = SuperuserDatabaseHelper.getLogs(context, u, 1); - if (logs.size() > 0) - u.last = logs.get(0).date; return u; } diff --git a/Superuser/src/com/koushikdutta/superuser/db/SuperuserDatabaseHelper.java b/Superuser/src/com/koushikdutta/superuser/db/SuperuserDatabaseHelper.java index f072e35..71adca4 100644 --- a/Superuser/src/com/koushikdutta/superuser/db/SuperuserDatabaseHelper.java +++ b/Superuser/src/com/koushikdutta/superuser/db/SuperuserDatabaseHelper.java @@ -51,8 +51,16 @@ public class SuperuserDatabaseHelper extends SQLiteOpenHelper { } public static ArrayList getLogs(Context context, UidPolicy policy, int limit) { - ArrayList ret = new ArrayList(); SQLiteDatabase db = new SuperuserDatabaseHelper(context).getReadableDatabase(); + try { + return getLogs(db, policy, limit); + } + finally { + db.close(); + } + } + public static ArrayList getLogs(SQLiteDatabase db, UidPolicy policy, int limit) { + ArrayList ret = new ArrayList(); 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)); @@ -72,7 +80,6 @@ public class SuperuserDatabaseHelper extends SQLiteOpenHelper { } finally { c.close(); - db.close(); } return ret; } @@ -147,6 +154,7 @@ public class SuperuserDatabaseHelper extends SQLiteOpenHelper { db.close(); } + db = new SuperuserDatabaseHelper(context).getReadableDatabase(); try { addLog(db, log); } diff --git a/Superuser/src/com/koushikdutta/superuser/db/UidPolicy.java b/Superuser/src/com/koushikdutta/superuser/db/UidPolicy.java index 4e1f5a8..8d14cfe 100644 --- a/Superuser/src/com/koushikdutta/superuser/db/UidPolicy.java +++ b/Superuser/src/com/koushikdutta/superuser/db/UidPolicy.java @@ -39,9 +39,4 @@ public class UidPolicy extends UidCommand { return R.string.interactive; return R.string.deny; } - - public int last; - public Date getLastDate() { - return new Date((long)last * 1000); - } } -- 2.11.0