From 5547f61e4f40a8e9e755c51fadc3b5e4ec8a7590 Mon Sep 17 00:00:00 2001 From: Thialfihar Date: Tue, 1 Jun 2010 14:59:06 +0000 Subject: [PATCH] deliver the user id as well for secret key selection, so k9mail can use it to display the signature key --- src/org/thialfihar/android/apg/Apg.java | 38 +++++++++++++++++++ .../android/apg/EncryptActivity.java | 10 +++-- .../apg/SelectSecretKeyListActivity.java | 1 + .../android/apg/provider/Database.java | 18 ++++----- 4 files changed, 54 insertions(+), 13 deletions(-) diff --git a/src/org/thialfihar/android/apg/Apg.java b/src/org/thialfihar/android/apg/Apg.java index f234d6e04..85663629d 100644 --- a/src/org/thialfihar/android/apg/Apg.java +++ b/src/org/thialfihar/android/apg/Apg.java @@ -81,6 +81,8 @@ import org.bouncycastle2.openpgp.PGPSignatureSubpacketVector; 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.Keys; +import org.thialfihar.android.apg.provider.UserIds; import org.thialfihar.android.apg.ui.widget.KeyEditor; import org.thialfihar.android.apg.ui.widget.SectionView; import org.thialfihar.android.apg.ui.widget.UserIdEditor; @@ -116,6 +118,7 @@ public class Apg { public static final String EXTRA_SIGNATURE_USER_ID = "signatureUserId"; public static final String EXTRA_SIGNATURE_SUCCESS = "signatureSuccess"; public static final String EXTRA_SIGNATURE_UNKNOWN = "signatureUnknown"; + public static final String EXTRA_USER_ID = "userId"; public static final String EXTRA_KEY_ID = "keyId"; public static final String EXTRA_REPLY_TO = "replyTo"; public static final String EXTRA_SEND_TO = "sendTo"; @@ -1042,6 +1045,41 @@ public class Apg { return keyIds; } + public static String getMainUserId(long keyId, int type) { + SQLiteDatabase db = mDatabase.db(); + Cursor c = db.query(Keys.TABLE_NAME + " INNER JOIN " + KeyRings.TABLE_NAME + " ON (" + + KeyRings.TABLE_NAME + "." + KeyRings._ID + " = " + + Keys.TABLE_NAME + "." + Keys.KEY_RING_ID + ") " + + " INNER JOIN " + Keys.TABLE_NAME + " AS masterKey ON (" + + KeyRings.TABLE_NAME + "." + KeyRings._ID + " = " + + "masterKey." + Keys.KEY_RING_ID + " AND " + + "masterKey." + Keys.IS_MASTER_KEY + " = '1') " + + " INNER JOIN " + UserIds.TABLE_NAME + " ON (" + + UserIds.TABLE_NAME + "." + UserIds.KEY_ID + " = " + + "masterKey." + Keys._ID + " AND " + + UserIds.TABLE_NAME + "." + UserIds.RANK + " = '0')", + new String[] { UserIds.USER_ID }, + Keys.TABLE_NAME + "." + Keys.KEY_ID + " = ? AND " + + KeyRings.TABLE_NAME + "." + KeyRings.TYPE + " = ?", + new String[] { + "" + keyId, + "" + type, + }, + null, null, null); + String userId = ""; + if (c != null && c.moveToFirst()) { + do { + userId = c.getString(0); + } while (c.moveToNext()); + } + + if (c != null) { + c.close(); + } + + return userId; + } + public static void encrypt(Context context, InputStream inStream, OutputStream outStream, long dataLength, diff --git a/src/org/thialfihar/android/apg/EncryptActivity.java b/src/org/thialfihar/android/apg/EncryptActivity.java index 9106c1c83..39a321f03 100644 --- a/src/org/thialfihar/android/apg/EncryptActivity.java +++ b/src/org/thialfihar/android/apg/EncryptActivity.java @@ -346,7 +346,7 @@ public class EncryptActivity extends BaseActivity { if (mMessage.getText().length() > 0 && ((mEncryptionKeyIds != null && mEncryptionKeyIds.length > 0) || - getSecretKeyId() > 0)) { + getSecretKeyId() != 0)) { encryptClicked(); } } @@ -467,7 +467,7 @@ public class EncryptActivity extends BaseActivity { return; } } else { - boolean encryptIt = mEncryptionKeyIds != null && mEncryptionKeyIds.length > 0; + boolean encryptIt = (mEncryptionKeyIds != null && mEncryptionKeyIds.length > 0); // for now require at least one form of encryption for files if (!encryptIt && mEncryptTarget == Id.target.file) { Toast.makeText(this, R.string.selectEncryptionKey, Toast.LENGTH_SHORT).show(); @@ -537,7 +537,7 @@ public class EncryptActivity extends BaseActivity { } else { encryptionKeyIds = mEncryptionKeyIds; signatureKeyId = getSecretKeyId(); - signOnly = mEncryptionKeyIds == null || mEncryptionKeyIds.length == 0; + signOnly = (mEncryptionKeyIds == null || mEncryptionKeyIds.length == 0); } if (mEncryptTarget == Id.target.file) { @@ -558,7 +558,9 @@ public class EncryptActivity extends BaseActivity { } else { String message = mMessage.getText().toString(); - if (signOnly) { + if (signOnly && + !(mIntent != null && + mIntent.getAction().equals(Apg.Intent.ENCRYPT_AND_RETURN))) { // fix the message a bit, trailing spaces and newlines break stuff, // because GMail sends as HTML and such things fuck up the signature, // TODO: things like "<" and ">" also fuck up the signature diff --git a/src/org/thialfihar/android/apg/SelectSecretKeyListActivity.java b/src/org/thialfihar/android/apg/SelectSecretKeyListActivity.java index cd87a94b6..af50b7e01 100644 --- a/src/org/thialfihar/android/apg/SelectSecretKeyListActivity.java +++ b/src/org/thialfihar/android/apg/SelectSecretKeyListActivity.java @@ -44,6 +44,7 @@ public class SelectSecretKeyListActivity extends BaseActivity { public void onItemClick(AdapterView adapterView, View view, int position, long id) { Intent data = new Intent(); data.putExtra(Apg.EXTRA_KEY_ID, id); + data.putExtra(Apg.EXTRA_USER_ID, Apg.getMainUserId(id, Id.database.type_secret)); setResult(RESULT_OK, data); finish(); } diff --git a/src/org/thialfihar/android/apg/provider/Database.java b/src/org/thialfihar/android/apg/provider/Database.java index acb5c0943..8c27b130e 100644 --- a/src/org/thialfihar/android/apg/provider/Database.java +++ b/src/org/thialfihar/android/apg/provider/Database.java @@ -248,9 +248,9 @@ public class Database extends SQLiteOpenHelper { seenIdsStr += id; } mDb.delete(Keys.TABLE_NAME, - Keys.KEY_RING_ID + " = ? AND " + - Keys._ID + " NOT IN (" + seenIdsStr + ")", - new String[] { "" + rowId }); + Keys.KEY_RING_ID + " = ? AND " + + Keys._ID + " NOT IN (" + seenIdsStr + ")", + new String[] { "" + rowId }); mDb.setTransactionSuccessful(); mDb.endTransaction(); @@ -289,9 +289,9 @@ public class Database extends SQLiteOpenHelper { seenIdsStr += id; } mDb.delete(Keys.TABLE_NAME, - Keys.KEY_RING_ID + " = ? AND " + - Keys._ID + " NOT IN (" + seenIdsStr + ")", - new String[] { "" + rowId }); + Keys.KEY_RING_ID + " = ? AND " + + Keys._ID + " NOT IN (" + seenIdsStr + ")", + new String[] { "" + rowId }); mDb.setTransactionSuccessful(); mDb.endTransaction(); @@ -340,9 +340,9 @@ public class Database extends SQLiteOpenHelper { seenIdsStr += id; } mDb.delete(UserIds.TABLE_NAME, - UserIds.KEY_ID + " = ? AND " + - UserIds._ID + " NOT IN (" + seenIdsStr + ")", - new String[] { "" + rowId }); + UserIds.KEY_ID + " = ? AND " + + UserIds._ID + " NOT IN (" + seenIdsStr + ")", + new String[] { "" + rowId }); return (int)rowId; }