some (hopefully better) database/cursor handling, picking encryption and signature keys working again, tho it'll have to be tidied up

This commit is contained in:
Thialfihar 2010-05-27 01:40:50 +00:00
parent 5fb8cb4d5e
commit edd9a22882
8 changed files with 236 additions and 345 deletions

View File

@ -62,7 +62,7 @@
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="fill_parent"/> android:layout_height="fill_parent"/>
<TextView <!-- <TextView
android:id="@+id/creation" android:id="@+id/creation"
android:textAppearance="?android:attr/textAppearanceSmall" android:textAppearance="?android:attr/textAppearanceSmall"
android:text="31.12.2009" android:text="31.12.2009"
@ -74,7 +74,7 @@
android:textAppearance="?android:attr/textAppearanceSmall" android:textAppearance="?android:attr/textAppearanceSmall"
android:text="31.12.2010" android:text="31.12.2010"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content"/> android:layout_height="wrap_content"/> -->
<TextView <TextView
android:id="@+id/status" android:id="@+id/status"

View File

@ -82,7 +82,6 @@ import org.bouncycastle2.openpgp.PGPUtil;
import org.thialfihar.android.apg.provider.Database; import org.thialfihar.android.apg.provider.Database;
import org.thialfihar.android.apg.provider.KeyRings; import org.thialfihar.android.apg.provider.KeyRings;
import org.thialfihar.android.apg.provider.PublicKeys; 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.KeyEditor;
import org.thialfihar.android.apg.ui.widget.SectionView; import org.thialfihar.android.apg.ui.widget.SectionView;
import org.thialfihar.android.apg.ui.widget.UserIdEditor; 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) { public static void setEditPassPhrase(String passPhrase) {
mEditPassPhrase = passPhrase; mEditPassPhrase = passPhrase;
} }
@ -1034,7 +1037,7 @@ public class Apg {
} }
public static Vector<Integer> getKeyRingIds(int type) { public static Vector<Integer> getKeyRingIds(int type) {
SQLiteDatabase db = mDatabase.getReadableDatabase(); SQLiteDatabase db = mDatabase.db();
Vector<Integer> keyIds = new Vector<Integer>(); Vector<Integer> keyIds = new Vector<Integer>();
Cursor c = db.query(KeyRings.TABLE_NAME, Cursor c = db.query(KeyRings.TABLE_NAME,
new String[] { KeyRings._ID }, new String[] { KeyRings._ID },
@ -1045,7 +1048,8 @@ public class Apg {
keyIds.add(c.getInt(0)); keyIds.add(c.getInt(0));
} while (c.moveToNext()); } while (c.moveToNext());
} }
db.close(); c.close();
return keyIds; return keyIds;
} }

View File

@ -23,7 +23,6 @@ import java.util.Vector;
import org.bouncycastle2.openpgp.PGPException; import org.bouncycastle2.openpgp.PGPException;
import org.bouncycastle2.openpgp.PGPPublicKeyRing; import org.bouncycastle2.openpgp.PGPPublicKeyRing;
import org.bouncycastle2.openpgp.PGPSecretKeyRing; 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.KeyRings;
import org.thialfihar.android.apg.provider.Keys; import org.thialfihar.android.apg.provider.Keys;
import org.thialfihar.android.apg.provider.UserIds; import org.thialfihar.android.apg.provider.UserIds;
@ -51,6 +50,7 @@ import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
public class KeyListActivity extends BaseActivity { public class KeyListActivity extends BaseActivity {
protected ExpandableListView mList; protected ExpandableListView mList;
protected KeyListAdapter mListAdapter;
protected int mSelectedItem = -1; protected int mSelectedItem = -1;
protected int mTask = 0; protected int mTask = 0;
@ -66,7 +66,8 @@ public class KeyListActivity extends BaseActivity {
setContentView(R.layout.key_list); setContentView(R.layout.key_list);
mList = (ExpandableListView) findViewById(R.id.list); mList = (ExpandableListView) findViewById(R.id.list);
mList.setAdapter(new KeyListAdapter(this)); mListAdapter = new KeyListAdapter(this);
mList.setAdapter(mListAdapter);
registerForContextMenu(mList); registerForContextMenu(mList);
} }
@ -124,7 +125,7 @@ public class KeyListActivity extends BaseActivity {
switch (id) { switch (id) {
case Id.dialog.delete_key: { case Id.dialog.delete_key: {
final int keyRingId = ((KeyListAdapter) mList.getExpandableListAdapter()).getKeyRingId(mSelectedItem); final int keyRingId = mListAdapter.getKeyRingId(mSelectedItem);
mSelectedItem = -1; mSelectedItem = -1;
// TODO: better way to do this? // TODO: better way to do this?
String userId = "<unknown>"; String userId = "<unknown>";
@ -258,7 +259,7 @@ public class KeyListActivity extends BaseActivity {
Id.database.type_public : Id.database.type_public :
Id.database.type_secret); Id.database.type_secret);
} else { } else {
int keyRingId = ((KeyListAdapter) mList.getExpandableListAdapter()).getKeyRingId(mSelectedItem); int keyRingId = mListAdapter.getKeyRingId(mSelectedItem);
keyRingIds.add(keyRingId); keyRingIds.add(keyRingId);
mSelectedItem = -1; mSelectedItem = -1;
} }
@ -294,8 +295,8 @@ public class KeyListActivity extends BaseActivity {
} }
protected void refreshList() { protected void refreshList() {
((KeyListAdapter) mList.getExpandableListAdapter()).rebuild(true); mListAdapter.rebuild(true);
((KeyListAdapter) mList.getExpandableListAdapter()).notifyDataSetChanged(); mListAdapter.notifyDataSetChanged();
} }
@Override @Override
@ -402,7 +403,7 @@ public class KeyListActivity extends BaseActivity {
public KeyListAdapter(Context context) { public KeyListAdapter(Context context) {
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mDatabase = new Database(context).getReadableDatabase(); mDatabase = Apg.getDatabase().db();
mCursor = mDatabase.query( mCursor = mDatabase.query(
KeyRings.TABLE_NAME + " INNER JOIN " + Keys.TABLE_NAME + " ON " + KeyRings.TABLE_NAME + " INNER JOIN " + Keys.TABLE_NAME + " ON " +
"(" + KeyRings.TABLE_NAME + "." + KeyRings._ID + " = " + "(" + KeyRings.TABLE_NAME + "." + KeyRings._ID + " = " +
@ -420,13 +421,18 @@ public class KeyListActivity extends BaseActivity {
}, },
KeyRings.TABLE_NAME + "." + KeyRings.TYPE + " = ?", KeyRings.TABLE_NAME + "." + KeyRings.TYPE + " = ?",
new String[] { "" + (mKeyType == Id.type.public_key ? new String[] { "" + (mKeyType == Id.type.public_key ?
Id.database.type_public : Id.database.type_public : Id.database.type_secret) },
Id.database.type_secret) },
null, null, UserIds.TABLE_NAME + "." + UserIds.USER_ID + " ASC"); null, null, UserIds.TABLE_NAME + "." + UserIds.USER_ID + " ASC");
rebuild(false); rebuild(false);
} }
@Override
protected void finalize() throws Throwable {
mCursor.deactivate();
super.finalize();
}
public void rebuild(boolean requery) { public void rebuild(boolean requery) {
if (requery) { if (requery) {
mCursor.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) { protected Vector<KeyChild> getChildrenOfGroup(int groupPosition) {
Vector<KeyChild> children = mChildren.get(groupPosition); Vector<KeyChild> children = mChildren.get(groupPosition);
if (children != null) { if (children != null) {
@ -497,8 +496,6 @@ public class KeyListActivity extends BaseActivity {
return children; return children;
} }
@Override @Override
public boolean hasStableIds() { public boolean hasStableIds() {
return true; return true;

View File

@ -16,12 +16,8 @@
package org.thialfihar.android.apg; package org.thialfihar.android.apg;
import java.util.Collections;
import java.util.Vector; import java.util.Vector;
import org.bouncycastle2.openpgp.PGPPublicKey;
import org.bouncycastle2.openpgp.PGPPublicKeyRing;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;

View File

@ -16,7 +16,6 @@
package org.thialfihar.android.apg; 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.KeyRings;
import org.thialfihar.android.apg.provider.Keys; import org.thialfihar.android.apg.provider.Keys;
import org.thialfihar.android.apg.provider.UserIds; import org.thialfihar.android.apg.provider.UserIds;
@ -40,7 +39,7 @@ public class SelectPublicKeyListAdapter extends BaseAdapter {
public SelectPublicKeyListAdapter(ListView parent) { public SelectPublicKeyListAdapter(ListView parent) {
mParent = parent; mParent = parent;
mDatabase = new Database(parent.getContext()).getReadableDatabase(); mDatabase = Apg.getDatabase().db();
mInflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); mInflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mCursor = mDatabase.query( mCursor = mDatabase.query(
KeyRings.TABLE_NAME + " INNER JOIN " + Keys.TABLE_NAME + " ON " + KeyRings.TABLE_NAME + " INNER JOIN " + Keys.TABLE_NAME + " ON " +
@ -68,8 +67,8 @@ public class SelectPublicKeyListAdapter extends BaseAdapter {
@Override @Override
protected void finalize() throws Throwable { protected void finalize() throws Throwable {
// TODO: this doesn't seem to work...
mCursor.close(); mCursor.close();
mDatabase.close();
super.finalize(); super.finalize();
} }
@ -176,8 +175,8 @@ public class SelectPublicKeyListAdapter extends BaseAdapter {
mainUserId.setEnabled(enabled); mainUserId.setEnabled(enabled);
mainUserIdRest.setEnabled(enabled); mainUserIdRest.setEnabled(enabled);
keyId.setEnabled(enabled); keyId.setEnabled(enabled);
//creation.setEnabled(enabled); /*creation.setEnabled(enabled);
//expiry.setEnabled(enabled); expiry.setEnabled(enabled);*/
selected.setEnabled(enabled); selected.setEnabled(enabled);
status.setEnabled(enabled); status.setEnabled(enabled);

View File

@ -16,32 +16,16 @@
package org.thialfihar.android.apg; 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.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView; import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemClickListener;
public class SelectSecretKeyListActivity extends BaseActivity { public class SelectSecretKeyListActivity extends BaseActivity {
protected Vector<PGPSecretKeyRing> mKeyRings;
protected LayoutInflater mInflater;
protected Intent mIntent;
protected ListView mList; protected ListView mList;
protected SelectSecretKeyListAdapter mListAdapter;
protected long mSelectedKeyId = 0; protected long mSelectedKeyId = 0;
@ -49,19 +33,11 @@ public class SelectSecretKeyListActivity extends BaseActivity {
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(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); setContentView(R.layout.select_secret_key);
mList = (ListView) findViewById(R.id.list); mList = (ListView) findViewById(R.id.list);
mList.setAdapter(new SecretKeyListAdapter(this)); mListAdapter = new SelectSecretKeyListAdapter(mList);
mList.setAdapter(mListAdapter);
mList.setOnItemClickListener(new OnItemClickListener() { mList.setOnItemClickListener(new OnItemClickListener() {
@Override @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;
}
}
}

View File

@ -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;
}
}

View File

@ -18,7 +18,6 @@ import android.content.Context;
import android.database.Cursor; import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.util.Log; import android.util.Log;
public class Database extends SQLiteOpenHelper { 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> sKeysProjection;
public static HashMap<String, String> sUserIdsProjection; public static HashMap<String, String> sUserIdsProjection;
private SQLiteDatabase mCurrentDb = null; private SQLiteDatabase mDb = null;
private int mStatus = 0; private int mStatus = 0;
static { static {
@ -71,11 +70,17 @@ public class Database extends SQLiteOpenHelper {
public Database(Context context) { public Database(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION); super(context, DATABASE_NAME, null, DATABASE_VERSION);
//getWritableDatabase(); mDb = getWritableDatabase();
// force upgrade to test things // force upgrade to test things
//onUpgrade(getWritableDatabase(), 1, 2); //onUpgrade(getWritableDatabase(), 1, 2);
} }
@Override
protected void finalize() throws Throwable {
mDb.close();
super.finalize();
}
@Override @Override
public void onCreate(SQLiteDatabase db) { public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + KeyRings.TABLE_NAME + " (" + db.execSQL("CREATE TABLE " + KeyRings.TABLE_NAME + " (" +
@ -111,7 +116,7 @@ public class Database extends SQLiteOpenHelper {
@Override @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
mCurrentDb = db; mDb = db;
for (int version = oldVersion; version < newVersion; ++version) { for (int version = oldVersion; version < newVersion; ++version) {
switch (version) { switch (version) {
case 1: { // upgrade 1 to 2 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 { public int saveKeyRing(PGPPublicKeyRing keyRing) throws IOException, GeneralException {
boolean grabbedNewDatabase = false; mDb.beginTransaction();
if (mCurrentDb == null) {
mCurrentDb = getWritableDatabase();
grabbedNewDatabase = true;
}
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
PGPPublicKey masterKey = keyRing.getPublicKey(); PGPPublicKey masterKey = keyRing.getPublicKey();
long masterKeyId = masterKey.getKeyID(); long masterKeyId = masterKey.getKeyID();
@ -234,26 +234,17 @@ public class Database extends SQLiteOpenHelper {
} }
seenIdsStr += id; seenIdsStr += id;
} }
mCurrentDb.delete(Keys.TABLE_NAME, mDb.delete(Keys.TABLE_NAME,
Keys.KEY_RING_ID + " = ? AND " + Keys.KEY_RING_ID + " = ? AND " +
Keys._ID + " NOT IN (" + seenIdsStr + ")", Keys._ID + " NOT IN (" + seenIdsStr + ")",
new String[] { "" + rowId }); new String[] { "" + rowId });
if (grabbedNewDatabase) { mDb.endTransaction();
mCurrentDb.close();
mCurrentDb = null;
}
return returnValue; return returnValue;
} }
public int saveKeyRing(PGPSecretKeyRing keyRing) throws IOException, GeneralException { public int saveKeyRing(PGPSecretKeyRing keyRing) throws IOException, GeneralException {
boolean grabbedNewDatabase = false; mDb.beginTransaction();
if (mCurrentDb == null) {
mCurrentDb = getWritableDatabase();
grabbedNewDatabase = true;
}
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
PGPSecretKey masterKey = keyRing.getSecretKey(); PGPSecretKey masterKey = keyRing.getSecretKey();
long masterKeyId = masterKey.getKeyID(); long masterKeyId = masterKey.getKeyID();
@ -283,27 +274,17 @@ public class Database extends SQLiteOpenHelper {
} }
seenIdsStr += id; seenIdsStr += id;
} }
mCurrentDb.delete(Keys.TABLE_NAME, mDb.delete(Keys.TABLE_NAME,
Keys.KEY_RING_ID + " = ? AND " + Keys.KEY_RING_ID + " = ? AND " +
Keys._ID + " NOT IN (" + seenIdsStr + ")", Keys._ID + " NOT IN (" + seenIdsStr + ")",
new String[] { "" + rowId }); new String[] { "" + rowId });
if (grabbedNewDatabase) { mDb.endTransaction();
mCurrentDb.close();
mCurrentDb = null;
}
return returnValue; return returnValue;
} }
private int saveKey(long keyRingId, PGPPublicKey key, int rank) private int saveKey(long keyRingId, PGPPublicKey key, int rank)
throws IOException, GeneralException { throws IOException, GeneralException {
boolean grabbedNewDatabase = false;
if (mCurrentDb == null) {
mCurrentDb = getWritableDatabase();
grabbedNewDatabase = true;
}
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put(Keys.KEY_ID, key.getKeyID()); values.put(Keys.KEY_ID, key.getKeyID());
@ -337,27 +318,16 @@ public class Database extends SQLiteOpenHelper {
} }
seenIdsStr += id; seenIdsStr += id;
} }
mCurrentDb.delete(UserIds.TABLE_NAME, mDb.delete(UserIds.TABLE_NAME,
UserIds.KEY_ID + " = ? AND " + UserIds.KEY_ID + " = ? AND " +
UserIds._ID + " NOT IN (" + seenIdsStr + ")", UserIds._ID + " NOT IN (" + seenIdsStr + ")",
new String[] { "" + rowId }); new String[] { "" + rowId });
if (grabbedNewDatabase) {
mCurrentDb.close();
mCurrentDb = null;
}
return (int)rowId; return (int)rowId;
} }
private int saveKey(long keyRingId, PGPSecretKey key, int rank) private int saveKey(long keyRingId, PGPSecretKey key, int rank)
throws IOException, GeneralException { throws IOException, GeneralException {
boolean grabbedNewDatabase = false;
if (mCurrentDb == null) {
mCurrentDb = getWritableDatabase();
grabbedNewDatabase = true;
}
ContentValues values = new ContentValues(); ContentValues values = new ContentValues();
values.put(Keys.KEY_ID, key.getPublicKey().getKeyID()); values.put(Keys.KEY_ID, key.getPublicKey().getKeyID());
@ -391,16 +361,11 @@ public class Database extends SQLiteOpenHelper {
} }
seenIdsStr += id; seenIdsStr += id;
} }
mCurrentDb.delete(UserIds.TABLE_NAME, mDb.delete(UserIds.TABLE_NAME,
UserIds.KEY_ID + " = ? AND " + UserIds.KEY_ID + " = ? AND " +
UserIds._ID + " NOT IN (" + seenIdsStr + ")", UserIds._ID + " NOT IN (" + seenIdsStr + ")",
new String[] { "" + rowId }); new String[] { "" + rowId });
if (grabbedNewDatabase) {
mCurrentDb.close();
mCurrentDb = null;
}
return (int)rowId; return (int)rowId;
} }
@ -421,12 +386,7 @@ public class Database extends SQLiteOpenHelper {
} }
private long insertOrUpdateKeyRing(ContentValues values) { private long insertOrUpdateKeyRing(ContentValues values) {
boolean grabbedNewDatabase = false; Cursor c = mDb.query(KeyRings.TABLE_NAME, new String[] { KeyRings._ID },
if (mCurrentDb == null) {
mCurrentDb = getWritableDatabase();
grabbedNewDatabase = true;
}
Cursor c = mCurrentDb.query(KeyRings.TABLE_NAME, new String[] { KeyRings._ID },
KeyRings.MASTER_KEY_ID + " = ? AND " + KeyRings.TYPE + " = ?", KeyRings.MASTER_KEY_ID + " = ? AND " + KeyRings.TYPE + " = ?",
new String[] { new String[] {
values.getAsString(KeyRings.MASTER_KEY_ID), values.getAsString(KeyRings.MASTER_KEY_ID),
@ -436,31 +396,20 @@ public class Database extends SQLiteOpenHelper {
long rowId = -1; long rowId = -1;
if (c != null && c.moveToFirst()) { if (c != null && c.moveToFirst()) {
rowId = c.getLong(0); rowId = c.getLong(0);
mCurrentDb.update(KeyRings.TABLE_NAME, values, mDb.update(KeyRings.TABLE_NAME, values,
KeyRings._ID + " = ?", new String[] { "" + rowId }); KeyRings._ID + " = ?", new String[] { "" + rowId });
mStatus = Id.return_value.updated; mStatus = Id.return_value.updated;
} else { } 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; mStatus = Id.return_value.ok;
} }
c.close(); c.close();
if (grabbedNewDatabase) {
mCurrentDb.close();
mCurrentDb = null;
}
return rowId; return rowId;
} }
private long insertOrUpdateKey(ContentValues values) { private long insertOrUpdateKey(ContentValues values) {
boolean grabbedNewDatabase = false; Cursor c = mDb.query(Keys.TABLE_NAME, new String[] { Keys._ID },
if (mCurrentDb == null) {
mCurrentDb = getWritableDatabase();
grabbedNewDatabase = true;
}
Cursor c = mCurrentDb.query(Keys.TABLE_NAME, new String[] { Keys._ID },
Keys.KEY_ID + " = ? AND " + Keys.TYPE + " = ?", Keys.KEY_ID + " = ? AND " + Keys.TYPE + " = ?",
new String[] { new String[] {
values.getAsString(Keys.KEY_ID), values.getAsString(Keys.KEY_ID),
@ -470,29 +419,18 @@ public class Database extends SQLiteOpenHelper {
long rowId = -1; long rowId = -1;
if (c != null && c.moveToFirst()) { if (c != null && c.moveToFirst()) {
rowId = c.getLong(0); rowId = c.getLong(0);
mCurrentDb.update(Keys.TABLE_NAME, values, mDb.update(Keys.TABLE_NAME, values,
Keys._ID + " = ?", new String[] { "" + rowId }); Keys._ID + " = ?", new String[] { "" + rowId });
} else { } else {
rowId = mCurrentDb.insert(Keys.TABLE_NAME, Keys.KEY_DATA, values); rowId = mDb.insert(Keys.TABLE_NAME, Keys.KEY_DATA, values);
} }
c.close(); c.close();
if (grabbedNewDatabase) {
mCurrentDb.close();
mCurrentDb = null;
}
return rowId; return rowId;
} }
private long insertOrUpdateUserId(ContentValues values) { private long insertOrUpdateUserId(ContentValues values) {
boolean grabbedNewDatabase = false; Cursor c = mDb.query(UserIds.TABLE_NAME, new String[] { UserIds._ID },
if (mCurrentDb == null) {
mCurrentDb = getWritableDatabase();
grabbedNewDatabase = true;
}
Cursor c = mCurrentDb.query(UserIds.TABLE_NAME, new String[] { UserIds._ID },
UserIds.KEY_ID + " = ? AND " + UserIds.USER_ID + " = ?", UserIds.KEY_ID + " = ? AND " + UserIds.USER_ID + " = ?",
new String[] { new String[] {
values.getAsString(UserIds.KEY_ID), values.getAsString(UserIds.KEY_ID),
@ -502,29 +440,18 @@ public class Database extends SQLiteOpenHelper {
long rowId = -1; long rowId = -1;
if (c != null && c.moveToFirst()) { if (c != null && c.moveToFirst()) {
rowId = c.getLong(0); rowId = c.getLong(0);
mCurrentDb.update(UserIds.TABLE_NAME, values, mDb.update(UserIds.TABLE_NAME, values,
UserIds._ID + " = ?", new String[] { "" + rowId }); UserIds._ID + " = ?", new String[] { "" + rowId });
} else { } else {
rowId = mCurrentDb.insert(UserIds.TABLE_NAME, UserIds.USER_ID, values); rowId = mDb.insert(UserIds.TABLE_NAME, UserIds.USER_ID, values);
} }
c.close(); c.close();
if (grabbedNewDatabase) {
mCurrentDb.close();
mCurrentDb = null;
}
return rowId; return rowId;
} }
public Object getKeyRing(int keyRingId) { public Object getKeyRing(int keyRingId) {
boolean grabbedNewDatabase = false; Cursor c = mDb.query(KeyRings.TABLE_NAME,
if (mCurrentDb == null) {
mCurrentDb = getWritableDatabase();
grabbedNewDatabase = true;
}
Cursor c = mCurrentDb.query(KeyRings.TABLE_NAME,
new String[] { KeyRings.KEY_RING_DATA, KeyRings.TYPE }, new String[] { KeyRings.KEY_RING_DATA, KeyRings.TYPE },
KeyRings._ID + " = ?", KeyRings._ID + " = ?",
new String[] { new String[] {
@ -549,24 +476,13 @@ public class Database extends SQLiteOpenHelper {
} }
} }
} }
c.close(); c.close();
if (grabbedNewDatabase) {
mCurrentDb.close();
mCurrentDb = null;
}
return keyRing; return keyRing;
} }
public byte[] getKeyRingDataFromKeyId(int type, long keyId) { public byte[] getKeyRingDataFromKeyId(int type, long keyId) {
boolean grabbedNewDatabase = false; Cursor c = mDb.query(Keys.TABLE_NAME + " INNER JOIN " + KeyRings.TABLE_NAME + " ON (" +
if (mCurrentDb == null) {
mCurrentDb = getReadableDatabase();
grabbedNewDatabase = true;
}
Cursor c = mCurrentDb.query(Keys.TABLE_NAME + " INNER JOIN " + KeyRings.TABLE_NAME + " ON (" +
KeyRings.TABLE_NAME + "." + KeyRings._ID + " = " + KeyRings.TABLE_NAME + "." + KeyRings._ID + " = " +
Keys.TABLE_NAME + "." + Keys.KEY_RING_ID + ")", Keys.TABLE_NAME + "." + Keys.KEY_RING_ID + ")",
new String[] { KeyRings.TABLE_NAME + "." + KeyRings.KEY_RING_DATA }, new String[] { KeyRings.TABLE_NAME + "." + KeyRings.KEY_RING_DATA },
@ -582,24 +498,13 @@ public class Database extends SQLiteOpenHelper {
if (c != null && c.moveToFirst()) { if (c != null && c.moveToFirst()) {
data = c.getBlob(0); data = c.getBlob(0);
} }
c.close(); c.close();
if (grabbedNewDatabase) {
mCurrentDb.close();
mCurrentDb = null;
}
return data; return data;
} }
public byte[] getKeyDataFromKeyId(int type, long keyId) { public byte[] getKeyDataFromKeyId(int type, long keyId) {
boolean grabbedNewDatabase = false; Cursor c = mDb.query(Keys.TABLE_NAME, new String[] { Keys.KEY_DATA },
if (mCurrentDb == null) {
mCurrentDb = getReadableDatabase();
grabbedNewDatabase = true;
}
Cursor c = mCurrentDb.query(Keys.TABLE_NAME, new String[] { Keys.KEY_DATA },
Keys.KEY_ID + " = ? AND " + Keys.TYPE + " = ?", Keys.KEY_ID + " = ? AND " + Keys.TYPE + " = ?",
new String[] { new String[] {
"" + keyId, "" + keyId,
@ -610,27 +515,17 @@ public class Database extends SQLiteOpenHelper {
if (c != null && c.moveToFirst()) { if (c != null && c.moveToFirst()) {
data = c.getBlob(0); data = c.getBlob(0);
} }
c.close(); c.close();
if (grabbedNewDatabase) {
mCurrentDb.close();
mCurrentDb = null;
}
return data; return data;
} }
public void deleteKeyRing(int keyRingId) { public void deleteKeyRing(int keyRingId) {
boolean grabbedNewDatabase = false; mDb.beginTransaction();
if (mCurrentDb == null) { mDb.delete(KeyRings.TABLE_NAME,
mCurrentDb = getWritableDatabase();
grabbedNewDatabase = true;
}
mCurrentDb.delete(KeyRings.TABLE_NAME,
KeyRings._ID + " = ?", new String[] { "" + keyRingId }); 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 + " = ?", Keys.KEY_RING_ID + " = ?",
new String[] { new String[] {
"" + keyRingId, "" + keyRingId,
@ -642,28 +537,20 @@ public class Database extends SQLiteOpenHelper {
deleteKey(keyId); deleteKey(keyId);
} while (c.moveToNext()); } while (c.moveToNext());
} }
c.close();
if (grabbedNewDatabase) { mDb.endTransaction();
mCurrentDb.close();
mCurrentDb = null;
}
} }
public void deleteKey(int keyId) { private void deleteKey(int keyId) {
boolean grabbedNewDatabase = false; mDb.delete(Keys.TABLE_NAME,
if (mCurrentDb == null) {
mCurrentDb = getWritableDatabase();
grabbedNewDatabase = true;
}
mCurrentDb.delete(Keys.TABLE_NAME,
Keys._ID + " = ?", new String[] { "" + keyId }); Keys._ID + " = ?", new String[] { "" + keyId });
mCurrentDb.delete(UserIds.TABLE_NAME, mDb.delete(UserIds.TABLE_NAME,
UserIds.KEY_ID + " = ?", new String[] { "" + keyId }); UserIds.KEY_ID + " = ?", new String[] { "" + keyId });
}
if (grabbedNewDatabase) { public SQLiteDatabase db() {
mCurrentDb.close(); return mDb;
mCurrentDb = null;
}
} }
} }