feat: add wake lock to map screen

This commit is contained in:
andrekir
2023-07-15 09:23:37 -03:00
committed by Andre K
parent d1e06e21bf
commit 04e8a6a514
2 changed files with 63 additions and 0 deletions

View File

@@ -49,6 +49,7 @@ import com.geeksville.mesh.ui.map.components.CacheLayout
import com.geeksville.mesh.ui.map.components.DownloadButton
import com.geeksville.mesh.ui.map.components.EditWaypointDialog
import com.geeksville.mesh.ui.map.components.MapStyleButton
import com.geeksville.mesh.util.EnableWakeLock
import com.geeksville.mesh.util.SqlTileWriterExt
import com.geeksville.mesh.util.requiredZoomLevel
import com.geeksville.mesh.util.formatAgo
@@ -136,6 +137,8 @@ fun MapView(model: UIViewModel = viewModel()) {
val haptic = LocalHapticFeedback.current
fun performHapticFeedback() = haptic.performHapticFeedback(HapticFeedbackType.LongPress)
EnableWakeLock(context)
val map = remember {
MapView(context).apply {
clipToOutline = true

View File

@@ -0,0 +1,60 @@
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()
}
}
}