diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/Session.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/Session.kt index 08e706c3e4..d06764c43c 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/Session.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/Session.kt @@ -84,7 +84,7 @@ interface Session : /** * This method start the sync thread. */ - fun startSync() + fun startSync(fromForeground : Boolean) /** * This method stop the sync thread. diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultSession.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultSession.kt index 6a1cda534a..69e359653e 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultSession.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/DefaultSession.kt @@ -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 { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/job/SyncThread.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/job/SyncThread.kt index b6d236edbf..65c79bd8a0 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/job/SyncThread.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/sync/job/SyncThread.kt @@ -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) } diff --git a/vector/src/main/java/im/vector/riotx/core/extensions/Session.kt b/vector/src/main/java/im/vector/riotx/core/extensions/Session.kt index b03686d488..95e17a4b7d 100644 --- a/vector/src/main/java/im/vector/riotx/core/extensions/Session.kt +++ b/vector/src/main/java/im/vector/riotx/core/extensions/Session.kt @@ -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()