diff --git a/src/com/fsck/k9/Account.java b/src/com/fsck/k9/Account.java index faf9d1e7c..ce565138b 100644 --- a/src/com/fsck/k9/Account.java +++ b/src/com/fsck/k9/Account.java @@ -338,7 +338,7 @@ public class Account implements BaseAccount { * Pick a nice Android guidelines color if we haven't used them all yet. */ private int pickColor(Context context) { - Account[] accounts = Preferences.getPreferences(context).getAccounts(); + List accounts = Preferences.getPreferences(context).getAccounts(); List availableColors = new ArrayList(PREDEFINED_COLORS.length); Collections.addAll(availableColors, PREDEFINED_COLORS); @@ -620,8 +620,8 @@ public class Account implements BaseAccount { } public static List getExistingAccountNumbers(Preferences preferences) { - Account[] accounts = preferences.getAccounts(); - List accountNumbers = new ArrayList(accounts.length); + List accounts = preferences.getAccounts(); + List accountNumbers = new ArrayList(accounts.size()); for (Account a : accounts) { accountNumbers.add(a.getAccountNumber()); } @@ -679,10 +679,10 @@ public class Account implements BaseAccount { * * I bet there is a much smarter way to do this. Anyone like to suggest it? */ - Account[] accounts = preferences.getAccounts(); - int[] accountNumbers = new int[accounts.length]; - for (int i = 0; i < accounts.length; i++) { - accountNumbers[i] = accounts[i].getAccountNumber(); + List accounts = preferences.getAccounts(); + int[] accountNumbers = new int[accounts.size()]; + for (int i = 0; i < accounts.size(); i++) { + accountNumbers[i] = accounts.get(i).getAccountNumber(); } Arrays.sort(accountNumbers); for (int accountNumber : accountNumbers) { diff --git a/src/com/fsck/k9/Preferences.java b/src/com/fsck/k9/Preferences.java index 51b83d873..c5d9b9a37 100644 --- a/src/com/fsck/k9/Preferences.java +++ b/src/com/fsck/k9/Preferences.java @@ -3,6 +3,7 @@ package com.fsck.k9; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.LinkedList; import java.util.List; @@ -18,11 +19,6 @@ import com.fsck.k9.preferences.Storage; public class Preferences { - /** - * Immutable empty {@link Account} array - */ - private static final Account[] EMPTY_ACCOUNT_ARRAY = new Account[0]; - private static Preferences preferences; public static synchronized Preferences getPreferences(Context context) { @@ -75,12 +71,12 @@ public class Preferences { * registered the method returns an empty array. * @return all accounts */ - public synchronized Account[] getAccounts() { + public synchronized List getAccounts() { if (accounts == null) { loadAccounts(); } - return accountsInOrder.toArray(EMPTY_ACCOUNT_ARRAY); + return Collections.unmodifiableList(accountsInOrder); } /** @@ -89,7 +85,7 @@ public class Preferences { * @return all accounts with {@link Account#isAvailable(Context)} */ public synchronized Collection getAvailableAccounts() { - Account[] allAccounts = getAccounts(); + List allAccounts = getAccounts(); Collection retval = new ArrayList(accounts.size()); for (Account account : allAccounts) { if (account.isEnabled() && account.isAvailable(mContext)) { diff --git a/src/com/fsck/k9/activity/AccountList.java b/src/com/fsck/k9/activity/AccountList.java index d5f29b32e..571dfc907 100644 --- a/src/com/fsck/k9/activity/AccountList.java +++ b/src/com/fsck/k9/activity/AccountList.java @@ -68,7 +68,7 @@ public abstract class AccountList extends K9ListActivity implements OnItemClickL * @param realAccounts * An array of accounts to display. */ - public void populateListView(Account[] realAccounts) { + public void populateListView(List realAccounts) { List accounts = new ArrayList(); if (displaySpecialAccounts() && !K9.isHideSpecialAccounts()) { @@ -79,7 +79,7 @@ public abstract class AccountList extends K9ListActivity implements OnItemClickL accounts.add(allMessagesAccount); } - accounts.addAll(Arrays.asList(realAccounts)); + accounts.addAll(realAccounts); AccountsAdapter adapter = new AccountsAdapter(accounts); ListView listView = getListView(); listView.setAdapter(adapter); @@ -169,15 +169,15 @@ public abstract class AccountList extends K9ListActivity implements OnItemClickL /** * Load accounts in a background thread */ - class LoadAccounts extends AsyncTask { + class LoadAccounts extends AsyncTask> { @Override - protected Account[] doInBackground(Void... params) { - Account[] accounts = Preferences.getPreferences(getApplicationContext()).getAccounts(); + protected List doInBackground(Void... params) { + List accounts = Preferences.getPreferences(getApplicationContext()).getAccounts(); return accounts; } @Override - protected void onPostExecute(Account[] accounts) { + protected void onPostExecute(List accounts) { populateListView(accounts); } } diff --git a/src/com/fsck/k9/activity/Accounts.java b/src/com/fsck/k9/activity/Accounts.java index 0b9a76f06..5331890d6 100644 --- a/src/com/fsck/k9/activity/Accounts.java +++ b/src/com/fsck/k9/activity/Accounts.java @@ -102,11 +102,6 @@ import de.cketti.library.changelog.ChangeLog; public class Accounts extends K9ListActivity implements OnItemClickListener { - /** - * Immutable empty {@link BaseAccount} array - */ - private static final BaseAccount[] EMPTY_BASE_ACCOUNT_ARRAY = new BaseAccount[0]; - /** * URL used to open Android Market application */ @@ -400,14 +395,14 @@ public class Accounts extends K9ListActivity implements OnItemClickListener { createSpecialAccounts(); } - Account[] accounts = Preferences.getPreferences(this).getAccounts(); + List accounts = Preferences.getPreferences(this).getAccounts(); Intent intent = getIntent(); //onNewIntent(intent); // see if we should show the welcome message if (ACTION_IMPORT_SETTINGS.equals(intent.getAction())) { onImport(); - } else if (accounts.length < 1) { + } else if (accounts.size() < 1) { WelcomeMessage.showWelcomeMessage(this); finish(); return; @@ -423,7 +418,7 @@ public class Accounts extends K9ListActivity implements OnItemClickListener { onOpenAccount(mUnifiedInboxAccount); finish(); return; - } else if (startup && accounts.length == 1 && onOpenAccount(accounts[0])) { + } else if (startup && accounts.size() == 1 && onOpenAccount(accounts.get(0))) { finish(); return; } @@ -543,18 +538,18 @@ public class Accounts extends K9ListActivity implements OnItemClickListener { return retain; } - private BaseAccount[] accounts = new BaseAccount[0]; + private List accounts = new ArrayList(); private enum ACCOUNT_LOCATION { TOP, MIDDLE, BOTTOM; } private EnumSet accountLocation(BaseAccount account) { EnumSet accountLocation = EnumSet.of(ACCOUNT_LOCATION.MIDDLE); - if (accounts.length > 0) { - if (accounts[0].equals(account)) { + if (accounts.size() > 0) { + if (accounts.get(0).equals(account)) { accountLocation.remove(ACCOUNT_LOCATION.MIDDLE); accountLocation.add(ACCOUNT_LOCATION.TOP); } - if (accounts[accounts.length - 1].equals(account)) { + if (accounts.get(accounts.size() - 1).equals(account)) { accountLocation.remove(ACCOUNT_LOCATION.MIDDLE); accountLocation.add(ACCOUNT_LOCATION.BOTTOM); } @@ -564,7 +559,8 @@ public class Accounts extends K9ListActivity implements OnItemClickListener { private void refresh() { - accounts = Preferences.getPreferences(this).getAccounts(); + accounts.clear(); + accounts.addAll(Preferences.getPreferences(this).getAccounts()); // see if we should show the welcome message // if (accounts.length < 1) { @@ -573,22 +569,22 @@ public class Accounts extends K9ListActivity implements OnItemClickListener { // } List newAccounts; - if (!K9.isHideSpecialAccounts() && accounts.length > 0) { + if (!K9.isHideSpecialAccounts() && accounts.size() > 0) { if (mUnifiedInboxAccount == null || mAllMessagesAccount == null) { createSpecialAccounts(); } - newAccounts = new ArrayList(accounts.length + + newAccounts = new ArrayList(accounts.size() + SPECIAL_ACCOUNTS_COUNT); newAccounts.add(mUnifiedInboxAccount); newAccounts.add(mAllMessagesAccount); } else { - newAccounts = new ArrayList(accounts.length); + newAccounts = new ArrayList(accounts.size()); } - newAccounts.addAll(Arrays.asList(accounts)); + newAccounts.addAll(accounts); - mAdapter = new AccountsAdapter(newAccounts.toArray(EMPTY_BASE_ACCOUNT_ARRAY)); + mAdapter = new AccountsAdapter(newAccounts); getListView().setAdapter(mAdapter); if (!newAccounts.isEmpty()) { mHandler.progress(Window.PROGRESS_START); @@ -1737,7 +1733,7 @@ public class Accounts extends K9ListActivity implements OnItemClickListener { } class AccountsAdapter extends ArrayAdapter { - public AccountsAdapter(BaseAccount[] accounts) { + public AccountsAdapter(List accounts) { super(Accounts.this, 0, accounts); } diff --git a/src/com/fsck/k9/activity/ChooseFolder.java b/src/com/fsck/k9/activity/ChooseFolder.java index 3061b2223..83e8070b8 100644 --- a/src/com/fsck/k9/activity/ChooseFolder.java +++ b/src/com/fsck/k9/activity/ChooseFolder.java @@ -261,7 +261,7 @@ public class ChooseFolder extends K9ListActivity { mHandler.progress(false); } @Override - public void listFolders(Account account, Folder[] folders) { + public void listFolders(Account account, List folders) { if (!account.equals(mAccount)) { return; } diff --git a/src/com/fsck/k9/activity/FolderList.java b/src/com/fsck/k9/activity/FolderList.java index 217658b33..670419cf1 100644 --- a/src/com/fsck/k9/activity/FolderList.java +++ b/src/com/fsck/k9/activity/FolderList.java @@ -740,7 +740,7 @@ public class FolderList extends K9ListActivity { } @Override - public void listFolders(Account account, Folder[] folders) { + public void listFolders(Account account, List folders) { if (account.equals(mAccount)) { List newFolders = new LinkedList(); diff --git a/src/com/fsck/k9/activity/MessageCompose.java b/src/com/fsck/k9/activity/MessageCompose.java index 4a0ab4cfa..a1dbf2873 100644 --- a/src/com/fsck/k9/activity/MessageCompose.java +++ b/src/com/fsck/k9/activity/MessageCompose.java @@ -9,6 +9,7 @@ import java.nio.charset.Charset; import java.text.DateFormat; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Date; import java.util.HashMap; import java.util.List; @@ -191,8 +192,6 @@ public class MessageCompose extends K9Activity implements OnClickListener, private static final int CONTACT_PICKER_CC2 = 8; private static final int CONTACT_PICKER_BCC2 = 9; - private static final Account[] EMPTY_ACCOUNT_ARRAY = new Account[0]; - private static final int REQUEST_CODE_SIGN_ENCRYPT = 12; /** @@ -3916,7 +3915,7 @@ public class MessageCompose extends K9Activity implements OnClickListener, List items = new ArrayList(); Preferences prefs = Preferences.getPreferences(context.getApplicationContext()); - Account[] accounts = prefs.getAvailableAccounts().toArray(EMPTY_ACCOUNT_ARRAY); + Collection accounts = prefs.getAvailableAccounts(); for (Account account : accounts) { items.add(account); List identities = account.getIdentities(); diff --git a/src/com/fsck/k9/activity/MessageList.java b/src/com/fsck/k9/activity/MessageList.java index 3da206094..25306140f 100644 --- a/src/com/fsck/k9/activity/MessageList.java +++ b/src/com/fsck/k9/activity/MessageList.java @@ -451,10 +451,10 @@ public class MessageList extends K9FragmentActivity implements MessageListFragme String[] accountUuids = mSearch.getAccountUuids(); if (mSearch.searchAllAccounts()) { - Account[] accounts = prefs.getAccounts(); - mSingleAccountMode = (accounts.length == 1); + List accounts = prefs.getAccounts(); + mSingleAccountMode = (accounts.size() == 1); if (mSingleAccountMode) { - mAccount = accounts[0]; + mAccount = accounts.get(0); } } else { mSingleAccountMode = (accountUuids.length == 1); diff --git a/src/com/fsck/k9/cache/EmailProviderCache.java b/src/com/fsck/k9/cache/EmailProviderCache.java index 972d3abdb..1cdf70103 100644 --- a/src/com/fsck/k9/cache/EmailProviderCache.java +++ b/src/com/fsck/k9/cache/EmailProviderCache.java @@ -143,7 +143,7 @@ public class EmailProviderCache { } } - public void unhideMessages(Message[] messages) { + public void unhideMessages(List messages) { synchronized (mHiddenMessageCache) { for (Message message : messages) { LocalMessage localMessage = (LocalMessage) message; diff --git a/src/com/fsck/k9/controller/MessagingController.java b/src/com/fsck/k9/controller/MessagingController.java index 49ecfea36..77eca6d49 100644 --- a/src/com/fsck/k9/controller/MessagingController.java +++ b/src/com/fsck/k9/controller/MessagingController.java @@ -8,6 +8,7 @@ import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.Date; +import java.util.EnumSet; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -116,16 +117,6 @@ public class MessagingController implements Runnable { */ private static final String[] EMPTY_STRING_ARRAY = new String[0]; - /** - * Immutable empty {@link Message} array - */ - private static final Message[] EMPTY_MESSAGE_ARRAY = new Message[0]; - - /** - * Immutable empty {@link Folder} array - */ - private static final Folder[] EMPTY_FOLDER_ARRAY = new Folder[0]; - /** * The maximum message size that we'll consider to be "small". A small message is downloaded * in full immediately instead of in pieces. Anything over this size will be downloaded in @@ -321,8 +312,7 @@ public class MessagingController implements Runnable { // Key is accountNumber private final ConcurrentMap notificationData = new ConcurrentHashMap(); - private static final Flag[] SYNC_FLAGS = new Flag[] { Flag.SEEN, Flag.FLAGGED, Flag.ANSWERED, Flag.FORWARDED }; - + private static final Set SYNC_FLAGS = EnumSet.of(Flag.SEEN, Flag.FLAGGED, Flag.ANSWERED, Flag.FORWARDED); private void suppressMessages(Account account, List messages) { EmailProviderCache cache = EmailProviderCache.getCache(account.getUuid(), @@ -330,7 +320,7 @@ public class MessagingController implements Runnable { cache.hideMessages(messages); } - private void unsuppressMessages(Account account, Message[] messages) { + private void unsuppressMessages(Account account, List messages) { EmailProviderCache cache = EmailProviderCache.getCache(account.getUuid(), mApplication.getApplicationContext()); cache.unhideMessages(messages); @@ -574,7 +564,7 @@ public class MessagingController implements Runnable { Store localStore = account.getLocalStore(); localFolders = localStore.getPersonalNamespaces(false); - Folder[] folderArray = localFolders.toArray(EMPTY_FOLDER_ARRAY); + List folderArray = localFolders; if (refreshRemote || localFolders.isEmpty()) { doRefreshRemote(account, listener); @@ -654,7 +644,7 @@ public class MessagingController implements Runnable { } localFolders = localStore.getPersonalNamespaces(false); - Folder[] folderArray = localFolders.toArray(EMPTY_FOLDER_ARRAY); + List folderArray = localFolders; for (MessagingListener l : getListeners(listener)) { l.listFolders(account, folderArray); @@ -694,7 +684,7 @@ public class MessagingController implements Runnable { public void searchLocalMessagesSynchronous(final LocalSearch search, final MessagingListener listener) { final AccountStats stats = new AccountStats(); final Set uuidSet = new HashSet(Arrays.asList(search.getAccountUuids())); - Account[] accounts = Preferences.getPreferences(mApplication.getApplicationContext()).getAccounts(); + List accounts = Preferences.getPreferences(mApplication.getApplicationContext()).getAccounts(); boolean allAccounts = uuidSet.contains(SearchSpecification.ALL_ACCOUNTS); // for every account we want to search do the query in the localstore @@ -755,7 +745,7 @@ public class MessagingController implements Runnable { public Future searchRemoteMessages(final String acctUuid, final String folderName, final String query, - final Flag[] requiredFlags, final Flag[] forbiddenFlags, final MessagingListener listener) { + final Set requiredFlags, final Set forbiddenFlags, final MessagingListener listener) { if (K9.DEBUG) { String msg = "searchRemoteMessages (" + "acct=" + acctUuid @@ -773,7 +763,7 @@ public class MessagingController implements Runnable { }); } public void searchRemoteMessagesSynchronous(final String acctUuid, final String folderName, final String query, - final Flag[] requiredFlags, final Flag[] forbiddenFlags, final MessagingListener listener) { + final Set requiredFlags, final Set forbiddenFlags, final MessagingListener listener) { final Account acct = Preferences.getPreferences(mApplication.getApplicationContext()).getAccount(acctUuid); if (listener != null) { @@ -885,10 +875,10 @@ public class MessagingController implements Runnable { LocalMessage localMsg = localFolder.getMessage(message.getUid()); if (localMsg == null) { - remoteFolder.fetch(new Message [] {message}, header, null); + remoteFolder.fetch(Collections.singletonList(message), header, null); //fun fact: ImapFolder.fetch can't handle getting STRUCTURE at same time as headers - remoteFolder.fetch(new Message [] {message}, structure, null); - localFolder.appendMessages(new Message [] {message}); + remoteFolder.fetch(Collections.singletonList(message), structure, null); + localFolder.appendMessages(Collections.singletonList(message)); localMsg = localFolder.getMessage(message.getUid()); } @@ -992,7 +982,7 @@ public class MessagingController implements Runnable { final LocalFolder localFolder = tLocalFolder; localFolder.open(Folder.OPEN_MODE_RW); localFolder.updateLastUid(); - Message[] localMessages = localFolder.getMessages(null); + List localMessages = localFolder.getMessages(null); Map localUidMap = new HashMap(); for (Message message : localMessages) { localUidMap.put(message.getUid(), message); @@ -1058,7 +1048,6 @@ public class MessagingController implements Runnable { visibleLimit = K9.DEFAULT_VISIBLE_LIMIT; } - Message[] remoteMessageArray = EMPTY_MESSAGE_ARRAY; final List remoteMessages = new ArrayList(); Map remoteUidMap = new HashMap(); @@ -1086,9 +1075,9 @@ public class MessagingController implements Runnable { } - remoteMessageArray = remoteFolder.getMessages(remoteStart, remoteEnd, earliestDate, null); + List remoteMessageArray = remoteFolder.getMessages(remoteStart, remoteEnd, earliestDate, null); - int messageCount = remoteMessageArray.length; + int messageCount = remoteMessageArray.size(); for (Message thisMess : remoteMessageArray) { headerProgress.incrementAndGet(); @@ -1104,7 +1093,6 @@ public class MessagingController implements Runnable { if (K9.DEBUG) Log.v(K9.LOG_TAG, "SYNC: Got " + remoteUidMap.size() + " messages for folder " + folder); - remoteMessageArray = null; for (MessagingListener l : getListeners(listener)) { l.synchronizeMailboxHeadersFinished(account, folder, headerProgress.get(), remoteUidMap.size()); } @@ -1125,7 +1113,7 @@ public class MessagingController implements Runnable { } - localFolder.destroyMessages(destroyMessages.toArray(EMPTY_MESSAGE_ARRAY)); + localFolder.destroyMessages(destroyMessages); for (Message destroyMessage : destroyMessages) { for (MessagingListener l : getListeners(listener)) { @@ -1437,7 +1425,7 @@ public class MessagingController implements Runnable { Log.v(K9.LOG_TAG, "Message with uid " + message.getUid() + " is partially or fully downloaded"); // Store the updated message locally - localFolder.appendMessages(new Message[] { message }); + localFolder.appendMessages(Collections.singletonList(message)); localMessage = localFolder.getMessage(message.getUid()); @@ -1489,7 +1477,7 @@ public class MessagingController implements Runnable { */ final List chunk = new ArrayList(UNSYNC_CHUNK_SIZE); - remoteFolder.fetch(unsyncedMessages.toArray(EMPTY_MESSAGE_ARRAY), fp, + remoteFolder.fetch(unsyncedMessages, fp, new MessageRetrievalListener() { @Override public void messageFinished(Message message, int number, int ofTotal) { @@ -1578,7 +1566,7 @@ public class MessagingController implements Runnable { } try { // Store the new message locally - localFolder.appendMessages(messages.toArray(new Message[messages.size()])); + localFolder.appendMessages(messages); for (final Message message : messages) { final Message localMessage = localFolder.getMessage(message.getUid()); @@ -1624,7 +1612,7 @@ public class MessagingController implements Runnable { if (K9.DEBUG) Log.d(K9.LOG_TAG, "SYNC: Fetching small messages for folder " + folder); - remoteFolder.fetch(smallMessages.toArray(new Message[smallMessages.size()]), + remoteFolder.fetch(smallMessages, fp, new MessageRetrievalListener() { @Override public void messageFinished(final Message message, int number, int ofTotal) { @@ -1703,7 +1691,7 @@ public class MessagingController implements Runnable { if (K9.DEBUG) Log.d(K9.LOG_TAG, "SYNC: Fetching large messages for folder " + folder); - remoteFolder.fetch(largeMessages.toArray(new Message[largeMessages.size()]), fp, null); + remoteFolder.fetch(largeMessages, fp, null); for (Message message : largeMessages) { if (!shouldImportMessage(account, folder, message, progress, earliestDate)) { @@ -1726,10 +1714,10 @@ public class MessagingController implements Runnable { * they equal we can mark this SYNCHRONIZED instead of PARTIALLY_SYNCHRONIZED */ - remoteFolder.fetch(new Message[] { message }, fp, null); + remoteFolder.fetch(Collections.singletonList(message), fp, null); // Store the updated message locally - localFolder.appendMessages(new Message[] { message }); + localFolder.appendMessages(Collections.singletonList(message)); Message localMessage = localFolder.getMessage(message.getUid()); @@ -1770,7 +1758,7 @@ public class MessagingController implements Runnable { remoteFolder.fetchPart(message, part, null); } // Store the updated message locally - localFolder.appendMessages(new Message[] { message }); + localFolder.appendMessages(Collections.singletonList(message)); Message localMessage = localFolder.getMessage(message.getUid()); @@ -1836,7 +1824,7 @@ public class MessagingController implements Runnable { } } - remoteFolder.fetch(undeletedMessages.toArray(EMPTY_MESSAGE_ARRAY), fp, null); + remoteFolder.fetch(undeletedMessages, fp, null); for (Message remoteMessage : syncFlagMessages) { Message localMessage = localFolder.getMessage(remoteMessage.getUid()); boolean messageChanged = syncFlags(localMessage, remoteMessage); @@ -2111,10 +2099,10 @@ public class MessagingController implements Runnable { */ FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.BODY); - localFolder.fetch(new Message[] { localMessage } , fp, null); + localFolder.fetch(Collections.singletonList(localMessage) , fp, null); String oldUid = localMessage.getUid(); localMessage.setFlag(Flag.X_REMOTE_COPY_STARTED, true); - remoteFolder.appendMessages(new Message[] { localMessage }); + remoteFolder.appendMessages(Collections.singletonList(localMessage)); localFolder.changeUid(localMessage); for (MessagingListener l : getListeners()) { @@ -2129,7 +2117,7 @@ public class MessagingController implements Runnable { */ FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.ENVELOPE); - remoteFolder.fetch(new Message[] { remoteMessage }, fp, null); + remoteFolder.fetch(Collections.singletonList(remoteMessage), fp, null); Date localDate = localMessage.getInternalDate(); Date remoteDate = remoteMessage.getInternalDate(); if (remoteDate != null && remoteDate.compareTo(localDate) > 0) { @@ -2146,12 +2134,12 @@ public class MessagingController implements Runnable { fp.clear(); fp = new FetchProfile(); fp.add(FetchProfile.Item.BODY); - localFolder.fetch(new Message[] { localMessage }, fp, null); + localFolder.fetch(Collections.singletonList(localMessage), fp, null); String oldUid = localMessage.getUid(); localMessage.setFlag(Flag.X_REMOTE_COPY_STARTED, true); - remoteFolder.appendMessages(new Message[] { localMessage }); + remoteFolder.appendMessages(Collections.singletonList(localMessage)); localFolder.changeUid(localMessage); for (MessagingListener l : getListeners()) { l.messageUidChanged(account, folder, oldUid, localMessage.getUid()); @@ -2325,14 +2313,14 @@ public class MessagingController implements Runnable { if (K9.FOLDER_NONE.equals(destFolderName)) { destFolderName = null; } - remoteSrcFolder.delete(messages.toArray(EMPTY_MESSAGE_ARRAY), destFolderName); + remoteSrcFolder.delete(messages, destFolderName); } else { remoteDestFolder = remoteStore.getFolder(destFolder); if (isCopy) { - remoteUidMap = remoteSrcFolder.copyMessages(messages.toArray(EMPTY_MESSAGE_ARRAY), remoteDestFolder); + remoteUidMap = remoteSrcFolder.copyMessages(messages, remoteDestFolder); } else { - remoteUidMap = remoteSrcFolder.moveMessages(messages.toArray(EMPTY_MESSAGE_ARRAY), remoteDestFolder); + remoteUidMap = remoteSrcFolder.moveMessages(messages, remoteDestFolder); } } if (!isCopy && Account.EXPUNGE_IMMEDIATELY.equals(account.getExpungePolicy())) { @@ -2425,7 +2413,7 @@ public class MessagingController implements Runnable { if (messages.isEmpty()) { return; } - remoteFolder.setFlags(messages.toArray(EMPTY_MESSAGE_ARRAY), new Flag[] { flag }, newState); + remoteFolder.setFlags(messages, Collections.singletonList(flag), newState); } finally { closeFolder(remoteFolder); } @@ -2571,9 +2559,9 @@ public class MessagingController implements Runnable { } if (isCopy) { - remoteSrcFolder.copyMessages(new Message[] { remoteMessage }, remoteDestFolder); + remoteSrcFolder.copyMessages(Collections.singletonList(remoteMessage), remoteDestFolder); } else { - remoteSrcFolder.moveMessages(new Message[] { remoteMessage }, remoteDestFolder); + remoteSrcFolder.moveMessages(Collections.singletonList(remoteMessage), remoteDestFolder); } remoteSrcFolder.close(); remoteDestFolder.close(); @@ -2587,7 +2575,7 @@ public class MessagingController implements Runnable { Store localStore = account.getLocalStore(); localFolder = (LocalFolder) localStore.getFolder(folder); localFolder.open(Folder.OPEN_MODE_RW); - Message[] messages = localFolder.getMessages(null, false); + List messages = localFolder.getMessages(null, false); for (Message message : messages) { if (!message.isSet(Flag.SEEN)) { message.setFlag(Flag.SEEN, true); @@ -2617,7 +2605,7 @@ public class MessagingController implements Runnable { return; } - remoteFolder.setFlags(new Flag[] {Flag.SEEN}, true); + remoteFolder.setFlags(Collections.singletonList(Flag.SEEN), true); remoteFolder.close(); } catch (UnsupportedOperationException uoe) { Log.w(K9.LOG_TAG, "Could not mark all server-side as read because store doesn't support operation", uoe); @@ -2727,7 +2715,7 @@ public class MessagingController implements Runnable { Store localStore = account.getLocalStore(); LocalFolder localFolder = (LocalFolder)localStore.getFolder(account.getErrorFolderName()); - Message[] messages = new Message[1]; + List messages = new ArrayList(); MimeMessage message = new MimeMessage(); @@ -2740,7 +2728,7 @@ public class MessagingController implements Runnable { message.setInternalDate(nowDate); message.addSentDate(nowDate); message.setFrom(new Address(account.getEmail(), "K9mail internal")); - messages[0] = message; + messages.set(0, message); localFolder.appendMessages(messages); @@ -2874,7 +2862,7 @@ public class MessagingController implements Runnable { * @param newState * {@code true}, if the flag should be set. {@code false} if it should be removed. */ - public void setFlag(Account account, String folderName, Message[] messages, Flag flag, + public void setFlag(Account account, String folderName, List messages, Flag flag, boolean newState) { // TODO: Put this into the background, but right now some callers depend on the message // objects being modified right after this method returns. @@ -2896,7 +2884,7 @@ public class MessagingController implements Runnable { } // Update the messages in the local store - localFolder.setFlags(messages, new Flag[] {flag}, newState); + localFolder.setFlags(messages, Collections.singletonList(flag), newState); int unreadMessageCount = localFolder.getUnreadMessageCount(); for (MessagingListener l : getListeners()) { @@ -2914,9 +2902,9 @@ public class MessagingController implements Runnable { return; } - String[] uids = new String[messages.length]; + String[] uids = new String[messages.size()]; for (int i = 0, end = uids.length; i < end; i++) { - uids[i] = messages[i].getUid(); + uids[i] = messages.get(i).getUid(); } queueSetFlag(account, folderName, Boolean.toString(newState), flag.toString(), uids); @@ -2953,7 +2941,7 @@ public class MessagingController implements Runnable { Message message = localFolder.getMessage(uid); if (message != null) { - setFlag(account, folderName, new Message[] { message }, flag, newState); + setFlag(account, folderName, Collections.singletonList(message), flag, newState); } } catch (MessagingException me) { addErrorMessage(account, null, me); @@ -3026,7 +3014,7 @@ public class MessagingController implements Runnable { FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.ENVELOPE); fp.add(FetchProfile.Item.BODY); - localFolder.fetch(new Message[] { message }, fp, null); + localFolder.fetch(Collections.singletonList(message), fp, null); } else { /* * At this point the message is not available, so we need to download it @@ -3042,16 +3030,16 @@ public class MessagingController implements Runnable { FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.BODY); - remoteFolder.fetch(new Message[] { remoteMessage }, fp, null); + remoteFolder.fetch(Collections.singletonList(remoteMessage), fp, null); // Store the message locally and load the stored message into memory - localFolder.appendMessages(new Message[] { remoteMessage }); + localFolder.appendMessages(Collections.singletonList(remoteMessage)); if (loadPartialFromSearch) { fp.add(FetchProfile.Item.BODY); } fp.add(FetchProfile.Item.ENVELOPE); message = localFolder.getMessage(uid); - localFolder.fetch(new Message[] { message }, fp, null); + localFolder.fetch(Collections.singletonList(message), fp, null); // Mark that this message is now fully synched if (account.isMarkMessageAsReadOnView()) { @@ -3124,9 +3112,7 @@ public class MessagingController implements Runnable { FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.ENVELOPE); fp.add(FetchProfile.Item.BODY); - localFolder.fetch(new Message[] { - message - }, fp, null); + localFolder.fetch(Collections.singletonList(message), fp, null); localFolder.close(); for (MessagingListener l : getListeners(listener)) { @@ -3266,7 +3252,7 @@ public class MessagingController implements Runnable { LocalStore localStore = account.getLocalStore(); LocalFolder localFolder = localStore.getFolder(account.getOutboxFolderName()); localFolder.open(Folder.OPEN_MODE_RW); - localFolder.appendMessages(new Message[] { message }); + localFolder.appendMessages(Collections.singletonList(message)); Message localMessage = localFolder.getMessage(message.getUid()); localMessage.setFlag(Flag.X_DOWNLOADED_FULL, true); localFolder.close(); @@ -3504,9 +3490,9 @@ public class MessagingController implements Runnable { } localFolder.open(Folder.OPEN_MODE_RW); - Message[] localMessages = localFolder.getMessages(null); + List localMessages = localFolder.getMessages(null); int progress = 0; - int todo = localMessages.length; + int todo = localMessages.size(); for (MessagingListener l : getListeners()) { l.synchronizeMailboxProgress(account, account.getSentFolderName(), progress, todo); } @@ -3545,7 +3531,7 @@ public class MessagingController implements Runnable { - localFolder.fetch(new Message[] { message }, fp, null); + localFolder.fetch(Collections.singletonList(message), fp, null); try { @@ -3576,7 +3562,7 @@ public class MessagingController implements Runnable { if (K9.DEBUG) Log.i(K9.LOG_TAG, "Moving sent message to folder '" + account.getSentFolderName() + "' (" + localSentFolder.getId() + ") "); - localFolder.moveMessages(new Message[] { message }, localSentFolder); + localFolder.moveMessages(Collections.singletonList(message), localSentFolder); if (K9.DEBUG) Log.i(K9.LOG_TAG, "Moved sent message to folder '" + account.getSentFolderName() + "' (" + localSentFolder.getId() + ") "); @@ -3594,7 +3580,7 @@ public class MessagingController implements Runnable { // This is a complete hack, but is worlds better than the previous // "don't even bother" functionality if (getRootCauseMessage(e).startsWith("5")) { - localFolder.moveMessages(new Message[] { message }, (LocalFolder) localStore.getFolder(account.getDraftsFolderName())); + localFolder.moveMessages(Collections.singletonList(message), (LocalFolder) localStore.getFolder(account.getDraftsFolderName())); } notifyUserIfCertificateProblem(mApplication, e, account, false); @@ -3679,14 +3665,14 @@ public class MessagingController implements Runnable { // Collect accounts that belong to the search String[] accountUuids = search.getAccountUuids(); - Account[] accounts; + List accounts; if (search.searchAllAccounts()) { accounts = preferences.getAccounts(); } else { - accounts = new Account[accountUuids.length]; + accounts = new ArrayList(accountUuids.length); for (int i = 0, len = accountUuids.length; i < len; i++) { String accountUuid = accountUuids[i]; - accounts[i] = preferences.getAccount(accountUuid); + accounts.set(i, preferences.getAccount(accountUuid)); } } @@ -3895,8 +3881,8 @@ public class MessagingController implements Runnable { } } - Message[] messages = localSrcFolder.getMessages(uids.toArray(EMPTY_STRING_ARRAY), null); - if (messages.length > 0) { + List messages = localSrcFolder.getMessages(uids.toArray(EMPTY_STRING_ARRAY), null); + if (messages.size() > 0) { Map origUidMap = new HashMap(); for (Message message : messages) { @@ -3905,7 +3891,7 @@ public class MessagingController implements Runnable { if (K9.DEBUG) Log.i(K9.LOG_TAG, "moveOrCopyMessageSynchronous: source folder = " + srcFolder - + ", " + messages.length + " messages, " + ", destination folder = " + destFolder + ", isCopy = " + isCopy); + + ", " + messages.size() + " messages, " + ", destination folder = " + destFolder + ", isCopy = " + isCopy); if (isCopy) { FetchProfile fp = new FetchProfile(); @@ -4014,7 +4000,7 @@ public class MessagingController implements Runnable { List messagesToDelete = collectMessagesInThreads(account, messages); deleteMessagesSynchronous(account, folderName, - messagesToDelete.toArray(EMPTY_MESSAGE_ARRAY), null); + messagesToDelete, null); } catch (MessagingException e) { Log.e(K9.LOG_TAG, "Something went wrong while deleting threads", e); } @@ -4031,8 +4017,9 @@ public class MessagingController implements Runnable { long rootId = localMessage.getRootId(); long threadId = (rootId == -1) ? localMessage.getThreadId() : rootId; - Message[] messagesInThread = localStore.getMessagesInThread(threadId); - Collections.addAll(messagesInThreads, messagesInThread); + List messagesInThread = localStore.getMessagesInThread(threadId); + + messagesInThreads.addAll(messagesInThread); } return messagesInThreads; @@ -4050,7 +4037,7 @@ public class MessagingController implements Runnable { @Override public void run() { deleteMessagesSynchronous(account, folder.getName(), - accountMessages.toArray(EMPTY_MESSAGE_ARRAY), listener); + accountMessages, listener); } }); } @@ -4059,7 +4046,7 @@ public class MessagingController implements Runnable { } - private void deleteMessagesSynchronous(final Account account, final String folder, final Message[] messages, + private void deleteMessagesSynchronous(final Account account, final String folder, final List messages, MessagingListener listener) { Folder localFolder = null; Folder localTrashFolder = null; @@ -4079,7 +4066,7 @@ public class MessagingController implements Runnable { if (K9.DEBUG) Log.d(K9.LOG_TAG, "Deleting messages in trash folder or trash set to -None-, not copying"); - localFolder.setFlags(messages, new Flag[] { Flag.DELETED }, true); + localFolder.setFlags(messages, Collections.singleton(Flag.DELETED), true); } else { localTrashFolder = localStore.getFolder(account.getTrashFolderName()); if (!localTrashFolder.exists()) { @@ -4147,10 +4134,10 @@ public class MessagingController implements Runnable { } } - private String[] getUidsFromMessages(Message[] messages) { - String[] uids = new String[messages.length]; - for (int i = 0; i < messages.length; i++) { - uids[i] = messages[i].getUid(); + private String[] getUidsFromMessages(List messages) { + String[] uids = new String[messages.size()]; + for (int i = 0; i < messages.size(); i++) { + uids[i] = messages.get(i).getUid(); } return uids; } @@ -4162,7 +4149,7 @@ public class MessagingController implements Runnable { try { if (remoteFolder.exists()) { remoteFolder.open(Folder.OPEN_MODE_RW); - remoteFolder.setFlags(new Flag [] { Flag.DELETED }, true); + remoteFolder.setFlags(Collections.singleton(Flag.DELETED), true); if (Account.EXPUNGE_IMMEDIATELY.equals(account.getExpungePolicy())) { remoteFolder.expunge(); } @@ -4193,7 +4180,7 @@ public class MessagingController implements Runnable { if (isTrashLocalOnly) { localFolder.clearAllMessages(); } else { - localFolder.setFlags(new Flag[] { Flag.DELETED }, true); + localFolder.setFlags(Collections.singleton(Flag.DELETED), true); } for (MessagingListener l : getListeners()) { @@ -5030,7 +5017,7 @@ public class MessagingController implements Runnable { } private boolean skipAccountsInBackStack(Context context) { - return Preferences.getPreferences(context).getAccounts().length == 1; + return Preferences.getPreferences(context).getAccounts().size() == 1; } /** @@ -5112,9 +5099,7 @@ public class MessagingController implements Runnable { } // Save the message to the store. - localFolder.appendMessages(new Message[] { - message - }); + localFolder.appendMessages(Collections.singletonList(message)); // Fetch the message back from the store. This is the Message that's returned to the caller. localMessage = localFolder.getMessage(message.getUid()); localMessage.setFlag(Flag.X_DOWNLOADED_FULL, true); diff --git a/src/com/fsck/k9/controller/MessagingListener.java b/src/com/fsck/k9/controller/MessagingListener.java index 6fce2c6e2..5c5a66f50 100644 --- a/src/com/fsck/k9/controller/MessagingListener.java +++ b/src/com/fsck/k9/controller/MessagingListener.java @@ -33,7 +33,7 @@ public class MessagingListener { public void listFoldersStarted(Account account) {} - public void listFolders(Account account, Folder[] folders) {} + public void listFolders(Account account, List folders) {} public void listFoldersFinished(Account account) {} diff --git a/src/com/fsck/k9/fragment/MessageListFragment.java b/src/com/fsck/k9/fragment/MessageListFragment.java index b73bfa495..c8dd39d73 100644 --- a/src/com/fsck/k9/fragment/MessageListFragment.java +++ b/src/com/fsck/k9/fragment/MessageListFragment.java @@ -960,16 +960,16 @@ public class MessageListFragment extends Fragment implements OnItemClickListener accountUuids[0].equals(SearchSpecification.ALL_ACCOUNTS)) { mAllAccounts = true; - Account[] accounts = mPreferences.getAccounts(); + List accounts = mPreferences.getAccounts(); - mAccountUuids = new String[accounts.length]; - for (int i = 0, len = accounts.length; i < len; i++) { - mAccountUuids[i] = accounts[i].getUuid(); + mAccountUuids = new String[accounts.size()]; + for (int i = 0, len = accounts.size(); i < len; i++) { + mAccountUuids[i] = accounts.get(i).getUuid(); } if (mAccountUuids.length == 1) { mSingleAccountMode = true; - mAccount = accounts[0]; + mAccount = accounts.get(0); } } else { mAccountUuids = accountUuids; @@ -1084,11 +1084,11 @@ public class MessageListFragment extends Fragment implements OnItemClickListener mController.addListener(mListener); //Cancel pending new mail notifications when we open an account - Account[] accountsWithNotification; + List accountsWithNotification; Account account = mAccount; if (account != null) { - accountsWithNotification = new Account[] { account }; + accountsWithNotification = Collections.singletonList(account); } else { accountsWithNotification = mPreferences.getAccounts(); } diff --git a/src/com/fsck/k9/fragment/MessageViewFragment.java b/src/com/fsck/k9/fragment/MessageViewFragment.java index ec1219780..5af307260 100644 --- a/src/com/fsck/k9/fragment/MessageViewFragment.java +++ b/src/com/fsck/k9/fragment/MessageViewFragment.java @@ -369,7 +369,7 @@ public class MessageViewFragment extends Fragment implements OnClickListener, if (mMessage != null) { boolean newState = !mMessage.isSet(Flag.FLAGGED); mController.setFlag(mAccount, mMessage.getFolder().getName(), - new Message[] { mMessage }, Flag.FLAGGED, newState); + Collections.singletonList(mMessage), Flag.FLAGGED, newState); mMessageView.setHeaders(mMessage, mAccount); } } @@ -485,7 +485,7 @@ public class MessageViewFragment extends Fragment implements OnClickListener, public void onToggleRead() { if (mMessage != null) { mController.setFlag(mAccount, mMessage.getFolder().getName(), - new Message[] { mMessage }, Flag.SEEN, !mMessage.isSet(Flag.SEEN)); + Collections.singletonList(mMessage), Flag.SEEN, !mMessage.isSet(Flag.SEEN)); mMessageView.setHeaders(mMessage, mAccount); String subject = mMessage.getSubject(); displayMessageSubject(subject); diff --git a/src/com/fsck/k9/helper/Utility.java b/src/com/fsck/k9/helper/Utility.java index c450ad448..a03a774fb 100644 --- a/src/com/fsck/k9/helper/Utility.java +++ b/src/com/fsck/k9/helper/Utility.java @@ -105,6 +105,22 @@ public class Utility { return TextUtils.join(String.valueOf(separator), parts); } + /** + * Combines the given Objects into a single String using + * each Object's toString() method and the separator character + * between each part. + * + * @param parts + * @param separator + * @return new String + */ + public static String combine(Iterable parts, char separator) { + if (parts == null) { + return null; + } + return TextUtils.join(String.valueOf(separator), parts); + } + public static String base64Decode(String encoded) { if (encoded == null) { return null; diff --git a/src/com/fsck/k9/mail/Folder.java b/src/com/fsck/k9/mail/Folder.java index d4bebf7da..7d838ea43 100644 --- a/src/com/fsck/k9/mail/Folder.java +++ b/src/com/fsck/k9/mail/Folder.java @@ -1,8 +1,10 @@ package com.fsck.k9.mail; +import java.util.Collection; import java.util.Date; import java.util.List; import java.util.Map; +import java.util.Set; import android.util.Log; import com.fsck.k9.Account; @@ -92,7 +94,7 @@ public abstract class Folder { * @return List of messages * @throws MessagingException */ - public abstract Message[] getMessages(int start, int end, Date earliestDate, MessageRetrievalListener listener) throws MessagingException; + public abstract List getMessages(int start, int end, Date earliestDate, MessageRetrievalListener listener) throws MessagingException; /** * Fetches the given list of messages. The specified listener is notified as @@ -102,36 +104,36 @@ public abstract class Folder { * @param listener Listener to notify as we download messages. * @return List of messages */ - public abstract Message[] getMessages(MessageRetrievalListener listener) throws MessagingException; + public abstract List getMessages(MessageRetrievalListener listener) throws MessagingException; - public Message[] getMessages(MessageRetrievalListener listener, boolean includeDeleted) throws MessagingException { + public List getMessages(MessageRetrievalListener listener, boolean includeDeleted) throws MessagingException { return getMessages(listener); } - public abstract Message[] getMessages(String[] uids, MessageRetrievalListener listener) + public abstract List getMessages(String[] uids, MessageRetrievalListener listener) throws MessagingException; - public abstract Map appendMessages(Message[] messages) throws MessagingException; + public abstract Map appendMessages(List messages) throws MessagingException; - public Map copyMessages(Message[] msgs, Folder folder) throws MessagingException { + public Map copyMessages(List msgs, Folder folder) throws MessagingException { return null; } - public Map moveMessages(Message[] msgs, Folder folder) throws MessagingException { + public Map moveMessages(List msgs, Folder folder) throws MessagingException { return null; } - public void delete(Message[] msgs, String trashFolderName) throws MessagingException { + public void delete(List msgs, String trashFolderName) throws MessagingException { for (Message message : msgs) { Message myMessage = getMessage(message.getUid()); myMessage.delete(trashFolderName); } } - public abstract void setFlags(Message[] messages, Flag[] flags, boolean value) + public abstract void setFlags(List messages, Collection flags, boolean value) throws MessagingException; - public abstract void setFlags(Flag[] flags, boolean value) throws MessagingException; + public abstract void setFlags(Collection flags, boolean value) throws MessagingException; public abstract String getUidFromMessageId(Message message) throws MessagingException; @@ -146,7 +148,7 @@ public abstract class Folder { * @param listener Listener to notify as we fetch messages. * @throws MessagingException */ - public abstract void fetch(Message[] messages, FetchProfile fp, + public abstract void fetch(List messages, FetchProfile fp, MessageRetrievalListener listener) throws MessagingException; public void fetchPart(Message message, Part part, @@ -243,7 +245,7 @@ public abstract class Folder { return mAccount; } - public List search(String queryString, final Flag[] requiredFlags, final Flag[] forbiddenFlags) + public List search(String queryString, final Set requiredFlags, final Set forbiddenFlags) throws MessagingException { throw new MessagingException("K-9 does not support searches on this folder type"); } diff --git a/src/com/fsck/k9/mail/Message.java b/src/com/fsck/k9/mail/Message.java index 7da6ff1fc..ce1a59711 100644 --- a/src/com/fsck/k9/mail/Message.java +++ b/src/com/fsck/k9/mail/Message.java @@ -2,6 +2,8 @@ package com.fsck.k9.mail; import java.io.IOException; +import java.util.Collection; +import java.util.Collections; import java.util.Date; import java.util.HashSet; import java.util.Set; @@ -16,7 +18,6 @@ import com.fsck.k9.mail.store.UnavailableStorageException; public abstract class Message implements Part, CompositeBody { - private static final Flag[] EMPTY_FLAG_ARRAY = new Flag[0]; private MessageReference mReference = null; @@ -208,8 +209,8 @@ public abstract class Message implements Part, CompositeBody { /* * TODO Refactor Flags at some point to be able to store user defined flags. */ - public Flag[] getFlags() { - return mFlags.toArray(EMPTY_FLAG_ARRAY); + public Collection getFlags() { + return Collections.unmodifiableCollection(mFlags); } /** @@ -233,7 +234,7 @@ public abstract class Message implements Part, CompositeBody { * @param flags * @param set */ - public void setFlags(Flag[] flags, boolean set) throws MessagingException { + public void setFlags(final Collection flags, boolean set) throws MessagingException { for (Flag flag : flags) { setFlag(flag, set); } diff --git a/src/com/fsck/k9/mail/Store.java b/src/com/fsck/k9/mail/Store.java index 5be7d7011..7dbfade7e 100644 --- a/src/com/fsck/k9/mail/Store.java +++ b/src/com/fsck/k9/mail/Store.java @@ -244,7 +244,7 @@ public abstract class Store { return true; } - public void sendMessages(Message[] messages) throws MessagingException { + public void sendMessages(List messages) throws MessagingException { } public Pusher getPusher(PushReceiver receiver) { diff --git a/src/com/fsck/k9/mail/store/ImapStore.java b/src/com/fsck/k9/mail/store/ImapStore.java index 1987e0b5a..038f17084 100644 --- a/src/com/fsck/k9/mail/store/ImapStore.java +++ b/src/com/fsck/k9/mail/store/ImapStore.java @@ -26,6 +26,7 @@ import java.security.Security; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import java.util.Date; import java.util.Deque; @@ -132,8 +133,6 @@ public class ImapStore extends Store { private static final String CAPABILITY_COMPRESS_DEFLATE = "COMPRESS=DEFLATE"; private static final String COMMAND_COMPRESS_DEFLATE = "COMPRESS DEFLATE"; - private static final Message[] EMPTY_MESSAGE_ARRAY = new Message[0]; - private static final String[] EMPTY_STRING_ARRAY = new String[0]; /** @@ -1092,22 +1091,22 @@ public class ImapStore extends Store { * @return The mapping of original message UIDs to the new server UIDs. */ @Override - public Map copyMessages(Message[] messages, Folder folder) + public Map copyMessages(List messages, Folder folder) throws MessagingException { if (!(folder instanceof ImapFolder)) { throw new MessagingException("ImapFolder.copyMessages passed non-ImapFolder"); } - if (messages.length == 0) { + if (messages.size() == 0) { return null; } ImapFolder iFolder = (ImapFolder)folder; checkOpen(); //only need READ access - String[] uids = new String[messages.length]; - for (int i = 0, count = messages.length; i < count; i++) { - uids[i] = messages[i].getUid(); + String[] uids = new String[messages.size()]; + for (int i = 0, count = messages.size(); i < count; i++) { + uids[i] = messages.get(i).getUid(); } try { @@ -1193,21 +1192,21 @@ public class ImapStore extends Store { } @Override - public Map moveMessages(Message[] messages, Folder folder) throws MessagingException { - if (messages.length == 0) + public Map moveMessages(List messages, Folder folder) throws MessagingException { + if (messages.size() == 0) return null; Map uidMap = copyMessages(messages, folder); - setFlags(messages, new Flag[] { Flag.DELETED }, true); + setFlags(messages, Collections.singleton(Flag.DELETED), true); return uidMap; } @Override - public void delete(Message[] messages, String trashFolderName) throws MessagingException { - if (messages.length == 0) + public void delete(List messages, String trashFolderName) throws MessagingException { + if (messages.size() == 0) return; if (trashFolderName == null || getName().equalsIgnoreCase(trashFolderName)) { - setFlags(messages, new Flag[] { Flag.DELETED }, true); + setFlags(messages, Collections.singleton(Flag.DELETED), true); } else { ImapFolder remoteTrashFolder = (ImapFolder)getStore().getFolder(trashFolderName); String remoteTrashName = encodeString(encodeFolderName(remoteTrashFolder.getPrefixedName())); @@ -1223,7 +1222,7 @@ public class ImapStore extends Store { if (exists(remoteTrashName)) { if (K9.DEBUG) - Log.d(K9.LOG_TAG, "IMAPMessage.delete: copying remote " + messages.length + " messages to '" + trashFolderName + "' for " + getLogId()); + Log.d(K9.LOG_TAG, "IMAPMessage.delete: copying remote " + messages.size() + " messages to '" + trashFolderName + "' for " + getLogId()); moveMessages(messages, remoteTrashFolder); } else { @@ -1278,9 +1277,9 @@ public class ImapStore extends Store { return executeSimpleCommand("UID SEARCH *:*"); } }; - Message[] messages = search(searcher, null).toArray(EMPTY_MESSAGE_ARRAY); - if (messages.length > 0) { - return Long.parseLong(messages[0].getUid()); + List messages = search(searcher, null); + if (messages.size() > 0) { + return Long.parseLong(messages.get(0).getUid()); } } catch (Exception e) { Log.e(K9.LOG_TAG, "Unable to find highest UID in folder " + getName(), e); @@ -1301,12 +1300,12 @@ public class ImapStore extends Store { @Override - public Message[] getMessages(int start, int end, Date earliestDate, MessageRetrievalListener listener) + public List getMessages(int start, int end, Date earliestDate, MessageRetrievalListener listener) throws MessagingException { return getMessages(start, end, earliestDate, false, listener); } - protected Message[] getMessages(final int start, final int end, Date earliestDate, final boolean includeDeleted, final MessageRetrievalListener listener) + protected List getMessages(final int start, final int end, Date earliestDate, final boolean includeDeleted, final MessageRetrievalListener listener) throws MessagingException { if (start < 1 || end < 1 || end < start) { throw new MessagingException( @@ -1328,10 +1327,10 @@ public class ImapStore extends Store { return executeSimpleCommand(String.format(Locale.US, "UID SEARCH %d:%d%s%s", start, end, dateSearchString, includeDeleted ? "" : " NOT DELETED")); } }; - return search(searcher, listener).toArray(EMPTY_MESSAGE_ARRAY); + return search(searcher, listener); } - protected Message[] getMessages(final List mesgSeqs, final boolean includeDeleted, final MessageRetrievalListener listener) + protected List getMessages(final List mesgSeqs, final boolean includeDeleted, final MessageRetrievalListener listener) throws MessagingException { ImapSearcher searcher = new ImapSearcher() { @Override @@ -1339,10 +1338,10 @@ public class ImapStore extends Store { return executeSimpleCommand(String.format("UID SEARCH %s%s", Utility.combine(mesgSeqs.toArray(), ','), includeDeleted ? "" : " NOT DELETED")); } }; - return search(searcher, listener).toArray(EMPTY_MESSAGE_ARRAY); + return search(searcher, listener); } - protected Message[] getMessagesFromUids(final List mesgUids, final boolean includeDeleted, final MessageRetrievalListener listener) + protected List getMessagesFromUids(final List mesgUids, final boolean includeDeleted, final MessageRetrievalListener listener) throws MessagingException { ImapSearcher searcher = new ImapSearcher() { @Override @@ -1350,7 +1349,7 @@ public class ImapStore extends Store { return executeSimpleCommand(String.format("UID SEARCH UID %s%s", Utility.combine(mesgUids.toArray(), ','), includeDeleted ? "" : " NOT DELETED")); } }; - return search(searcher, listener).toArray(EMPTY_MESSAGE_ARRAY); + return search(searcher, listener); } private List search(ImapSearcher searcher, MessageRetrievalListener listener) throws MessagingException { @@ -1395,12 +1394,12 @@ public class ImapStore extends Store { @Override - public Message[] getMessages(MessageRetrievalListener listener) throws MessagingException { + public List getMessages(MessageRetrievalListener listener) throws MessagingException { return getMessages(null, listener); } @Override - public Message[] getMessages(String[] uids, MessageRetrievalListener listener) + public List getMessages(String[] uids, MessageRetrievalListener listener) throws MessagingException { checkOpen(); //only need READ access List messages = new ArrayList(); @@ -1430,18 +1429,18 @@ public class ImapStore extends Store { } catch (IOException ioe) { throw ioExceptionHandler(mConnection, ioe); } - return messages.toArray(EMPTY_MESSAGE_ARRAY); + return messages; } @Override - public void fetch(Message[] messages, FetchProfile fp, MessageRetrievalListener listener) + public void fetch(List messages, FetchProfile fp, MessageRetrievalListener listener) throws MessagingException { - if (messages == null || messages.length == 0) { + if (messages == null || messages.size() == 0) { return; } checkOpen(); //only need READ access - List uids = new ArrayList(messages.length); - Map messageMap = new HashMap(); + List uids = new ArrayList(messages.size()); + HashMap messageMap = new HashMap(); for (Message msg : messages) { String uid = msg.getUid(); uids.add(uid); @@ -1482,8 +1481,8 @@ public class ImapStore extends Store { - for (int windowStart = 0; windowStart < messages.length; windowStart += (FETCH_WINDOW_SIZE)) { - List uidWindow = uids.subList(windowStart, Math.min((windowStart + FETCH_WINDOW_SIZE), messages.length)); + for (int windowStart = 0; windowStart < messages.size(); windowStart += (FETCH_WINDOW_SIZE)) { + List uidWindow = uids.subList(windowStart, Math.min((windowStart + FETCH_WINDOW_SIZE), messages.size())); try { mConnection.sendCommand(String.format("UID FETCH %s (%s)", @@ -1965,7 +1964,7 @@ public class ImapStore extends Store { * @return The mapping of original message UIDs to the new server UIDs. */ @Override - public Map appendMessages(Message[] messages) throws MessagingException { + public Map appendMessages(List messages) throws MessagingException { open(OPEN_MODE_RW); checkOpen(); try { @@ -2088,7 +2087,7 @@ public class ImapStore extends Store { } } - private String combineFlags(Flag[] flags) { + private String combineFlags(Iterable flags) { List flagNames = new ArrayList(); for (Flag flag : flags) { if (flag == Flag.SEEN) { @@ -2110,7 +2109,7 @@ public class ImapStore extends Store { @Override - public void setFlags(Flag[] flags, boolean value) + public void setFlags(Collection flags, boolean value) throws MessagingException { open(OPEN_MODE_RW); checkOpen(); @@ -2145,13 +2144,13 @@ public class ImapStore extends Store { @Override - public void setFlags(Message[] messages, Flag[] flags, boolean value) + public void setFlags(List messages, final Collection flags, boolean value) throws MessagingException { open(OPEN_MODE_RW); checkOpen(); - String[] uids = new String[messages.length]; - for (int i = 0, count = messages.length; i < count; i++) { - uids[i] = messages[i].getUid(); + String[] uids = new String[messages.size()]; + for (int i = 0, count = messages.size(); i < count; i++) { + uids[i] = messages.get(i).getUid(); } try { executeSimpleCommand(String.format("UID STORE %s %sFLAGS.SILENT (%s)", @@ -2212,7 +2211,7 @@ public class ImapStore extends Store { * @throws MessagingException On any error. */ @Override - public List search(final String queryString, final Flag[] requiredFlags, final Flag[] forbiddenFlags) + public List search(final String queryString, final Set requiredFlags, final Set forbiddenFlags) throws MessagingException { if (!mAccount.allowRemoteSearch()) { @@ -2911,12 +2910,12 @@ public class ImapStore extends Store { @Override public void setFlag(Flag flag, boolean set) throws MessagingException { super.setFlag(flag, set); - mFolder.setFlags(new Message[] { this }, new Flag[] { flag }, set); + mFolder.setFlags(Collections.singletonList(this), Collections.singletonList(flag), set); } @Override public void delete(String trashFolderName) throws MessagingException { - getFolder().delete(new Message[] { this }, trashFolderName); + getFolder().delete(Collections.singletonList(this), trashFolderName); } } @@ -3209,9 +3208,9 @@ public class ImapStore extends Store { Log.e(K9.LOG_TAG, "Unable to get oldUidNext for " + getLogId(), e); } - Message[] messageArray = getMessages(end, end, null, true, null); - if (messageArray != null && messageArray.length > 0) { - long newUid = Long.parseLong(messageArray[0].getUid()); + List messageArray = getMessages(end, end, null, true, null); + if (messageArray != null && messageArray.size() > 0) { + long newUid = Long.parseLong(messageArray.get(0).getUid()); if (K9.DEBUG) Log.i(K9.LOG_TAG, "Got newUid " + newUid + " for message " + end + " on " + getLogId()); long startUid = oldUidNext; @@ -3239,12 +3238,10 @@ public class ImapStore extends Store { private void syncMessages(List flagSyncMsgSeqs) { try { - Message[] messageArray = null; - - messageArray = getMessages(flagSyncMsgSeqs, true, null); + List messageArray = getMessages(flagSyncMsgSeqs, true, null); List messages = new ArrayList(); - messages.addAll(Arrays.asList(messageArray)); + messages.addAll(messageArray); pushMessages(messages, false); } catch (Exception e) { @@ -3256,7 +3253,7 @@ public class ImapStore extends Store { List messages = new ArrayList(removeUids.size()); try { - Message[] existingMessages = getMessagesFromUids(removeUids, true, null); + List existingMessages = getMessagesFromUids(removeUids, true, null); for (Message existingMessage : existingMessages) { needsPoll.set(true); msgSeqUidMap.clear(); diff --git a/src/com/fsck/k9/mail/store/Pop3Store.java b/src/com/fsck/k9/mail/store/Pop3Store.java index 414b334b2..031023563 100644 --- a/src/com/fsck/k9/mail/store/Pop3Store.java +++ b/src/com/fsck/k9/mail/store/Pop3Store.java @@ -24,6 +24,8 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; import java.util.Date; import java.util.LinkedList; import java.util.HashMap; @@ -566,7 +568,7 @@ public class Pop3Store extends Store { } @Override - public Message[] getMessages(int start, int end, Date earliestDate, MessageRetrievalListener listener) + public List getMessages(int start, int end, Date earliestDate, MessageRetrievalListener listener) throws MessagingException { if (start < 1 || end < 1 || end < start) { throw new MessagingException(String.format(Locale.US, "Invalid message set %d %d", @@ -599,7 +601,7 @@ public class Pop3Store extends Store { listener.messageFinished(message, i++, (end - start) + 1); } } - return messages.toArray(new Message[messages.size()]); + return messages; } /** @@ -741,12 +743,12 @@ public class Pop3Store extends Store { } @Override - public Message[] getMessages(MessageRetrievalListener listener) throws MessagingException { + public List getMessages(MessageRetrievalListener listener) throws MessagingException { throw new UnsupportedOperationException("Pop3: No getMessages"); } @Override - public Message[] getMessages(String[] uids, MessageRetrievalListener listener) + public List getMessages(String[] uids, MessageRetrievalListener listener) throws MessagingException { throw new UnsupportedOperationException("Pop3: No getMessages by uids"); } @@ -759,9 +761,9 @@ public class Pop3Store extends Store { * @throws MessagingException */ @Override - public void fetch(Message[] messages, FetchProfile fp, MessageRetrievalListener listener) + public void fetch(List messages, FetchProfile fp, MessageRetrievalListener listener) throws MessagingException { - if (messages == null || messages.length == 0) { + if (messages == null || messages.size() == 0) { return; } List uids = new ArrayList(); @@ -786,8 +788,8 @@ public class Pop3Store extends Store { } catch (IOException ioe) { throw new MessagingException("fetch", ioe); } - for (int i = 0, count = messages.length; i < count; i++) { - Message message = messages[i]; + for (int i = 0, count = messages.size(); i < count; i++) { + Message message = messages.get(i); if (!(message instanceof Pop3Message)) { throw new MessagingException("Pop3Store.fetch called with non-Pop3 Message"); } @@ -825,7 +827,7 @@ public class Pop3Store extends Store { } } - private void fetchEnvelope(Message[] messages, + private void fetchEnvelope(List messages, MessageRetrievalListener listener) throws IOException, MessagingException { int unsizedMessages = 0; for (Message message : messages) { @@ -841,8 +843,8 @@ public class Pop3Store extends Store { * In extreme cases we'll do a command per message instead of a bulk request * to hopefully save some time and bandwidth. */ - for (int i = 0, count = messages.length; i < count; i++) { - Message message = messages[i]; + for (int i = 0, count = messages.size(); i < count; i++) { + Message message = messages.get(i); if (!(message instanceof Pop3Message)) { throw new MessagingException("Pop3Store.fetch called with non-Pop3 Message"); } @@ -865,7 +867,7 @@ public class Pop3Store extends Store { for (Message message : messages) { msgUidIndex.add(message.getUid()); } - int i = 0, count = messages.length; + int i = 0, count = messages.size(); String response = executeSimpleCommand(LIST_COMMAND); while ((response = readLine()) != null) { if (response.equals(".")) { @@ -956,7 +958,7 @@ public class Pop3Store extends Store { } @Override - public Map appendMessages(Message[] messages) throws MessagingException { + public Map appendMessages(List messages) throws MessagingException { return null; } @@ -965,8 +967,8 @@ public class Pop3Store extends Store { } @Override - public void delete(Message[] msgs, String trashFolderName) throws MessagingException { - setFlags(msgs, new Flag[] { Flag.DELETED }, true); + public void delete(List msgs, String trashFolderName) throws MessagingException { + setFlags(msgs, Collections.singleton(Flag.DELETED), true); } @Override @@ -975,14 +977,14 @@ public class Pop3Store extends Store { } @Override - public void setFlags(Flag[] flags, boolean value) throws MessagingException { - throw new UnsupportedOperationException("POP3: No setFlags(Flag[],boolean)"); + public void setFlags(final Collection flags, boolean value) throws MessagingException { + throw new UnsupportedOperationException("POP3: No setFlags(Collection,boolean)"); } @Override - public void setFlags(Message[] messages, Flag[] flags, boolean value) + public void setFlags(List messages, final Collection flags, boolean value) throws MessagingException { - if (!value || !Utility.arrayContains(flags, Flag.DELETED)) { + if (!value || !flags.contains(Flag.DELETED)) { /* * The only flagging we support is setting the Deleted flag. */ @@ -1185,7 +1187,7 @@ public class Pop3Store extends Store { @Override public void setFlag(Flag flag, boolean set) throws MessagingException { super.setFlag(flag, set); - mFolder.setFlags(new Message[] { this }, new Flag[] { flag }, set); + mFolder.setFlags(Collections.singletonList(this), Collections.singletonList(flag), set); } @Override diff --git a/src/com/fsck/k9/mail/store/WebDavStore.java b/src/com/fsck/k9/mail/store/WebDavStore.java index 650be8812..85351a1f9 100644 --- a/src/com/fsck/k9/mail/store/WebDavStore.java +++ b/src/com/fsck/k9/mail/store/WebDavStore.java @@ -65,8 +65,6 @@ public class WebDavStore extends Store { private static final String[] EMPTY_STRING_ARRAY = new String[0]; - private static final Message[] EMPTY_MESSAGE_ARRAY = new Message[0]; - // These are the ids used from Exchange server to identify the special folders // http://social.technet.microsoft.com/Forums/en/exchangesvrdevelopment/thread/1cd2e98c-8a12-44bd-a3e3-9c5ee9e4e14d private static final String DAV_MAIL_INBOX_FOLDER = "inbox"; @@ -1197,11 +1195,11 @@ public class WebDavStore extends Store { } @Override - public void sendMessages(Message[] messages) throws MessagingException { + public void sendMessages(List messages) throws MessagingException { WebDavFolder tmpFolder = (WebDavStore.WebDavFolder) getFolder(mAccount.getDraftsFolderName()); try { tmpFolder.open(Folder.OPEN_MODE_RW); - Message[] retMessages = tmpFolder.appendWebDavMessages(messages); + List retMessages = tmpFolder.appendWebDavMessages(messages); tmpFolder.moveMessages(retMessages, getSendSpoolFolder()); } finally { @@ -1270,28 +1268,28 @@ public class WebDavStore extends Store { } @Override - public Map copyMessages(Message[] messages, Folder folder) throws MessagingException { + public Map copyMessages(List messages, Folder folder) throws MessagingException { moveOrCopyMessages(messages, folder.getName(), false); return null; } @Override - public Map moveMessages(Message[] messages, Folder folder) throws MessagingException { + public Map moveMessages(List messages, Folder folder) throws MessagingException { moveOrCopyMessages(messages, folder.getName(), true); return null; } @Override - public void delete(Message[] msgs, String trashFolderName) throws MessagingException { + public void delete(List msgs, String trashFolderName) throws MessagingException { moveOrCopyMessages(msgs, trashFolderName, true); } - private void moveOrCopyMessages(Message[] messages, String folderName, boolean isMove) + private void moveOrCopyMessages(List messages, String folderName, boolean isMove) throws MessagingException { - String[] uids = new String[messages.length]; + String[] uids = new String[messages.size()]; - for (int i = 0, count = messages.length; i < count; i++) { - uids[i] = messages[i].getUid(); + for (int i = 0, count = messages.size(); i < count; i++) { + uids[i] = messages.get(i).getUid(); } String messageBody = ""; Map headers = new HashMap(); @@ -1300,8 +1298,8 @@ public class WebDavStore extends Store { for (int i = 0, count = uids.length; i < count; i++) { urls[i] = uidToUrl.get(uids[i]); - if (urls[i] == null && messages[i] instanceof WebDavMessage) { - WebDavMessage wdMessage = (WebDavMessage) messages[i]; + if (urls[i] == null && messages.get(i) instanceof WebDavMessage) { + WebDavMessage wdMessage = (WebDavMessage) messages.get(i); urls[i] = wdMessage.getUrl(); } } @@ -1312,7 +1310,7 @@ public class WebDavStore extends Store { headers.put("Brief", "t"); headers.put("If-Match", "*"); String action = (isMove ? "BMOVE" : "BCOPY"); - Log.i(K9.LOG_TAG, "Moving " + messages.length + " messages to " + destFolder.mFolderUrl); + Log.i(K9.LOG_TAG, "Moving " + messages.size() + " messages to " + destFolder.mFolderUrl); processRequest(mFolderUrl, action, messageBody, headers, false); } @@ -1404,7 +1402,7 @@ public class WebDavStore extends Store { } @Override - public Message[] getMessages(int start, int end, Date earliestDate, MessageRetrievalListener listener) + public List getMessages(int start, int end, Date earliestDate, MessageRetrievalListener listener) throws MessagingException { List messages = new ArrayList(); String[] uids; @@ -1450,22 +1448,22 @@ public class WebDavStore extends Store { } } - return messages.toArray(EMPTY_MESSAGE_ARRAY); + return messages; } @Override - public Message[] getMessages(MessageRetrievalListener listener) throws MessagingException { + public List getMessages(MessageRetrievalListener listener) throws MessagingException { return getMessages(null, listener); } @Override - public Message[] getMessages(String[] uids, MessageRetrievalListener listener) throws MessagingException { + public List getMessages(String[] uids, MessageRetrievalListener listener) throws MessagingException { List messageList = new ArrayList(); - Message[] messages; + List messages; if (uids == null || uids.length == 0) { - return messageList.toArray(EMPTY_MESSAGE_ARRAY); + return messageList; } for (int i = 0, count = uids.length; i < count; i++) { @@ -1480,7 +1478,7 @@ public class WebDavStore extends Store { listener.messageFinished(message, i, count); } } - messages = messageList.toArray(EMPTY_MESSAGE_ARRAY); + messages = messageList; return messages; } @@ -1500,10 +1498,10 @@ public class WebDavStore extends Store { } @Override - public void fetch(Message[] messages, FetchProfile fp, MessageRetrievalListener listener) + public void fetch(List messages, FetchProfile fp, MessageRetrievalListener listener) throws MessagingException { if (messages == null || - messages.length == 0) { + messages.size() == 0) { return; } @@ -1535,7 +1533,7 @@ public class WebDavStore extends Store { /** * Fetches the full messages or up to lines lines and passes them to the message parser. */ - private void fetchMessages(Message[] messages, MessageRetrievalListener listener, int lines) + private void fetchMessages(List messages, MessageRetrievalListener listener, int lines) throws MessagingException { WebDavHttpClient httpclient; httpclient = getHttpClient(); @@ -1543,15 +1541,15 @@ public class WebDavStore extends Store { /** * We can't hand off to processRequest() since we need the stream to parse. */ - for (int i = 0, count = messages.length; i < count; i++) { + for (int i = 0, count = messages.size(); i < count; i++) { WebDavMessage wdMessage; int statusCode = 0; - if (!(messages[i] instanceof WebDavMessage)) { + if (!(messages.get(i) instanceof WebDavMessage)) { throw new MessagingException("WebDavStore fetch called with non-WebDavMessage"); } - wdMessage = (WebDavMessage) messages[i]; + wdMessage = (WebDavMessage) messages.get(i); if (listener != null) { listener.messageStarted(wdMessage.getUid(), i, count); @@ -1649,36 +1647,36 @@ public class WebDavStore extends Store { * Fetches and sets the message flags for the supplied messages. The idea is to have this be recursive so that * we do a series of medium calls instead of one large massive call or a large number of smaller calls. */ - private void fetchFlags(Message[] startMessages, MessageRetrievalListener listener) throws MessagingException { - Map headers = new HashMap(); + private void fetchFlags(List startMessages, MessageRetrievalListener listener) throws MessagingException { + HashMap headers = new HashMap(); String messageBody = ""; - Message[] messages = new Message[20]; + List messages = new ArrayList(20); String[] uids; if (startMessages == null || - startMessages.length == 0) { + startMessages.size() == 0) { return; } - if (startMessages.length > 20) { - Message[] newMessages = new Message[startMessages.length - 20]; - for (int i = 0, count = startMessages.length; i < count; i++) { + if (startMessages.size() > 20) { + List newMessages = new ArrayList(startMessages.size() - 20); + for (int i = 0, count = startMessages.size(); i < count; i++) { if (i < 20) { - messages[i] = startMessages[i]; + messages.set(i, startMessages.get(i)); } else { - newMessages[i - 20] = startMessages[i]; + newMessages.set(i - 20, startMessages.get(i)); } } fetchFlags(newMessages, listener); } else { - messages = startMessages; + messages.addAll(startMessages); } - uids = new String[messages.length]; + uids = new String[messages.size()]; - for (int i = 0, count = messages.length; i < count; i++) { - uids[i] = messages[i].getUid(); + for (int i = 0, count = messages.size(); i < count; i++) { + uids[i] = messages.get(i).getUid(); } messageBody = getMessageFlagsXml(uids); @@ -1691,11 +1689,11 @@ public class WebDavStore extends Store { Map uidToReadStatus = dataset.getUidToRead(); - for (int i = 0, count = messages.length; i < count; i++) { - if (!(messages[i] instanceof WebDavMessage)) { + for (int i = 0, count = messages.size(); i < count; i++) { + if (!(messages.get(i) instanceof WebDavMessage)) { throw new MessagingException("WebDavStore fetch called with non-WebDavMessage"); } - WebDavMessage wdMessage = (WebDavMessage) messages[i]; + WebDavMessage wdMessage = (WebDavMessage) messages.get(i); if (listener != null) { listener.messageStarted(wdMessage.getUid(), i, count); @@ -1718,37 +1716,37 @@ public class WebDavStore extends Store { * that we do a series of medium calls instead of one large massive call or a large number of smaller calls. * Call it a happy balance */ - private void fetchEnvelope(Message[] startMessages, MessageRetrievalListener listener) + private void fetchEnvelope(List startMessages, MessageRetrievalListener listener) throws MessagingException { Map headers = new HashMap(); String messageBody = ""; String[] uids; - Message[] messages = new Message[10]; + List messages = new ArrayList(10); if (startMessages == null || - startMessages.length == 0) { + startMessages.size() == 0) { return; } - if (startMessages.length > 10) { - Message[] newMessages = new Message[startMessages.length - 10]; - for (int i = 0, count = startMessages.length; i < count; i++) { + if (startMessages.size() > 10) { + List newMessages = new ArrayList(startMessages.size() - 10); + for (int i = 0, count = startMessages.size(); i < count; i++) { if (i < 10) { - messages[i] = startMessages[i]; + messages.set(i, startMessages.get(i)); } else { - newMessages[i - 10] = startMessages[i]; + newMessages.set(i - 10,startMessages.get(i)); } } fetchEnvelope(newMessages, listener); } else { - messages = startMessages; + messages.addAll(startMessages); } - uids = new String[messages.length]; + uids = new String[messages.size()]; - for (int i = 0, count = messages.length; i < count; i++) { - uids[i] = messages[i].getUid(); + for (int i = 0, count = messages.size(); i < count; i++) { + uids[i] = messages.get(i).getUid(); } messageBody = getMessageEnvelopeXml(uids); @@ -1757,15 +1755,15 @@ public class WebDavStore extends Store { Map envelopes = dataset.getMessageEnvelopes(); - int count = messages.length; - for (int i = messages.length - 1; i >= 0; i--) { - if (!(messages[i] instanceof WebDavMessage)) { + int count = messages.size(); + for (int i = messages.size() - 1; i >= 0; i--) { + if (!(messages.get(i) instanceof WebDavMessage)) { throw new MessagingException("WebDavStore fetch called with non-WebDavMessage"); } - WebDavMessage wdMessage = (WebDavMessage) messages[i]; + WebDavMessage wdMessage = (WebDavMessage) messages.get(i); if (listener != null) { - listener.messageStarted(messages[i].getUid(), i, count); + listener.messageStarted(messages.get(i).getUid(), i, count); } ParsedMessageEnvelope envelope = envelopes.get(wdMessage.getUid()); @@ -1777,18 +1775,18 @@ public class WebDavStore extends Store { } if (listener != null) { - listener.messageFinished(messages[i], i, count); + listener.messageFinished(messages.get(i), i, count); } } } @Override - public void setFlags(Message[] messages, Flag[] flags, boolean value) + public void setFlags(List messages, final Collection flags, boolean value) throws MessagingException { - String[] uids = new String[messages.length]; + String[] uids = new String[messages.size()]; - for (int i = 0, count = messages.length; i < count; i++) { - uids[i] = messages[i].getUid(); + for (int i = 0, count = messages.size(); i < count; i++) { + uids[i] = messages.get(i).getUid(); } for (Flag flag : flags) { @@ -1848,13 +1846,13 @@ public class WebDavStore extends Store { } @Override - public Map appendMessages(Message[] messages) throws MessagingException { + public Map appendMessages(List messages) throws MessagingException { appendWebDavMessages(messages); return null; } - public Message[] appendWebDavMessages(Message[] messages) throws MessagingException { - Message[] retMessages = new Message[messages.length]; + public List appendWebDavMessages(List messages) throws MessagingException { + List retMessages = new ArrayList(messages.size()); int ind = 0; WebDavHttpClient httpclient = getHttpClient(); @@ -1909,7 +1907,7 @@ public class WebDavStore extends Store { WebDavMessage retMessage = new WebDavMessage(message.getUid(), this); retMessage.setUrl(messageURL); - retMessages[ind++] = retMessage; + retMessages.set(ind++, retMessage); } catch (Exception e) { throw new MessagingException("Unable to append", e); } @@ -1931,9 +1929,9 @@ public class WebDavStore extends Store { } @Override - public void setFlags(Flag[] flags, boolean value) throws MessagingException { + public void setFlags(final Collection flags, boolean value) throws MessagingException { Log.e(K9.LOG_TAG, - "Unimplemented method setFlags(Flag[], boolean) breaks markAllMessagesAsRead and EmptyTrash"); + "Unimplemented method setFlags(Collection, boolean) breaks markAllMessagesAsRead and EmptyTrash"); // Try to make this efficient by not retrieving all of the messages } } @@ -2025,13 +2023,13 @@ public class WebDavStore extends Store { public void delete(String trashFolderName) throws MessagingException { WebDavFolder wdFolder = (WebDavFolder) getFolder(); Log.i(K9.LOG_TAG, "Deleting message by moving to " + trashFolderName); - wdFolder.moveMessages(new Message[] { this }, wdFolder.getStore().getFolder(trashFolderName)); + wdFolder.moveMessages(Collections.singletonList(this), wdFolder.getStore().getFolder(trashFolderName)); } @Override public void setFlag(Flag flag, boolean set) throws MessagingException { super.setFlag(flag, set); - mFolder.setFlags(new Message[] { this }, new Flag[] { flag }, set); + mFolder.setFlags(Collections.singletonList(this), Collections.singletonList(flag), set); } } diff --git a/src/com/fsck/k9/mail/store/local/LocalFolder.java b/src/com/fsck/k9/mail/store/local/LocalFolder.java index 627c4977c..0f7de26ba 100644 --- a/src/com/fsck/k9/mail/store/local/LocalFolder.java +++ b/src/com/fsck/k9/mail/store/local/LocalFolder.java @@ -7,6 +7,8 @@ import java.io.InputStream; import java.io.Serializable; import java.util.ArrayList; import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; import java.util.Date; import java.util.HashMap; import java.util.HashSet; @@ -370,12 +372,12 @@ public class LocalFolder extends Folder implements Serializable { return ; } open(OPEN_MODE_RW); - Message[] messages = getMessages(null, false); - for (int i = mVisibleLimit; i < messages.length; i++) { + List messages = getMessages(null, false); + for (int i = mVisibleLimit; i < messages.size(); i++) { if (listener != null) { - listener.messageRemoved(messages[i]); + listener.messageRemoved(messages.get(i)); } - messages[i].destroy(); + messages.get(i).destroy(); } } } @@ -597,7 +599,7 @@ public class LocalFolder extends Folder implements Serializable { } @Override - public void fetch(final Message[] messages, final FetchProfile fp, final MessageRetrievalListener listener) + public void fetch(final List messages, final FetchProfile fp, final MessageRetrievalListener listener) throws MessagingException { try { this.localStore.database.execute(false, new DbCallback() { @@ -798,7 +800,7 @@ public class LocalFolder extends Folder implements Serializable { } @Override - public Message[] getMessages(int start, int end, Date earliestDate, MessageRetrievalListener listener) + public List getMessages(int start, int end, Date earliestDate, MessageRetrievalListener listener) throws MessagingException { open(OPEN_MODE_RW); throw new MessagingException( @@ -813,7 +815,7 @@ public class LocalFolder extends Folder implements Serializable { * The messages whose headers should be loaded. * @throws UnavailableStorageException */ - void populateHeaders(final List messages) throws UnavailableStorageException { + void populateHeaders(final List messages) throws UnavailableStorageException { this.localStore.database.execute(false, new DbCallback() { @Override public Void doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { @@ -831,7 +833,7 @@ public class LocalFolder extends Folder implements Serializable { questions.append(", "); } questions.append("?"); - LocalMessage message = messages.get(i); + LocalMessage message = (LocalMessage)messages.get(i); Long id = message.getId(); ids.add(Long.toString(id)); popMessages.put(id, message); @@ -931,16 +933,16 @@ public class LocalFolder extends Folder implements Serializable { } @Override - public Message[] getMessages(MessageRetrievalListener listener) throws MessagingException { + public List getMessages(MessageRetrievalListener listener) throws MessagingException { return getMessages(listener, true); } @Override - public Message[] getMessages(final MessageRetrievalListener listener, final boolean includeDeleted) throws MessagingException { + public List getMessages(final MessageRetrievalListener listener, final boolean includeDeleted) throws MessagingException { try { - return this.localStore.database.execute(false, new DbCallback() { + return this.localStore.database.execute(false, new DbCallback>() { @Override - public Message[] doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { + public List doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { try { open(OPEN_MODE_RW); return LocalFolder.this.localStore.getMessages( @@ -965,7 +967,7 @@ public class LocalFolder extends Folder implements Serializable { } @Override - public Message[] getMessages(String[] uids, MessageRetrievalListener listener) + public List getMessages(String[] uids, MessageRetrievalListener listener) throws MessagingException { open(OPEN_MODE_RW); if (uids == null) { @@ -978,11 +980,11 @@ public class LocalFolder extends Folder implements Serializable { messages.add(message); } } - return messages.toArray(LocalStore.EMPTY_MESSAGE_ARRAY); + return messages; } @Override - public Map copyMessages(Message[] msgs, Folder folder) throws MessagingException { + public Map copyMessages(List msgs, Folder folder) throws MessagingException { if (!(folder instanceof LocalFolder)) { throw new MessagingException("copyMessages called with incorrect Folder"); } @@ -990,7 +992,7 @@ public class LocalFolder extends Folder implements Serializable { } @Override - public Map moveMessages(final Message[] msgs, final Folder destFolder) throws MessagingException { + public Map moveMessages(final List msgs, final Folder destFolder) throws MessagingException { if (!(destFolder instanceof LocalFolder)) { throw new MessagingException("moveMessages called with non-LocalFolder"); } @@ -1128,7 +1130,7 @@ public class LocalFolder extends Folder implements Serializable { @Override public Message doDbWork(final SQLiteDatabase db) throws WrappedException, UnavailableStorageException { try { - appendMessages(new Message[] { message }); + appendMessages(Collections.singletonList(message)); final String uid = message.getUid(); final Message result = getMessage(uid); runnable.run(); @@ -1154,11 +1156,11 @@ public class LocalFolder extends Folder implements Serializable { * message, retrieve the appropriate local message instance first (if it already exists). */ @Override - public Map appendMessages(Message[] messages) throws MessagingException { + public Map appendMessages(List messages) throws MessagingException { return appendMessages(messages, false); } - public void destroyMessages(final Message[] messages) { + public void destroyMessages(final List messages) { try { this.localStore.database.execute(true, new DbCallback() { @Override @@ -1221,7 +1223,7 @@ public class LocalFolder extends Folder implements Serializable { * @param copy * @return Map uidMap of srcUids -> destUids */ - private Map appendMessages(final Message[] messages, final boolean copy) throws MessagingException { + private Map appendMessages(final List messages, final boolean copy) throws MessagingException { open(OPEN_MODE_RW); try { final Map uidMap = new HashMap(); @@ -1490,12 +1492,12 @@ public class LocalFolder extends Folder implements Serializable { // Remember that all headers for this message have been saved, so it is // not necessary to download them again in case the user wants to see all headers. List appendedFlags = new ArrayList(); - appendedFlags.addAll(Arrays.asList(message.getFlags())); + appendedFlags.addAll(message.getFlags()); appendedFlags.add(Flag.X_GOT_ALL_HEADERS); db.execSQL("UPDATE messages " + "SET flags = ? " + " WHERE id = ?", new Object[] - { LocalFolder.this.localStore.serializeFlags(appendedFlags.toArray(LocalStore.EMPTY_FLAG_ARRAY)), id }); + { LocalFolder.this.localStore.serializeFlags(appendedFlags), id }); return null; } @@ -1722,7 +1724,7 @@ public class LocalFolder extends Folder implements Serializable { } @Override - public void setFlags(final Message[] messages, final Flag[] flags, final boolean value) + public void setFlags(final List messages, final Collection flags, final boolean value) throws MessagingException { open(OPEN_MODE_RW); @@ -1750,7 +1752,7 @@ public class LocalFolder extends Folder implements Serializable { } @Override - public void setFlags(Flag[] flags, boolean value) + public void setFlags(final Collection flags, boolean value) throws MessagingException { open(OPEN_MODE_RW); for (Message message : getMessages(null)) { @@ -1766,7 +1768,7 @@ public class LocalFolder extends Folder implements Serializable { public void clearMessagesOlderThan(long cutoff) throws MessagingException { open(OPEN_MODE_RO); - Message[] messages = this.localStore.getMessages( + List messages = this.localStore.getMessages( null, this, "SELECT " + LocalStore.GET_MESSAGES_COLS + @@ -1841,7 +1843,7 @@ public class LocalFolder extends Folder implements Serializable { try { // We need to open the folder first to make sure we've got it's id open(OPEN_MODE_RO); - Message[] messages = getMessages(null); + List messages = getMessages(null); for (Message message : messages) { deleteAttachments(message.getUid()); } diff --git a/src/com/fsck/k9/mail/store/local/LocalStore.java b/src/com/fsck/k9/mail/store/local/LocalStore.java index 92525d708..e36f453b6 100644 --- a/src/com/fsck/k9/mail/store/local/LocalStore.java +++ b/src/com/fsck/k9/mail/store/local/LocalStore.java @@ -4,6 +4,7 @@ package com.fsck.k9.mail.store.local; import java.io.File; import java.io.Serializable; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; import java.util.LinkedList; import java.util.List; @@ -55,9 +56,7 @@ public class LocalStore extends Store implements Serializable { private static final long serialVersionUID = -5142141896809423072L; - static final Message[] EMPTY_MESSAGE_ARRAY = new Message[0]; static final String[] EMPTY_STRING_ARRAY = new String[0]; - static final Flag[] EMPTY_FLAG_ARRAY = new Flag[0]; static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; /* @@ -523,7 +522,7 @@ public class LocalStore extends Store implements Serializable { return true; } - public Message[] searchForMessages(MessageRetrievalListener retrievalListener, + public List searchForMessages(MessageRetrievalListener retrievalListener, LocalSearch search) throws MessagingException { StringBuilder query = new StringBuilder(); @@ -554,7 +553,7 @@ public class LocalStore extends Store implements Serializable { * Given a query string, actually do the query for the messages and * call the MessageRetrievalListener for each one */ - Message[] getMessages( + List getMessages( final MessageRetrievalListener listener, final LocalFolder folder, final String queryString, final String[] placeHolders @@ -603,11 +602,11 @@ public class LocalStore extends Store implements Serializable { listener.messagesFinished(j); } - return messages.toArray(EMPTY_MESSAGE_ARRAY); + return Collections.unmodifiableList(messages); } - public Message[] getMessagesInThread(final long rootId) throws MessagingException { + public List getMessagesInThread(final long rootId) throws MessagingException { String rootIdString = Long.toString(rootId); LocalSearch search = new LocalSearch(); @@ -706,7 +705,7 @@ public class LocalStore extends Store implements Serializable { } - String serializeFlags(Flag[] flags) { + String serializeFlags(Iterable flags) { List extraFlags = new ArrayList(); for (Flag flag : flags) { @@ -724,7 +723,7 @@ public class LocalStore extends Store implements Serializable { } } - return Utility.combine(extraFlags.toArray(EMPTY_FLAG_ARRAY), ',').toUpperCase(Locale.US); + return Utility.combine(extraFlags, ',').toUpperCase(Locale.US); } public LockableDatabase getDatabase() { diff --git a/src/com/fsck/k9/mail/store/local/StoreSchemaDefinition.java b/src/com/fsck/k9/mail/store/local/StoreSchemaDefinition.java index 277ee719e..494a62af5 100644 --- a/src/com/fsck/k9/mail/store/local/StoreSchemaDefinition.java +++ b/src/com/fsck/k9/mail/store/local/StoreSchemaDefinition.java @@ -309,9 +309,9 @@ class StoreSchemaDefinition implements LockableDatabase.SchemaDefinition { LocalFolder obsoleteOutbox = new LocalFolder(this.localStore, localizedOutbox); if (obsoleteOutbox.exists()) { // Get all messages from the localized outbox ... - Message[] messages = obsoleteOutbox.getMessages(null, false); + List messages = obsoleteOutbox.getMessages(null, false); - if (messages.length > 0) { + if (messages.size() > 0) { // ... and move them to the drafts folder (we don't want to // surprise the user by sending potentially very old messages) LocalFolder drafts = new LocalFolder(this.localStore, this.localStore.getAccount().getDraftsFolderName()); @@ -424,7 +424,7 @@ class StoreSchemaDefinition implements LockableDatabase.SchemaDefinition { } - cv.put("flags", this.localStore.serializeFlags(extraFlags.toArray(LocalStore.EMPTY_FLAG_ARRAY))); + cv.put("flags", this.localStore.serializeFlags(extraFlags)); cv.put("read", read); cv.put("flagged", flagged); cv.put("answered", answered); diff --git a/src/com/fsck/k9/mail/transport/WebDavTransport.java b/src/com/fsck/k9/mail/transport/WebDavTransport.java index 6b5b61d60..1c94d01d5 100644 --- a/src/com/fsck/k9/mail/transport/WebDavTransport.java +++ b/src/com/fsck/k9/mail/transport/WebDavTransport.java @@ -11,6 +11,8 @@ import com.fsck.k9.mail.ServerSettings; import com.fsck.k9.mail.Transport; import com.fsck.k9.mail.store.WebDavStore; +import java.util.Collections; + public class WebDavTransport extends Transport { public static final String TRANSPORT_TYPE = WebDavStore.STORE_TYPE; @@ -66,6 +68,6 @@ public class WebDavTransport extends Transport { @Override public void sendMessage(Message message) throws MessagingException { - store.sendMessages(new Message[] { message }); + store.sendMessages(Collections.singletonList(message)); } } diff --git a/src/com/fsck/k9/preferences/SettingsExporter.java b/src/com/fsck/k9/preferences/SettingsExporter.java index e753e31f3..d7d77f6dc 100644 --- a/src/com/fsck/k9/preferences/SettingsExporter.java +++ b/src/com/fsck/k9/preferences/SettingsExporter.java @@ -132,7 +132,7 @@ public class SettingsExporter { Set exportAccounts; if (accountUuids == null) { - Account[] accounts = preferences.getAccounts(); + List accounts = preferences.getAccounts(); exportAccounts = new HashSet(); for (Account account : accounts) { exportAccounts.add(account.getUuid()); diff --git a/src/com/fsck/k9/preferences/SettingsImporter.java b/src/com/fsck/k9/preferences/SettingsImporter.java index c42995607..b3143f724 100644 --- a/src/com/fsck/k9/preferences/SettingsImporter.java +++ b/src/com/fsck/k9/preferences/SettingsImporter.java @@ -340,7 +340,7 @@ public class SettingsImporter { AccountDescription original = new AccountDescription(account.name, account.uuid); Preferences prefs = Preferences.getPreferences(context); - Account[] accounts = prefs.getAccounts(); + List accounts = prefs.getAccounts(); String uuid = account.uuid; Account existingAccount = prefs.getAccount(uuid); @@ -357,7 +357,7 @@ public class SettingsImporter { if (isAccountNameUsed(accountName, accounts)) { // Account name is already in use. So generate a new one by appending " (x)", where x // is the first number >= 1 that results in an unused account name. - for (int i = 1; i <= accounts.length; i++) { + for (int i = 1; i <= accounts.size(); i++) { accountName = account.name + " (" + i + ")"; if (!isAccountNameUsed(accountName, accounts)) { break; @@ -605,7 +605,7 @@ public class SettingsImporter { } } - private static boolean isAccountNameUsed(String name, Account[] accounts) { + private static boolean isAccountNameUsed(String name, List accounts) { for (Account account : accounts) { if (account == null) { continue; diff --git a/src/com/fsck/k9/service/DatabaseUpgradeService.java b/src/com/fsck/k9/service/DatabaseUpgradeService.java index 249af1e66..ee5b9b26b 100644 --- a/src/com/fsck/k9/service/DatabaseUpgradeService.java +++ b/src/com/fsck/k9/service/DatabaseUpgradeService.java @@ -1,5 +1,6 @@ package com.fsck.k9.service; +import java.util.List; import java.util.concurrent.atomic.AtomicBoolean; import android.app.Service; @@ -186,8 +187,8 @@ public class DatabaseUpgradeService extends Service { private void upgradeDatabases() { Preferences preferences = Preferences.getPreferences(this); - Account[] accounts = preferences.getAccounts(); - mProgressEnd = accounts.length; + List accounts = preferences.getAccounts(); + mProgressEnd = accounts.size(); mProgress = 0; for (Account account : accounts) { diff --git a/src/com/fsck/k9/service/RemoteControlReceiver.java b/src/com/fsck/k9/service/RemoteControlReceiver.java index 7843a1ac5..ecdbf12d2 100644 --- a/src/com/fsck/k9/service/RemoteControlReceiver.java +++ b/src/com/fsck/k9/service/RemoteControlReceiver.java @@ -11,6 +11,8 @@ import com.fsck.k9.K9; import com.fsck.k9.remotecontrol.K9RemoteControl; import com.fsck.k9.Preferences; +import java.util.List; + import static com.fsck.k9.remotecontrol.K9RemoteControl.*; public class RemoteControlReceiver extends CoreReceiver { @@ -25,12 +27,12 @@ public class RemoteControlReceiver extends CoreReceiver { } else if (K9RemoteControl.K9_REQUEST_ACCOUNTS.equals(intent.getAction())) { try { Preferences preferences = Preferences.getPreferences(context); - Account[] accounts = preferences.getAccounts(); - String[] uuids = new String[accounts.length]; - String[] descriptions = new String[accounts.length]; - for (int i = 0; i < accounts.length; i++) { + List accounts = preferences.getAccounts(); + String[] uuids = new String[accounts.size()]; + String[] descriptions = new String[accounts.size()]; + for (int i = 0; i < accounts.size(); i++) { //warning: account may not be isAvailable() - Account account = accounts[i]; + Account account = accounts.get(i); uuids[i] = account.getUuid(); descriptions[i] = account.getDescription(); diff --git a/src/com/fsck/k9/service/RemoteControlService.java b/src/com/fsck/k9/service/RemoteControlService.java index 126c71e28..e942f4d23 100644 --- a/src/com/fsck/k9/service/RemoteControlService.java +++ b/src/com/fsck/k9/service/RemoteControlService.java @@ -17,6 +17,8 @@ import android.content.SharedPreferences.Editor; import android.util.Log; import android.widget.Toast; +import java.util.List; + public class RemoteControlService extends CoreService { private final static String RESCHEDULE_ACTION = "com.fsck.k9.service.RemoteControlService.RESCHEDULE_ACTION"; private final static String PUSH_RESTART_ACTION = "com.fsck.k9.service.RemoteControlService.PUSH_RESTART_ACTION"; @@ -65,7 +67,7 @@ public class RemoteControlService extends CoreService { Log.i(K9.LOG_TAG, "RemoteControlService changing settings for account with UUID " + uuid); } } - Account[] accounts = preferences.getAccounts(); + List accounts = preferences.getAccounts(); for (Account account : accounts) { //warning: account may not be isAvailable() if (allAccounts || account.getUuid().equals(uuid)) {