From 006ea81b71a4bc60f5fad8df454a2c25c12cd415 Mon Sep 17 00:00:00 2001 From: Danny Baumann Date: Tue, 9 Jun 2015 12:54:00 +0200 Subject: [PATCH] Don't solely rely on the presence of RECENT for checking for new mail. It may happen (depending on server and/or timing) that only an EXISTS response is sent to the IDLE connection when new mail arrives. Don't discard that response, but evaluate it to determine whether there's new mail by checking whether the message count increased. Change-Id: Ia49714e6cd42dd71dfda8b7bbdf1fd622972edda --- .../android/email/mail/store/ImapFolder.java | 30 +++++++++++++++---- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/provider_src/com/android/email/mail/store/ImapFolder.java b/provider_src/com/android/email/mail/store/ImapFolder.java index 51e99d97a..97dca901d 100644 --- a/provider_src/com/android/email/mail/store/ImapFolder.java +++ b/provider_src/com/android/email/mail/store/ImapFolder.java @@ -548,7 +548,9 @@ public class ImapFolder extends Folder { @Override public int getMessageCount() { - return mMessageCount; + synchronized (this) { + return mMessageCount; + } } @Override @@ -1103,7 +1105,9 @@ public class ImapFolder extends Folder { */ private void handleUntaggedResponse(ImapResponse response) { if (response.isDataResponse(1, ImapConstants.EXISTS)) { - mMessageCount = response.getStringOrEmpty(0).getNumberOrZero(); + synchronized (this) { + mMessageCount = response.getStringOrEmpty(0).getNumberOrZero(); + } } } @@ -1541,9 +1545,10 @@ public class ImapFolder extends Folder { // OK DONE // No more changes // n EXISTS - // Indicates that the mailbox changed => ignore + // Indicates the number of messages in the mailbox => handle like + // RECENT if the number increased // n EXPUNGE - // Indicates a message were completely deleted => a full sync is required + // Indicates a message was completely deleted => a full sync is required // n RECENT // New messages waiting in the server => use UIDNEXT to search for the new messages. // If isn't possible to retrieve the new UID messages, then a full sync is required @@ -1587,9 +1592,24 @@ public class ImapFolder extends Folder { if (op.is(ImapConstants.DONE)) { break; } else if (op.is(ImapConstants.EXISTS)) { - continue; + int newMessageCount = change.getStringOrEmpty(0).getNumberOrZero(); + int oldMessageCount; + synchronized (this) { + oldMessageCount = mMessageCount; + mMessageCount = newMessageCount; + } + if (Logging.LOGD) { + LogUtils.d(LOG_TAG, "Got EXISTS idle response, message count now " + + newMessageCount + ", was " + oldMessageCount); + } + if (newMessageCount > oldMessageCount) { + hasNewMessages = true; + } } else if (op.is(ImapConstants.EXPUNGE)) { imapIdleChanges.mRequiredSync = true; + synchronized (this) { + mMessageCount--; + } } else if (op.is(ImapConstants.RECENT)) { hasNewMessages = true; } else if (op.is(ImapConstants.FETCH)