From d1d2e4bc185ef930c1f615d54e57be86e43eab4a Mon Sep 17 00:00:00 2001 From: Evgeny Date: Wed, 27 Aug 2025 19:54:53 +0100 Subject: [PATCH] android: hold wakelock for 30 seconds after received event was processed, to prevent race condition with incoming call activity. (#6224) * android: hold wakelock for 45 seconds after event is received * create new lock instances, delay release by 30 seconds * comment --- .../simplex/common/platform/SimplexService.android.kt | 7 +++++-- .../kotlin/chat/simplex/common/model/SimpleXAPI.kt | 9 ++++++++- .../simplex/common/platform/SimplexService.desktop.kt | 5 +---- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/platform/SimplexService.android.kt b/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/platform/SimplexService.android.kt index 14e4134010..3ccdcecab0 100644 --- a/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/platform/SimplexService.android.kt +++ b/apps/multiplatform/common/src/androidMain/kotlin/chat/simplex/common/platform/SimplexService.android.kt @@ -12,8 +12,11 @@ actual fun getWakeLock(timeout: Long): (() -> Unit) { } } return { - wakeLock?.release() - wakeLock = null + val lock = wakeLock + if (lock != null) { + if (lock.isHeld) lock.release() + wakeLock = null + } } } diff --git a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt index 1b0714dd9d..9b60a968b7 100644 --- a/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt +++ b/apps/multiplatform/common/src/commonMain/kotlin/chat/simplex/common/model/SimpleXAPI.kt @@ -653,7 +653,14 @@ object ChatController { break } try { - releaseLock() + val release = releaseLock + // delaying the release of wake lock in order to: + // 1. avoid race condition with the incoming call activity that fails to show if called after wake lock release (primary reason), + // 2. allow any other necessary processing for a bit of time with wakelock held. + launch { + delay(30000) + release() + } val msg = recvMsg(ctrl) releaseLock = getWakeLock(timeout = 60000) if (msg != null) { diff --git a/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/platform/SimplexService.desktop.kt b/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/platform/SimplexService.desktop.kt index a9d411916c..d29871f842 100644 --- a/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/platform/SimplexService.desktop.kt +++ b/apps/multiplatform/common/src/desktopMain/kotlin/chat/simplex/common/platform/SimplexService.desktop.kt @@ -1,6 +1,3 @@ package chat.simplex.common.platform -actual fun getWakeLock(timeout: Long): (() -> Unit) { - return {} -} - +actual fun getWakeLock(timeout: Long): (() -> Unit) = {}