Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d98f579b authored by Vincent Breitmoser's avatar Vincent Breitmoser Committed by Vincent Breitmoser
Browse files

messageview: move some logic into MessageCryptoPresenter, refresh after key import (solves #732)

parent 4da1a011
Loading
Loading
Loading
Loading
+86 −0
Original line number Diff line number Diff line
package com.fsck.k9.ui.messageview;


import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentSender;
import android.util.Log;

import com.fsck.k9.K9;
import com.fsck.k9.mailstore.MessageViewInfo;
import com.fsck.k9.view.MessageCryptoDisplayStatus;


public class MessageCryptoPresenter implements OnCryptoClickListener {
    public static final int REQUEST_CODE_UNKNOWN_KEY = 123;


    MessageCryptoMvpView messageCryptoMvpView;

    private MessageViewInfo messageViewInfo;


    public MessageCryptoPresenter(MessageCryptoMvpView messageCryptoMvpView) {
        this.messageCryptoMvpView = messageCryptoMvpView;
    }

    public void setMessageViewInfo(MessageViewInfo messageViewInfo) {
        this.messageViewInfo = messageViewInfo;
    }

    @Override
    public void onCryptoClick() {
        if (messageViewInfo == null) {
            return;
        }
        MessageCryptoDisplayStatus displayStatus =
                MessageCryptoDisplayStatus.fromResultAnnotation(messageViewInfo.cryptoResultAnnotation);
        switch (displayStatus) {
            case UNENCRYPTED_SIGN_UNKNOWN:
                launchPendingIntent(messageViewInfo);
                break;
            default:
                displaySignatureInfoDialog(displayStatus);
                break;
        }
    }

    @SuppressWarnings("UnusedParameters") // for consistency with Activity.onActivityResult
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode != REQUEST_CODE_UNKNOWN_KEY) {
            throw new IllegalStateException("got an activity result that wasn't meant for us. this is a bug!");
        }

        if (resultCode != Activity.RESULT_OK) {
            return;
        }

        messageCryptoMvpView.restartMessageCryptoProcessing();
    }

    private void displaySignatureInfoDialog(MessageCryptoDisplayStatus displayStatus) {
        messageCryptoMvpView.showCryptoInfoDialog(displayStatus);
    }

    private void launchPendingIntent(MessageViewInfo messageViewInfo) {
        try {
            PendingIntent pendingIntent = messageViewInfo.cryptoResultAnnotation.getOpenPgpPendingIntent();
            if (pendingIntent != null) {
                messageCryptoMvpView.startPendingIntentForCryptoPresenter(pendingIntent.getIntentSender(),
                        REQUEST_CODE_UNKNOWN_KEY, null, 0, 0, 0);
            }
        } catch (IntentSender.SendIntentException e) {
            Log.e(K9.LOG_TAG, "SendIntentException", e);
        }
    }

    public interface MessageCryptoMvpView {
        void restartMessageCryptoProcessing();

        void startPendingIntentForCryptoPresenter(IntentSender si, int requestCode, Intent fillIntent,
                int flagsMask, int flagValues, int extraFlags) throws IntentSender.SendIntentException;

        void showCryptoInfoDialog(MessageCryptoDisplayStatus displayStatus);
    }
}
+5 −2
Original line number Diff line number Diff line
@@ -36,6 +36,8 @@ public class MessageTopView extends LinearLayout implements ShowPicturesControll
    private Button showPicturesButton;
    private List<MessageContainerView> messageContainerViewsWithPictures = new ArrayList<MessageContainerView>();

    private MessageCryptoPresenter messageCryptoPresenter;


    public MessageTopView(Context context, AttributeSet attrs) {
        super(context, attrs);
@@ -147,8 +149,9 @@ public class MessageTopView extends LinearLayout implements ShowPicturesControll
        attachmentCallback = callback;
    }

    public void setOnCryptoClickListener(OnCryptoClickListener listener) {
        mHeaderContainer.setOnCryptoClickListener(listener);
    public void setMessageCryptoPresenter(MessageCryptoPresenter messageCryptoPresenter) {
        this.messageCryptoPresenter = messageCryptoPresenter;
        mHeaderContainer.setOnCryptoClickListener(messageCryptoPresenter);
    }

    public void enableDownloadButton() {
+27 −9
Original line number Diff line number Diff line
@@ -53,11 +53,12 @@ import com.fsck.k9.ui.crypto.MessageCryptoCallback;
import com.fsck.k9.ui.crypto.MessageCryptoHelper;
import com.fsck.k9.ui.message.LocalMessageExtractorLoader;
import com.fsck.k9.ui.message.LocalMessageLoader;
import com.fsck.k9.ui.messageview.MessageCryptoPresenter.MessageCryptoMvpView;
import com.fsck.k9.view.MessageCryptoDisplayStatus;
import com.fsck.k9.view.MessageHeader;

public class MessageViewFragment extends Fragment implements ConfirmationDialogFragmentListener,
        AttachmentViewCallback, OnCryptoClickListener, MessageCryptoCallback {
        AttachmentViewCallback, MessageCryptoCallback, MessageCryptoMvpView {

    private static final String ARG_REFERENCE = "reference";

@@ -69,6 +70,7 @@ public class MessageViewFragment extends Fragment implements ConfirmationDialogF
    private static final int DECODE_MESSAGE_LOADER_ID = 2;

    public static final int REQUEST_MASK_CRYPTO_HELPER = 1 << 8;
    public static final int REQUEST_MASK_CRYPTO_PRESENTER = 2 << 8;

    public static MessageViewFragment newInstance(MessageReference reference) {
        MessageViewFragment fragment = new MessageViewFragment();
@@ -90,6 +92,7 @@ public class MessageViewFragment extends Fragment implements ConfirmationDialogF
    private Handler handler = new Handler();
    private DownloadMessageListener downloadMessageListener = new DownloadMessageListener();
    private MessageCryptoHelper messageCryptoHelper;
    private MessageCryptoPresenter messageCryptoPresenter = new MessageCryptoPresenter(this);

    /**
     * Used to temporarily store the destination folder for refile operations if a confirmation
@@ -110,7 +113,6 @@ public class MessageViewFragment extends Fragment implements ConfirmationDialogF

    private LoaderCallbacks<LocalMessage> localMessageLoaderCallback = new LocalMessageLoaderCallback();
    private LoaderCallbacks<MessageViewInfo> decodeMessageLoaderCallback = new DecodeMessageLoaderCallback();
    private MessageViewInfo messageViewInfo;
    private AttachmentViewInfo currentAttachmentViewInfo;

    @Override
@@ -150,7 +152,7 @@ public class MessageViewFragment extends Fragment implements ConfirmationDialogF

        mMessageView = (MessageTopView) view.findViewById(R.id.message_view);
        mMessageView.setAttachmentCallback(this);
        mMessageView.setOnCryptoClickListener(this);
        mMessageView.setMessageCryptoPresenter(messageCryptoPresenter);

        mMessageView.setOnToggleFlagClickListener(new OnClickListener() {
            @Override
@@ -198,7 +200,6 @@ public class MessageViewFragment extends Fragment implements ConfirmationDialogF
    }



    public void onPendingIntentResult(int requestCode, int resultCode, Intent data) {
        if ((requestCode & REQUEST_MASK_CRYPTO_HELPER) == REQUEST_MASK_CRYPTO_HELPER) {
            hideKeyboard();
@@ -207,6 +208,11 @@ public class MessageViewFragment extends Fragment implements ConfirmationDialogF
            messageCryptoHelper.onActivityResult(requestCode, resultCode, data);
            return;
        }

        if ((requestCode & REQUEST_MASK_CRYPTO_PRESENTER) == REQUEST_MASK_CRYPTO_PRESENTER) {
            requestCode ^= REQUEST_MASK_CRYPTO_PRESENTER;
            messageCryptoPresenter.onActivityResult(requestCode, resultCode, data);
        }
    }

    private void hideKeyboard() {
@@ -277,7 +283,7 @@ public class MessageViewFragment extends Fragment implements ConfirmationDialogF

    private void startExtractingTextAndAttachments(MessageCryptoAnnotations annotations) {
        this.messageAnnotations = annotations;
        getLoaderManager().initLoader(DECODE_MESSAGE_LOADER_ID, null, decodeMessageLoaderCallback);
        getLoaderManager().restartLoader(DECODE_MESSAGE_LOADER_ID, null, decodeMessageLoaderCallback);
    }

    private void onDecodeMessageFinished(MessageViewInfo messageViewInfo) {
@@ -286,7 +292,6 @@ public class MessageViewFragment extends Fragment implements ConfirmationDialogF
            messageViewInfo = MessageViewInfo.createWithErrorState(mMessage);
        }

        this.messageViewInfo = messageViewInfo;
        showMessage(messageViewInfo);
    }

@@ -296,6 +301,7 @@ public class MessageViewFragment extends Fragment implements ConfirmationDialogF
    }

    private void showMessage(MessageViewInfo messageViewInfo) {
        messageCryptoPresenter.setMessageViewInfo(messageViewInfo);
        try {
            mMessageView.setMessage(mAccount, messageViewInfo);
            mMessageView.setShowDownloadButton(mMessage);
@@ -453,6 +459,8 @@ public class MessageViewFragment extends Fragment implements ConfirmationDialogF
            return;
        }

        messageCryptoPresenter.onActivityResult(requestCode, resultCode, data);

        switch (requestCode) {
            case ACTIVITY_CHOOSE_DIRECTORY: {
                if (data != null) {
@@ -711,14 +719,24 @@ public class MessageViewFragment extends Fragment implements ConfirmationDialogF
    }

    @Override
    public void onCryptoClick() {
        MessageCryptoDisplayStatus displayStatus =
                MessageCryptoDisplayStatus.fromResultAnnotation(messageViewInfo.cryptoResultAnnotation);
    public void startPendingIntentForCryptoPresenter(IntentSender si, int requestCode, Intent fillIntent,
            int flagsMask, int flagValues, int extraFlags) throws SendIntentException {
        requestCode |= REQUEST_MASK_CRYPTO_PRESENTER;
        getActivity().startIntentSenderForResult(
                si, requestCode, fillIntent, flagsMask, flagValues, extraFlags);
    }

    @Override
    public void showCryptoInfoDialog(MessageCryptoDisplayStatus displayStatus) {
        CryptoInfoDialog dialog = CryptoInfoDialog.newInstance(displayStatus);
        dialog.show(getFragmentManager(), "crypto_info_dialog");
    }

    @Override
    public void restartMessageCryptoProcessing() {
        messageCryptoHelper.decryptOrVerifyMessagePartsIfNecessary(mMessage);
    }

    @Override
    public void startPendingIntentForCryptoHelper(IntentSender si, int requestCode, Intent fillIntent,
            int flagsMask, int flagValues, int extraFlags) throws SendIntentException {