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

Commit 8c55e571 authored by cketti's avatar cketti Committed by GitHub
Browse files

Merge pull request #2169 from k9mail/GH-1822_remove_parcelable_from_intent

Fix notification actions
parents f491e00b 0a76ea60
Loading
Loading
Loading
Loading
+7 −2
Original line number Diff line number Diff line
@@ -82,7 +82,10 @@ public class ChooseFolder extends K9ListActivity {
        Intent intent = getIntent();
        String accountUuid = intent.getStringExtra(EXTRA_ACCOUNT);
        mAccount = Preferences.getPreferences(this).getAccount(accountUuid);
        mMessageReference = intent.getParcelableExtra(EXTRA_MESSAGE);
        if (intent.hasExtra(EXTRA_MESSAGE)) {
            String messageReferenceString = intent.getStringExtra(EXTRA_MESSAGE);
            mMessageReference = MessageReference.parse(messageReferenceString);
        }
        mFolder = intent.getStringExtra(EXTRA_CUR_FOLDER);
        mSelectFolder = intent.getStringExtra(EXTRA_SEL_FOLDER);
        if (intent.getStringExtra(EXTRA_SHOW_CURRENT) != null) {
@@ -125,7 +128,9 @@ public class ChooseFolder extends K9ListActivity {
                    destFolderName = mHeldInbox;
                }
                result.putExtra(EXTRA_NEW_FOLDER, destFolderName);
                result.putExtra(EXTRA_MESSAGE, mMessageReference);
                if (mMessageReference != null) {
                    result.putExtra(EXTRA_MESSAGE, mMessageReference.toIdentityString());
                }
                setResult(RESULT_OK, result);
                finish();
            }
+5 −6
Original line number Diff line number Diff line
@@ -235,7 +235,8 @@ public class MessageCompose extends K9Activity implements OnClickListener,

        final Intent intent = getIntent();

        relatedMessageReference = intent.getParcelableExtra(EXTRA_MESSAGE_REFERENCE);
        String messageReferenceString = intent.getStringExtra(EXTRA_MESSAGE_REFERENCE);
        relatedMessageReference = MessageReference.parse(messageReferenceString);

        final String accountUuid = (relatedMessageReference != null) ?
                relatedMessageReference.getAccountUuid() :
@@ -1302,18 +1303,16 @@ public class MessageCompose extends K9Activity implements OnClickListener,

        if (k9identity.containsKey(IdentityField.ORIGINAL_MESSAGE)) {
            relatedMessageReference = null;
            try {
            String originalMessage = k9identity.get(IdentityField.ORIGINAL_MESSAGE);
                MessageReference messageReference = new MessageReference(originalMessage);
            MessageReference messageReference = MessageReference.parse(originalMessage);

            if (messageReference != null) {
                // Check if this is a valid account in our database
                Preferences prefs = Preferences.getPreferences(getApplicationContext());
                Account account = prefs.getAccount(messageReference.getAccountUuid());
                if (account != null) {
                    relatedMessageReference = messageReference;
                }
            } catch (MessagingException e) {
                Log.e(K9.LOG_TAG, "Could not decode message reference in identity.", e);
            }
        }

+7 −4
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@ import com.fsck.k9.activity.setup.FolderSettings;
import com.fsck.k9.activity.setup.Prefs;
import com.fsck.k9.fragment.MessageListFragment;
import com.fsck.k9.fragment.MessageListFragment.MessageListFragmentListener;
import com.fsck.k9.helper.ParcelableUtil;
import com.fsck.k9.mailstore.StorageManager;
import com.fsck.k9.preferences.StorageEditor;
import com.fsck.k9.search.LocalSearch;
@@ -108,7 +109,7 @@ public class MessageList extends K9Activity implements MessageListFragmentListen
    public static Intent intentDisplaySearch(Context context, SearchSpecification search,
            boolean noThreading, boolean newTask, boolean clearTop) {
        Intent intent = new Intent(context, MessageList.class);
        intent.putExtra(EXTRA_SEARCH, search);
        intent.putExtra(EXTRA_SEARCH, ParcelableUtil.marshall(search));
        intent.putExtra(EXTRA_NO_THREADING, noThreading);

        if (clearTop) {
@@ -135,7 +136,7 @@ public class MessageList extends K9Activity implements MessageListFragmentListen
            MessageReference messageReference) {
        Intent intent = new Intent(context, MessageList.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.putExtra(EXTRA_MESSAGE_REFERENCE, messageReference);
        intent.putExtra(EXTRA_MESSAGE_REFERENCE, messageReference.toIdentityString());
        return intent;
    }

@@ -432,12 +433,14 @@ public class MessageList extends K9Activity implements MessageListFragmentListen
            }
        } else {
            // regular LocalSearch object was passed
            mSearch = intent.getParcelableExtra(EXTRA_SEARCH);
            mSearch = intent.hasExtra(EXTRA_SEARCH) ?
                    ParcelableUtil.unmarshall(intent.getByteArrayExtra(EXTRA_SEARCH), LocalSearch.CREATOR) : null;
            mNoThreading = intent.getBooleanExtra(EXTRA_NO_THREADING, false);
        }

        if (mMessageReference == null) {
            mMessageReference = intent.getParcelableExtra(EXTRA_MESSAGE_REFERENCE);
            String messageReferenceString = intent.getStringExtra(EXTRA_MESSAGE_REFERENCE);
            mMessageReference = MessageReference.parse(messageReferenceString);
        }

        if (mMessageReference != null) {
+44 −138
Original line number Diff line number Diff line
package com.fsck.k9.activity;

import android.content.Context;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

import com.fsck.k9.Account;
import com.fsck.k9.K9;
import com.fsck.k9.Preferences;

import java.util.StringTokenizer;

import android.support.annotation.Nullable;

import com.fsck.k9.mail.Flag;
import com.fsck.k9.mail.MessagingException;
import com.fsck.k9.mailstore.LocalFolder;
import com.fsck.k9.mailstore.LocalMessage;
import com.fsck.k9.mail.filter.Base64;

import java.util.StringTokenizer;
import static com.fsck.k9.helper.Preconditions.checkNotNull;


public class MessageReference {
    private static final char IDENTITY_VERSION_1 = '!';
    private static final String IDENTITY_SEPARATOR = ":";


public class MessageReference implements Parcelable {
    private final String accountUuid;
    private final String folderName;
    private final String uid;
    private final Flag flag;


    /**
     * Initialize a new MessageReference.
     */
    public MessageReference(String accountUuid, String folderName, String uid, Flag flag) {
        this.accountUuid = accountUuid;
        this.folderName = folderName;
        this.uid = uid;
        this.flag = flag;
    @Nullable
    public static MessageReference parse(String identity) {
        if (identity == null || identity.length() < 1 || identity.charAt(0) != IDENTITY_VERSION_1) {
            return null;
        }

    // Version identifier for use when serializing. This will allow us to introduce future versions
    // if we have to rev MessageReference.
    private static final String IDENTITY_VERSION_1 = "!";
    private static final String IDENTITY_SEPARATOR = ":";

    /**
     * Initialize a MessageReference from a serialized identity.
     * @param identity Serialized identity.
     * @throws MessagingException On missing or corrupted identity.
     */
    public MessageReference(final String identity) throws MessagingException {
        // Can't be null and must be at least length one so we can check the version.
        if (identity == null || identity.length() < 1) {
            throw new MessagingException("Null or truncated MessageReference identity.");
        }

        String accountUuid = null;
        String folderName = null;
        String uid = null;
        Flag flag = null;
        // Version check.
        if (identity.charAt(0) == IDENTITY_VERSION_1.charAt(0)) {
            // Split the identity, stripping away the first two characters representing the version and delimiter.
        StringTokenizer tokens = new StringTokenizer(identity.substring(2), IDENTITY_SEPARATOR, false);
            if (tokens.countTokens() >= 3) {
                accountUuid = Base64.decode(tokens.nextToken());
                folderName = Base64.decode(tokens.nextToken());
                uid = Base64.decode(tokens.nextToken());

                if (tokens.hasMoreTokens()) {
                    final String flagString = tokens.nextToken();
                    try {
                        flag = Flag.valueOf(flagString);
                    } catch (IllegalArgumentException ie) {
                        throw new MessagingException("Could not thaw message flag '" + flagString + "'", ie);
                    }
        if (tokens.countTokens() < 3) {
            return null;
        }

                if (K9.DEBUG) {
                    Log.d(K9.LOG_TAG, "Thawed " + toString());
        String accountUuid = Base64.decode(tokens.nextToken());
        String folderName = Base64.decode(tokens.nextToken());
        String uid = Base64.decode(tokens.nextToken());

        if (!tokens.hasMoreTokens()) {
            return new MessageReference(accountUuid, folderName, uid, null);
        }
            } else {
                throw new MessagingException("Invalid MessageReference in " + identity + " identity.");

        Flag flag;
        try {
            flag = Flag.valueOf(tokens.nextToken());
        } catch (IllegalArgumentException e) {
            return null;
        }

        return new MessageReference(accountUuid, folderName, uid, flag);
    }
        this.accountUuid = accountUuid;
        this.folderName = folderName;
        this.uid = uid;

    public MessageReference(String accountUuid, String folderName, String uid, Flag flag) {
        this.accountUuid = checkNotNull(accountUuid);
        this.folderName = checkNotNull(folderName);
        this.uid = checkNotNull(uid);
        this.flag = flag;
    }

    /**
     * Serialize this MessageReference for storing in a K9 identity.  This is a colon-delimited base64 string.
     *
     * @return Serialized string.
     */
    public String toIdentityString() {
        StringBuilder refString = new StringBuilder();

@@ -117,10 +86,7 @@ public class MessageReference implements Parcelable {
    }

    public boolean equals(String accountUuid, String folderName, String uid) {
        // noinspection StringEquality, we check for null values here
        return ((accountUuid == this.accountUuid || (accountUuid != null && accountUuid.equals(this.accountUuid)))
                && (folderName == this.folderName || (folderName != null && folderName.equals(this.folderName)))
                && (uid == this.uid || (uid != null && uid.equals(this.uid))));
        return this.accountUuid.equals(accountUuid) && this.folderName.equals(folderName) && this.uid.equals(uid);
    }

    @Override
@@ -128,9 +94,9 @@ public class MessageReference implements Parcelable {
        final int MULTIPLIER = 31;

        int result = 1;
        result = MULTIPLIER * result + ((accountUuid == null) ? 0 : accountUuid.hashCode());
        result = MULTIPLIER * result + ((folderName == null) ? 0 : folderName.hashCode());
        result = MULTIPLIER * result + ((uid == null) ? 0 : uid.hashCode());
        result = MULTIPLIER * result + accountUuid.hashCode();
        result = MULTIPLIER * result + folderName.hashCode();
        result = MULTIPLIER * result + uid.hashCode();
        return result;
    }

@@ -144,66 +110,6 @@ public class MessageReference implements Parcelable {
               '}';
    }

    public LocalMessage restoreToLocalMessage(Context context) {
        try {
            Account account = Preferences.getPreferences(context).getAccount(accountUuid);
            if (account != null) {
                LocalFolder folder = account.getLocalStore().getFolder(folderName);
                if (folder != null) {
                    LocalMessage message = folder.getMessage(uid);
                    if (message != null) {
                        return message;
                    } else {
                        Log.d(K9.LOG_TAG, "Could not restore message, uid " + uid + " is unknown.");
                    }
                } else {
                    Log.d(K9.LOG_TAG, "Could not restore message, folder " + folderName + " is unknown.");
                }
            } else {
                Log.d(K9.LOG_TAG, "Could not restore message, account " + accountUuid + " is unknown.");
            }
        } catch (MessagingException e) {
            Log.w(K9.LOG_TAG, "Could not retrieve message for reference.", e);
        }

        return null;
    }

    public static final Creator<MessageReference> CREATOR = new Creator<MessageReference>() {
        @Override
        public MessageReference createFromParcel(Parcel source) {
            MessageReference ref;
            String uid = source.readString();
            String accountUuid = source.readString();
            String folderName = source.readString();
            String flag = source.readString();
            if (flag != null) {
                ref = new MessageReference(accountUuid, folderName, uid, Flag.valueOf(flag));
            } else {
                ref = new MessageReference(accountUuid, folderName, uid, null);
            }
            return ref;
        }

        @Override
        public MessageReference[] newArray(int size) {
            return new MessageReference[size];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(uid);
        dest.writeString(accountUuid);
        dest.writeString(folderName);
        dest.writeString(flag == null ? null : flag.name());
    }

    public String getAccountUuid() {
        return accountUuid;
    }
+36 −0
Original line number Diff line number Diff line
package com.fsck.k9.activity;


import java.util.ArrayList;
import java.util.List;

import android.util.Log;

import com.fsck.k9.K9;


public class MessageReferenceHelper {
    public static List<MessageReference> toMessageReferenceList(List<String> messageReferenceStrings) {
        List<MessageReference> messageReferences = new ArrayList<>(messageReferenceStrings.size());
        for (String messageReferenceString : messageReferenceStrings) {
            MessageReference messageReference = MessageReference.parse(messageReferenceString);
            if (messageReference != null) {
                messageReferences.add(messageReference);
            } else {
                Log.w(K9.LOG_TAG, "Invalid message reference: " + messageReferenceString);
            }
        }

        return messageReferences;
    }

    public static ArrayList<String> toMessageReferenceStringList(List<MessageReference> messageReferences) {
        ArrayList<String> messageReferenceStrings = new ArrayList<>(messageReferences.size());
        for (MessageReference messageReference : messageReferences) {
            String messageReferenceString = messageReference.toIdentityString();
            messageReferenceStrings.add(messageReferenceString);
        }

        return messageReferenceStrings;
    }
}
Loading