Skip to content

Commit

Permalink
manager: Fix save log (#2170)
Browse files Browse the repository at this point in the history
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](1fb49d9)
directly)
  • Loading branch information
lightsummer233 authored Oct 31, 2024
1 parent f8310c4 commit b7b704e
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,8 @@ private fun AppMenuBox(packageName: String, content: @Composable () -> Unit) {
touchPoint = it
expanded = true
}
}) {
}
) {

content()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,6 @@ private fun TopBar(

@Composable
@Preview
fun SelectInstall_Preview() {
fun SelectInstallPreview() {
InstallScreen(EmptyDestinationsNavigator)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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),
Expand Down
80 changes: 36 additions & 44 deletions manager/app/src/main/java/me/weishu/kernelsu/ui/screen/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 = {
Expand All @@ -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 {
Expand All @@ -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) },
Expand Down Expand Up @@ -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(
Expand All @@ -256,7 +254,6 @@ fun SettingScreen(navigator: DestinationsNavigator) {

)
}

}
Box {
Column(
Expand All @@ -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(
Expand All @@ -305,16 +303,12 @@ fun SettingScreen(navigator: DestinationsNavigator) {
trim = LineHeightStyle.Trim.None
)
}

)
}

}
}
}
)


}

val shrink = stringResource(id = R.string.shrink_sparse_image)
Expand All @@ -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()
Expand All @@ -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)
Expand All @@ -353,7 +345,7 @@ fun SettingScreen(navigator: DestinationsNavigator) {
leadingContent = {
Icon(
Icons.Filled.ContactPage,
stringResource(id = R.string.about)
about
)
},
headlineContent = { Text(about) },
Expand Down
23 changes: 8 additions & 15 deletions manager/app/src/main/java/me/weishu/kernelsu/ui/util/Downloader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ inline fun <T> 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))
Expand Down
1 change: 1 addition & 0 deletions manager/app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,5 @@
<string name="flash_failed">刷写失败</string>
<string name="selected_lkm">选择的 LKM :%s</string>
<string name="save_log">保存日志</string>
<string name="log_saved">日志已保存</string>
</resources>
1 change: 1 addition & 0 deletions manager/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,5 @@
<string name="flash_failed">Flash failed</string>
<string name="selected_lkm">Selected LKM: %s</string>
<string name="save_log">Save logs</string>
<string name="log_saved">Logs saved</string>
</resources>
2 changes: 1 addition & 1 deletion manager/gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -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"
Expand Down

0 comments on commit b7b704e

Please sign in to comment.