public void stop() { synchronized (threadLock) { if (listeningThread == null) { throw new IllegalStateException("stop() called twice"); } stop = true; listeningThread.interrupt(); listeningThread = null; } ImapConnection conn = connection; if (conn != null) { if (K9MailLib.isDebug()) { Timber.v("Closing connection to stop pushing for %s", getLogId()); } conn.close(); } else { Timber.w("Attempt to interrupt null connection to stop pushing on folderPusher for %s", getLogId()); } }
private void notifyMessagesArrived(long startUid, long uidNext) { if (K9MailLib.isDebug()) { Timber.i("Needs sync from uid %d to %d for %s", startUid, uidNext, getLogId()); } int count = (int) (uidNext - startUid); List<Message> messages = new ArrayList<>(count); for (long uid = startUid; uid < uidNext; uid++) { ImapMessage message = new ImapMessage(Long.toString(uid), ImapFolderPusher.this); messages.add(message); } pushReceiver.messagesArrived(ImapFolderPusher.this, messages); }
private void processStoredUntaggedResponses() throws MessagingException { while (true) { List<ImapResponse> untaggedResponses = getAndClearStoredUntaggedResponses(); if (untaggedResponses.isEmpty()) { break; } if (K9MailLib.isDebug()) { Timber.i("Processing %d untagged responses from previous commands for %s", untaggedResponses.size(), getLogId()); } processUntaggedResponses(untaggedResponses); } }
private long getOldUidNext() { long oldUidNext = -1L; try { String serializedPushState = pushReceiver.getPushState(getServerId()); ImapPushState pushState = ImapPushState.parse(serializedPushState); oldUidNext = pushState.uidNext; if (K9MailLib.isDebug()) { Timber.i("Got oldUidNext %d for %s", oldUidNext, getLogId()); } } catch (Exception e) { Timber.e(e, "Unable to get oldUidNext for %s", getLogId()); } return oldUidNext; } }
private void syncMessages(int end) throws MessagingException { long oldUidNext = getOldUidNext(); List<ImapMessage> messageList = getMessages(end, end, null, true, null); if (messageList != null && messageList.size() > 0) { long newUid = Long.parseLong(messageList.get(0).getUid()); if (K9MailLib.isDebug()) { Timber.i("Got newUid %s for message %d on %s", newUid, end, getLogId()); } long startUid = oldUidNext; if (startUid < newUid - 10) { startUid = newUid - 10; } if (startUid < 1) { startUid = 1; } if (newUid >= startUid) { if (K9MailLib.isDebug()) { Timber.i("Needs sync from uid %d to %d for %s", startUid, newUid, getLogId()); } List<Message> messages = new ArrayList<>(); for (long uid = startUid; uid <= newUid; uid++) { ImapMessage message = new ImapMessage(Long.toString(uid), ImapFolderPusher.this); messages.add(message); } if (!messages.isEmpty()) { pushReceiver.messagesArrived(ImapFolderPusher.this, messages); } } } }
private void processUntaggedResponses(List<ImapResponse> responses) throws MessagingException { boolean skipSync = false; int oldMessageCount = messageCount; if (oldMessageCount == -1) { skipSync = true; } List<Long> flagSyncMsgSeqs = new ArrayList<>(); List<String> removeMsgUids = new LinkedList<>(); for (ImapResponse response : responses) { oldMessageCount += processUntaggedResponse(oldMessageCount, response, flagSyncMsgSeqs, removeMsgUids); } if (!skipSync) { if (oldMessageCount < 0) { oldMessageCount = 0; } if (messageCount > oldMessageCount) { syncMessages(messageCount); } } if (K9MailLib.isDebug()) { Timber.d("UIDs for messages needing flag sync are %s for %s", flagSyncMsgSeqs, getLogId()); } if (!flagSyncMsgSeqs.isEmpty()) { syncMessages(flagSyncMsgSeqs); } if (!removeMsgUids.isEmpty()) { removeMessages(removeMsgUids); } }
private void reacquireWakeLockAndCleanUp() { wakeLock.acquire(PUSH_WAKE_LOCK_TIMEOUT); clearStoredUntaggedResponses(); idling = false; pushReceiver.setPushActive(getServerId(), false); try { connection.close(); } catch (Exception me) { Timber.e(me, "Got exception while closing for exception for %s", getLogId()); } connection = null; }
Timber.i("Pusher starting for %s", getLogId()); Timber.i("About to IDLE for %s", getLogId()); Timber.i("Got exception while idling, but stop is set for %s", getLogId()); } else { pushReceiver.pushError("Push error for " + getServerId(), e); Timber.e("Got exception while idling for %s", getLogId()); Timber.e("Disabling pusher for %s after %d consecutive errors", getLogId(), idleFailureCount); pushReceiver.pushError("Push disabled for " + getServerId() + " after " + idleFailureCount + " consecutive errors", e); Timber.i("Pusher for %s is exiting", getLogId()); Timber.e(me, "Got exception while closing for %s", getLogId()); } finally { wakeLock.release();