mirror of
https://github.com/meshtastic/Meshtastic-Android.git
synced 2026-04-20 22:23:37 +00:00
refactor: handle MapView Lifecycle
This commit is contained in:
parent
fce97997d8
commit
0a47b8df3e
2 changed files with 25 additions and 15 deletions
|
|
@ -1,60 +0,0 @@
|
|||
package com.geeksville.mesh.util
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.content.Context
|
||||
import android.os.PowerManager
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.DisposableEffect
|
||||
import androidx.compose.ui.platform.LocalLifecycleOwner
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.LifecycleEventObserver
|
||||
import com.geeksville.mesh.android.BuildUtils.errormsg
|
||||
|
||||
@SuppressLint("WakelockTimeout")
|
||||
private fun PowerManager.WakeLock.safeAcquire() {
|
||||
if (!isHeld) try {
|
||||
acquire()
|
||||
} catch (e: SecurityException) {
|
||||
errormsg("WakeLock permission exception: ${e.message}")
|
||||
} catch (e: IllegalStateException) {
|
||||
errormsg("WakeLock acquire() exception: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
private fun PowerManager.WakeLock.safeRelease() {
|
||||
if (isHeld) try {
|
||||
release()
|
||||
} catch (e: IllegalStateException) {
|
||||
errormsg("WakeLock release() exception: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressLint("InvalidWakeLockTag")
|
||||
@Composable
|
||||
fun EnableWakeLock(context: Context) {
|
||||
val lifecycle = LocalLifecycleOwner.current.lifecycle
|
||||
|
||||
DisposableEffect(Unit) {
|
||||
val powerManager = context.getSystemService(Context.POWER_SERVICE) as PowerManager
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
val wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "ScreenLock")
|
||||
|
||||
wakeLock.safeAcquire()
|
||||
|
||||
val observer = LifecycleEventObserver { _, event ->
|
||||
when (event) {
|
||||
Lifecycle.Event.ON_PAUSE -> wakeLock.safeRelease()
|
||||
Lifecycle.Event.ON_RESUME -> wakeLock.safeAcquire()
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
|
||||
lifecycle.addObserver(observer)
|
||||
|
||||
onDispose {
|
||||
lifecycle.removeObserver(observer)
|
||||
wakeLock.safeRelease()
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue