manager: Show working mode

This commit is contained in:
weishu
2024-03-19 11:49:04 +08:00
parent f41d73f7eb
commit 99847cb986
6 changed files with 61 additions and 14 deletions

View File

@@ -46,6 +46,12 @@ Java_me_weishu_kernelsu_Natives_isSafeMode(JNIEnv *env, jclass clazz) {
return is_safe_mode(); return is_safe_mode();
} }
extern "C"
JNIEXPORT jboolean JNICALL
Java_me_weishu_kernelsu_Natives_isLkmMode(JNIEnv *env, jclass clazz) {
return is_lkm_mode();
}
static void fillIntArray(JNIEnv *env, jobject list, int *data, int count) { static void fillIntArray(JNIEnv *env, jobject list, int *data, int count) {
auto cls = env->GetObjectClass(list); auto cls = env->GetObjectClass(list);
auto add = env->GetMethodID(cls, "add", "(Ljava/lang/Object;)Z"); auto add = env->GetMethodID(cls, "add", "(Ljava/lang/Object;)Z");

View File

@@ -47,10 +47,14 @@ bool become_manager(const char* pkg) {
return ksuctl(CMD_BECOME_MANAGER, param, nullptr); return ksuctl(CMD_BECOME_MANAGER, param, nullptr);
} }
// cache the result to avoid unnecessary syscall
static bool is_lkm;
int get_version() { int get_version() {
int32_t version = -1; int32_t version = -1;
if (ksuctl(CMD_GET_VERSION, &version, nullptr)) { int32_t lkm = 0;
return version; ksuctl(CMD_GET_VERSION, &version, &lkm);
if (!is_lkm && lkm != 0) {
is_lkm = true;
} }
return version; return version;
} }
@@ -63,6 +67,11 @@ bool is_safe_mode() {
return ksuctl(CMD_CHECK_SAFEMODE, nullptr, nullptr); return ksuctl(CMD_CHECK_SAFEMODE, nullptr, nullptr);
} }
bool is_lkm_mode() {
// you should call get_version first!
return is_lkm;
}
bool uid_should_umount(int uid) { bool uid_should_umount(int uid) {
bool should; bool should;
return ksuctl(CMD_IS_UID_SHOULD_UMOUNT, reinterpret_cast<void*>(uid), &should) && should; return ksuctl(CMD_IS_UID_SHOULD_UMOUNT, reinterpret_cast<void*>(uid), &should) && should;

View File

@@ -17,6 +17,8 @@ bool uid_should_umount(int uid);
bool is_safe_mode(); bool is_safe_mode();
bool is_lkm_mode();
#define KSU_APP_PROFILE_VER 2 #define KSU_APP_PROFILE_VER 2
#define KSU_MAX_PACKAGE_NAME 256 #define KSU_MAX_PACKAGE_NAME 256
// NGROUPS_MAX for Linux is 65535 generally, but we only supports 32 groups. // NGROUPS_MAX for Linux is 65535 generally, but we only supports 32 groups.

View File

@@ -18,6 +18,9 @@ object Natives {
// 11071: Fix the issue of failing to set a custom SELinux type. // 11071: Fix the issue of failing to set a custom SELinux type.
const val MINIMAL_SUPPORTED_KERNEL = 11071 const val MINIMAL_SUPPORTED_KERNEL = 11071
// 11640: Support query working mode, LKM or GKI
// when MINIMAL_SUPPORTED_KERNEL > 11640, we can remove this constant.
const val MINIMAL_SUPPORTED_KERNEL_LKM = 11640
const val KERNEL_SU_DOMAIN = "u:r:su:s0" const val KERNEL_SU_DOMAIN = "u:r:su:s0"
const val ROOT_UID = 0 const val ROOT_UID = 0
@@ -39,6 +42,9 @@ object Natives {
val isSafeMode: Boolean val isSafeMode: Boolean
external get external get
val isLkmMode: Boolean
external get
external fun uidShouldUmount(uid: Int): Boolean external fun uidShouldUmount(uid: Int): Boolean
/** /**

View File

@@ -64,8 +64,12 @@ fun HomeScreen(navigator: DestinationsNavigator) {
if (isManager) install() if (isManager) install()
} }
val ksuVersion = if (isManager) Natives.version else null val ksuVersion = if (isManager) Natives.version else null
val lkmMode =
ksuVersion?.let {
if (it >= Natives.MINIMAL_SUPPORTED_KERNEL_LKM) Natives.isLkmMode else null
}
StatusCard(kernelVersion, ksuVersion) { StatusCard(kernelVersion, ksuVersion, lkmMode) {
navigator.navigate(InstallScreenDestination) navigator.navigate(InstallScreenDestination)
} }
if (isManager && Natives.requireNewKernel()) { if (isManager && Natives.requireNewKernel()) {
@@ -142,7 +146,11 @@ fun RebootDropdownItem(@StringRes id: Int, reason: String = "") {
@OptIn(ExperimentalMaterial3Api::class) @OptIn(ExperimentalMaterial3Api::class)
@Composable @Composable
private fun TopBar(kernelVersion: KernelVersion, onInstallClick: () -> Unit, onSettingsClick: () -> Unit) { private fun TopBar(
kernelVersion: KernelVersion,
onInstallClick: () -> Unit,
onSettingsClick: () -> Unit
) {
TopAppBar(title = { Text(stringResource(R.string.app_name)) }, actions = { TopAppBar(title = { Text(stringResource(R.string.app_name)) }, actions = {
if (kernelVersion.isGKI()) { if (kernelVersion.isGKI()) {
IconButton(onClick = onInstallClick) { IconButton(onClick = onInstallClick) {
@@ -190,14 +198,18 @@ private fun TopBar(kernelVersion: KernelVersion, onInstallClick: () -> Unit, onS
} }
@Composable @Composable
private fun StatusCard(kernelVersion: KernelVersion, ksuVersion: Int?, onClickInstall: () -> Unit = {}) { private fun StatusCard(
kernelVersion: KernelVersion,
ksuVersion: Int?,
lkmMode: Boolean?,
onClickInstall: () -> Unit = {}
) {
ElevatedCard( ElevatedCard(
colors = CardDefaults.elevatedCardColors(containerColor = run { colors = CardDefaults.elevatedCardColors(containerColor = run {
if (ksuVersion != null) MaterialTheme.colorScheme.secondaryContainer if (ksuVersion != null) MaterialTheme.colorScheme.secondaryContainer
else MaterialTheme.colorScheme.errorContainer else MaterialTheme.colorScheme.errorContainer
}) })
) { ) {
val uriHandler = LocalUriHandler.current
Row(modifier = Modifier Row(modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.clickable { .clickable {
@@ -208,15 +220,24 @@ private fun StatusCard(kernelVersion: KernelVersion, ksuVersion: Int?, onClickIn
.padding(24.dp), verticalAlignment = Alignment.CenterVertically) { .padding(24.dp), verticalAlignment = Alignment.CenterVertically) {
when { when {
ksuVersion != null -> { ksuVersion != null -> {
val appendText = if (Natives.isSafeMode) { val safeMode = when {
" [${stringResource(id = R.string.safe_mode)}]" Natives.isSafeMode -> " [${stringResource(id = R.string.safe_mode)}]"
} else { else -> ""
""
} }
val workingMode = when (lkmMode) {
null -> ""
true -> " <LKM>"
else -> " <GKI>"
}
val workingText =
"${stringResource(id = R.string.home_working)}$workingMode$safeMode"
Icon(Icons.Outlined.CheckCircle, stringResource(R.string.home_working)) Icon(Icons.Outlined.CheckCircle, stringResource(R.string.home_working))
Column(Modifier.padding(start = 20.dp)) { Column(Modifier.padding(start = 20.dp)) {
Text( Text(
text = stringResource(R.string.home_working) + appendText, text = workingText,
style = MaterialTheme.typography.titleMedium style = MaterialTheme.typography.titleMedium
) )
Spacer(Modifier.height(4.dp)) Spacer(Modifier.height(4.dp))
@@ -396,9 +417,10 @@ fun getManagerVersion(context: Context): Pair<String, Int> {
@Composable @Composable
private fun StatusCardPreview() { private fun StatusCardPreview() {
Column { Column {
StatusCard(KernelVersion(5, 10, 101), 1) StatusCard(KernelVersion(5, 10, 101), 1, null)
StatusCard(KernelVersion(5, 10, 101), null) StatusCard(KernelVersion(5, 10, 101), 20000, true)
StatusCard(KernelVersion(4, 10, 101), null) StatusCard(KernelVersion(5, 10, 101), null, true)
StatusCard(KernelVersion(4, 10, 101), null, false)
} }
} }

View File

@@ -86,6 +86,8 @@ fun getBugreportFile(context: Context): File {
pw.println("KernelSU: $ksuKernel") pw.println("KernelSU: $ksuKernel")
val safeMode = Natives.isSafeMode val safeMode = Natives.isSafeMode
pw.println("SafeMode: $safeMode") pw.println("SafeMode: $safeMode")
val lkmMode = Natives.isLkmMode
pw.println("LKM: $lkmMode")
} }
// modules // modules