Skip to content
This repository has been archived by the owner on Jul 2, 2024. It is now read-only.

Commit

Permalink
Add Logcat
Browse files Browse the repository at this point in the history
  • Loading branch information
SanmerDev committed Mar 9, 2024
1 parent 2b5f39f commit 6b9a8c2
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 2 deletions.
56 changes: 56 additions & 0 deletions app/src/main/kotlin/com/sanmer/mrepo/app/Logcat.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.sanmer.mrepo.app

import android.content.Context
import android.net.Uri
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import timber.log.Timber
import java.time.LocalDateTime

object Logcat {
val logfile get() = "MRepo_${LocalDateTime.now()}.log"

private suspend fun dump(context: Context) = withContext(Dispatchers.IO) {
val uid = context.applicationInfo.uid
val command = arrayOf(
"logcat",
"-d",
"--uid=${uid}"
)

try {
val process = Runtime.getRuntime().exec(command)

val result = process.inputStream.use { stream ->
stream.reader().readLines()
.filterNot { it.startsWith("------") }
.joinToString("\n")
}

process.waitFor()

result.trim()
} catch (e: Exception) {
""
}
}

suspend fun write(
context: Context,
writer: (ByteArray) -> Unit
) = withContext(Dispatchers.IO) {
val logs = dump(context)
writer(logs.toByteArray())
}

suspend fun writeTo(context: Context, uri: Uri) = withContext(Dispatchers.IO) {
runCatching {
val cr = context.contentResolver
cr.openOutputStream(uri)?.use {
write(context, it::write)
}
}.onFailure {
Timber.d(it)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.sanmer.mrepo.ui.screens.settings

import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxWidth
Expand All @@ -9,22 +11,28 @@ import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.SnackbarDuration
import androidx.compose.material3.SnackbarHost
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.TopAppBarScrollBehavior
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavController
import com.sanmer.mrepo.BuildConfig
import com.sanmer.mrepo.R
import com.sanmer.mrepo.app.Logcat
import com.sanmer.mrepo.datastore.WorkingMode
import com.sanmer.mrepo.ui.component.SettingNormalItem
import com.sanmer.mrepo.ui.component.TopAppBarTitle
Expand All @@ -35,24 +43,55 @@ import com.sanmer.mrepo.ui.screens.settings.items.RootItem
import com.sanmer.mrepo.ui.utils.navigateSingleTopTo
import com.sanmer.mrepo.ui.utils.none
import com.sanmer.mrepo.viewmodel.SettingsViewModel
import kotlinx.coroutines.launch

@Composable
fun SettingsScreen(
navController: NavController,
viewModel: SettingsViewModel = hiltViewModel()
) {
val userPreferences = LocalUserPreferences.current

val scrollBehavior = TopAppBarDefaults.pinnedScrollBehavior()
val scope = rememberCoroutineScope()
val snackbarHostState = remember { SnackbarHostState() }

val context = LocalContext.current
val launcher = rememberLauncherForActivityResult(
ActivityResultContracts.CreateDocument("*/*")
) { uri ->
if (uri == null) return@rememberLauncherForActivityResult

scope.launch {
Logcat.writeTo(context, uri)
.onSuccess {
val message = context.getString(R.string.install_logs_saved)
snackbarHostState.showSnackbar(
message = message,
duration = SnackbarDuration.Short
)
}.onFailure {
val message = context.getString(
R.string.install_logs_save_failed,
it.message ?: context.getString(R.string.unknown_error)
)
snackbarHostState.showSnackbar(
message = message,
duration = SnackbarDuration.Short
)
}
}
}

Scaffold(
modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection),
topBar = {
TopBar(
exportLog = { launcher.launch(Logcat.logfile) },
isRoot = userPreferences.isRoot,
scrollBehavior = scrollBehavior
)
},
snackbarHost = { SnackbarHost(snackbarHostState) },
contentWindowInsets = WindowInsets.none
) { innerPadding ->
Column(
Expand Down Expand Up @@ -115,15 +154,24 @@ fun SettingsScreen(

@Composable
private fun TopBar(
exportLog: () -> Unit,
isRoot: Boolean,
scrollBehavior: TopAppBarScrollBehavior
) = TopAppBar(
title = {
TopAppBarTitle(text = stringResource(id = R.string.page_settings))
},
actions = {
var expanded by remember { mutableStateOf(false) }
IconButton(
onClick = exportLog
) {
Icon(
painter = painterResource(id = R.drawable.bug),
contentDescription = null
)
}

var expanded by remember { mutableStateOf(false) }
IconButton(
onClick = { expanded = true },
enabled = isRoot
Expand Down
60 changes: 60 additions & 0 deletions app/src/main/res/drawable/bug.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:pathData="M9,9v-1a3,3 0,0 1,6 0v1"
android:strokeLineJoin="round"
android:strokeWidth="2"
android:strokeColor="#ffff"
android:strokeLineCap="round"/>
<path
android:pathData="M8,9h8a6,6 0,0 1,1 3v3a5,5 0,0 1,-10 0v-3a6,6 0,0 1,1 -3"
android:strokeLineJoin="round"
android:strokeWidth="2"
android:strokeColor="#ffff"
android:strokeLineCap="round"/>
<path
android:pathData="M3,13l4,0"
android:strokeLineJoin="round"
android:strokeWidth="2"
android:strokeColor="#ffff"
android:strokeLineCap="round"/>
<path
android:pathData="M17,13l4,0"
android:strokeLineJoin="round"
android:strokeWidth="2"
android:strokeColor="#ffff"
android:strokeLineCap="round"/>
<path
android:pathData="M12,20l0,-6"
android:strokeLineJoin="round"
android:strokeWidth="2"
android:strokeColor="#ffff"
android:strokeLineCap="round"/>
<path
android:pathData="M4,19l3.35,-2"
android:strokeLineJoin="round"
android:strokeWidth="2"
android:strokeColor="#ffff"
android:strokeLineCap="round"/>
<path
android:pathData="M20,19l-3.35,-2"
android:strokeLineJoin="round"
android:strokeWidth="2"
android:strokeColor="#ffff"
android:strokeLineCap="round"/>
<path
android:pathData="M4,7l3.75,2.4"
android:strokeLineJoin="round"
android:strokeWidth="2"
android:strokeColor="#ffff"
android:strokeLineCap="round"/>
<path
android:pathData="M20,7l-3.75,2.4"
android:strokeLineJoin="round"
android:strokeWidth="2"
android:strokeColor="#ffff"
android:strokeLineCap="round"/>
</vector>

0 comments on commit 6b9a8c2

Please sign in to comment.