use AuthFragment instead of AuthActivity

This commit is contained in:
BinTianqi
2024-05-15 12:42:59 +08:00
parent 11bd8a28a6
commit b7f6eeadb2
4 changed files with 68 additions and 46 deletions

View File

@@ -45,10 +45,6 @@
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity
android:name=".AuthActivity"
android:theme="@style/Theme.OwnDroid">
</activity>
<receiver <receiver
android:name=".Receiver" android:name=".Receiver"
android:description="@string/app_name" android:description="@string/app_name"

View File

@@ -2,60 +2,63 @@ package com.bintianqi.owndroid
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.content.Context import android.content.Context
import android.content.Intent
import android.os.Bundle import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast import android.widget.Toast
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.biometric.BiometricManager import androidx.biometric.BiometricManager
import androidx.biometric.BiometricPrompt import androidx.biometric.BiometricPrompt
import androidx.biometric.BiometricPrompt.AuthenticationCallback import androidx.biometric.BiometricPrompt.AuthenticationCallback
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Button import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text import androidx.compose.material3.Text
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.ComposeView
import androidx.core.content.ContextCompat import androidx.core.content.ContextCompat
import androidx.core.view.WindowCompat import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity import androidx.fragment.app.FragmentActivity
import com.bintianqi.owndroid.ui.theme.OwnDroidTheme import com.bintianqi.owndroid.ui.theme.OwnDroidTheme
var authenticated = false class AuthFragment: Fragment() {
class AuthActivity: FragmentActivity(){
@SuppressLint("UnrememberedMutableState") @SuppressLint("UnrememberedMutableState")
@OptIn(ExperimentalMaterial3Api::class) override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
override fun onCreate(savedInstanceState: Bundle?) { val sharedPref = context?.getSharedPreferences("data", Context.MODE_PRIVATE)!!
enableEdgeToEdge() val onAuthSucceed = {
WindowCompat.setDecorFitsSystemWindows(window, false) val fragmentManager = this.parentFragmentManager
super.onCreate(savedInstanceState) val fragment = fragmentManager.findFragmentByTag("auth")
val mainActivityIntent = Intent(applicationContext, MainActivity::class.java) if(fragment != null) {
val sharedPref = applicationContext.getSharedPreferences("data", Context.MODE_PRIVATE) val fragmentTransaction = fragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.base, homeFragment)
fragmentTransaction.commit()
}
}
val callback = object: AuthenticationCallback() { val callback = object: AuthenticationCallback() {
override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) { override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
super.onAuthenticationSucceeded(result) super.onAuthenticationSucceeded(result)
authenticated = true onAuthSucceed()
startActivity(mainActivityIntent)
finish()
} }
override fun onAuthenticationError(errorCode: Int, errString: CharSequence) { override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
super.onAuthenticationError(errorCode, errString) super.onAuthenticationError(errorCode, errString)
Toast.makeText(applicationContext, errString, Toast.LENGTH_SHORT).show() if(errorCode == BiometricPrompt.ERROR_NO_DEVICE_CREDENTIAL) onAuthSucceed()
/*if (errString.toString().isNotEmpty()) { if(errorCode == BiometricPrompt.ERROR_CANCELED) return
}*/ Toast.makeText(context, errString, Toast.LENGTH_SHORT).show()
} }
} }
return ComposeView(requireContext()).apply {
setContent { setContent {
val materialYou = mutableStateOf(sharedPref.getBoolean("material_you",true)) val materialYou = mutableStateOf(sharedPref.getBoolean("material_you",true))
val blackTheme = mutableStateOf(sharedPref.getBoolean("black_theme", false)) val blackTheme = mutableStateOf(sharedPref.getBoolean("black_theme", false))
OwnDroidTheme(materialYou.value, blackTheme.value) { OwnDroidTheme(materialYou.value, blackTheme.value) {
Auth(this, callback) Auth(this@AuthFragment.requireActivity(), callback)
}
} }
} }
} }
@@ -66,9 +69,9 @@ fun Auth(activity: FragmentActivity, callback: AuthenticationCallback) {
Column( Column(
horizontalAlignment = Alignment.CenterHorizontally, horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center, 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( Button(
onClick = { onClick = {
authWithBiometricPrompt(activity, callback) authWithBiometricPrompt(activity, callback)

View File

@@ -4,12 +4,13 @@ import android.annotation.SuppressLint
import android.app.admin.DevicePolicyManager import android.app.admin.DevicePolicyManager
import android.content.ComponentName import android.content.ComponentName
import android.content.Context import android.content.Context
import android.content.Intent
import android.os.Build.VERSION import android.os.Build.VERSION
import android.os.Bundle import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast import android.widget.Toast
import androidx.activity.ComponentActivity import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.clickable import androidx.compose.foundation.clickable
@@ -28,12 +29,14 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip import androidx.compose.ui.draw.clip
import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalFocusManager import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.core.view.WindowCompat import androidx.core.view.WindowCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity import androidx.fragment.app.FragmentActivity
import androidx.navigation.NavHostController import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost import androidx.navigation.compose.NavHost
@@ -45,22 +48,33 @@ import com.bintianqi.owndroid.ui.theme.OwnDroidTheme
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import java.util.Locale import java.util.Locale
val homeFragment = HomeFragment()
var backToHome = false var backToHome = false
@ExperimentalMaterial3Api @ExperimentalMaterial3Api
class MainActivity : FragmentActivity() { class MainActivity : FragmentActivity() {
@SuppressLint("UnrememberedMutableState") @SuppressLint("UnrememberedMutableState")
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
if(!authenticated){ registerActivityResult(this)
startActivity(Intent(applicationContext, AuthActivity::class.java))
finish()
}
enableEdgeToEdge() enableEdgeToEdge()
WindowCompat.setDecorFitsSystemWindows(window, false) WindowCompat.setDecorFitsSystemWindows(window, false)
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
registerActivityResult(this) setContentView(R.layout.base)
val locale = applicationContext.resources.configuration.locale 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 zhCN = locale==Locale.SIMPLIFIED_CHINESE||locale==Locale.CHINESE||locale==Locale.CHINA
val sharedPref = applicationContext.getSharedPreferences("data", Context.MODE_PRIVATE) }
}
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 { setContent {
val materialYou = mutableStateOf(sharedPref.getBoolean("material_you",true)) val materialYou = mutableStateOf(sharedPref.getBoolean("material_you",true))
val blackTheme = mutableStateOf(sharedPref.getBoolean("black_theme", false)) val blackTheme = mutableStateOf(sharedPref.getBoolean("black_theme", false))
@@ -69,6 +83,7 @@ class MainActivity : FragmentActivity() {
} }
} }
} }
}
} }
@SuppressLint("UnrememberedMutableState") @SuppressLint("UnrememberedMutableState")

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/base"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>