Merge pull request #378 from vector-im/feature/fix_sync_thread_wrong_autostart

Fix / SyncThread was started in background
This commit is contained in:
Benoit Marty 2019-07-17 14:32:19 +02:00 committed by GitHub
commit 927cd7285d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 5 deletions

View File

@ -87,7 +87,7 @@ interface Session :
/**
* This method start the sync thread.
*/
fun startSync()
fun startSync(fromForeground : Boolean)

/**
* This method stop the sync thread.

View File

@ -105,8 +105,10 @@ internal class DefaultSession @Inject constructor(override val sessionParams: Se
SyncWorker.stopAnyBackgroundSync(context)
}

override fun startSync() {
override fun startSync(fromForeground : Boolean) {
Timber.i("Starting sync thread")
assert(isOpen)
syncThread.setInitialForeground(fromForeground)
if (!syncThread.isAlive) {
syncThread.start()
} else {

View File

@ -54,6 +54,10 @@ internal class SyncThread @Inject constructor(private val syncTask: SyncTask,
updateStateTo(SyncState.IDLE)
}

fun setInitialForeground(initialForground : Boolean) {
updateStateTo(if (initialForground) SyncState.IDLE else SyncState.PAUSED)
}

fun restart() = synchronized(lock) {
if (state is SyncState.PAUSED) {
Timber.v("Resume sync...")
@ -84,7 +88,6 @@ internal class SyncThread @Inject constructor(private val syncTask: SyncTask,
Timber.v("Start syncing...")
networkConnectivityChecker.register(this)
backgroundDetectionObserver.register(this)
updateStateTo(SyncState.RUNNING(catchingUp = true))

while (state != SyncState.KILLING) {
if (!networkConnectivityChecker.isConnected() || state == SyncState.PAUSED) {
@ -93,7 +96,8 @@ internal class SyncThread @Inject constructor(private val syncTask: SyncTask,
lock.wait()
}
} else {
Timber.v("Execute sync request with timeout $DEFAULT_LONG_POOL_TIMEOUT")
updateStateTo(SyncState.RUNNING(catchingUp = true))
Timber.v("[$this] Execute sync request with timeout $DEFAULT_LONG_POOL_TIMEOUT")
val latch = CountDownLatch(1)
val params = SyncTask.Params(DEFAULT_LONG_POOL_TIMEOUT)
cancelableTask = syncTask.configureWith(params)
@ -148,6 +152,7 @@ internal class SyncThread @Inject constructor(private val syncTask: SyncTask,
}

private fun updateStateTo(newState: SyncState) {
Timber.v("Update state to $newState")
state = newState
liveState.postValue(newState)
}

View File

@ -16,14 +16,20 @@

package im.vector.riotx.core.extensions

import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ProcessLifecycleOwner
import im.vector.matrix.android.api.session.Session
import im.vector.matrix.android.api.session.sync.FilterService
import im.vector.riotx.features.notifications.PushRuleTriggerListener
import timber.log.Timber

fun Session.configureAndStart(pushRuleTriggerListener: PushRuleTriggerListener) {
open()
setFilter(FilterService.FilterPreset.RiotFilter)
startSync()
Timber.i("Configure and start session for ${this.myUserId}")
val isAtLeastStarted = ProcessLifecycleOwner.get().lifecycle.currentState.isAtLeast(Lifecycle.State.STARTED)
Timber.v("--> is at least started? $isAtLeastStarted")
startSync(isAtLeastStarted)
refreshPushers()
pushRuleTriggerListener.startWithSession(this)
fetchPushRules()