2011-02-10 12:03:10 -05:00
|
|
|
package com.fsck.k9.view;
|
|
|
|
|
|
|
|
import android.app.Activity;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.util.Log;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
import android.widget.*;
|
|
|
|
import com.fsck.k9.K9;
|
|
|
|
import com.fsck.k9.R;
|
|
|
|
import com.fsck.k9.crypto.CryptoProvider;
|
|
|
|
import com.fsck.k9.crypto.PgpData;
|
|
|
|
import com.fsck.k9.mail.Message;
|
|
|
|
import com.fsck.k9.mail.MessagingException;
|
|
|
|
import com.fsck.k9.mail.Part;
|
|
|
|
import com.fsck.k9.mail.internet.MimeUtility;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class MessageCryptoView extends LinearLayout {
|
|
|
|
|
|
|
|
private Context mContext;
|
|
|
|
private Activity mActivity;
|
|
|
|
private Button mDecryptButton;
|
|
|
|
private LinearLayout mCryptoSignatureLayout = null;
|
|
|
|
private ImageView mCryptoSignatureStatusImage = null;
|
|
|
|
private TextView mCryptoSignatureUserId = null;
|
|
|
|
private TextView mCryptoSignatureUserIdRest = null;
|
|
|
|
|
|
|
|
|
|
|
|
public MessageCryptoView(Context context, AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
|
|
|
mContext = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setupChildViews() {
|
|
|
|
mCryptoSignatureLayout = (LinearLayout) findViewById(R.id.crypto_signature);
|
|
|
|
mCryptoSignatureStatusImage = (ImageView) findViewById(R.id.ic_crypto_signature_status);
|
|
|
|
mCryptoSignatureUserId = (TextView) findViewById(R.id.userId);
|
|
|
|
mCryptoSignatureUserIdRest = (TextView) findViewById(R.id.userIdRest);
|
|
|
|
mCryptoSignatureLayout.setVisibility(View.INVISIBLE);
|
|
|
|
mDecryptButton = (Button) findViewById(R.id.btn_decrypt);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setActivity(Activity activity) {
|
|
|
|
mActivity = activity;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-02-11 12:12:09 -05:00
|
|
|
|
2011-02-13 19:47:30 -05:00
|
|
|
public void hide() {
|
2011-02-11 12:12:09 -05:00
|
|
|
this.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
|
2011-02-10 12:03:10 -05:00
|
|
|
/**
|
|
|
|
* Fill the decrypt layout with signature data, if known, make controls visible, if
|
|
|
|
* they should be visible.
|
|
|
|
*/
|
2011-02-11 10:09:15 -05:00
|
|
|
public void updateLayout(final CryptoProvider cryptoProvider, final PgpData pgpData, final Message message) {
|
|
|
|
if (pgpData.getSignatureKeyId() != 0) {
|
2011-02-10 12:03:10 -05:00
|
|
|
mCryptoSignatureUserIdRest.setText(
|
2011-02-11 10:09:15 -05:00
|
|
|
mContext.getString(R.string.key_id, Long.toHexString(pgpData.getSignatureKeyId() & 0xffffffffL)));
|
|
|
|
String userId = pgpData.getSignatureUserId();
|
2011-02-10 12:03:10 -05:00
|
|
|
if (userId == null) {
|
|
|
|
userId = mContext.getString(R.string.unknown_crypto_signature_user_id);
|
|
|
|
}
|
|
|
|
String chunks[] = userId.split(" <", 2);
|
|
|
|
String name = chunks[0];
|
|
|
|
if (chunks.length > 1) {
|
|
|
|
mCryptoSignatureUserIdRest.setText("<" + chunks[1]);
|
|
|
|
}
|
|
|
|
mCryptoSignatureUserId.setText(name);
|
2011-02-11 10:09:15 -05:00
|
|
|
if (pgpData.getSignatureSuccess()) {
|
2011-02-10 12:03:10 -05:00
|
|
|
mCryptoSignatureStatusImage.setImageResource(R.drawable.overlay_ok);
|
2011-02-11 10:09:15 -05:00
|
|
|
} else if (pgpData.getSignatureUnknown()) {
|
2011-02-10 12:03:10 -05:00
|
|
|
mCryptoSignatureStatusImage.setImageResource(R.drawable.overlay_error);
|
|
|
|
} else {
|
|
|
|
mCryptoSignatureStatusImage.setImageResource(R.drawable.overlay_error);
|
|
|
|
}
|
|
|
|
mCryptoSignatureLayout.setVisibility(View.VISIBLE);
|
|
|
|
this.setVisibility(View.VISIBLE);
|
|
|
|
} else {
|
|
|
|
mCryptoSignatureLayout.setVisibility(View.INVISIBLE);
|
|
|
|
}
|
2011-02-11 10:09:15 -05:00
|
|
|
if ((message == null) && (pgpData.getDecryptedData() == null)) {
|
2011-02-10 12:03:10 -05:00
|
|
|
this.setVisibility(View.GONE);
|
|
|
|
return;
|
|
|
|
}
|
2011-02-11 10:09:15 -05:00
|
|
|
if (pgpData.getDecryptedData() != null) {
|
|
|
|
if (pgpData.getSignatureKeyId() == 0) {
|
2011-02-10 12:03:10 -05:00
|
|
|
this.setVisibility(View.GONE);
|
|
|
|
} else {
|
|
|
|
// no need to show this after decryption/verification
|
|
|
|
mDecryptButton.setVisibility(View.GONE);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
mDecryptButton.setOnClickListener(new OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
try {
|
|
|
|
String data = null;
|
|
|
|
Part part = MimeUtility.findFirstPartByMimeType(message, "text/plain");
|
|
|
|
if (part == null) {
|
|
|
|
part = MimeUtility.findFirstPartByMimeType(message, "text/html");
|
|
|
|
}
|
|
|
|
if (part != null) {
|
|
|
|
data = MimeUtility.getTextFromPart(part);
|
|
|
|
}
|
2011-02-11 10:09:15 -05:00
|
|
|
cryptoProvider.decrypt(mActivity, data, pgpData);
|
2011-02-10 12:03:10 -05:00
|
|
|
} catch (MessagingException me) {
|
|
|
|
Log.e(K9.LOG_TAG, "Unable to decrypt email.", me);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
mDecryptButton.setVisibility(View.VISIBLE);
|
2011-02-11 10:09:15 -05:00
|
|
|
if (cryptoProvider.isEncrypted(message)) {
|
2011-02-10 12:03:10 -05:00
|
|
|
mDecryptButton.setText(R.string.btn_decrypt);
|
|
|
|
this.setVisibility(View.VISIBLE);
|
2011-02-11 10:09:15 -05:00
|
|
|
} else if (cryptoProvider.isSigned(message)) {
|
2011-02-10 12:03:10 -05:00
|
|
|
mDecryptButton.setText(R.string.btn_verify);
|
|
|
|
this.setVisibility(View.VISIBLE);
|
|
|
|
} else {
|
|
|
|
this.setVisibility(View.GONE);
|
|
|
|
try {
|
|
|
|
// check for PGP/MIME encryption
|
|
|
|
Part pgp = MimeUtility.findFirstPartByMimeType(message, "application/pgp-encrypted");
|
|
|
|
if (pgp != null) {
|
|
|
|
Toast.makeText(mContext, R.string.pgp_mime_unsupported, Toast.LENGTH_LONG).show();
|
|
|
|
}
|
|
|
|
} catch (MessagingException e) {
|
|
|
|
// nothing to do...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|