fix: implementation of --disable-screensaver
This commit is contained in:
@@ -5,6 +5,7 @@
|
|||||||
## 0.2.2
|
## 0.2.2
|
||||||
|
|
||||||
- 改进: 设备页连接设备后选项卡显示当前配置名
|
- 改进: 设备页连接设备后选项卡显示当前配置名
|
||||||
|
- 修复: 正确实现 `--disable-screensaver`,`FLAG_KEEP_SCREEN_ON` 而非 `PARTIAL_WAKE_LOCK`
|
||||||
|
|
||||||
## 0.2.1
|
## 0.2.1
|
||||||
|
|
||||||
|
|||||||
2
TODO.md
2
TODO.md
@@ -2,8 +2,6 @@
|
|||||||
|
|
||||||
# TODO
|
# TODO
|
||||||
|
|
||||||
fix wake lock
|
|
||||||
|
|
||||||
## PARAMS
|
## PARAMS
|
||||||
|
|
||||||
### LOWER PRIORITY
|
### LOWER PRIORITY
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ import io.github.miuzarte.scrcpyforandroid.password.BiometricGate
|
|||||||
import io.github.miuzarte.scrcpyforandroid.password.PasswordRepository
|
import io.github.miuzarte.scrcpyforandroid.password.PasswordRepository
|
||||||
import io.github.miuzarte.scrcpyforandroid.password.hasAuthenticatedOrigin
|
import io.github.miuzarte.scrcpyforandroid.password.hasAuthenticatedOrigin
|
||||||
import io.github.miuzarte.scrcpyforandroid.services.AppRuntime
|
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
|
import kotlinx.coroutines.runBlocking
|
||||||
|
|
||||||
// 生物认证需要 FragmentActivity
|
// 生物认证需要 FragmentActivity
|
||||||
@@ -18,7 +18,7 @@ class MainActivity : FragmentActivity() {
|
|||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
|
|
||||||
AppRuntime.init(applicationContext)
|
AppRuntime.init(applicationContext)
|
||||||
AppWakeLocks.init(applicationContext)
|
AppScreenOn.register(window)
|
||||||
|
|
||||||
runBlocking {
|
runBlocking {
|
||||||
PasswordRepository.refresh()
|
PasswordRepository.refresh()
|
||||||
@@ -41,4 +41,9 @@ class MainActivity : FragmentActivity() {
|
|||||||
super.onResume()
|
super.onResume()
|
||||||
StreamActivity.dismissActivePictureInPicture()
|
StreamActivity.dismissActivePictureInPicture()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun onDestroy() {
|
||||||
|
AppScreenOn.unregister(window)
|
||||||
|
super.onDestroy()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ import androidx.core.content.ContextCompat
|
|||||||
import androidx.core.pip.BasicPictureInPicture
|
import androidx.core.pip.BasicPictureInPicture
|
||||||
import androidx.fragment.app.FragmentActivity
|
import androidx.fragment.app.FragmentActivity
|
||||||
import io.github.miuzarte.scrcpyforandroid.pages.StreamScreen
|
import io.github.miuzarte.scrcpyforandroid.pages.StreamScreen
|
||||||
|
import io.github.miuzarte.scrcpyforandroid.services.AppScreenOn
|
||||||
import io.github.miuzarte.scrcpyforandroid.services.PictureInPictureActionReceiver
|
import io.github.miuzarte.scrcpyforandroid.services.PictureInPictureActionReceiver
|
||||||
import kotlinx.coroutines.flow.MutableStateFlow
|
import kotlinx.coroutines.flow.MutableStateFlow
|
||||||
import kotlinx.coroutines.flow.StateFlow
|
import kotlinx.coroutines.flow.StateFlow
|
||||||
@@ -43,6 +44,7 @@ class StreamActivity : FragmentActivity() {
|
|||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
currentActivityRef = WeakReference(this)
|
currentActivityRef = WeakReference(this)
|
||||||
|
AppScreenOn.register(window)
|
||||||
|
|
||||||
registerPipActionReceiver()
|
registerPipActionReceiver()
|
||||||
|
|
||||||
@@ -87,6 +89,7 @@ class StreamActivity : FragmentActivity() {
|
|||||||
currentActivityRef?.get()
|
currentActivityRef?.get()
|
||||||
?.takeIf { it === this }
|
?.takeIf { it === this }
|
||||||
?.let { currentActivityRef = null }
|
?.let { currentActivityRef = null }
|
||||||
|
AppScreenOn.unregister(window)
|
||||||
unregisterPipActionReceiver()
|
unregisterPipActionReceiver()
|
||||||
super.onDestroy()
|
super.onDestroy()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ import io.github.miuzarte.scrcpyforandroid.scaffolds.LazyColumn
|
|||||||
import io.github.miuzarte.scrcpyforandroid.scaffolds.SectionSmallTitle
|
import io.github.miuzarte.scrcpyforandroid.scaffolds.SectionSmallTitle
|
||||||
import io.github.miuzarte.scrcpyforandroid.scrcpy.Scrcpy
|
import io.github.miuzarte.scrcpyforandroid.scrcpy.Scrcpy
|
||||||
import io.github.miuzarte.scrcpyforandroid.services.AppRuntime
|
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.DeviceAdbBackgroundRunner
|
||||||
import io.github.miuzarte.scrcpyforandroid.services.DeviceAdbConnectionCoordinator
|
import io.github.miuzarte.scrcpyforandroid.services.DeviceAdbConnectionCoordinator
|
||||||
import io.github.miuzarte.scrcpyforandroid.services.DeviceAdbSessionState
|
import io.github.miuzarte.scrcpyforandroid.services.DeviceAdbSessionState
|
||||||
@@ -235,7 +235,7 @@ fun DeviceTabPage(
|
|||||||
|
|
||||||
DisposableEffect(Unit) {
|
DisposableEffect(Unit) {
|
||||||
onDispose {
|
onDispose {
|
||||||
AppWakeLocks.release()
|
AppScreenOn.release()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
DisposableEffect(adbBackgroundRunner) {
|
DisposableEffect(adbBackgroundRunner) {
|
||||||
@@ -430,7 +430,7 @@ fun DeviceTabPage(
|
|||||||
// Also stops scrcpy.
|
// Also stops scrcpy.
|
||||||
runCatching { scrcpy.stop() }
|
runCatching { scrcpy.stop() }
|
||||||
runCatching { adbCoordinator.disconnect() }
|
runCatching { adbCoordinator.disconnect() }
|
||||||
AppWakeLocks.release()
|
AppScreenOn.release()
|
||||||
adbSession = DeviceAdbSessionState()
|
adbSession = DeviceAdbSessionState()
|
||||||
AppRuntime.currentConnectionTarget = null
|
AppRuntime.currentConnectionTarget = null
|
||||||
clearQuickOnlineForTarget?.let { target ->
|
clearQuickOnlineForTarget?.let { target ->
|
||||||
@@ -691,7 +691,7 @@ fun DeviceTabPage(
|
|||||||
context.startActivity(StreamActivity.createIntent(context))
|
context.startActivity(StreamActivity.createIntent(context))
|
||||||
}
|
}
|
||||||
if (resolvedOptions.disableScreensaver)
|
if (resolvedOptions.disableScreensaver)
|
||||||
AppWakeLocks.acquire()
|
AppScreenOn.acquire()
|
||||||
|
|
||||||
adbSession = adbSession.copy(statusLine = "scrcpy 运行中")
|
adbSession = adbSession.copy(statusLine = "scrcpy 运行中")
|
||||||
@SuppressLint("DefaultLocale")
|
@SuppressLint("DefaultLocale")
|
||||||
@@ -727,7 +727,7 @@ fun DeviceTabPage(
|
|||||||
showSnackMessage = "scrcpy 已停止,ADB 已断开",
|
showSnackMessage = "scrcpy 已停止,ADB 已断开",
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
AppWakeLocks.release()
|
AppScreenOn.release()
|
||||||
adbSession = adbSession.copy(
|
adbSession = adbSession.copy(
|
||||||
statusLine = "${currentTarget!!.host}:${currentTarget.port}"
|
statusLine = "${currentTarget!!.host}:${currentTarget.port}"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -988,7 +988,7 @@ internal fun ScrcpyAllOptionsPage(
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
SwitchPreference(
|
SwitchPreference(
|
||||||
title = "scrcpy 启动后保持本机屏幕唤醒",
|
title = "scrcpy 启动后保持本机屏幕常亮",
|
||||||
summary = "--disable-screensaver",
|
summary = "--disable-screensaver",
|
||||||
checked = soBundle.disableScreensaver,
|
checked = soBundle.disableScreensaver,
|
||||||
onCheckedChange = {
|
onCheckedChange = {
|
||||||
@@ -996,7 +996,7 @@ internal fun ScrcpyAllOptionsPage(
|
|||||||
disableScreensaver = it
|
disableScreensaver = it
|
||||||
)
|
)
|
||||||
if (it) snackbar.show(
|
if (it) snackbar.show(
|
||||||
"不保证可用"
|
"注意防烧屏,画中画也生效"
|
||||||
)
|
)
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -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)
|
||||||
|
}
|
||||||
@@ -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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -17,7 +17,6 @@ class PictureInPictureActionReceiver : BroadcastReceiver() {
|
|||||||
try {
|
try {
|
||||||
val appContext = context.applicationContext
|
val appContext = context.applicationContext
|
||||||
AppRuntime.init(appContext)
|
AppRuntime.init(appContext)
|
||||||
AppWakeLocks.init(appContext)
|
|
||||||
runBlocking {
|
runBlocking {
|
||||||
AppRuntime.scrcpy?.stop()
|
AppRuntime.scrcpy?.stop()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user