android: reduce battery usage by not using permanent wakelock (#6176)

* android, desktop: core API event timeout setting

* chat_recv_msg_wait with STM timeout

* Revert "chat_recv_msg_wait with STM timeout"

This reverts commit fdfa2964c5.

* do not use permanent wake lock

* wakelock option, get wakelock on network information change

* remove option
This commit is contained in:
Evgeny
2025-08-19 21:02:50 +01:00
committed by GitHub
parent 24e1680a45
commit 83923b9740
9 changed files with 56 additions and 11 deletions
@@ -73,6 +73,7 @@ class NetworkObserver {
}
private fun setNetworkInfo(info: UserNetworkInfo) {
getWakeLock(timeout = 180000)
Log.d(TAG, "Network changed: $info")
noNetworkJob.cancel()
if (info.online) {
@@ -13,7 +13,6 @@ import java.lang.ref.WeakReference
import java.util.*
import java.util.concurrent.Semaphore
import kotlin.concurrent.thread
import kotlin.random.Random
actual val appPlatform = AppPlatform.ANDROID
@@ -0,0 +1,28 @@
package chat.simplex.common.platform
import android.content.Context
import android.os.PowerManager
actual fun getWakeLock(timeout: Long): (() -> Unit) {
val context = AppContextProvider.getApplicationContext()
?: throw IllegalStateException("Application context not initialized")
var wakeLock: PowerManager.WakeLock? = (context.applicationContext.getSystemService(Context.POWER_SERVICE) as PowerManager).run {
newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "SimplexService::lock").apply {
acquire(timeout)
}
}
return {
wakeLock?.release()
wakeLock = null
}
}
object AppContextProvider {
private var applicationContext: Context? = null
fun initialize(context: Context) {
this.applicationContext = context.applicationContext
}
fun getApplicationContext(): Context? = applicationContext
}