fix: implementation of --disable-screensaver

This commit is contained in:
Miuzarte
2026-04-23 09:48:27 +08:00
parent 0a8d992a28
commit c2bf024d48
9 changed files with 53 additions and 47 deletions

View File

@@ -9,7 +9,7 @@ import io.github.miuzarte.scrcpyforandroid.password.BiometricGate
import io.github.miuzarte.scrcpyforandroid.password.PasswordRepository
import io.github.miuzarte.scrcpyforandroid.password.hasAuthenticatedOrigin
import io.github.miuzarte.scrcpyforandroid.services.AppRuntime
import io.github.miuzarte.scrcpyforandroid.services.AppWakeLocks
import io.github.miuzarte.scrcpyforandroid.services.AppScreenOn
import kotlinx.coroutines.runBlocking
// 生物认证需要 FragmentActivity
@@ -18,7 +18,7 @@ class MainActivity : FragmentActivity() {
super.onCreate(savedInstanceState)
AppRuntime.init(applicationContext)
AppWakeLocks.init(applicationContext)
AppScreenOn.register(window)
runBlocking {
PasswordRepository.refresh()
@@ -41,4 +41,9 @@ class MainActivity : FragmentActivity() {
super.onResume()
StreamActivity.dismissActivePictureInPicture()
}
override fun onDestroy() {
AppScreenOn.unregister(window)
super.onDestroy()
}
}

View File

@@ -13,6 +13,7 @@ import androidx.core.content.ContextCompat
import androidx.core.pip.BasicPictureInPicture
import androidx.fragment.app.FragmentActivity
import io.github.miuzarte.scrcpyforandroid.pages.StreamScreen
import io.github.miuzarte.scrcpyforandroid.services.AppScreenOn
import io.github.miuzarte.scrcpyforandroid.services.PictureInPictureActionReceiver
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
@@ -43,6 +44,7 @@ class StreamActivity : FragmentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
currentActivityRef = WeakReference(this)
AppScreenOn.register(window)
registerPipActionReceiver()
@@ -87,6 +89,7 @@ class StreamActivity : FragmentActivity() {
currentActivityRef?.get()
?.takeIf { it === this }
?.let { currentActivityRef = null }
AppScreenOn.unregister(window)
unregisterPipActionReceiver()
super.onDestroy()
}

View File

@@ -39,7 +39,7 @@ import io.github.miuzarte.scrcpyforandroid.scaffolds.LazyColumn
import io.github.miuzarte.scrcpyforandroid.scaffolds.SectionSmallTitle
import io.github.miuzarte.scrcpyforandroid.scrcpy.Scrcpy
import io.github.miuzarte.scrcpyforandroid.services.AppRuntime
import io.github.miuzarte.scrcpyforandroid.services.AppWakeLocks
import io.github.miuzarte.scrcpyforandroid.services.AppScreenOn
import io.github.miuzarte.scrcpyforandroid.services.DeviceAdbBackgroundRunner
import io.github.miuzarte.scrcpyforandroid.services.DeviceAdbConnectionCoordinator
import io.github.miuzarte.scrcpyforandroid.services.DeviceAdbSessionState
@@ -235,7 +235,7 @@ fun DeviceTabPage(
DisposableEffect(Unit) {
onDispose {
AppWakeLocks.release()
AppScreenOn.release()
}
}
DisposableEffect(adbBackgroundRunner) {
@@ -430,7 +430,7 @@ fun DeviceTabPage(
// Also stops scrcpy.
runCatching { scrcpy.stop() }
runCatching { adbCoordinator.disconnect() }
AppWakeLocks.release()
AppScreenOn.release()
adbSession = DeviceAdbSessionState()
AppRuntime.currentConnectionTarget = null
clearQuickOnlineForTarget?.let { target ->
@@ -691,7 +691,7 @@ fun DeviceTabPage(
context.startActivity(StreamActivity.createIntent(context))
}
if (resolvedOptions.disableScreensaver)
AppWakeLocks.acquire()
AppScreenOn.acquire()
adbSession = adbSession.copy(statusLine = "scrcpy 运行中")
@SuppressLint("DefaultLocale")
@@ -727,7 +727,7 @@ fun DeviceTabPage(
showSnackMessage = "scrcpy 已停止ADB 已断开",
)
} else {
AppWakeLocks.release()
AppScreenOn.release()
adbSession = adbSession.copy(
statusLine = "${currentTarget!!.host}:${currentTarget.port}"
)

View File

@@ -988,7 +988,7 @@ internal fun ScrcpyAllOptionsPage(
},
)
SwitchPreference(
title = "scrcpy 启动后保持本机屏幕唤醒",
title = "scrcpy 启动后保持本机屏幕常亮",
summary = "--disable-screensaver",
checked = soBundle.disableScreensaver,
onCheckedChange = {
@@ -996,7 +996,7 @@ internal fun ScrcpyAllOptionsPage(
disableScreensaver = it
)
if (it) snackbar.show(
"不保证可用"
"注意防烧屏,画中画也生效"
)
},
)

View File

@@ -0,0 +1,35 @@
package io.github.miuzarte.scrcpyforandroid.services
import android.view.Window
import android.view.WindowManager
object AppScreenOn {
private val windows = linkedSetOf<Window>()
private var keepScreenOnEnabled = false
fun register(window: Window) = synchronized(this) {
windows += window
applyKeepScreenOn(window, enabled = keepScreenOnEnabled)
}
fun unregister(window: Window) = synchronized(this) {
windows.remove(window)
applyKeepScreenOn(window, enabled = false)
}
fun acquire() = synchronized(this) {
if (keepScreenOnEnabled) Unit
keepScreenOnEnabled = true
windows.forEach { applyKeepScreenOn(window = it, enabled = keepScreenOnEnabled) }
}
fun release() = synchronized(this) {
if (!keepScreenOnEnabled) Unit
keepScreenOnEnabled = false
windows.forEach { applyKeepScreenOn(window = it, enabled = keepScreenOnEnabled) }
}
private fun applyKeepScreenOn(window: Window, enabled: Boolean) =
if (enabled) window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
else window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}

View File

@@ -1,35 +0,0 @@
package io.github.miuzarte.scrcpyforandroid.services
import android.annotation.SuppressLint
import android.content.Context
import android.os.PowerManager
object AppWakeLocks {
private lateinit var appContext: Context
private lateinit var powerManager: PowerManager
fun init(context: Context) {
appContext = context.applicationContext
powerManager = appContext.getSystemService(Context.POWER_SERVICE) as PowerManager
}
private var wakeLock: PowerManager.WakeLock? = null
@SuppressLint("WakelockTimeout")
fun acquire() {
if (wakeLock != null) return
wakeLock = powerManager.newWakeLock(
PowerManager.PARTIAL_WAKE_LOCK,
"${appContext.packageName}:scrcpy-wakelock",
).apply {
setReferenceCounted(false)
acquire()
}
}
fun release() {
wakeLock?.runCatching { release() }
wakeLock = null
}
}

View File

@@ -17,7 +17,6 @@ class PictureInPictureActionReceiver : BroadcastReceiver() {
try {
val appContext = context.applicationContext
AppRuntime.init(appContext)
AppWakeLocks.init(appContext)
runBlocking {
AppRuntime.scrcpy?.stop()
}