mirror of
https://github.com/awfixers-stuff/OwnDroid.git
synced 2026-03-23 19:15:58 +00:00
add a option to lock OwnDroid when it is switched to background
This commit is contained in:
@@ -7,7 +7,6 @@ import android.util.Log
|
|||||||
import android.view.LayoutInflater
|
import android.view.LayoutInflater
|
||||||
import android.view.View
|
import android.view.View
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import android.widget.FrameLayout
|
|
||||||
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
|
||||||
@@ -19,7 +18,10 @@ import androidx.compose.foundation.layout.fillMaxSize
|
|||||||
import androidx.compose.material3.Button
|
import androidx.compose.material3.Button
|
||||||
import androidx.compose.material3.MaterialTheme
|
import androidx.compose.material3.MaterialTheme
|
||||||
import androidx.compose.material3.Text
|
import androidx.compose.material3.Text
|
||||||
import androidx.compose.runtime.*
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.runtime.LaunchedEffect
|
||||||
|
import androidx.compose.runtime.MutableState
|
||||||
|
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.compose.ui.platform.ComposeView
|
||||||
@@ -28,7 +30,9 @@ import androidx.core.content.ContextCompat
|
|||||||
import androidx.fragment.app.Fragment
|
import androidx.fragment.app.Fragment
|
||||||
import androidx.lifecycle.lifecycleScope
|
import androidx.lifecycle.lifecycleScope
|
||||||
import com.bintianqi.owndroid.ui.theme.OwnDroidTheme
|
import com.bintianqi.owndroid.ui.theme.OwnDroidTheme
|
||||||
import kotlinx.coroutines.*
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.delay
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
|
|
||||||
class AuthFragment: Fragment() {
|
class AuthFragment: Fragment() {
|
||||||
@SuppressLint("UnrememberedMutableState")
|
@SuppressLint("UnrememberedMutableState")
|
||||||
@@ -40,13 +44,7 @@ class AuthFragment: Fragment() {
|
|||||||
val fragmentManager = this.parentFragmentManager
|
val fragmentManager = this.parentFragmentManager
|
||||||
val transaction = fragmentManager.beginTransaction()
|
val transaction = fragmentManager.beginTransaction()
|
||||||
transaction.setCustomAnimations(R.anim.enter, R.anim.exit)
|
transaction.setCustomAnimations(R.anim.enter, R.anim.exit)
|
||||||
transaction.add(R.id.base, homeFragment)
|
transaction.remove(this@AuthFragment).commit()
|
||||||
requireActivity().findViewById<FrameLayout>(R.id.base).bringChildToFront(homeFragment.view)
|
|
||||||
transaction.commit()
|
|
||||||
lifecycleScope.launch {
|
|
||||||
delay(500)
|
|
||||||
fragmentManager.beginTransaction().remove(this@AuthFragment).commit()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
val promptInfo = Builder()
|
val promptInfo = Builder()
|
||||||
.setTitle(context.getText(R.string.authenticate))
|
.setTitle(context.getText(R.string.authenticate))
|
||||||
@@ -110,6 +108,7 @@ fun Auth(activity: Fragment, promptInfo: Builder, callback: AuthenticationCallba
|
|||||||
color = MaterialTheme.colorScheme.onBackground
|
color = MaterialTheme.colorScheme.onBackground
|
||||||
)
|
)
|
||||||
LaunchedEffect(Unit){
|
LaunchedEffect(Unit){
|
||||||
|
delay(300)
|
||||||
startAuth(activity, promptInfo, callback)
|
startAuth(activity, promptInfo, callback)
|
||||||
canStartAuth.value = false
|
canStartAuth.value = false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,11 +48,10 @@ 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() {
|
||||||
|
private var auth = false
|
||||||
@SuppressLint("UnrememberedMutableState")
|
@SuppressLint("UnrememberedMutableState")
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
registerActivityResult(this)
|
registerActivityResult(this)
|
||||||
@@ -63,15 +62,39 @@ class MainActivity : FragmentActivity() {
|
|||||||
val sharedPref = applicationContext.getSharedPreferences("data", Context.MODE_PRIVATE)
|
val sharedPref = applicationContext.getSharedPreferences("data", Context.MODE_PRIVATE)
|
||||||
val fragmentManager = supportFragmentManager
|
val fragmentManager = supportFragmentManager
|
||||||
val transaction = fragmentManager.beginTransaction()
|
val transaction = fragmentManager.beginTransaction()
|
||||||
|
transaction.add(R.id.base, HomeFragment(), "home")
|
||||||
if(sharedPref.getBoolean("auth", false)){
|
if(sharedPref.getBoolean("auth", false)){
|
||||||
transaction.add(R.id.base, AuthFragment(), "auth")
|
transaction.add(R.id.base, AuthFragment(), "auth")
|
||||||
}else{
|
|
||||||
transaction.add(R.id.base, homeFragment, "home")
|
|
||||||
}
|
}
|
||||||
transaction.commit()
|
transaction.commit()
|
||||||
val locale = applicationContext.resources?.configuration?.locale
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onResume() {
|
||||||
|
super.onResume()
|
||||||
|
if(auth){
|
||||||
|
val sharedPref = applicationContext.getSharedPreferences("data", Context.MODE_PRIVATE)
|
||||||
|
if(
|
||||||
|
sharedPref.getBoolean("auth", false) &&
|
||||||
|
sharedPref.getBoolean("lock_in_background", false)
|
||||||
|
){
|
||||||
|
val fragmentManager = supportFragmentManager
|
||||||
|
val fragment = fragmentManager.findFragmentByTag("auth")
|
||||||
|
if(fragment == null){
|
||||||
|
val transaction = fragmentManager.beginTransaction()
|
||||||
|
transaction.setCustomAnimations(R.anim.enter, R.anim.exit)
|
||||||
|
transaction.add(R.id.base, AuthFragment(), "auth").commit()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
auth = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onRestart() {
|
||||||
|
super.onRestart()
|
||||||
|
auth = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class HomeFragment: Fragment() {
|
class HomeFragment: Fragment() {
|
||||||
|
|||||||
@@ -111,6 +111,11 @@ private fun AuthSettings(){
|
|||||||
{ sharedPref.getBoolean("bio_auth",false) },
|
{ sharedPref.getBoolean("bio_auth",false) },
|
||||||
{ sharedPref.edit().putBoolean("bio_auth",it).apply() }
|
{ sharedPref.edit().putBoolean("bio_auth",it).apply() }
|
||||||
)
|
)
|
||||||
|
SwitchItem(
|
||||||
|
R.string.lock_in_background, "", null,
|
||||||
|
{ sharedPref.getBoolean("lock_in_background",false) },
|
||||||
|
{ sharedPref.edit().putBoolean("lock_in_background",it).apply() }
|
||||||
|
)
|
||||||
}
|
}
|
||||||
Box(modifier = Modifier.padding(horizontal = 8.dp)){
|
Box(modifier = Modifier.padding(horizontal = 8.dp)){
|
||||||
Information {
|
Information {
|
||||||
|
|||||||
@@ -2,16 +2,16 @@
|
|||||||
<set xmlns:android="http://schemas.android.com/apk/res/android">
|
<set xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
<scale
|
<scale
|
||||||
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
|
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
|
||||||
android:fromXScale="0.9"
|
android:fromXScale="0.95"
|
||||||
android:toXScale="1.0"
|
android:toXScale="1.0"
|
||||||
android:fromYScale="0.9"
|
android:fromYScale="0.95"
|
||||||
android:toYScale="1.0"
|
android:toYScale="1.0"
|
||||||
android:pivotX="50%"
|
android:pivotX="50%"
|
||||||
android:pivotY="50%"
|
android:pivotY="50%"
|
||||||
android:fillAfter="false"
|
android:fillAfter="false"
|
||||||
android:duration="250" />
|
android:duration="200" />
|
||||||
<alpha
|
<alpha
|
||||||
android:duration="200"
|
android:duration="150"
|
||||||
android:fromAlpha="0.0"
|
android:fromAlpha="0.0"
|
||||||
android:interpolator="@android:anim/accelerate_interpolator"
|
android:interpolator="@android:anim/accelerate_interpolator"
|
||||||
android:toAlpha="1.0" />
|
android:toAlpha="1.0" />
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<set xmlns:android="http://schemas.android.com/apk/res/android">
|
<set xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
<alpha
|
<alpha
|
||||||
android:duration="200"
|
android:duration="250"
|
||||||
android:fromAlpha="1.0"
|
android:fromAlpha="1.0"
|
||||||
android:interpolator="@android:anim/accelerate_interpolator"
|
android:interpolator="@android:anim/accelerate_interpolator"
|
||||||
android:toAlpha="0.5" />
|
android:toAlpha="0.0" />
|
||||||
</set>
|
</set>
|
||||||
|
|||||||
@@ -491,6 +491,7 @@
|
|||||||
<string name="use_password">使用密码</string>
|
<string name="use_password">使用密码</string>
|
||||||
<string name="auth_with_password">使用密码进行验证</string>
|
<string name="auth_with_password">使用密码进行验证</string>
|
||||||
<string name="auth_with_bio">使用生物识别进行验证</string>
|
<string name="auth_with_bio">使用生物识别进行验证</string>
|
||||||
|
<string name="lock_in_background">处于后台时锁定</string>
|
||||||
|
|
||||||
<!--AndroidPermission-->
|
<!--AndroidPermission-->
|
||||||
<string name="permission_READ_EXTERNAL_STORAGE">读取外部存储</string>
|
<string name="permission_READ_EXTERNAL_STORAGE">读取外部存储</string>
|
||||||
|
|||||||
@@ -506,6 +506,7 @@
|
|||||||
<string name="use_password">Use password</string>
|
<string name="use_password">Use password</string>
|
||||||
<string name="auth_with_password">Authenticate OwnDroid with password</string>
|
<string name="auth_with_password">Authenticate OwnDroid with password</string>
|
||||||
<string name="auth_with_bio">Authenticate OwnDroid with biometrics</string>
|
<string name="auth_with_bio">Authenticate OwnDroid with biometrics</string>
|
||||||
|
<string name="lock_in_background">Lock when switch to background</string>
|
||||||
|
|
||||||
<!--AndroidPermission-->
|
<!--AndroidPermission-->
|
||||||
<string name="permission_READ_EXTERNAL_STORAGE">Read external storage</string>
|
<string name="permission_READ_EXTERNAL_STORAGE">Read external storage</string>
|
||||||
|
|||||||
Reference in New Issue
Block a user