1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-08-13 17:03:48 -04:00

decoupled PgpData from CryptoProvider, moved the crypto provider into Account

This commit is contained in:
Thialfihar 2010-08-22 09:51:17 +00:00
parent df0ae4e4bf
commit a93da0ed80
7 changed files with 214 additions and 178 deletions

View File

@ -8,6 +8,7 @@ import android.net.Uri;
import android.util.Log; import android.util.Log;
import com.fsck.k9.crypto.Apg; import com.fsck.k9.crypto.Apg;
import com.fsck.k9.crypto.CryptoProvider;
import com.fsck.k9.helper.Utility; import com.fsck.k9.helper.Utility;
import com.fsck.k9.mail.Address; import com.fsck.k9.mail.Address;
import com.fsck.k9.mail.Folder; import com.fsck.k9.mail.Folder;
@ -112,6 +113,8 @@ public class Account implements BaseAccount
private String mCryptoApp; private String mCryptoApp;
private boolean mCryptoAutoSignature; private boolean mCryptoAutoSignature;
private CryptoProvider mCryptoProvider = null;
/** /**
* Name of the folder that was last selected for a copy or move operation. * Name of the folder that was last selected for a copy or move operation.
* *
@ -1455,6 +1458,8 @@ public class Account implements BaseAccount
public void setCryptoApp(String cryptoApp) public void setCryptoApp(String cryptoApp)
{ {
mCryptoApp = cryptoApp; mCryptoApp = cryptoApp;
// invalidate the provider
mCryptoProvider = null;
} }
public boolean getCryptoAutoSignature() public boolean getCryptoAutoSignature()
@ -1485,4 +1490,11 @@ public class Account implements BaseAccount
{ {
lastSelectedFolderName = folderName; lastSelectedFolderName = folderName;
} }
public synchronized CryptoProvider getCryptoProvider() {
if (mCryptoProvider == null) {
mCryptoProvider = CryptoProvider.createInstance(getCryptoApp());
}
return mCryptoProvider;
}
} }

View File

@ -55,6 +55,7 @@ import com.fsck.k9.R;
import com.fsck.k9.controller.MessagingController; import com.fsck.k9.controller.MessagingController;
import com.fsck.k9.controller.MessagingListener; import com.fsck.k9.controller.MessagingListener;
import com.fsck.k9.crypto.CryptoProvider; import com.fsck.k9.crypto.CryptoProvider;
import com.fsck.k9.crypto.PgpData;
import com.fsck.k9.helper.Utility; import com.fsck.k9.helper.Utility;
import com.fsck.k9.mail.Address; import com.fsck.k9.mail.Address;
import com.fsck.k9.mail.Body; import com.fsck.k9.mail.Body;
@ -103,7 +104,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, OnFoc
"com.fsck.k9.activity.MessageCompose.identityChanged"; "com.fsck.k9.activity.MessageCompose.identityChanged";
private static final String STATE_IDENTITY = private static final String STATE_IDENTITY =
"com.fsck.k9.activity.MessageCompose.identity"; "com.fsck.k9.activity.MessageCompose.identity";
private static final String STATE_CRYPTO = "crypto"; private static final String STATE_PGP_DATA = "pgpData";
private static final String STATE_IN_REPLY_TO = "com.fsck.k9.activity.MessageCompose.inReplyTo"; private static final String STATE_IN_REPLY_TO = "com.fsck.k9.activity.MessageCompose.inReplyTo";
private static final String STATE_REFERENCES = "com.fsck.k9.activity.MessageCompose.references"; private static final String STATE_REFERENCES = "com.fsck.k9.activity.MessageCompose.references";
@ -174,7 +175,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, OnFoc
private TextView mCryptoSignatureUserId; private TextView mCryptoSignatureUserId;
private TextView mCryptoSignatureUserIdRest; private TextView mCryptoSignatureUserIdRest;
private CryptoProvider mCrypto = null; private PgpData mPgpData = null;
private String mReferences; private String mReferences;
private String mInReplyTo; private String mInReplyTo;
@ -676,7 +677,8 @@ public class MessageCompose extends K9Activity implements OnClickListener, OnFoc
mEncryptCheckbox = (CheckBox)findViewById(R.id.cb_encrypt); mEncryptCheckbox = (CheckBox)findViewById(R.id.cb_encrypt);
initializeCrypto(); initializeCrypto();
if (mCrypto.isAvailable(this)) final CryptoProvider crypto = mAccount.getCryptoProvider();
if (crypto.isAvailable(this))
{ {
mEncryptLayout.setVisibility(View.VISIBLE); mEncryptLayout.setVisibility(View.VISIBLE);
mCryptoSignatureCheckbox.setOnClickListener(new OnClickListener() mCryptoSignatureCheckbox.setOnClickListener(new OnClickListener()
@ -688,7 +690,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, OnFoc
if (checkBox.isChecked()) if (checkBox.isChecked())
{ {
mPreventDraftSaving = true; mPreventDraftSaving = true;
if (!mCrypto.selectSecretKey(MessageCompose.this)) if (!crypto.selectSecretKey(MessageCompose.this, mPgpData))
{ {
mPreventDraftSaving = false; mPreventDraftSaving = false;
} }
@ -696,7 +698,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, OnFoc
} }
else else
{ {
mCrypto.setSignatureKeyId(0); mPgpData.setSignatureKeyId(0);
updateEncryptLayout(); updateEncryptLayout();
} }
} }
@ -704,16 +706,16 @@ public class MessageCompose extends K9Activity implements OnClickListener, OnFoc
if (mAccount.getCryptoAutoSignature()) if (mAccount.getCryptoAutoSignature())
{ {
long ids[] = mCrypto.getSecretKeyIdsFromEmail(this, mIdentity.getEmail()); long ids[] = crypto.getSecretKeyIdsFromEmail(this, mIdentity.getEmail());
if (ids != null && ids.length > 0) if (ids != null && ids.length > 0)
{ {
mCrypto.setSignatureKeyId(ids[0]); mPgpData.setSignatureKeyId(ids[0]);
mCrypto.setSignatureUserId(mCrypto.getUserId(this, ids[0])); mPgpData.setSignatureUserId(crypto.getUserId(this, ids[0]));
} }
else else
{ {
mCrypto.setSignatureKeyId(0); mPgpData.setSignatureKeyId(0);
mCrypto.setSignatureUserId(null); mPgpData.setSignatureUserId(null);
} }
} }
updateEncryptLayout(); updateEncryptLayout();
@ -728,11 +730,11 @@ public class MessageCompose extends K9Activity implements OnClickListener, OnFoc
private void initializeCrypto() private void initializeCrypto()
{ {
if (mCrypto != null) if (mPgpData != null)
{ {
return; return;
} }
mCrypto = CryptoProvider.createInstance(mAccount); mPgpData = new PgpData();
} }
/** /**
@ -740,7 +742,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, OnFoc
*/ */
public void updateEncryptLayout() public void updateEncryptLayout()
{ {
if (!mCrypto.hasSignatureKey()) if (!mPgpData.hasSignatureKey())
{ {
mCryptoSignatureCheckbox.setText(R.string.btn_crypto_sign); mCryptoSignatureCheckbox.setText(R.string.btn_crypto_sign);
mCryptoSignatureCheckbox.setChecked(false); mCryptoSignatureCheckbox.setChecked(false);
@ -757,16 +759,16 @@ public class MessageCompose extends K9Activity implements OnClickListener, OnFoc
mCryptoSignatureUserId.setText(R.string.unknown_crypto_signature_user_id); mCryptoSignatureUserId.setText(R.string.unknown_crypto_signature_user_id);
mCryptoSignatureUserIdRest.setText(""); mCryptoSignatureUserIdRest.setText("");
String userId = mCrypto.getSignatureUserId(); String userId = mPgpData.getSignatureUserId();
if (userId == null) if (userId == null)
{ {
userId = mCrypto.getUserId(this, mCrypto.getSignatureKeyId()); userId = mAccount.getCryptoProvider().getUserId(this, mPgpData.getSignatureKeyId());
mCrypto.setSignatureUserId(userId); mPgpData.setSignatureUserId(userId);
} }
if (userId != null) if (userId != null)
{ {
String chunks[] = mCrypto.getSignatureUserId().split(" <", 2); String chunks[] = mPgpData.getSignatureUserId().split(" <", 2);
mCryptoSignatureUserId.setText(chunks[0]); mCryptoSignatureUserId.setText(chunks[0]);
if (chunks.length > 1) if (chunks.length > 1)
{ {
@ -819,7 +821,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, OnFoc
outState.putString(STATE_KEY_DRAFT_UID, mDraftUid); outState.putString(STATE_KEY_DRAFT_UID, mDraftUid);
outState.putSerializable(STATE_IDENTITY, mIdentity); outState.putSerializable(STATE_IDENTITY, mIdentity);
outState.putBoolean(STATE_IDENTITY_CHANGED, mIdentityChanged); outState.putBoolean(STATE_IDENTITY_CHANGED, mIdentityChanged);
outState.putSerializable(STATE_CRYPTO, mCrypto); outState.putSerializable(STATE_PGP_DATA, mPgpData);
outState.putString(STATE_IN_REPLY_TO, mInReplyTo); outState.putString(STATE_IN_REPLY_TO, mInReplyTo);
outState.putString(STATE_REFERENCES, mReferences); outState.putString(STATE_REFERENCES, mReferences);
} }
@ -843,7 +845,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, OnFoc
mDraftUid = savedInstanceState.getString(STATE_KEY_DRAFT_UID); mDraftUid = savedInstanceState.getString(STATE_KEY_DRAFT_UID);
mIdentity = (Identity)savedInstanceState.getSerializable(STATE_IDENTITY); mIdentity = (Identity)savedInstanceState.getSerializable(STATE_IDENTITY);
mIdentityChanged = savedInstanceState.getBoolean(STATE_IDENTITY_CHANGED); mIdentityChanged = savedInstanceState.getBoolean(STATE_IDENTITY_CHANGED);
mCrypto = (CryptoProvider) savedInstanceState.getSerializable(STATE_CRYPTO); mPgpData = (PgpData) savedInstanceState.getSerializable(STATE_PGP_DATA);
mInReplyTo = savedInstanceState.getString(STATE_IN_REPLY_TO); mInReplyTo = savedInstanceState.getString(STATE_IN_REPLY_TO);
mReferences = savedInstanceState.getString(STATE_REFERENCES); mReferences = savedInstanceState.getString(STATE_REFERENCES);
@ -953,9 +955,9 @@ public class MessageCompose extends K9Activity implements OnClickListener, OnFoc
} }
String text = null; String text = null;
if (mCrypto.getEncryptedData() != null) if (mPgpData.getEncryptedData() != null)
{ {
text = mCrypto.getEncryptedData(); text = mPgpData.getEncryptedData();
} }
else else
{ {
@ -1058,7 +1060,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, OnFoc
private void saveIfNeeded() private void saveIfNeeded()
{ {
if (!mDraftNeedsSaving || mPreventDraftSaving || mCrypto.hasEncryptionKeys()) if (!mDraftNeedsSaving || mPreventDraftSaving || mPgpData.hasEncryptionKeys())
{ {
return; return;
} }
@ -1069,7 +1071,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, OnFoc
public void onEncryptionKeySelectionDone() public void onEncryptionKeySelectionDone()
{ {
if (mCrypto.hasEncryptionKeys()) if (mPgpData.hasEncryptionKeys())
{ {
onSend(); onSend();
} }
@ -1081,7 +1083,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, OnFoc
public void onEncryptDone() public void onEncryptDone()
{ {
if (mCrypto.getEncryptedData() != null) if (mPgpData.getEncryptedData() != null)
{ {
onSend(); onSend();
} }
@ -1099,7 +1101,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, OnFoc
Toast.makeText(this, getString(R.string.message_compose_error_no_recipients), Toast.LENGTH_LONG).show(); Toast.makeText(this, getString(R.string.message_compose_error_no_recipients), Toast.LENGTH_LONG).show();
return; return;
} }
if (mEncryptCheckbox.isChecked() && !mCrypto.hasEncryptionKeys()) if (mEncryptCheckbox.isChecked() && !mPgpData.hasEncryptionKeys())
{ {
// key selection before encryption // key selection before encryption
String emails = ""; String emails = "";
@ -1125,19 +1127,19 @@ public class MessageCompose extends K9Activity implements OnClickListener, OnFoc
emails += mIdentity.getEmail(); emails += mIdentity.getEmail();
mPreventDraftSaving = true; mPreventDraftSaving = true;
if (!mCrypto.selectEncryptionKeys(MessageCompose.this, emails)) if (!mAccount.getCryptoProvider().selectEncryptionKeys(MessageCompose.this, emails, mPgpData))
{ {
mPreventDraftSaving = false; mPreventDraftSaving = false;
} }
return; return;
} }
if (mCrypto.hasEncryptionKeys() || mCrypto.hasSignatureKey()) if (mPgpData.hasEncryptionKeys() || mPgpData.hasSignatureKey())
{ {
if (mCrypto.getEncryptedData() == null) if (mPgpData.getEncryptedData() == null)
{ {
String text = buildText(true); String text = buildText(true);
mPreventDraftSaving = true; mPreventDraftSaving = true;
if (!mCrypto.encrypt(this, text)) if (!mAccount.getCryptoProvider().encrypt(this, text, mPgpData))
{ {
mPreventDraftSaving = false; mPreventDraftSaving = false;
} }
@ -1203,7 +1205,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, OnFoc
*/ */
private void onAddAttachment2(final String mime_type) private void onAddAttachment2(final String mime_type)
{ {
if (mCrypto.isAvailable(this)) if (mAccount.getCryptoProvider().isAvailable(this))
{ {
Toast.makeText(this, R.string.attachment_encryption_unsupported, Toast.LENGTH_LONG).show(); Toast.makeText(this, R.string.attachment_encryption_unsupported, Toast.LENGTH_LONG).show();
} }
@ -1304,7 +1306,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, OnFoc
// if a CryptoSystem activity is returning, then mPreventDraftSaving was set to true // if a CryptoSystem activity is returning, then mPreventDraftSaving was set to true
mPreventDraftSaving = false; mPreventDraftSaving = false;
if (mCrypto.onActivityResult(this, requestCode, resultCode, data)) if (mAccount.getCryptoProvider().onActivityResult(this, requestCode, resultCode, data, mPgpData))
{ {
return; return;
} }
@ -1454,7 +1456,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, OnFoc
switch (item.getItemId()) switch (item.getItemId())
{ {
case R.id.send: case R.id.send:
mCrypto.setEncryptionKeys(null); mPgpData.setEncryptionKeys(null);
onSend(); onSend();
break; break;
case R.id.save: case R.id.save:

View File

@ -67,6 +67,7 @@ import com.fsck.k9.R;
import com.fsck.k9.controller.MessagingController; import com.fsck.k9.controller.MessagingController;
import com.fsck.k9.controller.MessagingListener; import com.fsck.k9.controller.MessagingListener;
import com.fsck.k9.crypto.CryptoProvider; import com.fsck.k9.crypto.CryptoProvider;
import com.fsck.k9.crypto.PgpData;
import com.fsck.k9.helper.Contacts; import com.fsck.k9.helper.Contacts;
import com.fsck.k9.mail.Address; import com.fsck.k9.mail.Address;
import com.fsck.k9.mail.Flag; import com.fsck.k9.mail.Flag;
@ -89,7 +90,7 @@ public class MessageView extends K9Activity implements OnClickListener
private static final String EXTRA_NEXT = "com.fsck.k9.MessageView_next"; private static final String EXTRA_NEXT = "com.fsck.k9.MessageView_next";
private static final String SHOW_PICTURES = "showPictures"; private static final String SHOW_PICTURES = "showPictures";
private static final String STATE_CRYPTO = "crypto"; private static final String STATE_PGP_DATA = "pgpData";
private static final int ACTIVITY_CHOOSE_FOLDER_MOVE = 1; private static final int ACTIVITY_CHOOSE_FOLDER_MOVE = 1;
private static final int ACTIVITY_CHOOSE_FOLDER_COPY = 2; private static final int ACTIVITY_CHOOSE_FOLDER_COPY = 2;
@ -146,7 +147,7 @@ public class MessageView extends K9Activity implements OnClickListener
private ArrayList<MessageReference> mMessageReferences; private ArrayList<MessageReference> mMessageReferences;
private Message mMessage; private Message mMessage;
private CryptoProvider mCrypto = null; private PgpData mPgpData = null;
private static final int PREVIOUS = 1; private static final int PREVIOUS = 1;
private static final int NEXT = 2; private static final int NEXT = 2;
@ -806,7 +807,7 @@ public class MessageView extends K9Activity implements OnClickListener
{ {
data = MimeUtility.getTextFromPart(part); data = MimeUtility.getTextFromPart(part);
} }
mCrypto.decrypt(MessageView.this, data); mAccount.getCryptoProvider().decrypt(MessageView.this, data, mPgpData);
} }
catch (MessagingException me) catch (MessagingException me)
{ {
@ -900,7 +901,7 @@ public class MessageView extends K9Activity implements OnClickListener
mMessageReference = (MessageReference)icicle.getSerializable(EXTRA_MESSAGE_REFERENCE); mMessageReference = (MessageReference)icicle.getSerializable(EXTRA_MESSAGE_REFERENCE);
mMessageReferences = (ArrayList<MessageReference>)icicle.getSerializable(EXTRA_MESSAGE_REFERENCES); mMessageReferences = (ArrayList<MessageReference>)icicle.getSerializable(EXTRA_MESSAGE_REFERENCES);
mCrypto = (CryptoProvider) icicle.getSerializable(STATE_CRYPTO); mPgpData = (PgpData) icicle.getSerializable(STATE_PGP_DATA);
updateDecryptLayout(); updateDecryptLayout();
} }
else else
@ -1081,7 +1082,7 @@ public class MessageView extends K9Activity implements OnClickListener
{ {
outState.putSerializable(EXTRA_MESSAGE_REFERENCE, mMessageReference); outState.putSerializable(EXTRA_MESSAGE_REFERENCE, mMessageReference);
outState.putSerializable(EXTRA_MESSAGE_REFERENCES, mMessageReferences); outState.putSerializable(EXTRA_MESSAGE_REFERENCES, mMessageReferences);
outState.putSerializable(STATE_CRYPTO, mCrypto); outState.putSerializable(STATE_PGP_DATA, mPgpData);
outState.putBoolean(SHOW_PICTURES, mShowPictures); outState.putBoolean(SHOW_PICTURES, mShowPictures);
} }
@ -1093,7 +1094,7 @@ public class MessageView extends K9Activity implements OnClickListener
mShowPictures = savedInstanceState.getBoolean(SHOW_PICTURES); mShowPictures = savedInstanceState.getBoolean(SHOW_PICTURES);
setLoadPictures(mShowPictures); setLoadPictures(mShowPictures);
mCrypto = (CryptoProvider) savedInstanceState.getSerializable(STATE_CRYPTO); mPgpData = (PgpData) savedInstanceState.getSerializable(STATE_PGP_DATA);
initializeCrypto(); initializeCrypto();
updateDecryptLayout(); updateDecryptLayout();
@ -1115,22 +1116,16 @@ public class MessageView extends K9Activity implements OnClickListener
mAttachments.removeAllViews(); mAttachments.removeAllViews();
findSurroundingMessagesUid(); findSurroundingMessagesUid();
// grab a new crypto provider object, as the account may have changed, and currently // start with fresh, empty PGP data
// the decrypted data and signature are stored in the crypto provider object mPgpData = null;
// TODO: separate that storage from the provider
// TODO: then move the provider object directly into the account object
mCrypto = null;
initializeCrypto(); initializeCrypto();
MessagingController.getInstance(getApplication()).loadMessageForView( MessagingController.getInstance(getApplication()).loadMessageForView(
mAccount, mAccount,
mMessageReference.folderName, mMessageReference.folderName,
mMessageReference.uid, mMessageReference.uid,
mListener); mListener);
setupDisplayMessageButtons(); setupDisplayMessageButtons();
} }
private void setupDisplayMessageButtons() private void setupDisplayMessageButtons()
@ -1388,7 +1383,7 @@ public class MessageView extends K9Activity implements OnClickListener
{ {
if (mMessage != null) if (mMessage != null)
{ {
MessageCompose.actionReply(this, mAccount, mMessage, false, mCrypto.getDecryptedData()); MessageCompose.actionReply(this, mAccount, mMessage, false, mPgpData.getDecryptedData());
finish(); finish();
} }
} }
@ -1397,7 +1392,7 @@ public class MessageView extends K9Activity implements OnClickListener
{ {
if (mMessage != null) if (mMessage != null)
{ {
MessageCompose.actionReply(this, mAccount, mMessage, true, mCrypto.getDecryptedData()); MessageCompose.actionReply(this, mAccount, mMessage, true, mPgpData.getDecryptedData());
finish(); finish();
} }
} }
@ -1406,7 +1401,7 @@ public class MessageView extends K9Activity implements OnClickListener
{ {
if (mMessage != null) if (mMessage != null)
{ {
MessageCompose.actionForward(this, mAccount, mMessage, mCrypto.getDecryptedData()); MessageCompose.actionForward(this, mAccount, mMessage, mPgpData.getDecryptedData());
finish(); finish();
} }
} }
@ -1513,7 +1508,7 @@ public class MessageView extends K9Activity implements OnClickListener
@Override @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) protected void onActivityResult(int requestCode, int resultCode, Intent data)
{ {
if (mCrypto.onActivityResult(this, requestCode, resultCode, data)) if (mAccount.getCryptoProvider().onActivityResult(this, requestCode, resultCode, data, mPgpData))
{ {
return; return;
} }
@ -2164,9 +2159,9 @@ public class MessageView extends K9Activity implements OnClickListener
String text; String text;
String type = "text/html"; String type = "text/html";
if (mCrypto.getDecryptedData() != null) if (mPgpData.getDecryptedData() != null)
{ {
text = mCrypto.getDecryptedData(); text = mPgpData.getDecryptedData();
type = "text/plain"; type = "text/plain";
} }
else else
@ -2509,7 +2504,7 @@ public class MessageView extends K9Activity implements OnClickListener
private void initializeCrypto() private void initializeCrypto()
{ {
if (mCrypto != null) if (mPgpData != null)
{ {
return; return;
} }
@ -2517,7 +2512,7 @@ public class MessageView extends K9Activity implements OnClickListener
{ {
mAccount = Preferences.getPreferences(this).getAccount(mMessageReference.accountUuid); mAccount = Preferences.getPreferences(this).getAccount(mMessageReference.accountUuid);
} }
mCrypto = CryptoProvider.createInstance(mAccount); mPgpData = new PgpData();
} }
/** /**
@ -2526,11 +2521,11 @@ public class MessageView extends K9Activity implements OnClickListener
*/ */
public void updateDecryptLayout() public void updateDecryptLayout()
{ {
if (mCrypto.getSignatureKeyId() != 0) if (mPgpData.getSignatureKeyId() != 0)
{ {
mCryptoSignatureUserIdRest.setText( mCryptoSignatureUserIdRest.setText(
getString(R.string.key_id, Long.toHexString(mCrypto.getSignatureKeyId() & 0xffffffffL))); getString(R.string.key_id, Long.toHexString(mPgpData.getSignatureKeyId() & 0xffffffffL)));
String userId = mCrypto.getSignatureUserId(); String userId = mPgpData.getSignatureUserId();
if (userId == null) if (userId == null)
{ {
userId = getString(R.string.unknown_crypto_signature_user_id); userId = getString(R.string.unknown_crypto_signature_user_id);
@ -2543,11 +2538,11 @@ public class MessageView extends K9Activity implements OnClickListener
} }
mCryptoSignatureUserId.setText(name); mCryptoSignatureUserId.setText(name);
if (mCrypto.getSignatureSuccess()) if (mPgpData.getSignatureSuccess())
{ {
mCryptoSignatureStatusImage.setImageResource(R.drawable.overlay_ok); mCryptoSignatureStatusImage.setImageResource(R.drawable.overlay_ok);
} }
else if (mCrypto.getSignatureUnknown()) else if (mPgpData.getSignatureUnknown())
{ {
mCryptoSignatureStatusImage.setImageResource(R.drawable.overlay_error); mCryptoSignatureStatusImage.setImageResource(R.drawable.overlay_error);
} }
@ -2563,15 +2558,15 @@ public class MessageView extends K9Activity implements OnClickListener
mCryptoSignatureLayout.setVisibility(View.INVISIBLE); mCryptoSignatureLayout.setVisibility(View.INVISIBLE);
} }
if (!true || ((mMessage == null) && (mCrypto.getDecryptedData() == null))) if (!true || ((mMessage == null) && (mPgpData.getDecryptedData() == null)))
{ {
mDecryptLayout.setVisibility(View.GONE); mDecryptLayout.setVisibility(View.GONE);
return; return;
} }
if (mCrypto.getDecryptedData() != null) if (mPgpData.getDecryptedData() != null)
{ {
if (mCrypto.getSignatureKeyId() == 0) if (mPgpData.getSignatureKeyId() == 0)
{ {
mDecryptLayout.setVisibility(View.GONE); mDecryptLayout.setVisibility(View.GONE);
} }
@ -2585,12 +2580,13 @@ public class MessageView extends K9Activity implements OnClickListener
mDecryptButton.setVisibility(View.VISIBLE); mDecryptButton.setVisibility(View.VISIBLE);
if (mCrypto.isEncrypted(mMessage)) CryptoProvider crypto = mAccount.getCryptoProvider();
if (crypto.isEncrypted(mMessage))
{ {
mDecryptButton.setText(R.string.btn_decrypt); mDecryptButton.setText(R.string.btn_decrypt);
mDecryptLayout.setVisibility(View.VISIBLE); mDecryptLayout.setVisibility(View.VISIBLE);
} }
else if (mCrypto.isSigned(mMessage)) else if (crypto.isSigned(mMessage))
{ {
mDecryptButton.setText(R.string.btn_verify); mDecryptButton.setText(R.string.btn_verify);
mDecryptLayout.setVisibility(View.VISIBLE); mDecryptLayout.setVisibility(View.VISIBLE);
@ -2618,7 +2614,7 @@ public class MessageView extends K9Activity implements OnClickListener
{ {
// TODO: this might not be enough if the orientation was changed while in APG, // TODO: this might not be enough if the orientation was changed while in APG,
// sometimes shows the original encrypted content // sometimes shows the original encrypted content
mMessageContentView.loadDataWithBaseURL("email://", mCrypto.getDecryptedData(), "text/plain", "utf-8", null); mMessageContentView.loadDataWithBaseURL("email://", mPgpData.getDecryptedData(), "text/plain", "utf-8", null);
updateDecryptLayout(); updateDecryptLayout();
} }

View File

@ -89,7 +89,7 @@ public class Apg extends CryptoProvider
Pattern.compile(".*?(-----BEGIN PGP SIGNED MESSAGE-----.*?-----BEGIN PGP SIGNATURE-----.*?-----END PGP SIGNATURE-----).*", Pattern.compile(".*?(-----BEGIN PGP SIGNED MESSAGE-----.*?-----BEGIN PGP SIGNATURE-----.*?-----END PGP SIGNATURE-----).*",
Pattern.DOTALL); Pattern.DOTALL);
public static Apg createInstance(Account account) public static Apg createInstance()
{ {
return new Apg(); return new Apg();
} }
@ -128,10 +128,11 @@ public class Apg extends CryptoProvider
* Select the signature key. * Select the signature key.
* *
* @param activity * @param activity
* @param pgpData
* @return success or failure * @return success or failure
*/ */
@Override @Override
public boolean selectSecretKey(Activity activity) public boolean selectSecretKey(Activity activity, PgpData pgpData)
{ {
android.content.Intent intent = new android.content.Intent(Intent.SELECT_SECRET_KEY); android.content.Intent intent = new android.content.Intent(Intent.SELECT_SECRET_KEY);
intent.putExtra(EXTRA_INTENT_VERSION, INTENT_VERSION); intent.putExtra(EXTRA_INTENT_VERSION, INTENT_VERSION);
@ -154,20 +155,21 @@ public class Apg extends CryptoProvider
* *
* @param activity * @param activity
* @param emails The emails that should be used for preselection. * @param emails The emails that should be used for preselection.
* @param pgpData
* @return success or failure * @return success or failure
*/ */
@Override @Override
public boolean selectEncryptionKeys(Activity activity, String emails) public boolean selectEncryptionKeys(Activity activity, String emails, PgpData pgpData)
{ {
android.content.Intent intent = new android.content.Intent(Apg.Intent.SELECT_PUBLIC_KEYS); android.content.Intent intent = new android.content.Intent(Apg.Intent.SELECT_PUBLIC_KEYS);
intent.putExtra(EXTRA_INTENT_VERSION, INTENT_VERSION); intent.putExtra(EXTRA_INTENT_VERSION, INTENT_VERSION);
long[] initialKeyIds = null; long[] initialKeyIds = null;
if (!hasEncryptionKeys()) if (!pgpData.hasEncryptionKeys())
{ {
Vector<Long> keyIds = new Vector<Long>(); Vector<Long> keyIds = new Vector<Long>();
if (hasSignatureKey()) if (pgpData.hasSignatureKey())
{ {
keyIds.add(getSignatureKeyId()); keyIds.add(pgpData.getSignatureKeyId());
} }
try try
@ -208,7 +210,7 @@ public class Apg extends CryptoProvider
} }
else else
{ {
initialKeyIds = mEncryptionKeyIds; initialKeyIds = pgpData.getEncryptionKeys();
} }
intent.putExtra(Apg.EXTRA_SELECTION, initialKeyIds); intent.putExtra(Apg.EXTRA_SELECTION, initialKeyIds);
try try
@ -321,7 +323,7 @@ public class Apg extends CryptoProvider
*/ */
@Override @Override
public boolean onActivityResult(Activity activity, int requestCode, int resultCode, public boolean onActivityResult(Activity activity, int requestCode, int resultCode,
android.content.Intent data) android.content.Intent data, PgpData pgpData)
{ {
switch (requestCode) switch (requestCode)
{ {
@ -330,37 +332,37 @@ public class Apg extends CryptoProvider
{ {
break; break;
} }
setSignatureKeyId(data.getLongExtra(Apg.EXTRA_KEY_ID, 0)); pgpData.setSignatureKeyId(data.getLongExtra(Apg.EXTRA_KEY_ID, 0));
setSignatureUserId(data.getStringExtra(Apg.EXTRA_USER_ID)); pgpData.setSignatureUserId(data.getStringExtra(Apg.EXTRA_USER_ID));
((MessageCompose) activity).updateEncryptLayout(); ((MessageCompose) activity).updateEncryptLayout();
break; break;
case Apg.SELECT_PUBLIC_KEYS: case Apg.SELECT_PUBLIC_KEYS:
if (resultCode != Activity.RESULT_OK || data == null) if (resultCode != Activity.RESULT_OK || data == null)
{ {
mEncryptionKeyIds = null; pgpData.setEncryptionKeys(null);
((MessageCompose) activity).onEncryptionKeySelectionDone(); ((MessageCompose) activity).onEncryptionKeySelectionDone();
break; break;
} }
mEncryptionKeyIds = data.getLongArrayExtra(Apg.EXTRA_SELECTION); pgpData.setEncryptionKeys(data.getLongArrayExtra(Apg.EXTRA_SELECTION));
((MessageCompose) activity).onEncryptionKeySelectionDone(); ((MessageCompose) activity).onEncryptionKeySelectionDone();
break; break;
case Apg.ENCRYPT_MESSAGE: case Apg.ENCRYPT_MESSAGE:
if (resultCode != Activity.RESULT_OK || data == null) if (resultCode != Activity.RESULT_OK || data == null)
{ {
mEncryptedData = null; pgpData.setEncryptionKeys(null);
((MessageCompose) activity).onEncryptDone(); ((MessageCompose) activity).onEncryptDone();
break; break;
} }
mEncryptedData = data.getStringExtra(Apg.EXTRA_ENCRYPTED_MESSAGE); pgpData.setEncryptedData(data.getStringExtra(Apg.EXTRA_ENCRYPTED_MESSAGE));
// this was a stupid bug in an earlier version, just gonna leave this in for an APG // this was a stupid bug in an earlier version, just gonna leave this in for an APG
// version or two // version or two
if (mEncryptedData == null) if (pgpData.getEncryptedData() == null)
{ {
mEncryptedData = data.getStringExtra(Apg.EXTRA_DECRYPTED_MESSAGE); pgpData.setEncryptedData(data.getStringExtra(Apg.EXTRA_DECRYPTED_MESSAGE));
} }
if (mEncryptedData != null) if (pgpData.getEncryptedData() != null)
{ {
((MessageCompose) activity).onEncryptDone(); ((MessageCompose) activity).onEncryptDone();
} }
@ -372,12 +374,12 @@ public class Apg extends CryptoProvider
break; break;
} }
mSignatureUserId = data.getStringExtra(Apg.EXTRA_SIGNATURE_USER_ID); pgpData.setSignatureUserId(data.getStringExtra(Apg.EXTRA_SIGNATURE_USER_ID));
mSignatureKeyId = data.getLongExtra(Apg.EXTRA_SIGNATURE_KEY_ID, 0); pgpData.setSignatureKeyId(data.getLongExtra(Apg.EXTRA_SIGNATURE_KEY_ID, 0));
mSignatureSuccess = data.getBooleanExtra(Apg.EXTRA_SIGNATURE_SUCCESS, false); pgpData.setSignatureSuccess(data.getBooleanExtra(Apg.EXTRA_SIGNATURE_SUCCESS, false));
mSignatureUnknown = data.getBooleanExtra(Apg.EXTRA_SIGNATURE_UNKNOWN, false); pgpData.setSignatureUnknown(data.getBooleanExtra(Apg.EXTRA_SIGNATURE_UNKNOWN, false));
mDecryptedData = data.getStringExtra(Apg.EXTRA_DECRYPTED_MESSAGE); pgpData.setDecryptedData(data.getStringExtra(Apg.EXTRA_DECRYPTED_MESSAGE));
((MessageView) activity).onDecryptDone(); ((MessageView) activity).onDecryptDone();
break; break;
@ -394,17 +396,18 @@ public class Apg extends CryptoProvider
* *
* @param activity * @param activity
* @param data * @param data
* @param pgpData
* @return success or failure * @return success or failure
*/ */
@Override @Override
public boolean encrypt(Activity activity, String data) public boolean encrypt(Activity activity, String data, PgpData pgpData)
{ {
android.content.Intent intent = new android.content.Intent(Intent.ENCRYPT_AND_RETURN); android.content.Intent intent = new android.content.Intent(Intent.ENCRYPT_AND_RETURN);
intent.putExtra(EXTRA_INTENT_VERSION, INTENT_VERSION); intent.putExtra(EXTRA_INTENT_VERSION, INTENT_VERSION);
intent.setType("text/plain"); intent.setType("text/plain");
intent.putExtra(Apg.EXTRA_TEXT, data); intent.putExtra(Apg.EXTRA_TEXT, data);
intent.putExtra(Apg.EXTRA_ENCRYPTION_KEY_IDS, mEncryptionKeyIds); intent.putExtra(Apg.EXTRA_ENCRYPTION_KEY_IDS, pgpData.getEncryptionKeys());
intent.putExtra(Apg.EXTRA_SIGNATURE_KEY_ID, mSignatureKeyId); intent.putExtra(Apg.EXTRA_SIGNATURE_KEY_ID, pgpData.getSignatureKeyId());
try try
{ {
activity.startActivityForResult(intent, Apg.ENCRYPT_MESSAGE); activity.startActivityForResult(intent, Apg.ENCRYPT_MESSAGE);
@ -424,10 +427,11 @@ public class Apg extends CryptoProvider
* *
* @param activity * @param activity
* @param data * @param data
* @param pgpData
* @return success or failure * @return success or failure
*/ */
@Override @Override
public boolean decrypt(Activity activity, String data) public boolean decrypt(Activity activity, String data, PgpData pgpData)
{ {
android.content.Intent intent = new android.content.Intent(Apg.Intent.DECRYPT_AND_RETURN); android.content.Intent intent = new android.content.Intent(Apg.Intent.DECRYPT_AND_RETURN);
intent.putExtra(EXTRA_INTENT_VERSION, INTENT_VERSION); intent.putExtra(EXTRA_INTENT_VERSION, INTENT_VERSION);

View File

@ -1,12 +1,9 @@
package com.fsck.k9.crypto; package com.fsck.k9.crypto;
import java.io.Serializable;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import com.fsck.k9.Account;
import com.fsck.k9.mail.Message; import com.fsck.k9.mail.Message;
/** /**
@ -14,99 +11,31 @@ import com.fsck.k9.mail.Message;
* It currently also stores the results of such encryption or decryption. * It currently also stores the results of such encryption or decryption.
* TODO: separate the storage from the provider * TODO: separate the storage from the provider
*/ */
abstract public class CryptoProvider implements Serializable abstract public class CryptoProvider
{ {
static final long serialVersionUID = 0x21071234; static final long serialVersionUID = 0x21071234;
protected long mEncryptionKeyIds[] = null;
protected long mSignatureKeyId = 0;
protected String mSignatureUserId = null;
protected boolean mSignatureSuccess = false;
protected boolean mSignatureUnknown = false;
protected String mDecryptedData = null;
protected String mEncryptedData = null;
abstract public boolean isAvailable(Context context); abstract public boolean isAvailable(Context context);
abstract public boolean isEncrypted(Message message); abstract public boolean isEncrypted(Message message);
abstract public boolean isSigned(Message message); abstract public boolean isSigned(Message message);
abstract public boolean onActivityResult(Activity activity, int requestCode, int resultCode, abstract public boolean onActivityResult(Activity activity, int requestCode, int resultCode,
Intent data); Intent data, PgpData pgpData);
abstract public boolean selectSecretKey(Activity activity); abstract public boolean selectSecretKey(Activity activity, PgpData pgpData);
abstract public boolean selectEncryptionKeys(Activity activity, String emails); abstract public boolean selectEncryptionKeys(Activity activity, String emails, PgpData pgpData);
abstract public boolean encrypt(Activity activity, String data); abstract public boolean encrypt(Activity activity, String data, PgpData pgpData);
abstract public boolean decrypt(Activity activity, String data); abstract public boolean decrypt(Activity activity, String data, PgpData pgpData);
abstract public long[] getSecretKeyIdsFromEmail(Context context, String email); abstract public long[] getSecretKeyIdsFromEmail(Context context, String email);
abstract public String getUserId(Context context, long keyId); abstract public String getUserId(Context context, long keyId);
abstract public String getName(); abstract public String getName();
abstract public boolean test(Context context); abstract public boolean test(Context context);
public static CryptoProvider createInstance(Account account) public static CryptoProvider createInstance(String name)
{ {
if (Apg.NAME.equals(account.getCryptoApp())) if (Apg.NAME.equals(name))
{ {
return Apg.createInstance(account); return Apg.createInstance();
} }
return None.createInstance(account); return None.createInstance();
}
public void setSignatureKeyId(long keyId)
{
mSignatureKeyId = keyId;
}
public long getSignatureKeyId()
{
return mSignatureKeyId;
}
public void setEncryptionKeys(long keyIds[])
{
mEncryptionKeyIds = keyIds;
}
public long[] getEncryptionKeys()
{
return mEncryptionKeyIds;
}
public boolean hasSignatureKey()
{
return mSignatureKeyId != 0;
}
public boolean hasEncryptionKeys()
{
return (mEncryptionKeyIds != null) && (mEncryptionKeyIds.length > 0);
}
public String getEncryptedData()
{
return mEncryptedData;
}
public String getDecryptedData()
{
return mDecryptedData;
}
public void setSignatureUserId(String userId)
{
mSignatureUserId = userId;
}
public String getSignatureUserId()
{
return mSignatureUserId;
}
public boolean getSignatureSuccess()
{
return mSignatureSuccess;
}
public boolean getSignatureUnknown()
{
return mSignatureUnknown;
} }
} }

View File

@ -15,7 +15,7 @@ public class None extends CryptoProvider
static final long serialVersionUID = 0x21071230; static final long serialVersionUID = 0x21071230;
public static final String NAME = ""; public static final String NAME = "";
public static None createInstance(Account account) public static None createInstance()
{ {
return new None(); return new None();
} }
@ -27,13 +27,13 @@ public class None extends CryptoProvider
} }
@Override @Override
public boolean selectSecretKey(Activity activity) public boolean selectSecretKey(Activity activity, PgpData pgpData)
{ {
return false; return false;
} }
@Override @Override
public boolean selectEncryptionKeys(Activity activity, String emails) public boolean selectEncryptionKeys(Activity activity, String emails, PgpData pgpData)
{ {
return false; return false;
} }
@ -52,19 +52,19 @@ public class None extends CryptoProvider
@Override @Override
public boolean onActivityResult(Activity activity, int requestCode, int resultCode, public boolean onActivityResult(Activity activity, int requestCode, int resultCode,
android.content.Intent data) android.content.Intent data, PgpData pgpData)
{ {
return false; return false;
} }
@Override @Override
public boolean encrypt(Activity activity, String data) public boolean encrypt(Activity activity, String data, PgpData pgpData)
{ {
return false; return false;
} }
@Override @Override
public boolean decrypt(Activity activity, String data) public boolean decrypt(Activity activity, String data, PgpData pgpData)
{ {
return false; return false;
} }

View File

@ -0,0 +1,93 @@
package com.fsck.k9.crypto;
import java.io.Serializable;
public class PgpData implements Serializable {
protected long mEncryptionKeyIds[] = null;
protected long mSignatureKeyId = 0;
protected String mSignatureUserId = null;
protected boolean mSignatureSuccess = false;
protected boolean mSignatureUnknown = false;
protected String mDecryptedData = null;
protected String mEncryptedData = null;
public void setSignatureKeyId(long keyId)
{
mSignatureKeyId = keyId;
}
public long getSignatureKeyId()
{
return mSignatureKeyId;
}
public void setEncryptionKeys(long keyIds[])
{
mEncryptionKeyIds = keyIds;
}
public long[] getEncryptionKeys()
{
return mEncryptionKeyIds;
}
public boolean hasSignatureKey()
{
return mSignatureKeyId != 0;
}
public boolean hasEncryptionKeys()
{
return (mEncryptionKeyIds != null) && (mEncryptionKeyIds.length > 0);
}
public String getEncryptedData()
{
return mEncryptedData;
}
public void setEncryptedData(String data)
{
mEncryptedData = data;
}
public String getDecryptedData()
{
return mDecryptedData;
}
public void setDecryptedData(String data)
{
mDecryptedData = data;
}
public void setSignatureUserId(String userId)
{
mSignatureUserId = userId;
}
public String getSignatureUserId()
{
return mSignatureUserId;
}
public boolean getSignatureSuccess()
{
return mSignatureSuccess;
}
public void setSignatureSuccess(boolean success)
{
mSignatureSuccess = success;
}
public boolean getSignatureUnknown()
{
return mSignatureUnknown;
}
public void setSignatureUnknown(boolean unknown)
{
mSignatureUnknown = unknown;
}
}