fix: Session not authorized showing when it shouldn't

This commit is contained in:
zvonimir
2025-10-08 16:14:12 +02:00
parent bce93b8e0f
commit 1bab4d398e
2 changed files with 15 additions and 3 deletions
@@ -110,7 +110,19 @@ class SyncPairActivity : AppCompatActivity() {
lifecycleScope.launch(Dispatchers.IO) { lifecycleScope.launch(Dispatchers.IO) {
try { try {
var wasCompleted = false
StateSync.instance.syncService?.connect(deviceInfo, true) { complete, message -> StateSync.instance.syncService?.connect(deviceInfo, true) { complete, message ->
if (wasCompleted) {
Logger.i(TAG, "onStatusUpdate(complete = ${complete}, message = '${message} ignored because wasCompleted')")
return@connect
}
if (complete == true) {
wasCompleted = true
}
Logger.i(TAG, "onStatusUpdate(complete = ${complete}, message = '${message}')")
lifecycleScope.launch(Dispatchers.Main) { lifecycleScope.launch(Dispatchers.Main) {
if (complete != null) { if (complete != null) {
if (complete) { if (complete) {
@@ -758,7 +758,7 @@ class SyncService(
fun removeAuthorizedDevice(publicKey: String) = database.removeAuthorizedDevice(publicKey) fun removeAuthorizedDevice(publicKey: String) = database.removeAuthorizedDevice(publicKey)
suspend fun connect(deviceInfo: SyncDeviceInfo, alsoTryRelayed: Boolean = false, timeout_ms: Int = 5_000, onStatusUpdate: ((complete: Boolean?, message: String) -> Unit)? = null) { suspend fun connect(deviceInfo: SyncDeviceInfo, alsoTryRelayed: Boolean = false, timeout_ms: Int = 10_000, onStatusUpdate: ((complete: Boolean?, message: String) -> Unit)? = null) {
val rs = _relaySession val rs = _relaySession
val startTime = System.currentTimeMillis() val startTime = System.currentTimeMillis()
if (alsoTryRelayed && rs != null && settings.relayPairAllowed) { if (alsoTryRelayed && rs != null && settings.relayPairAllowed) {
@@ -791,7 +791,7 @@ class SyncService(
suspend fun connect(addresses: Array<String>, port: Int, publicKey: String, pairingCode: String?, onStatusUpdate: ((complete: Boolean?, message: String) -> Unit)? = null, timeout_ms: Int = 10_000): SyncSocketSession { suspend fun connect(addresses: Array<String>, port: Int, publicKey: String, pairingCode: String?, onStatusUpdate: ((complete: Boolean?, message: String) -> Unit)? = null, timeout_ms: Int = 10_000): SyncSocketSession {
val startTime_ms = System.currentTimeMillis() val startTime_ms = System.currentTimeMillis()
Log.i(TAG, "Connecting directly (timeout_ms = ${timeout_ms})...")
onStatusUpdate?.invoke(null, "Connecting directly...") onStatusUpdate?.invoke(null, "Connecting directly...")
val socket = getConnectedSocket(addresses.map { InetAddress.getByName(it) }, port, timeout_ms) ?: throw Exception("Failed to connect") val socket = getConnectedSocket(addresses.map { InetAddress.getByName(it) }, port, timeout_ms) ?: throw Exception("Failed to connect")
onStatusUpdate?.invoke(null, "Handshaking...") onStatusUpdate?.invoke(null, "Handshaking...")
@@ -805,7 +805,7 @@ class SyncService(
session.startAsInitiator(publicKey, appId, pairingCode) session.startAsInitiator(publicKey, appId, pairingCode)
while (timeout_ms - (startTime_ms - System.currentTimeMillis()) > 0 && !session.isAuthorized && session.started) { while ((System.currentTimeMillis() - startTime_ms) < timeout_ms && !session.isAuthorized) {
delay(100) delay(100)
} }