Skip to content

Commit

Permalink
Merge pull request #6029 from vector-im/feature/bma/sdk_sync_service
Browse files Browse the repository at this point in the history
Feature/bma/sdk sync service
  • Loading branch information
bmarty authored Jun 8, 2022
2 parents 6ad672e + ac61aee commit 3b2f9d9
Show file tree
Hide file tree
Showing 55 changed files with 457 additions and 378 deletions.
5 changes: 5 additions & 0 deletions changelog.d/6029.sdk
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Some methods from `Session` have been moved to a new `SyncService`, that you can retrieve from a `Session`.
- `SyncStatusService` method has been moved to the new `SyncService`
- `InitSyncStep` have been moved and renamed to `InitialSyncStep`
- `SyncStatusService.Status` has been renamed to `SyncRequestState`
- The existing `SyncService` has been renamed to `SyncAndroidService` because of name clash with the new SDK Service
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class FlowSession(private val session: Session) {
}

fun liveSyncState(): Flow<SyncState> {
return session.getSyncStateLive().asFlow()
return session.syncService().getSyncStateLive().asFlow()
}

fun livePushers(): Flow<List<Pusher>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,11 @@ class CommonTestHelper private constructor(context: Context) {
fun syncSession(session: Session, timeout: Long = TestConstants.timeOutMillis * 10) {
val lock = CountDownLatch(1)
coroutineScope.launch {
session.startSync(true)
val syncLiveData = session.getSyncStateLive()
session.syncService().startSync(true)
val syncLiveData = session.syncService().getSyncStateLive()
val syncObserver = object : Observer<SyncState> {
override fun onChanged(t: SyncState?) {
if (session.hasAlreadySynced()) {
if (session.syncService().hasAlreadySynced()) {
lock.countDown()
syncLiveData.removeObserver(this)
}
Expand All @@ -160,18 +160,18 @@ class CommonTestHelper private constructor(context: Context) {
fun clearCacheAndSync(session: Session, timeout: Long = TestConstants.timeOutMillis) {
waitWithLatch(timeout) { latch ->
session.clearCache()
val syncLiveData = session.getSyncStateLive()
val syncLiveData = session.syncService().getSyncStateLive()
val syncObserver = object : Observer<SyncState> {
override fun onChanged(t: SyncState?) {
if (session.hasAlreadySynced()) {
if (session.syncService().hasAlreadySynced()) {
Timber.v("Clear cache and synced")
syncLiveData.removeObserver(this)
latch.countDown()
}
}
}
syncLiveData.observeForever(syncObserver)
session.startSync(true)
session.syncService().startSync(true)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ class KeyShareTests : InstrumentedTest {

// /!\ Stop initial alice session syncing so that it can't reply
aliceSession.cryptoService().enableKeyGossiping(false)
aliceSession.stopSync()
aliceSession.syncService().stopSync()

// Let's now try to request
aliceNewSession.cryptoService().reRequestRoomKeyForEvent(sentEvents.first().root)
Expand All @@ -447,7 +447,7 @@ class KeyShareTests : InstrumentedTest {

// let's wake up alice
aliceSession.cryptoService().enableKeyGossiping(true)
aliceSession.startSync(true)
aliceSession.syncService().startSync(true)

// We should now get a reply from first session
commonTestHelper.waitWithLatch { latch ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class ThreadMessagingTest : InstrumentedTest {
val timeline = aliceRoom.timelineService().createTimeline(null, TimelineSettings(30))
timeline.start()

aliceSession.startSync(true)
aliceSession.syncService().startSync(true)
run {
val lock = CountDownLatch(1)
val eventsListener = commonTestHelper.createEventListener(lock) { snapshot ->
Expand All @@ -97,7 +97,7 @@ class ThreadMessagingTest : InstrumentedTest {
timeline.addListener(eventsListener)
commonTestHelper.await(lock, 600_000)
}
aliceSession.stopSync()
aliceSession.syncService().stopSync()
}

@Test
Expand Down Expand Up @@ -144,7 +144,7 @@ class ThreadMessagingTest : InstrumentedTest {
val timeline = aliceRoom.timelineService().createTimeline(null, TimelineSettings(30))
timeline.start()

aliceSession.startSync(true)
aliceSession.syncService().startSync(true)
run {
val lock = CountDownLatch(1)
val eventsListener = commonTestHelper.createEventListener(lock) { snapshot ->
Expand All @@ -156,9 +156,9 @@ class ThreadMessagingTest : InstrumentedTest {
timeline.addListener(eventsListener)
commonTestHelper.await(lock, 600_000)
}
aliceSession.stopSync()
aliceSession.syncService().stopSync()

bobSession.startSync(true)
bobSession.syncService().startSync(true)
run {
val lock = CountDownLatch(1)
val eventsListener = commonTestHelper.createEventListener(lock) { snapshot ->
Expand All @@ -170,7 +170,7 @@ class ThreadMessagingTest : InstrumentedTest {
timeline.addListener(eventsListener)
commonTestHelper.await(lock, 600_000)
}
bobSession.stopSync()
bobSession.syncService().stopSync()
}

@Test
Expand Down Expand Up @@ -217,7 +217,7 @@ class ThreadMessagingTest : InstrumentedTest {
val timeline = aliceRoom.timelineService().createTimeline(null, TimelineSettings(30))
timeline.start()

aliceSession.startSync(true)
aliceSession.syncService().startSync(true)
run {
val lock = CountDownLatch(1)
val eventsListener = commonTestHelper.createEventListener(lock) { snapshot ->
Expand All @@ -233,7 +233,7 @@ class ThreadMessagingTest : InstrumentedTest {
timeline.addListener(eventsListener)
commonTestHelper.await(lock, 600_000)
}
aliceSession.stopSync()
aliceSession.syncService().stopSync()
}

@Test
Expand Down Expand Up @@ -314,7 +314,7 @@ class ThreadMessagingTest : InstrumentedTest {
val timeline = aliceRoom.timelineService().createTimeline(null, TimelineSettings(30))
timeline.start()

aliceSession.startSync(true)
aliceSession.syncService().startSync(true)
run {
val lock = CountDownLatch(1)
val eventsListener = commonTestHelper.createEventListener(lock) { snapshot ->
Expand All @@ -338,6 +338,6 @@ class ThreadMessagingTest : InstrumentedTest {
timeline.addListener(eventsListener)
commonTestHelper.await(lock, 600_000)
}
aliceSession.stopSync()
aliceSession.syncService().stopSync()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class PollAggregationTest : InstrumentedTest {
// Bob creates a poll
roomFromBobPOV.sendService().sendPoll(PollType.DISCLOSED, pollQuestion, pollOptions)

aliceSession.startSync(true)
aliceSession.syncService().startSync(true)
val aliceTimeline = roomFromAlicePOV.timelineService().createTimeline(null, TimelineSettings(30))
aliceTimeline.start()

Expand Down Expand Up @@ -133,7 +133,7 @@ class PollAggregationTest : InstrumentedTest {

aliceTimeline.removeAllListeners()

aliceSession.stopSync()
aliceSession.syncService().stopSync()
aliceTimeline.dispose()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class TimelinePreviousLastForwardTest : InstrumentedTest {
}

// Bob stop to sync
bobSession.stopSync()
bobSession.syncService().stopSync()

val firstMessage = "First messages from Alice"
// Alice sends 30 messages
Expand All @@ -101,7 +101,7 @@ class TimelinePreviousLastForwardTest : InstrumentedTest {
.eventId

// Bob start to sync
bobSession.startSync(true)
bobSession.syncService().startSync(true)

run {
val lock = CountDownLatch(1)
Expand All @@ -125,7 +125,7 @@ class TimelinePreviousLastForwardTest : InstrumentedTest {
}

// Bob stop to sync
bobSession.stopSync()
bobSession.syncService().stopSync()

val secondMessage = "Second messages from Alice"
// Alice sends again 30 messages
Expand All @@ -136,7 +136,7 @@ class TimelinePreviousLastForwardTest : InstrumentedTest {
)

// Bob start to sync
bobSession.startSync(true)
bobSession.syncService().startSync(true)

run {
val lock = CountDownLatch(1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class TimelineWithManyMembersTest : InstrumentedTest {
val timelineForCurrentMember = roomForCurrentMember.timelineService().createTimeline(null, TimelineSettings(30))
timelineForCurrentMember.start()

session.startSync(true)
session.syncService().startSync(true)

run {
val lock = CountDownLatch(1)
Expand All @@ -92,7 +92,7 @@ class TimelineWithManyMembersTest : InstrumentedTest {
timelineForCurrentMember.addListener(eventsListener)
commonTestHelper.await(lock, 600_000)
}
session.stopSync()
session.syncService().stopSync()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
package org.matrix.android.sdk.api.session

import androidx.annotation.MainThread
import androidx.lifecycle.LiveData
import kotlinx.coroutines.flow.SharedFlow
import okhttp3.OkHttpClient
import org.matrix.android.sdk.api.MatrixCoroutineDispatchers
import org.matrix.android.sdk.api.auth.data.SessionParams
Expand All @@ -37,7 +35,6 @@ import org.matrix.android.sdk.api.session.file.FileService
import org.matrix.android.sdk.api.session.group.GroupService
import org.matrix.android.sdk.api.session.homeserver.HomeServerCapabilitiesService
import org.matrix.android.sdk.api.session.identity.IdentityService
import org.matrix.android.sdk.api.session.initsync.SyncStatusService
import org.matrix.android.sdk.api.session.integrationmanager.IntegrationManagerService
import org.matrix.android.sdk.api.session.media.MediaService
import org.matrix.android.sdk.api.session.openid.OpenIdService
Expand All @@ -55,8 +52,7 @@ import org.matrix.android.sdk.api.session.signout.SignOutService
import org.matrix.android.sdk.api.session.space.SpaceService
import org.matrix.android.sdk.api.session.statistics.StatisticsListener
import org.matrix.android.sdk.api.session.sync.FilterService
import org.matrix.android.sdk.api.session.sync.SyncState
import org.matrix.android.sdk.api.session.sync.model.SyncResponse
import org.matrix.android.sdk.api.session.sync.SyncService
import org.matrix.android.sdk.api.session.terms.TermsService
import org.matrix.android.sdk.api.session.thirdparty.ThirdPartyService
import org.matrix.android.sdk.api.session.typing.TypingUsersTracker
Expand Down Expand Up @@ -98,59 +94,11 @@ interface Session {
@MainThread
fun open()

/**
* Requires a one time background sync.
*/
fun requireBackgroundSync()

/**
* Launches infinite self rescheduling background syncs via the WorkManager.
*
* While dozing, syncs will only occur during maintenance windows.
* For reliability it's recommended to also start a long running foreground service
* along with disabling battery optimizations.
*/
fun startAutomaticBackgroundSync(timeOutInSeconds: Long, repeatDelayInSeconds: Long)

fun stopAnyBackgroundSync()

/**
* This method start the sync thread.
*/
fun startSync(fromForeground: Boolean)

/**
* This method stop the sync thread.
*/
fun stopSync()

/**
* Clear cache of the session.
*/
suspend fun clearCache()

/**
* This method allows to listen the sync state.
* @return a [LiveData] of [SyncState].
*/
fun getSyncStateLive(): LiveData<SyncState>

/**
* This method returns the current sync state.
* @return the current [SyncState].
*/
fun getSyncState(): SyncState

/**
* This method returns a flow of SyncResponse. New value will be pushed through the sync thread.
*/
fun syncFlow(): SharedFlow<SyncResponse>

/**
* This methods return true if an initial sync has been processed.
*/
fun hasAlreadySynced(): Boolean

/**
* This method allow to close a session. It does stop some services.
*/
Expand Down Expand Up @@ -247,9 +195,9 @@ interface Session {
fun termsService(): TermsService

/**
* Returns the SyncStatusService associated with the session.
* Returns the SyncService associated with the session.
*/
fun syncStatusService(): SyncStatusService
fun syncService(): SyncService

/**
* Returns the SecureStorageService associated with the session.
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
* limitations under the License.
*/

package org.matrix.android.sdk.api.session.initsync
package org.matrix.android.sdk.api.session.sync

enum class InitSyncStep {
enum class InitialSyncStep {
ServerComputing,
Downloading,
ImportingAccount,
Expand Down
Loading

0 comments on commit 3b2f9d9

Please sign in to comment.