Add debug preference to dump heap

This commit is contained in:
MM20 2023-07-20 19:47:33 +02:00
parent 1241067357
commit 8da8ff4078
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389
2 changed files with 64 additions and 0 deletions

View File

@ -1,27 +1,52 @@
package de.mm20.launcher2.ui.settings.debug package de.mm20.launcher2.ui.settings.debug
import android.content.Intent
import android.os.Debug
import android.widget.Toast import android.widget.Toast
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.runtime.Composable 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.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.core.content.FileProvider
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import de.mm20.launcher2.ktx.tryStartActivity
import de.mm20.launcher2.ui.R import de.mm20.launcher2.ui.R
import de.mm20.launcher2.ui.component.preferences.Preference import de.mm20.launcher2.ui.component.preferences.Preference
import de.mm20.launcher2.ui.component.preferences.PreferenceCategory import de.mm20.launcher2.ui.component.preferences.PreferenceCategory
import de.mm20.launcher2.ui.component.preferences.PreferenceScreen import de.mm20.launcher2.ui.component.preferences.PreferenceScreen
import de.mm20.launcher2.ui.locals.LocalNavController import de.mm20.launcher2.ui.locals.LocalNavController
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File
import java.text.SimpleDateFormat
import java.util.Date
@Composable @Composable
fun DebugSettingsScreen() { fun DebugSettingsScreen() {
val context = LocalContext.current val context = LocalContext.current
val scope = rememberCoroutineScope() val scope = rememberCoroutineScope()
var dumpingHeap by remember { mutableStateOf(false) }
val viewModel: DebugSettingsScreenVM = viewModel() val viewModel: DebugSettingsScreenVM = viewModel()
val navController = LocalNavController.current val navController = LocalNavController.current
PreferenceScreen( PreferenceScreen(
stringResource(R.string.preference_screen_debug) stringResource(R.string.preference_screen_debug)
) { ) {
if (dumpingHeap) {
item {
LinearProgressIndicator(
modifier = Modifier.fillMaxWidth()
)
}
}
item { item {
PreferenceCategory { PreferenceCategory {
Preference( Preference(
@ -37,6 +62,42 @@ fun DebugSettingsScreen() {
onClick = { onClick = {
navController?.navigate("settings/debug/logs") navController?.navigate("settings/debug/logs")
}) })
Preference(
title = stringResource(R.string.preference_debug_dump_heap),
summary = if (dumpingHeap) stringResource(R.string.preference_debug_dump_heap_in_progress)
else stringResource(R.string.preference_debug_dump_heap_summary),
enabled = !dumpingHeap,
onClick = {
scope.launch {
dumpingHeap = true
val df = SimpleDateFormat("yyyy-MM-dd-HH-mm-ss")
val path = File(
context.getExternalFilesDir(null),
"kvaesitso-dump-${df.format(Date(System.currentTimeMillis()))}.hprof"
).absolutePath
delay(100)
withContext(Dispatchers.Default) {
Debug.dumpHprofData(path)
}
dumpingHeap = false
context.tryStartActivity(
Intent.createChooser(
Intent(Intent.ACTION_SEND).apply {
type = "application/octet-stream"
putExtra(
Intent.EXTRA_STREAM, FileProvider.getUriForFile(
context,
context.applicationContext.packageName + ".fileprovider",
File(path)
)
)
}, null
)
)
}
}
)
} }
PreferenceCategory(stringResource(R.string.preference_category_debug_tools)) { PreferenceCategory(stringResource(R.string.preference_category_debug_tools)) {
Preference( Preference(

View File

@ -466,6 +466,9 @@
<string name="preference_compact_mode_summary">Hide hourly and daily forecasts</string> <string name="preference_compact_mode_summary">Hide hourly and daily forecasts</string>
<string name="preference_category_debug">Debug</string> <string name="preference_category_debug">Debug</string>
<string name="preference_category_debug_tools">Tools</string> <string name="preference_category_debug_tools">Tools</string>
<string name="preference_debug_dump_heap">Memory snapshot</string>
<string name="preference_debug_dump_heap_summary">Capture a snapshot to analyze memory usageg. The app will freeze while the memory snapshot is captured.</string>
<string name="preference_debug_dump_heap_in_progress">Dumping heap…</string>
<string name="preference_debug_cleanup_database">Clean up database</string> <string name="preference_debug_cleanup_database">Clean up database</string>
<string name="preference_debug_cleanup_database_summary">Remove broken and unused entries from the launcher database</string> <string name="preference_debug_cleanup_database_summary">Remove broken and unused entries from the launcher database</string>
<string name="preference_debug_reinstall_iconpacks">Reinstall icon packs</string> <string name="preference_debug_reinstall_iconpacks">Reinstall icon packs</string>