mirror of
https://github.com/simplex-chat/simplex-chat.git
synced 2026-09-01 22:18:43 +00:00
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:
+1
@@ -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) {
|
||||
|
||||
-1
@@ -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
|
||||
|
||||
|
||||
+28
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user