Skip to content

Commit b7b704e

Browse files
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](1fb49d9) directly)
1 parent f8310c4 commit b7b704e

File tree

9 files changed

+53
-68
lines changed

9 files changed

+53
-68
lines changed

manager/app/src/main/java/me/weishu/kernelsu/ui/screen/AppProfile.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,8 @@ private fun AppMenuBox(packageName: String, content: @Composable () -> Unit) {
326326
touchPoint = it
327327
expanded = true
328328
}
329-
}) {
329+
}
330+
) {
330331

331332
content()
332333

manager/app/src/main/java/me/weishu/kernelsu/ui/screen/Install.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,6 @@ private fun TopBar(
356356

357357
@Composable
358358
@Preview
359-
fun SelectInstall_Preview() {
359+
fun SelectInstallPreview() {
360360
InstallScreen(EmptyDestinationsNavigator)
361361
}

manager/app/src/main/java/me/weishu/kernelsu/ui/screen/Module.kt

+1-4
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,7 @@ fun ModuleScreen(navigator: DestinationsNavigator) {
131131
)
132132
},
133133
floatingActionButton = {
134-
if (hideInstallButton) {
135-
/* Empty */
136-
} else {
134+
if (!hideInstallButton) {
137135
val moduleInstall = stringResource(id = R.string.module_install)
138136
val selectZipLauncher = rememberLauncherForActivityResult(
139137
contract = ActivityResultContracts.StartActivityForResult()
@@ -162,7 +160,6 @@ fun ModuleScreen(navigator: DestinationsNavigator) {
162160
icon = { Icon(Icons.Filled.Add, moduleInstall) },
163161
text = { Text(text = moduleInstall) },
164162
)
165-
166163
}
167164
},
168165
contentWindowInsets = WindowInsets.safeDrawing.only(WindowInsetsSides.Top + WindowInsetsSides.Horizontal),

manager/app/src/main/java/me/weishu/kernelsu/ui/screen/Settings.kt

+36-44
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import android.content.Context
44
import android.content.Intent
55
import android.net.Uri
66
import android.widget.Toast
7+
import androidx.activity.compose.rememberLauncherForActivityResult
8+
import androidx.activity.result.contract.ActivityResultContracts
79
import androidx.compose.foundation.clickable
810
import androidx.compose.foundation.layout.Box
911
import androidx.compose.foundation.layout.Column
@@ -35,6 +37,7 @@ import androidx.compose.material3.IconButton
3537
import androidx.compose.material3.ListItem
3638
import androidx.compose.material3.ModalBottomSheet
3739
import androidx.compose.material3.Scaffold
40+
import androidx.compose.material3.SnackbarHost
3841
import androidx.compose.material3.Text
3942
import androidx.compose.material3.TopAppBar
4043
import androidx.compose.material3.TopAppBarDefaults
@@ -83,9 +86,11 @@ import me.weishu.kernelsu.ui.component.SwitchItem
8386
import me.weishu.kernelsu.ui.component.rememberConfirmDialog
8487
import me.weishu.kernelsu.ui.component.rememberCustomDialog
8588
import me.weishu.kernelsu.ui.component.rememberLoadingDialog
89+
import me.weishu.kernelsu.ui.util.LocalSnackbarHost
8690
import me.weishu.kernelsu.ui.util.getBugreportFile
87-
import me.weishu.kernelsu.ui.util.getFileNameFromUri
8891
import me.weishu.kernelsu.ui.util.shrinkModules
92+
import java.time.LocalDateTime
93+
import java.time.format.DateTimeFormatter
8994

9095
/**
9196
* @author weishu
@@ -96,6 +101,7 @@ import me.weishu.kernelsu.ui.util.shrinkModules
96101
@Composable
97102
fun SettingScreen(navigator: DestinationsNavigator) {
98103
val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior(rememberTopAppBarState())
104+
val snackBarHost = LocalSnackbarHost.current
99105

100106
Scaffold(
101107
topBar = {
@@ -106,6 +112,7 @@ fun SettingScreen(navigator: DestinationsNavigator) {
106112
scrollBehavior = scrollBehavior
107113
)
108114
},
115+
snackbarHost = { SnackbarHost(snackBarHost) },
109116
contentWindowInsets = WindowInsets.safeDrawing.only(WindowInsetsSides.Top + WindowInsetsSides.Horizontal)
110117
) { paddingValues ->
111118
val aboutDialog = rememberCustomDialog {
@@ -124,6 +131,22 @@ fun SettingScreen(navigator: DestinationsNavigator) {
124131
val context = LocalContext.current
125132
val scope = rememberCoroutineScope()
126133

134+
val exportBugreportLauncher = rememberLauncherForActivityResult(
135+
ActivityResultContracts.CreateDocument("application/gzip")
136+
) { uri: Uri? ->
137+
if (uri == null) return@rememberLauncherForActivityResult
138+
scope.launch(Dispatchers.IO) {
139+
loadingDialog.show()
140+
context.contentResolver.openOutputStream(uri)?.use { output ->
141+
getBugreportFile(context).inputStream().use {
142+
it.copyTo(output)
143+
}
144+
}
145+
loadingDialog.hide()
146+
snackBarHost.showSnackbar(context.getString(R.string.log_saved))
147+
}
148+
}
149+
127150
val profileTemplate = stringResource(id = R.string.settings_profile_template)
128151
ListItem(
129152
leadingContent = { Icon(Icons.Filled.Fence, profileTemplate) },
@@ -208,35 +231,10 @@ fun SettingScreen(navigator: DestinationsNavigator) {
208231
modifier = Modifier
209232
.padding(16.dp)
210233
.clickable {
211-
scope.launch {
212-
val bugreport = loadingDialog.withLoading {
213-
withContext(Dispatchers.IO) {
214-
getBugreportFile(context)
215-
}
216-
}
217-
218-
val uri: Uri =
219-
FileProvider.getUriForFile(
220-
context,
221-
"${BuildConfig.APPLICATION_ID}.fileprovider",
222-
bugreport
223-
)
224-
val filename = getFileNameFromUri(context, uri)
225-
val savefile =
226-
Intent(Intent.ACTION_CREATE_DOCUMENT).apply {
227-
addCategory(Intent.CATEGORY_OPENABLE)
228-
type = "application/zip"
229-
putExtra(Intent.EXTRA_STREAM, uri)
230-
putExtra(Intent.EXTRA_TITLE, filename)
231-
flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
232-
}
233-
context.startActivity(
234-
Intent.createChooser(
235-
savefile,
236-
context.getString(R.string.save_log)
237-
)
238-
)
239-
}
234+
val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd_HH_mm")
235+
val current = LocalDateTime.now().format(formatter)
236+
exportBugreportLauncher.launch("KernelSU_bugreport_${current}.tar.gz")
237+
showBottomsheet = false
240238
}
241239
) {
242240
Icon(
@@ -256,7 +254,6 @@ fun SettingScreen(navigator: DestinationsNavigator) {
256254

257255
)
258256
}
259-
260257
}
261258
Box {
262259
Column(
@@ -277,10 +274,11 @@ fun SettingScreen(navigator: DestinationsNavigator) {
277274
bugreport
278275
)
279276

280-
val shareIntent = Intent(Intent.ACTION_SEND)
281-
shareIntent.putExtra(Intent.EXTRA_STREAM, uri)
282-
shareIntent.setDataAndType(uri, "application/zip")
283-
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
277+
val shareIntent = Intent(Intent.ACTION_SEND).apply {
278+
putExtra(Intent.EXTRA_STREAM, uri)
279+
setDataAndType(uri, "application/gzip")
280+
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
281+
}
284282

285283
context.startActivity(
286284
Intent.createChooser(
@@ -305,16 +303,12 @@ fun SettingScreen(navigator: DestinationsNavigator) {
305303
trim = LineHeightStyle.Trim.None
306304
)
307305
}
308-
309306
)
310307
}
311-
312308
}
313309
}
314310
}
315311
)
316-
317-
318312
}
319313

320314
val shrink = stringResource(id = R.string.shrink_sparse_image)
@@ -329,8 +323,7 @@ fun SettingScreen(navigator: DestinationsNavigator) {
329323
headlineContent = { Text(shrink) },
330324
modifier = Modifier.clickable {
331325
scope.launch {
332-
val result =
333-
shrinkDialog.awaitConfirm(title = shrink, content = shrinkMessage)
326+
val result = shrinkDialog.awaitConfirm(title = shrink, content = shrinkMessage)
334327
if (result == ConfirmResult.Confirmed) {
335328
loadingDialog.withLoading {
336329
shrinkModules()
@@ -340,8 +333,7 @@ fun SettingScreen(navigator: DestinationsNavigator) {
340333
}
341334
)
342335

343-
val lkmMode =
344-
Natives.version >= Natives.MINIMAL_SUPPORTED_KERNEL_LKM && Natives.isLkmMode
336+
val lkmMode = Natives.version >= Natives.MINIMAL_SUPPORTED_KERNEL_LKM && Natives.isLkmMode
345337
if (lkmMode) {
346338
UninstallItem(navigator) {
347339
loadingDialog.withLoading(it)
@@ -353,7 +345,7 @@ fun SettingScreen(navigator: DestinationsNavigator) {
353345
leadingContent = {
354346
Icon(
355347
Icons.Filled.ContactPage,
356-
stringResource(id = R.string.about)
348+
about
357349
)
358350
},
359351
headlineContent = { Text(about) },

manager/app/src/main/java/me/weishu/kernelsu/ui/util/Downloader.kt

+8-15
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ import android.content.Context
77
import android.content.Intent
88
import android.content.IntentFilter
99
import android.net.Uri
10-
import android.os.Build
1110
import android.os.Environment
1211
import androidx.compose.runtime.Composable
1312
import androidx.compose.runtime.DisposableEffect
13+
import androidx.core.content.ContextCompat
1414
import me.weishu.kernelsu.ui.util.module.LatestVersionInfo
1515

1616
/**
@@ -26,8 +26,7 @@ fun download(
2626
onDownloaded: (Uri) -> Unit = {},
2727
onDownloading: () -> Unit = {}
2828
) {
29-
val downloadManager =
30-
context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
29+
val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
3130

3231
val query = DownloadManager.Query()
3332
query.setFilterByStatus(DownloadManager.STATUS_RUNNING or DownloadManager.STATUS_PAUSED or DownloadManager.STATUS_PENDING)
@@ -130,18 +129,12 @@ fun DownloadListener(context: Context, onDownloaded: (Uri) -> Unit) {
130129
}
131130
}
132131
}
133-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
134-
context.registerReceiver(
135-
receiver,
136-
IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE),
137-
Context.RECEIVER_EXPORTED
138-
)
139-
} else {
140-
context.registerReceiver(
141-
receiver,
142-
IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)
143-
)
144-
}
132+
ContextCompat.registerReceiver(
133+
context,
134+
receiver,
135+
IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE),
136+
ContextCompat.RECEIVER_EXPORTED
137+
)
145138
onDispose {
146139
context.unregisterReceiver(receiver)
147140
}

manager/app/src/main/java/me/weishu/kernelsu/ui/util/KsuCli.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ inline fun <T> withNewRootShell(
5151
return createRootShell(globalMnt).use(block)
5252
}
5353

54-
fun getFileNameFromUri(context: Context, uri: Uri): String? {
54+
fun Uri.getFileName(context: Context): String? {
5555
var fileName: String? = null
5656
val contentResolver: ContentResolver = context.contentResolver
57-
val cursor: Cursor? = contentResolver.query(uri, null, null, null, null)
57+
val cursor: Cursor? = contentResolver.query(this, null, null, null, null)
5858
cursor?.use {
5959
if (it.moveToFirst()) {
6060
fileName = it.getString(it.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME))

manager/app/src/main/res/values-zh-rCN/strings.xml

+1
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,5 @@
130130
<string name="flash_failed">刷写失败</string>
131131
<string name="selected_lkm">选择的 LKM :%s</string>
132132
<string name="save_log">保存日志</string>
133+
<string name="log_saved">日志已保存</string>
133134
</resources>

manager/app/src/main/res/values/strings.xml

+1
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,5 @@
132132
<string name="flash_failed">Flash failed</string>
133133
<string name="selected_lkm">Selected LKM: %s</string>
134134
<string name="save_log">Save logs</string>
135+
<string name="log_saved">Logs saved</string>
135136
</resources>

manager/gradle/libs.versions.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[versions]
22
agp = "8.7.1"
33
kotlin = "2.0.21"
4-
ksp = "2.0.21-1.0.25"
4+
ksp = "2.0.21-1.0.26"
55
compose-bom = "2024.10.00"
66
lifecycle = "2.8.6"
77
navigation = "2.8.3"

0 commit comments

Comments
 (0)