mirror of
https://github.com/awfixers-stuff/OwnDroid.git
synced 2026-03-24 03:16:00 +00:00
Fix some bugs of authentication
Display authentication screen in NavHost Remove "Protect storage", authenticate to clear storage instead Force enable biometrics on if using password alone is not supported
This commit is contained in:
@@ -1,55 +1,34 @@
|
||||
package com.bintianqi.owndroid
|
||||
|
||||
import android.content.Context
|
||||
import androidx.activity.compose.BackHandler
|
||||
import androidx.biometric.BiometricManager
|
||||
import androidx.biometric.BiometricPrompt
|
||||
import androidx.biometric.BiometricPrompt.AuthenticationCallback
|
||||
import androidx.biometric.BiometricPrompt.PromptInfo.Builder
|
||||
import androidx.compose.animation.core.animateFloatAsState
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.isSystemInDarkTheme
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Surface
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.*
|
||||
import androidx.compose.runtime.saveable.rememberSaveable
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.alpha
|
||||
import androidx.compose.ui.graphics.Color
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.fragment.app.FragmentActivity
|
||||
import com.bintianqi.owndroid.ui.Animations
|
||||
import androidx.navigation.NavHostController
|
||||
import kotlinx.coroutines.delay
|
||||
import kotlinx.coroutines.launch
|
||||
|
||||
@Composable
|
||||
fun AuthScreen(activity: FragmentActivity, showAuth: MutableState<Boolean>) {
|
||||
val context = activity.applicationContext
|
||||
val coroutineScope = rememberCoroutineScope()
|
||||
var canStartAuth by remember { mutableStateOf(true) }
|
||||
var fallback by remember { mutableStateOf(false) }
|
||||
var startFade by remember { mutableStateOf(false) }
|
||||
val alpha by animateFloatAsState(
|
||||
targetValue = if(startFade) 0F else 1F,
|
||||
label = "AuthScreenFade",
|
||||
animationSpec = Animations.authScreenFade
|
||||
)
|
||||
val onAuthSucceed = {
|
||||
startFade = true
|
||||
coroutineScope.launch {
|
||||
delay(300)
|
||||
showAuth.value = false
|
||||
}
|
||||
}
|
||||
val promptInfo = Builder()
|
||||
.setTitle(context.getText(R.string.authenticate))
|
||||
.setSubtitle(context.getText(R.string.auth_with_bio))
|
||||
.setConfirmationRequired(true)
|
||||
fun Authenticate(activity: FragmentActivity, navCtrl: NavHostController) {
|
||||
BackHandler { activity.moveTaskToBack(true) }
|
||||
var status by rememberSaveable { mutableIntStateOf(0) } // 0:Prompt automatically, 1:Authenticating, 2:Prompt manually
|
||||
val onAuthSucceed = { navCtrl.navigateUp() }
|
||||
val callback = object: AuthenticationCallback() {
|
||||
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
|
||||
super.onAuthenticationSucceeded(result)
|
||||
@@ -59,49 +38,33 @@ fun AuthScreen(activity: FragmentActivity, showAuth: MutableState<Boolean>) {
|
||||
super.onAuthenticationError(errorCode, errString)
|
||||
when(errorCode) {
|
||||
BiometricPrompt.ERROR_NO_DEVICE_CREDENTIAL -> onAuthSucceed()
|
||||
BiometricPrompt.ERROR_NEGATIVE_BUTTON -> fallback = true
|
||||
else -> canStartAuth = true
|
||||
else -> status = 2
|
||||
}
|
||||
}
|
||||
}
|
||||
LaunchedEffect(fallback) {
|
||||
if(fallback) {
|
||||
val fallbackPromptInfo = promptInfo
|
||||
.setAllowedAuthenticators(BiometricManager.Authenticators.DEVICE_CREDENTIAL)
|
||||
.setSubtitle(context.getText(R.string.auth_with_password))
|
||||
.build()
|
||||
val executor = ContextCompat.getMainExecutor(context)
|
||||
val biometricPrompt = BiometricPrompt(activity, executor, callback)
|
||||
biometricPrompt.authenticate(fallbackPromptInfo)
|
||||
LaunchedEffect(Unit) {
|
||||
if(status == 0) {
|
||||
delay(300)
|
||||
startAuth(activity, callback)
|
||||
status = 1
|
||||
}
|
||||
}
|
||||
Surface(
|
||||
modifier = Modifier
|
||||
.fillMaxSize()
|
||||
.alpha(alpha)
|
||||
.background(if(isSystemInDarkTheme()) Color.Black else Color.White)
|
||||
) {
|
||||
Scaffold { paddingValues ->
|
||||
Column(
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.Center,
|
||||
modifier = Modifier.fillMaxSize().background(MaterialTheme.colorScheme.background)
|
||||
modifier = Modifier.fillMaxSize().padding(paddingValues)
|
||||
) {
|
||||
LaunchedEffect(Unit) {
|
||||
delay(300)
|
||||
startAuth(activity, promptInfo, callback)
|
||||
canStartAuth = false
|
||||
}
|
||||
Text(
|
||||
text = stringResource(R.string.authenticate),
|
||||
style = MaterialTheme.typography.headlineLarge,
|
||||
color = MaterialTheme.colorScheme.onBackground
|
||||
)
|
||||
Button(
|
||||
onClick = {
|
||||
startAuth(activity, promptInfo, callback)
|
||||
canStartAuth = false
|
||||
startAuth(activity, callback)
|
||||
status = 1
|
||||
},
|
||||
enabled = canStartAuth
|
||||
enabled = status != 1
|
||||
) {
|
||||
Text(text = stringResource(R.string.start))
|
||||
}
|
||||
@@ -109,31 +72,15 @@ fun AuthScreen(activity: FragmentActivity, showAuth: MutableState<Boolean>) {
|
||||
}
|
||||
}
|
||||
|
||||
private fun startAuth(activity: FragmentActivity, basicPromptInfo: Builder, callback: AuthenticationCallback) {
|
||||
fun startAuth(activity: FragmentActivity, callback: AuthenticationCallback) {
|
||||
val context = activity.applicationContext
|
||||
val promptInfo = basicPromptInfo
|
||||
val bioManager = BiometricManager.from(context)
|
||||
val sharedPref = context.getSharedPreferences("data", Context.MODE_PRIVATE)
|
||||
if(sharedPref.getBoolean("bio_auth", false)) {
|
||||
when(BiometricManager.BIOMETRIC_SUCCESS) {
|
||||
bioManager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_STRONG) ->
|
||||
promptInfo
|
||||
.setAllowedAuthenticators(BiometricManager.Authenticators.BIOMETRIC_STRONG)
|
||||
.setNegativeButtonText(context.getText(R.string.use_password))
|
||||
bioManager.canAuthenticate(BiometricManager.Authenticators.BIOMETRIC_WEAK) ->
|
||||
promptInfo
|
||||
.setAllowedAuthenticators(BiometricManager.Authenticators.BIOMETRIC_WEAK)
|
||||
.setNegativeButtonText(context.getText(R.string.use_password))
|
||||
else -> promptInfo
|
||||
.setAllowedAuthenticators(BiometricManager.Authenticators.DEVICE_CREDENTIAL)
|
||||
.setSubtitle(context.getText(R.string.auth_with_password))
|
||||
}
|
||||
}else{
|
||||
promptInfo
|
||||
.setAllowedAuthenticators(BiometricManager.Authenticators.DEVICE_CREDENTIAL)
|
||||
.setSubtitle(context.getText(R.string.auth_with_password))
|
||||
val promptInfo = Builder().setTitle(context.getText(R.string.authenticate))
|
||||
if(sharedPref.getInt("biometrics_auth", 0) != 0) {
|
||||
promptInfo.setAllowedAuthenticators(BiometricManager.Authenticators.DEVICE_CREDENTIAL or BiometricManager.Authenticators.BIOMETRIC_WEAK)
|
||||
} else {
|
||||
promptInfo.setAllowedAuthenticators(BiometricManager.Authenticators.DEVICE_CREDENTIAL)
|
||||
}
|
||||
val executor = ContextCompat.getMainExecutor(context)
|
||||
val biometricPrompt = BiometricPrompt(activity, executor, callback)
|
||||
biometricPrompt.authenticate(promptInfo.build())
|
||||
BiometricPrompt(activity, executor, callback).authenticate(promptInfo.build())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user