aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThialfihar <thialfihar@gmail.com>2010-05-27 01:40:50 +0000
committerThialfihar <thialfihar@gmail.com>2010-05-27 01:40:50 +0000
commitedd9a22882c02a1278af137b3e0b234b163e2b9e (patch)
tree775b7cb04f2775a2452adca94ca63cd225434d55
parent5fb8cb4d5efab46a7019d2766102a9fca5f73d47 (diff)
downloadopen-keychain-edd9a22882c02a1278af137b3e0b234b163e2b9e.tar.gz
open-keychain-edd9a22882c02a1278af137b3e0b234b163e2b9e.tar.bz2
open-keychain-edd9a22882c02a1278af137b3e0b234b163e2b9e.zip
some (hopefully better) database/cursor handling, picking encryption and signature keys working again, tho it'll have to be tidied up
-rw-r--r--res/layout/select_secret_key_item.xml4
-rw-r--r--src/org/thialfihar/android/apg/Apg.java10
-rw-r--r--src/org/thialfihar/android/apg/KeyListActivity.java33
-rw-r--r--src/org/thialfihar/android/apg/SelectPublicKeyListActivity.java4
-rw-r--r--src/org/thialfihar/android/apg/SelectPublicKeyListAdapter.java9
-rw-r--r--src/org/thialfihar/android/apg/SelectSecretKeyListActivity.java163
-rw-r--r--src/org/thialfihar/android/apg/SelectSecretKeyListAdapter.java163
-rw-r--r--src/org/thialfihar/android/apg/provider/Database.java195
8 files changed, 236 insertions, 345 deletions
diff --git a/res/layout/select_secret_key_item.xml b/res/layout/select_secret_key_item.xml
index 35bf58e23..257128cdb 100644
--- a/res/layout/select_secret_key_item.xml
+++ b/res/layout/select_secret_key_item.xml
@@ -62,7 +62,7 @@
android:layout_width="wrap_content"
android:layout_height="fill_parent"/>
- <TextView
+ <!-- <TextView
android:id="@+id/creation"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="31.12.2009"
@@ -74,7 +74,7 @@
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="31.12.2010"
android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
+ android:layout_height="wrap_content"/> -->
<TextView
android:id="@+id/status"
diff --git a/src/org/thialfihar/android/apg/Apg.java b/src/org/thialfihar/android/apg/Apg.java
index fbb931dbb..7ce45279a 100644
--- a/src/org/thialfihar/android/apg/Apg.java
+++ b/src/org/thialfihar/android/apg/Apg.java
@@ -82,7 +82,6 @@ import org.bouncycastle2.openpgp.PGPUtil;
import org.thialfihar.android.apg.provider.Database;
import org.thialfihar.android.apg.provider.KeyRings;
import org.thialfihar.android.apg.provider.PublicKeys;
-import org.thialfihar.android.apg.provider.SecretKeys;
import org.thialfihar.android.apg.ui.widget.KeyEditor;
import org.thialfihar.android.apg.ui.widget.SectionView;
import org.thialfihar.android.apg.ui.widget.UserIdEditor;
@@ -176,6 +175,10 @@ public class Apg {
}
}
+ public static Database getDatabase() {
+ return mDatabase;
+ }
+
public static void setEditPassPhrase(String passPhrase) {
mEditPassPhrase = passPhrase;
}
@@ -1034,7 +1037,7 @@ public class Apg {
}
public static Vector<Integer> getKeyRingIds(int type) {
- SQLiteDatabase db = mDatabase.getReadableDatabase();
+ SQLiteDatabase db = mDatabase.db();
Vector<Integer> keyIds = new Vector<Integer>();
Cursor c = db.query(KeyRings.TABLE_NAME,
new String[] { KeyRings._ID },
@@ -1045,7 +1048,8 @@ public class Apg {
keyIds.add(c.getInt(0));
} while (c.moveToNext());
}
- db.close();
+ c.close();
+
return keyIds;
}
diff --git a/src/org/thialfihar/android/apg/KeyListActivity.java b/src/org/thialfihar/android/apg/KeyListActivity.java
index 78d5b6f8e..4d9e1ba12 100644
--- a/src/org/thialfihar/android/apg/KeyListActivity.java
+++ b/src/org/thialfihar/android/apg/KeyListActivity.java
@@ -23,7 +23,6 @@ import java.util.Vector;
import org.bouncycastle2.openpgp.PGPException;
import org.bouncycastle2.openpgp.PGPPublicKeyRing;
import org.bouncycastle2.openpgp.PGPSecretKeyRing;
-import org.thialfihar.android.apg.provider.Database;
import org.thialfihar.android.apg.provider.KeyRings;
import org.thialfihar.android.apg.provider.Keys;
import org.thialfihar.android.apg.provider.UserIds;
@@ -51,6 +50,7 @@ import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
public class KeyListActivity extends BaseActivity {
protected ExpandableListView mList;
+ protected KeyListAdapter mListAdapter;
protected int mSelectedItem = -1;
protected int mTask = 0;
@@ -66,7 +66,8 @@ public class KeyListActivity extends BaseActivity {
setContentView(R.layout.key_list);
mList = (ExpandableListView) findViewById(R.id.list);
- mList.setAdapter(new KeyListAdapter(this));
+ mListAdapter = new KeyListAdapter(this);
+ mList.setAdapter(mListAdapter);
registerForContextMenu(mList);
}
@@ -124,7 +125,7 @@ public class KeyListActivity extends BaseActivity {
switch (id) {
case Id.dialog.delete_key: {
- final int keyRingId = ((KeyListAdapter) mList.getExpandableListAdapter()).getKeyRingId(mSelectedItem);
+ final int keyRingId = mListAdapter.getKeyRingId(mSelectedItem);
mSelectedItem = -1;
// TODO: better way to do this?
String userId = "<unknown>";
@@ -258,7 +259,7 @@ public class KeyListActivity extends BaseActivity {
Id.database.type_public :
Id.database.type_secret);
} else {
- int keyRingId = ((KeyListAdapter) mList.getExpandableListAdapter()).getKeyRingId(mSelectedItem);
+ int keyRingId = mListAdapter.getKeyRingId(mSelectedItem);
keyRingIds.add(keyRingId);
mSelectedItem = -1;
}
@@ -294,8 +295,8 @@ public class KeyListActivity extends BaseActivity {
}
protected void refreshList() {
- ((KeyListAdapter) mList.getExpandableListAdapter()).rebuild(true);
- ((KeyListAdapter) mList.getExpandableListAdapter()).notifyDataSetChanged();
+ mListAdapter.rebuild(true);
+ mListAdapter.notifyDataSetChanged();
}
@Override
@@ -402,7 +403,7 @@ public class KeyListActivity extends BaseActivity {
public KeyListAdapter(Context context) {
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- mDatabase = new Database(context).getReadableDatabase();
+ mDatabase = Apg.getDatabase().db();
mCursor = mDatabase.query(
KeyRings.TABLE_NAME + " INNER JOIN " + Keys.TABLE_NAME + " ON " +
"(" + KeyRings.TABLE_NAME + "." + KeyRings._ID + " = " +
@@ -420,13 +421,18 @@ public class KeyListActivity extends BaseActivity {
},
KeyRings.TABLE_NAME + "." + KeyRings.TYPE + " = ?",
new String[] { "" + (mKeyType == Id.type.public_key ?
- Id.database.type_public :
- Id.database.type_secret) },
+ Id.database.type_public : Id.database.type_secret) },
null, null, UserIds.TABLE_NAME + "." + UserIds.USER_ID + " ASC");
rebuild(false);
}
+ @Override
+ protected void finalize() throws Throwable {
+ mCursor.deactivate();
+ super.finalize();
+ }
+
public void rebuild(boolean requery) {
if (requery) {
mCursor.requery();
@@ -437,13 +443,6 @@ public class KeyListActivity extends BaseActivity {
}
}
- @Override
- protected void finalize() throws Throwable {
- mCursor.close();
- mDatabase.close();
- super.finalize();
- }
-
protected Vector<KeyChild> getChildrenOfGroup(int groupPosition) {
Vector<KeyChild> children = mChildren.get(groupPosition);
if (children != null) {
@@ -497,8 +496,6 @@ public class KeyListActivity extends BaseActivity {
return children;
}
-
-
@Override
public boolean hasStableIds() {
return true;
diff --git a/src/org/thialfihar/android/apg/SelectPublicKeyListActivity.java b/src/org/thialfihar/android/apg/SelectPublicKeyListActivity.java
index 24cba08dc..655003095 100644
--- a/src/org/thialfihar/android/apg/SelectPublicKeyListActivity.java
+++ b/src/org/thialfihar/android/apg/SelectPublicKeyListActivity.java
@@ -16,12 +16,8 @@
package org.thialfihar.android.apg;
-import java.util.Collections;
import java.util.Vector;
-import org.bouncycastle2.openpgp.PGPPublicKey;
-import org.bouncycastle2.openpgp.PGPPublicKeyRing;
-
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
diff --git a/src/org/thialfihar/android/apg/SelectPublicKeyListAdapter.java b/src/org/thialfihar/android/apg/SelectPublicKeyListAdapter.java
index ab07f989f..483ab0caa 100644
--- a/src/org/thialfihar/android/apg/SelectPublicKeyListAdapter.java
+++ b/src/org/thialfihar/android/apg/SelectPublicKeyListAdapter.java
@@ -16,7 +16,6 @@
package org.thialfihar.android.apg;
-import org.thialfihar.android.apg.provider.Database;
import org.thialfihar.android.apg.provider.KeyRings;
import org.thialfihar.android.apg.provider.Keys;
import org.thialfihar.android.apg.provider.UserIds;
@@ -40,7 +39,7 @@ public class SelectPublicKeyListAdapter extends BaseAdapter {
public SelectPublicKeyListAdapter(ListView parent) {
mParent = parent;
- mDatabase = new Database(parent.getContext()).getReadableDatabase();
+ mDatabase = Apg.getDatabase().db();
mInflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mCursor = mDatabase.query(
KeyRings.TABLE_NAME + " INNER JOIN " + Keys.TABLE_NAME + " ON " +
@@ -68,8 +67,8 @@ public class SelectPublicKeyListAdapter extends BaseAdapter {
@Override
protected void finalize() throws Throwable {
+ // TODO: this doesn't seem to work...
mCursor.close();
- mDatabase.close();
super.finalize();
}
@@ -176,8 +175,8 @@ public class SelectPublicKeyListAdapter extends BaseAdapter {
mainUserId.setEnabled(enabled);
mainUserIdRest.setEnabled(enabled);
keyId.setEnabled(enabled);
- //creation.setEnabled(enabled);
- //expiry.setEnabled(enabled);
+ /*creation.setEnabled(enabled);
+ expiry.setEnabled(enabled);*/
selected.setEnabled(enabled);
status.setEnabled(enabled);
diff --git a/src/org/thialfihar/android/apg/SelectSecretKeyListActivity.java b/src/org/thialfihar/android/apg/SelectSecretKeyListActivity.java
index 53a5bd5a9..8c17c7724 100644
--- a/src/org/thialfihar/android/apg/SelectSecretKeyListActivity.java
+++ b/src/org/thialfihar/android/apg/SelectSecretKeyListActivity.java
@@ -16,32 +16,16 @@
package org.thialfihar.android.apg;
-import java.text.DateFormat;
-import java.util.Collections;
-import java.util.Date;
-import java.util.Vector;
-
-import org.bouncycastle2.openpgp.PGPSecretKey;
-import org.bouncycastle2.openpgp.PGPSecretKeyRing;
-import org.thialfihar.android.apg.utils.IterableIterator;
-
-import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
-import android.view.LayoutInflater;
import android.view.View;
-import android.view.ViewGroup;
import android.widget.AdapterView;
-import android.widget.BaseAdapter;
import android.widget.ListView;
-import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
public class SelectSecretKeyListActivity extends BaseActivity {
- protected Vector<PGPSecretKeyRing> mKeyRings;
- protected LayoutInflater mInflater;
- protected Intent mIntent;
protected ListView mList;
+ protected SelectSecretKeyListAdapter mListAdapter;
protected long mSelectedKeyId = 0;
@@ -49,19 +33,11 @@ public class SelectSecretKeyListActivity extends BaseActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
-
- // fill things
- mIntent = getIntent();
-
- mKeyRings = new Vector<PGPSecretKeyRing>();
- //(Vector<PGPSecretKeyRing>) Apg.getSecretKeyRings().clone();
- //Collections.sort(mKeyRings, new Apg.SecretKeySorter());
-
setContentView(R.layout.select_secret_key);
mList = (ListView) findViewById(R.id.list);
- mList.setAdapter(new SecretKeyListAdapter(this));
+ mListAdapter = new SelectSecretKeyListAdapter(mList);
+ mList.setAdapter(mListAdapter);
mList.setOnItemClickListener(new OnItemClickListener() {
@Override
@@ -73,135 +49,4 @@ public class SelectSecretKeyListActivity extends BaseActivity {
}
});
}
-
- private class SecretKeyListAdapter extends BaseAdapter {
-
- public SecretKeyListAdapter(Context context) {
- }
-
- @Override
- public boolean isEnabled(int position) {
- PGPSecretKeyRing keyRing = mKeyRings.get(position);
-
- if (Apg.getMasterKey(keyRing) == null) {
- return false;
- }
-
- Vector<PGPSecretKey> usableKeys = Apg.getUsableSigningKeys(keyRing);
- if (usableKeys.size() == 0) {
- return false;
- }
-
- return true;
- }
-
- @Override
- public boolean hasStableIds() {
- return true;
- }
-
- @Override
- public int getCount() {
- return mKeyRings.size();
- }
-
- @Override
- public Object getItem(int position) {
- return mKeyRings.get(position);
- }
-
- @Override
- public long getItemId(int position) {
- PGPSecretKeyRing keyRing = mKeyRings.get(position);
- PGPSecretKey key = Apg.getMasterKey(keyRing);
- if (key != null) {
- return key.getKeyID();
- }
-
- return 0;
- }
-
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- View view = mInflater.inflate(R.layout.select_secret_key_item, null);
- boolean enabled = isEnabled(position);
-
- PGPSecretKeyRing keyRing = mKeyRings.get(position);
- PGPSecretKey key = null;
- for (PGPSecretKey tKey : new IterableIterator<PGPSecretKey>(keyRing.getSecretKeys())) {
- if (tKey.isMasterKey()) {
- key = tKey;
- break;
- }
- }
-
- TextView mainUserId = (TextView) view.findViewById(R.id.mainUserId);
- mainUserId.setText(R.string.unknownUserId);
- TextView mainUserIdRest = (TextView) view.findViewById(R.id.mainUserIdRest);
- mainUserIdRest.setText("");
- TextView keyId = (TextView) view.findViewById(R.id.keyId);
- keyId.setText(R.string.noKey);
- TextView creation = (TextView) view.findViewById(R.id.creation);
- creation.setText(R.string.noDate);
- TextView expiry = (TextView) view.findViewById(R.id.expiry);
- expiry.setText(R.string.noExpiry);
- TextView status = (TextView) view.findViewById(R.id.status);
- status.setText(R.string.unknownStatus);
-
- if (key != null) {
- String userId = Apg.getMainUserId(key);
- if (userId != null) {
- String chunks[] = userId.split(" <", 2);
- userId = chunks[0];
- if (chunks.length > 1) {
- mainUserIdRest.setText("<" + chunks[1]);
- }
- mainUserId.setText(userId);
- }
-
- keyId.setText("" + Long.toHexString(key.getKeyID() & 0xffffffffL));
- }
-
- if (mainUserIdRest.getText().length() == 0) {
- mainUserIdRest.setVisibility(View.GONE);
- }
-
- Vector<PGPSecretKey> signingKeys = Apg.getSigningKeys(keyRing);
- Vector<PGPSecretKey> usableKeys = Apg.getUsableSigningKeys(keyRing);
-
- PGPSecretKey timespanKey = key;
- if (usableKeys.size() > 0) {
- timespanKey = usableKeys.get(0);
- status.setText(R.string.canSign);
- } else if (signingKeys.size() > 0) {
- timespanKey = signingKeys.get(0);
- Date now = new Date();
- if (now.compareTo(Apg.getCreationDate(timespanKey)) > 0) {
- status.setText(R.string.notValid);
- } else {
- status.setText(R.string.expired);
- }
- } else {
- status.setText(R.string.noKey);
- }
-
- creation.setText(DateFormat.getDateInstance().format(Apg.getCreationDate(timespanKey)));
- Date expiryDate = Apg.getExpiryDate(timespanKey);
- if (expiryDate != null) {
- expiry.setText(DateFormat.getDateInstance().format(expiryDate));
- }
-
- status.setText(status.getText() + " ");
-
- view.setEnabled(enabled);
- mainUserId.setEnabled(enabled);
- mainUserIdRest.setEnabled(enabled);
- keyId.setEnabled(enabled);
- creation.setEnabled(enabled);
- expiry.setEnabled(enabled);
- status.setEnabled(enabled);
-
- return view;
- }
- }
-} \ No newline at end of file
+}
diff --git a/src/org/thialfihar/android/apg/SelectSecretKeyListAdapter.java b/src/org/thialfihar/android/apg/SelectSecretKeyListAdapter.java
new file mode 100644
index 000000000..2d7bdb623
--- /dev/null
+++ b/src/org/thialfihar/android/apg/SelectSecretKeyListAdapter.java
@@ -0,0 +1,163 @@
+package org.thialfihar.android.apg;
+
+import org.thialfihar.android.apg.provider.KeyRings;
+import org.thialfihar.android.apg.provider.Keys;
+import org.thialfihar.android.apg.provider.UserIds;
+
+import android.content.Context;
+import android.database.Cursor;
+import android.database.sqlite.SQLiteDatabase;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.BaseAdapter;
+import android.widget.ListView;
+import android.widget.TextView;
+
+public class SelectSecretKeyListAdapter extends BaseAdapter {
+ protected LayoutInflater mInflater;
+ protected ListView mParent;
+ protected SQLiteDatabase mDatabase;
+ protected Cursor mCursor;
+
+ public SelectSecretKeyListAdapter(ListView parent) {
+ mParent = parent;
+ mDatabase = Apg.getDatabase().db();
+ mInflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+ mCursor = mDatabase.query(
+ KeyRings.TABLE_NAME + " INNER JOIN " + Keys.TABLE_NAME + " ON " +
+ "(" + KeyRings.TABLE_NAME + "." + KeyRings._ID + " = " +
+ Keys.TABLE_NAME + "." + Keys.KEY_RING_ID + " AND " +
+ Keys.TABLE_NAME + "." + Keys.IS_MASTER_KEY + " = '1'" +
+ ") " +
+ " INNER JOIN " + UserIds.TABLE_NAME + " ON " +
+ "(" + Keys.TABLE_NAME + "." + Keys._ID + " = " +
+ UserIds.TABLE_NAME + "." + UserIds.KEY_ID + " AND " +
+ UserIds.TABLE_NAME + "." + UserIds.RANK + " = '0') ",
+ new String[] {
+ KeyRings.TABLE_NAME + "." + KeyRings._ID, // 0
+ KeyRings.TABLE_NAME + "." + KeyRings.MASTER_KEY_ID, // 1
+ UserIds.TABLE_NAME + "." + UserIds.USER_ID, // 2
+ "(SELECT COUNT(tmp." + Keys._ID + ") FROM " + Keys.TABLE_NAME + " AS tmp WHERE " +
+ "tmp." + Keys.KEY_RING_ID + " = " +
+ KeyRings.TABLE_NAME + "." + KeyRings._ID + " AND " +
+ "tmp." + Keys.CAN_SIGN + " = '1')", // 3
+ },
+ KeyRings.TABLE_NAME + "." + KeyRings.TYPE + " = ?",
+ new String[] { "" + Id.database.type_secret },
+ null, null, UserIds.TABLE_NAME + "." + UserIds.USER_ID + " ASC");
+ }
+
+ @Override
+ protected void finalize() throws Throwable {
+ // TODO: this doesn't seem to work...
+ mCursor.close();
+ super.finalize();
+ }
+
+ @Override
+ public boolean isEnabled(int position) {
+ mCursor.moveToPosition(position);
+ return mCursor.getInt(3) > 0; // CAN_SIGN
+ }
+
+ @Override
+ public boolean hasStableIds() {
+ return true;
+ }
+
+ @Override
+ public int getCount() {
+ return mCursor.getCount();
+ }
+
+ @Override
+ public Object getItem(int position) {
+ return position;
+ }
+
+ @Override
+ public long getItemId(int position) {
+ mCursor.moveToPosition(position);
+ return mCursor.getLong(1); // MASTER_KEY_ID
+ }
+
+ @Override
+ public View getView(int position, View convertView, ViewGroup parent) {
+ mCursor.moveToPosition(position);
+
+ View view = mInflater.inflate(R.layout.select_secret_key_item, null);
+ boolean enabled = isEnabled(position);
+
+ TextView mainUserId = (TextView) view.findViewById(R.id.mainUserId);
+ mainUserId.setText(R.string.unknownUserId);
+ TextView mainUserIdRest = (TextView) view.findViewById(R.id.mainUserIdRest);
+ mainUserIdRest.setText("");
+ TextView keyId = (TextView) view.findViewById(R.id.keyId);
+ keyId.setText(R.string.noKey);
+ /*TextView creation = (TextView) view.findViewById(R.id.creation);
+ creation.setText(R.string.noDate);
+ TextView expiry = (TextView) view.findViewById(R.id.expiry);
+ expiry.setText(R.string.noExpiry);*/
+ TextView status = (TextView) view.findViewById(R.id.status);
+ status.setText(R.string.unknownStatus);
+
+ String userId = mCursor.getString(2); // USER_ID
+ if (userId != null) {
+ String chunks[] = userId.split(" <", 2);
+ userId = chunks[0];
+ if (chunks.length > 1) {
+ mainUserIdRest.setText("<" + chunks[1]);
+ }
+ mainUserId.setText(userId);
+ }
+
+ long masterKeyId = mCursor.getLong(1); // MASTER_KEY_ID
+ keyId.setText("" + Long.toHexString(masterKeyId & 0xffffffffL));
+
+ if (mainUserIdRest.getText().length() == 0) {
+ mainUserIdRest.setVisibility(View.GONE);
+ }
+
+ // TODO: must get this functionality in again
+ /*PGPSecretKey timespanKey = key;
+ if (usableKeys.size() > 0) {
+ timespanKey = usableKeys.get(0);
+ status.setText(R.string.canSign);
+ } else if (signingKeys.size() > 0) {
+ timespanKey = signingKeys.get(0);
+ Date now = new Date();
+ if (now.compareTo(Apg.getCreationDate(timespanKey)) > 0) {
+ status.setText(R.string.notValid);
+ } else {
+ status.setText(R.string.expired);
+ }
+ } else {
+ status.setText(R.string.noKey);
+ }*/
+
+ if (enabled) {
+ status.setText(R.string.canSign);
+ } else {
+ status.setText(R.string.noKey);
+ }
+
+ /*creation.setText(DateFormat.getDateInstance().format(Apg.getCreationDate(timespanKey)));
+ Date expiryDate = Apg.getExpiryDate(timespanKey);
+ if (expiryDate != null) {
+ expiry.setText(DateFormat.getDateInstance().format(expiryDate));
+ }*/
+
+ status.setText(status.getText() + " ");
+
+ view.setEnabled(enabled);
+ mainUserId.setEnabled(enabled);
+ mainUserIdRest.setEnabled(enabled);
+ keyId.setEnabled(enabled);
+ /*creation.setEnabled(enabled);
+ expiry.setEnabled(enabled);*/
+ status.setEnabled(enabled);
+
+ return view;
+ }
+} \ No newline at end of file
diff --git a/src/org/thialfihar/android/apg/provider/Database.java b/src/org/thialfihar/android/apg/provider/Database.java
index de76513f9..c4313d9be 100644
--- a/src/org/thialfihar/android/apg/provider/Database.java
+++ b/src/org/thialfihar/android/apg/provider/Database.java
@@ -18,7 +18,6 @@ import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
-import android.database.sqlite.SQLiteQueryBuilder;
import android.util.Log;
public class Database extends SQLiteOpenHelper {
@@ -39,7 +38,7 @@ public class Database extends SQLiteOpenHelper {
public static HashMap<String, String> sKeysProjection;
public static HashMap<String, String> sUserIdsProjection;
- private SQLiteDatabase mCurrentDb = null;
+ private SQLiteDatabase mDb = null;
private int mStatus = 0;
static {
@@ -71,12 +70,18 @@ public class Database extends SQLiteOpenHelper {
public Database(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
- //getWritableDatabase();
+ mDb = getWritableDatabase();
// force upgrade to test things
//onUpgrade(getWritableDatabase(), 1, 2);
}
@Override
+ protected void finalize() throws Throwable {
+ mDb.close();
+ super.finalize();
+ }
+
+ @Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + KeyRings.TABLE_NAME + " (" +
KeyRings._ID + " " + KeyRings._ID_type + "," +
@@ -111,7 +116,7 @@ public class Database extends SQLiteOpenHelper {
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- mCurrentDb = db;
+ mDb = db;
for (int version = oldVersion; version < newVersion; ++version) {
switch (version) {
case 1: { // upgrade 1 to 2
@@ -195,16 +200,11 @@ public class Database extends SQLiteOpenHelper {
}
}
}
- mCurrentDb = null;
+ mDb = null;
}
public int saveKeyRing(PGPPublicKeyRing keyRing) throws IOException, GeneralException {
- boolean grabbedNewDatabase = false;
- if (mCurrentDb == null) {
- mCurrentDb = getWritableDatabase();
- grabbedNewDatabase = true;
- }
-
+ mDb.beginTransaction();
ContentValues values = new ContentValues();
PGPPublicKey masterKey = keyRing.getPublicKey();
long masterKeyId = masterKey.getKeyID();
@@ -234,26 +234,17 @@ public class Database extends SQLiteOpenHelper {
}
seenIdsStr += id;
}
- mCurrentDb.delete(Keys.TABLE_NAME,
+ mDb.delete(Keys.TABLE_NAME,
Keys.KEY_RING_ID + " = ? AND " +
Keys._ID + " NOT IN (" + seenIdsStr + ")",
new String[] { "" + rowId });
- if (grabbedNewDatabase) {
- mCurrentDb.close();
- mCurrentDb = null;
- }
-
+ mDb.endTransaction();
return returnValue;
}
public int saveKeyRing(PGPSecretKeyRing keyRing) throws IOException, GeneralException {
- boolean grabbedNewDatabase = false;
- if (mCurrentDb == null) {
- mCurrentDb = getWritableDatabase();
- grabbedNewDatabase = true;
- }
-
+ mDb.beginTransaction();
ContentValues values = new ContentValues();
PGPSecretKey masterKey = keyRing.getSecretKey();
long masterKeyId = masterKey.getKeyID();
@@ -283,27 +274,17 @@ public class Database extends SQLiteOpenHelper {
}
seenIdsStr += id;
}
- mCurrentDb.delete(Keys.TABLE_NAME,
+ mDb.delete(Keys.TABLE_NAME,
Keys.KEY_RING_ID + " = ? AND " +
Keys._ID + " NOT IN (" + seenIdsStr + ")",
new String[] { "" + rowId });
- if (grabbedNewDatabase) {
- mCurrentDb.close();
- mCurrentDb = null;
- }
-
+ mDb.endTransaction();
return returnValue;
}
private int saveKey(long keyRingId, PGPPublicKey key, int rank)
throws IOException, GeneralException {
- boolean grabbedNewDatabase = false;
- if (mCurrentDb == null) {
- mCurrentDb = getWritableDatabase();
- grabbedNewDatabase = true;
- }
-
ContentValues values = new ContentValues();
values.put(Keys.KEY_ID, key.getKeyID());
@@ -337,27 +318,16 @@ public class Database extends SQLiteOpenHelper {
}
seenIdsStr += id;
}
- mCurrentDb.delete(UserIds.TABLE_NAME,
+ mDb.delete(UserIds.TABLE_NAME,
UserIds.KEY_ID + " = ? AND " +
UserIds._ID + " NOT IN (" + seenIdsStr + ")",
new String[] { "" + rowId });
- if (grabbedNewDatabase) {
- mCurrentDb.close();
- mCurrentDb = null;
- }
-
return (int)rowId;
}
private int saveKey(long keyRingId, PGPSecretKey key, int rank)
throws IOException, GeneralException {
- boolean grabbedNewDatabase = false;
- if (mCurrentDb == null) {
- mCurrentDb = getWritableDatabase();
- grabbedNewDatabase = true;
- }
-
ContentValues values = new ContentValues();
values.put(Keys.KEY_ID, key.getPublicKey().getKeyID());
@@ -391,16 +361,11 @@ public class Database extends SQLiteOpenHelper {
}
seenIdsStr += id;
}
- mCurrentDb.delete(UserIds.TABLE_NAME,
+ mDb.delete(UserIds.TABLE_NAME,
UserIds.KEY_ID + " = ? AND " +
UserIds._ID + " NOT IN (" + seenIdsStr + ")",
new String[] { "" + rowId });
- if (grabbedNewDatabase) {
- mCurrentDb.close();
- mCurrentDb = null;
- }
-
return (int)rowId;
}
@@ -421,12 +386,7 @@ public class Database extends SQLiteOpenHelper {
}
private long insertOrUpdateKeyRing(ContentValues values) {
- boolean grabbedNewDatabase = false;
- if (mCurrentDb == null) {
- mCurrentDb = getWritableDatabase();
- grabbedNewDatabase = true;
- }
- Cursor c = mCurrentDb.query(KeyRings.TABLE_NAME, new String[] { KeyRings._ID },
+ Cursor c = mDb.query(KeyRings.TABLE_NAME, new String[] { KeyRings._ID },
KeyRings.MASTER_KEY_ID + " = ? AND " + KeyRings.TYPE + " = ?",
new String[] {
values.getAsString(KeyRings.MASTER_KEY_ID),
@@ -436,31 +396,20 @@ public class Database extends SQLiteOpenHelper {
long rowId = -1;
if (c != null && c.moveToFirst()) {
rowId = c.getLong(0);
- mCurrentDb.update(KeyRings.TABLE_NAME, values,
+ mDb.update(KeyRings.TABLE_NAME, values,
KeyRings._ID + " = ?", new String[] { "" + rowId });
mStatus = Id.return_value.updated;
} else {
- rowId = mCurrentDb.insert(KeyRings.TABLE_NAME, KeyRings.WHO_ID, values);
+ rowId = mDb.insert(KeyRings.TABLE_NAME, KeyRings.WHO_ID, values);
mStatus = Id.return_value.ok;
}
-
c.close();
- if (grabbedNewDatabase) {
- mCurrentDb.close();
- mCurrentDb = null;
- }
-
return rowId;
}
private long insertOrUpdateKey(ContentValues values) {
- boolean grabbedNewDatabase = false;
- if (mCurrentDb == null) {
- mCurrentDb = getWritableDatabase();
- grabbedNewDatabase = true;
- }
- Cursor c = mCurrentDb.query(Keys.TABLE_NAME, new String[] { Keys._ID },
+ Cursor c = mDb.query(Keys.TABLE_NAME, new String[] { Keys._ID },
Keys.KEY_ID + " = ? AND " + Keys.TYPE + " = ?",
new String[] {
values.getAsString(Keys.KEY_ID),
@@ -470,29 +419,18 @@ public class Database extends SQLiteOpenHelper {
long rowId = -1;
if (c != null && c.moveToFirst()) {
rowId = c.getLong(0);
- mCurrentDb.update(Keys.TABLE_NAME, values,
+ mDb.update(Keys.TABLE_NAME, values,
Keys._ID + " = ?", new String[] { "" + rowId });
} else {
- rowId = mCurrentDb.insert(Keys.TABLE_NAME, Keys.KEY_DATA, values);
+ rowId = mDb.insert(Keys.TABLE_NAME, Keys.KEY_DATA, values);
}
-
c.close();
- if (grabbedNewDatabase) {
- mCurrentDb.close();
- mCurrentDb = null;
- }
-
return rowId;
}
private long insertOrUpdateUserId(ContentValues values) {
- boolean grabbedNewDatabase = false;
- if (mCurrentDb == null) {
- mCurrentDb = getWritableDatabase();
- grabbedNewDatabase = true;
- }
- Cursor c = mCurrentDb.query(UserIds.TABLE_NAME, new String[] { UserIds._ID },
+ Cursor c = mDb.query(UserIds.TABLE_NAME, new String[] { UserIds._ID },
UserIds.KEY_ID + " = ? AND " + UserIds.USER_ID + " = ?",
new String[] {
values.getAsString(UserIds.KEY_ID),
@@ -502,29 +440,18 @@ public class Database extends SQLiteOpenHelper {
long rowId = -1;
if (c != null && c.moveToFirst()) {
rowId = c.getLong(0);
- mCurrentDb.update(UserIds.TABLE_NAME, values,
+ mDb.update(UserIds.TABLE_NAME, values,
UserIds._ID + " = ?", new String[] { "" + rowId });
} else {
- rowId = mCurrentDb.insert(UserIds.TABLE_NAME, UserIds.USER_ID, values);
+ rowId = mDb.insert(UserIds.TABLE_NAME, UserIds.USER_ID, values);
}
-
c.close();
- if (grabbedNewDatabase) {
- mCurrentDb.close();
- mCurrentDb = null;
- }
-
return rowId;
}
public Object getKeyRing(int keyRingId) {
- boolean grabbedNewDatabase = false;
- if (mCurrentDb == null) {
- mCurrentDb = getWritableDatabase();
- grabbedNewDatabase = true;
- }
- Cursor c = mCurrentDb.query(KeyRings.TABLE_NAME,
+ Cursor c = mDb.query(KeyRings.TABLE_NAME,
new String[] { KeyRings.KEY_RING_DATA, KeyRings.TYPE },
KeyRings._ID + " = ?",
new String[] {
@@ -549,24 +476,13 @@ public class Database extends SQLiteOpenHelper {
}
}
}
-
c.close();
- if (grabbedNewDatabase) {
- mCurrentDb.close();
- mCurrentDb = null;
- }
-
return keyRing;
}
public byte[] getKeyRingDataFromKeyId(int type, long keyId) {
- boolean grabbedNewDatabase = false;
- if (mCurrentDb == null) {
- mCurrentDb = getReadableDatabase();
- grabbedNewDatabase = true;
- }
- Cursor c = mCurrentDb.query(Keys.TABLE_NAME + " INNER JOIN " + KeyRings.TABLE_NAME + " ON (" +
+ Cursor c = mDb.query(Keys.TABLE_NAME + " INNER JOIN " + KeyRings.TABLE_NAME + " ON (" +
KeyRings.TABLE_NAME + "." + KeyRings._ID + " = " +
Keys.TABLE_NAME + "." + Keys.KEY_RING_ID + ")",
new String[] { KeyRings.TABLE_NAME + "." + KeyRings.KEY_RING_DATA },
@@ -582,24 +498,13 @@ public class Database extends SQLiteOpenHelper {
if (c != null && c.moveToFirst()) {
data = c.getBlob(0);
}
-
c.close();
- if (grabbedNewDatabase) {
- mCurrentDb.close();
- mCurrentDb = null;
- }
-
return data;
}
public byte[] getKeyDataFromKeyId(int type, long keyId) {
- boolean grabbedNewDatabase = false;
- if (mCurrentDb == null) {
- mCurrentDb = getReadableDatabase();
- grabbedNewDatabase = true;
- }
- Cursor c = mCurrentDb.query(Keys.TABLE_NAME, new String[] { Keys.KEY_DATA },
+ Cursor c = mDb.query(Keys.TABLE_NAME, new String[] { Keys.KEY_DATA },
Keys.KEY_ID + " = ? AND " + Keys.TYPE + " = ?",
new String[] {
"" + keyId,
@@ -610,27 +515,17 @@ public class Database extends SQLiteOpenHelper {
if (c != null && c.moveToFirst()) {
data = c.getBlob(0);
}
-
c.close();
- if (grabbedNewDatabase) {
- mCurrentDb.close();
- mCurrentDb = null;
- }
-
return data;
}
public void deleteKeyRing(int keyRingId) {
- boolean grabbedNewDatabase = false;
- if (mCurrentDb == null) {
- mCurrentDb = getWritableDatabase();
- grabbedNewDatabase = true;
- }
- mCurrentDb.delete(KeyRings.TABLE_NAME,
+ mDb.beginTransaction();
+ mDb.delete(KeyRings.TABLE_NAME,
KeyRings._ID + " = ?", new String[] { "" + keyRingId });
- Cursor c = mCurrentDb.query(Keys.TABLE_NAME, new String[] { Keys._ID },
+ Cursor c = mDb.query(Keys.TABLE_NAME, new String[] { Keys._ID },
Keys.KEY_RING_ID + " = ?",
new String[] {
"" + keyRingId,
@@ -642,28 +537,20 @@ public class Database extends SQLiteOpenHelper {
deleteKey(keyId);
} while (c.moveToNext());
}
+ c.close();
- if (grabbedNewDatabase) {
- mCurrentDb.close();
- mCurrentDb = null;
- }
+ mDb.endTransaction();
}
- public void deleteKey(int keyId) {
- boolean grabbedNewDatabase = false;
- if (mCurrentDb == null) {
- mCurrentDb = getWritableDatabase();
- grabbedNewDatabase = true;
- }
- mCurrentDb.delete(Keys.TABLE_NAME,
+ private void deleteKey(int keyId) {
+ mDb.delete(Keys.TABLE_NAME,
Keys._ID + " = ?", new String[] { "" + keyId });
- mCurrentDb.delete(UserIds.TABLE_NAME,
+ mDb.delete(UserIds.TABLE_NAME,
UserIds.KEY_ID + " = ?", new String[] { "" + keyId });
+ }
- if (grabbedNewDatabase) {
- mCurrentDb.close();
- mCurrentDb = null;
- }
+ public SQLiteDatabase db() {
+ return mDb;
}
}