messageview: refactor OpenPgpResultAnnotation into immutable CryptoResultAnnotation

This commit is contained in:
Vincent Breitmoser 2016-04-19 15:38:53 +02:00
parent e002451296
commit 8de494412e
9 changed files with 143 additions and 132 deletions

View file

@ -0,0 +1,98 @@
package com.fsck.k9.mailstore;
import android.app.PendingIntent;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import com.fsck.k9.mail.internet.MimeBodyPart;
import org.openintents.openpgp.OpenPgpDecryptionResult;
import org.openintents.openpgp.OpenPgpError;
import org.openintents.openpgp.OpenPgpSignatureResult;
public final class CryptoResultAnnotation {
@NonNull private final CryptoError errorType;
private final MimeBodyPart replacementData;
private final OpenPgpDecryptionResult openPgpDecryptionResult;
private final OpenPgpSignatureResult openPgpSignatureResult;
private final OpenPgpError openPgpError;
private final PendingIntent openPgpPendingIntent;
private CryptoResultAnnotation(@NonNull CryptoError errorType, MimeBodyPart replacementData,
OpenPgpDecryptionResult openPgpDecryptionResult,
OpenPgpSignatureResult openPgpSignatureResult,
PendingIntent openPgpPendingIntent, OpenPgpError openPgpError) {
this.errorType = errorType;
this.replacementData = replacementData;
this.openPgpDecryptionResult = openPgpDecryptionResult;
this.openPgpSignatureResult = openPgpSignatureResult;
this.openPgpPendingIntent = openPgpPendingIntent;
this.openPgpError = openPgpError;
}
public static CryptoResultAnnotation createOpenPgpResultAnnotation(OpenPgpDecryptionResult decryptionResult,
OpenPgpSignatureResult signatureResult, PendingIntent pendingIntent, MimeBodyPart replacementPart) {
return new CryptoResultAnnotation(CryptoError.NONE, replacementPart,
decryptionResult, signatureResult, pendingIntent, null);
}
public static CryptoResultAnnotation createErrorAnnotation(CryptoError error, MimeBodyPart replacementData) {
if (error == CryptoError.NONE) {
throw new AssertionError("CryptoError must be actual error state!");
}
return new CryptoResultAnnotation(error, replacementData, null, null, null, null);
}
public static CryptoResultAnnotation createOpenPgpErrorAnnotation(OpenPgpError error) {
return new CryptoResultAnnotation(CryptoError.OPENPGP_API_RETURNED_ERROR, null, null, null, null, error);
}
@Nullable
public OpenPgpDecryptionResult getOpenPgpDecryptionResult() {
return openPgpDecryptionResult;
}
@Nullable
public OpenPgpSignatureResult getOpenPgpSignatureResult() {
return openPgpSignatureResult;
}
@Nullable
public PendingIntent getOpenPgpPendingIntent() {
return openPgpPendingIntent;
}
@Nullable
public OpenPgpError getOpenPgpError() {
return openPgpError;
}
@NonNull
public CryptoError getErrorType() {
return errorType;
}
public boolean hasReplacementData() {
return replacementData != null;
}
@Nullable
public MimeBodyPart getReplacementData() {
return replacementData;
}
public enum CryptoError {
NONE,
OPENPGP_API_RETURNED_ERROR,
SIGNED_BUT_INCOMPLETE,
ENCRYPTED_BUT_INCOMPLETE,
SIGNED_BUT_UNSUPPORTED,
ENCRYPTED_BUT_UNSUPPORTED,
}
}

View file

@ -22,11 +22,11 @@ public class MessageViewInfo {
public final String text;
public final Part rootPart;
public final List<AttachmentViewInfo> attachments;
public final OpenPgpResultAnnotation cryptoAnnotation;
public final CryptoResultAnnotation cryptoAnnotation;
MessageViewContainer(String text, Part rootPart, List<AttachmentViewInfo> attachments,
OpenPgpResultAnnotation cryptoAnnotation) {
CryptoResultAnnotation cryptoAnnotation) {
this.text = text;
this.rootPart = rootPart;
this.attachments = attachments;

View file

@ -39,7 +39,7 @@ public class MessageViewInfoExtractor {
private static final int FILENAME_PREFIX_LENGTH = FILENAME_PREFIX.length();
private static final String FILENAME_SUFFIX = " ";
private static final int FILENAME_SUFFIX_LENGTH = FILENAME_SUFFIX.length();
private static final OpenPgpResultAnnotation NO_ANNOTATIONS = null;
private static final CryptoResultAnnotation NO_ANNOTATIONS = null;
private MessageViewInfoExtractor() {}
@ -53,11 +53,11 @@ public class MessageViewInfoExtractor {
// 2. extract viewables/attachments of parts
ArrayList<MessageViewContainer> containers = new ArrayList<>();
for (Part part : parts) {
OpenPgpResultAnnotation pgpAnnotation = annotations.get(part);
CryptoResultAnnotation pgpAnnotation = annotations.get(part);
// TODO properly handle decrypted data part - this just replaces the part
if (pgpAnnotation != NO_ANNOTATIONS && pgpAnnotation.hasOutputData()) {
part = pgpAnnotation.getOutputData();
if (pgpAnnotation != NO_ANNOTATIONS && pgpAnnotation.hasReplacementData()) {
part = pgpAnnotation.getReplacementData();
}
ArrayList<Viewable> viewableParts = new ArrayList<>();

View file

@ -1,81 +0,0 @@
package com.fsck.k9.mailstore;
import android.app.PendingIntent;
import com.fsck.k9.mail.internet.MimeBodyPart;
import org.openintents.openpgp.OpenPgpDecryptionResult;
import org.openintents.openpgp.OpenPgpError;
import org.openintents.openpgp.OpenPgpSignatureResult;
public final class OpenPgpResultAnnotation {
private OpenPgpDecryptionResult decryptionResult;
private OpenPgpSignatureResult signatureResult;
private OpenPgpError error;
private CryptoError errorType = CryptoError.NONE;
private PendingIntent pendingIntent;
private MimeBodyPart outputData;
public OpenPgpDecryptionResult getDecryptionResult() {
return decryptionResult;
}
public void setDecryptionResult(OpenPgpDecryptionResult decryptionResult) {
this.decryptionResult = decryptionResult;
}
public OpenPgpSignatureResult getSignatureResult() {
return signatureResult;
}
public void setSignatureResult(OpenPgpSignatureResult signatureResult) {
this.signatureResult = signatureResult;
}
public PendingIntent getPendingIntent() {
return pendingIntent;
}
public void setPendingIntent(PendingIntent pendingIntent) {
this.pendingIntent = pendingIntent;
}
public OpenPgpError getError() {
return error;
}
public void setError(OpenPgpError error) {
this.error = error;
setErrorType(CryptoError.CRYPTO_API_RETURNED_ERROR);
}
public CryptoError getErrorType() {
return errorType;
}
public void setErrorType(CryptoError errorType) {
this.errorType = errorType;
}
public boolean hasOutputData() {
return outputData != null;
}
public void setOutputData(MimeBodyPart outputData) {
this.outputData = outputData;
}
public MimeBodyPart getOutputData() {
return outputData;
}
public enum CryptoError {
NONE,
CRYPTO_API_RETURNED_ERROR,
SIGNED_BUT_INCOMPLETE,
ENCRYPTED_BUT_INCOMPLETE
}
}

View file

@ -4,21 +4,21 @@ package com.fsck.k9.ui.crypto;
import java.util.HashMap;
import com.fsck.k9.mail.Part;
import com.fsck.k9.mailstore.OpenPgpResultAnnotation;
import com.fsck.k9.mailstore.CryptoResultAnnotation;
public class MessageCryptoAnnotations {
private HashMap<Part, OpenPgpResultAnnotation> annotations = new HashMap<Part, OpenPgpResultAnnotation>();
private HashMap<Part, CryptoResultAnnotation> annotations = new HashMap<Part, CryptoResultAnnotation>();
MessageCryptoAnnotations() {
// Package-private constructor
}
void put(Part part, OpenPgpResultAnnotation annotation) {
void put(Part part, CryptoResultAnnotation annotation) {
annotations.put(part, annotation);
}
public OpenPgpResultAnnotation get(Part part) {
public CryptoResultAnnotation get(Part part) {
return annotations.get(part);
}

View file

@ -33,9 +33,8 @@ import com.fsck.k9.mail.internet.TextBody;
import com.fsck.k9.mailstore.DecryptStreamParser;
import com.fsck.k9.mailstore.LocalMessage;
import com.fsck.k9.mailstore.MessageHelper;
import com.fsck.k9.mailstore.OpenPgpResultAnnotation;
import com.fsck.k9.mailstore.OpenPgpResultAnnotation.CryptoError;
import org.openintents.openpgp.IOpenPgpService;
import com.fsck.k9.mailstore.CryptoResultAnnotation;
import com.fsck.k9.mailstore.CryptoResultAnnotation.CryptoError;
import org.openintents.openpgp.IOpenPgpService2;
import org.openintents.openpgp.OpenPgpDecryptionResult;
import org.openintents.openpgp.OpenPgpError;
@ -58,7 +57,7 @@ public class MessageCryptoHelper {
private final Account account;
private LocalMessage message;
private Deque<CryptoPart> partsToDecryptOrVerify = new ArrayDeque<CryptoPart>();
private Deque<CryptoPart> partsToDecryptOrVerify = new ArrayDeque<>();
private OpenPgpApi openPgpApi;
private CryptoPart currentCryptoPart;
private Intent currentCryptoResult;
@ -109,10 +108,8 @@ public class MessageCryptoHelper {
}
}
private void addErrorAnnotation(Part part, CryptoError error, MimeBodyPart outputData) {
OpenPgpResultAnnotation annotation = new OpenPgpResultAnnotation();
annotation.setErrorType(error);
annotation.setOutputData(outputData);
private void addErrorAnnotation(Part part, CryptoError error, MimeBodyPart replacementPart) {
CryptoResultAnnotation annotation = CryptoResultAnnotation.createErrorAnnotation(error, replacementPart);
messageAnnotations.put(part, annotation);
}
@ -411,11 +408,8 @@ public class MessageCryptoHelper {
currentCryptoResult.getParcelableExtra(OpenPgpApi.RESULT_SIGNATURE);
PendingIntent pendingIntent = currentCryptoResult.getParcelableExtra(OpenPgpApi.RESULT_INTENT);
OpenPgpResultAnnotation resultAnnotation = new OpenPgpResultAnnotation();
resultAnnotation.setOutputData(outputPart);
resultAnnotation.setDecryptionResult(decryptionResult);
resultAnnotation.setSignatureResult(signatureResult);
resultAnnotation.setPendingIntent(pendingIntent);
CryptoResultAnnotation resultAnnotation = CryptoResultAnnotation.createOpenPgpResultAnnotation(
decryptionResult, signatureResult, pendingIntent, outputPart);
onCryptoSuccess(resultAnnotation);
}
@ -433,23 +427,22 @@ public class MessageCryptoHelper {
}
}
private void onCryptoSuccess(OpenPgpResultAnnotation resultAnnotation) {
addOpenPgpResultPartToMessage(resultAnnotation);
private void onCryptoSuccess(CryptoResultAnnotation resultAnnotation) {
addCryptoResultAnnotationToMessage(resultAnnotation);
onCryptoFinished();
}
private void addOpenPgpResultPartToMessage(OpenPgpResultAnnotation resultAnnotation) {
Part part = currentCryptoPart.part;
messageAnnotations.put(part, resultAnnotation);
}
private void onCryptoFailed(OpenPgpError error) {
OpenPgpResultAnnotation errorPart = new OpenPgpResultAnnotation();
errorPart.setError(error);
addOpenPgpResultPartToMessage(errorPart);
CryptoResultAnnotation errorPart = CryptoResultAnnotation.createOpenPgpErrorAnnotation(error);
addCryptoResultAnnotationToMessage(errorPart);
onCryptoFinished();
}
private void addCryptoResultAnnotationToMessage(CryptoResultAnnotation resultAnnotation) {
Part part = currentCryptoPart.part;
messageAnnotations.put(part, resultAnnotation);
}
private void onCryptoFinished() {
partsToDecryptOrVerify.removeFirst();
decryptOrVerifyNextPart();

View file

@ -38,8 +38,8 @@ import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mailstore.AttachmentViewInfo;
import com.fsck.k9.mailstore.MessageViewInfo.MessageViewContainer;
import com.fsck.k9.mailstore.OpenPgpResultAnnotation;
import com.fsck.k9.mailstore.OpenPgpResultAnnotation.CryptoError;
import com.fsck.k9.mailstore.CryptoResultAnnotation;
import com.fsck.k9.mailstore.CryptoResultAnnotation.CryptoError;
import com.fsck.k9.view.K9WebViewClient;
import com.fsck.k9.view.MessageHeader.OnLayoutChangedListener;
import com.fsck.k9.view.MessageWebView;
@ -443,7 +443,7 @@ public class MessageContainerView extends LinearLayout implements OnClickListene
ViewStub openPgpHeaderStub = (ViewStub) findViewById(R.id.openpgp_header_stub);
OpenPgpHeaderView openPgpHeaderView = (OpenPgpHeaderView) openPgpHeaderStub.inflate();
OpenPgpResultAnnotation cryptoAnnotation = messageViewContainer.cryptoAnnotation;
CryptoResultAnnotation cryptoAnnotation = messageViewContainer.cryptoAnnotation;
openPgpHeaderView.setOpenPgpData(cryptoAnnotation);
openPgpHeaderView.setCallback(openPgpHeaderViewCallback);
mSidebar.setVisibility(View.VISIBLE);
@ -462,16 +462,16 @@ public class MessageContainerView extends LinearLayout implements OnClickListene
}
private String getTextToDisplay(MessageViewContainer messageViewContainer) {
OpenPgpResultAnnotation cryptoAnnotation = messageViewContainer.cryptoAnnotation;
CryptoResultAnnotation cryptoAnnotation = messageViewContainer.cryptoAnnotation;
if (cryptoAnnotation == null) {
return messageViewContainer.text;
}
CryptoError errorType = cryptoAnnotation.getErrorType();
switch (errorType) {
case CRYPTO_API_RETURNED_ERROR: {
case OPENPGP_API_RETURNED_ERROR: {
// TODO make a nice view for this
return wrapStatusMessage(cryptoAnnotation.getError().getMessage());
return wrapStatusMessage(cryptoAnnotation.getOpenPgpError().getMessage());
}
case ENCRYPTED_BUT_INCOMPLETE: {
return wrapStatusMessage(getContext().getString(R.string.crypto_download_complete_message_to_decrypt));

View file

@ -17,7 +17,7 @@ import android.widget.LinearLayout;
import android.widget.TextView;
import com.fsck.k9.R;
import com.fsck.k9.mailstore.OpenPgpResultAnnotation;
import com.fsck.k9.mailstore.CryptoResultAnnotation;
import org.openintents.openpgp.OpenPgpDecryptionResult;
import org.openintents.openpgp.OpenPgpError;
import org.openintents.openpgp.OpenPgpSignatureResult;
@ -28,7 +28,7 @@ public class OpenPgpHeaderView extends LinearLayout {
private Context context;
private OpenPgpHeaderViewCallback callback;
private OpenPgpResultAnnotation cryptoAnnotation;
private CryptoResultAnnotation cryptoAnnotation;
private ImageView resultEncryptionIcon;
private TextView resultEncryptionText;
@ -63,7 +63,7 @@ public class OpenPgpHeaderView extends LinearLayout {
this.callback = callback;
}
public void setOpenPgpData(OpenPgpResultAnnotation cryptoAnnotation) {
public void setOpenPgpData(CryptoResultAnnotation cryptoAnnotation) {
this.cryptoAnnotation = cryptoAnnotation;
initializeEncryptionHeader();
@ -78,7 +78,7 @@ public class OpenPgpHeaderView extends LinearLayout {
switch (cryptoAnnotation.getErrorType()) {
case NONE: {
OpenPgpDecryptionResult decryptionResult = cryptoAnnotation.getDecryptionResult();
OpenPgpDecryptionResult decryptionResult = cryptoAnnotation.getOpenPgpDecryptionResult();
switch (decryptionResult.getResult()) {
case OpenPgpDecryptionResult.RESULT_NOT_ENCRYPTED: {
displayNotEncrypted();
@ -97,7 +97,7 @@ public class OpenPgpHeaderView extends LinearLayout {
}
break;
}
case CRYPTO_API_RETURNED_ERROR: {
case OPENPGP_API_RETURNED_ERROR: {
displayEncryptionError();
break;
}
@ -134,7 +134,7 @@ public class OpenPgpHeaderView extends LinearLayout {
private void displayEncryptionError() {
setEncryptionImageAndTextColor(CryptoState.INVALID);
OpenPgpError error = cryptoAnnotation.getError();
OpenPgpError error = cryptoAnnotation.getOpenPgpError();
String text;
if (error == null) {
text = context.getString(R.string.openpgp_unknown_error);
@ -158,7 +158,7 @@ public class OpenPgpHeaderView extends LinearLayout {
}
switch (cryptoAnnotation.getErrorType()) {
case CRYPTO_API_RETURNED_ERROR:
case OPENPGP_API_RETURNED_ERROR:
displayEncryptionError();
hideVerificationState();
break;
@ -187,7 +187,7 @@ public class OpenPgpHeaderView extends LinearLayout {
}
private void displayVerificationResult() {
OpenPgpSignatureResult signatureResult = cryptoAnnotation.getSignatureResult();
OpenPgpSignatureResult signatureResult = cryptoAnnotation.getOpenPgpSignatureResult();
switch (signatureResult.getResult()) {
case OpenPgpSignatureResult.RESULT_NO_SIGNATURE: {
@ -239,11 +239,11 @@ public class OpenPgpHeaderView extends LinearLayout {
}
private boolean isSignatureButtonUsed() {
return cryptoAnnotation.getPendingIntent() != null;
return cryptoAnnotation.getOpenPgpPendingIntent() != null;
}
private void setSignatureButtonClickListener() {
final PendingIntent pendingIntent = cryptoAnnotation.getPendingIntent();
final PendingIntent pendingIntent = cryptoAnnotation.getOpenPgpPendingIntent();
resultSignatureButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
@ -287,7 +287,7 @@ public class OpenPgpHeaderView extends LinearLayout {
setSignatureImageAndTextColor(CryptoState.UNKNOWN_KEY);
resultSignatureText.setText(R.string.openpgp_result_signature_missing_key);
setUserId(cryptoAnnotation.getSignatureResult());
setUserId(cryptoAnnotation.getOpenPgpSignatureResult());
showSignatureButtonWithTextIfNecessary(R.string.openpgp_result_action_lookup);
showSignatureLayout();
}
@ -321,7 +321,7 @@ public class OpenPgpHeaderView extends LinearLayout {
}
private void displayUserIdAndSignatureButton() {
setUserId(cryptoAnnotation.getSignatureResult());
setUserId(cryptoAnnotation.getOpenPgpSignatureResult());
showSignatureButtonWithTextIfNecessary(R.string.openpgp_result_action_show);
showSignatureLayout();
}

View file

@ -42,6 +42,7 @@ import com.fsck.k9.mail.Message;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.internet.MimeUtility;
public class MessageHeader extends LinearLayout implements OnClickListener, OnLongClickListener {
private Context mContext;
private TextView mFromView;