<commit_before>package com.fsck.k9.ui.messageview;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.Log;

import com.fsck.k9.Account;
import com.fsck.k9.Identity;
import com.fsck.k9.K9;
import com.fsck.k9.activity.MessageList;
import com.fsck.k9.crypto.MessageDecryptVerifier;
import com.fsck.k9.crypto.OpenPgpApiHelper;
import com.fsck.k9.helper.IdentityHelper;
import com.fsck.k9.mail.Body;
import com.fsck.k9.mail.BodyPart;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.Multipart;
import com.fsck.k9.mail.Part;
import com.fsck.k9.mail.internet.MessageExtractor;
import com.fsck.k9.mail.internet.MimeBodyPart;
import com.fsck.k9.mail.internet.TextBody;
import com.fsck.k9.mailstore.DecryptStreamParser;
import com.fsck.k9.mailstore.LocalMessage;
import com.fsck.k9.mailstore.OpenPgpResultAnnotation;
import org.openintents.openpgp.IOpenPgpService;
import org.openintents.openpgp.OpenPgpError;
import org.openintents.openpgp.OpenPgpSignatureResult;
import org.openintents.openpgp.util.OpenPgpApi;
import org.openintents.openpgp.util.OpenPgpApi.IOpenPgpCallback;
import org.openintents.openpgp.util.OpenPgpServiceConnection;
import org.openintents.openpgp.util.OpenPgpServiceConnection.OnBound;


public class MessageCryptoHelper {

    private final Context context;
    private final MessageViewFragment fragment;
    private final Account account;
    private LocalMessage message;

    private Deque<Part> partsToDecryptOrVerify;
    private OpenPgpApi openPgpApi;
    private Part currentlyDecryptingOrVerifyingPart;
    private Intent currentCryptoResult;

    private MessageCryptoAnnotations messageAnnotations;

    private static final int INVALID_OPENPGP_RESULT_CODE = -1;

    public MessageCryptoHelper(Context context, MessageViewFragment fragment, Account account) {
        this.context = context;
        this.fragment = fragment;
        this.account = account;

        this.messageAnnotations = new MessageCryptoAnnotations();
    }

    public void decryptOrVerifyMessagePartsIfNecessary(LocalMessage message) {
        this.message = message;

        if (!isCryptoProviderConfigured()) {
            returnResultToFragment();
            return;
        }

        List<Part> encryptedParts = MessageDecryptVerifier.findEncryptedParts(message);
        List<Part> signedParts = MessageDecryptVerifier.findSignedParts(message);
        List<Part> inlineParts = MessageDecryptVerifier.findPgpInlineParts(message);
        if (!encryptedParts.isEmpty() || !signedParts.isEmpty() || !inlineParts.isEmpty()) {
            partsToDecryptOrVerify = new ArrayDeque<Part>();
            partsToDecryptOrVerify.addAll(encryptedParts);
            partsToDecryptOrVerify.addAll(signedParts);
            partsToDecryptOrVerify.addAll(inlineParts);
            decryptOrVerifyNextPartOrStartExtractingTextAndAttachments();
        } else {
            returnResultToFragment();
        }
    }

    private boolean isCryptoProviderConfigured() {
        return !TextUtils.isEmpty(account.getCryptoApp());
    }

    private void decryptOrVerifyNextPartOrStartExtractingTextAndAttachments() {
        if (!partsToDecryptOrVerify.isEmpty()) {

            Part part = partsToDecryptOrVerify.peekFirst();
            if ("text/plain".equalsIgnoreCase(part.getMimeType())) {
                startDecryptingOrVerifyingPart(part);
            } else if (MessageDecryptVerifier.isPgpMimePart(part)) {
                Multipart multipart = (Multipart) part.getBody();
                if (multipart == null) {
                    throw new RuntimeException("Downloading missing parts before decryption isn't supported yet");
                }

                startDecryptingOrVerifyingPart(part);
            } else {
                partsToDecryptOrVerify.removeFirst();
                decryptOrVerifyNextPartOrStartExtractingTextAndAttachments();
            }

            return;
        }

        returnResultToFragment();

    }

    private void startDecryptingOrVerifyingPart(Part part) {
        if (!isBoundToCryptoProviderService()) {
            connectToCryptoProviderService();
        } else {
            decryptOrVerifyPart(part);
        }
    }
<commit_msg>Simplify control flow<commit_after>package com.fsck.k9.ui.messageview;


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.Log;

import com.fsck.k9.Account;
import com.fsck.k9.Identity;
import com.fsck.k9.K9;
import com.fsck.k9.activity.MessageList;
import com.fsck.k9.crypto.MessageDecryptVerifier;
import com.fsck.k9.crypto.OpenPgpApiHelper;
import com.fsck.k9.helper.IdentityHelper;
import com.fsck.k9.mail.Body;
import com.fsck.k9.mail.BodyPart;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mail.Multipart;
import com.fsck.k9.mail.Part;
import com.fsck.k9.mail.internet.MessageExtractor;
import com.fsck.k9.mail.internet.MimeBodyPart;
import com.fsck.k9.mail.internet.TextBody;
import com.fsck.k9.mailstore.DecryptStreamParser;
import com.fsck.k9.mailstore.LocalMessage;
import com.fsck.k9.mailstore.OpenPgpResultAnnotation;
import org.openintents.openpgp.IOpenPgpService;
import org.openintents.openpgp.OpenPgpError;
import org.openintents.openpgp.OpenPgpSignatureResult;
import org.openintents.openpgp.util.OpenPgpApi;
import org.openintents.openpgp.util.OpenPgpApi.IOpenPgpCallback;
import org.openintents.openpgp.util.OpenPgpServiceConnection;
import org.openintents.openpgp.util.OpenPgpServiceConnection.OnBound;


public class MessageCryptoHelper {

    private final Context context;
    private final MessageViewFragment fragment;
    private final Account account;
    private LocalMessage message;

    private Deque<Part> partsToDecryptOrVerify;
    private OpenPgpApi openPgpApi;
    private Part currentlyDecryptingOrVerifyingPart;
    private Intent currentCryptoResult;

    private MessageCryptoAnnotations messageAnnotations;

    private static final int INVALID_OPENPGP_RESULT_CODE = -1;

    public MessageCryptoHelper(Context context, MessageViewFragment fragment, Account account) {
        this.context = context;
        this.fragment = fragment;
        this.account = account;

        this.messageAnnotations = new MessageCryptoAnnotations();
    }

    public void decryptOrVerifyMessagePartsIfNecessary(LocalMessage message) {
        this.message = message;

        if (!isCryptoProviderConfigured()) {
            returnResultToFragment();
            return;
        }

        List<Part> encryptedParts = MessageDecryptVerifier.findEncryptedParts(message);
        List<Part> signedParts = MessageDecryptVerifier.findSignedParts(message);
        List<Part> inlineParts = MessageDecryptVerifier.findPgpInlineParts(message);
        if (!encryptedParts.isEmpty() || !signedParts.isEmpty() || !inlineParts.isEmpty()) {
            partsToDecryptOrVerify = new ArrayDeque<Part>();
            partsToDecryptOrVerify.addAll(encryptedParts);
            partsToDecryptOrVerify.addAll(signedParts);
            partsToDecryptOrVerify.addAll(inlineParts);
            decryptOrVerifyNextPartOrStartExtractingTextAndAttachments();
        } else {
            returnResultToFragment();
        }
    }

    private boolean isCryptoProviderConfigured() {
        return !TextUtils.isEmpty(account.getCryptoApp());
    }

    private void decryptOrVerifyNextPartOrStartExtractingTextAndAttachments() {
        if (partsToDecryptOrVerify.isEmpty()) {
            returnResultToFragment();
            return;
        }

        Part part = partsToDecryptOrVerify.peekFirst();
        if ("text/plain".equalsIgnoreCase(part.getMimeType())) {
            startDecryptingOrVerifyingPart(part);
        } else if (MessageDecryptVerifier.isPgpMimePart(part)) {
            Multipart multipart = (Multipart) part.getBody();
            if (multipart == null) {
                throw new RuntimeException("Downloading missing parts before decryption isn't supported yet");
            }

            startDecryptingOrVerifyingPart(part);
        } else {
            partsToDecryptOrVerify.removeFirst();
            decryptOrVerifyNextPartOrStartExtractingTextAndAttachments();
        }
    }

    private void startDecryptingOrVerifyingPart(Part part) {
        if (!isBoundToCryptoProviderService()) {
            connectToCryptoProviderService();
        } else {
            decryptOrVerifyPart(part);
        }
    }

{'fraction_non_alphanumeric': 0.08776993799444088, 'fraction_numerical': 0.004490057729313663, 'mean_word_length': 3.6915747241725176, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5565016', 'n_tokens_mistral': 2767, 'n_tokens_neox': 2738, 'n_words': 450}