diff --git a/.gitignore b/.gitignore
index 94af20a..559bf7b 100644
--- a/.gitignore
+++ b/.gitignore
@@ -10,4 +10,5 @@ captures/
keystore.properties
*.jks
*.keystore
+keystore/
.git-commit-helper.sh
diff --git a/app/build.gradle.kts b/app/build.gradle.kts
index 02298f0..f6974f1 100644
--- a/app/build.gradle.kts
+++ b/app/build.gradle.kts
@@ -11,8 +11,8 @@ android {
applicationId = "dev.vibex.companion"
minSdk = 26
targetSdk = 35
- versionCode = 18
- versionName = "0.4.5"
+ versionCode = 20
+ versionName = "0.4.7"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
@@ -24,7 +24,10 @@ android {
isMinifyEnabled = false
}
}
- buildFeatures { compose = true }
+ buildFeatures {
+ compose = true
+ buildConfig = true
+ }
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index b51c42a..bd35061 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -4,12 +4,14 @@
+
+
+ android:windowSoftInputMode="stateHidden|adjustResize">
@@ -41,5 +43,14 @@
android:name=".monitor.CompanionMonitorService"
android:exported="false"
android:foregroundServiceType="dataSync" />
+
+
+
diff --git a/app/src/main/java/dev/vibex/companion/app/CompanionModel.kt b/app/src/main/java/dev/vibex/companion/app/CompanionModel.kt
index 3a4153d..e900313 100644
--- a/app/src/main/java/dev/vibex/companion/app/CompanionModel.kt
+++ b/app/src/main/java/dev/vibex/companion/app/CompanionModel.kt
@@ -82,6 +82,7 @@ import dev.vibex.companion.setKanbanSessionPinned
import dev.vibex.companion.updateKanbanSessionStatus
import dev.vibex.companion.app.data.CredentialStore
import dev.vibex.companion.app.data.HostProfile
+import dev.vibex.companion.app.data.ModelPreferencesStore
import dev.vibex.companion.app.data.OfflineStore
import dev.vibex.companion.app.data.ProfileStore
import dev.vibex.companion.app.data.StoredReachability
@@ -111,6 +112,7 @@ import dev.vibex.companion.app.monitor.SystemNotificationAction
import dev.vibex.companion.app.monitor.SystemNotifier
import dev.vibex.companion.app.monitor.TaskLiveActivityState
import dev.vibex.companion.app.monitor.VibeXActivityPhase
+import dev.vibex.companion.app.ui.theme.AppLanguage
import dev.vibex.companion.app.ui.theme.Appearance
import dev.vibex.companion.app.ui.theme.AccentChoice
import dev.vibex.companion.app.net.EventSocket
@@ -125,6 +127,12 @@ import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
+enum class CloudNodePreference {
+ Auto,
+ OfficialCloud,
+ CustomNode,
+}
+
data class UiState(
val profiles: List = emptyList(),
val selectedHostId: String? = null,
@@ -151,6 +159,11 @@ data class UiState(
val monitor: Boolean = true,
val appearance: Appearance = Appearance.System,
val accent: AccentChoice = AccentChoice.Blue,
+ val language: AppLanguage = AppLanguage.ZhHans,
+ val confirmHighRiskApproval: Boolean = true,
+ val muteForegroundNotifications: Boolean = true,
+ val customRelayOrigin: String = "",
+ val pendingQrNodeChoiceRaw: String? = null,
val streamDisplay: StreamDisplayPrefs = StreamDisplayPrefs(),
val hostVersion: String = "",
val hostProtocol: String = "",
@@ -191,6 +204,12 @@ data class UiState(
val accountError: String? = null,
val accountLoginRequested: Boolean = false,
val desktopConnect: DesktopConnectSheet? = null,
+ val connectedSasNotice: ConnectedSasNotice? = null,
+ val isAutoConnectingInBackground: Boolean = false,
+ val autoConnectingHostName: String? = null,
+ val lanSasState: LanSasInlineState? = null,
+ val collabUnreadCount: Int = 0,
+ val manuallyCompletedSessionIds: Set = emptySet(),
val route: Route = Route.Conversations,
) {
/** Signed out with nothing paired, or right after an explicit sign-out. */
@@ -204,6 +223,7 @@ sealed class Route {
data object ManualPair : Route()
data object Folders : Route()
data object Conversations : Route()
+ data object NewThread : Route()
data object Status : Route()
data object Collaboration : Route()
data class Settings(val section: SettingsSection? = null) : Route()
@@ -219,9 +239,15 @@ data class CollabDetailBundle(
)
private const val PREF_ACCOUNT_LOGIN_REQUESTED = "account_login_requested"
+private const val PREF_MANUALLY_COMPLETED_SESSIONS = "vibex.manuallyCompletedSessionIDs"
+
+/** The desktop revokes a phone when another phone connects, it signs out, or the user removes it. */
+private const val REVOKED_FAILURE_REASON = "连接授权已失效"
+private const val REVOKED_NOTICE = "电脑已收回本机的连接授权,请重新连接"
class CompanionModel(application: Application) : AndroidViewModel(application) {
private val prefs = application.getSharedPreferences("vibex_companion_prefs", Context.MODE_PRIVATE)
+ val modelPrefs = ModelPreferencesStore(application)
private val profiles = ProfileStore(application)
private val credentials = CredentialStore(application)
private val offline = OfflineStore(application)
@@ -237,8 +263,10 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
private var pingJob: Job? = null
private var timelinePollJob: Job? = null
private var connectPollJob: Job? = null
+ private var lanSasPollJob: Job? = null
private var autoReconnectJob: Job? = null
private var noticeDismissJob: Job? = null
+ private var sasNoticeDismissJob: Job? = null
private var pendingConflictRetryAction: (() -> Unit)? = null
private val declinedCloudFallbackHostIds = java.util.concurrent.ConcurrentHashMap.newKeySet()
private val notifiedEventKeys = java.util.concurrent.ConcurrentHashMap.newKeySet()
@@ -254,9 +282,7 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
@Volatile
private var didSeedKnownConversations: Boolean = false
@Volatile
- private var hasAutoOpenedInitialConversation: Boolean = false
- @Volatile
- private var userRequestedNewChatLanding: Boolean = false
+ private var didAttemptInitialAccountAutoConnect: Boolean = false
@Volatile
private var boostPollingUntil: Long = 0L
@@ -268,12 +294,40 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
}
}
+ fun presentConnectedSasNotice(hostName: String, sasCode: String) {
+ val code = sasCode.trim()
+ if (code.isEmpty()) return
+ val notice = ConnectedSasNotice(
+ id = java.util.UUID.randomUUID().toString(),
+ hostName = hostName.ifBlank { "电脑" },
+ sasCode = code,
+ )
+ sasNoticeDismissJob?.cancel()
+ _state.update { it.copy(connectedSasNotice = notice) }
+ sasNoticeDismissJob = viewModelScope.launch {
+ delay(4_500L)
+ _state.update { current ->
+ if (current.connectedSasNotice?.id == notice.id) current.copy(connectedSasNotice = null) else current
+ }
+ }
+ }
+
+ fun dismissConnectedSasNotice() {
+ sasNoticeDismissJob?.cancel()
+ sasNoticeDismissJob = null
+ _state.update { it.copy(connectedSasNotice = null) }
+ }
+
private val _state = MutableStateFlow(UiState())
val state: StateFlow = _state
init {
val stored = profiles.load()
val savedAutoConnect = prefs.getBoolean("auto_connect", true)
+ val savedManualDone = prefs.getStringSet(PREF_MANUALLY_COMPLETED_SESSIONS, emptySet())
+ .orEmpty()
+ .filter { it.isNotBlank() }
+ .toSet()
val selectedHostId = stored.selectedHostId ?: stored.profiles.firstOrNull()?.hostId
val selected = stored.profiles.find { it.hostId == selectedHostId }
val hasAccountToken = accountRepo.getToken() != null
@@ -283,10 +337,15 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
selectedProjectId = selected?.selectedProjectId,
appearance = stored.appearance,
accent = if (stored.accent == AccentChoice.Ink) AccentChoice.Blue else stored.accent,
+ language = stored.language,
+ confirmHighRiskApproval = stored.confirmHighRiskApproval,
+ muteForegroundNotifications = stored.muteForegroundNotifications,
+ customRelayOrigin = stored.customRelayOrigin,
streamDisplay = stored.streamDisplay,
autoConnect = savedAutoConnect,
accountSignedIn = hasAccountToken,
accountLoginRequested = prefs.getBoolean(PREF_ACCOUNT_LOGIN_REQUESTED, false),
+ manuallyCompletedSessionIds = savedManualDone,
route = Route.Conversations,
connection = if (stored.profiles.isEmpty() || !savedAutoConnect) ConnectionState.Offline else ConnectionState.Connecting,
connectionFailureReason = if (!savedAutoConnect && stored.profiles.isNotEmpty()) "自动连接已关闭" else null,
@@ -324,7 +383,10 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
)
}
+ private var isAppInForeground: Boolean = true
+
fun onAppForegroundChanged(inForeground: Boolean) {
+ isAppInForeground = inForeground
if (inForeground) {
// If the live activity was only a temporary background standby pill, clear it on return
val currentLive = _state.value.liveActivity
@@ -467,19 +529,39 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
}
}
- /** Opens the「发现你的电脑」popup and refreshes the account's desktops and the LAN scan. */
+ /** Opens the「连接你的电脑」popup and refreshes the account's desktops and the LAN scan. */
fun openDesktopConnect() {
- if (!_state.value.accountSignedIn) return
connectPollJob?.cancel()
- _state.update { it.copy(desktopConnect = DesktopConnectSheet()) }
- refreshAccount()
+ val signedIn = _state.value.accountSignedIn
+ val initialStage = if (signedIn && _state.value.accountDesktops.isEmpty()) {
+ DesktopConnectStage.Syncing
+ } else {
+ DesktopConnectStage.Choosing
+ }
+ val initialSelected = _state.value.accountDesktops.firstOrNull()?.hostId
+ _state.update {
+ it.copy(
+ desktopConnect = DesktopConnectSheet(stage = initialStage, selectedHostId = initialSelected),
+ isAutoConnectingInBackground = false,
+ autoConnectingHostName = null,
+ )
+ }
+ if (signedIn) {
+ refreshAccount()
+ }
discoverHosts()
}
fun dismissDesktopConnect() {
connectPollJob?.cancel()
connectPollJob = null
- _state.update { it.copy(desktopConnect = null) }
+ _state.update {
+ it.copy(
+ desktopConnect = null,
+ isAutoConnectingInBackground = false,
+ autoConnectingHostName = null,
+ )
+ }
}
fun selectAccountDesktop(hostId: String) {
@@ -490,74 +572,343 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
fun retryDesktopConnect() {
val stage = _state.value.desktopConnect?.stage as? DesktopConnectStage.Failed ?: return
- val route = stage.route
- if (route == null) openDesktopConnect() else connectAccountDesktop(route)
+ val target = stage.retryTarget
+ if (target == null) openDesktopConnect() else connectDesktopTarget(target)
}
/** LAN origin of an account desktop when the current Wi-Fi scan found it. */
fun lanOriginFor(desktop: AccountDesktop): String? =
- _state.value.discoveredHosts.firstOrNull { it.hostId == desktop.hostId }?.origin
+ _state.value.discoveredHosts.firstOrNull {
+ !it.isCloudService && AccountDesktops.rawHostId(it.hostId.orEmpty()) == desktop.hostId
+ }?.origin
- fun connectAccountDesktop(route: DesktopConnectRoute, replaceExistingMobile: Boolean = false) {
+ fun connectSelectedAccountDesktop() {
val current = _state.value
- val sheet = current.desktopConnect ?: return
- val desktop = current.accountDesktops.firstOrNull { it.hostId == sheet.selectedHostId } ?: return
- val originUrl = when (route) {
- DesktopConnectRoute.Lan -> lanOriginFor(desktop) ?: return
- DesktopConnectRoute.Cloud -> AccountDesktops.tenantOrigin(
- AccountDesktops.relayBase(current.accountOverview?.assignedRelayServer, PairingClient.DEFAULT_OFFICIAL_RELAY_BASE),
- desktop.hostId,
- )
+ val hostId = current.desktopConnect?.selectedHostId ?: current.accountDesktops.firstOrNull()?.hostId
+ val desktop = current.accountDesktops.firstOrNull { it.hostId == hostId } ?: current.accountDesktops.firstOrNull() ?: return
+ connectDesktopTarget(DesktopConnectTarget.Account(desktop))
+ }
+
+ fun connectLanHostInSheet(host: DiscoveredLanHost) {
+ connectDesktopTarget(DesktopConnectTarget.Lan(host))
+ }
+
+ fun connectDesktopTarget(
+ target: DesktopConnectTarget,
+ replaceExistingMobile: Boolean = false,
+ silentUntilNeeded: Boolean = false,
+ ) {
+ fun setStage(stage: DesktopConnectStage) {
+ _state.update { current ->
+ val baseSheet = current.desktopConnect ?: DesktopConnectSheet(
+ selectedHostId = (target as? DesktopConnectTarget.Account)?.desktop?.hostId
+ ?: current.accountDesktops.firstOrNull()?.hostId,
+ )
+ current.copy(
+ desktopConnect = baseSheet.copy(stage = stage),
+ isAutoConnectingInBackground = false,
+ autoConnectingHostName = null,
+ )
+ }
}
- val origin = runCatching { HostOrigin.parse(originUrl) }.getOrNull() ?: return
- if (route == DesktopConnectRoute.Cloud) PairingClient.rememberRelayBase(origin.value)
- val reachabilityKind = if (route == DesktopConnectRoute.Lan) "lan" else "published"
- fun setStage(stage: DesktopConnectStage) =
- _state.update { it.copy(desktopConnect = it.desktopConnect?.copy(stage = stage)) }
connectPollJob?.cancel()
connectPollJob = viewModelScope.launch {
- setStage(DesktopConnectStage.Connecting(route))
- val saved = current.accountSavedHosts.firstOrNull {
- AccountDesktops.rawHostId(it.hostId) == desktop.hostId && it.accessToken.isNotBlank()
- }
- if (saved != null) {
- val existing = current.profiles.firstOrNull { AccountDesktops.rawHostId(it.hostId) == desktop.hostId }
- val hostId = existing?.hostId ?: desktop.hostId
- credentials.put(hostId, saved.accessToken)
- val base = existing ?: HostProfile(
- hostId = hostId,
- name = desktop.hostName,
- deviceId = saved.deviceId.ifBlank { saved.profileId },
- reachability = emptyList(),
- lastSuccessOrigin = null,
- lastSyncAt = System.currentTimeMillis(),
- )
- val profile = base.copy(
- reachability = (listOf(StoredReachability(origin.value, reachabilityKind)) + base.reachability)
- .distinctBy { it.origin },
+ if (silentUntilNeeded) {
+ _state.update {
+ it.copy(
+ isAutoConnectingInBackground = true,
+ autoConnectingHostName = target.displayName,
+ )
+ }
+ } else {
+ setStage(DesktopConnectStage.Connecting(target))
+ }
+
+ val candidates = mutableListOf>()
+ when (target) {
+ is DesktopConnectTarget.Account -> {
+ val desktop = target.desktop
+ var lanUrl = lanOriginFor(desktop)
+ if (lanUrl == null) {
+ val scanned = withContext(Dispatchers.IO) {
+ runCatching { PairingClient.discoverLanHosts() }.getOrDefault(emptyList())
+ }
+ if (scanned.isNotEmpty()) {
+ _state.update { it.copy(discoveredHosts = scanned) }
+ lanUrl = scanned.firstOrNull {
+ !it.isCloudService && AccountDesktops.rawHostId(it.hostId.orEmpty()) == desktop.hostId
+ }?.origin
+ }
+ }
+ lanUrl?.let { url ->
+ runCatching { HostOrigin.parse(url) }.getOrNull()?.let { candidates += it to "lan" }
+ }
+ val current = _state.value
+ val canUseCloud = current.accountOverview?.canUseCloudRelay ?: true
+ if (canUseCloud) {
+ val cloudUrl = AccountDesktops.tenantOrigin(
+ AccountDesktops.relayBase(
+ current.accountOverview?.assignedRelayServer,
+ PairingClient.DEFAULT_OFFICIAL_RELAY_BASE,
+ ),
+ desktop.hostId,
+ )
+ runCatching { HostOrigin.parse(cloudUrl) }.getOrNull()?.let { origin ->
+ PairingClient.rememberRelayBase(origin.value)
+ candidates += origin to "published"
+ }
+ }
+ }
+ is DesktopConnectTarget.Lan -> {
+ val kind = if (target.host.isCloudService) "published" else "lan"
+ runCatching { HostOrigin.parse(target.host.origin) }.getOrNull()?.let {
+ candidates += it to kind
+ }
+ }
+ }
+
+ if (candidates.isEmpty()) {
+ if (silentUntilNeeded) {
+ _state.update { it.copy(isAutoConnectingInBackground = false, autoConnectingHostName = null) }
+ } else {
+ setStage(DesktopConnectStage.Failed("当前不在同一 Wi-Fi,且账号未开通云服务", target))
+ }
+ return@launch
+ }
+
+ // 1. Try existing synced/local credential fast-path if available
+ if (target is DesktopConnectTarget.Account) {
+ val desktop = target.desktop
+ val current = _state.value
+ val saved = current.accountSavedHosts.firstOrNull {
+ AccountDesktops.rawHostId(it.hostId) == desktop.hostId && it.accessToken.isNotBlank()
+ }
+ if (saved != null) {
+ val existing = current.profiles.firstOrNull { AccountDesktops.rawHostId(it.hostId) == desktop.hostId }
+ val hostId = existing?.hostId ?: desktop.hostId
+ credentials.put(hostId, saved.accessToken)
+ val primary = candidates.first()
+ val base = existing ?: HostProfile(
+ hostId = hostId,
+ name = desktop.hostName,
+ deviceId = saved.deviceId.ifBlank { saved.profileId },
+ reachability = emptyList(),
+ lastSuccessOrigin = null,
+ lastSyncAt = System.currentTimeMillis(),
+ )
+ val mergedReach = (candidates.map { StoredReachability(it.first.value, it.second) } + base.reachability)
+ .distinctBy { it.origin }
+ val profile = base.copy(
+ reachability = mergedReach,
+ lastSuccessOrigin = primary.first.value,
+ )
+ upsertProfile(profile, selected = true)
+ connect(profile, allowCloudSwitch = candidates.any { it.second == "published" })
+ val revoked = _state.value.profiles.none { it.hostId == hostId }
+ if (!revoked) {
+ if (_state.value.connection == ConnectionState.Online) {
+ _state.update {
+ it.copy(
+ desktopConnect = null,
+ isAutoConnectingInBackground = false,
+ autoConnectingHostName = null,
+ )
+ }
+ return@launch
+ }
+ if (silentUntilNeeded) {
+ _state.update { it.copy(isAutoConnectingInBackground = false, autoConnectingHostName = null) }
+ } else {
+ setStage(DesktopConnectStage.Failed(_state.value.connectionFailureReason ?: "电脑离线", target))
+ }
+ return@launch
+ }
+ _state.update {
+ it.copy(
+ error = null,
+ connectionFailureReason = null,
+ accountSavedHosts = it.accountSavedHosts - saved,
+ )
+ }
+ }
+ }
+
+ // 2. Request connect (with optional same-account JWT for zero-click auto-approval)
+ val accountToken = accountRepo.getToken()
+ var lastFailure: Throwable? = null
+ var chosenOrigin: HostOrigin? = null
+ var chosenKind = "lan"
+ var requestedResult: dev.vibex.companion.DeviceConnectRequestResult? = null
+
+ for ((origin, kind) in candidates) {
+ val attempt = withContext(Dispatchers.IO) {
+ runCatching {
+ pairingClient.requestConnect(
+ origin = origin,
+ deviceName = Build.MODEL.ifBlank { "Android Companion" },
+ replaceExistingMobile = replaceExistingMobile,
+ accountToken = accountToken,
+ )
+ }
+ }
+ val ok = attempt.getOrNull()
+ if (ok != null) {
+ chosenOrigin = origin
+ chosenKind = kind
+ requestedResult = ok
+ break
+ }
+ val err = attempt.exceptionOrNull()
+ if (err is MobileDeviceConflictException) {
+ _state.update { it.copy(isAutoConnectingInBackground = false, autoConnectingHostName = null) }
+ pendingConflictRetryAction = {
+ connectDesktopTarget(target, replaceExistingMobile = true, silentUntilNeeded = false)
+ }
+ _state.update { it.copy(pendingConflictDeviceName = err.existingDeviceName) }
+ return@launch
+ }
+ if (err is AccountLinkException && err.failure == AccountLinkFailure.AccountTokenInvalid) {
+ _state.update { it.copy(isAutoConnectingInBackground = false, autoConnectingHostName = null) }
+ signOutAccount()
+ return@launch
+ }
+ // If account verification mismatched/signed out on LAN, retry without accountToken so manual 4-digit SAS works
+ if (err is AccountLinkException && (
+ err.failure == AccountLinkFailure.AccountMismatch ||
+ err.failure == AccountLinkFailure.HostAccountSignedOut ||
+ err.failure == AccountLinkFailure.VerificationUnavailable
+ )
+ ) {
+ val unauthAttempt = withContext(Dispatchers.IO) {
+ runCatching {
+ pairingClient.requestConnect(
+ origin = origin,
+ deviceName = Build.MODEL.ifBlank { "Android Companion" },
+ replaceExistingMobile = replaceExistingMobile,
+ accountToken = null,
+ )
+ }
+ }
+ val unauthOk = unauthAttempt.getOrNull()
+ if (unauthOk != null) {
+ chosenOrigin = origin
+ chosenKind = kind
+ requestedResult = unauthOk
+ break
+ }
+ }
+ lastFailure = err
+ }
+
+ val origin = chosenOrigin
+ val requested = requestedResult
+ if (origin == null || requested == null) {
+ if (silentUntilNeeded) {
+ _state.update { it.copy(isAutoConnectingInBackground = false, autoConnectingHostName = null) }
+ } else {
+ val msg = lastFailure?.let(::desktopConnectErrorMessage) ?: "无法连接到电脑,请确认电脑端在线"
+ setStage(DesktopConnectStage.Failed(msg, target))
+ }
+ return@launch
+ }
+
+ suspend fun adoptApprovedConnection(
+ cred: dev.vibex.remote.v1.DeviceCredential,
+ rawHostId: String,
+ rawHostName: String?,
+ sasCode: String,
+ ) {
+ val resolvedHostId = rawHostId.ifBlank { target.hostId.orEmpty() }.ifBlank { "host:${origin.value.hashCode()}" }
+ credentials.put(resolvedHostId, cred.access_token)
+ val allReach = (
+ listOf(StoredReachability(origin.value, chosenKind)) +
+ candidates.map { StoredReachability(it.first.value, it.second) }
+ ).distinctBy { it.origin }
+ val profile = HostProfile(
+ hostId = resolvedHostId,
+ name = rawHostName?.ifBlank { null } ?: target.displayName,
+ deviceId = cred.device_id,
+ reachability = allReach,
lastSuccessOrigin = origin.value,
+ lastSyncAt = System.currentTimeMillis(),
+ selectedProjectId = null,
)
upsertProfile(profile, selected = true)
- connect(profile, allowCloudSwitch = route == DesktopConnectRoute.Cloud)
- val revoked = _state.value.profiles.none { it.hostId == hostId }
- if (!revoked) {
- if (_state.value.connection != ConnectionState.Online) {
- setStage(DesktopConnectStage.Failed(_state.value.connectionFailureReason ?: "电脑离线", route))
+ setAccountLoginRequested(false)
+ _state.update {
+ it.copy(
+ desktopConnect = null,
+ isAutoConnectingInBackground = false,
+ autoConnectingHostName = null,
+ lanSasState = null,
+ route = if (it.route == Route.ManualPair) Route.Conversations else it.route,
+ )
+ }
+ presentConnectedSasNotice(profile.name, sasCode)
+ SystemNotifier.notifyStatus(getApplication(), "已连接电脑", "${profile.name} · 连接码 $sasCode")
+ connect(profile, allowCloudSwitch = candidates.any { it.second == "published" })
+ if (_state.value.connection != ConnectionState.Online && !silentUntilNeeded) {
+ setStage(DesktopConnectStage.Failed(_state.value.connectionFailureReason ?: "电脑离线", target))
+ }
+ }
+
+ // Same-account auto-approved in 0ms!
+ if (requested.status == "approved" && requested.credential != null) {
+ adoptApprovedConnection(
+ cred = requested.credential!!,
+ rawHostId = requested.hostId,
+ rawHostName = requested.hostName,
+ sasCode = requested.sasCode,
+ )
+ return@launch
+ }
+
+ // Manual approval required on desktop: show 4-digit SAS code
+ val displayHost = requested.hostName?.ifBlank { null } ?: target.displayName
+ setStage(DesktopConnectStage.AwaitingApproval(target, requested.sasCode, displayHost))
+
+ while (isActive) {
+ delay(1_200)
+ val poll = withContext(Dispatchers.IO) {
+ runCatching { pairingClient.pollConnect(origin, requested.requestId) }.getOrNull()
+ } ?: continue
+ when (poll.status) {
+ "approved" -> {
+ val cred = poll.credential ?: continue
+ adoptApprovedConnection(
+ cred = cred,
+ rawHostId = poll.hostId,
+ rawHostName = poll.hostName ?: requested.hostName,
+ sasCode = poll.sasCode.ifBlank { requested.sasCode },
+ )
+ return@launch
+ }
+ "rejected" -> {
+ setStage(DesktopConnectStage.Failed(DESKTOP_APPROVAL_REJECTED, target))
+ return@launch
+ }
+ "expired" -> {
+ setStage(DesktopConnectStage.Failed(DESKTOP_APPROVAL_EXPIRED, target))
+ return@launch
}
- return@launch
}
- // The synced credential was revoked on the desktop; ask it for a fresh one instead.
- _state.update { it.copy(error = null, connectionFailureReason = null, accountSavedHosts = it.accountSavedHosts - saved) }
}
+ }
+ }
- val accountToken = accountRepo.getToken()
- if (accountToken == null) {
- signOutAccount()
+ fun connectLanHostInline(host: DiscoveredLanHost, replaceExistingMobile: Boolean = false) {
+ lanSasPollJob?.cancel()
+ lanSasPollJob = viewModelScope.launch {
+ _state.update { it.copy(lanSasState = LanSasInlineState.Connecting(host), error = null) }
+ val origin = runCatching { HostOrigin.parse(host.origin) }.getOrNull()
+ if (origin == null) {
+ _state.update { it.copy(lanSasState = null, error = "无效的电脑地址") }
return@launch
}
+ val accountToken = accountRepo.getToken()
val requested = withContext(Dispatchers.IO) {
- runCatching {
+ val primary = runCatching {
pairingClient.requestConnect(
origin = origin,
deviceName = Build.MODEL.ifBlank { "Android Companion" },
@@ -565,53 +916,95 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
accountToken = accountToken,
)
}
- }.getOrElse { err ->
- when {
- err is MobileDeviceConflictException -> {
- pendingConflictRetryAction = { connectAccountDesktop(route, replaceExistingMobile = true) }
- _state.update { it.copy(pendingConflictDeviceName = err.existingDeviceName) }
+ if (primary.isSuccess) {
+ primary
+ } else {
+ val err = primary.exceptionOrNull()
+ if (accountToken != null && err is AccountLinkException) {
+ runCatching {
+ pairingClient.requestConnect(
+ origin = origin,
+ deviceName = Build.MODEL.ifBlank { "Android Companion" },
+ replaceExistingMobile = replaceExistingMobile,
+ accountToken = null,
+ )
+ }
+ } else {
+ primary
}
- err is AccountLinkException && err.failure == AccountLinkFailure.AccountTokenInvalid -> signOutAccount()
- else -> setStage(DesktopConnectStage.Failed(desktopConnectErrorMessage(err), route))
}
+ }.getOrElse { err ->
+ _state.update { it.copy(lanSasState = null) }
+ if (err is MobileDeviceConflictException) {
+ pendingConflictRetryAction = { connectLanHostInline(host, replaceExistingMobile = true) }
+ _state.update { it.copy(pendingConflictDeviceName = err.existingDeviceName) }
+ } else {
+ fail(err)
+ }
+ return@launch
+ }
+
+ suspend fun finishInlineApproved(
+ cred: dev.vibex.remote.v1.DeviceCredential,
+ rawHostId: String,
+ rawHostName: String?,
+ sasCode: String,
+ ) {
+ val resolvedHostId = rawHostId.ifBlank { host.hostId.orEmpty() }.ifBlank { "host:${origin.value.hashCode()}" }
+ credentials.put(resolvedHostId, cred.access_token)
+ val kind = if (host.isCloudService) "published" else "lan"
+ val profile = HostProfile(
+ hostId = resolvedHostId,
+ name = rawHostName?.ifBlank { null } ?: host.displayName,
+ deviceId = cred.device_id,
+ reachability = listOf(StoredReachability(origin.value, kind)),
+ lastSuccessOrigin = origin.value,
+ lastSyncAt = System.currentTimeMillis(),
+ selectedProjectId = null,
+ )
+ upsertProfile(profile, selected = true)
+ setAccountLoginRequested(false)
+ _state.update { it.copy(lanSasState = null, route = Route.Conversations, error = null) }
+ presentConnectedSasNotice(profile.name, sasCode)
+ connect(profile, allowCloudSwitch = host.isCloudService)
+ }
+
+ if (requested.status == "approved" && requested.credential != null) {
+ finishInlineApproved(
+ cred = requested.credential!!,
+ rawHostId = requested.hostId,
+ rawHostName = requested.hostName,
+ sasCode = requested.sasCode,
+ )
return@launch
}
- setStage(DesktopConnectStage.AwaitingApproval(route, requested.sasCode))
+ val hostLabel = requested.hostName?.ifBlank { null } ?: host.displayName
+ _state.update {
+ it.copy(lanSasState = LanSasInlineState.AwaitingApproval(host, requested.sasCode, hostLabel))
+ }
while (isActive) {
- delay(1_500)
+ delay(1_200)
val poll = withContext(Dispatchers.IO) {
runCatching { pairingClient.pollConnect(origin, requested.requestId) }.getOrNull()
} ?: continue
when (poll.status) {
"approved" -> {
val cred = poll.credential ?: continue
- val hostId = poll.hostId.ifBlank { desktop.hostId }
- credentials.put(hostId, cred.access_token)
- val profile = HostProfile(
- hostId = hostId,
- name = (poll.hostName ?: requested.hostName)?.ifBlank { null } ?: desktop.hostName,
- deviceId = cred.device_id,
- reachability = listOf(StoredReachability(origin.value, reachabilityKind)),
- lastSuccessOrigin = origin.value,
- lastSyncAt = System.currentTimeMillis(),
- selectedProjectId = null,
+ finishInlineApproved(
+ cred = cred,
+ rawHostId = poll.hostId,
+ rawHostName = poll.hostName ?: requested.hostName,
+ sasCode = poll.sasCode.ifBlank { requested.sasCode },
)
- upsertProfile(profile, selected = true)
- _state.update { it.copy(route = Route.Conversations) }
- SystemNotifier.notifyStatus(getApplication(), "已连接电脑", profile.name)
- connect(profile, allowCloudSwitch = route == DesktopConnectRoute.Cloud)
- if (_state.value.connection != ConnectionState.Online) {
- setStage(DesktopConnectStage.Failed(_state.value.connectionFailureReason ?: "电脑离线", route))
- }
return@launch
}
"rejected" -> {
- setStage(DesktopConnectStage.Failed(DESKTOP_APPROVAL_REJECTED, route))
+ _state.update { it.copy(lanSasState = null, error = DESKTOP_APPROVAL_REJECTED) }
return@launch
}
"expired" -> {
- setStage(DesktopConnectStage.Failed(DESKTOP_APPROVAL_EXPIRED, route))
+ _state.update { it.copy(lanSasState = null, error = DESKTOP_APPROVAL_EXPIRED) }
return@launch
}
}
@@ -619,6 +1012,12 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
}
}
+ fun cancelLanHostInline() {
+ lanSasPollJob?.cancel()
+ lanSasPollJob = null
+ _state.update { it.copy(lanSasState = null) }
+ }
+
fun selectProject(projectId: String) {
val profile = selectedProfile()?.copy(selectedProjectId = projectId) ?: return
@@ -676,6 +1075,57 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
persist { it.copy(appearance = appearance) }
}
+ fun setLanguage(language: AppLanguage) {
+ _state.update { it.copy(language = language) }
+ persist { it.copy(language = language) }
+ }
+
+ fun setConfirmHighRiskApproval(enabled: Boolean) {
+ _state.update { it.copy(confirmHighRiskApproval = enabled) }
+ persist { it.copy(confirmHighRiskApproval = enabled) }
+ }
+
+ fun setMuteForegroundNotifications(enabled: Boolean) {
+ _state.update { it.copy(muteForegroundNotifications = enabled) }
+ persist { it.copy(muteForegroundNotifications = enabled) }
+ }
+
+ fun setCustomRelayOrigin(raw: String) {
+ val normalized = normalizeCustomRelayOrigin(raw)
+ _state.update { it.copy(customRelayOrigin = normalized) }
+ persist { it.copy(customRelayOrigin = normalized) }
+ }
+
+ private fun normalizeCustomRelayOrigin(raw: String): String {
+ val trimmed = raw.trim().trimEnd('/')
+ if (trimmed.isEmpty()) return ""
+ return if (trimmed.startsWith("http://") || trimmed.startsWith("https://")) {
+ trimmed
+ } else {
+ "https://$trimmed"
+ }
+ }
+
+ private fun notifyAlertIfAllowed(
+ notificationKey: String,
+ title: String,
+ body: String,
+ conversationId: String? = null,
+ actions: List = emptyList(),
+ ) {
+ if (isAppInForeground && _state.value.muteForegroundNotifications) {
+ return
+ }
+ SystemNotifier.notifyAlert(
+ context = getApplication(),
+ notificationKey = notificationKey,
+ title = title,
+ body = body,
+ conversationId = conversationId,
+ actions = actions,
+ )
+ }
+
fun setAccent(accent: AccentChoice) {
_state.update { it.copy(accent = accent) }
persist { it.copy(accent = accent) }
@@ -709,16 +1159,28 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
}
}
- fun pairFromRaw(raw: String, replaceExistingMobile: Boolean = false) {
+ fun pairFromRaw(
+ raw: String,
+ replaceExistingMobile: Boolean = false,
+ nodePreference: CloudNodePreference? = null,
+ ) {
+ val hasCustomNode = _state.value.customRelayOrigin.isNotBlank()
+ val canChooseCloud = hasCustomNode && _state.value.accountSignedIn
+ if (nodePreference == null && canChooseCloud) {
+ _state.update { it.copy(pendingQrNodeChoiceRaw = raw) }
+ return
+ }
+ val effectivePreference = nodePreference ?: CloudNodePreference.Auto
viewModelScope.launch {
- _state.update { it.copy(pairing = true, error = null) }
+ _state.update { it.copy(pairing = true, error = null, pendingQrNodeChoiceRaw = null) }
runCatching {
- val invitation = PairingInvitationParser.parse(raw)
+ val parsed = PairingInvitationParser.parse(raw)
+ val invitation = applyNodePreference(parsed, effectivePreference)
redeemOrMerge(invitation, replaceExistingMobile = replaceExistingMobile)
}.onFailure { err ->
if (err is MobileDeviceConflictException) {
pendingConflictRetryAction = {
- pairFromRaw(raw, replaceExistingMobile = true)
+ pairFromRaw(raw, replaceExistingMobile = true, nodePreference = effectivePreference)
}
_state.update {
it.copy(
@@ -735,6 +1197,40 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
}
}
+ fun confirmPendingNodeChoice(preference: CloudNodePreference) {
+ val raw = _state.value.pendingQrNodeChoiceRaw ?: return
+ _state.update { it.copy(pendingQrNodeChoiceRaw = null) }
+ pairFromRaw(raw, replaceExistingMobile = false, nodePreference = preference)
+ }
+
+ fun cancelPendingNodeChoice() {
+ _state.update { it.copy(pendingQrNodeChoiceRaw = null) }
+ }
+
+ private fun applyNodePreference(
+ invitation: dev.vibex.companion.PairingInvitation,
+ preference: CloudNodePreference,
+ ): dev.vibex.companion.PairingInvitation {
+ val customBase = normalizeCustomRelayOrigin(_state.value.customRelayOrigin)
+ if (preference != CloudNodePreference.CustomNode || customBase.isEmpty()) {
+ return invitation
+ }
+ val customUrl = when {
+ customBase.contains("/t/") -> customBase
+ !invitation.hostId.isNullOrBlank() -> "$customBase/t/${invitation.hostId}"
+ else -> customBase
+ }
+ val customTarget = runCatching {
+ ReachabilityTarget(HostOrigin.parse(customUrl), "published")
+ }.getOrNull() ?: return invitation
+ val filtered = invitation.reachability.filterNot {
+ it.origin.value.contains("vibex.xforever.xin")
+ }
+ return invitation.copy(
+ reachability = listOf(customTarget) + filtered.filterNot { it.origin.value == customTarget.origin.value },
+ )
+ }
+
fun pairManual(origin: String, token: String, replaceExistingMobile: Boolean = false) {
viewModelScope.launch {
_state.update { it.copy(pairing = true, error = null) }
@@ -841,9 +1337,8 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
}
fun openNewChatLanding() {
- userRequestedNewChatLanding = true
timelinePollJob?.cancel()
- _state.update { it.copy(route = Route.Conversations, timeline = null) }
+ _state.update { it.copy(route = Route.NewThread, timeline = null) }
}
private var activeInlineConversationId: String? = null
@@ -853,8 +1348,6 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
fun openConversation(id: String) {
activeInlineConversationId = null
- userRequestedNewChatLanding = false
- hasAutoOpenedInitialConversation = true
_state.update { it.copy(route = Route.Timeline(id), timeline = sessions[id]?.snapshot()) }
viewModelScope.launch { attachConversation(id) }
startTimelinePoller(id)
@@ -882,7 +1375,9 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
delay(800)
while (isActive && currentObservedConversationId() == id) {
val session = sessions[id]
- val fast = session?.inFlightTurnId != null || System.currentTimeMillis() < boostPollingUntil
+ val fast = session?.inFlightTurnId != null ||
+ (session?.optimisticThinkingUntilMs ?: 0L) > System.currentTimeMillis() ||
+ System.currentTimeMillis() < boostPollingUntil
runCatching { syncConversationTimeline(id, forceRows = fast) }
if (fast) {
delay(900)
@@ -929,23 +1424,13 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
resolvedWorkspaceId,
resolvedAgentId,
title.ifBlank { null },
- prompt.ifBlank { null },
+ null,
)
}
locallyCreatedConversationIds.add(created.id)
knownConversationIds.add(created.id)
+ unmarkManuallyCompleted(created.id)
val agent = _state.value.catalog?.agents?.find { it.id == resolvedAgentId }
- withContext(Dispatchers.IO) {
- runCatching { api.ensureSessionControls(origin, token, created.id) }
- if (!modeId.isNullOrBlank()) {
- runCatching { api.setSessionMode(origin, token, created.id, modeId) }
- }
- config.forEach { (key, value) ->
- if (key.isNotBlank() && value.isNotBlank()) {
- runCatching { api.setSessionConfigOption(origin, token, created.id, key, value) }
- }
- }
- }
val boundAgent = created.agentId.ifBlank { resolvedAgentId }
val boundWorkspace = created.workspaceId.ifBlank { resolvedWorkspaceId }
val session = sessions.getOrPut(created.id) {
@@ -968,10 +1453,17 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
options = bound?.sessionConfig.orEmpty(),
selected = config,
)
+ if (prompt.isNotBlank()) {
+ boostPollingUntil = System.currentTimeMillis() + 30_000L
+ synchronized(session) {
+ session.beginOptimisticSend(prompt)
+ }
+ }
val summary = created.copy(
workspaceId = boundWorkspace,
title = title.ifBlank { created.title },
agentId = boundAgent,
+ status = if (prompt.isNotBlank()) "inprogress" else created.status,
)
_state.update { state ->
state.copy(
@@ -979,10 +1471,33 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
)
}
openConversation(created.id)
- if (prompt.isNotBlank()) {
- send(created.id, prompt)
+
+ // Apply mode/config & initial prompt in background without blocking navigation
+ viewModelScope.launch {
+ runCatching {
+ withContext(Dispatchers.IO) {
+ if (!modeId.isNullOrBlank()) {
+ runCatching { api.setSessionMode(origin, token, created.id, modeId) }
+ }
+ config.forEach { (key, value) ->
+ if (key.isNotBlank() && value.isNotBlank()) {
+ runCatching { api.setSessionConfigOption(origin, token, created.id, key, value) }
+ }
+ }
+ if (prompt.isNotBlank()) {
+ api.submitInput(origin, token, created.id, boundAgent, boundWorkspace, prompt)
+ }
+ }
+ if (prompt.isNotBlank()) {
+ syncConversationTimeline(created.id, forceRows = true)
+ }
+ runCatching { loadConversations(silent = true) }
+ }.onFailure { err ->
+ synchronized(session) { session.clearOptimisticSend() }
+ publishTimeline(session)
+ fail(err)
+ }
}
- viewModelScope.launch { runCatching { loadConversations() } }
}.onFailure { fail(it) }
}
}
@@ -1043,8 +1558,20 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
fun setSessionConfig(conversationId: String, key: String, value: String) {
if (!_state.value.connection.allowsWrites || key.isBlank()) return
sessions[conversationId]?.let { session ->
- val index = session.sessionConfig.indexOfFirst { it.key == key }
- if (index >= 0) session.sessionConfig[index] = session.sessionConfig[index].copy(value = value)
+ synchronized(session) {
+ val index = session.sessionConfig.indexOfFirst { it.key == key }
+ if (index >= 0) {
+ val existing = session.sessionConfig[index]
+ val nextChoices = if (existing.choices.any { it.value == value }) {
+ existing.choices
+ } else {
+ existing.choices + CatalogAgent.makeCustomChoice(value)
+ }
+ session.sessionConfig[index] = existing.copy(value = value, choices = nextChoices)
+ } else {
+ session.sessionConfig += CatalogAgent.makeCustomModelOption(key = key, modelId = value)
+ }
+ }
publishTimeline(session)
}
viewModelScope.launch {
@@ -1104,6 +1631,7 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
fun send(conversationId: String, text: String) {
if (!_state.value.connection.allowsWrites || text.isBlank()) return
+ unmarkManuallyCompleted(conversationId)
val summary = selectedConversation(conversationId)
val session = sessions.getOrPut(conversationId) {
TimelineSession().also {
@@ -1113,42 +1641,75 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
it.workspaceId = summary?.workspaceId.orEmpty()
}
}
- if (session.agentId.isBlank()) session.agentId = summary?.agentId.orEmpty()
- if (session.workspaceId.isBlank()) session.workspaceId = summary?.workspaceId.orEmpty()
+ val agentId = synchronized(session) {
+ if (session.agentId.isBlank()) session.agentId = summary?.agentId.orEmpty()
+ if (session.workspaceId.isBlank()) session.workspaceId = summary?.workspaceId.orEmpty()
+ val resolvedAg = session.agentId.ifBlank { summary?.agentId.orEmpty() }
+ .ifBlank {
+ _state.value.catalog?.agents?.firstOrNull { it.usable }?.id
+ ?: _state.value.catalog?.agents?.firstOrNull()?.id
+ ?: "claude_code"
+ }
+ session.agentId = resolvedAg
+ session.beginOptimisticSend(text)
+ resolvedAg
+ }
+ boostPollingUntil = System.currentTimeMillis() + 30_000L
+ publishTimeline(session)
+
viewModelScope.launch {
runCatching {
- val agentId = session.agentId.ifBlank { summary?.agentId.orEmpty() }
- .ifBlank {
- _state.value.catalog?.agents?.firstOrNull { it.usable }?.id
- ?: _state.value.catalog?.agents?.firstOrNull()?.id
- ?: "claude_code"
- }
- session.agentId = agentId
- val workspaceId = session.workspaceId.ifBlank { summary?.workspaceId.orEmpty() }
+ val workspaceId = synchronized(session) { session.workspaceId }
+ .ifBlank { summary?.workspaceId.orEmpty() }
.ifBlank { currentCollaborationWorkspaceId() }
if (workspaceId.isBlank()) error("会话还没有工作区")
- session.workspaceId = workspaceId
- boostPollingUntil = System.currentTimeMillis() + 30_000L
+ synchronized(session) { session.workspaceId = workspaceId }
val (origin, token) = live()
- val localId = "local:${System.currentTimeMillis()}"
- if (session.inFlightTurnId == null) session.inFlightTurnId = "local"
- session.rows += dev.vibex.companion.TimelineRow(
- id = localId,
- kind = "user",
- title = "",
- body = text,
- tone = dev.vibex.companion.TimelineTone.Quiet,
- )
- publishTimeline(session)
withContext(Dispatchers.IO) {
api.submitInput(origin, token, conversationId, agentId, workspaceId, text)
}
- syncConversationTimeline(conversationId)
- if (session.rows.any { it.kind == "user" && it.id != localId && it.body == text }) {
- session.rows.removeAll { it.id == localId }
- }
+ syncConversationTimeline(conversationId, forceRows = true)
+ }.onFailure { err ->
+ synchronized(session) { session.clearOptimisticSend() }
publishTimeline(session)
- }.onFailure { fail(it) }
+ fail(err)
+ }
+ }
+ }
+
+ fun markSessionViewed(conversationId: String) {
+ if (!_state.value.connection.allowsWrites || conversationId.isBlank()) return
+ viewModelScope.launch {
+ runCatching {
+ mutateKanbanSessionViewed(conversationId)
+ loadConversations(silent = true)
+ }
+ }
+ }
+
+ fun setManualSessionStatus(sessionId: String, status: KanbanSessionStatus) {
+ if (!_state.value.connection.allowsWrites) return
+ val nextSet = _state.value.manuallyCompletedSessionIds.toMutableSet().apply {
+ if (status == KanbanSessionStatus.Done) add(sessionId) else remove(sessionId)
+ }
+ prefs.edit().putStringSet(PREF_MANUALLY_COMPLETED_SESSIONS, nextSet).apply()
+ _state.update { it.copy(manuallyCompletedSessionIds = nextSet) }
+ viewModelScope.launch {
+ runCatching { mutateKanbanSessionStatus(sessionId, status) }.onFailure { fail(it) }
+ }
+ }
+
+ private fun unmarkManuallyCompleted(sessionId: String) {
+ if (sessionId !in _state.value.manuallyCompletedSessionIds) return
+ val nextSet = _state.value.manuallyCompletedSessionIds - sessionId
+ prefs.edit().putStringSet(PREF_MANUALLY_COMPLETED_SESSIONS, nextSet).apply()
+ _state.update { it.copy(manuallyCompletedSessionIds = nextSet) }
+ }
+
+ fun updateCollabUnreadCount(count: Int) {
+ val safe = count.coerceAtLeast(0)
+ if (_state.value.collabUnreadCount != safe) {
+ _state.update { it.copy(collabUnreadCount = safe) }
}
}
@@ -1497,8 +2058,11 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
val (origin, token) = live()
val catalog = withContext(Dispatchers.IO) { api.catalog(origin, token) }
val selected = _state.value.selectedProjectId
- val stillValid = selected != null && catalog.projects.any { it.id == selected }
- val resolvedProject = if (stillValid) selected else catalog.projects.firstOrNull()?.id
+ val resolvedProject = if (selected == null || catalog.projects.any { it.id == selected }) {
+ selected
+ } else {
+ null
+ }
_state.update {
it.copy(
catalog = catalog,
@@ -1507,15 +2071,29 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
error = null,
)
}
- if (resolvedProject != null) {
- loadConversations()
- }
+ loadConversations()
} catch (error: Throwable) {
_state.update { it.copy(catalogLoading = false) }
throw error
}
}
+ /**
+ * The account relay URL of a desktop signed in to the same account. It lets the phone reach
+ * that desktop off-LAN even when the profile never learned a published origin.
+ */
+ private fun accountTenantOriginFor(hostId: String): String? {
+ val current = _state.value
+ if (!current.accountSignedIn || current.accountOverview?.canUseCloudRelay == false) return null
+ val rawHostId = AccountDesktops.rawHostId(hostId)
+ if (current.accountDesktops.none { it.hostId == rawHostId }) return null
+ val relayBase = AccountDesktops.relayBase(
+ current.accountOverview?.assignedRelayServer,
+ PairingClient.DEFAULT_OFFICIAL_RELAY_BASE,
+ )
+ return AccountDesktops.tenantOrigin(relayBase, rawHostId)
+ }
+
private fun handleForcedOfflineByRevocation(hostId: String? = _state.value.selectedHostId) {
val targetId = hostId ?: selectedProfile()?.hostId
if (!targetId.isNullOrBlank()) {
@@ -1532,12 +2110,12 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
activeOrigin = null,
connection = ConnectionState.Offline,
realtimeRttMs = null,
- connectionFailureReason = "已被其他手机接管下线",
+ connectionFailureReason = REVOKED_FAILURE_REASON,
conversations = emptyList(),
timeline = null,
inbox = emptyList(),
pairing = false,
- error = "当前电脑已切换至其他手机远程连接,本机已强制下线并清除凭据,下次需重新连接",
+ error = REVOKED_NOTICE,
)
}
}
@@ -1630,7 +2208,17 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
Regex("""^http://(192\.168\.|10\.|172\.(1[6-9]|2\d|3[0-1])\.)""").containsMatchIn(lower)
}
+ val customBase = normalizeCustomRelayOrigin(_state.value.customRelayOrigin)
+ if (customBase.isNotEmpty() && !profile.hostId.startsWith("host:")) {
+ val customUrl = if (customBase.contains("/t/")) customBase else "$customBase/t/${profile.hostId}"
+ val customOrigin = runCatching { HostOrigin.parse(customUrl) }.getOrNull()
+ if (customOrigin != null && order.none { it.value == customOrigin.value }) {
+ order.add(customOrigin)
+ }
+ }
+ val canUseCloudRelay = (_state.value.accountSignedIn && (_state.value.accountOverview?.canUseCloudRelay == true)) || customBase.isNotEmpty()
val wasLanMode = !allowCloudSwitch &&
+ !canUseCloudRelay &&
profile.lastSuccessOrigin != null &&
isLanOriginUrl(profile.lastSuccessOrigin)
@@ -1756,7 +2344,8 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
lower.contains(":17890") ||
lower.contains(":17891")
}
- val combinedRemote = (rendezvousOrigins + cleanRemoteCandidates).distinct()
+ val combinedRemote = (rendezvousOrigins + cleanRemoteCandidates + listOfNotNull(accountTenantOriginFor(profile.hostId)))
+ .distinct()
// If previously connected via LAN direct, ask the user before switching to Cloud Service!
if (wasLanMode) {
@@ -1845,8 +2434,7 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
val project = resolveProjectName(session.workspaceId)
val titleText = session.title.ifBlank { "对话任务" }
if (event.kind == "turn_failed") {
- SystemNotifier.notifyAlert(
- getApplication(),
+ notifyAlertIfAllowed(
key,
"$project · 会话执行出错",
titleText,
@@ -1866,8 +2454,7 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
)
finishLiveActivity(conversationId, failedMessage = titleText)
} else {
- SystemNotifier.notifyAlert(
- getApplication(),
+ notifyAlertIfAllowed(
key,
"$project · 会话已完成",
titleText,
@@ -1975,27 +2562,31 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
runCatching {
val (origin, token) = live()
syncConversationTimeline(id, forceRows = true)
- val needControls = synchronized(session) {
- session.sessionModes.isEmpty() && session.sessionConfig.isEmpty()
- }
- if (needControls) {
- val controls = withContext(Dispatchers.IO) {
- api.ensureSessionControls(origin, token, id)
- }
- synchronized(session) {
- EventFold.applySessionControls(session, controls)
+ viewModelScope.launch {
+ runCatching {
+ val needControls = synchronized(session) {
+ session.sessionModes.isEmpty() && session.sessionConfig.isEmpty()
+ }
+ if (needControls) {
+ val controls = withContext(Dispatchers.IO) {
+ api.ensureSessionControls(origin, token, id)
+ }
+ synchronized(session) {
+ EventFold.applySessionControls(session, controls)
+ }
+ }
+ val skills = withContext(Dispatchers.IO) {
+ val agId = synchronized(session) { session.agentId }.ifBlank { summary?.agentId.orEmpty() }
+ val wsId = synchronized(session) { session.workspaceId }.ifBlank { summary?.workspaceId.orEmpty() }
+ api.listSlashCommands(origin, token, agId, wsId)
+ }
+ synchronized(session) {
+ session.skillCommands.clear()
+ session.skillCommands += skills
+ }
+ publishTimeline(session)
}
}
- val skills = withContext(Dispatchers.IO) {
- val agId = synchronized(session) { session.agentId }.ifBlank { summary?.agentId.orEmpty() }
- val wsId = synchronized(session) { session.workspaceId }.ifBlank { summary?.workspaceId.orEmpty() }
- api.listSlashCommands(origin, token, agId, wsId)
- }
- synchronized(session) {
- session.skillCommands.clear()
- session.skillCommands += skills
- }
- publishTimeline(session)
}
}
@@ -2071,8 +2662,7 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
if (notifiedEventKeys.add(alertKey)) {
val project = resolveProjectName(conv.workspaceId)
val titleText = conv.title.ifBlank { "新对话已创建,点击查看" }
- SystemNotifier.notifyAlert(
- getApplication(),
+ notifyAlertIfAllowed(
alertKey,
"$project · 新会话已创建",
titleText,
@@ -2090,25 +2680,11 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
kind = AttentionToastKind.Created,
),
)
- if (_state.value.route == Route.Conversations) {
- userRequestedNewChatLanding = false
- openConversation(conv.id)
- }
}
}
}
}
- if (
- !hasAutoOpenedInitialConversation &&
- !userRequestedNewChatLanding &&
- merged.isNotEmpty() &&
- _state.value.route == Route.Conversations
- ) {
- hasAutoOpenedInitialConversation = true
- openConversation(merged.first().id)
- }
-
viewModelScope.launch(Dispatchers.IO) {
var anyChanged = false
merged.take(6).forEach { summary ->
@@ -2358,8 +2934,7 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
add(SystemNotificationAction("拒绝", "perm_reject", convId, perm.id, reject.id))
}
}
- SystemNotifier.notifyAlert(
- getApplication(),
+ notifyAlertIfAllowed(
eventKey,
"$project:需要批准操作权限",
"$sessionTitle · $subtitle",
@@ -2390,8 +2965,7 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
SystemNotificationAction("批准执行", "question_confirm", convId, quest.id, "Yes"),
SystemNotificationAction("拒绝", "question_reject", convId, quest.id, ""),
)
- SystemNotifier.notifyAlert(
- getApplication(),
+ notifyAlertIfAllowed(
eventKey,
"$project:等待你的回答",
"$sessionTitle · $qText",
@@ -2420,8 +2994,7 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
if (notifiedEventKeys.add(eventKey)) {
if (lastRow?.kind == "turn_failed") {
val errText = lastRow.body.ifBlank { sessionTitle }
- SystemNotifier.notifyAlert(
- getApplication(),
+ notifyAlertIfAllowed(
eventKey,
"$project · 会话执行异常",
errText,
@@ -2441,8 +3014,7 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
)
finishLiveActivity(convId, failedMessage = errText)
} else {
- SystemNotifier.notifyAlert(
- getApplication(),
+ notifyAlertIfAllowed(
eventKey,
"$project · 会话已完成",
sessionTitle,
@@ -2805,10 +3377,12 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
accountCodeSent = false,
accountCodeResendAtMs = 0L,
accountError = null,
- desktopConnect = if (it.connection == ConnectionState.Online) null else DesktopConnectSheet(),
+ desktopConnect = null,
+ isAutoConnectingInBackground = it.connection != ConnectionState.Online,
+ autoConnectingHostName = null,
)
}
- refreshAccount()
+ refreshAccount(justLoggedIn = true)
discoverHosts()
}
@@ -2856,9 +3430,17 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
_state.update { it.copy(accountLoginRequested = requested) }
}
- fun refreshAccount() {
+ fun refreshAccount(justLoggedIn: Boolean = false) {
if (accountRepo.getToken() == null) {
- _state.update { it.copy(accountSignedIn = false, accountOverview = null, desktopConnect = null) }
+ _state.update {
+ it.copy(
+ accountSignedIn = false,
+ accountOverview = null,
+ desktopConnect = null,
+ isAutoConnectingInBackground = false,
+ autoConnectingHostName = null,
+ )
+ }
return
}
viewModelScope.launch {
@@ -2879,7 +3461,7 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
accountError = null,
)
}
- syncAccountHosts(forcePush = true)
+ syncAccountHosts(forcePush = true, justLoggedIn = justLoggedIn)
}
.onFailure { err ->
if (err is VibeXAccountException && err.code == "signed_out") {
@@ -2891,16 +3473,20 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
accountSavedHosts = emptyList(),
accountLoading = false,
desktopConnect = null,
+ isAutoConnectingInBackground = false,
+ autoConnectingHostName = null,
)
}
} else {
_state.update {
val sheet = it.desktopConnect?.takeIf { s -> s.stage == DesktopConnectStage.Syncing }
- ?.copy(stage = DesktopConnectStage.Failed(DESKTOP_SYNC_FAILED, route = null))
+ ?.copy(stage = DesktopConnectStage.Failed(DESKTOP_SYNC_FAILED, retryTarget = null))
it.copy(
accountLoading = false,
accountError = describeAccountError(err),
desktopConnect = sheet ?: it.desktopConnect,
+ isAutoConnectingInBackground = false,
+ autoConnectingHostName = null,
)
}
}
@@ -2908,7 +3494,7 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
}
}
- fun syncAccountHosts(forcePush: Boolean = false) {
+ fun syncAccountHosts(forcePush: Boolean = false, justLoggedIn: Boolean = false) {
if (accountRepo.getToken() == null) return
viewModelScope.launch {
_state.update { it.copy(accountSyncingHosts = true) }
@@ -2921,20 +3507,55 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
} else {
remoteSaved
}
+ var autoConnectDesktop: AccountDesktop? = null
_state.update { current ->
val listed = desktops.getOrDefault(current.accountDesktops)
- val sheet = current.desktopConnect?.takeIf { it.stage == DesktopConnectStage.Syncing }?.let {
- if (desktops.isSuccess) {
- it.copy(stage = DesktopConnectStage.Choosing, selectedHostId = listed.firstOrNull()?.hostId)
- } else {
- it.copy(stage = DesktopConnectStage.Failed(DESKTOP_SYNC_FAILED, route = null))
+ val shouldAutoConnectSingle = listed.size == 1 &&
+ current.connection != ConnectionState.Online &&
+ current.desktopConnect == null &&
+ (justLoggedIn || (!didAttemptInitialAccountAutoConnect && current.profiles.isEmpty() && current.autoConnect))
+ if (shouldAutoConnectSingle) {
+ didAttemptInitialAccountAutoConnect = true
+ autoConnectDesktop = listed.single()
+ }
+ val sheet = when {
+ current.desktopConnect?.stage == DesktopConnectStage.Syncing -> {
+ if (desktops.isSuccess) {
+ current.desktopConnect.copy(
+ stage = DesktopConnectStage.Choosing,
+ selectedHostId = listed.firstOrNull()?.hostId,
+ )
+ } else {
+ current.desktopConnect.copy(
+ stage = DesktopConnectStage.Failed(DESKTOP_SYNC_FAILED, retryTarget = null),
+ )
+ }
+ }
+ justLoggedIn && autoConnectDesktop == null && current.connection != ConnectionState.Online -> {
+ DesktopConnectSheet(
+ stage = if (desktops.isSuccess) {
+ DesktopConnectStage.Choosing
+ } else {
+ DesktopConnectStage.Failed(DESKTOP_SYNC_FAILED, retryTarget = null)
+ },
+ selectedHostId = listed.firstOrNull()?.hostId,
+ )
}
- } ?: current.desktopConnect
+ else -> current.desktopConnect
+ }
current.copy(
accountDesktops = listed,
accountSavedHosts = finalSaved,
accountSyncingHosts = false,
desktopConnect = sheet,
+ isAutoConnectingInBackground = if (autoConnectDesktop != null) true else false,
+ autoConnectingHostName = autoConnectDesktop?.hostName,
+ )
+ }
+ autoConnectDesktop?.let { desktop ->
+ connectDesktopTarget(
+ target = DesktopConnectTarget.Account(desktop),
+ silentUntilNeeded = true,
)
}
}
@@ -3029,7 +3650,20 @@ class CompanionModel(application: Application) : AndroidViewModel(application) {
}
private fun persist(transform: (StoredState) -> StoredState) {
- val next = transform(StoredState(_state.value.selectedHostId, _state.value.profiles, _state.value.appearance, _state.value.streamDisplay, _state.value.accent))
+ val cur = _state.value
+ val next = transform(
+ StoredState(
+ selectedHostId = cur.selectedHostId,
+ profiles = cur.profiles,
+ appearance = cur.appearance,
+ streamDisplay = cur.streamDisplay,
+ accent = cur.accent,
+ language = cur.language,
+ confirmHighRiskApproval = cur.confirmHighRiskApproval,
+ muteForegroundNotifications = cur.muteForegroundNotifications,
+ customRelayOrigin = cur.customRelayOrigin,
+ ),
+ )
viewModelScope.launch(Dispatchers.IO) {
profiles.save(next)
}
diff --git a/app/src/main/java/dev/vibex/companion/app/DesktopConnect.kt b/app/src/main/java/dev/vibex/companion/app/DesktopConnect.kt
index 866b924..4be1c53 100644
--- a/app/src/main/java/dev/vibex/companion/app/DesktopConnect.kt
+++ b/app/src/main/java/dev/vibex/companion/app/DesktopConnect.kt
@@ -1,25 +1,61 @@
package dev.vibex.companion.app
+import dev.vibex.companion.AccountDesktop
import dev.vibex.companion.AccountLinkException
import dev.vibex.companion.AccountLinkFailure
+import dev.vibex.companion.DiscoveredLanHost
import dev.vibex.companion.HostUnreachableException
import dev.vibex.companion.PairingException
import java.io.IOException
-enum class DesktopConnectRoute { Cloud, Lan }
+data class ConnectedSasNotice(
+ val id: String,
+ val hostName: String,
+ val sasCode: String,
+)
+
+sealed interface DesktopConnectTarget {
+ data class Account(val desktop: AccountDesktop) : DesktopConnectTarget
+ data class Lan(val host: DiscoveredLanHost) : DesktopConnectTarget
+
+ val displayName: String
+ get() = when (this) {
+ is Account -> desktop.hostName
+ is Lan -> host.displayName
+ }
+
+ val hostId: String?
+ get() = when (this) {
+ is Account -> desktop.hostId
+ is Lan -> host.hostId
+ }
+}
+
+sealed interface LanSasInlineState {
+ data class Connecting(val host: DiscoveredLanHost) : LanSasInlineState
+ data class AwaitingApproval(
+ val host: DiscoveredLanHost,
+ val sasCode: String,
+ val hostName: String,
+ ) : LanSasInlineState
+}
-/** Steps of the account popup that finds the user's desktops and connects one in a single tap. */
+/** Steps of the popup that finds the user's desktops or LAN hosts and connects in a single tap. */
sealed interface DesktopConnectStage {
data object Syncing : DesktopConnectStage
data object Choosing : DesktopConnectStage
- data class Connecting(val route: DesktopConnectRoute) : DesktopConnectStage
+ data class Connecting(val target: DesktopConnectTarget) : DesktopConnectStage
- data class AwaitingApproval(val route: DesktopConnectRoute, val sasCode: String) : DesktopConnectStage
+ data class AwaitingApproval(
+ val target: DesktopConnectTarget,
+ val sasCode: String,
+ val hostName: String,
+ ) : DesktopConnectStage
- /** [route] is null when the desktop list itself could not be loaded. */
- data class Failed(val message: String, val route: DesktopConnectRoute?) : DesktopConnectStage
+ /** [retryTarget] is null when the desktop list itself could not be loaded. */
+ data class Failed(val message: String, val retryTarget: DesktopConnectTarget?) : DesktopConnectStage
}
data class DesktopConnectSheet(
@@ -31,19 +67,18 @@ internal const val DESKTOP_SYNC_FAILED = "同步失败,请重试"
internal const val DESKTOP_APPROVAL_REJECTED = "电脑端已拒绝连接"
internal const val DESKTOP_APPROVAL_EXPIRED = "确认超时,请重试"
private const val ACCOUNT_VERIFICATION_UNAVAILABLE = "暂时无法验证账号,请稍后重试"
-
private const val HOST_UNREACHABLE = "无法连接到电脑,请确认电脑端在线并已开启远程连接"
internal fun desktopConnectErrorMessage(error: Throwable): String =
when (error) {
- is AccountLinkException -> when (error.failure) {
- AccountLinkFailure.AccountMismatch -> "这台电脑登录的是其他 VibeX 账号"
- AccountLinkFailure.HostAccountSignedOut -> "这台电脑未登录 VibeX 账号"
- AccountLinkFailure.AccountTokenInvalid,
- AccountLinkFailure.VerificationUnavailable,
- -> ACCOUNT_VERIFICATION_UNAVAILABLE
- }
- is HostUnreachableException, is IOException -> HOST_UNREACHABLE
- is PairingException -> error.message ?: HOST_UNREACHABLE
+ is AccountLinkException -> when (error.failure) {
+ AccountLinkFailure.AccountMismatch -> "这台电脑登录的是其他 VibeX 账号"
+ AccountLinkFailure.HostAccountSignedOut -> "这台电脑未登录 VibeX 账号"
+ AccountLinkFailure.AccountTokenInvalid,
+ AccountLinkFailure.VerificationUnavailable,
+ -> ACCOUNT_VERIFICATION_UNAVAILABLE
+ }
+ is HostUnreachableException, is IOException -> HOST_UNREACHABLE
+ is PairingException -> error.message ?: HOST_UNREACHABLE
else -> error.message ?: HOST_UNREACHABLE
}
diff --git a/app/src/main/java/dev/vibex/companion/app/MainActivity.kt b/app/src/main/java/dev/vibex/companion/app/MainActivity.kt
index 9476c1d..0e4a664 100644
--- a/app/src/main/java/dev/vibex/companion/app/MainActivity.kt
+++ b/app/src/main/java/dev/vibex/companion/app/MainActivity.kt
@@ -18,9 +18,11 @@ import dev.vibex.companion.app.monitor.SystemIslandOverlayController
import dev.vibex.companion.app.monitor.SystemNotifier
import dev.vibex.companion.app.ui.CompanionApp
import dev.vibex.companion.app.ui.theme.CompanionTheme
+import dev.vibex.companion.app.update.AppUpdateManager
class MainActivity : ComponentActivity() {
private val model: CompanionModel by viewModels()
+ private val updateManager by lazy { AppUpdateManager.getInstance(this) }
private val notificationPermissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission(),
) {}
@@ -44,6 +46,12 @@ class MainActivity : ComponentActivity() {
super.onStart()
SystemIslandOverlayController.setAppForeground(this, true)
model.onAppForegroundChanged(true)
+ updateManager.checkIfDue(fromBackground = false)
+ }
+
+ override fun onResume() {
+ super.onResume()
+ updateManager.onActivityResumed()
}
override fun onStop() {
@@ -68,6 +76,11 @@ class MainActivity : ComponentActivity() {
}
private fun handleIntentExtras(intent: Intent?) {
+ if (intent?.getBooleanExtra(AppUpdateManager.EXTRA_OPEN_UPDATE_DIALOG, false) == true) {
+ intent.removeExtra(AppUpdateManager.EXTRA_OPEN_UPDATE_DIALOG)
+ updateManager.openUpdateDialog()
+ return
+ }
val notifAction = intent?.getStringExtra("vibex_notif_action")?.takeIf { it.isNotBlank() }
if (notifAction != null) {
val convId = intent.getStringExtra("vibex_notif_conv_id").orEmpty()
diff --git a/app/src/main/java/dev/vibex/companion/app/data/ModelPreferencesStore.kt b/app/src/main/java/dev/vibex/companion/app/data/ModelPreferencesStore.kt
new file mode 100644
index 0000000..3400354
--- /dev/null
+++ b/app/src/main/java/dev/vibex/companion/app/data/ModelPreferencesStore.kt
@@ -0,0 +1,79 @@
+package dev.vibex.companion.app.data
+
+import android.content.Context
+import kotlinx.coroutines.flow.MutableStateFlow
+import kotlinx.coroutines.flow.StateFlow
+import kotlinx.coroutines.flow.update
+
+class ModelPreferencesStore(context: Context) {
+ private val prefs = context.getSharedPreferences("vibex_model_prefs", Context.MODE_PRIVATE)
+
+ private val _favoriteIds = MutableStateFlow(loadSet(KEY_FAVORITES))
+ val favoriteIds: StateFlow> = _favoriteIds
+
+ private val _hiddenIds = MutableStateFlow(loadSet(KEY_HIDDEN))
+ val hiddenIds: StateFlow> = _hiddenIds
+
+ private val _customModelsByAgent = MutableStateFlow(loadCustomMap())
+ val customModelsByAgent: StateFlow