feat: add ability to share debug logs

This commit is contained in:
Ax333l
2024-07-03 13:54:37 +02:00
committed by oSumAtrIX
parent 0383bd74f7
commit feb0ca4cf3
3 changed files with 63 additions and 1 deletions

View File

@@ -2,6 +2,8 @@ package app.revanced.manager.ui.screen.settings
import android.app.ActivityManager
import android.os.Build
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
@@ -86,6 +88,15 @@ fun AdvancedSettingsScreen(
}
)
val exportDebugLogsLauncher =
rememberLauncherForActivityResult(ActivityResultContracts.CreateDocument("text/plain")) {
it?.let(vm::exportDebugLogs)
}
SettingsListItem(
headlineContent = stringResource(R.string.debug_logs_export),
modifier = Modifier.clickable { exportDebugLogsLauncher.launch(vm.debugLogFileName) }
)
GroupHeader(stringResource(R.string.patcher))
BooleanItem(
preference = vm.prefs.useProcessRuntime,

View File

@@ -1,21 +1,41 @@
package app.revanced.manager.ui.viewmodel
import android.app.Application
import android.net.Uri
import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import app.revanced.manager.R
import app.revanced.manager.domain.bundles.RemotePatchBundle
import app.revanced.manager.domain.manager.PreferencesManager
import app.revanced.manager.domain.repository.PatchBundleRepository
import app.revanced.manager.domain.bundles.RemotePatchBundle
import app.revanced.manager.util.tag
import app.revanced.manager.util.toast
import app.revanced.manager.util.uiSafe
import com.github.pgreze.process.Redirect
import com.github.pgreze.process.process
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
class AdvancedSettingsViewModel(
val prefs: PreferencesManager,
private val app: Application,
private val patchBundleRepository: PatchBundleRepository
) : ViewModel() {
val debugLogFileName: String
get() {
val time = DateTimeFormatter.ISO_LOCAL_DATE_TIME.format(LocalDateTime.now())
return "revanced-manager_logcat_$time"
}
fun setApiUrl(value: String) = viewModelScope.launch(Dispatchers.Default) {
if (value == prefs.api.get()) return@launch
@@ -32,4 +52,31 @@ class AdvancedSettingsViewModel(
fun resetBundles() = viewModelScope.launch {
patchBundleRepository.reset()
}
fun exportDebugLogs(target: Uri) = viewModelScope.launch {
val exitCode = try {
withContext(Dispatchers.IO) {
app.contentResolver.openOutputStream(target)!!.bufferedWriter().use { writer ->
val consumer = Redirect.Consume { flow ->
flow.onEach {
writer.write(it)
}.flowOn(Dispatchers.IO).collect()
}
process("logcat", "-d", stdout = consumer).resultCode
}
}
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
Log.e(tag, "Got exception while exporting logs", e)
app.toast(app.getString(R.string.debug_logs_export_failed))
return@launch
}
if (exitCode == 0)
app.toast(app.getString(R.string.debug_logs_export_success))
else
app.toast(app.getString(R.string.debug_logs_export_read_failed, exitCode))
}
}