manager: improve local LKM selection

Co-authored-by: YuKongA <70465933+YuKongA@users.noreply.github.com>
Co-authored-by: ShirkNeko <109797057+ShirkNeko@users.noreply.github.com>
This commit is contained in:
ShirkNeko
2025-11-03 11:39:42 +08:00
parent bfed2d700a
commit d1aa6c8beb
3 changed files with 73 additions and 38 deletions

View File

@@ -1,8 +1,10 @@
package com.sukisu.ultra.ui.screen
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.provider.OpenableColumns
import android.widget.Toast
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
@@ -211,7 +213,17 @@ fun InstallScreen(
) {
if (it.resultCode == Activity.RESULT_OK) {
it.data?.data?.let { uri ->
lkmSelection = LkmSelection.LkmUri(uri)
val isKo = isKoFile(context, uri)
if (isKo) {
lkmSelection = LkmSelection.LkmUri(uri)
} else {
lkmSelection = LkmSelection.KmiNone
Toast.makeText(
context,
context.getString(R.string.install_only_support_ko_file),
Toast.LENGTH_SHORT
).show()
}
}
}
}
@@ -228,7 +240,6 @@ fun InstallScreen(
topBar = {
TopBar(
onBack = { navigator.popBackStack() },
onLkmUpload = onLkmUpload,
scrollBehavior = scrollBehavior
)
},
@@ -268,32 +279,39 @@ fun InstallScreen(
.fillMaxWidth()
.padding(16.dp)
) {
(lkmSelection as? LkmSelection.LkmUri)?.let {
ElevatedCard(
colors = getCardColors(MaterialTheme.colorScheme.surfaceVariant),
elevation = getCardElevation(),
ElevatedCard(
colors = getCardColors(MaterialTheme.colorScheme.surfaceVariant),
elevation = getCardElevation(),
modifier = Modifier
.fillMaxWidth()
.padding(bottom = 12.dp),
) {
ListItem(
headlineContent = {
Text(stringResource(id = R.string.install_upload_lkm_file))
},
supportingContent = {
(lkmSelection as? LkmSelection.LkmUri)?.let {
Text(
stringResource(
id = R.string.selected_lkm,
it.uri.lastPathSegment ?: "(file)"
)
)
}
},
leadingContent = {
Icon(
Icons.AutoMirrored.Filled.Input,
contentDescription = null
)
},
modifier = Modifier
.fillMaxWidth()
.padding(bottom = 12.dp)
.clip(MaterialTheme.shapes.medium)
.shadow(
elevation = cardElevation,
shape = MaterialTheme.shapes.medium,
spotColor = MaterialTheme.colorScheme.primary.copy(alpha = 0.1f)
)
) {
Text(
text = stringResource(
id = R.string.selected_lkm,
it.uri.lastPathSegment ?: "(file)"
),
style = MaterialTheme.typography.bodyMedium,
modifier = Modifier.padding(16.dp)
)
}
.clickable { onLkmUpload() }
)
}
(installMethod as? InstallMethod.HorizonKernel)?.let { method ->
(installMethod as? InstallMethod.HorizonKernel)?.let { method ->
if (method.slot != null) {
ElevatedCard(
colors = getCardColors(MaterialTheme.colorScheme.surfaceVariant),
@@ -952,7 +970,6 @@ fun rememberSelectKmiDialog(onSelected: (String?) -> Unit): DialogHandle {
@Composable
private fun TopBar(
onBack: () -> Unit = {},
onLkmUpload: () -> Unit = {},
scrollBehavior: TopAppBarScrollBehavior? = null
) {
val colorScheme = MaterialTheme.colorScheme
@@ -985,21 +1002,35 @@ private fun TopBar(
windowInsets = WindowInsets.safeDrawing.only(
WindowInsetsSides.Top + WindowInsetsSides.Horizontal
),
actions = {
IconButton(
modifier = Modifier.padding(end = 16.dp),
onClick = onLkmUpload
) {
Icon(
Icons.AutoMirrored.Filled.Input,
contentDescription = null
)
}
},
scrollBehavior = scrollBehavior
)
}
private fun isKoFile(context: Context, uri: Uri): Boolean {
val seg = uri.lastPathSegment ?: ""
if (seg.endsWith(".ko", ignoreCase = true)) return true
return try {
context.contentResolver.query(
uri,
arrayOf(OpenableColumns.DISPLAY_NAME),
null,
null,
null
)?.use { cursor ->
val idx = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)
if (idx != -1 && cursor.moveToFirst()) {
val name = cursor.getString(idx)
name?.endsWith(".ko", ignoreCase = true) == true
} else {
false
}
} ?: false
} catch (_: Throwable) {
false
}
}
@Preview
@Composable
fun SelectInstallPreview() {