mirror of
https://github.com/awfixers-stuff/OwnDroid.git
synced 2026-03-23 19:15:58 +00:00
use AuthFragment instead of AuthActivity
This commit is contained in:
@@ -2,60 +2,63 @@ package com.bintianqi.owndroid
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.Toast
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.biometric.BiometricManager
|
||||
import androidx.biometric.BiometricPrompt
|
||||
import androidx.biometric.BiometricPrompt.AuthenticationCallback
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.layout.Arrangement
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.material3.Button
|
||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||
import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.platform.ComposeView
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.view.WindowCompat
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.FragmentActivity
|
||||
import com.bintianqi.owndroid.ui.theme.OwnDroidTheme
|
||||
|
||||
var authenticated = false
|
||||
|
||||
class AuthActivity: FragmentActivity(){
|
||||
class AuthFragment: Fragment() {
|
||||
@SuppressLint("UnrememberedMutableState")
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
enableEdgeToEdge()
|
||||
WindowCompat.setDecorFitsSystemWindows(window, false)
|
||||
super.onCreate(savedInstanceState)
|
||||
val mainActivityIntent = Intent(applicationContext, MainActivity::class.java)
|
||||
val sharedPref = applicationContext.getSharedPreferences("data", Context.MODE_PRIVATE)
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
||||
val sharedPref = context?.getSharedPreferences("data", Context.MODE_PRIVATE)!!
|
||||
val onAuthSucceed = {
|
||||
val fragmentManager = this.parentFragmentManager
|
||||
val fragment = fragmentManager.findFragmentByTag("auth")
|
||||
if(fragment != null) {
|
||||
val fragmentTransaction = fragmentManager.beginTransaction()
|
||||
fragmentTransaction.replace(R.id.base, homeFragment)
|
||||
fragmentTransaction.commit()
|
||||
}
|
||||
}
|
||||
val callback = object: AuthenticationCallback() {
|
||||
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
|
||||
super.onAuthenticationSucceeded(result)
|
||||
authenticated = true
|
||||
startActivity(mainActivityIntent)
|
||||
finish()
|
||||
onAuthSucceed()
|
||||
}
|
||||
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
|
||||
super.onAuthenticationError(errorCode, errString)
|
||||
Toast.makeText(applicationContext, errString, Toast.LENGTH_SHORT).show()
|
||||
/*if (errString.toString().isNotEmpty()) {
|
||||
}*/
|
||||
if(errorCode == BiometricPrompt.ERROR_NO_DEVICE_CREDENTIAL) onAuthSucceed()
|
||||
if(errorCode == BiometricPrompt.ERROR_CANCELED) return
|
||||
Toast.makeText(context, errString, Toast.LENGTH_SHORT).show()
|
||||
}
|
||||
}
|
||||
setContent {
|
||||
val materialYou = mutableStateOf(sharedPref.getBoolean("material_you",true))
|
||||
val blackTheme = mutableStateOf(sharedPref.getBoolean("black_theme", false))
|
||||
OwnDroidTheme(materialYou.value, blackTheme.value) {
|
||||
Auth(this, callback)
|
||||
return ComposeView(requireContext()).apply {
|
||||
setContent {
|
||||
val materialYou = mutableStateOf(sharedPref.getBoolean("material_you",true))
|
||||
val blackTheme = mutableStateOf(sharedPref.getBoolean("black_theme", false))
|
||||
OwnDroidTheme(materialYou.value, blackTheme.value) {
|
||||
Auth(this@AuthFragment.requireActivity(), callback)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -66,9 +69,9 @@ fun Auth(activity: FragmentActivity, callback: AuthenticationCallback) {
|
||||
Column(
|
||||
horizontalAlignment = Alignment.CenterHorizontally,
|
||||
verticalArrangement = Arrangement.Center,
|
||||
modifier = Modifier.fillMaxSize()
|
||||
modifier = Modifier.fillMaxSize().background(MaterialTheme.colorScheme.background)
|
||||
){
|
||||
Text(text = "Authenticate", style = MaterialTheme.typography.headlineLarge)
|
||||
Text(text = "Authenticate", style = MaterialTheme.typography.headlineLarge, color = MaterialTheme.colorScheme.onBackground)
|
||||
Button(
|
||||
onClick = {
|
||||
authWithBiometricPrompt(activity, callback)
|
||||
|
||||
@@ -4,12 +4,13 @@ import android.annotation.SuppressLint
|
||||
import android.app.admin.DevicePolicyManager
|
||||
import android.content.ComponentName
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.os.Build.VERSION
|
||||
import android.os.Bundle
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.Toast
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.compose.foundation.background
|
||||
import androidx.compose.foundation.clickable
|
||||
@@ -28,12 +29,14 @@ import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.draw.clip
|
||||
import androidx.compose.ui.input.pointer.pointerInput
|
||||
import androidx.compose.ui.platform.ComposeView
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.compose.ui.platform.LocalFocusManager
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import androidx.compose.ui.unit.dp
|
||||
import androidx.core.view.WindowCompat
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.fragment.app.FragmentActivity
|
||||
import androidx.navigation.NavHostController
|
||||
import androidx.navigation.compose.NavHost
|
||||
@@ -45,27 +48,39 @@ import com.bintianqi.owndroid.ui.theme.OwnDroidTheme
|
||||
import kotlinx.coroutines.delay
|
||||
import java.util.Locale
|
||||
|
||||
val homeFragment = HomeFragment()
|
||||
|
||||
var backToHome = false
|
||||
@ExperimentalMaterial3Api
|
||||
class MainActivity : FragmentActivity() {
|
||||
@SuppressLint("UnrememberedMutableState")
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
if(!authenticated){
|
||||
startActivity(Intent(applicationContext, AuthActivity::class.java))
|
||||
finish()
|
||||
}
|
||||
registerActivityResult(this)
|
||||
enableEdgeToEdge()
|
||||
WindowCompat.setDecorFitsSystemWindows(window, false)
|
||||
super.onCreate(savedInstanceState)
|
||||
registerActivityResult(this)
|
||||
val locale = applicationContext.resources.configuration.locale
|
||||
setContentView(R.layout.base)
|
||||
val fragmentManager = supportFragmentManager
|
||||
val transaction = fragmentManager.beginTransaction()
|
||||
transaction.add(R.id.base, AuthFragment(), "auth")
|
||||
transaction.commit()
|
||||
val locale = applicationContext.resources?.configuration?.locale
|
||||
zhCN = locale==Locale.SIMPLIFIED_CHINESE||locale==Locale.CHINESE||locale==Locale.CHINA
|
||||
val sharedPref = applicationContext.getSharedPreferences("data", Context.MODE_PRIVATE)
|
||||
setContent {
|
||||
val materialYou = mutableStateOf(sharedPref.getBoolean("material_you",true))
|
||||
val blackTheme = mutableStateOf(sharedPref.getBoolean("black_theme", false))
|
||||
OwnDroidTheme(materialYou.value, blackTheme.value){
|
||||
MyScaffold(materialYou, blackTheme)
|
||||
}
|
||||
}
|
||||
|
||||
class HomeFragment: Fragment() {
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@SuppressLint("UnrememberedMutableState")
|
||||
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
|
||||
val sharedPref = context?.getSharedPreferences("data", Context.MODE_PRIVATE)!!
|
||||
return ComposeView(requireContext()).apply {
|
||||
setContent {
|
||||
val materialYou = mutableStateOf(sharedPref.getBoolean("material_you",true))
|
||||
val blackTheme = mutableStateOf(sharedPref.getBoolean("black_theme", false))
|
||||
OwnDroidTheme(materialYou.value, blackTheme.value){
|
||||
MyScaffold(materialYou, blackTheme)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user