From 23d4c3fd68381bd48b3b461583729ae36cf5a89d Mon Sep 17 00:00:00 2001 From: cketti Date: Wed, 10 Feb 2016 23:55:59 +0100 Subject: [PATCH] Reorder methods --- .../k9/mail/store/imap/ImapFolderPusher.java | 350 +++++++++--------- 1 file changed, 175 insertions(+), 175 deletions(-) diff --git a/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapFolderPusher.java b/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapFolderPusher.java index 5b60c7745..7fe290814 100644 --- a/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapFolderPusher.java +++ b/k9mail-library/src/main/java/com/fsck/k9/mail/store/imap/ImapFolderPusher.java @@ -59,6 +59,11 @@ class ImapFolderPusher extends ImapFolder implements UntaggedHandler { wakeLock.setReferenceCounted(false); } + public void start() { + listeningThread = new Thread(new PushRunnable()); + listeningThread.start(); + } + public void refresh() throws IOException, MessagingException { if (idling.get()) { wakeLock.acquire(PUSH_WAKE_LOCK_TIMEOUT); @@ -66,6 +71,25 @@ class ImapFolderPusher extends ImapFolder implements UntaggedHandler { } } + public void stop() { + stop.set(true); + + if (listeningThread != null) { + listeningThread.interrupt(); + } + + ImapConnection conn = connection; + if (conn != null) { + if (K9MailLib.isDebug()) { + Log.v(LOG_TAG, "Closing connection to stop pushing for " + getLogId()); + } + + conn.close(); + } else { + Log.w(LOG_TAG, "Attempt to interrupt null connection to stop pushing on folderPusher for " + getLogId()); + } + } + private void sendDone() throws IOException, MessagingException { if (doneSent.compareAndSet(false, true)) { ImapConnection connection = this.connection; @@ -85,9 +109,50 @@ class ImapFolderPusher extends ImapFolder implements UntaggedHandler { conn.setReadTimeout(idleRefreshTimeout + IDLE_READ_TIMEOUT_INCREMENT); } - public void start() { - listeningThread = new Thread(new PushRunnable()); - listeningThread.start(); + @Override + public void handleAsyncUntaggedResponse(ImapResponse response) { + if (K9MailLib.isDebug()) { + Log.v(LOG_TAG, "Got async response: " + response); + } + + if (stop.get()) { + if (K9MailLib.isDebug()) { + Log.d(LOG_TAG, "Got async untagged response: " + response + ", but stop is set for " + getLogId()); + } + + try { + sendDone(); + } catch (Exception e) { + Log.e(LOG_TAG, "Exception while sending DONE for " + getLogId(), e); + } + } else { + if (response.getTag() == null) { + if (response.size() > 1) { + Object responseType = response.get(1); + if (equalsIgnoreCase(responseType, "EXISTS") || equalsIgnoreCase(responseType, "EXPUNGE") || + equalsIgnoreCase(responseType, "FETCH")) { + + wakeLock.acquire(PUSH_WAKE_LOCK_TIMEOUT); + + if (K9MailLib.isDebug()) { + Log.d(LOG_TAG, "Got useful async untagged response: " + response + " for " + getLogId()); + } + + try { + sendDone(); + } catch (Exception e) { + Log.e(LOG_TAG, "Exception while sending DONE for " + getLogId(), e); + } + } + } else if (response.isContinuationRequested()) { + if (K9MailLib.isDebug()) { + Log.d(LOG_TAG, "Idling " + getLogId()); + } + + wakeLock.release(); + } + } + } } @Override @@ -108,7 +173,21 @@ class ImapFolderPusher extends ImapFolder implements UntaggedHandler { } } - protected void processUntaggedResponses(List responses) throws MessagingException { + private void processStoredUntaggedResponses() throws MessagingException { + List untaggedResponses; + while (!storedUntaggedResponses.isEmpty()) { + if (K9MailLib.isDebug()) { + Log.i(LOG_TAG, "Processing " + storedUntaggedResponses.size() + + " untagged responses from previous commands for " + getLogId()); + } + + untaggedResponses = new ArrayList(storedUntaggedResponses); + storedUntaggedResponses.clear(); + processUntaggedResponses(untaggedResponses); + } + } + + private void processUntaggedResponses(List responses) throws MessagingException { boolean skipSync = false; int oldMessageCount = messageCount; @@ -146,6 +225,91 @@ class ImapFolderPusher extends ImapFolder implements UntaggedHandler { } } + private int processUntaggedResponse(long oldMessageCount, ImapResponse response, List flagSyncMsgSeqs, + List removeMsgUids) { + super.handleUntaggedResponse(response); + + int messageCountDelta = 0; + if (response.getTag() == null && response.size() > 1) { + try { + Object responseType = response.get(1); + if (equalsIgnoreCase(responseType, "FETCH")) { + Log.i(LOG_TAG, "Got FETCH " + response); + + long msgSeq = response.getLong(0); + + if (K9MailLib.isDebug()) { + Log.d(LOG_TAG, "Got untagged FETCH for msgseq " + msgSeq + " for " + getLogId()); + } + + if (!flagSyncMsgSeqs.contains(msgSeq)) { + flagSyncMsgSeqs.add(msgSeq); + } + } + + if (equalsIgnoreCase(responseType, "EXPUNGE")) { + long msgSeq = response.getLong(0); + if (msgSeq <= oldMessageCount) { + messageCountDelta = -1; + } + + if (K9MailLib.isDebug()) { + Log.d(LOG_TAG, "Got untagged EXPUNGE for msgseq " + msgSeq + " for " + getLogId()); + } + + List newSeqs = new ArrayList(); + Iterator flagIter = flagSyncMsgSeqs.iterator(); + while (flagIter.hasNext()) { + long flagMsg = flagIter.next(); + if (flagMsg >= msgSeq) { + flagIter.remove(); + if (flagMsg > msgSeq) { + newSeqs.add(flagMsg); + } + } + } + + flagSyncMsgSeqs.addAll(newSeqs); + + List msgSeqs = new ArrayList(msgSeqUidMap.keySet()); + Collections.sort(msgSeqs); // Have to do comparisons in order because of msgSeq reductions + + for (long msgSeqNum : msgSeqs) { + if (K9MailLib.isDebug()) { + Log.v(LOG_TAG, "Comparing EXPUNGEd msgSeq " + msgSeq + " to " + msgSeqNum); + } + + if (msgSeqNum == msgSeq) { + String uid = msgSeqUidMap.get(msgSeqNum); + + if (K9MailLib.isDebug()) { + Log.d(LOG_TAG, "Scheduling removal of UID " + uid + " because msgSeq " + msgSeqNum + + " was expunged"); + } + + removeMsgUids.add(uid); + msgSeqUidMap.remove(msgSeqNum); + } else if (msgSeqNum > msgSeq) { + String uid = msgSeqUidMap.get(msgSeqNum); + + if (K9MailLib.isDebug()) { + Log.d(LOG_TAG, "Reducing msgSeq for UID " + uid + " from " + msgSeqNum + " to " + + (msgSeqNum - 1)); + } + + msgSeqUidMap.remove(msgSeqNum); + msgSeqUidMap.put(msgSeqNum - 1, uid); + } + } + } + } catch (Exception e) { + Log.e(LOG_TAG, "Could not handle untagged FETCH for " + getLogId(), e); + } + } + + return messageCountDelta; + } + private void syncMessages(int end) throws MessagingException { long oldUidNext = getOldUidNext(); @@ -230,103 +394,16 @@ class ImapFolderPusher extends ImapFolder implements UntaggedHandler { } } - protected int processUntaggedResponse(long oldMessageCount, ImapResponse response, List flagSyncMsgSeqs, - List removeMsgUids) { - super.handleUntaggedResponse(response); + private void syncFolderOnConnect() throws MessagingException { + List untaggedResponses = new ArrayList(storedUntaggedResponses); + storedUntaggedResponses.clear(); + processUntaggedResponses(untaggedResponses); - int messageCountDelta = 0; - if (response.getTag() == null && response.size() > 1) { - try { - Object responseType = response.get(1); - if (equalsIgnoreCase(responseType, "FETCH")) { - Log.i(LOG_TAG, "Got FETCH " + response); - - long msgSeq = response.getLong(0); - - if (K9MailLib.isDebug()) { - Log.d(LOG_TAG, "Got untagged FETCH for msgseq " + msgSeq + " for " + getLogId()); - } - - if (!flagSyncMsgSeqs.contains(msgSeq)) { - flagSyncMsgSeqs.add(msgSeq); - } - } - - if (equalsIgnoreCase(responseType, "EXPUNGE")) { - long msgSeq = response.getLong(0); - if (msgSeq <= oldMessageCount) { - messageCountDelta = -1; - } - - if (K9MailLib.isDebug()) { - Log.d(LOG_TAG, "Got untagged EXPUNGE for msgseq " + msgSeq + " for " + getLogId()); - } - - List newSeqs = new ArrayList(); - Iterator flagIter = flagSyncMsgSeqs.iterator(); - while (flagIter.hasNext()) { - long flagMsg = flagIter.next(); - if (flagMsg >= msgSeq) { - flagIter.remove(); - if (flagMsg > msgSeq) { - newSeqs.add(flagMsg); - } - } - } - - flagSyncMsgSeqs.addAll(newSeqs); - - List msgSeqs = new ArrayList(msgSeqUidMap.keySet()); - Collections.sort(msgSeqs); // Have to do comparisons in order because of msgSeq reductions - - for (long msgSeqNum : msgSeqs) { - if (K9MailLib.isDebug()) { - Log.v(LOG_TAG, "Comparing EXPUNGEd msgSeq " + msgSeq + " to " + msgSeqNum); - } - - if (msgSeqNum == msgSeq) { - String uid = msgSeqUidMap.get(msgSeqNum); - - if (K9MailLib.isDebug()) { - Log.d(LOG_TAG, "Scheduling removal of UID " + uid + " because msgSeq " + msgSeqNum + - " was expunged"); - } - - removeMsgUids.add(uid); - msgSeqUidMap.remove(msgSeqNum); - } else if (msgSeqNum > msgSeq) { - String uid = msgSeqUidMap.get(msgSeqNum); - - if (K9MailLib.isDebug()) { - Log.d(LOG_TAG, "Reducing msgSeq for UID " + uid + " from " + msgSeqNum + " to " + - (msgSeqNum - 1)); - } - - msgSeqUidMap.remove(msgSeqNum); - msgSeqUidMap.put(msgSeqNum - 1, uid); - } - } - } - } catch (Exception e) { - Log.e(LOG_TAG, "Could not handle untagged FETCH for " + getLogId(), e); - } + if (messageCount == -1) { + throw new MessagingException("Message count = -1 for idling"); } - return messageCountDelta; - } - - private void processStoredUntaggedResponses() throws MessagingException { - List untaggedResponses; - while (!storedUntaggedResponses.isEmpty()) { - if (K9MailLib.isDebug()) { - Log.i(LOG_TAG, "Processing " + storedUntaggedResponses.size() + - " untagged responses from previous commands for " + getLogId()); - } - - untaggedResponses = new ArrayList(storedUntaggedResponses); - storedUntaggedResponses.clear(); - processUntaggedResponses(untaggedResponses); - } + pushReceiver.syncFolder(ImapFolderPusher.this); } private void notifyMessagesArrived(long startUid, long uidNext) { @@ -345,71 +422,6 @@ class ImapFolderPusher extends ImapFolder implements UntaggedHandler { pushReceiver.messagesArrived(this, messages); } - public void stop() { - stop.set(true); - - if (listeningThread != null) { - listeningThread.interrupt(); - } - - ImapConnection conn = connection; - if (conn != null) { - if (K9MailLib.isDebug()) { - Log.v(LOG_TAG, "Closing connection to stop pushing for " + getLogId()); - } - - conn.close(); - } else { - Log.w(LOG_TAG, "Attempt to interrupt null connection to stop pushing on folderPusher for " + getLogId()); - } - } - - @Override - public void handleAsyncUntaggedResponse(ImapResponse response) { - if (K9MailLib.isDebug()) { - Log.v(LOG_TAG, "Got async response: " + response); - } - - if (stop.get()) { - if (K9MailLib.isDebug()) { - Log.d(LOG_TAG, "Got async untagged response: " + response + ", but stop is set for " + getLogId()); - } - - try { - sendDone(); - } catch (Exception e) { - Log.e(LOG_TAG, "Exception while sending DONE for " + getLogId(), e); - } - } else { - if (response.getTag() == null) { - if (response.size() > 1) { - Object responseType = response.get(1); - if (equalsIgnoreCase(responseType, "EXISTS") || equalsIgnoreCase(responseType, "EXPUNGE") || - equalsIgnoreCase(responseType, "FETCH")) { - - wakeLock.acquire(PUSH_WAKE_LOCK_TIMEOUT); - - if (K9MailLib.isDebug()) { - Log.d(LOG_TAG, "Got useful async untagged response: " + response + " for " + getLogId()); - } - - try { - sendDone(); - } catch (Exception e) { - Log.e(LOG_TAG, "Exception while sending DONE for " + getLogId(), e); - } - } - } else if (response.isContinuationRequested()) { - if (K9MailLib.isDebug()) { - Log.d(LOG_TAG, "Idling " + getLogId()); - } - - wakeLock.release(); - } - } - } - } - private long getOldUidNext() { long oldUidNext = -1L; try { @@ -427,18 +439,6 @@ class ImapFolderPusher extends ImapFolder implements UntaggedHandler { return oldUidNext; } - private void syncFolderOnConnect() throws MessagingException { - List untaggedResponses = new ArrayList(storedUntaggedResponses); - storedUntaggedResponses.clear(); - processUntaggedResponses(untaggedResponses); - - if (messageCount == -1) { - throw new MessagingException("Message count = -1 for idling"); - } - - pushReceiver.syncFolder(ImapFolderPusher.this); - } - private class PushRunnable implements Runnable { @Override