Skip to content

Commit

Permalink
optimize performance
Browse files Browse the repository at this point in the history
  • Loading branch information
BinTianqi committed Aug 12, 2024
1 parent 7ab60e4 commit 439a75b
Show file tree
Hide file tree
Showing 14 changed files with 177 additions and 163 deletions.
37 changes: 19 additions & 18 deletions app/src/main/java/com/bintianqi/owndroid/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,8 @@ fun Home(materialYou:MutableState<Boolean>, blackTheme:MutableState<Boolean>) {
val context = LocalContext.current
val dpm = context.getDPM()
val receiver = context.getReceiver()
val sharedPref = LocalContext.current.getSharedPreferences("data", Context.MODE_PRIVATE)
val sharedPref = context.getSharedPreferences("data", Context.MODE_PRIVATE)
val focusMgr = LocalFocusManager.current
val pkgName = remember { mutableStateOf("") }
val dialogStatus = remember { mutableIntStateOf(0) }
val backToHome by backToHomeStateFlow.collectAsState()
LaunchedEffect(backToHome) {
Expand All @@ -152,17 +151,17 @@ fun Home(materialYou:MutableState<Boolean>, blackTheme:MutableState<Boolean>) {
popEnterTransition = Animations.navHostPopEnterTransition,
popExitTransition = Animations.navHostPopExitTransition
) {
composable(route = "HomePage") { HomePage(navCtrl, pkgName) }
composable(route = "HomePage") { HomePage(navCtrl) }
composable(route = "SystemManage") { SystemManage(navCtrl) }
composable(route = "ManagedProfile") { ManagedProfile(navCtrl) }
composable(route = "Permissions") { DpmPermissions(navCtrl) }
composable(route = "ApplicationManage") { ApplicationManage(navCtrl, pkgName, dialogStatus) }
composable(route = "ApplicationManage") { ApplicationManage(navCtrl, dialogStatus) }
composable(route = "UserRestriction") { UserRestriction(navCtrl) }
composable(route = "UserManage") { UserManage(navCtrl) }
composable(route = "Password") { Password(navCtrl) }
composable(route = "AppSetting") { AppSetting(navCtrl, materialYou, blackTheme) }
composable(route = "Network") { Network(navCtrl) }
composable(route = "PackageSelector") { PackageSelector(navCtrl, pkgName) }
composable(route = "PackageSelector") { PackageSelector(navCtrl) }
}
LaunchedEffect(Unit) {
val profileInited = sharedPref.getBoolean("ManagedProfileActivated", false)
Expand All @@ -177,24 +176,26 @@ fun Home(materialYou:MutableState<Boolean>, blackTheme:MutableState<Boolean>) {
}

@Composable
private fun HomePage(navCtrl:NavHostController, pkgName: MutableState<String>) {
private fun HomePage(navCtrl:NavHostController) {
val context = LocalContext.current
val dpm = context.getDPM()
val receiver = context.getReceiver()
val sharedPref = LocalContext.current.getSharedPreferences("data", Context.MODE_PRIVATE)
val refreshStatus by dhizukuErrorStatus.collectAsState()
val sharedPref = context.getSharedPreferences("data", Context.MODE_PRIVATE)
var activateType by remember { mutableStateOf("") }
val deviceAdmin = context.isDeviceAdmin
val deviceOwner = context.isDeviceOwner
val profileOwner = context.isProfileOwner
val refreshStatus by dhizukuErrorStatus.collectAsState()
LaunchedEffect(refreshStatus) {
activateType = if(sharedPref.getBoolean("dhizuku", false)) context.getString(R.string.dhizuku) + " - " else ""
activateType += context.getString(
if(context.isDeviceOwner) { R.string.device_owner }
else if(context.isProfileOwner) {
if(deviceOwner) { R.string.device_owner }
else if(profileOwner) {
if(VERSION.SDK_INT >= 24 && dpm.isManagedProfile(receiver)) R.string.work_profile_owner else R.string.profile_owner
}
else if(context.isDeviceAdmin) R.string.device_admin else R.string.click_to_activate
else if(deviceAdmin) R.string.device_admin else R.string.click_to_activate
)
}
LaunchedEffect(Unit) { pkgName.value = "" }
Column(modifier = Modifier.background(colorScheme.background).statusBarsPadding().verticalScroll(rememberScrollState())) {
Spacer(Modifier.padding(vertical = 25.dp))
Text(
Expand All @@ -214,14 +215,14 @@ private fun HomePage(navCtrl:NavHostController, pkgName: MutableState<String>) {
) {
Spacer(modifier = Modifier.padding(start = 22.dp))
Icon(
painter = painterResource(if(context.isDeviceAdmin) R.drawable.check_circle_fill1 else R.drawable.block_fill0),
painter = painterResource(if(deviceAdmin) R.drawable.check_circle_fill1 else R.drawable.block_fill0),
contentDescription = null,
tint = colorScheme.onPrimary
)
Spacer(modifier = Modifier.padding(start = 10.dp))
Column {
Text(
text = stringResource(if(context.isDeviceAdmin) R.string.activated else R.string.deactivated),
text = stringResource(if(deviceAdmin) R.string.activated else R.string.deactivated),
style = typography.headlineSmall,
color = colorScheme.onPrimary,
modifier = Modifier.padding(bottom = 2.dp)
Expand All @@ -230,17 +231,17 @@ private fun HomePage(navCtrl:NavHostController, pkgName: MutableState<String>) {
}
}
HomePageItem(R.string.system_manage, R.drawable.mobile_phone_fill0, "SystemManage", navCtrl)
if(context.isDeviceOwner || context.isProfileOwner) { HomePageItem(R.string.network, R.drawable.wifi_fill0, "Network", navCtrl) }
if(deviceOwner || profileOwner) { HomePageItem(R.string.network, R.drawable.wifi_fill0, "Network", navCtrl) }
if(
(VERSION.SDK_INT < 24 && !context.isDeviceOwner) || (
(VERSION.SDK_INT < 24 && !deviceOwner) || (
VERSION.SDK_INT >= 24 && (dpm.isProvisioningAllowed(DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE) ||
(context.isProfileOwner && dpm.isManagedProfile(receiver)))
(profileOwner && dpm.isManagedProfile(receiver)))
)
) {
HomePageItem(R.string.work_profile, R.drawable.work_fill0, "ManagedProfile", navCtrl)
}
HomePageItem(R.string.app_manager, R.drawable.apps_fill0, "ApplicationManage", navCtrl)
if(VERSION.SDK_INT >= 24 && (context.isProfileOwner || context.isDeviceOwner)) {
if(VERSION.SDK_INT >= 24 && (profileOwner || deviceOwner)) {
HomePageItem(R.string.user_restrict, R.drawable.person_off, "UserRestriction", navCtrl)
}
HomePageItem(R.string.user_manager,R.drawable.manage_accounts_fill0,"UserManage", navCtrl)
Expand Down
14 changes: 8 additions & 6 deletions app/src/main/java/com/bintianqi/owndroid/PkgSelector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
Expand All @@ -55,6 +54,7 @@ import com.bintianqi.owndroid.ui.NavIcon
import com.google.accompanist.drawablepainter.rememberDrawablePainter
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

Expand All @@ -67,9 +67,11 @@ private data class PkgInfo(

private val pkgs = mutableListOf<PkgInfo>()

val selectedPackage = MutableStateFlow("")

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun PackageSelector(navCtrl: NavHostController, pkgName: MutableState<String>) {
fun PackageSelector(navCtrl: NavHostController) {
val context = LocalContext.current
val pm = context.packageManager
val apps = pm.getInstalledApplications(0)
Expand Down Expand Up @@ -182,10 +184,10 @@ fun PackageSelector(navCtrl: NavHostController, pkgName: MutableState<String>) {
if(system == it.system) {
if(search != "") {
if(it.pkgName.contains(search, ignoreCase = true) || it.label.contains(search, ignoreCase = true)) {
PackageItem(it, navCtrl, pkgName)
PackageItem(it, navCtrl)
}
} else {
PackageItem(it, navCtrl, pkgName)
PackageItem(it, navCtrl)
}
}
}
Expand All @@ -204,15 +206,15 @@ fun PackageSelector(navCtrl: NavHostController, pkgName: MutableState<String>) {
}

@Composable
private fun PackageItem(pkg: PkgInfo, navCtrl: NavHostController, pkgName: MutableState<String>) {
private fun PackageItem(pkg: PkgInfo, navCtrl: NavHostController) {
val focusMgr = LocalFocusManager.current
Row(
verticalAlignment = Alignment.CenterVertically,
modifier = Modifier
.fillMaxWidth()
.clickable{
pkgName.value = pkg.pkgName
focusMgr.clearFocus()
selectedPackage.value = pkg.pkgName
navCtrl.navigateUp()
}
.padding(vertical = 6.dp)
Expand Down
Loading

0 comments on commit 439a75b

Please sign in to comment.