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

Commit e1015d32 authored by Vincent Breitmoser's avatar Vincent Breitmoser
Browse files

Load draft state from AutocryptDraftState header

parent 07b272a1
Loading
Loading
Loading
Loading
+0 −4
Original line number Diff line number Diff line
@@ -39,8 +39,4 @@ class AutocryptDraftStateHeaderParser internal constructor() {
        }
        return false
    }

    companion object {
        val instance = AutocryptDraftStateHeaderParser()
    }
}
+1 −0
Original line number Diff line number Diff line
@@ -4,4 +4,5 @@ import org.koin.dsl.module.applicationContext

val autocryptModule = applicationContext {
    bean { AutocryptTransferMessageCreator(get()) }
    bean { AutocryptDraftStateHeaderParser() }
}
 No newline at end of file
+6 −3
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@ import android.widget.Toast;

import com.fsck.k9.Account;
import com.fsck.k9.Account.MessageFormat;
import com.fsck.k9.DI;
import com.fsck.k9.Identity;
import com.fsck.k9.K9;
import com.fsck.k9.Preferences;
@@ -63,6 +64,7 @@ import com.fsck.k9.activity.compose.RecipientMvpView;
import com.fsck.k9.activity.compose.RecipientPresenter;
import com.fsck.k9.activity.compose.SaveMessageTask;
import com.fsck.k9.activity.misc.Attachment;
import com.fsck.k9.autocrypt.AutocryptDraftStateHeaderParser;
import com.fsck.k9.controller.MessageReference;
import com.fsck.k9.controller.MessagingController;
import com.fsck.k9.controller.MessagingListener;
@@ -284,11 +286,12 @@ public class MessageCompose extends K9Activity implements OnClickListener,
        RecipientMvpView recipientMvpView = new RecipientMvpView(this);
        ComposePgpInlineDecider composePgpInlineDecider = new ComposePgpInlineDecider();
        ComposePgpEnableByDefaultDecider composePgpEnableByDefaultDecider = new ComposePgpEnableByDefaultDecider();

        OpenPgpApiManager openPgpApiManager = new OpenPgpApiManager(getApplicationContext(), this);
        recipientPresenter = new RecipientPresenter(getApplicationContext(), getSupportLoaderManager(),
                openPgpApiManager, recipientMvpView, account, composePgpInlineDecider, composePgpEnableByDefaultDecider,
                AutocryptStatusInteractor.getInstance(), new ReplyToParser(), this
        );
                AutocryptStatusInteractor.getInstance(), new ReplyToParser(), this,
                DI.get(AutocryptDraftStateHeaderParser.class));
        recipientPresenter.asyncUpdateCryptoStatus();


@@ -1302,7 +1305,7 @@ public class MessageCompose extends K9Activity implements OnClickListener,
        draftId = MessagingController.getInstance(getApplication()).getId(message);
        subjectView.setText(messageViewInfo.subject);

        recipientPresenter.initFromDraftMessage(message);
        recipientPresenter.initFromDraftMessage(message, messageViewInfo.rootPart);

        // Read In-Reply-To header from draft
        final String[] inReplyTo = message.getHeader("In-Reply-To");
+36 −9
Original line number Diff line number Diff line
@@ -27,6 +27,8 @@ import com.fsck.k9.activity.compose.ComposeCryptoStatus.AttachErrorState;
import com.fsck.k9.activity.compose.ComposeCryptoStatus.ComposeCryptoStatusBuilder;
import com.fsck.k9.activity.compose.ComposeCryptoStatus.SendErrorState;
import com.fsck.k9.activity.compose.RecipientMvpView.CryptoStatusDisplayType;
import com.fsck.k9.autocrypt.AutocryptDraftStateHeader;
import com.fsck.k9.autocrypt.AutocryptDraftStateHeaderParser;
import com.fsck.k9.helper.Contacts;
import com.fsck.k9.helper.MailTo;
import com.fsck.k9.helper.ReplyToParser;
@@ -35,6 +37,7 @@ import com.fsck.k9.mail.Address;
import com.fsck.k9.mail.Flag;
import com.fsck.k9.mail.Message;
import com.fsck.k9.mail.Message.RecipientType;
import com.fsck.k9.mail.Part;
import com.fsck.k9.message.AutocryptStatusInteractor;
import com.fsck.k9.message.AutocryptStatusInteractor.RecipientAutocryptStatus;
import com.fsck.k9.message.ComposePgpEnableByDefaultDecider;
@@ -73,12 +76,13 @@ public class RecipientPresenter {
    private final ComposePgpInlineDecider composePgpInlineDecider;
    private final AutocryptStatusInteractor autocryptStatusInteractor;
    private final RecipientsChangedListener listener;
    private final OpenPgpApiManager openPgpApiManager;
    private final AutocryptDraftStateHeaderParser draftStateHeaderParser;
    private ReplyToParser replyToParser;
    private Account account;
    private Boolean hasContactPicker;
    @Nullable
    private ComposeCryptoStatus cachedCryptoStatus;
    private OpenPgpApiManager openPgpApiManager;


    // persistent state, saved during onSaveInstanceState
@@ -93,7 +97,8 @@ public class RecipientPresenter {
            ComposePgpInlineDecider composePgpInlineDecider,
            ComposePgpEnableByDefaultDecider composePgpEnableByDefaultDecider,
            AutocryptStatusInteractor autocryptStatusInteractor,
            ReplyToParser replyToParser, RecipientsChangedListener recipientsChangedListener) {
            ReplyToParser replyToParser, RecipientsChangedListener recipientsChangedListener,
            AutocryptDraftStateHeaderParser draftStateHeaderParser) {
        this.recipientMvpView = recipientMvpView;
        this.context = context;
        this.autocryptStatusInteractor = autocryptStatusInteractor;
@@ -102,6 +107,7 @@ public class RecipientPresenter {
        this.replyToParser = replyToParser;
        this.listener = recipientsChangedListener;
        this.openPgpApiManager = openPgpApiManager;
        this.draftStateHeaderParser = draftStateHeaderParser;

        recipientMvpView.setPresenter(this);
        recipientMvpView.setLoaderManager(loaderManager);
@@ -220,11 +226,24 @@ public class RecipientPresenter {
        outState.putBoolean(STATE_KEY_CRYPTO_ENABLE_PGP_INLINE, cryptoEnablePgpInline);
    }

    public void initFromDraftMessage(Message message) {
    public void initFromDraftMessage(Message message, Part rootPart) {
        initRecipientsFromDraftMessage(message);
        initEncryptionStatus(message);

        String[] draftStateHeader = rootPart.getHeader(AutocryptDraftStateHeader.AUTOCRYPT_DRAFT_STATE_HEADER);
        if (draftStateHeader.length == 1) {
            initEncryptionStateFromDraftStateHeader(draftStateHeader[0]);
        } else {
            initPgpInlineFromDraftMessage(message);
        }
    }

    private void initEncryptionStateFromDraftStateHeader(String headerValue) {
        AutocryptDraftStateHeader autocryptDraftStateHeader =
                draftStateHeaderParser.parseAutocryptDraftStateHeader(headerValue);
        if (autocryptDraftStateHeader != null) {
            initEncryptionStateFromDraftStateHeader(autocryptDraftStateHeader);
        }
    }

    private void initRecipientsFromDraftMessage(Message message) {
        addToAddresses(message.getRecipients(RecipientType.TO));
@@ -236,10 +255,18 @@ public class RecipientPresenter {
        addBccAddresses(bccRecipients);
    }

    private void initEncryptionStatus(Message message) {
        boolean isEncryptedDraft = composePgpEnableByDefaultDecider.shouldEncryptByDefault(message);
        if (isEncryptedDraft) {
            currentCryptoMode = CryptoMode.CHOICE_ENABLED;
    private void initEncryptionStateFromDraftStateHeader(AutocryptDraftStateHeader draftState) {
        cryptoEnablePgpInline = draftState.isPgpInline();
        isReplyToEncryptedMessage = draftState.isReply();
        if (!draftState.isByChoice()) {
            // TODO if it's not by choice, we're going with our defaults. should we do something here if those differ?
            return;
        }

        if (draftState.isSignOnly()) {
            currentCryptoMode = CryptoMode.SIGN_ONLY;
        } else {
            currentCryptoMode = draftState.isEncrypt() ? CryptoMode.CHOICE_ENABLED : CryptoMode.CHOICE_DISABLED;
        }
    }

+4 −1
Original line number Diff line number Diff line
@@ -8,10 +8,12 @@ import android.content.Context;
import android.support.v4.app.LoaderManager;

import com.fsck.k9.Account;
import com.fsck.k9.DI;
import com.fsck.k9.K9RobolectricTest;
import com.fsck.k9.activity.compose.RecipientMvpView.CryptoSpecialModeDisplayType;
import com.fsck.k9.activity.compose.RecipientMvpView.CryptoStatusDisplayType;
import com.fsck.k9.activity.compose.RecipientPresenter.CryptoMode;
import com.fsck.k9.autocrypt.AutocryptDraftStateHeaderParser;
import com.fsck.k9.helper.ReplyToParser;
import com.fsck.k9.helper.ReplyToParser.ReplyToAddresses;
import com.fsck.k9.mail.Address;
@@ -90,7 +92,8 @@ public class RecipientPresenterTest extends K9RobolectricTest {

        recipientPresenter = new RecipientPresenter(
                context, loaderManager, openPgpApiManager, recipientMvpView, account, composePgpInlineDecider,
                composePgpEnableByDefaultDecider, autocryptStatusInteractor, replyToParser, listener
                composePgpEnableByDefaultDecider, autocryptStatusInteractor, replyToParser, listener,
                DI.get(AutocryptDraftStateHeaderParser.class)
        );

        ArgumentCaptor<OpenPgpApiManagerCallback> callbackCaptor = ArgumentCaptor.forClass(OpenPgpApiManagerCallback.class);