mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2024-11-27 03:49:04 +03:00
Fix / SyncThread was started in background
Upon reception of a push, is the session is instantiated the sync thread was starting to loop
This commit is contained in:
parent
42584fc55a
commit
63d2861bc8
4 changed files with 18 additions and 5 deletions
|
@ -84,7 +84,7 @@ interface Session :
|
||||||
/**
|
/**
|
||||||
* This method start the sync thread.
|
* This method start the sync thread.
|
||||||
*/
|
*/
|
||||||
fun startSync()
|
fun startSync(fromForeground : Boolean)
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method stop the sync thread.
|
* This method stop the sync thread.
|
||||||
|
|
|
@ -105,8 +105,10 @@ internal class DefaultSession @Inject constructor(override val sessionParams: Se
|
||||||
SyncWorker.stopAnyBackgroundSync(context)
|
SyncWorker.stopAnyBackgroundSync(context)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun startSync() {
|
override fun startSync(fromForeground : Boolean) {
|
||||||
|
Timber.i("Starting sync thread")
|
||||||
assert(isOpen)
|
assert(isOpen)
|
||||||
|
syncThread.setInitialForeground(fromForeground)
|
||||||
if (!syncThread.isAlive) {
|
if (!syncThread.isAlive) {
|
||||||
syncThread.start()
|
syncThread.start()
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -54,6 +54,10 @@ internal class SyncThread @Inject constructor(private val syncTask: SyncTask,
|
||||||
updateStateTo(SyncState.IDLE)
|
updateStateTo(SyncState.IDLE)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun setInitialForeground(initialForground : Boolean) {
|
||||||
|
updateStateTo(if (initialForground) SyncState.IDLE else SyncState.PAUSED)
|
||||||
|
}
|
||||||
|
|
||||||
fun restart() = synchronized(lock) {
|
fun restart() = synchronized(lock) {
|
||||||
if (state is SyncState.PAUSED) {
|
if (state is SyncState.PAUSED) {
|
||||||
Timber.v("Resume sync...")
|
Timber.v("Resume sync...")
|
||||||
|
@ -84,7 +88,6 @@ internal class SyncThread @Inject constructor(private val syncTask: SyncTask,
|
||||||
Timber.v("Start syncing...")
|
Timber.v("Start syncing...")
|
||||||
networkConnectivityChecker.register(this)
|
networkConnectivityChecker.register(this)
|
||||||
backgroundDetectionObserver.register(this)
|
backgroundDetectionObserver.register(this)
|
||||||
updateStateTo(SyncState.RUNNING(catchingUp = true))
|
|
||||||
|
|
||||||
while (state != SyncState.KILLING) {
|
while (state != SyncState.KILLING) {
|
||||||
if (!networkConnectivityChecker.isConnected() || state == SyncState.PAUSED) {
|
if (!networkConnectivityChecker.isConnected() || state == SyncState.PAUSED) {
|
||||||
|
@ -93,7 +96,8 @@ internal class SyncThread @Inject constructor(private val syncTask: SyncTask,
|
||||||
lock.wait()
|
lock.wait()
|
||||||
}
|
}
|
||||||
} else {
|
} 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 latch = CountDownLatch(1)
|
||||||
val params = SyncTask.Params(DEFAULT_LONG_POOL_TIMEOUT)
|
val params = SyncTask.Params(DEFAULT_LONG_POOL_TIMEOUT)
|
||||||
cancelableTask = syncTask.configureWith(params)
|
cancelableTask = syncTask.configureWith(params)
|
||||||
|
@ -148,6 +152,7 @@ internal class SyncThread @Inject constructor(private val syncTask: SyncTask,
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun updateStateTo(newState: SyncState) {
|
private fun updateStateTo(newState: SyncState) {
|
||||||
|
Timber.v("Update state to $newState")
|
||||||
state = newState
|
state = newState
|
||||||
liveState.postValue(newState)
|
liveState.postValue(newState)
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,14 +16,20 @@
|
||||||
|
|
||||||
package im.vector.riotx.core.extensions
|
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.Session
|
||||||
import im.vector.matrix.android.api.session.sync.FilterService
|
import im.vector.matrix.android.api.session.sync.FilterService
|
||||||
import im.vector.riotx.features.notifications.PushRuleTriggerListener
|
import im.vector.riotx.features.notifications.PushRuleTriggerListener
|
||||||
|
import timber.log.Timber
|
||||||
|
|
||||||
fun Session.configureAndStart(pushRuleTriggerListener: PushRuleTriggerListener) {
|
fun Session.configureAndStart(pushRuleTriggerListener: PushRuleTriggerListener) {
|
||||||
open()
|
open()
|
||||||
setFilter(FilterService.FilterPreset.RiotFilter)
|
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()
|
refreshPushers()
|
||||||
pushRuleTriggerListener.startWithSession(this)
|
pushRuleTriggerListener.startWithSession(this)
|
||||||
fetchPushRules()
|
fetchPushRules()
|
||||||
|
|
Loading…
Reference in a new issue