From cd020f2af9b8f370113c10969410ac018ad7da63 Mon Sep 17 00:00:00 2001 From: Joshua Queen <139182194+joshua-livefront@users.noreply.github.com> Date: Thu, 25 Jan 2024 14:25:28 -0500 Subject: [PATCH] Adding screen capture allow key to settings disk source (#780) --- .../datasource/disk/SettingsDiskSource.kt | 10 ++++ .../datasource/disk/SettingsDiskSourceImpl.kt | 16 ++++++ .../datasource/disk/SettingsDiskSourceTest.kt | 54 +++++++++++++++++++ .../disk/util/FakeSettingsDiskSource.kt | 14 ++++- 4 files changed, 92 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/x8bit/bitwarden/data/platform/datasource/disk/SettingsDiskSource.kt b/app/src/main/java/com/x8bit/bitwarden/data/platform/datasource/disk/SettingsDiskSource.kt index 2d3a954bb..ec6a2f981 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/platform/datasource/disk/SettingsDiskSource.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/platform/datasource/disk/SettingsDiskSource.kt @@ -171,4 +171,14 @@ interface SettingsDiskSource { userId: String, isApprovePasswordlessLoginsEnabled: Boolean?, ) + + /** + * Gets whether or not the given [userId] has enabled screen capture. + */ + fun getScreenCaptureAllowed(userId: String): Boolean? + + /** + * Stores whether or not [isScreenCaptureAllowed] for the given [userId]. + */ + fun storeScreenCaptureAllowed(userId: String, isScreenCaptureAllowed: Boolean?) } diff --git a/app/src/main/java/com/x8bit/bitwarden/data/platform/datasource/disk/SettingsDiskSourceImpl.kt b/app/src/main/java/com/x8bit/bitwarden/data/platform/datasource/disk/SettingsDiskSourceImpl.kt index a829c288a..163c93387 100644 --- a/app/src/main/java/com/x8bit/bitwarden/data/platform/datasource/disk/SettingsDiskSourceImpl.kt +++ b/app/src/main/java/com/x8bit/bitwarden/data/platform/datasource/disk/SettingsDiskSourceImpl.kt @@ -26,6 +26,7 @@ private const val DEFAULT_URI_MATCH_TYPE_KEY = "$BASE_KEY:defaultUriMatch" private const val DISABLE_AUTOFILL_SAVE_PROMPT_KEY = "$BASE_KEY:autofillDisableSavePrompt" private const val DISABLE_ICON_LOADING_KEY = "$BASE_KEY:disableFavicon" private const val APPROVE_PASSWORDLESS_LOGINS_KEY = "$BASE_KEY:approvePasswordlessLogins" +private const val SCREEN_CAPTURE_ALLOW_KEY = "$BASE_KEY:screenCaptureAllowed" /** * Primary implementation of [SettingsDiskSource]. @@ -107,6 +108,7 @@ class SettingsDiskSourceImpl( isApprovePasswordlessLoginsEnabled = null, ) storeLastSyncTime(userId = userId, lastSyncTime = null) + storeScreenCaptureAllowed(userId = userId, isScreenCaptureAllowed = null) } override fun getLastSyncTime(userId: String): Instant? = @@ -271,4 +273,18 @@ class SettingsDiskSourceImpl( value = isApprovePasswordlessLoginsEnabled, ) } + + override fun getScreenCaptureAllowed(userId: String): Boolean? { + return getBoolean(key = "${SCREEN_CAPTURE_ALLOW_KEY}_$userId") + } + + override fun storeScreenCaptureAllowed( + userId: String, + isScreenCaptureAllowed: Boolean?, + ) { + putBoolean( + key = "${SCREEN_CAPTURE_ALLOW_KEY}_$userId", + value = isScreenCaptureAllowed, + ) + } } diff --git a/app/src/test/java/com/x8bit/bitwarden/data/platform/datasource/disk/SettingsDiskSourceTest.kt b/app/src/test/java/com/x8bit/bitwarden/data/platform/datasource/disk/SettingsDiskSourceTest.kt index baa984bdb..fdfd9154e 100644 --- a/app/src/test/java/com/x8bit/bitwarden/data/platform/datasource/disk/SettingsDiskSourceTest.kt +++ b/app/src/test/java/com/x8bit/bitwarden/data/platform/datasource/disk/SettingsDiskSourceTest.kt @@ -104,6 +104,10 @@ class SettingsDiskSourceTest { userId = userId, lastSyncTime = Instant.parse("2023-10-27T12:00:00Z"), ) + settingsDiskSource.storeScreenCaptureAllowed( + userId = userId, + isScreenCaptureAllowed = true, + ) settingsDiskSource.clearData(userId = userId) @@ -116,6 +120,7 @@ class SettingsDiskSourceTest { assertNull(settingsDiskSource.getBlockedAutofillUris(userId = userId)) assertNull(settingsDiskSource.getApprovePasswordlessLoginsEnabled(userId = userId)) assertNull(settingsDiskSource.getLastSyncTime(userId = userId)) + assertNull(settingsDiskSource.getScreenCaptureAllowed(userId = userId)) } @Test @@ -717,4 +722,53 @@ class SettingsDiskSourceTest { ) assertFalse(fakeSharedPreferences.contains(approvePasswordlessLoginsKey)) } + + @Test + fun `getScreenCaptureAllowed should pull from SharedPreferences`() { + val screenCaptureAllowBaseKey = "bwPreferencesStorage:screenCaptureAllowed" + val mockUserId = "mockUserId" + val isScreenCaptureAllowed = true + fakeSharedPreferences.edit { + putBoolean("${screenCaptureAllowBaseKey}_$mockUserId", isScreenCaptureAllowed) + } + val actual = settingsDiskSource.getScreenCaptureAllowed(userId = mockUserId) + assertEquals( + isScreenCaptureAllowed, + actual, + ) + } + + @Test + fun `storeScreenCaptureAllowed for non-null values should update SharedPreferences`() { + val screenCaptureAllowBaseKey = "bwPreferencesStorage:screenCaptureAllowed" + val mockUserId = "mockUserId" + val isScreenCaptureAllowed = true + settingsDiskSource.storeScreenCaptureAllowed( + userId = mockUserId, + isScreenCaptureAllowed = isScreenCaptureAllowed, + ) + val actual = fakeSharedPreferences.getBoolean( + "${screenCaptureAllowBaseKey}_$mockUserId", + false, + ) + assertEquals( + isScreenCaptureAllowed, + actual, + ) + } + + @Test + fun `storeScreenCaptureAllowed for null values should clear SharedPreferences`() { + val screenCaptureAllowBaseKey = "bwPreferencesStorage:screenCaptureAllowed" + val mockUserId = "mockUserId" + val screenCaptureAllowKey = "${screenCaptureAllowBaseKey}_$mockUserId" + fakeSharedPreferences.edit { + putBoolean(screenCaptureAllowKey, true) + } + settingsDiskSource.storeScreenCaptureAllowed( + userId = mockUserId, + isScreenCaptureAllowed = null, + ) + assertFalse(fakeSharedPreferences.contains(screenCaptureAllowKey)) + } } diff --git a/app/src/test/java/com/x8bit/bitwarden/data/platform/datasource/disk/util/FakeSettingsDiskSource.kt b/app/src/test/java/com/x8bit/bitwarden/data/platform/datasource/disk/util/FakeSettingsDiskSource.kt index 7c6ebd105..ea61c3a8f 100644 --- a/app/src/test/java/com/x8bit/bitwarden/data/platform/datasource/disk/util/FakeSettingsDiskSource.kt +++ b/app/src/test/java/com/x8bit/bitwarden/data/platform/datasource/disk/util/FakeSettingsDiskSource.kt @@ -42,10 +42,9 @@ class FakeSettingsDiskSource : SettingsDiskSource { private val storedPullToRefreshEnabled = mutableMapOf() private val storedInlineAutofillEnabled = mutableMapOf() private val storedBlockedAutofillUris = mutableMapOf?>() - private var storedIsIconLoadingDisabled: Boolean? = null - private val storedApprovePasswordLoginsEnabled = mutableMapOf() + private val storedScreenCaptureAllowed = mutableMapOf() override var appLanguage: AppLanguage? = null @@ -81,6 +80,7 @@ class FakeSettingsDiskSource : SettingsDiskSource { storedPullToRefreshEnabled.remove(userId) storedInlineAutofillEnabled.remove(userId) storedBlockedAutofillUris.remove(userId) + storedScreenCaptureAllowed.remove(userId) mutableVaultTimeoutActionsFlowMap.remove(userId) mutableVaultTimeoutInMinutesFlowMap.remove(userId) @@ -190,6 +190,16 @@ class FakeSettingsDiskSource : SettingsDiskSource { storedApprovePasswordLoginsEnabled[userId] = isApprovePasswordlessLoginsEnabled } + override fun getScreenCaptureAllowed(userId: String): Boolean? = + storedScreenCaptureAllowed[userId] + + override fun storeScreenCaptureAllowed( + userId: String, + isScreenCaptureAllowed: Boolean?, + ) { + storedScreenCaptureAllowed[userId] = isScreenCaptureAllowed + } + //region Private helper functions private fun getMutableLastSyncTimeFlow(