A rootless Android app that boots Alpine Linux: run containers (Podman/Docker/LXC) and GUI desktop apps.
fix(engine): keep Auto on QEMU when the AVF build has networking off
ExTvfe5904c6f96c
diff --git a/app/src/main/java/com/excp/podroid/engine/EngineHolder.kt b/app/src/main/java/com/excp/podroid/engine/EngineHolder.ktindex b1bf15f..d053626 100644--- a/app/src/main/java/com/excp/podroid/engine/EngineHolder.kt+++ b/app/src/main/java/com/excp/podroid/engine/EngineHolder.kt@@ -23,6 +23,7 @@ import com.excp.podroid.data.repository.SettingsRepositoryimport com.excp.podroid.engine.avf.AvfCapabilitiesimport com.excp.podroid.engine.avf.AvfDiagnosticsimport com.excp.podroid.engine.avf.AvfEngine+import com.excp.podroid.engine.avf.AvfReflectimport com.excp.podroid.engine.avf.AvfReportimport com.termux.terminal.TerminalSessionimport com.termux.terminal.TerminalSessionClient@@ -94,14 +95,17 @@ class EngineHolder @Inject constructor(.map { it.backendId }.stateIn(scope, SharingStarted.Eagerly, _currentFlow.value.backendId)- // Non-null only while the current pick is a forced-AVF selection that fell- // back to QEMU: the @StringRes ID of the reason (see FallbackReason /- // pick()). Keep the ID unresolved until the UI renders it so a locale- // change cannot leave this in-memory state with text from the old locale.- // Updated at the same two points _currentFlow is (publishFirstPick, trySwap)- // so it always describes the pick that produced the active engine, even on- // a no-op swap where the engine instance doesn't change but the selection- // (and therefore the reason) does.+ // Non-null while the current pick's backend differs from what the raw+ // selection would naively suggest: either a forced-AVF selection fell+ // back to QEMU, or an AUTO selection avoided AVF because the OEM build+ // has networking compiled out (FallbackReason.NETWORK_DISABLED, #43).+ // The @StringRes ID of the reason (see FallbackReason / pick()). Keep the+ // ID unresolved until the UI renders it so a locale change cannot leave+ // this in-memory state with text from the old locale. Updated at the same+ // two points _currentFlow is (publishFirstPick, trySwap) so it always+ // describes the pick that produced the active engine, even on a no-op+ // swap where the engine instance doesn't change but the selection (and+ // therefore the reason) does.private val _backendFallback = MutableStateFlow<Int?>(null)val backendFallback: StateFlow<Int?> = _backendFallback.asStateFlow()@@ -257,7 +261,17 @@ class EngineHolder @Inject constructor(probe.customVmConfigSupported &&capsChoice !is AvfCapabilities.ProtectedVmChoice.Unsupportedval protectedOnly = capsChoice is AvfCapabilities.ProtectedVmChoice.Unsupported- val (backendId, fallbackReason) = decideBackend(sel, avfUsable, protectedOnly)+ // Only probed when it can actually change the outcome: AUTO selection+ // with AVF otherwise usable (decideBackend only reads this flag on the+ // AUTO+avfUsable branch). A forced selection, or an AUTO selection+ // that would pick QEMU anyway, skips the extra binder call. Missing+ // method/throw already resolves to "available" inside+ // isNetworkFeatureEnabled.+ val networkAvailable = if (shouldProbeNetworkFeature(sel, avfUsable)) {+ runCatching { AvfReflect.isNetworkFeatureEnabled(AvfReflect.manager(context)) }+ .getOrDefault(true)+ } else true+ val (backendId, fallbackReason) = decideBackend(sel, avfUsable, protectedOnly, networkAvailable)// Logging unchanged from the old inline branch: protected-only keeps its// dedicated message, everything else logs the full prerequisite dump.when (fallbackReason) {@@ -275,6 +289,11 @@ class EngineHolder @Inject constructor("customVm=${probe.customVmConfigSupported} " +"caps=${probe.capabilitiesRaw}(${probe.capabilitiesDecoded})")+ FallbackReason.NETWORK_DISABLED -> android.util.Log.w(+ TAG,+ "AVF networking compiled out by this device's OEM build " ++ "(isFeatureEnabled(com.android.kvm.NETWORK)=false); AUTO falling back to QEMU."+ )null -> {}}val engine = if (backendId == "avf") avfProvider.get() else qemuProvider.get()@@ -449,25 +468,38 @@ class EngineHolder @Inject constructor(companion object {private const val TAG = "EngineHolder"- /** Coarse reason a forced-AVF selection fell back to QEMU. Protected-only+ /** Coarse reason a forced-AVF selection fell back to QEMU, or (for+ * NETWORK_DISABLED) an AUTO selection avoided AVF. Protected-only* devices get their own value because pick() already logs them on a* dedicated branch (a signed pvmfw kernel can never appear later). */- enum class FallbackReason { UNAVAILABLE, PROTECTED_ONLY }+ enum class FallbackReason { UNAVAILABLE, PROTECTED_ONLY, NETWORK_DISABLED }/**- * Select the localized reason resource for a forced-AVF fallback. The- * first failed prerequisite is reported, except protected-only devices- * which have their dedicated explanation.+ * Select the localized reason resource for a backend fallback. The+ * first failed prerequisite is reported, except protected-only and+ * network-disabled devices, which have their dedicated explanations.*/@StringResinternal fun fallbackReasonResId(reason: FallbackReason, probe: AvfReport): Int = when {reason == FallbackReason.PROTECTED_ONLY -> R.string.backend_fallback_reason_protected_only+ reason == FallbackReason.NETWORK_DISABLED -> R.string.backend_fallback_reason_network!probe.featureSupported -> R.string.backend_fallback_reason_feature!probe.managePermissionGranted || !probe.customPermissionGranted ->R.string.backend_fallback_reason_permissionselse -> R.string.backend_fallback_reason_service}+ /**+ * Pure gate for pick()'s reflective AvfReflect.isNetworkFeatureEnabled+ * probe (#43 follow-up, T5b): the probe is a binder IPC, so it's only+ * worth making when it can change decideBackend's outcome - AUTO+ * selection with AVF otherwise usable (avfUsable). A forced selection+ * never consults networkAvailable, and AUTO already falls to QEMU+ * without it when AVF isn't usable, so skip the call there too.+ */+ fun shouldProbeNetworkFeature(selection: EngineSelection, avfUsable: Boolean): Boolean =+ selection == EngineSelection.AUTO && avfUsable+/*** Pure backend-selection decision, extracted from pick() so it's unit* testable without a Context or a binder-IPC AVF probe. Mirrors pick()'s@@ -476,11 +508,17 @@ class EngineHolder @Inject constructor(* human reason (which prerequisite failed) is derived separately in* pick() from the raw probe, since this function only sees the already-* collapsed avfUsable/protectedOnly booleans.+ *+ * [networkAvailable] is #43's OEM networking check+ * (isFeatureEnabled(com.android.kvm.NETWORK)); it only steers the AUTO+ * path - a forced AVF selection stays AVF even when it's false, since+ * the user explicitly asked for that backend.*/fun decideBackend(selection: EngineSelection,avfUsable: Boolean,protectedOnly: Boolean,+ networkAvailable: Boolean,): Pair<String, FallbackReason?> = when {selection == EngineSelection.QEMU -> "qemu" to null// Forced AVF, but AVF can't run here → transparent QEMU fallback@@ -488,6 +526,10 @@ class EngineHolder @Inject constructor(selection == EngineSelection.AVF && !avfUsable ->"qemu" to (if (protectedOnly) FallbackReason.PROTECTED_ONLY else FallbackReason.UNAVAILABLE)selection == EngineSelection.AVF -> "avf" to null+ // AUTO would otherwise pick AVF, but the OEM build compiled out+ // networking - keep the VM online on QEMU instead of booting a+ // NIC-less AVF guest.+ avfUsable && !networkAvailable -> "qemu" to FallbackReason.NETWORK_DISABLEDavfUsable -> "avf" to nullelse -> "qemu" to null}diff --git a/app/src/main/java/com/excp/podroid/engine/avf/AvfReflect.kt b/app/src/main/java/com/excp/podroid/engine/avf/AvfReflect.ktindex d384a5b..8c008a4 100644--- a/app/src/main/java/com/excp/podroid/engine/avf/AvfReflect.kt+++ b/app/src/main/java/com/excp/podroid/engine/avf/AvfReflect.kt@@ -87,6 +87,34 @@ object AvfReflect {fun getCapabilities(mgr: Any): Int =runCatching { invokeDecl(mgr, "getCapabilities") as? Int ?: 0 }.getOrDefault(0)+ private const val FEATURE_NETWORK = "com.android.kvm.NETWORK"++ /** Logged at most once per process so a missing method doesn't spam logcat+ * on every AUTO-selection pick(). */+ @Volatile private var networkFeatureCheckLogged = false++ /**+ * VirtualMachineManager.isFeatureEnabled("com.android.kvm.NETWORK"): false+ * on OEM virtualization APEXes built with networking compiled out (#43:+ * release_avf_enable_network off), which otherwise boot an AVF VM with no+ * NIC. Missing method or any throw is treated as "available" (no behaviour+ * change from before this check existed) and logged once.+ */+ fun isNetworkFeatureEnabled(mgr: Any): Boolean =+ runCatching {+ invokeDecl(mgr, "isFeatureEnabled", String::class.java to FEATURE_NETWORK) as? Boolean ?: true+ }.getOrElse {+ if (!networkFeatureCheckLogged) {+ networkFeatureCheckLogged = true+ android.util.Log.i(+ "AvfReflect",+ "isFeatureEnabled($FEATURE_NETWORK) unavailable on this AVF revision; " ++ "treating networking as available: ${it.message}",+ )+ }+ true+ }+/*** Applies the capability-driven protected-VM choice to a VM config builder.* Returns the choice so the caller can fall back to QEMU on Unsupported.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 2ab5112..2ee6ae9 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@@ -356,6 +356,20 @@ fun SettingsScreen(modifier = Modifier.padding(horizontal = PodroidTokens.Spacing.MD),)}+ } else if (ui.engineSelection == EngineSelection.AUTO) {+ // Not an error: Auto did its job and kept the VM on QEMU+ // because this device's AVF build has networking off+ // (#43). Plain secondary text, not the AVF-forced error+ // styling above.+ backendFallback?.takeIf { it == R.string.backend_fallback_reason_network }?.let { reasonResId ->+ Spacer(Modifier.height(PodroidTokens.Spacing.XS))+ Text(+ text = stringResource(reasonResId),+ style = MaterialTheme.typography.bodySmall,+ color = MaterialTheme.colorScheme.onSurfaceVariant,+ modifier = Modifier.padding(horizontal = PodroidTokens.Spacing.MD),+ )+ }}Spacer(Modifier.height(PodroidTokens.Spacing.MD))AdvancedFieldsBlock(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 f3c221f..18e2559 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@@ -328,9 +328,11 @@ class SettingsViewModel @Inject constructor(*/val activeBackendIdFlow: StateFlow<String> = engine.backendIdFlow- /** Non-null when the current pick forced AVF but fell back to QEMU; the- * @StringRes ID for the Settings backend caption. The screen resolves it- * in the current locale. See #66. */+ /** Non-null when the current pick's backend differs from what the raw+ * selection would naively suggest: a forced AVF selection fell back to+ * QEMU, or an AUTO selection avoided AVF because the OEM build has+ * networking compiled out (#43). The @StringRes ID for the Settings+ * backend caption. The screen resolves it in the current locale. See #66. */val backendFallback: StateFlow<Int?> = engine.backendFallbackprivate val _exportError = MutableStateFlow<String?>(null)diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xmlindex d30585b..6811d94 100644--- a/app/src/main/res/values-zh/strings.xml+++ b/app/src/main/res/values-zh/strings.xml@@ -212,6 +212,8 @@<string name="backend_fallback_reason_permissions">所需权限尚未授予</string><string name="backend_fallback_reason_service">系统服务不可达</string><string name="backend_fallback_reason_protected_only">此设备仅支持受保护虚拟机</string>+ <!-- #43: AUTO avoided AVF because the OEM build compiled out networking (isFeatureEnabled(com.android.kvm.NETWORK) == false). Standalone sentence, unlike the fragments above. -->+ <string name="backend_fallback_reason_network">此设备的虚拟化服务关闭了网络功能,因此 Podroid 使用 QEMU 以保持虚拟机联网。</string><!-- Permissions --><string name="permissions">权限</string>diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xmlindex 32f876a..a68dbc3 100644--- a/app/src/main/res/values/strings.xml+++ b/app/src/main/res/values/strings.xml@@ -212,6 +212,8 @@<string name="backend_fallback_reason_permissions">required permissions have not been granted</string><string name="backend_fallback_reason_service">the system service is unreachable</string><string name="backend_fallback_reason_protected_only">this device only supports protected virtual machines</string>+ <!-- #43: AUTO avoided AVF because the OEM build compiled out networking (isFeatureEnabled(com.android.kvm.NETWORK) == false). Standalone sentence, unlike the fragments above. -->+ <string name="backend_fallback_reason_network">This device\'s virtualization service has networking turned off, so Podroid uses QEMU to keep the VM online.</string><!-- Permissions --><string name="permissions">Permissions</string>diff --git a/app/src/test/java/com/excp/podroid/engine/EngineHolderDecideBackendTest.kt b/app/src/test/java/com/excp/podroid/engine/EngineHolderDecideBackendTest.ktindex e3a70ce..7658993 100644--- a/app/src/test/java/com/excp/podroid/engine/EngineHolderDecideBackendTest.kt+++ b/app/src/test/java/com/excp/podroid/engine/EngineHolderDecideBackendTest.kt@@ -8,9 +8,12 @@ import com.excp.podroid.Rimport com.excp.podroid.engine.EngineHolder.Companion.FallbackReasonimport com.excp.podroid.engine.EngineHolder.Companion.decideBackendimport com.excp.podroid.engine.EngineHolder.Companion.fallbackReasonResId+import com.excp.podroid.engine.EngineHolder.Companion.shouldProbeNetworkFeatureimport com.excp.podroid.engine.avf.AvfReportimport org.junit.Assert.assertEquals+import org.junit.Assert.assertFalseimport org.junit.Assert.assertNull+import org.junit.Assert.assertTrueimport org.junit.Test/**@@ -27,6 +30,7 @@ class EngineHolderDecideBackendTest {selection = EngineSelection.AUTO,avfUsable = true,protectedOnly = false,+ networkAvailable = true,)assertEquals("avf", backendId)assertNull(reason)@@ -40,6 +44,7 @@ class EngineHolderDecideBackendTest {selection = EngineSelection.AUTO,avfUsable = false,protectedOnly = false,+ networkAvailable = true,)assertEquals("qemu", backendId)assertNull(reason)@@ -51,6 +56,7 @@ class EngineHolderDecideBackendTest {selection = EngineSelection.AVF,avfUsable = false,protectedOnly = false,+ networkAvailable = true,)assertEquals("qemu", backendId)assertEquals(FallbackReason.UNAVAILABLE, reason)@@ -62,6 +68,7 @@ class EngineHolderDecideBackendTest {selection = EngineSelection.AVF,avfUsable = true,protectedOnly = false,+ networkAvailable = true,)assertEquals("avf", backendId)assertNull(reason)@@ -73,6 +80,7 @@ class EngineHolderDecideBackendTest {selection = EngineSelection.QEMU,avfUsable = true,protectedOnly = true,+ networkAvailable = true,)assertEquals("qemu", backendId)assertNull(reason)@@ -84,11 +92,63 @@ class EngineHolderDecideBackendTest {selection = EngineSelection.AVF,avfUsable = false,protectedOnly = true,+ networkAvailable = true,)assertEquals("qemu", backendId)assertEquals(FallbackReason.PROTECTED_ONLY, reason)}+ @Test+ fun `auto selection with network compiled out falls back to qemu with the network reason`() {+ // #43: OEM build has isFeatureEnabled(com.android.kvm.NETWORK) == false.+ val (backendId, reason) = decideBackend(+ selection = EngineSelection.AUTO,+ avfUsable = true,+ protectedOnly = false,+ networkAvailable = false,+ )+ assertEquals("qemu", backendId)+ assertEquals(FallbackReason.NETWORK_DISABLED, reason)+ }++ @Test+ fun `auto selection with unknown network support picks avf`() {+ // Missing method or a throw resolves to networkAvailable = true before+ // reaching decideBackend, so an "unknown" device behaves like before #43.+ val (backendId, reason) = decideBackend(+ selection = EngineSelection.AUTO,+ avfUsable = true,+ protectedOnly = false,+ networkAvailable = true,+ )+ assertEquals("avf", backendId)+ assertNull(reason)+ }++ @Test+ fun `forced avf with network compiled out stays avf`() {+ // The user explicitly asked for AVF; #43's check only steers AUTO.+ val (backendId, reason) = decideBackend(+ selection = EngineSelection.AVF,+ avfUsable = true,+ protectedOnly = false,+ networkAvailable = false,+ )+ assertEquals("avf", backendId)+ assertNull(reason)+ }++ @Test+ fun `network-disabled fallback maps to its dedicated resource`() {+ assertEquals(+ R.string.backend_fallback_reason_network,+ fallbackReasonResId(+ FallbackReason.NETWORK_DISABLED,+ report(),+ ),+ )+ }+@Testfun `protected-only fallback maps to its dedicated resource`() {assertEquals(@@ -133,6 +193,28 @@ class EngineHolderDecideBackendTest {)}+ @Test+ fun `network feature probe runs only for auto with avf otherwise usable`() {+ // #43 follow-up (T5b): the probe is a binder IPC, so it must be+ // skipped whenever it cannot change decideBackend's outcome.+ assertTrue(shouldProbeNetworkFeature(EngineSelection.AUTO, avfUsable = true))+ }++ @Test+ fun `network feature probe skipped for auto when avf is not usable anyway`() {+ assertFalse(shouldProbeNetworkFeature(EngineSelection.AUTO, avfUsable = false))+ }++ @Test+ fun `network feature probe skipped for forced avf`() {+ assertFalse(shouldProbeNetworkFeature(EngineSelection.AVF, avfUsable = true))+ }++ @Test+ fun `network feature probe skipped for forced qemu`() {+ assertFalse(shouldProbeNetworkFeature(EngineSelection.QEMU, avfUsable = true))+ }+private fun report(featureSupported: Boolean = true,managePermissionGranted: Boolean = true,