Refactor App lock

This commit is contained in:
BinTianqi
2025-03-09 20:11:24 +08:00
parent 10e34a4e96
commit 6ffe79fd8b
15 changed files with 252 additions and 255 deletions

View File

@@ -5,8 +5,6 @@ import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.biometric.BiometricPrompt
import androidx.biometric.BiometricPrompt.AuthenticationCallback
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
@@ -15,7 +13,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.res.stringResource
import androidx.core.view.WindowCompat
import androidx.compose.ui.window.Dialog
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.bintianqi.owndroid.ui.theme.OwnDroidTheme
@@ -24,67 +22,50 @@ import kotlin.system.exitProcess
class ManageSpaceActivity: FragmentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
enableEdgeToEdge()
WindowCompat.setDecorFitsSystemWindows(window, false)
super.onCreate(savedInstanceState)
val authenticate = SharedPrefs(applicationContext).auth
val vm by viewModels<MyViewModel>()
fun clearStorage() {
filesDir.deleteRecursively()
cacheDir.deleteRecursively()
codeCacheDir.deleteRecursively()
if(Build.VERSION.SDK_INT >= 24) {
dataDir.resolve("shared_prefs").deleteRecursively()
} else {
val sharedPref = applicationContext.getSharedPreferences("data", MODE_PRIVATE)
sharedPref.edit().clear().apply()
}
finish()
exitProcess(0)
}
setContent {
var authenticating by remember { mutableStateOf(false) }
val callback = object: AuthenticationCallback() {
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
super.onAuthenticationSucceeded(result)
clearStorage()
}
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString)
when(errorCode) {
BiometricPrompt.ERROR_NO_DEVICE_CREDENTIAL -> clearStorage()
else -> authenticating = false
}
}
}
val theme by vm.theme.collectAsStateWithLifecycle()
OwnDroidTheme(theme) {
AlertDialog(
text = {
Text(stringResource(R.string.clear_storage))
},
onDismissRequest = { finish() },
dismissButton = {
TextButton(onClick = { finish() }) {
Text(stringResource(R.string.cancel))
}
},
confirmButton = {
TextButton(
onClick = {
if(authenticate) {
authenticating = true
startAuth(this, callback)
} else {
clearStorage()
}
},
enabled = !authenticating
) {
Text(stringResource(R.string.confirm))
}
var appLockDialog by remember { mutableStateOf(!SharedPrefs(this).lockPassword.isNullOrEmpty()) }
if(appLockDialog) {
Dialog(::finish) {
AppLockDialog({ appLockDialog = false }, ::finish)
}
)
} else {
AlertDialog(
text = {
Text(stringResource(R.string.clear_storage))
},
onDismissRequest = ::finish,
dismissButton = {
TextButton(::finish) {
Text(stringResource(R.string.cancel))
}
},
confirmButton = {
TextButton(::clearStorage) {
Text(stringResource(R.string.confirm))
}
}
)
}
}
}
}
fun clearStorage() {
filesDir.deleteRecursively()
cacheDir.deleteRecursively()
codeCacheDir.deleteRecursively()
if(Build.VERSION.SDK_INT >= 24) {
dataDir.resolve("shared_prefs").deleteRecursively()
} else {
val sharedPref = applicationContext.getSharedPreferences("data", MODE_PRIVATE)
sharedPref.edit().clear().apply()
}
this.showOperationResultToast(true)
finish()
exitProcess(0)
}
}