A rootless Android app that boots Alpine Linux: run containers (Podman/Docker/LXC) and GUI desktop apps.
feat(service): start the VM at boot and from automation intents
ExTv94ed6555e88a
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xmlindex 3780b9c..decef39 100644--- a/app/src/main/AndroidManifest.xml+++ b/app/src/main/AndroidManifest.xml@@ -28,6 +28,9 @@<uses-permission android:name="android.permission.FOREGROUND_SERVICE" /><uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />+ <!-- Autostart the VM on boot when the setting is enabled (#78) -->+ <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />+<!-- Required on Android 13+ to show the foreground service notification --><uses-permission android:name="android.permission.POST_NOTIFICATIONS" />@@ -85,12 +88,20 @@android:name=".MainActivity"android:configChanges="orientation|screenSize|keyboardHidden|keyboard"android:exported="true"+ android:launchMode="singleTop"android:theme="@style/Theme.Podroid"android:windowSoftInputMode="adjustResize"><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter>+ <!-- Foreground-launch path for automation (#78 fix round): an+ Activity launch is always allowed to start a foreground+ service, unlike a cold background receiver. -->+ <intent-filter>+ <action android:name="com.excp.podroid.action.START_VM" />+ <category android:name="android.intent.category.DEFAULT" />+ </intent-filter></activity><!-- FileProvider for sharing console logs -->@@ -114,6 +125,23 @@android:value="Podman emulation via QEMU" /></service>+ <!--+ Boot autostart + Tasker/automation entry point (#78). Exported+ without a permission on purpose: automation apps (Tasker etc.)+ cannot hold a custom signature permission, and the two custom+ actions only start/stop the VM via PodroidService's own+ companion - there is nothing privileged for a permission to gate.+ -->+ <receiver+ android:name=".service.VmControlReceiver"+ android:exported="true">+ <intent-filter>+ <action android:name="android.intent.action.BOOT_COMPLETED" />+ <action android:name="com.excp.podroid.action.START_VM" />+ <action android:name="com.excp.podroid.action.STOP_VM" />+ </intent-filter>+ </receiver>+</application></manifest>diff --git a/app/src/main/java/com/excp/podroid/MainActivity.kt b/app/src/main/java/com/excp/podroid/MainActivity.ktindex 7e73b22..c73a1a3 100644--- a/app/src/main/java/com/excp/podroid/MainActivity.kt+++ b/app/src/main/java/com/excp/podroid/MainActivity.kt@@ -1,6 +1,7 @@package com.excp.podroidimport android.content.Context+import android.content.Intentimport android.os.Bundleimport androidx.activity.ComponentActivityimport androidx.activity.compose.setContent@@ -15,6 +16,8 @@ import androidx.compose.ui.Modifierimport androidx.hilt.navigation.compose.hiltViewModelimport androidx.lifecycle.compose.collectAsStateWithLifecycleimport com.excp.podroid.data.repository.LanguageManager+import com.excp.podroid.service.PodroidService+import com.excp.podroid.service.VmControlReceiverimport com.excp.podroid.ui.navigation.NavGraphViewModelimport com.excp.podroid.ui.navigation.PodroidNavGraphimport com.excp.podroid.ui.theme.PodroidTheme@@ -33,6 +36,7 @@ class MainActivity : ComponentActivity() {override fun onCreate(savedInstanceState: Bundle?) {enableEdgeToEdge()super.onCreate(savedInstanceState)+ handleStartVmIntent(intent)setContent {val windowSizeClass = calculateWindowSizeClass(this)val navVm: NavGraphViewModel = hiltViewModel()@@ -88,4 +92,22 @@ class MainActivity : ComponentActivity() {}}}++ override fun onNewIntent(intent: Intent) {+ super.onNewIntent(intent)+ setIntent(intent)+ handleStartVmIntent(intent)+ }++ /**+ * Foreground-launch path for automation (#78 fix round): a cold+ * background receiver may be denied a foreground-service start, but an+ * Activity launch is always foreground, so this route is reliable on+ * both cold start (onCreate) and warm delivery (onNewIntent).+ */+ private fun handleStartVmIntent(intent: Intent?) {+ if (intent?.action == VmControlReceiver.ACTION_START_VM) {+ PodroidService.start(this)+ }+ }}diff --git a/app/src/main/java/com/excp/podroid/data/repository/SettingsRepository.kt b/app/src/main/java/com/excp/podroid/data/repository/SettingsRepository.ktindex 0b97542..d232374 100644--- a/app/src/main/java/com/excp/podroid/data/repository/SettingsRepository.kt+++ b/app/src/main/java/com/excp/podroid/data/repository/SettingsRepository.kt@@ -75,6 +75,8 @@ class SettingsRepository @Inject constructor(val KEY_LOAD_BALANCE_ENABLED = booleanPreferencesKey("load_balance_enabled")/** Guest egress cap via `podroid.bandwidth=` cmdline + tc. 0 = unlimited. */val KEY_BANDWIDTH_MBPS = intPreferencesKey("bandwidth_mbps")+ /** Start the VM automatically on BOOT_COMPLETED. Read by VmControlReceiver. */+ val KEY_AUTOSTART_ON_BOOT = booleanPreferencesKey("autostart_on_boot")val KEY_X11_RES_MODE = stringPreferencesKey("x11_resolution_mode")val KEY_X11_RES_PRESET = stringPreferencesKey("x11_resolution_preset")@@ -156,6 +158,7 @@ class SettingsRepository @Inject constructor(val usbPassthroughEnabled = pref(KEY_USB_PASSTHROUGH_ENABLED, false)val loadBalanceEnabled = pref(KEY_LOAD_BALANCE_ENABLED, false)val bandwidthMbps = pref(KEY_BANDWIDTH_MBPS, 0)+ val autostartOnBoot = pref(KEY_AUTOSTART_ON_BOOT, false)val avfVerboseLogging: Flow<Boolean> = context.dataStore.data.catch { e -> if (e is IOException) emit(androidx.datastore.preferences.core.emptyPreferences()) else throw e }.map { prefs -> prefs[KEY_AVF_VERBOSE_LOGGING] ?: false }@@ -205,6 +208,7 @@ class SettingsRepository @Inject constructor(suspend fun setUsbPassthroughEnabled(value: Boolean) = set(KEY_USB_PASSTHROUGH_ENABLED, value)suspend fun setLoadBalanceEnabled(value: Boolean) = set(KEY_LOAD_BALANCE_ENABLED, value)suspend fun setBandwidthMbps(value: Int) = set(KEY_BANDWIDTH_MBPS, value.coerceAtLeast(0))+ suspend fun setAutostartOnBoot(value: Boolean) = set(KEY_AUTOSTART_ON_BOOT, value)/*** Persists all first-run setup choices in a single transaction so a process@@ -285,4 +289,5 @@ class SettingsRepository @Inject constructor(suspend fun getUsbPassthroughEnabledSnapshot() = usbPassthroughEnabled.first()suspend fun getLoadBalanceEnabledSnapshot() = loadBalanceEnabled.first()suspend fun getBandwidthMbpsSnapshot() = bandwidthMbps.first()+ suspend fun getAutostartOnBootSnapshot() = autostartOnBoot.first()}diff --git a/app/src/main/java/com/excp/podroid/service/PodroidService.kt b/app/src/main/java/com/excp/podroid/service/PodroidService.ktindex afb04c1..ae750ff 100644--- a/app/src/main/java/com/excp/podroid/service/PodroidService.kt+++ b/app/src/main/java/com/excp/podroid/service/PodroidService.kt@@ -14,6 +14,7 @@ import android.app.NotificationChannelimport android.app.NotificationManagerimport android.app.PendingIntentimport android.app.Service+import android.content.BroadcastReceiverimport android.content.Contextimport android.content.Intentimport android.content.pm.PackageManager@@ -539,3 +540,77 @@ class PodroidService : Service() {}}}++/**+ * Boot autostart + Tasker/automation intent entry point (#78). Exported with no+ * required permission (see the manifest declaration): automation apps can't hold+ * custom permissions, and every action here only starts/stops the VM through+ * [PodroidService]'s own companion, so there's nothing privileged to gate. Kept+ * dumb on purpose - no work beyond dispatching to the existing start()/stop().+ *+ * Fix round (device-observed): a cold background receiver may NOT be allowed+ * to start the foreground service - Android denies it with+ * "Background started FGS: Disallowed" and throws+ * ForegroundServiceStartNotAllowedException (API 31+, extends+ * IllegalStateException; pre-31 the same denial surfaces as a plain+ * IllegalStateException from startService()). BOOT_COMPLETED itself is+ * platform-exempt, but START_VM/STOP_VM fired cold are not. [dispatch] keeps+ * every call here from crashing the receiver process on that denial.+ */+class VmControlReceiver : BroadcastReceiver() {++ override fun onReceive(context: Context, intent: Intent) {+ when (intent.action) {+ Intent.ACTION_BOOT_COMPLETED -> {+ // The DataStore read is async; goAsync() keeps the receiver+ // (and process) alive long enough for it to complete, since a+ // BroadcastReceiver's onReceive is normally torn down the+ // instant it returns.+ val pending = goAsync()+ CoroutineScope(Dispatchers.IO).launch {+ try {+ val settingsRepository = SettingsRepository(context.applicationContext)+ if (settingsRepository.getAutostartOnBootSnapshot()) {+ dispatch { PodroidService.start(context.applicationContext) }+ }+ } finally {+ pending.finish()+ }+ }+ }+ ACTION_START_VM -> dispatch { PodroidService.start(context.applicationContext) }+ ACTION_STOP_VM -> dispatch { PodroidService.stop(context.applicationContext) }+ }+ }++ /**+ * Runs a start()/stop() dispatch, swallowing a denied foreground-service+ * start instead of letting it crash the receiver. Never silent: logs one+ * warning naming the cure (launch Podroid itself so its activity's+ * START_VM intent-filter fires the start in the foreground, or exempt+ * the app from battery optimization so a cold background start is+ * allowed).+ */+ private fun dispatch(action: () -> Unit) {+ try {+ action()+ } catch (e: IllegalStateException) {+ val backgroundFgsDenied = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S &&+ e is android.app.ForegroundServiceStartNotAllowedException+ Log.w(+ TAG,+ "VmControlReceiver could not start/stop the VM from the background " ++ "(${if (backgroundFgsDenied) "foreground service start disallowed" else e.message}) - " ++ "launch Podroid so its START_VM activity intent runs in the foreground, " ++ "or exempt the app from battery optimization",+ e,+ )+ }+ }++ companion object {+ private const val TAG = "VmControlReceiver"+ const val ACTION_START_VM = "com.excp.podroid.action.START_VM"+ const val ACTION_STOP_VM = "com.excp.podroid.action.STOP_VM"+ }+}diff --git a/app/src/main/java/com/excp/podroid/ui/screens/settings/SettingsScreen.kt b/app/src/main/java/com/excp/podroid/ui/screens/settings/SettingsScreen.ktindex 64fc599..678469f 100644--- a/app/src/main/java/com/excp/podroid/ui/screens/settings/SettingsScreen.kt+++ b/app/src/main/java/com/excp/podroid/ui/screens/settings/SettingsScreen.kt@@ -124,6 +124,7 @@ fun SettingsScreen(val vmState by viewModel.vmState.collectAsStateWithLifecycle()val exportError by viewModel.exportError.collectAsStateWithLifecycle()val usbPassthrough by viewModel.usbPassthroughEnabled.collectAsStateWithLifecycle()+ val autostartOnBoot by viewModel.autostartOnBoot.collectAsStateWithLifecycle()var advancedExpanded by remember { mutableStateOf(false) }var showAddDialog by remember { mutableStateOf(false) }@@ -266,6 +267,25 @@ fun SettingsScreen(label = stringResource(R.string.storage),value = "${ui.storageSizeGb} GB",)+ PodroidListRow(+ label = stringResource(R.string.autostart_on_boot),+ rightSlot = {+ PodroidSwitch(+ checked = autostartOnBoot,+ onCheckedChange = { viewModel.setAutostartOnBoot(it) },+ )+ },+ )+ Text(+ text = stringResource(R.string.autostart_on_boot_desc),+ style = MaterialTheme.typography.bodySmall,+ color = MaterialTheme.colorScheme.onSurfaceVariant,+ modifier = Modifier.padding(+ start = PodroidTokens.Spacing.MD,+ end = PodroidTokens.Spacing.MD,+ bottom = PodroidTokens.Spacing.SM,+ ),+ )// ── NETWORK ───────────────────────────────────────────PodroidSectionLabel(stringResource(R.string.network))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.ktindex 388627a..83f380c 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@@ -210,6 +210,13 @@ class SettingsViewModel @Inject constructor(viewModelScope.launch { settingsRepository.setUsbPassthroughEnabled(value) }}+ val autostartOnBoot: StateFlow<Boolean> = settingsRepository.autostartOnBoot+ .stateIn(viewModelScope, SharingStarted.WhileSubscribed(5000), false)++ fun setAutostartOnBoot(value: Boolean) {+ viewModelScope.launch { settingsRepository.setAutostartOnBoot(value) }+ }+suspend fun setLanguage(value: String) {Log.d(TAG, "setLanguage: $value")languageManager.setLanguage(value)diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xmlindex e903401..2ca782e 100644--- a/app/src/main/res/values-zh/strings.xml+++ b/app/src/main/res/values-zh/strings.xml@@ -76,6 +76,8 @@<string name="vm_resources">虚拟机资源</string><string name="stop_vm_to_change">停止虚拟机后才能更改这些设置。</string><string name="storage">存储</string>+ <string name="autostart_on_boot">开机时启动虚拟机</string>+ <string name="autostart_on_boot_desc">手机启动后自动开启虚拟机。自动化应用也可以通过 com.excp.podroid.action.START_VM 和 STOP_VM 意图操作触发。</string><string name="reset_vm">重置虚拟机(删除所有数据)</string><string name="reset_vm_description">完全重置应用?</string><string name="reset_vm_text">这将清除所有应用数据,包括虚拟机存储、设置和端口规则。应用将关闭并恢复到刚安装时的状态。</string>diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xmlindex e94e5e7..82162ee 100644--- a/app/src/main/res/values/strings.xml+++ b/app/src/main/res/values/strings.xml@@ -76,6 +76,8 @@<string name="vm_resources">VM Resources</string><string name="stop_vm_to_change">Stop the VM to change these settings.</string><string name="storage">Storage</string>+ <string name="autostart_on_boot">Start VM on boot</string>+ <string name="autostart_on_boot_desc">Boot the VM automatically after the phone starts. Automation apps can also trigger it with the intent actions com.excp.podroid.action.START_VM and STOP_VM.</string><string name="reset_vm">Reset VM (deletes all data)</string><string name="reset_vm_description">Full App Reset?</string><string name="reset_vm_text">This will clear ALL application data, including your VM storage, settings, and port rules. The app will close and return to a freshly-installed state.</string>diff --git a/app/src/test/java/com/excp/podroid/data/repository/DataStoreKeyContractTest.kt b/app/src/test/java/com/excp/podroid/data/repository/DataStoreKeyContractTest.ktindex 1934250..063e03a 100644--- a/app/src/test/java/com/excp/podroid/data/repository/DataStoreKeyContractTest.kt+++ b/app/src/test/java/com/excp/podroid/data/repository/DataStoreKeyContractTest.kt@@ -64,6 +64,7 @@ class DataStoreKeyContractTest {SettingsRepository.KEY_X11_SHOW_EXTRA_KEYS.name to "x11_show_extra_keys",SettingsRepository.KEY_X11_DPI.name to "x11_dpi",SettingsRepository.KEY_LANGUAGE.name to "language",+ SettingsRepository.KEY_AUTOSTART_ON_BOOT.name to "autostart_on_boot",)@Test