From 66827ab7de20b42b96d3ec02b32215a96a9c07c1 Mon Sep 17 00:00:00 2001 From: weishu Date: Sat, 3 Jun 2023 17:17:19 +0800 Subject: [PATCH] manager: show profile info in superuser list --- manager/app/src/main/cpp/jni.cc | 4 ++ .../me/weishu/kernelsu/ui/screen/SuperUser.kt | 41 ++++++++++++++++++- .../ui/viewmodel/SuperUserViewModel.kt | 30 +++++++++----- 3 files changed, 64 insertions(+), 11 deletions(-) diff --git a/manager/app/src/main/cpp/jni.cc b/manager/app/src/main/cpp/jni.cc index c66edd33..154cc46a 100644 --- a/manager/app/src/main/cpp/jni.cc +++ b/manager/app/src/main/cpp/jni.cc @@ -111,6 +111,10 @@ Java_me_weishu_kernelsu_Natives_getAppProfile(JNIEnv *env, jobject, jstring pkg, strcpy(profile.key, key); profile.current_uid = uid; + // set default value, don't allow root and use default profile! + profile.allow_su = false; + profile.non_root_profile.use_default = true; + if (!get_app_profile(key, &profile)) { return nullptr; } diff --git a/manager/app/src/main/java/me/weishu/kernelsu/ui/screen/SuperUser.kt b/manager/app/src/main/java/me/weishu/kernelsu/ui/screen/SuperUser.kt index a5180beb..eb85a52d 100644 --- a/manager/app/src/main/java/me/weishu/kernelsu/ui/screen/SuperUser.kt +++ b/manager/app/src/main/java/me/weishu/kernelsu/ui/screen/SuperUser.kt @@ -1,9 +1,11 @@ package me.weishu.kernelsu.ui.screen +import androidx.compose.foundation.background import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.* import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items +import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.ExperimentalMaterialApi import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.MoreVert @@ -14,9 +16,12 @@ import androidx.compose.material3.* import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource +import androidx.compose.ui.text.TextStyle import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp import androidx.lifecycle.viewmodel.compose.viewModel import coil.compose.AsyncImage import coil.request.ImageRequest @@ -119,6 +124,7 @@ fun SuperUserScreen(navigator: DestinationsNavigator) { } } +@OptIn(ExperimentalLayoutApi::class) @Composable private fun AppItem( app: SuperUserViewModel.AppInfo, @@ -127,7 +133,19 @@ private fun AppItem( ListItem( modifier = Modifier.clickable(onClick = onClickListener), headlineContent = { Text(app.label) }, - supportingContent = { Text(app.packageName) }, + supportingContent = { + Column { + Text(app.packageName) + FlowRow { + if (app.allowSu) { + LabelText(label = "ROOT") + } + if (app.hasCustomProfile) { + LabelText(label = "CUSTOM") + } + } + } + }, leadingContent = { AsyncImage( model = ImageRequest.Builder(LocalContext.current) @@ -143,3 +161,24 @@ private fun AppItem( }, ) } + +@Composable +fun LabelText(label: String) { + Box( + modifier = Modifier + .padding(top = 4.dp, end = 4.dp) + .background( + Color.Black, + shape = RoundedCornerShape(4.dp) + ) + ) { + Text( + text = label, + modifier = Modifier.padding(vertical = 2.dp, horizontal = 5.dp), + style = TextStyle( + fontSize = 8.sp, + color = Color.White, + ) + ) + } +} \ No newline at end of file diff --git a/manager/app/src/main/java/me/weishu/kernelsu/ui/viewmodel/SuperUserViewModel.kt b/manager/app/src/main/java/me/weishu/kernelsu/ui/viewmodel/SuperUserViewModel.kt index 84b3a562..41bc1a9d 100644 --- a/manager/app/src/main/java/me/weishu/kernelsu/ui/viewmodel/SuperUserViewModel.kt +++ b/manager/app/src/main/java/me/weishu/kernelsu/ui/viewmodel/SuperUserViewModel.kt @@ -40,13 +40,27 @@ class SuperUserViewModel : ViewModel() { data class AppInfo( val label: String, val packageInfo: PackageInfo, - val onAllowList: Boolean, - val onDenyList: Boolean, + val profile: Natives.Profile?, ) : Parcelable { val packageName: String get() = packageInfo.packageName val uid: Int get() = packageInfo.applicationInfo.uid + + val allowSu: Boolean + get() = profile != null && profile.allowSu + val hasCustomProfile: Boolean + get() { + if (profile == null) { + return false + } + + return if (profile.allowSu) { + !profile.rootUseDefault + } else { + !profile.nonRootUseDefault + } + } } var search by mutableStateOf("") @@ -57,8 +71,8 @@ class SuperUserViewModel : ViewModel() { private val sortedList by derivedStateOf { val comparator = compareBy { when { - it.onAllowList -> 0 - it.onDenyList -> 1 + it.allowSu -> 0 + it.hasCustomProfile -> 1 else -> 2 } }.then(compareBy(Collator.getInstance(Locale.getDefault()), AppInfo::label)) @@ -116,10 +130,6 @@ class SuperUserViewModel : ViewModel() { withContext(Dispatchers.IO) { val pm = ksuApp.packageManager - val allowList = Natives.allowList.toSet() - val denyList = Natives.denyList.toSet() - Log.i(TAG, "allowList: $allowList") - Log.i(TAG, "denyList: $denyList") val start = SystemClock.elapsedRealtime() val binder = result.first @@ -134,11 +144,11 @@ class SuperUserViewModel : ViewModel() { apps = packages.map { val appInfo = it.applicationInfo val uid = appInfo.uid + val profile = Natives.getAppProfile(it.packageName, uid) AppInfo( label = appInfo.loadLabel(pm).toString(), packageInfo = it, - onAllowList = uid in allowList, - onDenyList = uid in denyList + profile = profile, ) }.filter { it.packageName != ksuApp.packageName } Log.i(TAG, "load cost: ${SystemClock.elapsedRealtime() - start}")