Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 20 additions & 8 deletions app/src/main/kotlin/ai/opencode/app/AppFeature.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ import ai.opencode.feature.voice.VoiceCallLogClient
import ai.opencode.feature.voice.VoiceCallServiceClient
import ai.opencode.server.CachedHomeSnapshot
import ai.opencode.server.ConnectionFailureDetail
import ai.opencode.server.LocalNetworkPermission
import ai.opencode.server.OpenCodeClient
import ai.opencode.server.ServerLinkClient
import ai.opencode.server.OpenCodeClientError
Expand Down Expand Up @@ -136,6 +137,8 @@ class AppFeature : Reducer<AppFeature.State, AppFeature.Action> {
val message: String,
val phase: FailureContext.Phase = FailureContext.Phase.Connect,
val context: FailureContext? = null,
/** Android 17's local network access is off for this server; Settings can turn it on. */
val localNetworkDenied: Boolean = false,
) : Connection
/** Left the session list on purpose to pick a server; must not auto-reconnect. */
data object Picking : Connection
Expand Down Expand Up @@ -239,6 +242,7 @@ class AppFeature : Reducer<AppFeature.State, AppFeature.Action> {
val errorMessage: String? get() = (connection as? Connection.Failed)?.message
val errorContext: FailureContext? get() = (connection as? Connection.Failed)?.context
val errorPhase: FailureContext.Phase? get() = (connection as? Connection.Failed)?.let { it.context?.phase ?: it.phase }
val errorNeedsLocalNetwork: Boolean get() = (connection as? Connection.Failed)?.localNetworkDenied == true

/** Fills the connection form with a complete target. */
fun load(server: OpenCodeServer): State = copy(
Expand Down Expand Up @@ -479,6 +483,7 @@ class AppFeature : Reducer<AppFeature.State, AppFeature.Action> {
data class PairingRedeemed(val attemptID: Int, val result: Result<OpenCodeServer>) : Action
data class HealthResponse(val attemptID: Int, val result: Result<OpenCodeServer>) : Action
data class CopyTextTapped(val text: String) : Action
data object LocalNetworkSettingsTapped : Action
data class Home(val action: HomeFeature.Action) : Action
data class Welcome(val action: WelcomeFeature.Action) : Action
data class ClientTools(val action: ClientToolsFeature.Action) : Action
Expand Down Expand Up @@ -655,6 +660,10 @@ class AppFeature : Reducer<AppFeature.State, AppFeature.Action> {
}
Action.PairingErrorDismissed -> Reduced(state.copy(pairingError = null))
is Action.PairingRedeemed -> pairingRedeemed(state, action)
Action.LocalNetworkSettingsTapped -> {
val localNetwork = dependency(LocalNetworkPermission)
Reduced(state, Effect.run { localNetwork.openSettings() })
}
is Action.CopyTextTapped -> {
if (action.text.isEmpty()) Reduced(state)
else {
Expand Down Expand Up @@ -921,10 +930,7 @@ class AppFeature : Reducer<AppFeature.State, AppFeature.Action> {
return Reduced(
state.copy(
connectionAttempt = null,
connection = State.Connection.Failed(
message = clientError.message,
context = FailureContext.of(attempt.server, FailureContext.Phase.Pair, action.attemptID, clientError.connectionFailures),
),
connection = failed(clientError, attempt.server, FailureContext.of(attempt.server, FailureContext.Phase.Pair, action.attemptID, clientError.connectionFailures)),
),
)
}
Expand All @@ -945,10 +951,7 @@ class AppFeature : Reducer<AppFeature.State, AppFeature.Action> {
return Reduced(
state.copy(
connectionAttempt = null,
connection = State.Connection.Failed(
message = clientError.message,
context = FailureContext.of(attempt.server, FailureContext.Phase.Connect, action.attemptID, clientError.connectionFailures),
),
connection = failed(clientError, attempt.server, FailureContext.of(attempt.server, FailureContext.Phase.Connect, action.attemptID, clientError.connectionFailures)),
).withoutHome(),
)
}
Expand Down Expand Up @@ -1079,7 +1082,10 @@ class AppFeature : Reducer<AppFeature.State, AppFeature.Action> {
next = next.copy(connection = State.Connection.Connecting, connectionGeneration = generation, connectionAttempt = attempt)
if (!keepingHome) next = next.withoutHome()
val link = dependency(ServerLinkClient)
val localNetwork = dependency(LocalNetworkPermission)
val health = Effect.run<Action> { send ->
// Android 17 asks on the first local network connection, as iOS does.
if (localNetwork.blocks(attempt.server)) localNetwork.request()
val result = try {
Result.success(link.probe(attempt.server))
} catch (error: OpenCodeClientError) {
Expand All @@ -1097,6 +1103,12 @@ class AppFeature : Reducer<AppFeature.State, AppFeature.Action> {
)
}

/** A failure while local network access is off says so, instead of the bare timeout. */
private fun failed(error: OpenCodeClientError, server: OpenCodeServer, context: FailureContext): State.Connection.Failed {
if (!dependency(LocalNetworkPermission).blocks(server)) return State.Connection.Failed(message = error.message, context = context)
return State.Connection.Failed(message = LocalNetworkPermission.DENIED_MESSAGE, context = context, localNetworkDenied = true)
}

companion object {
/** A typed address without a scheme means plain HTTP on a LAN or tailnet (iOS `withDefaultScheme`). */
fun withDefaultScheme(address: String): String {
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/kotlin/ai/opencode/app/AppFeaturePairing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import ai.opencode.core.store.cancellable
import ai.opencode.core.ui.ThisDevice
import ai.opencode.feature.drawer.HomeFeature
import ai.opencode.feature.drawer.notices.AccessibilityAnnouncer
import ai.opencode.server.LocalNetworkPermission
import ai.opencode.server.OpenCodeClient
import ai.opencode.server.OpenCodeClientError
import ai.opencode.server.OpenCodePairingLink
Expand Down Expand Up @@ -146,9 +147,11 @@ internal fun redeem(link: OpenCodePairingLink, state: AppFeature.State): Reduced
connectionAttempt = attempt,
)
val client = dependency(OpenCodeClient)
val localNetwork = dependency(LocalNetworkPermission)
return Reduced(
next,
Effect.run<AppFeature.Action> { send ->
if (localNetwork.blocks(normalized.server)) localNetwork.request()
val result = try {
Result.success(client.redeemPairing(normalized))
} catch (error: OpenCodeClientError) {
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/kotlin/ai/opencode/app/AppView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import ai.opencode.core.ui.OpenCodeIcon
import ai.opencode.core.ui.OpenCodeIconButton
import ai.opencode.core.ui.OpenCodeIconButtonStyle
import ai.opencode.core.ui.OpenCodeMinTapTarget
import ai.opencode.core.ui.OpenCodePrimaryButton
import ai.opencode.core.ui.OpenCodeSecondaryButton
import ai.opencode.core.ui.OpenCodeTextStyles
import ai.opencode.core.ui.OpenCodeTheme
Expand Down Expand Up @@ -650,6 +651,7 @@ private fun errorTitle(phase: AppFeature.FailureContext.Phase?, context: AppFeat
private fun ConnectionErrorCard(store: StoreOf<AppFeature.State, AppFeature.Action>, message: String, announces: Boolean) {
val phase by store.observe { it.errorPhase }
val context by store.observe { it.errorContext }
val needsLocalNetwork by store.observe { it.errorNeedsLocalNetwork }
var didCopy by remember(message) { mutableStateOf(false) }
val title = errorTitle(phase, context)
val secondary = OpenCodeTheme.colors.secondaryText
Expand Down Expand Up @@ -683,6 +685,9 @@ private fun ConnectionErrorCard(store: StoreOf<AppFeature.State, AppFeature.Acti
}
}
}
if (needsLocalNetwork) {
OpenCodePrimaryButton(onClick = { store.send(AppFeature.Action.LocalNetworkSettingsTapped) }) { Text("Open Settings") }
}
OpenCodeSecondaryButton(onClick = {
store.state.errorReport(BuildConfig.VERSION_NAME, BuildConfig.VERSION_CODE.toString(), java.time.Instant.now())?.let {
store.send(AppFeature.Action.CopyTextTapped(it))
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/kotlin/ai/opencode/app/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ai.opencode.feature.system.LeftToRight
import ai.opencode.feature.system.NotificationDestination
import ai.opencode.feature.system.SystemAction
import ai.opencode.feature.system.share.ShareConversation
import ai.opencode.server.LocalNetworkPermission
import ai.opencode.server.OpenCodePairingLink
import ai.opencode.core.dependencies.dependency
import ai.opencode.core.diagnostics.PerfFrameSampler
Expand Down Expand Up @@ -62,6 +63,11 @@ class MainActivity : ComponentActivity() {
microphoneRequest?.complete(granted)
microphoneRequest = null
}
private var localNetworkRequest: CompletableDeferred<Boolean>? = null
private val localNetworkLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) { granted ->
localNetworkRequest?.complete(granted)
localNetworkRequest = null
}
private var removeRequesters: List<() -> Unit> = emptyList()

override fun attachBaseContext(newBase: Context) {
Expand Down Expand Up @@ -180,9 +186,18 @@ class MainActivity : ComponentActivity() {
granted
},
dependency(DictationPermissionClient).register { requestMicrophone() },
dependency(LocalNetworkPermission).register { requestLocalNetwork() },
)
}

private suspend fun requestLocalNetwork(): Boolean {
val pending = localNetworkRequest ?: CompletableDeferred<Boolean>().also {
localNetworkRequest = it
localNetworkLauncher.launch(LocalNetworkPermission.PERMISSION)
}
return pending.await()
}

private suspend fun requestMicrophone(): Boolean {
if (checkSelfPermission(Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED) return true
val pending = microphoneRequest ?: CompletableDeferred<Boolean>().also {
Expand Down
1 change: 1 addition & 0 deletions docs/parity/app.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Status values: `todo`, `partial: <what is missing>`, `done`, `n/a: <reason and w
| `app/src/main/res/xml/data_extraction_rules.xml` | backup and device transfer exclude `support/` (incl. `composer-state.json`, `ComposerAttachments/`) and `documents/`, as iOS sets `isExcludedFromBackup` on file-stored shared values and the composer's state; preferences back up like UserDefaults |
| `app/build.gradle.kts` (`androidResources.localeFilters += "en"`) | packages English resources only, as iOS's English-only bundle, so library strings (Material's "Close sheet", "Drag handle") read in English beside the app's own under any system language (Y-03). Revisit with translations |
| `app/src/main/res/xml/network_security_config.xml` | cleartext permitted (the config can't name address ranges); iOS's ATS policy (`Info.plist` local networking + overlay `NSExceptionDomains`) is enforced in code by `server/.../TransportSecurity.kt` on the live transport |
| `server/.../LocalNetworkPermission.kt`, `MainActivity.kt` (`requestLocalNetwork`), `AppFeature.kt` (`connect`, `failed`), `AppFeaturePairing.kt` (`redeem`) | Android 17 blocks the local network until the app holds `ACCESS_LOCAL_NETWORK` (Nearby devices), so connecting or pairing to a server with a local network address asks first, and a failure while it is off reads “OpenCode isn’t allowed to reach devices on your local network…” with Open Settings; iOS shows its own local network prompt (no `NSLocalNetworkUsageDescription`). |
| `app/src/main/AndroidManifest.xml` (`android:supportsRtl="false"`) | layout direction matches iOS: iOS ships English only (no `*.lproj`, no `CFBundleLocalizations`; `OpenCode/Info.plist:5-6`), so UIKit lays it out left-to-right under an Arabic or Hebrew language. Without RTL support Android resolves every window of the app (MainActivity, ShareActivity, Ask OpenCode, widget configuration, Compose dialogs, sheets and popups, which inherit the root's `LocalLayoutDirection`) and the widgets' RemoteViews to LTR. The configuration itself still says RTL under Arabic or Hebrew, and Compose popups and dialogs start from it, so their text kept an RTL paragraph (“…Manage Servers”); each Compose activity therefore applies `LeftToRight.configuration()` (`feature/system/.../LeftToRight.kt`) in `attachBaseContext`; text keeps its own paragraph direction (Arabic or Hebrew titles still read right-to-left). Revisit when RTL translations ship: the composer, + menu and code blocks place pieces from LTR frames and need RTL-aware placement first (Y-01) |

## Tests
Expand Down
Loading
Loading