From bf0333ba31b02d4ff1a8f1b867df09aef5195be0 Mon Sep 17 00:00:00 2001 From: Valentin CAULIER Date: Mon, 16 Mar 2015 16:29:42 +0100 Subject: [PATCH 1/8] Beginning MessageReference update to immutable object --- .../fsck/k9/activity/MessageReference.java | 63 ++++++++++++++++--- 1 file changed, 56 insertions(+), 7 deletions(-) diff --git a/k9mail/src/main/java/com/fsck/k9/activity/MessageReference.java b/k9mail/src/main/java/com/fsck/k9/activity/MessageReference.java index 131b0715a..7d480dd88 100644 --- a/k9mail/src/main/java/com/fsck/k9/activity/MessageReference.java +++ b/k9mail/src/main/java/com/fsck/k9/activity/MessageReference.java @@ -20,12 +20,29 @@ public class MessageReference implements Parcelable { public String accountUuid; public String folderName; public String uid; - public Flag flag = null; + public Flag flag; /** + * @deprecated MessageReference should be immutable now, and this way, it won't be possible to modify it anymore + * * Initialize an empty MessageReference. */ + @Deprecated public MessageReference() { + this.accountUuid = null; + this.folderName = null; + this.uid = null; + this.flag = null; + } + + /** + * 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; } // Version identifier for use when serializing. This will allow us to introduce future versions @@ -34,7 +51,7 @@ public class MessageReference implements Parcelable { private static final String IDENTITY_SEPARATOR = ":"; /** - * Initialize a MessageReference from a seraialized identity. + * Initialize a MessageReference from a serialized identity. * @param identity Serialized identity. * @throws MessagingException On missing or corrupted identity. */ @@ -61,6 +78,9 @@ public class MessageReference implements Parcelable { throw new MessagingException("Could not thaw message flag '" + flagString + "'", ie); } } + else { + flag = null; + } if (K9.DEBUG) Log.d(K9.LOG_TAG, "Thawed " + toString()); @@ -156,12 +176,17 @@ public class MessageReference implements Parcelable { public static final Creator CREATOR = new Creator() { @Override public MessageReference createFromParcel(Parcel source) { - MessageReference ref = new MessageReference(); - ref.uid = source.readString(); - ref.accountUuid = source.readString(); - ref.folderName = source.readString(); + MessageReference ref; + String uid = source.readString(); + String accountUuid = source.readString(); + String folderName = source.readString(); String flag = source.readString(); - if (flag != null) ref.flag = Flag.valueOf(flag); + if (flag != null){ + ref = new MessageReference(uid, accountUuid, folderName, Flag.valueOf(flag)); + } + else { + ref = new MessageReference(uid, accountUuid, folderName, null); + } return ref; } @@ -183,4 +208,28 @@ public class MessageReference implements Parcelable { dest.writeString(folderName); dest.writeString(flag == null ? null : flag.name()); } + + public String getAccountUuid(){ + return accountUuid; + } + + public String getFolderName(){ + return folderName; + } + + public String getUid(){ + return uid; + } + + public MessageReference withModifiedUid(String newUid){ + return new MessageReference(accountUuid, folderName, newUid, flag); + } + + public MessageReference withModifiedFlag(Flag newFlag){ + return new MessageReference(accountUuid, folderName, uid, newFlag); + } + + public Flag getFlag(){ + return flag; + } } From 3e84c20c9b2c27dd5cd9ccf9af82522855754c86 Mon Sep 17 00:00:00 2001 From: Valentin CAULIER Date: Mon, 16 Mar 2015 16:30:28 +0100 Subject: [PATCH 2/8] Updating other classes to use new constructor and getters of MessageReference --- .../k9/activity/MessageReferenceTest.java | 14 ++---- .../com/fsck/k9/activity/MessageCompose.java | 50 +++++++++---------- .../com/fsck/k9/activity/MessageList.java | 13 ++--- .../k9/controller/MessagingController.java | 6 +-- .../fsck/k9/fragment/MessageListFragment.java | 29 ++++------- .../fsck/k9/fragment/MessageViewFragment.java | 40 +++++++-------- .../com/fsck/k9/mailstore/LocalMessage.java | 5 +- .../k9/service/NotificationActionService.java | 2 +- 8 files changed, 70 insertions(+), 89 deletions(-) diff --git a/k9mail/src/androidTest/java/com/fsck/k9/activity/MessageReferenceTest.java b/k9mail/src/androidTest/java/com/fsck/k9/activity/MessageReferenceTest.java index 1bc708e1e..f35d67b24 100644 --- a/k9mail/src/androidTest/java/com/fsck/k9/activity/MessageReferenceTest.java +++ b/k9mail/src/androidTest/java/com/fsck/k9/activity/MessageReferenceTest.java @@ -71,7 +71,8 @@ public class MessageReferenceTest { @Test public void equalsWithAnObjectShouldReturnFalse() { - MessageReference messageReference = new MessageReference(); + // null : creating a new empty MessageReference + MessageReference messageReference = new MessageReference(null, null, null, null); Object object = new Object(); assertFalse(messageReference.equals(object)); @@ -125,21 +126,14 @@ public class MessageReferenceTest { } private MessageReference createMessageReference(String accountUuid, String folderName, String uid) { - MessageReference messageReference = new MessageReference(); - messageReference.accountUuid = accountUuid; - messageReference.folderName = folderName; - messageReference.uid = uid; + MessageReference messageReference = new MessageReference(accountUuid, folderName, uid, null); return messageReference; } private MessageReference createMessageReferenceWithFlag(String accountUuid, String folderName, String uid, Flag flag) { - MessageReference messageReference = new MessageReference(); - messageReference.accountUuid = accountUuid; - messageReference.folderName = folderName; - messageReference.uid = uid; - messageReference.flag = flag; + MessageReference messageReference = new MessageReference(accountUuid, folderName, uid, flag); return messageReference; } diff --git a/k9mail/src/main/java/com/fsck/k9/activity/MessageCompose.java b/k9mail/src/main/java/com/fsck/k9/activity/MessageCompose.java index 0129b82e1..4a91aa4e0 100644 --- a/k9mail/src/main/java/com/fsck/k9/activity/MessageCompose.java +++ b/k9mail/src/main/java/com/fsck/k9/activity/MessageCompose.java @@ -544,7 +544,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, } final String accountUuid = (mMessageReference != null) ? - mMessageReference.accountUuid : + mMessageReference.getAccountUuid() : intent.getStringExtra(EXTRA_ACCOUNT); mAccount = Preferences.getPreferences(this).getAccount(accountUuid); @@ -786,9 +786,9 @@ public class MessageCompose extends K9Activity implements OnClickListener, */ MessagingController.getInstance(getApplication()).addListener(mListener); - final Account account = Preferences.getPreferences(this).getAccount(mMessageReference.accountUuid); - final String folderName = mMessageReference.folderName; - final String sourceMessageUid = mMessageReference.uid; + final Account account = Preferences.getPreferences(this).getAccount(mMessageReference.getAccountUuid()); + final String folderName = mMessageReference.getFolderName(); + final String sourceMessageUid = mMessageReference.getUid(); MessagingController.getInstance(getApplication()).loadMessageForView(account, folderName, sourceMessageUid, null); } @@ -798,7 +798,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, } if (mAction == Action.REPLY || mAction == Action.REPLY_ALL) { - mMessageReference.flag = Flag.ANSWERED; + mMessageReference = mMessageReference.withModifiedFlag(Flag.ANSWERED); } if (mAction == Action.REPLY || mAction == Action.REPLY_ALL || @@ -811,7 +811,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, } if (mAction == Action.FORWARD) { - mMessageReference.flag = Flag.FORWARDED; + mMessageReference = mMessageReference.withModifiedFlag(Flag.FORWARDED); } mEncryptLayout = findViewById(R.id.layout_encrypt); @@ -1771,15 +1771,15 @@ public class MessageCompose extends K9Activity implements OnClickListener, } sendMessage(); - if (mMessageReference != null && mMessageReference.flag != null) { + if (mMessageReference != null && mMessageReference.getFlag() != null) { if (K9.DEBUG) { - Log.d(K9.LOG_TAG, "Setting referenced message (" + mMessageReference.folderName + ", " + mMessageReference.uid + ") flag to " + mMessageReference.flag); + Log.d(K9.LOG_TAG, "Setting referenced message (" + mMessageReference.getFolderName() + ", " + mMessageReference.getUid() + ") flag to " + mMessageReference.getFlag()); } - final Account account = Preferences.getPreferences(this).getAccount(mMessageReference.accountUuid); - final String folderName = mMessageReference.folderName; - final String sourceMessageUid = mMessageReference.uid; - MessagingController.getInstance(getApplication()).setFlag(account, folderName, sourceMessageUid, mMessageReference.flag, true); + final Account account = Preferences.getPreferences(this).getAccount(mMessageReference.getAccountUuid()); + final String folderName = mMessageReference.getFolderName(); + final String sourceMessageUid = mMessageReference.getUid(); + MessagingController.getInstance(getApplication()).setFlag(account, folderName, sourceMessageUid, mMessageReference.getFlag(), true); } mDraftNeedsSaving = false; @@ -2351,9 +2351,9 @@ public class MessageCompose extends K9Activity implements OnClickListener, if (mMessageReference != null) { // shouldn't happen... // TODO - Should we check if mSourceMessageBody is already present and bypass the MessagingController call? MessagingController.getInstance(getApplication()).addListener(mListener); - final Account account = Preferences.getPreferences(this).getAccount(mMessageReference.accountUuid); - final String folderName = mMessageReference.folderName; - final String sourceMessageUid = mMessageReference.uid; + final Account account = Preferences.getPreferences(this).getAccount(mMessageReference.getAccountUuid()); + final String folderName = mMessageReference.getFolderName(); + final String sourceMessageUid = mMessageReference.getUid(); MessagingController.getInstance(getApplication()).loadMessageForView(account, folderName, sourceMessageUid, null); } break; @@ -2876,7 +2876,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, // Check if this is a valid account in our database Preferences prefs = Preferences.getPreferences(getApplicationContext()); - Account account = prefs.getAccount(messageReference.accountUuid); + Account account = prefs.getAccount(messageReference.getAccountUuid()); if (account != null) { mMessageReference = messageReference; } @@ -3392,7 +3392,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, class Listener extends MessagingListener { @Override public void loadMessageForViewStarted(Account account, String folder, String uid) { - if ((mMessageReference == null) || !mMessageReference.uid.equals(uid)) { + if ((mMessageReference == null) || !mMessageReference.getUid().equals(uid)) { return; } @@ -3401,7 +3401,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, @Override public void loadMessageForViewFinished(Account account, String folder, String uid, Message message) { - if ((mMessageReference == null) || !mMessageReference.uid.equals(uid)) { + if ((mMessageReference == null) || !mMessageReference.getUid().equals(uid)) { return; } @@ -3410,7 +3410,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, @Override public void loadMessageForViewBodyAvailable(Account account, String folder, String uid, final Message message) { - if ((mMessageReference == null) || !mMessageReference.uid.equals(uid)) { + if ((mMessageReference == null) || !mMessageReference.getUid().equals(uid)) { return; } @@ -3441,7 +3441,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, @Override public void loadMessageForViewFailed(Account account, String folder, String uid, Throwable t) { - if ((mMessageReference == null) || !mMessageReference.uid.equals(uid)) { + if ((mMessageReference == null) || !mMessageReference.getUid().equals(uid)) { return; } mHandler.sendEmptyMessage(MSG_PROGRESS_OFF); @@ -3452,13 +3452,13 @@ public class MessageCompose extends K9Activity implements OnClickListener, public void messageUidChanged(Account account, String folder, String oldUid, String newUid) { // Track UID changes of the source message if (mMessageReference != null) { - final Account sourceAccount = Preferences.getPreferences(MessageCompose.this).getAccount(mMessageReference.accountUuid); - final String sourceFolder = mMessageReference.folderName; - final String sourceMessageUid = mMessageReference.uid; + final Account sourceAccount = Preferences.getPreferences(MessageCompose.this).getAccount(mMessageReference.getAccountUuid()); + final String sourceFolder = mMessageReference.getFolderName(); + final String sourceMessageUid = mMessageReference.getUid(); if (account.equals(sourceAccount) && (folder.equals(sourceFolder))) { if (oldUid.equals(sourceMessageUid)) { - mMessageReference.uid = newUid; + mMessageReference = mMessageReference.withModifiedUid(newUid); } if ((mSourceMessage != null) && (oldUid.equals(mSourceMessage.getUid()))) { mSourceMessage.setUid(newUid); @@ -3602,7 +3602,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, * to the old message's uid. */ if (mMessageReference != null) { - message.setUid(mMessageReference.uid); + message.setUid(mMessageReference.getUid()); } } diff --git a/k9mail/src/main/java/com/fsck/k9/activity/MessageList.java b/k9mail/src/main/java/com/fsck/k9/activity/MessageList.java index 357e0ccf6..f555f25e5 100644 --- a/k9mail/src/main/java/com/fsck/k9/activity/MessageList.java +++ b/k9mail/src/main/java/com/fsck/k9/activity/MessageList.java @@ -379,10 +379,7 @@ public class MessageList extends K9Activity implements MessageListFragmentListen Collection accounts = Preferences.getPreferences(this).getAvailableAccounts(); for (Account account : accounts) { if (String.valueOf(account.getAccountNumber()).equals(accountId)) { - mMessageReference = new MessageReference(); - mMessageReference.accountUuid = account.getUuid(); - mMessageReference.folderName = segmentList.get(1); - mMessageReference.uid = segmentList.get(2); + mMessageReference = new MessageReference(account.getUuid(), segmentList.get(1), segmentList.get(2), null); break; } } @@ -431,8 +428,8 @@ public class MessageList extends K9Activity implements MessageListFragmentListen if (mMessageReference != null) { mSearch = new LocalSearch(); - mSearch.addAccountUuid(mMessageReference.accountUuid); - mSearch.addAllowedFolder(mMessageReference.folderName); + mSearch.addAccountUuid(mMessageReference.getAccountUuid()); + mSearch.addAllowedFolder(mMessageReference.getFolderName()); } if (mSearch == null) { @@ -1168,8 +1165,8 @@ public class MessageList extends K9Activity implements MessageListFragmentListen @Override public void openMessage(MessageReference messageReference) { Preferences prefs = Preferences.getPreferences(getApplicationContext()); - Account account = prefs.getAccount(messageReference.accountUuid); - String folderName = messageReference.folderName; + Account account = prefs.getAccount(messageReference.getAccountUuid()); + String folderName = messageReference.getFolderName(); if (folderName.equals(account.getDraftsFolderName())) { MessageCompose.actionEditDraft(this, messageReference); diff --git a/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java b/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java index 9a5c356f9..1bfc508d0 100644 --- a/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java +++ b/k9mail/src/main/java/com/fsck/k9/controller/MessagingController.java @@ -4936,7 +4936,7 @@ public class MessagingController implements Runnable { String initialFolder = message.getFolder().getName(); /* only go to folder if all messages are in the same folder, else go to folder list */ for (MessageReference ref : allRefs) { - if (!TextUtils.equals(initialFolder, ref.folderName)) { + if (!TextUtils.equals(initialFolder, ref.getFolderName())) { initialFolder = null; break; } @@ -5008,8 +5008,8 @@ public class MessagingController implements Runnable { } private TaskStackBuilder buildMessageViewBackStack(Context context, MessageReference message) { - Account account = Preferences.getPreferences(context).getAccount(message.accountUuid); - TaskStackBuilder stack = buildMessageListBackStack(context, account, message.folderName); + Account account = Preferences.getPreferences(context).getAccount(message.getAccountUuid()); + TaskStackBuilder stack = buildMessageListBackStack(context, account, message.getFolderName()); stack.addNextIntent(MessageList.actionDisplayMessageIntent(context, message)); return stack; } diff --git a/k9mail/src/main/java/com/fsck/k9/fragment/MessageListFragment.java b/k9mail/src/main/java/com/fsck/k9/fragment/MessageListFragment.java index a2c239b3b..29049d587 100644 --- a/k9mail/src/main/java/com/fsck/k9/fragment/MessageListFragment.java +++ b/k9mail/src/main/java/com/fsck/k9/fragment/MessageListFragment.java @@ -2007,9 +2007,9 @@ public class MessageListFragment extends Fragment implements OnItemClickListener String uid = cursor.getString(UID_COLUMN); String folderName = cursor.getString(FOLDER_NAME_COLUMN); - if (account.getUuid().equals(mActiveMessage.accountUuid) && - folderName.equals(mActiveMessage.folderName) && - uid.equals(mActiveMessage.uid)) { + if (account.getUuid().equals(mActiveMessage.getAccountUuid()) && + folderName.equals(mActiveMessage.getFolderName()) && + uid.equals(mActiveMessage.getUid())) { int res = R.attr.messageListActiveItemBackgroundColor; TypedValue outValue = new TypedValue(); @@ -2992,11 +2992,7 @@ public class MessageListFragment extends Fragment implements OnItemClickListener for (int i = 0, len = mAdapter.getCount(); i < len; i++) { Cursor cursor = (Cursor) mAdapter.getItem(i); - MessageReference ref = new MessageReference(); - ref.accountUuid = cursor.getString(ACCOUNT_UUID_COLUMN); - ref.folderName = cursor.getString(FOLDER_NAME_COLUMN); - ref.uid = cursor.getString(UID_COLUMN); - + MessageReference ref = new MessageReference(cursor.getString(ACCOUNT_UUID_COLUMN), cursor.getString(FOLDER_NAME_COLUMN), cursor.getString(UID_COLUMN), null); messageRefs.add(ref); } @@ -3058,10 +3054,7 @@ public class MessageListFragment extends Fragment implements OnItemClickListener private MessageReference getReferenceForPosition(int position) { Cursor cursor = (Cursor) mAdapter.getItem(position); - MessageReference ref = new MessageReference(); - ref.accountUuid = cursor.getString(ACCOUNT_UUID_COLUMN); - ref.folderName = cursor.getString(FOLDER_NAME_COLUMN); - ref.uid = cursor.getString(UID_COLUMN); + MessageReference ref = new MessageReference(cursor.getString(ACCOUNT_UUID_COLUMN), cursor.getString(FOLDER_NAME_COLUMN), cursor.getString(UID_COLUMN), null); return ref; } @@ -3091,9 +3084,9 @@ public class MessageListFragment extends Fragment implements OnItemClickListener String folderName = cursor.getString(FOLDER_NAME_COLUMN); String uid = cursor.getString(UID_COLUMN); - if (accountUuid.equals(messageReference.accountUuid) && - folderName.equals(messageReference.folderName) && - uid.equals(messageReference.uid)) { + if (accountUuid.equals(messageReference.getAccountUuid()) && + folderName.equals(messageReference.getFolderName()) && + uid.equals(messageReference.getUid())) { return i; } } @@ -3330,12 +3323,12 @@ public class MessageListFragment extends Fragment implements OnItemClickListener StringBuilder query = new StringBuilder(); List queryArgs = new ArrayList(); if (needConditions) { - boolean selectActive = mActiveMessage != null && mActiveMessage.accountUuid.equals(accountUuid); + boolean selectActive = mActiveMessage != null && mActiveMessage.getAccountUuid().equals(accountUuid); if (selectActive) { query.append("(" + MessageColumns.UID + " = ? AND " + SpecialColumns.FOLDER_NAME + " = ?) OR ("); - queryArgs.add(mActiveMessage.uid); - queryArgs.add(mActiveMessage.folderName); + queryArgs.add(mActiveMessage.getUid()); + queryArgs.add(mActiveMessage.getFolderName()); } SqlQueryBuilder.buildWhereClause(account, mSearch.getConditions(), query, queryArgs); diff --git a/k9mail/src/main/java/com/fsck/k9/fragment/MessageViewFragment.java b/k9mail/src/main/java/com/fsck/k9/fragment/MessageViewFragment.java index 961a38555..5de1dc41f 100644 --- a/k9mail/src/main/java/com/fsck/k9/fragment/MessageViewFragment.java +++ b/k9mail/src/main/java/com/fsck/k9/fragment/MessageViewFragment.java @@ -271,7 +271,7 @@ public class MessageViewFragment extends Fragment implements OnClickListener, } Context appContext = getActivity().getApplicationContext(); - mAccount = Preferences.getPreferences(appContext).getAccount(mMessageReference.accountUuid); + mAccount = Preferences.getPreferences(appContext).getAccount(mMessageReference.getAccountUuid()); if (resetPgpData) { // start with fresh, empty PGP data @@ -282,7 +282,7 @@ public class MessageViewFragment extends Fragment implements OnClickListener, mMessageView.resetView(); mMessageView.resetHeaderView(); - mController.loadMessageForView(mAccount, mMessageReference.folderName, mMessageReference.uid, mListener); + mController.loadMessageForView(mAccount, mMessageReference.getFolderName(), mMessageReference.getUid(), mListener); mFragmentListener.updateMenu(); } @@ -340,7 +340,7 @@ public class MessageViewFragment extends Fragment implements OnClickListener, } private void refileMessage(String dstFolder) { - String srcFolder = mMessageReference.folderName; + String srcFolder = mMessageReference.getFolderName(); LocalMessage messageToMove = mMessage; mFragmentListener.showNextMessageOrReturn(); mController.moveMessage(mAccount, srcFolder, messageToMove, dstFolder, null); @@ -417,7 +417,7 @@ public class MessageViewFragment extends Fragment implements OnClickListener, private void startRefileActivity(int activity) { Intent intent = new Intent(getActivity(), ChooseFolder.class); intent.putExtra(ChooseFolder.EXTRA_ACCOUNT, mAccount.getUuid()); - intent.putExtra(ChooseFolder.EXTRA_CUR_FOLDER, mMessageReference.folderName); + intent.putExtra(ChooseFolder.EXTRA_CUR_FOLDER, mMessageReference.getFolderName()); intent.putExtra(ChooseFolder.EXTRA_SEL_FOLDER, mAccount.getLastSelectedFolderName()); intent.putExtra(ChooseFolder.EXTRA_MESSAGE, mMessageReference); startActivityForResult(intent, activity); @@ -493,7 +493,7 @@ public class MessageViewFragment extends Fragment implements OnClickListener, return; } mMessageView.downloadRemainderButton().setEnabled(false); - mController.loadMessageForViewRemote(mAccount, mMessageReference.folderName, mMessageReference.uid, mListener); + mController.loadMessageForViewRemote(mAccount, mMessageReference.getFolderName(), mMessageReference.getUid(), mListener); } @Override @@ -516,12 +516,12 @@ public class MessageViewFragment extends Fragment implements OnClickListener, } public void moveMessage(MessageReference reference, String destFolderName) { - mController.moveMessage(mAccount, mMessageReference.folderName, mMessage, + mController.moveMessage(mAccount, mMessageReference.getFolderName(), mMessage, destFolderName, null); } public void copyMessage(MessageReference reference, String destFolderName) { - mController.copyMessage(mAccount, mMessageReference.folderName, mMessage, + mController.copyMessage(mAccount, mMessageReference.getFolderName(), mMessage, destFolderName, null); } @@ -529,8 +529,8 @@ public class MessageViewFragment extends Fragment implements OnClickListener, @Override public void loadMessageForViewHeadersAvailable(final Account account, String folder, String uid, final Message message) { - if (!mMessageReference.uid.equals(uid) || !mMessageReference.folderName.equals(folder) - || !mMessageReference.accountUuid.equals(account.getUuid())) { + if (!mMessageReference.getUid().equals(uid) || !mMessageReference.getFolderName().equals(folder) + || !mMessageReference.getAccountUuid().equals(account.getUuid())) { return; } @@ -577,9 +577,9 @@ public class MessageViewFragment extends Fragment implements OnClickListener, public void loadMessageForViewBodyAvailable(final Account account, String folder, String uid, final Message message) { if (!(message instanceof LocalMessage) || - !mMessageReference.uid.equals(uid) || - !mMessageReference.folderName.equals(folder) || - !mMessageReference.accountUuid.equals(account.getUuid())) { + !mMessageReference.getUid().equals(uid) || + !mMessageReference.getFolderName().equals(folder) || + !mMessageReference.getAccountUuid().equals(account.getUuid())) { return; } @@ -601,8 +601,8 @@ public class MessageViewFragment extends Fragment implements OnClickListener, @Override public void loadMessageForViewFailed(Account account, String folder, String uid, final Throwable t) { - if (!mMessageReference.uid.equals(uid) || !mMessageReference.folderName.equals(folder) - || !mMessageReference.accountUuid.equals(account.getUuid())) { + if (!mMessageReference.getUid().equals(uid) || !mMessageReference.getFolderName().equals(folder) + || !mMessageReference.getAccountUuid().equals(account.getUuid())) { return; } mHandler.post(new Runnable() { @@ -624,8 +624,8 @@ public class MessageViewFragment extends Fragment implements OnClickListener, @Override public void loadMessageForViewFinished(Account account, String folder, String uid, final Message message) { - if (!mMessageReference.uid.equals(uid) || !mMessageReference.folderName.equals(folder) - || !mMessageReference.accountUuid.equals(account.getUuid())) { + if (!mMessageReference.getUid().equals(uid) || !mMessageReference.getFolderName().equals(folder) + || !mMessageReference.getAccountUuid().equals(account.getUuid())) { return; } mHandler.post(new Runnable() { @@ -639,8 +639,8 @@ public class MessageViewFragment extends Fragment implements OnClickListener, @Override public void loadMessageForViewStarted(Account account, String folder, String uid) { - if (!mMessageReference.uid.equals(uid) || !mMessageReference.folderName.equals(folder) - || !mMessageReference.accountUuid.equals(account.getUuid())) { + if (!mMessageReference.getUid().equals(uid) || !mMessageReference.getFolderName().equals(folder) + || !mMessageReference.getAccountUuid().equals(account.getUuid())) { return; } mHandler.post(new Runnable() { @@ -830,12 +830,12 @@ public class MessageViewFragment extends Fragment implements OnClickListener, } public boolean canMessageBeArchived() { - return (!mMessageReference.folderName.equals(mAccount.getArchiveFolderName()) + return (!mMessageReference.getFolderName().equals(mAccount.getArchiveFolderName()) && mAccount.hasArchiveFolder()); } public boolean canMessageBeMovedToSpam() { - return (!mMessageReference.folderName.equals(mAccount.getSpamFolderName()) + return (!mMessageReference.getFolderName().equals(mAccount.getSpamFolderName()) && mAccount.hasSpamFolder()); } diff --git a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalMessage.java b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalMessage.java index 48ed576d2..48735d364 100644 --- a/k9mail/src/main/java/com/fsck/k9/mailstore/LocalMessage.java +++ b/k9mail/src/main/java/com/fsck/k9/mailstore/LocalMessage.java @@ -576,10 +576,7 @@ public class LocalMessage extends MimeMessage { public MessageReference makeMessageReference() { if (mReference == null) { - mReference = new MessageReference(); - mReference.folderName = getFolder().getName(); - mReference.uid = mUid; - mReference.accountUuid = getFolder().getAccountUuid(); + mReference = new MessageReference(getFolder().getAccountUuid(), getFolder().getName(), mUid, null); } return mReference; } diff --git a/k9mail/src/main/java/com/fsck/k9/service/NotificationActionService.java b/k9mail/src/main/java/com/fsck/k9/service/NotificationActionService.java index 50f14bc15..8b4e59440 100644 --- a/k9mail/src/main/java/com/fsck/k9/service/NotificationActionService.java +++ b/k9mail/src/main/java/com/fsck/k9/service/NotificationActionService.java @@ -80,7 +80,7 @@ public class NotificationActionService extends CoreService { List refs = intent.getParcelableArrayListExtra(EXTRA_MESSAGE_LIST); for (MessageReference ref : refs) { - controller.setFlag(account, ref.folderName, ref.uid, Flag.SEEN, true); + controller.setFlag(account, ref.getFolderName(), ref.getUid(), Flag.SEEN, true); } } else if (DELETE_ALL_ACTION.equals(action)) { if (K9.DEBUG) From 13f6b4225098866c42b30ab6d118e0fbf8644031 Mon Sep 17 00:00:00 2001 From: Valentin CAULIER Date: Sat, 21 Mar 2015 11:02:15 +0100 Subject: [PATCH 3/8] MessageReference is now immutable --- .../fsck/k9/activity/MessageReference.java | 29 +++++++++++-------- 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/k9mail/src/main/java/com/fsck/k9/activity/MessageReference.java b/k9mail/src/main/java/com/fsck/k9/activity/MessageReference.java index 7d480dd88..5e3ddcffa 100644 --- a/k9mail/src/main/java/com/fsck/k9/activity/MessageReference.java +++ b/k9mail/src/main/java/com/fsck/k9/activity/MessageReference.java @@ -17,10 +17,10 @@ import com.fsck.k9.mail.filter.Base64; import java.util.StringTokenizer; public class MessageReference implements Parcelable { - public String accountUuid; - public String folderName; - public String uid; - public Flag flag; + protected final String accountUuid; + protected final String folderName; + protected final String uid; + protected final Flag flag; /** * @deprecated MessageReference should be immutable now, and this way, it won't be possible to modify it anymore @@ -60,7 +60,11 @@ public class MessageReference implements Parcelable { if (identity == null || identity.length() < 1) { throw new MessagingException("Null or truncated MessageReference identity."); } - + // Some temp variables + 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. @@ -78,9 +82,6 @@ public class MessageReference implements Parcelable { throw new MessagingException("Could not thaw message flag '" + flagString + "'", ie); } } - else { - flag = null; - } if (K9.DEBUG) Log.d(K9.LOG_TAG, "Thawed " + toString()); @@ -88,6 +89,10 @@ public class MessageReference implements Parcelable { throw new MessagingException("Invalid MessageReference in " + identity + " identity."); } } + this.accountUuid = accountUuid; + this.folderName = folderName; + this.uid = uid; + this.flag = flag; } /** @@ -221,6 +226,10 @@ public class MessageReference implements Parcelable { return uid; } + public Flag getFlag(){ + return flag; + } + public MessageReference withModifiedUid(String newUid){ return new MessageReference(accountUuid, folderName, newUid, flag); } @@ -228,8 +237,4 @@ public class MessageReference implements Parcelable { public MessageReference withModifiedFlag(Flag newFlag){ return new MessageReference(accountUuid, folderName, uid, newFlag); } - - public Flag getFlag(){ - return flag; - } } From 632517be814d0989edf7ae6bc7149432ab8551fd Mon Sep 17 00:00:00 2001 From: cketti Date: Fri, 3 Apr 2015 19:50:50 +0200 Subject: [PATCH 4/8] Remove trivial comments --- .../java/com/fsck/k9/activity/MessageReferenceTest.java | 1 - k9mail/src/main/java/com/fsck/k9/activity/MessageReference.java | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/k9mail/src/androidTest/java/com/fsck/k9/activity/MessageReferenceTest.java b/k9mail/src/androidTest/java/com/fsck/k9/activity/MessageReferenceTest.java index f35d67b24..81c3e80c3 100644 --- a/k9mail/src/androidTest/java/com/fsck/k9/activity/MessageReferenceTest.java +++ b/k9mail/src/androidTest/java/com/fsck/k9/activity/MessageReferenceTest.java @@ -71,7 +71,6 @@ public class MessageReferenceTest { @Test public void equalsWithAnObjectShouldReturnFalse() { - // null : creating a new empty MessageReference MessageReference messageReference = new MessageReference(null, null, null, null); Object object = new Object(); diff --git a/k9mail/src/main/java/com/fsck/k9/activity/MessageReference.java b/k9mail/src/main/java/com/fsck/k9/activity/MessageReference.java index 5e3ddcffa..6f5084546 100644 --- a/k9mail/src/main/java/com/fsck/k9/activity/MessageReference.java +++ b/k9mail/src/main/java/com/fsck/k9/activity/MessageReference.java @@ -60,7 +60,7 @@ public class MessageReference implements Parcelable { if (identity == null || identity.length() < 1) { throw new MessagingException("Null or truncated MessageReference identity."); } - // Some temp variables + String accountUuid = null; String folderName = null; String uid = null; From af36129449deb8eb689c41a02e2ce724e039edb8 Mon Sep 17 00:00:00 2001 From: cketti Date: Fri, 3 Apr 2015 20:27:11 +0200 Subject: [PATCH 5/8] Extract local variables for easier readability --- .../main/java/com/fsck/k9/activity/MessageList.java | 4 +++- .../com/fsck/k9/fragment/MessageListFragment.java | 12 +++++++++--- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/k9mail/src/main/java/com/fsck/k9/activity/MessageList.java b/k9mail/src/main/java/com/fsck/k9/activity/MessageList.java index 1c3244324..9e01b846a 100644 --- a/k9mail/src/main/java/com/fsck/k9/activity/MessageList.java +++ b/k9mail/src/main/java/com/fsck/k9/activity/MessageList.java @@ -379,7 +379,9 @@ public class MessageList extends K9Activity implements MessageListFragmentListen Collection accounts = Preferences.getPreferences(this).getAvailableAccounts(); for (Account account : accounts) { if (String.valueOf(account.getAccountNumber()).equals(accountId)) { - mMessageReference = new MessageReference(account.getUuid(), segmentList.get(1), segmentList.get(2), null); + String folderName = segmentList.get(1); + String messageUid = segmentList.get(2); + mMessageReference = new MessageReference(account.getUuid(), folderName, messageUid, null); break; } } diff --git a/k9mail/src/main/java/com/fsck/k9/fragment/MessageListFragment.java b/k9mail/src/main/java/com/fsck/k9/fragment/MessageListFragment.java index 29049d587..9f681cff5 100644 --- a/k9mail/src/main/java/com/fsck/k9/fragment/MessageListFragment.java +++ b/k9mail/src/main/java/com/fsck/k9/fragment/MessageListFragment.java @@ -2992,7 +2992,11 @@ public class MessageListFragment extends Fragment implements OnItemClickListener for (int i = 0, len = mAdapter.getCount(); i < len; i++) { Cursor cursor = (Cursor) mAdapter.getItem(i); - MessageReference ref = new MessageReference(cursor.getString(ACCOUNT_UUID_COLUMN), cursor.getString(FOLDER_NAME_COLUMN), cursor.getString(UID_COLUMN), null); + String accountUuid = cursor.getString(ACCOUNT_UUID_COLUMN); + String folderName = cursor.getString(FOLDER_NAME_COLUMN); + String messageUid = cursor.getString(UID_COLUMN); + MessageReference ref = new MessageReference(accountUuid, folderName, messageUid, null); + messageRefs.add(ref); } @@ -3054,9 +3058,11 @@ public class MessageListFragment extends Fragment implements OnItemClickListener private MessageReference getReferenceForPosition(int position) { Cursor cursor = (Cursor) mAdapter.getItem(position); - MessageReference ref = new MessageReference(cursor.getString(ACCOUNT_UUID_COLUMN), cursor.getString(FOLDER_NAME_COLUMN), cursor.getString(UID_COLUMN), null); - return ref; + String accountUuid = cursor.getString(ACCOUNT_UUID_COLUMN); + String folderName = cursor.getString(FOLDER_NAME_COLUMN); + String messageUid = cursor.getString(UID_COLUMN); + return new MessageReference(accountUuid, folderName, messageUid, null); } private void openMessageAtPosition(int position) { From 9275bb2943b444cdc9666db409e1b82649cc9a4b Mon Sep 17 00:00:00 2001 From: cketti Date: Fri, 3 Apr 2015 20:35:33 +0200 Subject: [PATCH 6/8] Remove unused constructor --- .../java/com/fsck/k9/activity/MessageReference.java | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/k9mail/src/main/java/com/fsck/k9/activity/MessageReference.java b/k9mail/src/main/java/com/fsck/k9/activity/MessageReference.java index 6f5084546..12bd48549 100644 --- a/k9mail/src/main/java/com/fsck/k9/activity/MessageReference.java +++ b/k9mail/src/main/java/com/fsck/k9/activity/MessageReference.java @@ -22,18 +22,6 @@ public class MessageReference implements Parcelable { protected final String uid; protected final Flag flag; - /** - * @deprecated MessageReference should be immutable now, and this way, it won't be possible to modify it anymore - * - * Initialize an empty MessageReference. - */ - @Deprecated - public MessageReference() { - this.accountUuid = null; - this.folderName = null; - this.uid = null; - this.flag = null; - } /** * Initialize a new MessageReference. From a3375d7030ab7b193dd8481797aa044d193ddc41 Mon Sep 17 00:00:00 2001 From: cketti Date: Fri, 3 Apr 2015 20:43:49 +0200 Subject: [PATCH 7/8] Make MessageReference fields private --- .../fsck/k9/activity/MessageReferenceTest.java | 18 +++++++++--------- .../com/fsck/k9/activity/MessageCompose.java | 2 +- .../com/fsck/k9/activity/MessageReference.java | 8 ++++---- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/k9mail/src/androidTest/java/com/fsck/k9/activity/MessageReferenceTest.java b/k9mail/src/androidTest/java/com/fsck/k9/activity/MessageReferenceTest.java index 81c3e80c3..3ad1ba75b 100644 --- a/k9mail/src/androidTest/java/com/fsck/k9/activity/MessageReferenceTest.java +++ b/k9mail/src/androidTest/java/com/fsck/k9/activity/MessageReferenceTest.java @@ -36,27 +36,27 @@ public class MessageReferenceTest { public void parseIdentityStringWithoutFlag() throws MessagingException { MessageReference messageReference = new MessageReference("!:byBoYWkh:Zm9sZGVy:MTAxMDEwMTA="); - assertEquals("o hai!", messageReference.accountUuid); - assertEquals("folder", messageReference.folderName); - assertEquals("10101010", messageReference.uid); - assertNull(messageReference.flag); + assertEquals("o hai!", messageReference.getAccountUuid()); + assertEquals("folder", messageReference.getFolderName()); + assertEquals("10101010", messageReference.getUid()); + assertNull(messageReference.getFlag()); } @Test public void parseIdentityStringWithFlag() throws MessagingException { MessageReference messageReference = new MessageReference("!:byBoYWkh:Zm9sZGVy:MTAxMDEwMTA=:ANSWERED"); - assertEquals("o hai!", messageReference.accountUuid); - assertEquals("folder", messageReference.folderName); - assertEquals("10101010", messageReference.uid); - assertEquals(Flag.ANSWERED, messageReference.flag); + assertEquals("o hai!", messageReference.getAccountUuid()); + assertEquals("folder", messageReference.getFolderName()); + assertEquals("10101010", messageReference.getUid()); + assertEquals(Flag.ANSWERED, messageReference.getFlag()); } @Test public void parseIdentityStringContainingBadVersionNumber() throws MessagingException { MessageReference messageReference = new MessageReference("@:byBoYWkh:Zm9sZGVy:MTAxMDEwMTA=:ANSWERED"); - assertNull(messageReference.accountUuid); + assertNull(messageReference.getAccountUuid()); } @Test(expected = MessagingException.class) diff --git a/k9mail/src/main/java/com/fsck/k9/activity/MessageCompose.java b/k9mail/src/main/java/com/fsck/k9/activity/MessageCompose.java index 3dc3dcb88..d3e7091b0 100644 --- a/k9mail/src/main/java/com/fsck/k9/activity/MessageCompose.java +++ b/k9mail/src/main/java/com/fsck/k9/activity/MessageCompose.java @@ -2942,7 +2942,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, @Override public void loadMessageForViewFinished(Account account, String folder, String uid, LocalMessage message) { - if ((mMessageReference == null) || !mMessageReference.uid.equals(uid)) { + if ((mMessageReference == null) || !mMessageReference.getUid().equals(uid)) { return; } diff --git a/k9mail/src/main/java/com/fsck/k9/activity/MessageReference.java b/k9mail/src/main/java/com/fsck/k9/activity/MessageReference.java index 12bd48549..2bbdbd912 100644 --- a/k9mail/src/main/java/com/fsck/k9/activity/MessageReference.java +++ b/k9mail/src/main/java/com/fsck/k9/activity/MessageReference.java @@ -17,10 +17,10 @@ import com.fsck.k9.mail.filter.Base64; import java.util.StringTokenizer; public class MessageReference implements Parcelable { - protected final String accountUuid; - protected final String folderName; - protected final String uid; - protected final Flag flag; + private final String accountUuid; + private final String folderName; + private final String uid; + private final Flag flag; /** From 24b61e07434c9bbac602e0c0fb886234d9ebb5e8 Mon Sep 17 00:00:00 2001 From: cketti Date: Fri, 3 Apr 2015 21:33:38 +0200 Subject: [PATCH 8/8] Fix code formatting --- .../fsck/k9/activity/MessageReference.java | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/k9mail/src/main/java/com/fsck/k9/activity/MessageReference.java b/k9mail/src/main/java/com/fsck/k9/activity/MessageReference.java index 2bbdbd912..e21e10112 100644 --- a/k9mail/src/main/java/com/fsck/k9/activity/MessageReference.java +++ b/k9mail/src/main/java/com/fsck/k9/activity/MessageReference.java @@ -71,8 +71,9 @@ public class MessageReference implements Parcelable { } } - if (K9.DEBUG) + if (K9.DEBUG) { Log.d(K9.LOG_TAG, "Thawed " + toString()); + } } else { throw new MessagingException("Invalid MessageReference in " + identity + " identity."); } @@ -111,7 +112,7 @@ public class MessageReference implements Parcelable { if (o instanceof MessageReference == false) { return false; } - MessageReference other = (MessageReference)o; + MessageReference other = (MessageReference) o; if ((accountUuid == other.accountUuid || (accountUuid != null && accountUuid.equals(other.accountUuid))) && (folderName == other.folderName || (folderName != null && folderName.equals(other.folderName))) && (uid == other.uid || (uid != null && uid.equals(other.uid)))) { @@ -174,10 +175,9 @@ public class MessageReference implements Parcelable { String accountUuid = source.readString(); String folderName = source.readString(); String flag = source.readString(); - if (flag != null){ + if (flag != null) { ref = new MessageReference(uid, accountUuid, folderName, Flag.valueOf(flag)); - } - else { + } else { ref = new MessageReference(uid, accountUuid, folderName, null); } return ref; @@ -202,27 +202,27 @@ public class MessageReference implements Parcelable { dest.writeString(flag == null ? null : flag.name()); } - public String getAccountUuid(){ + public String getAccountUuid() { return accountUuid; } - public String getFolderName(){ + public String getFolderName() { return folderName; } - public String getUid(){ + public String getUid() { return uid; } - public Flag getFlag(){ + public Flag getFlag() { return flag; } - public MessageReference withModifiedUid(String newUid){ + public MessageReference withModifiedUid(String newUid) { return new MessageReference(accountUuid, folderName, newUid, flag); } - public MessageReference withModifiedFlag(Flag newFlag){ + public MessageReference withModifiedFlag(Flag newFlag) { return new MessageReference(accountUuid, folderName, uid, newFlag); } }