public void start() { synchronized (threadLock) { if (listeningThread != null) { throw new IllegalStateException("start() called twice"); } listeningThread = new Thread(new PushRunnable()); listeningThread.start(); } }
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 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); } } } }
while (!stop) { try { long oldUidNext = getOldUidNext(); boolean openedNewConnection = openConnectionIfNecessary(); if (pushPollOnConnect && (openedNewConnection || needsPoll)) { needsPoll = false; syncFolderOnConnect(); long newUidNext = getNewUidNext(); lastUidNext = newUidNext; long startUid = getStartUid(oldUidNext, newUidNext); notifyMessagesArrived(startUid, newUidNext); } else { processStoredUntaggedResponses(); prepareForIdle(); setReadTimeoutForIdle(conn); sendIdle(conn); returnFromIdle(); reacquireWakeLockAndCleanUp(); stop = true; } catch (CertificateValidationException e) {
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; }
private boolean openConnectionIfNecessary() throws MessagingException { ImapConnection oldConnection = connection; internalOpen(OPEN_MODE_RO); ImapConnection conn = connection; checkConnectionNotNull(conn); checkConnectionIdleCapable(conn); return conn != oldConnection; }