From e4124e93b8c1ec08b02be1ff7f558444bb530cab Mon Sep 17 00:00:00 2001 From: Jorge Martin Espinosa Date: Thu, 31 Aug 2023 13:37:20 +0200 Subject: [PATCH] Re-enable `SyncService.withEncryptionSync` to improve decryption of notifications (#1199) * Re-enable `SyncService.withEncryptionSync` to improve decryption of notifications. * Add feature flag --- changelog.d/1198.bugfix | 1 + .../impl/developer/DeveloperSettingsPresenter.kt | 10 ++++++++-- .../android/libraries/featureflag/api/FeatureFlags.kt | 6 ++++++ .../featureflag/impl/BuildtimeFeatureFlagProvider.kt | 1 + libraries/matrix/impl/build.gradle.kts | 1 + .../libraries/matrix/impl/RustMatrixClientFactory.kt | 10 +++++++++- samples/minimal/build.gradle.kts | 1 + .../io/element/android/samples/minimal/MainActivity.kt | 4 +++- 8 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 changelog.d/1198.bugfix diff --git a/changelog.d/1198.bugfix b/changelog.d/1198.bugfix new file mode 100644 index 0000000000..6ef69c4eff --- /dev/null +++ b/changelog.d/1198.bugfix @@ -0,0 +1 @@ +Re-enable `SyncService.withEncryptionSync` to improve decryption of notifications. diff --git a/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/developer/DeveloperSettingsPresenter.kt b/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/developer/DeveloperSettingsPresenter.kt index 010e17bd35..5f50fde309 100644 --- a/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/developer/DeveloperSettingsPresenter.kt +++ b/features/preferences/impl/src/main/kotlin/io/element/android/features/preferences/impl/developer/DeveloperSettingsPresenter.kt @@ -83,7 +83,8 @@ class DeveloperSettingsPresenter @Inject constructor( features, enabledFeatures, event.feature, - event.isEnabled + event.isEnabled, + triggerClearCache = { handleEvents(DeveloperSettingsEvents.ClearCache) } ) DeveloperSettingsEvents.ClearCache -> coroutineScope.clearCache(clearCacheAction) } @@ -122,12 +123,17 @@ class DeveloperSettingsPresenter @Inject constructor( features: SnapshotStateMap, enabledFeatures: SnapshotStateMap, featureUiModel: FeatureUiModel, - enabled: Boolean + enabled: Boolean, + triggerClearCache: () -> Unit, ) = launch { val feature = features[featureUiModel.key] ?: return@launch if (featureFlagService.setFeatureEnabled(feature, enabled)) { enabledFeatures[featureUiModel.key] = enabled } + + if (featureUiModel.key == FeatureFlags.UseEncryptionSync.key) { + triggerClearCache() + } } private fun CoroutineScope.computeCacheSize(cacheSize: MutableState>) = launch { diff --git a/libraries/featureflag/api/src/main/kotlin/io/element/android/libraries/featureflag/api/FeatureFlags.kt b/libraries/featureflag/api/src/main/kotlin/io/element/android/libraries/featureflag/api/FeatureFlags.kt index e1f4b740b0..08ac0ae039 100644 --- a/libraries/featureflag/api/src/main/kotlin/io/element/android/libraries/featureflag/api/FeatureFlags.kt +++ b/libraries/featureflag/api/src/main/kotlin/io/element/android/libraries/featureflag/api/FeatureFlags.kt @@ -31,5 +31,11 @@ enum class FeatureFlags( title = "Polls", description = "Create poll and render poll events in the timeline", defaultValue = false, + ), + UseEncryptionSync( + key = "feature.useencryptionsync", + title = "Use encryption sync", + description = "Use the encryption sync API for decrypting notifications.", + defaultValue = true, ) } diff --git a/libraries/featureflag/impl/src/main/kotlin/io/element/android/libraries/featureflag/impl/BuildtimeFeatureFlagProvider.kt b/libraries/featureflag/impl/src/main/kotlin/io/element/android/libraries/featureflag/impl/BuildtimeFeatureFlagProvider.kt index 83913cbac5..5640d108a6 100644 --- a/libraries/featureflag/impl/src/main/kotlin/io/element/android/libraries/featureflag/impl/BuildtimeFeatureFlagProvider.kt +++ b/libraries/featureflag/impl/src/main/kotlin/io/element/android/libraries/featureflag/impl/BuildtimeFeatureFlagProvider.kt @@ -31,6 +31,7 @@ class BuildtimeFeatureFlagProvider @Inject constructor() : when (feature) { FeatureFlags.LocationSharing -> true FeatureFlags.Polls -> false + FeatureFlags.UseEncryptionSync -> true } } else { false diff --git a/libraries/matrix/impl/build.gradle.kts b/libraries/matrix/impl/build.gradle.kts index b2a21f26df..a2b616f989 100644 --- a/libraries/matrix/impl/build.gradle.kts +++ b/libraries/matrix/impl/build.gradle.kts @@ -35,6 +35,7 @@ dependencies { implementation(projects.libraries.androidutils) implementation(projects.libraries.network) implementation(projects.services.toolbox.api) + implementation(projects.libraries.featureflag.api) api(projects.libraries.matrix.api) implementation(libs.dagger) implementation(projects.libraries.core) diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClientFactory.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClientFactory.kt index bb80032230..3615115bb4 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClientFactory.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClientFactory.kt @@ -19,6 +19,8 @@ package io.element.android.libraries.matrix.impl import android.content.Context import io.element.android.libraries.core.coroutine.CoroutineDispatchers import io.element.android.libraries.di.ApplicationContext +import io.element.android.libraries.featureflag.api.FeatureFlagService +import io.element.android.libraries.featureflag.api.FeatureFlags import io.element.android.libraries.network.useragent.UserAgentProvider import io.element.android.libraries.sessionstorage.api.SessionData import io.element.android.libraries.sessionstorage.api.SessionStore @@ -39,6 +41,7 @@ class RustMatrixClientFactory @Inject constructor( private val sessionStore: SessionStore, private val userAgentProvider: UserAgentProvider, private val clock: SystemClock, + private val featureFlagsService: FeatureFlagService, ) { suspend fun create(sessionData: SessionData): RustMatrixClient = withContext(coroutineDispatchers.io) { @@ -53,7 +56,12 @@ class RustMatrixClientFactory @Inject constructor( client.restoreSession(sessionData.toSession()) - val syncService = client.syncService().finish() + val syncService = client.syncService().apply { + if (featureFlagsService.isFeatureEnabled(FeatureFlags.UseEncryptionSync)) { + withEncryptionSync(withCrossProcessLock = false, appIdentifier = null) + } + } + .finish() RustMatrixClient( client = client, diff --git a/samples/minimal/build.gradle.kts b/samples/minimal/build.gradle.kts index 8063ac9b0a..1473bd7f93 100644 --- a/samples/minimal/build.gradle.kts +++ b/samples/minimal/build.gradle.kts @@ -63,6 +63,7 @@ dependencies { implementation(projects.features.login.impl) implementation(projects.features.networkmonitor.impl) implementation(projects.services.toolbox.impl) + implementation(projects.libraries.featureflag.impl) implementation(libs.coroutines.core) coreLibraryDesugaring(libs.android.desugar) } diff --git a/samples/minimal/src/main/kotlin/io/element/android/samples/minimal/MainActivity.kt b/samples/minimal/src/main/kotlin/io/element/android/samples/minimal/MainActivity.kt index a915e70046..94cd28f021 100644 --- a/samples/minimal/src/main/kotlin/io/element/android/samples/minimal/MainActivity.kt +++ b/samples/minimal/src/main/kotlin/io/element/android/samples/minimal/MainActivity.kt @@ -26,6 +26,7 @@ import androidx.compose.runtime.getValue import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalContext import androidx.core.view.WindowCompat +import io.element.android.libraries.featureflag.impl.DefaultFeatureFlagService import io.element.android.libraries.matrix.api.auth.MatrixAuthenticationService import io.element.android.libraries.matrix.impl.RustMatrixClientFactory import io.element.android.libraries.matrix.impl.auth.RustMatrixAuthenticationService @@ -54,7 +55,8 @@ class MainActivity : ComponentActivity() { coroutineDispatchers = Singleton.coroutineDispatchers, sessionStore = sessionStore, userAgentProvider = userAgentProvider, - clock = DefaultSystemClock() + clock = DefaultSystemClock(), + featureFlagsService = DefaultFeatureFlagService(emptySet()) ) ) }