]> hydra-www.ietfng.org Git - android-vcpass-oisafe/commitdiff
Some small refactoring in DBHelper
authorNathaniel Wesley Filardo <nwf@pf.priv.oc.ietfng.org>
Thu, 3 Jun 2010 03:27:54 +0000 (23:27 -0400)
committerNathaniel Wesley Filardo <nwf@pf.priv.oc.ietfng.org>
Thu, 3 Jun 2010 03:27:54 +0000 (23:27 -0400)
src/org/openintents/safe/DBHelper.java

index e98c8cc85b32b0d1d31827206716f5ec19f1822a..66570a99ee3196fc1a3915576df06acae9c71446 100644 (file)
@@ -230,48 +230,58 @@ public class DBHelper {
                }
                return version;
     }
-    
-////////// Salt Functions ////////////////
 
-    /**
-     * Store the salt
-     * 
-     * @return String version of salt
-     */
-    public String fetchSalt() {
-       String salt="";
+////////// Single value accessor Functions ////////////////
+
+    private String fetchSV(String table, String key) {
+       String res="";
         try {
-                       Cursor c = db.query(true, TABLE_SALT, new String[] {"salt"},
+                       Cursor c = db.query(true, table, new String[] {key},
                                null, null, null, null, null,null);
                        if(c.getCount() > 0) {
                            c.moveToFirst();
-                           salt=c.getString(0);
+                           res=c.getString(0);
                        }
                        c.close();
                } catch (SQLException e)
                {
                        Log.d(TAG,"SQLite exception: " + e.getLocalizedMessage());
                }
-               return salt;
+               return res;
     }
     
-    /**
-     * Store the salt into the database.
-     * 
-     * @param salt String version of the salt
-     */
-    public void storeSalt(String salt) {
+    public void storeSV(String table, String key, String value) {
                ContentValues args = new ContentValues();
         try {
-                       db.delete(TABLE_SALT, "1=1", null);
-                       args.put("salt", salt);
-                       db.insert(TABLE_SALT, null, args);
+                       db.delete(table, "1=1", null);
+                       args.put(key, value);
+                       db.insert(table, null, args);
                } catch (SQLException e)
                {
                        Log.d(TAG,"SQLite exception: " + e.getLocalizedMessage());
                }
     }
     
+////////// Salt Functions ////////////////
+
+    /**
+     * Store the salt
+     * 
+     * @return String version of salt
+     */
+    public String fetchSalt() {
+               return fetchSV(TABLE_SALT, "salt");
+    }
+    
+    /**
+     * Store the salt into the database.
+     * 
+     * @param salt String version of the salt
+     */
+    public void storeSalt(String salt) {
+               storeSV(TABLE_SALT, "salt", salt);
+    }
+    
 ////////// Master Key Functions ////////////////
 
     /**
@@ -279,20 +289,7 @@ public class DBHelper {
      * @return
      */
     public String fetchMasterKey() {
-       String key="";
-        try {
-                       Cursor c = db.query(true, TABLE_MASTER_KEY, new String[] {"encryptedkey"},
-                               null, null, null, null, null,null);
-                       if(c.getCount() > 0) {
-                           c.moveToFirst();
-                           key=c.getString(0);
-                       }
-                       c.close();
-               } catch (SQLException e)
-               {
-                       Log.d(TAG,"SQLite exception: " + e.getLocalizedMessage());
-               }
-               return key;
+               return fetchSV(TABLE_MASTER_KEY, "encryptedkey");
     }
     
     /**
@@ -300,17 +297,10 @@ public class DBHelper {
      * @param PBEKey
      */
     public void storeMasterKey(String MasterKey) {
-               ContentValues args = new ContentValues();
-        try {
-                       db.delete(TABLE_MASTER_KEY, "1=1", null);
-                       args.put("encryptedkey", MasterKey);
-                       db.insert(TABLE_MASTER_KEY, null, args);
-               } catch (SQLException e)
-               {
-                       Log.d(TAG,"SQLite exception: " + e.getLocalizedMessage());
-               }
+               storeSV(TABLE_MASTER_KEY, "encryptedkey", MasterKey);
     }
 
+
 //////////Category Functions ////////////////
 
     /**