From 7ea823327f4c62066a3d466158a6ab67882319df Mon Sep 17 00:00:00 2001 From: Martin Hibdon Date: Thu, 3 Apr 2014 12:40:09 -0700 Subject: [PATCH] Bound the number of headers we might try to fetch b/13545303 Prior to this change, there is no bound on the number of message headers we might try to fetch at the same time. Now, we fetch headers in chunks of limited size. Change-Id: I06db96eefa8732f970c31fc3617480de40723f2d --- .../android/email/service/ImapService.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/com/android/email/service/ImapService.java b/src/com/android/email/service/ImapService.java index 521c610da..719bb64cf 100644 --- a/src/com/android/email/service/ImapService.java +++ b/src/com/android/email/service/ImapService.java @@ -68,6 +68,7 @@ import java.util.Arrays; import java.util.Comparator; import java.util.Date; import java.util.HashMap; +import java.util.List; public class ImapService extends Service { // TODO get these from configurations or settings. @@ -75,6 +76,8 @@ public class ImapService extends Service { private static final long FULL_SYNC_WINDOW_MILLIS = 7 * DateUtils.DAY_IN_MILLIS; private static final long FULL_SYNC_INTERVAL_MILLIS = 4 * DateUtils.HOUR_IN_MILLIS; + // The maximum number of messages to fetch in a single command. + private static final int MAX_MESSAGES_TO_FETCH = 500; private static final int MINIMUM_MESSAGES_TO_SYNC = 10; private static final int LOAD_MORE_MIN_INCREMENT = 10; private static final int LOAD_MORE_MAX_INCREMENT = 20; @@ -615,9 +618,25 @@ public class ImapService extends Service { // 11. Refresh the flags for any messages in the local store that we didn't just download. // TODO This is a bit wasteful because we're also updating any messages we already did get // the flags and envelope for previously. + // TODO: the fetch() function, and others, should take List<>s of messages, not + // arrays of messages. FetchProfile fp = new FetchProfile(); fp.add(FetchProfile.Item.FLAGS); - remoteFolder.fetch(remoteMessages, fp, null); + if (remoteMessages.length > MAX_MESSAGES_TO_FETCH) { + List remoteMessageList = Arrays.asList(remoteMessages); + for (int start = 0; start < remoteMessageList.size(); start += MAX_MESSAGES_TO_FETCH) { + int end = start + MAX_MESSAGES_TO_FETCH; + if (end >= remoteMessageList.size()) { + end = remoteMessageList.size() - 1; + } + List chunk = remoteMessageList.subList(start, end); + final Message[] partialArray = chunk.toArray(new Message[chunk.size()]); + // Fetch this one chunk of messages + remoteFolder.fetch(partialArray, fp, null); + } + } else { + remoteFolder.fetch(remoteMessages, fp, null); + } boolean remoteSupportsSeen = false; boolean remoteSupportsFlagged = false; boolean remoteSupportsAnswered = false;