From 858051154111e8e725ea47d335adc3b3a0f095e9 Mon Sep 17 00:00:00 2001 From: Marc Blank Date: Tue, 11 Sep 2012 08:56:56 -0700 Subject: [PATCH] Handle loadMore when no messages are loaded Bug: 7129772 Change-Id: I9d2fd4f2bc488930c53e9fe44dc3fa852409b5c0 --- .../android/email/imap2/Imap2SyncService.java | 73 +++++++++++-------- 1 file changed, 44 insertions(+), 29 deletions(-) diff --git a/src/com/android/email/imap2/Imap2SyncService.java b/src/com/android/email/imap2/Imap2SyncService.java index 3c3b002e5..b0e0ab006 100644 --- a/src/com/android/email/imap2/Imap2SyncService.java +++ b/src/com/android/email/imap2/Imap2SyncService.java @@ -2032,9 +2032,19 @@ public class Imap2SyncService extends AbstractSyncService { * @throws IOException */ private int[] handleLoadMore(int[] serverList, int howManyMore) throws IOException { - // User has asked for more; find the oldest message + // Easiest case; we have no messages.. just start at the last one in the mailbox + if (serverList.length == 0) { + if (mLastExists > 0) { + return loadMoreImpl(serverList, mLastExists, howManyMore); + } + return serverList; + } + + // First, sort the list Arrays.sort(serverList); + // Oldest is the first in the sorted list int oldest = serverList[0]; + // Get its current sequence number String tag = writeCommand(mWriter, "uid fetch " + oldest + " (UID)"); // IMAP_OK if we want it to work @@ -2042,38 +2052,43 @@ public class Imap2SyncService extends AbstractSyncService { String line = mImapResponse.get(0); Parser lp = new Parser(line.substring(2)); // Last one we want is one before this message - int end = lp.parseInteger() - 1; - int start = end - howManyMore + 1; - if (start < 1) { - start = 1; - } - if (end > 0) { - // Get the uid's of the messages to load - tag = writeCommand(mWriter, "uid search " + start + ":" + end); - // IMAP_OK if we want it to work - if (readResponse(mReader, tag, "SEARCH").equals(IMAP_OK)) { - int[] moreServerList; + return loadMoreImpl(serverList, lp.parseInteger() - 1, howManyMore); + } + // In worst case, just return the original list + return serverList; + } - // Parse the list - if (mImapResponse.isEmpty()) { - // Just return the original list - return serverList; - } else { - String msgs = mImapResponse.get(0); - // Length of "* search" - Parser p = new Parser(msgs, 8); - moreServerList = p.gatherInts(); - userLog("[Load more found " + moreServerList.length + " messages]"); - int[] completeList = new int[serverList.length + moreServerList.length]; - System.arraycopy(serverList, 0, completeList, 0, serverList.length); - System.arraycopy(moreServerList, 0, completeList, serverList.length, - moreServerList.length); - return completeList; - } + private int[] loadMoreImpl(int[] serverList, int end, int howManyMore) throws IOException { + + int start = end - howManyMore + 1; + if (start < 1) { + start = 1; + } + if (end > 0) { + // Get the uid's of the messages to load + String tag = writeCommand(mWriter, "uid search " + start + ":" + end); + // IMAP_OK if we want it to work + if (readResponse(mReader, tag, "SEARCH").equals(IMAP_OK)) { + int[] moreServerList; + + // Parse the list + if (mImapResponse.isEmpty()) { + // Just return the original list + return serverList; + } else { + String msgs = mImapResponse.get(0); + // Length of "* search" + Parser p = new Parser(msgs, 8); + moreServerList = p.gatherInts(); + userLog("[Load more found " + moreServerList.length + " messages]"); + int[] completeList = new int[serverList.length + moreServerList.length]; + System.arraycopy(serverList, 0, completeList, 0, serverList.length); + System.arraycopy(moreServerList, 0, completeList, serverList.length, + moreServerList.length); + return completeList; } } } - // In worst case, just return the original list return serverList; }