diff --git a/app/src/main/kotlin/com/bitwarden/authenticator/data/platform/datasource/disk/FeatureFlagDiskSource.kt b/app/src/main/kotlin/com/bitwarden/authenticator/data/platform/datasource/disk/FeatureFlagDiskSource.kt new file mode 100644 index 0000000000..93b9e6273c --- /dev/null +++ b/app/src/main/kotlin/com/bitwarden/authenticator/data/platform/datasource/disk/FeatureFlagDiskSource.kt @@ -0,0 +1,20 @@ +package com.bitwarden.authenticator.data.platform.datasource.disk + +import com.bitwarden.authenticator.data.platform.datasource.disk.model.FeatureFlagsConfiguration +import kotlinx.coroutines.flow.Flow + +/** + * Primary access point for feature flag configuration. + */ +interface FeatureFlagDiskSource { + + /** + * The currently persisted [FeatureFlagsConfiguration]. + */ + var featureFlagsConfiguration: FeatureFlagsConfiguration? + + /** + * Emits updates to track [FeatureFlagsConfiguration]. This will replay the last known value. + */ + val featureFlagsConfigurationFlow: Flow +} diff --git a/app/src/main/kotlin/com/bitwarden/authenticator/data/platform/datasource/disk/FeatureFlagDiskSourceImpl.kt b/app/src/main/kotlin/com/bitwarden/authenticator/data/platform/datasource/disk/FeatureFlagDiskSourceImpl.kt new file mode 100644 index 0000000000..97203518a1 --- /dev/null +++ b/app/src/main/kotlin/com/bitwarden/authenticator/data/platform/datasource/disk/FeatureFlagDiskSourceImpl.kt @@ -0,0 +1,42 @@ +package com.bitwarden.authenticator.data.platform.datasource.disk + +import android.content.SharedPreferences +import com.bitwarden.authenticator.data.platform.datasource.disk.BaseDiskSource.Companion.BASE_KEY +import com.bitwarden.authenticator.data.platform.datasource.disk.model.FeatureFlagsConfiguration +import com.bitwarden.authenticator.data.platform.repository.util.bufferedMutableSharedFlow +import com.bitwarden.authenticator.data.platform.util.decodeFromStringOrNull +import kotlinx.coroutines.flow.Flow +import kotlinx.coroutines.flow.onSubscription +import kotlinx.serialization.encodeToString +import kotlinx.serialization.json.Json + +private const val KEY_FEATURE_FLAGS = "$BASE_KEY:featureFlags" + +/** + * Primary implementation of [FeatureFlagDiskSource]. + */ +class FeatureFlagDiskSourceImpl( + sharedPreferences: SharedPreferences, + private val json: Json, +) : BaseDiskSource(sharedPreferences = sharedPreferences), + FeatureFlagDiskSource { + + private val mutableFeatureFlagsConfigurationFlow = + bufferedMutableSharedFlow(replay = 1) + + override val featureFlagsConfigurationFlow: Flow + get() = mutableFeatureFlagsConfigurationFlow.onSubscription { + emit(featureFlagsConfiguration) + } + + override var featureFlagsConfiguration: FeatureFlagsConfiguration? + get() = getString(key = KEY_FEATURE_FLAGS) + ?.let { json.decodeFromStringOrNull(it) } + set(value) { + putString( + key = KEY_FEATURE_FLAGS, + value = value.let { json.encodeToString(it) }, + ) + mutableFeatureFlagsConfigurationFlow.tryEmit(value) + } +} diff --git a/app/src/main/kotlin/com/bitwarden/authenticator/data/platform/datasource/disk/di/PlatformDiskModule.kt b/app/src/main/kotlin/com/bitwarden/authenticator/data/platform/datasource/disk/di/PlatformDiskModule.kt index 76d0fd3af6..a42e4be54f 100644 --- a/app/src/main/kotlin/com/bitwarden/authenticator/data/platform/datasource/disk/di/PlatformDiskModule.kt +++ b/app/src/main/kotlin/com/bitwarden/authenticator/data/platform/datasource/disk/di/PlatformDiskModule.kt @@ -2,12 +2,15 @@ package com.bitwarden.authenticator.data.platform.datasource.disk.di import android.content.SharedPreferences import com.bitwarden.authenticator.data.platform.datasource.di.UnencryptedPreferences +import com.bitwarden.authenticator.data.platform.datasource.disk.FeatureFlagDiskSource +import com.bitwarden.authenticator.data.platform.datasource.disk.FeatureFlagDiskSourceImpl import com.bitwarden.authenticator.data.platform.datasource.disk.SettingsDiskSource import com.bitwarden.authenticator.data.platform.datasource.disk.SettingsDiskSourceImpl import dagger.Module import dagger.Provides import dagger.hilt.InstallIn import dagger.hilt.components.SingletonComponent +import kotlinx.serialization.json.Json import javax.inject.Singleton @Module @@ -20,4 +23,15 @@ object PlatformDiskModule { @UnencryptedPreferences sharedPreferences: SharedPreferences, ): SettingsDiskSource = SettingsDiskSourceImpl(sharedPreferences = sharedPreferences) + + @Provides + @Singleton + fun provideFeatureFlagDiskSource( + @UnencryptedPreferences sharedPreferences: SharedPreferences, + json: Json, + ): FeatureFlagDiskSource = + FeatureFlagDiskSourceImpl( + sharedPreferences = sharedPreferences, + json = json, + ) } diff --git a/app/src/main/kotlin/com/bitwarden/authenticator/data/platform/datasource/disk/model/FeatureFlagsConfiguration.kt b/app/src/main/kotlin/com/bitwarden/authenticator/data/platform/datasource/disk/model/FeatureFlagsConfiguration.kt new file mode 100644 index 0000000000..e4f87de408 --- /dev/null +++ b/app/src/main/kotlin/com/bitwarden/authenticator/data/platform/datasource/disk/model/FeatureFlagsConfiguration.kt @@ -0,0 +1,14 @@ +package com.bitwarden.authenticator.data.platform.datasource.disk.model + +import kotlinx.serialization.SerialName +import kotlinx.serialization.Serializable +import kotlinx.serialization.json.JsonPrimitive + +/** + * Models the state of feature flags. + */ +@Serializable +data class FeatureFlagsConfiguration( + @SerialName("featureFlags") + val featureFlags: Map, +) diff --git a/app/src/main/kotlin/com/bitwarden/authenticator/data/platform/manager/model/FeatureFlag.kt b/app/src/main/kotlin/com/bitwarden/authenticator/data/platform/manager/model/FeatureFlag.kt new file mode 100644 index 0000000000..0f2a259316 --- /dev/null +++ b/app/src/main/kotlin/com/bitwarden/authenticator/data/platform/manager/model/FeatureFlag.kt @@ -0,0 +1,40 @@ +package com.bitwarden.authenticator.data.platform.manager.model + +/** + * Represents feature flags used by the application. + * + * @property name The string value of the given key. + * @property defaultValue The value to be used if the flags value cannot be determined or is not remotely configured. + * @property isRemotelyConfigured Indicates if the flag should respect the network value or not. + */ +sealed class FeatureFlag( + val name: String, + val defaultValue: T, + val isRemotelyConfigured: Boolean, +) + +/** + * Models feature flags that are managed locally. E.g., [isRemotelyConfigured] is `false`. + */ +sealed class LocalFeatureFlag( + name: String, + defaultValue: T, +) : FeatureFlag(name, defaultValue, isRemotelyConfigured = false) { + + /** + * Indicates the state of Bitwarden authentication. + */ + data object BitwardenAuthenticationEnabled : LocalFeatureFlag( + name = "bitwarden-authentication-enabled", + defaultValue = false, + ) +} + +/** + * Models feature flags that are managed remotely. E.g., [isRemotelyConfigured] is `true`. + */ +sealed class RemoteFeatureFlag( + name: String, + defaultValue: T, +) : FeatureFlag(name, defaultValue, isRemotelyConfigured = true) + diff --git a/app/src/test/java/com/bitwarden/authenticator/data/platform/base/FakeSharedPreferences.kt b/app/src/test/java/com/bitwarden/authenticator/data/platform/base/FakeSharedPreferences.kt new file mode 100644 index 0000000000..b1b67882d7 --- /dev/null +++ b/app/src/test/java/com/bitwarden/authenticator/data/platform/base/FakeSharedPreferences.kt @@ -0,0 +1,109 @@ +package com.bitwarden.authenticator.data.platform.base + +import android.content.SharedPreferences + +/** + * A faked implementation of [SharedPreferences] that is backed by an internal, memory-based map. + */ +class FakeSharedPreferences : SharedPreferences { + private val sharedPreferences: MutableMap = mutableMapOf() + private val listeners = mutableSetOf() + + override fun contains(key: String): Boolean = + sharedPreferences.containsKey(key) + + override fun edit(): SharedPreferences.Editor = Editor() + + override fun getAll(): Map = sharedPreferences + + override fun getBoolean(key: String, defaultValue: Boolean): Boolean = + getValue(key, defaultValue) + + override fun getFloat(key: String, defaultValue: Float): Float = + getValue(key, defaultValue) + + override fun getInt(key: String, defaultValue: Int): Int = + getValue(key, defaultValue) + + override fun getLong(key: String, defaultValue: Long): Long = + getValue(key, defaultValue) + + override fun getString(key: String, defaultValue: String?): String? = + getValue(key, defaultValue) + + override fun getStringSet(key: String, defaultValue: Set?): Set? = + getValue(key, defaultValue) + + override fun registerOnSharedPreferenceChangeListener( + listener: SharedPreferences.OnSharedPreferenceChangeListener, + ) { + listeners += listener + } + + override fun unregisterOnSharedPreferenceChangeListener( + listener: SharedPreferences.OnSharedPreferenceChangeListener, + ) { + listeners -= listener + } + + private inline fun getValue( + key: String, + defaultValue: T, + ): T = sharedPreferences[key] as? T ?: defaultValue + + inner class Editor : SharedPreferences.Editor { + private val pendingSharedPreferences = sharedPreferences.toMutableMap() + + override fun apply() { + sharedPreferences.apply { + clear() + putAll(pendingSharedPreferences) + + // Notify listeners + listeners.forEach { listener -> + pendingSharedPreferences.keys.forEach { key -> + listener.onSharedPreferenceChanged(this@FakeSharedPreferences, key) + } + } + } + } + + override fun clear(): SharedPreferences.Editor = + apply { pendingSharedPreferences.clear() } + + override fun commit(): Boolean { + apply() + return true + } + + override fun putBoolean(key: String, value: Boolean): SharedPreferences.Editor = + putValue(key, value) + + override fun putFloat(key: String, value: Float): SharedPreferences.Editor = + putValue(key, value) + + override fun putInt(key: String, value: Int): SharedPreferences.Editor = + putValue(key, value) + + override fun putLong(key: String, value: Long): SharedPreferences.Editor = + putValue(key, value) + + override fun putString(key: String, value: String?): SharedPreferences.Editor = + putValue(key, value) + + override fun putStringSet(key: String, value: Set?): SharedPreferences.Editor = + putValue(key, value) + + override fun remove(key: String): SharedPreferences.Editor = + apply { pendingSharedPreferences.remove(key) } + + private inline fun putValue( + key: String, + value: T, + ): SharedPreferences.Editor = apply { + value + ?.let { pendingSharedPreferences[key] = it } + ?: pendingSharedPreferences.remove(key) + } + } +} diff --git a/app/src/test/java/com/bitwarden/authenticator/data/platform/datasource/disk/FeatureFlagDiskSourceTest.kt b/app/src/test/java/com/bitwarden/authenticator/data/platform/datasource/disk/FeatureFlagDiskSourceTest.kt new file mode 100644 index 0000000000..1911b455f5 --- /dev/null +++ b/app/src/test/java/com/bitwarden/authenticator/data/platform/datasource/disk/FeatureFlagDiskSourceTest.kt @@ -0,0 +1,72 @@ +package com.bitwarden.authenticator.data.platform.datasource.disk + +import androidx.core.content.edit +import app.cash.turbine.test +import com.bitwarden.authenticator.data.platform.base.FakeSharedPreferences +import com.bitwarden.authenticator.data.platform.datasource.disk.model.FeatureFlagsConfiguration +import com.bitwarden.authenticator.data.platform.datasource.network.di.PlatformNetworkModule +import com.bitwarden.authenticator.data.platform.manager.model.LocalFeatureFlag +import kotlinx.coroutines.test.runTest +import kotlinx.serialization.json.JsonPrimitive +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertNull +import org.junit.jupiter.api.Test + +class FeatureFlagDiskSourceTest { + private val json = PlatformNetworkModule.providesJson() + + private val fakeSharedPreferences = FakeSharedPreferences() + + private val featureFlagDiskSource = FeatureFlagDiskSourceImpl( + sharedPreferences = fakeSharedPreferences, + json = json, + ) + + @Test + fun `featureFlagsConfiguration should pull from and update SharedPreferences`() { + val featureFlagsConfigKey = "bwPreferencesStorage:featureFlags" + + assertNull(featureFlagDiskSource.featureFlagsConfiguration) + assertNull(fakeSharedPreferences.getString(featureFlagsConfigKey, null)) + + featureFlagDiskSource.featureFlagsConfiguration = FEATURE_FLAGS_CONFIGURATION + assertEquals( + json.parseToJsonElement( + FEATURE_FLAGS_CONFIGURATION_JSON + ), + json.parseToJsonElement( + fakeSharedPreferences.getString(featureFlagsConfigKey, null)!! + ), + ) + + fakeSharedPreferences.edit { putString(featureFlagsConfigKey, null) } + assertNull(featureFlagDiskSource.featureFlagsConfiguration) + } + + @Test + fun `featureFlagsConfigFlow should react to changes in featureFlagsConfig`() = runTest { + featureFlagDiskSource.featureFlagsConfigurationFlow.test { + assertNull(featureFlagDiskSource.featureFlagsConfiguration) + assertNull(awaitItem()) + + featureFlagDiskSource.featureFlagsConfiguration = FEATURE_FLAGS_CONFIGURATION + assertEquals(FEATURE_FLAGS_CONFIGURATION, awaitItem()) + } + } + +} + +private const val FEATURE_FLAGS_CONFIGURATION_JSON = """ +{ + "featureFlags" : { + "bitwarden-authentication-enabled" : true + } +} + +""" + +private val FEATURE_FLAGS_CONFIGURATION = FeatureFlagsConfiguration( + featureFlags = mapOf( + LocalFeatureFlag.BitwardenAuthenticationEnabled.name to JsonPrimitive(true) + ) +)