Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d96641d7 authored by cketti's avatar cketti
Browse files

Remove optimization to reduce the number of times a wakelock is acquired

ImapConnection.isDataAvailable() didn't reliably work on all devices (returned true when no complete IMAP response was available). This lead to situations where a wakelock was being held the whole time the IDLE command was active. At the same time no alarm was set to refresh the IDLE connection. So most of the time the blocking read would time out.
parent ec1f3c37
Loading
Loading
Loading
Loading
+0 −3
Original line number Diff line number Diff line
@@ -44,7 +44,4 @@ internal interface ImapConnection {

    @Throws(SocketException::class)
    fun setSocketReadTimeout(timeout: Int)

    @Throws(IOException::class)
    fun isDataAvailable(): Boolean
}
+0 −5
Original line number Diff line number Diff line
@@ -910,11 +910,6 @@ class RealImapConnection implements ImapConnection {
        return isCondstoreCapable() ? LENGTH_LIMIT_WITH_CONDSTORE : LENGTH_LIMIT_WITHOUT_CONDSTORE;
    }

    @Override
    public boolean isDataAvailable() throws IOException {
        return inputStream.available() > 0;
    }

    @Override
    public int getConnectionGeneration() {
        return connectionGeneration;
+7 −14
Original line number Diff line number Diff line
@@ -95,26 +95,19 @@ internal class RealImapFolderIdler(

            var response: ImapResponse
            do {
                val expectSleeping = !connection.isDataAvailable() && !stopIdle
                idleRefreshTimer = idleRefreshManager.startTimer(
                    timeout = idleRefreshTimeoutProvider.idleRefreshTimeoutMs,
                    callback = ::idleRefresh
                )

                idleRefreshTimer = if (expectSleeping) {
                    idleRefreshManager.startTimer(idleRefreshTimeoutProvider.idleRefreshTimeoutMs) { idleRefresh() }
                } else {
                    null
                }

                if (expectSleeping) {
                wakeLock.release()
                }

                try {
                    response = connection.readResponse()
                } finally {
                    if (expectSleeping) {
                    wakeLock.acquire()
                    idleRefreshTimer?.cancel()
                }
                }

                if (response.isRelevant && !stopIdle) {
                    Timber.v("%s.idle(): Received a relevant untagged response during IDLE", logTag)
+0 −1
Original line number Diff line number Diff line
@@ -121,7 +121,6 @@ class RealImapFolderIdlerTest {
        wakeLock.waitForRelease()
        imapConnection.enqueueUntaggedServerResponse("1 EXISTS")
        imapConnection.waitForCommand("DONE")
        assertThat(wakeLock.isHeld).isTrue()
        imapConnection.enqueueTaggedServerResponse("OK")

        latch.awaitWithTimeout()
+0 −2
Original line number Diff line number Diff line
@@ -94,8 +94,6 @@ internal open class TestImapConnection(val timeout: Long, override val connectio
        currentSocketReadTimeout = timeout
    }

    override fun isDataAvailable(): Boolean = false

    fun waitForCommand(command: String) {
        do {
            val receivedCommand = receivedCommands.poll(timeout, TimeUnit.SECONDS) ?: throw AssertionError("Timeout")