From 9a2686afa2e02c62a8e5f9c42c82bd2da70b96af Mon Sep 17 00:00:00 2001 From: Yu Ping Hu Date: Mon, 15 Apr 2013 13:27:07 -0700 Subject: [PATCH] Improve POP sync. - Set socket timeout as soon as you open it. This was how IMAP worked, so I just moved the call up one level. This should help one of the sync forever scenarios. - Simplify the SQL query for getting a Mailbox. - Try to load messages that failed to load last time. - Always close the connection to the remote folder. - Don't try to be too clever in fetching the body. This needs to be fixed later for attachements, but for now seems to work better and will let me get more testing. Change-Id: I91b6a6f2a2846b34b1a0c50eb4eb37fc947389ce --- .../android/email/mail/store/ImapConnection.java | 2 -- .../email/mail/transport/MailTransport.java | 2 +- src/com/android/email/service/Pop3Service.java | 15 ++++++++------- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/src/com/android/email/mail/store/ImapConnection.java b/src/com/android/email/mail/store/ImapConnection.java index 68baa4fc3..ee9acfc2a 100644 --- a/src/com/android/email/mail/store/ImapConnection.java +++ b/src/com/android/email/mail/store/ImapConnection.java @@ -106,7 +106,6 @@ class ImapConnection { } mTransport.open(); - mTransport.setSoTimeout(MailTransport.SOCKET_READ_TIMEOUT); createParser(); @@ -498,7 +497,6 @@ class ImapConnection { executeSimpleCommand(ImapConstants.STARTTLS); mTransport.reopenTls(); - mTransport.setSoTimeout(MailTransport.SOCKET_READ_TIMEOUT); createParser(); // Per RFC requirement (3501-6.2.1) gather new capabilities return(queryCapabilities()); diff --git a/src/com/android/email/mail/transport/MailTransport.java b/src/com/android/email/mail/transport/MailTransport.java index 692eef177..128596460 100644 --- a/src/com/android/email/mail/transport/MailTransport.java +++ b/src/com/android/email/mail/transport/MailTransport.java @@ -123,7 +123,7 @@ public class MailTransport { } mIn = new BufferedInputStream(mSocket.getInputStream(), 1024); mOut = new BufferedOutputStream(mSocket.getOutputStream(), 512); - + mSocket.setSoTimeout(SOCKET_READ_TIMEOUT); } catch (SSLException e) { if (MailActivityEmail.DEBUG) { Log.d(Logging.LOG_TAG, e.toString()); diff --git a/src/com/android/email/service/Pop3Service.java b/src/com/android/email/service/Pop3Service.java index 1f2cfdedd..e16ed00bc 100644 --- a/src/com/android/email/service/Pop3Service.java +++ b/src/com/android/email/service/Pop3Service.java @@ -205,8 +205,10 @@ public class Pop3Service extends Service { // We'll load them from most recent to oldest for (int i = cnt - 1; i >= 0; i--) { Pop3Message message = unsyncedMessages.get(i); - remoteFolder.fetchBody(message, Pop3Store.FETCH_BODY_SANE_SUGGESTED_SIZE / 76, - null); + // TODO: this fetches the entire message at once. This should go back to trying + // to avoid downloading attachments initially. Specifically, the second argument + // below used to be Pop3Store.FETCH_BODY_SANE_SUGGESTED_SIZE / 76 + remoteFolder.fetchBody(message, -1, null); int flag = EmailContent.Message.FLAG_LOADED_COMPLETE; if (!message.isComplete()) { flag = EmailContent.Message.FLAG_LOADED_UNKNOWN; @@ -266,10 +268,8 @@ public class Pop3Service extends Service { localUidCursor = resolver.query( EmailContent.Message.CONTENT_URI, LocalMessageInfo.PROJECTION, - EmailContent.MessageColumns.ACCOUNT_KEY + "=?" + - " AND " + MessageColumns.MAILBOX_KEY + "=?", + MessageColumns.MAILBOX_KEY + "=?", new String[] { - String.valueOf(account.mId), String.valueOf(mailbox.mId) }, null); @@ -356,9 +356,9 @@ public class Pop3Service extends Service { remoteUidMap.put(uid, message); LocalMessageInfo localMessage = localMessageMap.get(uid); // localMessage == null -> message has never been created (not even headers) - // mFlagLoaded = UNLOADED -> message created, but none of body loaded + // mFlagLoaded != FLAG_LOADED_COMPLETE -> message failed to sync completely if (localMessage == null || - (localMessage.mFlagLoaded == EmailContent.Message.FLAG_LOADED_UNLOADED)) { + (localMessage.mFlagLoaded != EmailContent.Message.FLAG_LOADED_COMPLETE)) { unsyncedMessages.add(message); } } @@ -366,6 +366,7 @@ public class Pop3Service extends Service { if (MailActivityEmail.DEBUG) { Log.d(TAG, "*** Message count is zero??"); } + remoteFolder.close(false); return; }