Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: App crash on app unlock with fingerprint (WPB-5110) (WPB-5108) #2348

Merged
merged 2 commits into from
Oct 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ object BiometricPromptUtils {
override fun onAuthenticationError(errorCode: Int, errorString: CharSequence) {
super.onAuthenticationError(errorCode, errorString)
appLogger.i("$TAG errorCode is $errorCode and errorString is: $errorString")
if (errorCode == ERROR_NEGATIVE_BUTTON) {
if (errorCode == ERROR_NEGATIVE_BUTTON || errorCode == BiometricPrompt.ERROR_LOCKOUT) {
onRequestPasscode()
} else {
onCancel()
Expand Down Expand Up @@ -78,16 +78,15 @@ fun AppCompatActivity.showBiometricPrompt(
onCancel: () -> Unit,
onRequestPasscode: () -> Unit
) {
appLogger.i("$TAG showing biometrics dialog...")

val canAuthenticate = BiometricManager.from(this)
.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_STRONG)
if (canAuthenticate == BiometricManager.BIOMETRIC_SUCCESS) {
appLogger.i("$TAG showing biometrics dialog...")
val biometricPrompt = BiometricPromptUtils.createBiometricPrompt(
this,
onSuccess,
onCancel,
onRequestPasscode,
activity = this,
onSuccess = onSuccess,
onCancel = onCancel,
onRequestPasscode = onRequestPasscode
)
val promptInfo = BiometricPromptUtils.createPromptInfo(this)
biometricPrompt.authenticate(promptInfo)
Expand Down
57 changes: 31 additions & 26 deletions app/src/main/kotlin/com/wire/android/ui/WireActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import androidx.activity.compose.setContent
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.biometric.BiometricManager
import androidx.biometric.BiometricManager.Authenticators.BIOMETRIC_STRONG
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.material3.SnackbarHostState
Expand All @@ -40,7 +41,6 @@ import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import androidx.compose.runtime.staticCompositionLocalOf
Expand All @@ -49,7 +49,10 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.core.view.WindowCompat
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.navigation.NavController
import androidx.navigation.NavHostController
import com.ramcosta.composedestinations.spec.Route
Expand Down Expand Up @@ -93,12 +96,9 @@ import com.wire.android.util.debug.LocalFeatureVisibilityFlags
import com.wire.android.util.deeplink.DeepLinkResult
import com.wire.android.util.ui.updateScreenSettings
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.onSubscription
import kotlinx.coroutines.launch
import javax.inject.Inject
Expand Down Expand Up @@ -170,7 +170,6 @@ class WireActivity : AppCompatActivity() {
Column(modifier = Modifier.statusBarsPadding()) {
ReportDrawnWhen { isLoaded }
val navigator = rememberNavigator(this@WireActivity::finish)
val scope = rememberCoroutineScope()
CommonTopAppBar(
connectivityUIState = commonTopAppBarViewModel.connectivityState,
onReturnToCallClick = { establishedCall ->
Expand All @@ -184,7 +183,7 @@ class WireActivity : AppCompatActivity() {

// This setup needs to be done after the navigation graph is created, because building the graph takes some time,
// and if any NavigationCommand is executed before the graph is fully built, it will cause a NullPointerException.
setUpNavigation(navigator.navController, onComplete, scope)
setUpNavigation(navigator.navController, onComplete)
isLoaded = true
handleScreenshotCensoring()
handleAppLock(navigator::navigate)
Expand All @@ -199,17 +198,20 @@ class WireActivity : AppCompatActivity() {
private fun setUpNavigation(
navController: NavHostController,
onComplete: () -> Unit,
scope: CoroutineScope
) {
val currentKeyboardController by rememberUpdatedState(LocalSoftwareKeyboardController.current)
val currentNavController by rememberUpdatedState(navController)
LaunchedEffect(scope) {
navigationCommands
.onSubscription { onComplete() }
.onEach { command ->
currentKeyboardController?.hide()
currentNavController.navigateToItem(command)
}.launchIn(scope)
LaunchedEffect(Unit) {
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
navigationCommands
.onSubscription { onComplete() }
.collectLatest {
currentKeyboardController?.hide()
currentNavController.navigateToItem(it)
}
}
}
}

DisposableEffect(navController) {
Expand Down Expand Up @@ -241,19 +243,22 @@ class WireActivity : AppCompatActivity() {
@Composable
private fun handleAppLock(navigate: (NavigationCommand) -> Unit) {
LaunchedEffect(Unit) {
lockCodeTimeManager.isLocked()
.filter { it }
.collectLatest {
val canAuthenticateWithBiometrics = BiometricManager
.from(this@WireActivity)
.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_STRONG)

if (canAuthenticateWithBiometrics == BiometricManager.BIOMETRIC_SUCCESS) {
navigate(NavigationCommand(AppUnlockWithBiometricsScreenDestination, BackStackMode.UPDATE_EXISTED))
} else {
navigate(NavigationCommand(EnterLockCodeScreenDestination, BackStackMode.UPDATE_EXISTED))
lifecycleScope.launch {
// Listen to one flow in a lifecycle-aware manner using flowWithLifecycle
lockCodeTimeManager.isLocked()
.flowWithLifecycle(lifecycle, Lifecycle.State.STARTED)
.filter { it }
.collectLatest {
val canAuthenticateWithBiometrics = BiometricManager
.from(this@WireActivity)
.canAuthenticate(BIOMETRIC_STRONG)
if (canAuthenticateWithBiometrics == BiometricManager.BIOMETRIC_SUCCESS) {
navigate(NavigationCommand(AppUnlockWithBiometricsScreenDestination, BackStackMode.UPDATE_EXISTED))
} else {
navigate(NavigationCommand(EnterLockCodeScreenDestination, BackStackMode.UPDATE_EXISTED))
}
}
}
}
}
}

Expand Down