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 54ae24c7..2dde175b 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 @@ -87,7 +87,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 a84e0193..09baebb2 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 b6d236ed..65c79bd8 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 b03686d4..95e17a4b 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()