Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.
Android: keep app windows running when the phone would sleep
Beea7f1f2b09a91
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex ee6b3a4..fc16a20 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -46,6 +46,11 @@ change things that need a step from you, and its notes say so.the PC dropped or was replaced just as you opened an app window or thephone's screen, the phone gave up at once with "Offline". It now waits upto 10 seconds for the connection to come back, and opens the stream on it.+- **App windows keep running when the phone would sleep.** With an app open+ in a window on the PC, the phone went to sleep after its screen timeout,+ which stopped the app and froze the window. While a window is open, the+ phone now stays awake with its screen dimmed, and sleeps as usual once the+ last window closes.## 0.1.3 (2026-10-15)diff --git a/android/app/src/main/kotlin/app/nectarlink/android/elevated/AppDisplay.kt b/android/app/src/main/kotlin/app/nectarlink/android/elevated/AppDisplay.ktindex 192f433..dfd761f 100644--- a/android/app/src/main/kotlin/app/nectarlink/android/elevated/AppDisplay.kt+++ b/android/app/src/main/kotlin/app/nectarlink/android/elevated/AppDisplay.kt@@ -72,6 +72,7 @@ class AppDisplay(if (!fullScreen) {launch(pkg, displayId)watchActivities(displayId)+ keepActive(displayId)}while (running) {val activeCodec = codec ?: break@@ -305,6 +306,27 @@ class AppDisplay(last?.takeIf { it.startsWith("$pkg/") }}.getOrNull()+ /**+ * Tells Android there is activity on [display] every few seconds while+ * the window is open. Otherwise the phone sleeps from inactivity, which+ * stops the app and freezes its window on the PC. The phone's own screen+ * still dims as usual (scrcpy's `--keep-active` does the same).+ */+ private fun keepActive(display: Int) {+ val signal = UserActivity.forDisplay(display) ?: return+ Thread({+ try {+ while (running && signal()) {+ Thread.sleep(KEEP_ACTIVE_MS)+ }+ } catch (_: InterruptedException) {+ }+ }, "app-display-active").apply {+ isDaemon = true+ start()+ }+ }+/** Stops the stream when the app on [display] finishes its last activity. */private fun watchActivities(display: Int) {Thread({@@ -361,6 +383,9 @@ class AppDisplay(const val KIND_FRAME = 1const val KIND_KEYFRAME = 2+ /** How often an open window counts as activity; well under any screen timeout. */+ private const val KEEP_ACTIVE_MS = 4_000L+/** WindowManager.DISPLAY_IME_POLICY_HIDE. */private const val DISPLAY_IME_POLICY_HIDE = 2@@ -441,3 +466,53 @@ internal class ShellContext private constructor(base: Context) : ContextWrapper(}}}++/** `IPowerManager.userActivity`, which the shell may call (DEVICE_POWER). */+@SuppressLint("PrivateApi", "DiscouragedPrivateApi")+private object UserActivity {+ private val power: Any? by lazy {+ runCatching {+ val binder = Class.forName("android.os.ServiceManager")+ .getMethod("getService", String::class.java)+ .invoke(null, "power") as android.os.IBinder+ Class.forName("android.os.IPowerManager\$Stub")+ .getMethod("asInterface", android.os.IBinder::class.java)+ .invoke(null, binder)+ }.onFailure { System.err.println("no power manager: $it") }.getOrNull()+ }++ /**+ * A call that counts as activity on [display] (false once Android refuses+ * it), or null if there is none. Before Android 12 activity is for the+ * whole device.+ */+ fun forDisplay(display: Int): (() -> Boolean)? {+ val manager = power ?: return null+ val perDisplay = Build.VERSION.SDK_INT >= Build.VERSION_CODES.S+ val int = Int::class.javaPrimitiveType+ val long = Long::class.javaPrimitiveType+ val method = runCatching {+ if (perDisplay) {+ manager.javaClass.getMethod("userActivity", int, long, int, int)+ } else {+ manager.javaClass.getMethod("userActivity", long, int, int)+ }+ }.onFailure { System.err.println("no userActivity: $it") }.getOrNull() ?: return null+ return {+ val now = android.os.SystemClock.uptimeMillis()+ runCatching {+ if (perDisplay) {+ method.invoke(manager, display, now, EVENT_OTHER, NO_CHANGE_LIGHTS)+ } else {+ method.invoke(manager, now, EVENT_OTHER, NO_CHANGE_LIGHTS)+ }+ }.onFailure { System.err.println("user activity: $it") }.isSuccess+ }+ }++ /** PowerManager.USER_ACTIVITY_EVENT_OTHER. */+ private const val EVENT_OTHER = 0++ /** PowerManager.USER_ACTIVITY_FLAG_NO_CHANGE_LIGHTS: the screen may still dim. */+ private const val NO_CHANGE_LIGHTS = 1+}