Skip to content

Commit

Permalink
PM-9659: Do not show push notification permissions on FDroid
Browse files Browse the repository at this point in the history
  • Loading branch information
david-livefront committed Jul 18, 2024
1 parent 96324f0 commit 7225504
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.x8bit.bitwarden.ui.vault.feature.vault

import android.Manifest
import android.os.Build
import android.annotation.SuppressLint
import android.widget.Toast
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.scaleIn
Expand Down Expand Up @@ -125,7 +125,10 @@ fun VaultScreen(
}
}
val vaultHandlers = remember(viewModel) { VaultHandlers.create(viewModel) }
VaultScreenPushNotifications(permissionsManager = permissionsManager)
VaultScreenPushNotifications(
hideNotificationsDialog = state.hideNotificationsDialog,
permissionsManager = permissionsManager,
)
VaultScreenScaffold(
state = state,
pullToRefreshState = pullToRefreshState,
Expand All @@ -139,14 +142,18 @@ fun VaultScreen(
*/
@Composable
private fun VaultScreenPushNotifications(
hideNotificationsDialog: Boolean,
permissionsManager: PermissionsManager,
) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) return
if (hideNotificationsDialog) return
val launcher = permissionsManager.getLauncher {
// We do not actually care what the response is, we just need
// to give the user a chance to give us the permission.
}
println("LaunchedEffect")
LaunchedEffect(key1 = Unit) {
@SuppressLint("InlinedApi")
// We check the version code as part of the 'hideNotificationsDialog' property.
if (!permissionsManager.checkPermission(Manifest.permission.POST_NOTIFICATIONS)) {
launcher.launch(Manifest.permission.POST_NOTIFICATIONS)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.x8bit.bitwarden.ui.vault.feature.vault

import android.os.Build
import android.os.Parcelable
import androidx.compose.ui.graphics.Color
import androidx.lifecycle.viewModelScope
Expand All @@ -15,6 +16,8 @@ import com.x8bit.bitwarden.data.platform.manager.model.OrganizationEvent
import com.x8bit.bitwarden.data.platform.repository.SettingsRepository
import com.x8bit.bitwarden.data.platform.repository.model.DataState
import com.x8bit.bitwarden.data.platform.repository.util.baseIconUrl
import com.x8bit.bitwarden.data.platform.util.isBuildVersionBelow
import com.x8bit.bitwarden.data.platform.util.isFdroid
import com.x8bit.bitwarden.data.vault.datasource.network.model.PolicyTypeJson
import com.x8bit.bitwarden.data.vault.repository.VaultRepository
import com.x8bit.bitwarden.data.vault.repository.model.GenerateTotpResult
Expand Down Expand Up @@ -87,6 +90,7 @@ class VaultViewModel @Inject constructor(
isPullToRefreshSettingEnabled = settingsRepository.getPullToRefreshEnabledFlow().value,
baseIconUrl = userState.activeAccount.environment.environmentUrlData.baseIconUrl,
hasMasterPassword = userState.activeAccount.hasMasterPassword,
hideNotificationsDialog = isBuildVersionBelow(Build.VERSION_CODES.TIRAMISU) || isFdroid,
)
},
) {
Expand Down Expand Up @@ -628,6 +632,7 @@ data class VaultState(
private val isPullToRefreshSettingEnabled: Boolean,
val baseIconUrl: String,
val isIconLoadingDisabled: Boolean,
val hideNotificationsDialog: Boolean,
) : Parcelable {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,20 @@ class FakePermissionManager : PermissionsManager {
var getMultiplePermissionsResult: Map<String, Boolean> = emptyMap()

/**
* * The value for whether a rationale should be shown to the user.
* The value for whether a rationale should be shown to the user.
*/
var shouldShowRequestRationale: Boolean = false

/**
* Indicates that the [getLauncher] function has been called.
*/
var hasGetLauncherBeenCalled: Boolean = false

@Composable
override fun getLauncher(
onResult: (Boolean) -> Unit,
): ManagedActivityResultLauncher<String, Boolean> {
hasGetLauncherBeenCalled = true
return mockk {
every { launch(any()) } answers { onResult.invoke(getPermissionsResult) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1129,6 +1129,14 @@ class VaultScreenTest : BaseComposeTest() {
viewModel.trySendAction(VaultAction.TrashClick)
}
}

@Test
fun `permissionManager is invoked for notifications based on state`() {
assertFalse(permissionsManager.hasGetLauncherBeenCalled)
mutableStateFlow.update { it.copy(hideNotificationsDialog = false) }
composeTestRule.waitForIdle()
assertTrue(permissionsManager.hasGetLauncherBeenCalled)
}
}

private val ACTIVE_ACCOUNT_SUMMARY = AccountSummary(
Expand Down Expand Up @@ -1181,6 +1189,7 @@ private val DEFAULT_STATE: VaultState = VaultState(
baseIconUrl = Environment.Us.environmentUrlData.baseIconUrl,
isIconLoadingDisabled = false,
hasMasterPassword = true,
hideNotificationsDialog = true,
)

private val DEFAULT_CONTENT_VIEW_STATE: VaultState.ViewState.Content = VaultState.ViewState.Content(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1572,4 +1572,5 @@ private fun createMockVaultState(
baseIconUrl = Environment.Us.environmentUrlData.baseIconUrl,
isIconLoadingDisabled = false,
hasMasterPassword = true,
hideNotificationsDialog = true,
)

0 comments on commit 7225504

Please sign in to comment.