OSDN Git Service

fix up some db churn by reusing a single db connection
authorKoushik Dutta <koushd@gmail.com>
Tue, 5 Mar 2013 22:06:08 +0000 (14:06 -0800)
committerKoushik Dutta <koushd@gmail.com>
Tue, 5 Mar 2013 22:06:08 +0000 (14:06 -0800)
Change-Id: I2fd61d43331cd5014cde888a18b0c14d47fcc149

Superuser/src/com/koushikdutta/superuser/PolicyFragmentInternal.java
Superuser/src/com/koushikdutta/superuser/SuReceiver.java
Superuser/src/com/koushikdutta/superuser/db/SuDatabaseHelper.java
Superuser/src/com/koushikdutta/superuser/db/SuperuserDatabaseHelper.java
Superuser/src/com/koushikdutta/superuser/db/UidPolicy.java

index 9332c85..d16baff 100644 (file)
 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<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();
         }
     }
     
@@ -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);
index f4ab638..b44b803 100644 (file)
@@ -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 {
index f3bcee6..7626283 100644 (file)
@@ -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<LogEntry> 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<LogEntry> logs = SuperuserDatabaseHelper.getLogs(context, u, 1);
-        if (logs.size() > 0)
-            u.last = logs.get(0).date;
         return u;
     }
     
index f072e35..71adca4 100644 (file)
@@ -51,8 +51,16 @@ public class SuperuserDatabaseHelper extends SQLiteOpenHelper {
     }
     
     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));
@@ -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);
         }
index 4e1f5a8..8d14cfe 100644 (file)
@@ -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);
-    }
 }