From b7b704ede74a7a90c499888b7dfe43a771804ddc Mon Sep 17 00:00:00 2001 From: Light_summer <93428659+lightsummer233@users.noreply.github.com> Date: Thu, 31 Oct 2024 18:06:11 +0800 Subject: [PATCH] manager: Fix save log (#2170) https://github.com/user-attachments/assets/69467e00-0af9-4d46-add8-e24e767462bd Use `ContextCompat` in `DownloadListener` Bump ksp to `2.0.21-1.0.26` Misc changes (See the [commit](https://github.com/tiann/KernelSU/commit/1fb49d918a1f5bb76d1e70eb848ede36b2292cd9) directly) --- .../weishu/kernelsu/ui/screen/AppProfile.kt | 3 +- .../me/weishu/kernelsu/ui/screen/Install.kt | 2 +- .../me/weishu/kernelsu/ui/screen/Module.kt | 5 +- .../me/weishu/kernelsu/ui/screen/Settings.kt | 80 +++++++++---------- .../me/weishu/kernelsu/ui/util/Downloader.kt | 23 ++---- .../java/me/weishu/kernelsu/ui/util/KsuCli.kt | 4 +- .../src/main/res/values-zh-rCN/strings.xml | 1 + manager/app/src/main/res/values/strings.xml | 1 + manager/gradle/libs.versions.toml | 2 +- 9 files changed, 53 insertions(+), 68 deletions(-) diff --git a/manager/app/src/main/java/me/weishu/kernelsu/ui/screen/AppProfile.kt b/manager/app/src/main/java/me/weishu/kernelsu/ui/screen/AppProfile.kt index a9976700505a..30e96e97aac3 100644 --- a/manager/app/src/main/java/me/weishu/kernelsu/ui/screen/AppProfile.kt +++ b/manager/app/src/main/java/me/weishu/kernelsu/ui/screen/AppProfile.kt @@ -326,7 +326,8 @@ private fun AppMenuBox(packageName: String, content: @Composable () -> Unit) { touchPoint = it expanded = true } - }) { + } + ) { content() diff --git a/manager/app/src/main/java/me/weishu/kernelsu/ui/screen/Install.kt b/manager/app/src/main/java/me/weishu/kernelsu/ui/screen/Install.kt index 01f7ce3012d9..aa4843295159 100644 --- a/manager/app/src/main/java/me/weishu/kernelsu/ui/screen/Install.kt +++ b/manager/app/src/main/java/me/weishu/kernelsu/ui/screen/Install.kt @@ -356,6 +356,6 @@ private fun TopBar( @Composable @Preview -fun SelectInstall_Preview() { +fun SelectInstallPreview() { InstallScreen(EmptyDestinationsNavigator) } \ No newline at end of file diff --git a/manager/app/src/main/java/me/weishu/kernelsu/ui/screen/Module.kt b/manager/app/src/main/java/me/weishu/kernelsu/ui/screen/Module.kt index 75bedce17fb7..a0d28f093189 100644 --- a/manager/app/src/main/java/me/weishu/kernelsu/ui/screen/Module.kt +++ b/manager/app/src/main/java/me/weishu/kernelsu/ui/screen/Module.kt @@ -131,9 +131,7 @@ fun ModuleScreen(navigator: DestinationsNavigator) { ) }, floatingActionButton = { - if (hideInstallButton) { - /* Empty */ - } else { + if (!hideInstallButton) { val moduleInstall = stringResource(id = R.string.module_install) val selectZipLauncher = rememberLauncherForActivityResult( contract = ActivityResultContracts.StartActivityForResult() @@ -162,7 +160,6 @@ fun ModuleScreen(navigator: DestinationsNavigator) { icon = { Icon(Icons.Filled.Add, moduleInstall) }, text = { Text(text = moduleInstall) }, ) - } }, contentWindowInsets = WindowInsets.safeDrawing.only(WindowInsetsSides.Top + WindowInsetsSides.Horizontal), diff --git a/manager/app/src/main/java/me/weishu/kernelsu/ui/screen/Settings.kt b/manager/app/src/main/java/me/weishu/kernelsu/ui/screen/Settings.kt index 42caaa853dd5..fe6b8786f335 100644 --- a/manager/app/src/main/java/me/weishu/kernelsu/ui/screen/Settings.kt +++ b/manager/app/src/main/java/me/weishu/kernelsu/ui/screen/Settings.kt @@ -4,6 +4,8 @@ import android.content.Context import android.content.Intent import android.net.Uri import android.widget.Toast +import androidx.activity.compose.rememberLauncherForActivityResult +import androidx.activity.result.contract.ActivityResultContracts import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Column @@ -35,6 +37,7 @@ import androidx.compose.material3.IconButton import androidx.compose.material3.ListItem import androidx.compose.material3.ModalBottomSheet import androidx.compose.material3.Scaffold +import androidx.compose.material3.SnackbarHost import androidx.compose.material3.Text import androidx.compose.material3.TopAppBar import androidx.compose.material3.TopAppBarDefaults @@ -83,9 +86,11 @@ import me.weishu.kernelsu.ui.component.SwitchItem import me.weishu.kernelsu.ui.component.rememberConfirmDialog import me.weishu.kernelsu.ui.component.rememberCustomDialog import me.weishu.kernelsu.ui.component.rememberLoadingDialog +import me.weishu.kernelsu.ui.util.LocalSnackbarHost import me.weishu.kernelsu.ui.util.getBugreportFile -import me.weishu.kernelsu.ui.util.getFileNameFromUri import me.weishu.kernelsu.ui.util.shrinkModules +import java.time.LocalDateTime +import java.time.format.DateTimeFormatter /** * @author weishu @@ -96,6 +101,7 @@ import me.weishu.kernelsu.ui.util.shrinkModules @Composable fun SettingScreen(navigator: DestinationsNavigator) { val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior(rememberTopAppBarState()) + val snackBarHost = LocalSnackbarHost.current Scaffold( topBar = { @@ -106,6 +112,7 @@ fun SettingScreen(navigator: DestinationsNavigator) { scrollBehavior = scrollBehavior ) }, + snackbarHost = { SnackbarHost(snackBarHost) }, contentWindowInsets = WindowInsets.safeDrawing.only(WindowInsetsSides.Top + WindowInsetsSides.Horizontal) ) { paddingValues -> val aboutDialog = rememberCustomDialog { @@ -124,6 +131,22 @@ fun SettingScreen(navigator: DestinationsNavigator) { val context = LocalContext.current val scope = rememberCoroutineScope() + val exportBugreportLauncher = rememberLauncherForActivityResult( + ActivityResultContracts.CreateDocument("application/gzip") + ) { uri: Uri? -> + if (uri == null) return@rememberLauncherForActivityResult + scope.launch(Dispatchers.IO) { + loadingDialog.show() + context.contentResolver.openOutputStream(uri)?.use { output -> + getBugreportFile(context).inputStream().use { + it.copyTo(output) + } + } + loadingDialog.hide() + snackBarHost.showSnackbar(context.getString(R.string.log_saved)) + } + } + val profileTemplate = stringResource(id = R.string.settings_profile_template) ListItem( leadingContent = { Icon(Icons.Filled.Fence, profileTemplate) }, @@ -208,35 +231,10 @@ fun SettingScreen(navigator: DestinationsNavigator) { modifier = Modifier .padding(16.dp) .clickable { - scope.launch { - val bugreport = loadingDialog.withLoading { - withContext(Dispatchers.IO) { - getBugreportFile(context) - } - } - - val uri: Uri = - FileProvider.getUriForFile( - context, - "${BuildConfig.APPLICATION_ID}.fileprovider", - bugreport - ) - val filename = getFileNameFromUri(context, uri) - val savefile = - Intent(Intent.ACTION_CREATE_DOCUMENT).apply { - addCategory(Intent.CATEGORY_OPENABLE) - type = "application/zip" - putExtra(Intent.EXTRA_STREAM, uri) - putExtra(Intent.EXTRA_TITLE, filename) - flags = Intent.FLAG_GRANT_READ_URI_PERMISSION - } - context.startActivity( - Intent.createChooser( - savefile, - context.getString(R.string.save_log) - ) - ) - } + val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH_mm") + val current = LocalDateTime.now().format(formatter) + exportBugreportLauncher.launch("KernelSU_bugreport_${current}.tar.gz") + showBottomsheet = false } ) { Icon( @@ -256,7 +254,6 @@ fun SettingScreen(navigator: DestinationsNavigator) { ) } - } Box { Column( @@ -277,10 +274,11 @@ fun SettingScreen(navigator: DestinationsNavigator) { bugreport ) - val shareIntent = Intent(Intent.ACTION_SEND) - shareIntent.putExtra(Intent.EXTRA_STREAM, uri) - shareIntent.setDataAndType(uri, "application/zip") - shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + val shareIntent = Intent(Intent.ACTION_SEND).apply { + putExtra(Intent.EXTRA_STREAM, uri) + setDataAndType(uri, "application/gzip") + addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + } context.startActivity( Intent.createChooser( @@ -305,16 +303,12 @@ fun SettingScreen(navigator: DestinationsNavigator) { trim = LineHeightStyle.Trim.None ) } - ) } - } } } ) - - } val shrink = stringResource(id = R.string.shrink_sparse_image) @@ -329,8 +323,7 @@ fun SettingScreen(navigator: DestinationsNavigator) { headlineContent = { Text(shrink) }, modifier = Modifier.clickable { scope.launch { - val result = - shrinkDialog.awaitConfirm(title = shrink, content = shrinkMessage) + val result = shrinkDialog.awaitConfirm(title = shrink, content = shrinkMessage) if (result == ConfirmResult.Confirmed) { loadingDialog.withLoading { shrinkModules() @@ -340,8 +333,7 @@ fun SettingScreen(navigator: DestinationsNavigator) { } ) - val lkmMode = - Natives.version >= Natives.MINIMAL_SUPPORTED_KERNEL_LKM && Natives.isLkmMode + val lkmMode = Natives.version >= Natives.MINIMAL_SUPPORTED_KERNEL_LKM && Natives.isLkmMode if (lkmMode) { UninstallItem(navigator) { loadingDialog.withLoading(it) @@ -353,7 +345,7 @@ fun SettingScreen(navigator: DestinationsNavigator) { leadingContent = { Icon( Icons.Filled.ContactPage, - stringResource(id = R.string.about) + about ) }, headlineContent = { Text(about) }, diff --git a/manager/app/src/main/java/me/weishu/kernelsu/ui/util/Downloader.kt b/manager/app/src/main/java/me/weishu/kernelsu/ui/util/Downloader.kt index 954cac50da30..bdf8496633fd 100644 --- a/manager/app/src/main/java/me/weishu/kernelsu/ui/util/Downloader.kt +++ b/manager/app/src/main/java/me/weishu/kernelsu/ui/util/Downloader.kt @@ -7,10 +7,10 @@ import android.content.Context import android.content.Intent import android.content.IntentFilter import android.net.Uri -import android.os.Build import android.os.Environment import androidx.compose.runtime.Composable import androidx.compose.runtime.DisposableEffect +import androidx.core.content.ContextCompat import me.weishu.kernelsu.ui.util.module.LatestVersionInfo /** @@ -26,8 +26,7 @@ fun download( onDownloaded: (Uri) -> Unit = {}, onDownloading: () -> Unit = {} ) { - val downloadManager = - context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager + val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager val query = DownloadManager.Query() query.setFilterByStatus(DownloadManager.STATUS_RUNNING or DownloadManager.STATUS_PAUSED or DownloadManager.STATUS_PENDING) @@ -130,18 +129,12 @@ fun DownloadListener(context: Context, onDownloaded: (Uri) -> Unit) { } } } - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - context.registerReceiver( - receiver, - IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE), - Context.RECEIVER_EXPORTED - ) - } else { - context.registerReceiver( - receiver, - IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE) - ) - } + ContextCompat.registerReceiver( + context, + receiver, + IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE), + ContextCompat.RECEIVER_EXPORTED + ) onDispose { context.unregisterReceiver(receiver) } diff --git a/manager/app/src/main/java/me/weishu/kernelsu/ui/util/KsuCli.kt b/manager/app/src/main/java/me/weishu/kernelsu/ui/util/KsuCli.kt index 32a94b7e1768..1a2416b280d2 100644 --- a/manager/app/src/main/java/me/weishu/kernelsu/ui/util/KsuCli.kt +++ b/manager/app/src/main/java/me/weishu/kernelsu/ui/util/KsuCli.kt @@ -51,10 +51,10 @@ inline fun withNewRootShell( return createRootShell(globalMnt).use(block) } -fun getFileNameFromUri(context: Context, uri: Uri): String? { +fun Uri.getFileName(context: Context): String? { var fileName: String? = null val contentResolver: ContentResolver = context.contentResolver - val cursor: Cursor? = contentResolver.query(uri, null, null, null, null) + val cursor: Cursor? = contentResolver.query(this, null, null, null, null) cursor?.use { if (it.moveToFirst()) { fileName = it.getString(it.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME)) diff --git a/manager/app/src/main/res/values-zh-rCN/strings.xml b/manager/app/src/main/res/values-zh-rCN/strings.xml index 950c69e5c257..d56b625a91ff 100644 --- a/manager/app/src/main/res/values-zh-rCN/strings.xml +++ b/manager/app/src/main/res/values-zh-rCN/strings.xml @@ -130,4 +130,5 @@ 刷写失败 选择的 LKM :%s 保存日志 + 日志已保存 \ No newline at end of file diff --git a/manager/app/src/main/res/values/strings.xml b/manager/app/src/main/res/values/strings.xml index e9d058e9e0bd..85ce47da2d8a 100644 --- a/manager/app/src/main/res/values/strings.xml +++ b/manager/app/src/main/res/values/strings.xml @@ -132,4 +132,5 @@ Flash failed Selected LKM: %s Save logs + Logs saved \ No newline at end of file diff --git a/manager/gradle/libs.versions.toml b/manager/gradle/libs.versions.toml index 42e9ce44741d..ef564f8c0644 100644 --- a/manager/gradle/libs.versions.toml +++ b/manager/gradle/libs.versions.toml @@ -1,7 +1,7 @@ [versions] agp = "8.7.1" kotlin = "2.0.21" -ksp = "2.0.21-1.0.25" +ksp = "2.0.21-1.0.26" compose-bom = "2024.10.00" lifecycle = "2.8.6" navigation = "2.8.3"