Skip to content

A rootless Android app that boots Alpine Linux: run containers (Podman/Docker/LXC) and GUI desktop apps.

perf(engine,service): suspend start(), cache notification builder, resetVm off-main

ExTv9d716f0105af
diff --git a/app/src/main/java/com/excp/podroid/engine/PodroidQemu.kt b/app/src/main/java/com/excp/podroid/engine/PodroidQemu.kt
index b492e49..941ea80 100644
--- a/app/src/main/java/com/excp/podroid/engine/PodroidQemu.kt
+++ b/app/src/main/java/com/excp/podroid/engine/PodroidQemu.kt
@@ -209,8 +209,6 @@ class PodroidQemu @Inject constructor(
return sess
}
- fun start() = start(emptyList())
-
/** All snapshotted settings the engine needs for a single VM launch. */
data class LaunchConfig(
val ramMb: Int = 512,
@@ -223,15 +221,7 @@ class PodroidQemu @Inject constructor(
val kernelExtraCmdline: String = "",
)
- fun start(
- portForwards: List<PortForwardRule>,
- ramMb: Int = 512,
- cpus: Int = 1,
- sshEnabled: Boolean = false,
- androidIp: String = "unknown",
- ) = start(portForwards, LaunchConfig(ramMb = ramMb, cpus = cpus, sshEnabled = sshEnabled, androidIp = androidIp))
-
- fun start(portForwards: List<PortForwardRule>, config: LaunchConfig) {
+ suspend fun start(portForwards: List<PortForwardRule>, config: LaunchConfig) {
if (_state.value is VmState.Starting || _state.value is VmState.Running) {
Log.w(TAG, "start() called while VM is ${_state.value}, ignoring")
return
@@ -306,7 +296,7 @@ class PodroidQemu @Inject constructor(
socketsReady = true
break
}
- Thread.sleep(200)
+ delay(200)
}
if (!socketsReady) {
Log.e(TAG, "Socket timeout — QEMU sockets not ready after ${SOCKET_READY_TIMEOUT_MS}ms")
diff --git a/app/src/main/java/com/excp/podroid/service/PodroidService.kt b/app/src/main/java/com/excp/podroid/service/PodroidService.kt
index 5f795a9..287485c 100644
--- a/app/src/main/java/com/excp/podroid/service/PodroidService.kt
+++ b/app/src/main/java/com/excp/podroid/service/PodroidService.kt
@@ -43,10 +43,13 @@ class PodroidService : Service() {
@Inject lateinit var settingsRepository: SettingsRepository
private val serviceScope = CoroutineScope(SupervisorJob() + Dispatchers.Main)
- private var launchJob: Job? = null
private var notificationJob: Job? = null
private var wakeLock: PowerManager.WakeLock? = null
+ private var notificationBuilder: NotificationCompat.Builder? = null
+ private var stopPendingIntent: PendingIntent? = null
+ private var openPendingIntent: PendingIntent? = null
+
override fun onBind(intent: Intent?): IBinder? = null
override fun onCreate() {
@@ -166,8 +169,7 @@ class PodroidService : Service() {
}
private fun launchPodroid() {
- launchJob?.cancel()
- launchJob = serviceScope.launch {
+ serviceScope.launch {
startNotificationUpdates()
withContext(Dispatchers.IO) {
try {
@@ -219,7 +221,16 @@ class PodroidService : Service() {
.createNotificationChannel(channel)
}
- private fun buildNotification(status: String): Notification {
+ /**
+ * Lazily build (and cache) the NotificationCompat.Builder + its PendingIntents
+ * once per service lifetime. Boot streams ~5 state-change emits per second, so
+ * recreating the Builder + two PendingIntents on every emit is pure churn.
+ * After the first call, updateNotification() just mutates contentText on the
+ * cached builder.
+ */
+ private fun getOrCreateNotificationBuilder(): NotificationCompat.Builder {
+ notificationBuilder?.let { return it }
+
val openIntent = PendingIntent.getActivity(
this, 0,
Intent(this, MainActivity::class.java),
@@ -230,15 +241,23 @@ class PodroidService : Service() {
Intent(this, PodroidService::class.java).apply { action = ACTION_STOP },
PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT,
)
+ openPendingIntent = openIntent
+ stopPendingIntent = stopIntent
- return NotificationCompat.Builder(this, CHANNEL_ID)
+ val builder = NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Podroid")
- .setContentText(status)
.setSmallIcon(R.drawable.ic_vm_notification)
.setContentIntent(openIntent)
.setOngoing(true)
.setOnlyAlertOnce(true)
.addAction(R.drawable.ic_stop, "Stop", stopIntent)
+ notificationBuilder = builder
+ return builder
+ }
+
+ private fun buildNotification(status: String): Notification {
+ return getOrCreateNotificationBuilder()
+ .setContentText(status)
.build()
}
diff --git a/app/src/main/java/com/excp/podroid/ui/screens/settings/SettingsViewModel.kt b/app/src/main/java/com/excp/podroid/ui/screens/settings/SettingsViewModel.kt
index f2700c0..fd56fe1 100644
--- a/app/src/main/java/com/excp/podroid/ui/screens/settings/SettingsViewModel.kt
+++ b/app/src/main/java/com/excp/podroid/ui/screens/settings/SettingsViewModel.kt
@@ -9,6 +9,7 @@ package com.excp.podroid.ui.screens.settings
import android.content.Context
import android.content.Intent
import android.os.Build
+import android.util.Log
import androidx.core.content.FileProvider
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
@@ -203,8 +204,12 @@ class SettingsViewModel @Inject constructor(
}
fun resetVm() {
- val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as android.app.ActivityManager
- activityManager.clearApplicationUserData()
+ viewModelScope.launch {
+ withContext(Dispatchers.IO) {
+ val activityManager = context.getSystemService(Context.ACTIVITY_SERVICE) as android.app.ActivityManager
+ activityManager.clearApplicationUserData()
+ }
+ }
}
/**
@@ -221,27 +226,31 @@ class SettingsViewModel @Inject constructor(
*/
fun exportConsoleLogs() {
viewModelScope.launch {
- val logFile = withContext(Dispatchers.IO) {
- val file = File(context.filesDir, "log.txt")
- file.writeText(buildDiagnosticLog())
- file
- }
+ try {
+ val logFile = withContext(Dispatchers.IO) {
+ val file = File(context.filesDir, "log.txt")
+ file.writeText(buildDiagnosticLog())
+ file
+ }
- val uri = FileProvider.getUriForFile(
- context,
- "${context.packageName}.fileprovider",
- logFile,
- )
- val intent = Intent(Intent.ACTION_SEND).apply {
- type = "text/plain"
- putExtra(Intent.EXTRA_STREAM, uri)
- putExtra(Intent.EXTRA_SUBJECT, "Podroid Diagnostic Log")
- addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
- addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
+ val uri = FileProvider.getUriForFile(
+ context,
+ "${context.packageName}.fileprovider",
+ logFile,
+ )
+ val intent = Intent(Intent.ACTION_SEND).apply {
+ type = "text/plain"
+ putExtra(Intent.EXTRA_STREAM, uri)
+ putExtra(Intent.EXTRA_SUBJECT, "Podroid Diagnostic Log")
+ addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
+ addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
+ }
+ context.startActivity(Intent.createChooser(intent, "Share diagnostic log").apply {
+ addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
+ })
+ } catch (e: Exception) {
+ Log.w(TAG, "Export failed", e)
}
- context.startActivity(Intent.createChooser(intent, "Share diagnostic log").apply {
- addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
- })
}
}
@@ -350,4 +359,8 @@ class SettingsViewModel @Inject constructor(
"(failed to capture logcat: ${e.message})\n"
}
}
+
+ companion object {
+ private const val TAG = "SettingsViewModel"
+ }
}