mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-18 12:28:40 +00:00
Add session log management to setup diagnostics (#2099)
* feat: manage session logs in setup * fix: address session log review feedback * fix: honor device date and time settings
This commit is contained in:
1 parent
f005241549
commit
d62f017bce
5 files changed
+144
-5
No files matched your search
@@ -209,6 +209,7 @@ class FrontEndActivity : ComponentActivity() {
|
||||
private var wifiDiscoveryBlocked by mutableStateOf(false)
|
||||
private var integrationState by mutableStateOf(SteamIntegration.State())
|
||||
private var integrationFocus by mutableStateOf<SteamIntegration.Feature?>(null)
|
||||
private var pendingSessionLogExport: File? = null
|
||||
private val wifiLocationReceiver = object : android.content.BroadcastReceiver() {
|
||||
override fun onReceive(context: android.content.Context?, intent: Intent?) {
|
||||
refreshWifiDiscovery()
|
||||
@@ -267,6 +268,26 @@ class FrontEndActivity : ComponentActivity() {
|
||||
val then = onSavePicked.also { onSavePicked = null } ?: return@registerForActivityResult
|
||||
if (r.resultCode == RESULT_OK) InAppFilePicker.pickedFile(r.data)?.let(then)
|
||||
}
|
||||
private val pickSessionLogExport = registerForActivityResult(ActivityResultContracts.CreateDocument("application/zip")) { uri ->
|
||||
val folder = pendingSessionLogExport.also { pendingSessionLogExport = null } ?: return@registerForActivityResult
|
||||
if (uri != null) {
|
||||
SessionLogShare.prepare(this, { folder }) { zip ->
|
||||
if (zip == null) {
|
||||
android.widget.Toast.makeText(this, R.string.setup_log_export_failed, android.widget.Toast.LENGTH_LONG).show()
|
||||
} else Thread({
|
||||
val saved = runCatching {
|
||||
contentResolver.openOutputStream(uri)?.use { output -> zip.inputStream().buffered().use { it.copyTo(output) } }
|
||||
?: error("Could not open destination")
|
||||
}.isSuccess
|
||||
ui.post {
|
||||
android.widget.Toast.makeText(this,
|
||||
if (saved) R.string.setup_log_export_saved else R.string.setup_log_export_failed,
|
||||
android.widget.Toast.LENGTH_LONG).show()
|
||||
}
|
||||
}, "save-session-logs").start()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun refreshWifiDiscovery() {
|
||||
wifiDiscoveryPermission = WifiDiscovery.permissionGranted(this)
|
||||
@@ -1027,6 +1048,7 @@ class FrontEndActivity : ComponentActivity() {
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
pendingSessionLogExport = savedInstanceState?.getString(KEY_PENDING_SESSION_LOG_EXPORT)?.let(::File)
|
||||
homeChoiceOpen = savedInstanceState?.getBoolean(STATE_HOME_CHOICE_OPEN) == true
|
||||
gameSpaceOffered = savedInstanceState?.getBoolean(STATE_GAME_SPACE_OFFERED) == true
|
||||
if (savedInstanceState == null) readIntegrationIntent(intent)
|
||||
@@ -1299,6 +1321,25 @@ class FrontEndActivity : ComponentActivity() {
|
||||
else startActivity(SessionLogShare.shareIntent(this, zip))
|
||||
}
|
||||
},
|
||||
onShareSessionLog = { folder ->
|
||||
SessionLogShare.prepare(this, { folder }) { zip ->
|
||||
if (zip == null) android.widget.Toast.makeText(this, R.string.main_no_logs_yet, android.widget.Toast.LENGTH_LONG).show()
|
||||
else startActivity(SessionLogShare.shareIntent(this, zip))
|
||||
}
|
||||
},
|
||||
onSaveSessionLog = { folder ->
|
||||
pendingSessionLogExport = folder
|
||||
pickSessionLogExport.launch("DroidDeck-${folder.name}.zip")
|
||||
},
|
||||
onDeleteSessionLog = { folder, done ->
|
||||
Thread({
|
||||
val deleted = runCatching { SessionLogShare.deleteSession(this, folder) }.getOrDefault(false)
|
||||
ui.post {
|
||||
if (!deleted) android.widget.Toast.makeText(this, R.string.setup_log_delete_failed, android.widget.Toast.LENGTH_SHORT).show()
|
||||
done()
|
||||
}
|
||||
}, "delete-session-logs").start()
|
||||
},
|
||||
onClearLogs = {
|
||||
Thread({
|
||||
val cleared = runCatching { SessionArtifacts.clearAll(this) }.getOrDefault(0)
|
||||
@@ -1804,6 +1845,7 @@ class FrontEndActivity : ComponentActivity() {
|
||||
super.onSaveInstanceState(outState)
|
||||
outState.putBoolean(STATE_HOME_CHOICE_OPEN, homeChoiceOpen)
|
||||
outState.putBoolean(STATE_GAME_SPACE_OFFERED, gameSpaceOffered)
|
||||
pendingSessionLogExport?.let { outState.putString(KEY_PENDING_SESSION_LOG_EXPORT, it.absolutePath) }
|
||||
if (slowUsbNever) SessionPrefs.setSlowUsbWarningOff(this)
|
||||
saveCompatLaunch(outState)
|
||||
pendingSwitch?.let { outState.putParcelable(KEY_PENDING_SWITCH, it) }
|
||||
@@ -2875,6 +2917,7 @@ class FrontEndActivity : ComponentActivity() {
|
||||
|
||||
companion object {
|
||||
private const val STATE_HOME_CHOICE_OPEN = "home_choice_open"
|
||||
private const val KEY_PENDING_SESSION_LOG_EXPORT = "pending_session_log_export"
|
||||
private const val STATE_GAME_SPACE_OFFERED = "game_space_offered"
|
||||
private const val TAG = "MainActivity"
|
||||
private const val MEDIA_SETTLE_MS = 1500L
|
||||
|
||||
@@ -19,6 +19,8 @@ import java.util.zip.ZipEntry
|
||||
import java.util.zip.ZipOutputStream
|
||||
|
||||
object SessionLogShare {
|
||||
data class Details(val sizeBytes: Long, val modifiedAt: Long)
|
||||
|
||||
private val main = Handler(Looper.getMainLooper())
|
||||
|
||||
var progress by mutableStateOf<Float?>(null)
|
||||
@@ -35,7 +37,30 @@ object SessionLogShare {
|
||||
}, "share-logs").start()
|
||||
}
|
||||
|
||||
fun latest(context: Context): File? = SessionPaths.sessionFolders(context).lastOrNull()
|
||||
fun latest(context: Context): File? = sessions(context).lastOrNull()
|
||||
|
||||
fun sessions(context: Context): List<File> {
|
||||
val current = SessionPaths.current()?.takeIf { it.isDirectory && SessionPaths.isSessionFolder(it) }
|
||||
return (SessionPaths.sessionFolders(context) + listOfNotNull(current))
|
||||
.distinctBy { it.absoluteFile.toPath().normalize().toString() }
|
||||
.sortedWith(SessionPaths.chronological)
|
||||
}
|
||||
|
||||
fun details(folder: File): Details {
|
||||
var size = 0L
|
||||
var modified = folder.lastModified()
|
||||
folder.walkTopDown().filter { it.isFile }.forEach { file ->
|
||||
size += file.length()
|
||||
modified = maxOf(modified, file.lastModified())
|
||||
}
|
||||
return Details(size, modified)
|
||||
}
|
||||
|
||||
fun deleteSession(context: Context, folder: File): Boolean {
|
||||
if (runCatching { SessionPaths.current()?.canonicalFile == folder.canonicalFile }.getOrDefault(false)) return false
|
||||
val knownSession = runCatching { folder.canonicalFile }.getOrNull() in sessions(context).mapNotNull { runCatching { it.canonicalFile }.getOrNull() }
|
||||
return knownSession && folder.isDirectory && folder.deleteRecursively()
|
||||
}
|
||||
|
||||
|
||||
fun zipFolder(context: Context, folder: File, onProgress: ((Float) -> Unit)? = null): File? {
|
||||
|
||||
@@ -232,6 +232,9 @@ class FrontEndActions(
|
||||
val onMemoryHud: (String) -> Unit = {},
|
||||
val onBackgroundAppLimit: (Int) -> Unit = {},
|
||||
val onShareLogs: () -> Unit = {},
|
||||
val onShareSessionLog: (File) -> Unit = {},
|
||||
val onSaveSessionLog: (File) -> Unit = {},
|
||||
val onDeleteSessionLog: (File, () -> Unit) -> Unit = { _, done -> done() },
|
||||
val onClearLogs: () -> Unit = {},
|
||||
val onResetSettings: () -> Unit = {},
|
||||
val onOffline: () -> Unit,
|
||||
|
||||
@@ -193,11 +193,13 @@ internal fun SetupPanel(
|
||||
)
|
||||
val tabs = listOf(stringResource(R.string.setup_tab_overview), stringResource(R.string.setup_tab_controller), stringResource(R.string.setup_tab_launcher), stringResource(R.string.setup_tab_storage), stringResource(R.string.setup_tab_sync), stringResource(R.string.setup_tab_diagnostics), stringResource(R.string.setup_tab_glasses))
|
||||
var tab by rememberSaveable { mutableStateOf(if (SetupTabs.openLauncher) LAUNCHER_TAB else 0) }
|
||||
var logsRefresh by remember { mutableStateOf(0) }
|
||||
var deleteSessionLogs by remember { mutableStateOf<java.io.File?>(null) }
|
||||
SetupTabs.openLauncher = false
|
||||
if (SetupTabs.openOverview) { tab = 0; SetupTabs.openOverview = false }
|
||||
val chipFocus = remember { List(tabs.size) { FocusRequester() } }
|
||||
var tabTurned by remember { mutableStateOf(false) }
|
||||
val pick: (Int) -> Unit = { i -> tab = i; tabTurned = true }
|
||||
val pick: (Int) -> Unit = { i -> tab = i; tabTurned = true; if (i == 5) logsRefresh++ }
|
||||
val inputModeManager = LocalInputModeManager.current
|
||||
LaunchedEffect(com.droiddeck.launcher.storage.StorageState.openRequests) {
|
||||
if (com.droiddeck.launcher.storage.StorageState.openPending) {
|
||||
@@ -382,11 +384,12 @@ internal fun SetupPanel(
|
||||
val shownChecks = all.filterIndexed { i, _ -> checks[i] }
|
||||
shownChecks.forEachIndexed { i, check -> check(i < shownChecks.lastIndex) }
|
||||
}
|
||||
if (passing < checks.size) {
|
||||
val needsAttention = all.filterIndexed { i, _ -> !checks[i] && i != 1 }
|
||||
if (needsAttention.isNotEmpty()) {
|
||||
SectionTitle(stringResource(R.string.setup_needs_you), null)
|
||||
Column(verticalArrangement = Arrangement.spacedBy(10.dp)) {
|
||||
all.forEachIndexed { i, check ->
|
||||
if (!checks[i]) Column(Modifier.fillMaxWidth().clip(Shape14).background(colors.surface).border(1.dp, AttentionAmber.copy(alpha = 0.5f), Shape14)) { check(false) }
|
||||
needsAttention.forEach { check ->
|
||||
Column(Modifier.fillMaxWidth().clip(Shape14).background(colors.surface).border(1.dp, AttentionAmber.copy(alpha = 0.5f), Shape14)) { check(false) }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -553,6 +556,44 @@ internal fun SetupPanel(
|
||||
ToggleRow(host, "agent-commands", stringResource(R.string.setup_agent_commands), null, s.agentCommands) { a.onAgentCommands() }
|
||||
ActionRow(stringResource(R.string.setup_latest_logs), stringResource(R.string.drawer_logs_hint), stringResource(R.string.drawer_share_logs), a.onShareLogs,
|
||||
progress = com.droiddeck.launcher.session.SessionLogShare.progress)
|
||||
androidx.compose.material3.HorizontalDivider()
|
||||
Text(stringResource(R.string.setup_log_sessions), fontSize = 13.sp, fontWeight = FontWeight.SemiBold,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant, modifier = Modifier.padding(horizontal = 14.dp, vertical = 10.dp))
|
||||
Text(stringResource(R.string.setup_log_export_hint), fontSize = 12.sp, color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
modifier = Modifier.padding(start = 14.dp, end = 14.dp, bottom = 8.dp))
|
||||
val sessionLogs = remember(ctx, logsRefresh) { com.droiddeck.launcher.session.SessionLogShare.sessions(ctx).asReversed() }
|
||||
if (sessionLogs.isEmpty()) {
|
||||
Text(stringResource(R.string.setup_log_empty), fontSize = 13.sp, color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
modifier = Modifier.padding(horizontal = 14.dp, vertical = 10.dp))
|
||||
}
|
||||
sessionLogs.forEach { folder ->
|
||||
val active = folder == com.droiddeck.launcher.session.SessionPaths.current()
|
||||
var details by remember(folder) { mutableStateOf<com.droiddeck.launcher.session.SessionLogShare.Details?>(null) }
|
||||
LaunchedEffect(folder, logsRefresh) {
|
||||
details = kotlinx.coroutines.withContext(kotlinx.coroutines.Dispatchers.IO) {
|
||||
com.droiddeck.launcher.session.SessionLogShare.details(folder)
|
||||
}
|
||||
}
|
||||
Column(Modifier.fillMaxWidth().padding(horizontal = 14.dp, vertical = 8.dp)) {
|
||||
Text(folder.name, fontSize = 14.sp, fontWeight = FontWeight.SemiBold, color = MaterialTheme.colorScheme.onBackground,
|
||||
maxLines = 1, overflow = TextOverflow.Ellipsis)
|
||||
details?.let { detail ->
|
||||
Text(
|
||||
stringResource(R.string.setup_log_size_date,
|
||||
android.text.format.Formatter.formatShortFileSize(ctx, detail.sizeBytes),
|
||||
"${android.text.format.DateFormat.getDateFormat(ctx).format(java.util.Date(detail.modifiedAt))} ${android.text.format.DateFormat.getTimeFormat(ctx).format(java.util.Date(detail.modifiedAt))}"),
|
||||
fontSize = 12.sp, color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
)
|
||||
}
|
||||
if (active) Text(stringResource(R.string.setup_log_current), fontSize = 12.sp, color = MaterialTheme.colorScheme.primary)
|
||||
Row(Modifier.fillMaxWidth().padding(top = 6.dp), horizontalArrangement = Arrangement.spacedBy(8.dp)) {
|
||||
SecondaryButton(stringResource(R.string.setup_log_share), enabled = com.droiddeck.launcher.session.SessionLogShare.progress == null, compact = true, modifier = Modifier.weight(1f)) { a.onShareSessionLog(folder) }
|
||||
SecondaryButton(stringResource(R.string.setup_log_save), enabled = com.droiddeck.launcher.session.SessionLogShare.progress == null, compact = true, modifier = Modifier.weight(1f)) { a.onSaveSessionLog(folder) }
|
||||
SecondaryButton(stringResource(R.string.setup_log_delete), compact = true, enabled = !active, modifier = Modifier.weight(1f)) { deleteSessionLogs = folder }
|
||||
}
|
||||
}
|
||||
androidx.compose.material3.HorizontalDivider()
|
||||
}
|
||||
ActionRow(stringResource(R.string.setup_saved_logs), stringResource(R.string.setup_saved_logs_hint, com.droiddeck.launcher.session.SessionPaths.KEEP_SESSIONS), stringResource(R.string.setup_clear_logs), a.onClearLogs)
|
||||
ActionRow(stringResource(R.string.setup_reset_settings), stringResource(R.string.setup_reset_settings_hint),
|
||||
stringResource(R.string.setup_reset_settings_button), a.onResetSettings)
|
||||
@@ -595,6 +636,20 @@ internal fun SetupPanel(
|
||||
}
|
||||
}
|
||||
}
|
||||
deleteSessionLogs?.let { folder ->
|
||||
androidx.compose.material3.AlertDialog(
|
||||
onDismissRequest = { deleteSessionLogs = null },
|
||||
title = { Text(stringResource(R.string.setup_log_delete_title)) },
|
||||
text = { Text(stringResource(R.string.setup_log_delete_message, folder.name)) },
|
||||
confirmButton = {
|
||||
androidx.compose.material3.TextButton(onClick = {
|
||||
deleteSessionLogs = null
|
||||
a.onDeleteSessionLog(folder) { logsRefresh++ }
|
||||
}) { Text(stringResource(R.string.setup_log_delete)) }
|
||||
},
|
||||
dismissButton = { androidx.compose.material3.TextButton(onClick = { deleteSessionLogs = null }) { Text(stringResource(R.string.common_cancel)) } },
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
|
||||
Reference in new issue
Block a user