From 5941fa1ec7360c66fa4d851cab94b0792b613f96 Mon Sep 17 00:00:00 2001 From: ShirkNeko <109797057+ShirkNeko@users.noreply.github.com> Date: Sun, 20 Apr 2025 22:30:23 +0800 Subject: [PATCH] manager: hide root-related features if kernelsu version null (#71) Related PR: tiann#2483 Also attempting to address this: tiann#2483 (comment) Co-authored-by: rsuntk Co-authored-by: ShirkNeko <109797057+ShirkNeko@users.noreply.github.com> Signed-off-by: ShirkNeko <109797057+ShirkNeko@users.noreply.github.com> --- .../src/main/java/zako/zako/zako/Natives.kt | 8 +++ .../java/zako/zako/zako/ui/screen/Home.kt | 32 +++++---- .../zako/zako/zako/ui/screen/MoreSettings.kt | 26 ++++--- .../java/zako/zako/zako/ui/screen/Settings.kt | 71 +++++++++++-------- 4 files changed, 83 insertions(+), 54 deletions(-) diff --git a/manager/app/src/main/java/zako/zako/zako/Natives.kt b/manager/app/src/main/java/zako/zako/zako/Natives.kt index 42f4ee95..b56f64ab 100644 --- a/manager/app/src/main/java/zako/zako/zako/Natives.kt +++ b/manager/app/src/main/java/zako/zako/zako/Natives.kt @@ -90,6 +90,14 @@ object Natives { fun requireNewKernel(): Boolean { return version < MINIMAL_SUPPORTED_KERNEL } + + fun isKsuValid(pkgName: String?): Boolean { + if (becomeManager(pkgName)) { + return true + } else { + return false + } + } @Immutable @Parcelize diff --git a/manager/app/src/main/java/zako/zako/zako/ui/screen/Home.kt b/manager/app/src/main/java/zako/zako/zako/ui/screen/Home.kt index b7739a03..e2d77ecb 100644 --- a/manager/app/src/main/java/zako/zako/zako/ui/screen/Home.kt +++ b/manager/app/src/main/java/zako/zako/zako/ui/screen/Home.kt @@ -283,22 +283,27 @@ private fun TopBar( } var showDropdown by remember { mutableStateOf(false) } - IconButton(onClick = { showDropdown = true }) { - Icon(Icons.Filled.Refresh, stringResource(R.string.reboot)) - DropdownMenu(expanded = showDropdown, onDismissRequest = { showDropdown = false } - ) { + if (Natives.isKsuValid(ksuApp.packageName)) { + IconButton(onClick = { showDropdown = true }) { + Icon(Icons.Filled.Refresh, stringResource(R.string.reboot)) + DropdownMenu( + expanded = showDropdown, + onDismissRequest = { showDropdown = false } + ) { - RebootDropdownItem(id = R.string.reboot) + RebootDropdownItem(id = R.string.reboot) - val pm = LocalContext.current.getSystemService(Context.POWER_SERVICE) as PowerManager? - @Suppress("DEPRECATION") - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && pm?.isRebootingUserspaceSupported == true) { - RebootDropdownItem(id = R.string.reboot_userspace, reason = "userspace") + val pm = + LocalContext.current.getSystemService(Context.POWER_SERVICE) as PowerManager? + @Suppress("DEPRECATION") + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && pm?.isRebootingUserspaceSupported == true) { + RebootDropdownItem(id = R.string.reboot_userspace, reason = "userspace") + } + RebootDropdownItem(id = R.string.reboot_recovery, reason = "recovery") + RebootDropdownItem(id = R.string.reboot_bootloader, reason = "bootloader") + RebootDropdownItem(id = R.string.reboot_download, reason = "download") + RebootDropdownItem(id = R.string.reboot_edl, reason = "edl") } - RebootDropdownItem(id = R.string.reboot_recovery, reason = "recovery") - RebootDropdownItem(id = R.string.reboot_bootloader, reason = "bootloader") - RebootDropdownItem(id = R.string.reboot_download, reason = "download") - RebootDropdownItem(id = R.string.reboot_edl, reason = "edl") } } }, @@ -307,6 +312,7 @@ private fun TopBar( ) } + @Composable private fun StatusCard( kernelVersion: KernelVersion, diff --git a/manager/app/src/main/java/zako/zako/zako/ui/screen/MoreSettings.kt b/manager/app/src/main/java/zako/zako/zako/ui/screen/MoreSettings.kt index 8f7b2e00..6629dbcd 100644 --- a/manager/app/src/main/java/zako/zako/zako/ui/screen/MoreSettings.kt +++ b/manager/app/src/main/java/zako/zako/zako/ui/screen/MoreSettings.kt @@ -40,6 +40,7 @@ import zako.zako.zako.ui.theme.* import zako.zako.zako.ui.util.* import androidx.core.content.edit import zako.zako.zako.R +import zako.zako.zako.* fun saveCardConfig(context: Context) { @@ -175,6 +176,7 @@ fun MoreSettingsScreen(navigator: DestinationsNavigator) { ) var showThemeColorDialog by remember { mutableStateOf(false) } + val ksuIsValid = Natives.isKsuValid(ksuApp.packageName) // 图片选择器 val pickImageLauncher = rememberLauncherForActivityResult( @@ -208,17 +210,19 @@ fun MoreSettingsScreen(navigator: DestinationsNavigator) { .padding(top = 12.dp) ) { // SELinux 开关 - SwitchItem( - icon = Icons.Filled.Security, - title = stringResource(R.string.selinux), - summary = if (selinuxEnabled) - stringResource(R.string.selinux_enabled) else - stringResource(R.string.selinux_disabled), - checked = selinuxEnabled - ) { enabled -> - val command = if (enabled) "setenforce 1" else "setenforce 0" - Shell.getShell().newJob().add(command).exec().let { result -> - if (result.isSuccess) selinuxEnabled = enabled + if (ksuIsValid) { + SwitchItem( + icon = Icons.Filled.Security, + title = stringResource(R.string.selinux), + summary = if (selinuxEnabled) + stringResource(R.string.selinux_enabled) else + stringResource(R.string.selinux_disabled), + checked = selinuxEnabled + ) { enabled -> + val command = if (enabled) "setenforce 1" else "setenforce 0" + Shell.getShell().newJob().add(command).exec().let { result -> + if (result.isSuccess) selinuxEnabled = enabled + } } } diff --git a/manager/app/src/main/java/zako/zako/zako/ui/screen/Settings.kt b/manager/app/src/main/java/zako/zako/zako/ui/screen/Settings.kt index 18b020e3..670aaea6 100644 --- a/manager/app/src/main/java/zako/zako/zako/ui/screen/Settings.kt +++ b/manager/app/src/main/java/zako/zako/zako/ui/screen/Settings.kt @@ -49,6 +49,7 @@ import kotlinx.coroutines.withContext import zako.zako.zako.BuildConfig import zako.zako.zako.Natives import zako.zako.zako.R +import zako.zako.zako.* import zako.zako.zako.ui.component.* import zako.zako.zako.ui.theme.* import zako.zako.zako.ui.util.LocalSnackbarHost @@ -68,6 +69,7 @@ fun SettingScreen(navigator: DestinationsNavigator) { // region 界面基础设置 val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior(rememberTopAppBarState()) val snackBarHost = LocalSnackbarHost.current + val ksuIsValid = Natives.isKsuValid(ksuApp.packageName) // endregion Scaffold( @@ -115,6 +117,7 @@ fun SettingScreen(navigator: DestinationsNavigator) { // region 配置项列表 // 配置文件模板入口 val profileTemplate = stringResource(id = R.string.settings_profile_template) + if (ksuIsValid) { ListItem( leadingContent = { Icon(Icons.Filled.Fence, profileTemplate) }, headlineContent = { Text(profileTemplate) }, @@ -123,34 +126,40 @@ fun SettingScreen(navigator: DestinationsNavigator) { navigator.navigate(AppProfileTemplateScreenDestination) } ) + } // 卸载模块开关 var umountChecked by rememberSaveable { mutableStateOf(Natives.isDefaultUmountModules()) } - SwitchItem( - icon = Icons.Filled.FolderDelete, - title = stringResource(id = R.string.settings_umount_modules_default), - summary = stringResource(id = R.string.settings_umount_modules_default_summary), - checked = umountChecked - ) { - if (Natives.setDefaultUmountModules(it)) { - umountChecked = it - } + + if (ksuIsValid) { + SwitchItem( + icon = Icons.Filled.FolderDelete, + title = stringResource(id = R.string.settings_umount_modules_default), + summary = stringResource(id = R.string.settings_umount_modules_default_summary), + checked = umountChecked + ) { + if (Natives.setDefaultUmountModules(it)) { + umountChecked = it + } + } } // SU 禁用开关(仅在兼容版本显示) - if (Natives.version >= Natives.MINIMAL_SUPPORTED_SU_COMPAT) { - var isSuDisabled by rememberSaveable { - mutableStateOf(!Natives.isSuEnabled()) - } - SwitchItem( - icon = Icons.Filled.RemoveModerator, - title = stringResource(id = R.string.settings_disable_su), - summary = stringResource(id = R.string.settings_disable_su_summary), - checked = isSuDisabled, - ) { checked -> - val shouldEnable = !checked - if (Natives.setSuEnabled(shouldEnable)) { - isSuDisabled = !shouldEnable + if (ksuIsValid) { + if (Natives.version >= Natives.MINIMAL_SUPPORTED_SU_COMPAT) { + var isSuDisabled by rememberSaveable { + mutableStateOf(!Natives.isSuEnabled()) + } + SwitchItem( + icon = Icons.Filled.RemoveModerator, + title = stringResource(id = R.string.settings_disable_su), + summary = stringResource(id = R.string.settings_disable_su_summary), + checked = isSuDisabled, + ) { checked -> + val shouldEnable = !checked + if (Natives.setSuEnabled(shouldEnable)) { + isSuDisabled = !shouldEnable + } } } } @@ -179,14 +188,16 @@ fun SettingScreen(navigator: DestinationsNavigator) { prefs.getBoolean("enable_web_debugging", false) ) } - SwitchItem( - icon = Icons.Filled.DeveloperMode, - title = stringResource(id = R.string.enable_web_debugging), - summary = stringResource(id = R.string.enable_web_debugging_summary), - checked = enableWebDebugging - ) { - prefs.edit { putBoolean("enable_web_debugging", it) } - enableWebDebugging = it + if (Natives.isKsuValid(ksuApp.packageName)) { + SwitchItem( + icon = Icons.Filled.DeveloperMode, + title = stringResource(id = R.string.enable_web_debugging), + summary = stringResource(id = R.string.enable_web_debugging_summary), + checked = enableWebDebugging + ) { + prefs.edit { putBoolean("enable_web_debugging", it) } + enableWebDebugging = it + } } // 更多设置 val newButtonTitle = stringResource(id = R.string.more_settings)