Parse "io.element.disable_network_constraint" from login response, to ensure that the config is retrieved.

The add pusher worker can be configured before the .well-known config is retrieved.
This commit is contained in:
Benoit Marty 2023-10-12 18:12:49 +02:00
parent 973246819a
commit d4c6a46e90
13 changed files with 141 additions and 26 deletions

View File

@ -36,5 +36,11 @@ data class DiscoveryInformation(
* Note: matrix.org does not send this field
*/
@Json(name = "m.identity_server")
val identityServer: WellKnownBaseConfig? = null
val identityServer: WellKnownBaseConfig? = null,
/**
* If set to true, the SDK will not use the network constraint when configuring Worker for the WorkManager.
*/
@Json(name = "io.element.disable_network_constraint")
val disableNetworkConstraint: Boolean? = null,
)

View File

@ -29,9 +29,8 @@ import androidx.work.WorkRequest
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.MatrixCoroutineDispatchers
import org.matrix.android.sdk.api.extensions.orFalse
import org.matrix.android.sdk.internal.session.SessionScope
import org.matrix.android.sdk.internal.session.homeserver.HomeServerCapabilitiesDataSource
import org.matrix.android.sdk.internal.session.workmanager.WorkManagerConfig
import org.matrix.android.sdk.internal.worker.MatrixWorkerFactory
import timber.log.Timber
import java.util.concurrent.TimeUnit
@ -106,12 +105,12 @@ internal class WorkManagerProvider @Inject constructor(
private const val MATRIX_SDK_TAG_PREFIX = "MatrixSDK-"
fun getWorkConstraints(
homeServerCapabilitiesDataSource: HomeServerCapabilitiesDataSource,
workManagerConfig: WorkManagerConfig,
): Constraints {
val withNetworkConstraint = homeServerCapabilitiesDataSource.getHomeServerCapabilities()?.disableNetworkConstraint.orFalse().not()
val widthNetworkConstraint = workManagerConfig.withNetworkConstraint()
return Constraints.Builder()
.apply {
if (withNetworkConstraint) {
if (widthNetworkConstraint) {
setRequiredNetworkType(NetworkType.CONNECTED)
} else {
Timber.w("Network constraint is disabled")

View File

@ -97,6 +97,8 @@ import org.matrix.android.sdk.internal.session.room.tombstone.RoomTombstoneEvent
import org.matrix.android.sdk.internal.session.typing.DefaultTypingUsersTracker
import org.matrix.android.sdk.internal.session.user.accountdata.DefaultSessionAccountDataService
import org.matrix.android.sdk.internal.session.widgets.DefaultWidgetURLFormatter
import org.matrix.android.sdk.internal.session.workmanager.DefaultWorkManagerConfig
import org.matrix.android.sdk.internal.session.workmanager.WorkManagerConfig
import retrofit2.Retrofit
import java.io.File
import javax.inject.Provider
@ -422,4 +424,7 @@ internal abstract class SessionModule {
@Binds
abstract fun bindPollAggregationProcessor(processor: DefaultPollAggregationProcessor): PollAggregationProcessor
@Binds
abstract fun bindWorkManaerConfig(config: DefaultWorkManagerConfig): WorkManagerConfig
}

View File

@ -27,8 +27,8 @@ import org.matrix.android.sdk.internal.database.query.where
import org.matrix.android.sdk.internal.di.SessionDatabase
import org.matrix.android.sdk.internal.di.SessionId
import org.matrix.android.sdk.internal.di.WorkManagerProvider
import org.matrix.android.sdk.internal.session.homeserver.HomeServerCapabilitiesDataSource
import org.matrix.android.sdk.internal.session.pushers.gateway.PushGatewayNotifyTask
import org.matrix.android.sdk.internal.session.workmanager.WorkManagerConfig
import org.matrix.android.sdk.internal.task.TaskExecutor
import org.matrix.android.sdk.internal.task.configureWith
import org.matrix.android.sdk.internal.worker.WorkerParamsFactory
@ -46,7 +46,7 @@ internal class DefaultPushersService @Inject constructor(
private val togglePusherTask: TogglePusherTask,
private val removePusherTask: RemovePusherTask,
private val taskExecutor: TaskExecutor,
private val homeServerCapabilitiesDataSource: HomeServerCapabilitiesDataSource,
private val workManagerConfig: WorkManagerConfig,
) : PushersService {
override suspend fun testPush(
@ -132,7 +132,7 @@ internal class DefaultPushersService @Inject constructor(
private fun enqueueAddPusher(pusher: JsonPusher): UUID {
val params = AddPusherWorker.Params(sessionId, pusher)
val request = workManagerProvider.matrixOneTimeWorkRequestBuilder<AddPusherWorker>()
.setConstraints(WorkManagerProvider.getWorkConstraints(homeServerCapabilitiesDataSource))
.setConstraints(WorkManagerProvider.getWorkConstraints(workManagerConfig))
.setInputData(WorkerParamsFactory.toData(params))
.setBackoffCriteria(BackoffPolicy.LINEAR, WorkManagerProvider.BACKOFF_DELAY_MILLIS, TimeUnit.MILLISECONDS)
.build()

View File

@ -55,6 +55,7 @@ import org.matrix.android.sdk.internal.di.WorkManagerProvider
import org.matrix.android.sdk.internal.session.content.UploadContentWorker
import org.matrix.android.sdk.internal.session.homeserver.HomeServerCapabilitiesDataSource
import org.matrix.android.sdk.internal.session.room.send.queue.EventSenderProcessor
import org.matrix.android.sdk.internal.session.workmanager.WorkManagerConfig
import org.matrix.android.sdk.internal.task.TaskExecutor
import org.matrix.android.sdk.internal.util.CancelableWork
import org.matrix.android.sdk.internal.worker.WorkerParamsFactory
@ -75,7 +76,7 @@ internal class DefaultSendService @AssistedInject constructor(
private val localEchoRepository: LocalEchoRepository,
private val eventSenderProcessor: EventSenderProcessor,
private val cancelSendTracker: CancelSendTracker,
private val homeServerCapabilitiesDataSource: HomeServerCapabilitiesDataSource,
private val workManagerConfig: WorkManagerConfig,
) : SendService {
@AssistedFactory
@ -375,7 +376,7 @@ internal class DefaultSendService @AssistedInject constructor(
val uploadWorkData = WorkerParamsFactory.toData(uploadMediaWorkerParams)
return workManagerProvider.matrixOneTimeWorkRequestBuilder<UploadContentWorker>()
.setConstraints(WorkManagerProvider.getWorkConstraints(homeServerCapabilitiesDataSource))
.setConstraints(WorkManagerProvider.getWorkConstraints(workManagerConfig))
.startChain(true)
.setInputData(uploadWorkData)
.setBackoffCriteria(BackoffPolicy.LINEAR, WorkManagerProvider.BACKOFF_DELAY_MILLIS, TimeUnit.MILLISECONDS)

View File

@ -23,6 +23,7 @@ import androidx.work.OneTimeWorkRequest
import org.matrix.android.sdk.api.util.Cancelable
import org.matrix.android.sdk.internal.di.WorkManagerProvider
import org.matrix.android.sdk.internal.session.homeserver.HomeServerCapabilitiesDataSource
import org.matrix.android.sdk.internal.session.workmanager.WorkManagerConfig
import org.matrix.android.sdk.internal.util.CancelableWork
import org.matrix.android.sdk.internal.worker.startChain
import java.util.concurrent.TimeUnit
@ -36,7 +37,7 @@ import javax.inject.Inject
*/
internal class TimelineSendEventWorkCommon @Inject constructor(
private val workManagerProvider: WorkManagerProvider,
private val homeServerCapabilitiesDataSource: HomeServerCapabilitiesDataSource,
private val workManagerConfig: WorkManagerConfig,
) {
fun postWork(roomId: String, workRequest: OneTimeWorkRequest, policy: ExistingWorkPolicy = ExistingWorkPolicy.APPEND_OR_REPLACE): Cancelable {
@ -49,7 +50,7 @@ internal class TimelineSendEventWorkCommon @Inject constructor(
inline fun <reified W : ListenableWorker> createWork(data: Data, startChain: Boolean): OneTimeWorkRequest {
return workManagerProvider.matrixOneTimeWorkRequestBuilder<W>()
.setConstraints(WorkManagerProvider.getWorkConstraints(homeServerCapabilitiesDataSource))
.setConstraints(WorkManagerProvider.getWorkConstraints(workManagerConfig))
.startChain(startChain)
.setInputData(data)
.setBackoffCriteria(BackoffPolicy.LINEAR, WorkManagerProvider.BACKOFF_DELAY_MILLIS, TimeUnit.MILLISECONDS)

View File

@ -23,6 +23,7 @@ import org.matrix.android.sdk.internal.session.SessionState
import org.matrix.android.sdk.internal.session.homeserver.HomeServerCapabilitiesDataSource
import org.matrix.android.sdk.internal.session.sync.job.SyncThread
import org.matrix.android.sdk.internal.session.sync.job.SyncWorker
import org.matrix.android.sdk.internal.session.workmanager.WorkManagerConfig
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Provider
@ -34,7 +35,7 @@ internal class DefaultSyncService @Inject constructor(
private val syncTokenStore: SyncTokenStore,
private val syncRequestStateTracker: SyncRequestStateTracker,
private val sessionState: SessionState,
private val homeServerCapabilitiesDataSource: HomeServerCapabilitiesDataSource,
private val workManagerConfig: WorkManagerConfig,
) : SyncService {
private var syncThread: SyncThread? = null
@ -42,7 +43,7 @@ internal class DefaultSyncService @Inject constructor(
SyncWorker.requireBackgroundSync(
workManagerProvider = workManagerProvider,
sessionId = sessionId,
homeServerCapabilitiesDataSource = homeServerCapabilitiesDataSource,
workManagerConfig = workManagerConfig,
)
}
@ -50,7 +51,7 @@ internal class DefaultSyncService @Inject constructor(
SyncWorker.automaticallyBackgroundSync(
workManagerProvider = workManagerProvider,
sessionId = sessionId,
homeServerCapabilitiesDataSource = homeServerCapabilitiesDataSource,
workManagerConfig = workManagerConfig,
serverTimeoutInSeconds = timeOutInSeconds,
delayInSeconds = repeatDelayInSeconds,
)

View File

@ -28,6 +28,7 @@ import org.matrix.android.sdk.internal.session.SessionComponent
import org.matrix.android.sdk.internal.session.homeserver.HomeServerCapabilitiesDataSource
import org.matrix.android.sdk.internal.session.sync.SyncPresence
import org.matrix.android.sdk.internal.session.sync.SyncTask
import org.matrix.android.sdk.internal.session.workmanager.WorkManagerConfig
import org.matrix.android.sdk.internal.worker.SessionSafeCoroutineWorker
import org.matrix.android.sdk.internal.worker.SessionWorkerParams
import org.matrix.android.sdk.internal.worker.WorkerParamsFactory
@ -60,7 +61,7 @@ internal class SyncWorker(context: Context, workerParameters: WorkerParameters,
@Inject lateinit var syncTask: SyncTask
@Inject lateinit var workManagerProvider: WorkManagerProvider
@Inject lateinit var homeServerCapabilitiesDataSource: HomeServerCapabilitiesDataSource
@Inject lateinit var workManagerConfig: WorkManagerConfig
override fun injectWith(injector: SessionComponent) {
injector.inject(this)
@ -79,7 +80,7 @@ internal class SyncWorker(context: Context, workerParameters: WorkerParameters,
automaticallyBackgroundSync(
workManagerProvider = workManagerProvider,
sessionId = params.sessionId,
homeServerCapabilitiesDataSource = homeServerCapabilitiesDataSource,
workManagerConfig = workManagerConfig,
serverTimeoutInSeconds = params.timeout,
delayInSeconds = params.delay,
forceImmediate = hasToDeviceEvents
@ -89,7 +90,7 @@ internal class SyncWorker(context: Context, workerParameters: WorkerParameters,
requireBackgroundSync(
workManagerProvider = workManagerProvider,
sessionId = params.sessionId,
homeServerCapabilitiesDataSource = homeServerCapabilitiesDataSource,
workManagerConfig = workManagerConfig,
serverTimeoutInSeconds = 0
)
}
@ -127,7 +128,7 @@ internal class SyncWorker(context: Context, workerParameters: WorkerParameters,
fun requireBackgroundSync(
workManagerProvider: WorkManagerProvider,
sessionId: String,
homeServerCapabilitiesDataSource: HomeServerCapabilitiesDataSource,
workManagerConfig: WorkManagerConfig,
serverTimeoutInSeconds: Long = 0
) {
val data = WorkerParamsFactory.toData(
@ -139,7 +140,7 @@ internal class SyncWorker(context: Context, workerParameters: WorkerParameters,
)
)
val workRequest = workManagerProvider.matrixOneTimeWorkRequestBuilder<SyncWorker>()
.setConstraints(WorkManagerProvider.getWorkConstraints(homeServerCapabilitiesDataSource))
.setConstraints(WorkManagerProvider.getWorkConstraints(workManagerConfig))
.setBackoffCriteria(BackoffPolicy.LINEAR, WorkManagerProvider.BACKOFF_DELAY_MILLIS, TimeUnit.MILLISECONDS)
.setInputData(data)
.startChain(true)
@ -151,7 +152,7 @@ internal class SyncWorker(context: Context, workerParameters: WorkerParameters,
fun automaticallyBackgroundSync(
workManagerProvider: WorkManagerProvider,
sessionId: String,
homeServerCapabilitiesDataSource: HomeServerCapabilitiesDataSource,
workManagerConfig: WorkManagerConfig,
serverTimeoutInSeconds: Long = 0,
delayInSeconds: Long = 30,
forceImmediate: Boolean = false
@ -166,7 +167,7 @@ internal class SyncWorker(context: Context, workerParameters: WorkerParameters,
)
)
val workRequest = workManagerProvider.matrixOneTimeWorkRequestBuilder<SyncWorker>()
.setConstraints(WorkManagerProvider.getWorkConstraints(homeServerCapabilitiesDataSource))
.setConstraints(WorkManagerProvider.getWorkConstraints(workManagerConfig))
.setInputData(data)
.setBackoffCriteria(BackoffPolicy.LINEAR, WorkManagerProvider.BACKOFF_DELAY_MILLIS, TimeUnit.MILLISECONDS)
.setInitialDelay(if (forceImmediate) 0 else delayInSeconds, TimeUnit.SECONDS)

View File

@ -0,0 +1,35 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.matrix.android.sdk.internal.session.workmanager
import org.matrix.android.sdk.api.auth.data.Credentials
import org.matrix.android.sdk.api.extensions.orFalse
import org.matrix.android.sdk.internal.session.homeserver.HomeServerCapabilitiesDataSource
import javax.inject.Inject
internal class DefaultWorkManagerConfig @Inject constructor(
private val credentials: Credentials,
private val homeServerCapabilitiesDataSource: HomeServerCapabilitiesDataSource,
) : WorkManagerConfig {
override fun withNetworkConstraint(): Boolean {
return if (credentials.discoveryInformation?.disableNetworkConstraint == true) {
false
} else {
homeServerCapabilitiesDataSource.getHomeServerCapabilities()?.disableNetworkConstraint.orFalse().not()
}
}
}

View File

@ -0,0 +1,21 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.matrix.android.sdk.internal.session.workmanager
internal interface WorkManagerConfig {
fun withNetworkConstraint(): Boolean
}

View File

@ -21,11 +21,11 @@ import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.matrix.android.sdk.test.fakes.FakeAddPusherTask
import org.matrix.android.sdk.test.fakes.FakeGetPushersTask
import org.matrix.android.sdk.test.fakes.FakeHomeServerCapabilitiesDataSource
import org.matrix.android.sdk.test.fakes.FakeMonarchy
import org.matrix.android.sdk.test.fakes.FakeRemovePusherTask
import org.matrix.android.sdk.test.fakes.FakeTaskExecutor
import org.matrix.android.sdk.test.fakes.FakeTogglePusherTask
import org.matrix.android.sdk.test.fakes.FakeWorkManagerConfig
import org.matrix.android.sdk.test.fakes.FakeWorkManagerProvider
import org.matrix.android.sdk.test.fakes.internal.FakePushGatewayNotifyTask
import org.matrix.android.sdk.test.fixtures.PusherFixture
@ -42,7 +42,7 @@ class DefaultPushersServiceTest {
private val togglePusherTask = FakeTogglePusherTask()
private val removePusherTask = FakeRemovePusherTask()
private val taskExecutor = FakeTaskExecutor()
private val homeServerCapabilitiesDataSource = FakeHomeServerCapabilitiesDataSource()
private val fakeWorkManagerConfig = FakeWorkManagerConfig()
private val pushersService = DefaultPushersService(
workManagerProvider.instance,
@ -54,7 +54,7 @@ class DefaultPushersServiceTest {
togglePusherTask,
removePusherTask,
taskExecutor.instance,
homeServerCapabilitiesDataSource.instance,
fakeWorkManagerConfig,
)
@Test

View File

@ -0,0 +1,20 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.matrix.android.sdk.test.fakes
class FakeWork {
}

View File

@ -0,0 +1,25 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.matrix.android.sdk.test.fakes
import org.matrix.android.sdk.internal.session.workmanager.WorkManagerConfig
class FakeWorkManagerConfig : WorkManagerConfig {
override fun withNetworkConstraint(): Boolean {
return true
}
}