diff --git a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt index 618452d02..b50175f60 100644 --- a/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/SessionActivity.kt @@ -5615,6 +5615,11 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde override fun onPictureInPictureModeChanged(inPip: Boolean, newConfig: android.content.res.Configuration) { super.onPictureInPictureModeChanged(inPip, newConfig) + if (inPip) { + uiHandler.removeCallbacks(guardWhenRunning) + uiHandler.removeCallbacks(rearmAfterYield) + com.droiddeck.launcher.session.KeepTopActivity.stepAside() + } if (::pip.isInitialized) pip.changed(inPip) if (!inPip && surfaceW > 0) resizeOutputForWindow(surfaceW, surfaceH) applyUnfoldedControls() @@ -5657,6 +5662,9 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde override fun onStop() { if (lightPause.stopped()) departureCleanup() + uiHandler.removeCallbacks(guardWhenRunning) + uiHandler.removeCallbacks(rearmAfterYield) + com.droiddeck.launcher.session.KeepTopActivity.stepAside() if (runtimeRemovalBlocked) { super.onStop(); return } started = false if (::hud.isInitialized) hud.setBackgrounded(true) @@ -5700,6 +5708,7 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde override fun onResume() { super.onResume() + com.droiddeck.launcher.session.KeepTopActivity.onYield = onGuardYield uiHandler.removeCallbacks(lightPauseCheck) when (lightPause.resumed(com.droiddeck.launcher.session.KeepTopActivity.bounces)) { com.droiddeck.launcher.session.LightPause.Resume.LIGHT -> return @@ -5783,6 +5792,19 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde } } + // A touch that removed a guard while the session kept the top spot did not move focus to that + // display, so the guard comes back unless the user carries on there. + private val rearmAfterYield = Runnable { + if (topResumed && !isFinishing) { + com.droiddeck.launcher.session.KeepTopActivity.rearm() + guardOtherDisplays() + } + } + private val onGuardYield: () -> Unit = { + uiHandler.removeCallbacks(rearmAfterYield) + uiHandler.postDelayed(rearmAfterYield, com.droiddeck.launcher.session.KeepTopActivity.YIELD_GRACE_MS) + } + // RUNNING_LOW and RUNNING_CRITICAL are not delivered from API 34, so the guard also goes up // as soon as a running session is on top. override fun onTrimMemory(level: Int) { @@ -5796,13 +5818,17 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde super.onTopResumedActivityChanged(isTopResumedActivity) topResumed = isTopResumedActivity uiHandler.removeCallbacks(guardWhenRunning) - if (isTopResumedActivity) uiHandler.post(guardWhenRunning) + uiHandler.removeCallbacks(rearmAfterYield) + if (isTopResumedActivity) { + com.droiddeck.launcher.session.KeepTopActivity.rearm() + uiHandler.post(guardWhenRunning) + } else if (com.droiddeck.launcher.session.TopUnderPressure.shouldReclaim( android.os.SystemClock.elapsedRealtime(), memoryPressureAt, SessionState.running, isFinishing)) guardOtherDisplays() } private fun guardOtherDisplays() { - if (!SessionState.running || isFinishing) return + if (!SessionState.running || isFinishing || isInPictureInPictureMode) return val wasResumed = lifecycle.currentState.isAtLeast(androidx.lifecycle.Lifecycle.State.RESUMED) val bounced = com.droiddeck.launcher.session.KeepTopActivity.guardOtherDisplays(this, display?.displayId ?: android.view.Display.DEFAULT_DISPLAY) if (com.droiddeck.launcher.session.KeepTopActivity.hasPendingStarts()) { @@ -6483,6 +6509,8 @@ class SessionActivity : ComponentActivity(), SurfaceHolder.Callback, com.droidde override fun onDestroy() { uiHandler.removeCallbacks(guardWhenRunning) + uiHandler.removeCallbacks(rearmAfterYield) + if (com.droiddeck.launcher.session.KeepTopActivity.onYield === onGuardYield) com.droiddeck.launcher.session.KeepTopActivity.onYield = null com.droiddeck.launcher.session.KeepTopActivity.release() synchronized(frameGenGlobalLock) { frameGenActivityActive = false diff --git a/app/src/main/java/com/droiddeck/launcher/session/KeepTopActivity.kt b/app/src/main/java/com/droiddeck/launcher/session/KeepTopActivity.kt index 849c0aafc..109f6d282 100644 --- a/app/src/main/java/com/droiddeck/launcher/session/KeepTopActivity.kt +++ b/app/src/main/java/com/droiddeck/launcher/session/KeepTopActivity.kt @@ -9,11 +9,15 @@ import android.os.Bundle import android.os.SystemClock import android.util.Log import android.view.Display +import android.view.MotionEvent import android.view.WindowManager -// While a session runs, sits invisibly on top of every other display until it ends, so -// that display's home launcher is not its top activity. lmkd kills the launcher first; restarted as a -// top activity it takes focus from the session, which then drops to adj 50 and is killed next. +// While a session is on screen, sits invisibly on top of every other display, so that display's +// home launcher is not its top activity. lmkd kills the launcher first; restarted as a top activity +// it takes focus from the session, which then drops to adj 50 and is killed next. +// Android gives a translucent activity alone in its task an input sink that eats every touch on the +// display whatever its window flags say, so a touch makes the guard leave that display until the +// session is back on top. class KeepTopActivity : Activity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -27,13 +31,28 @@ class KeepTopActivity : Activity() { finishAndRemoveTask() return } - window.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) + window.addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE) val id = display?.displayId ?: Display.INVALID_DISPLAY starting.remove(id) + if (id in yielded) { + finishAndRemoveTask() + return + } guards[id]?.takeIf { it !== this && !it.isFinishing }?.finishAndRemoveTask() guards[id] = this } + override fun dispatchTouchEvent(event: MotionEvent): Boolean { + if (event.actionMasked == MotionEvent.ACTION_DOWN && !isFinishing) { + val id = display?.displayId ?: Display.INVALID_DISPLAY + yielded.add(id) + Log.i(TAG, "display $id touched, leaving it to the app below") + finishAndRemoveTask() + onYield?.invoke() + } + return true + } + override fun onDestroy() { guards.values.remove(this) super.onDestroy() @@ -49,13 +68,18 @@ class KeepTopActivity : Activity() { private val guards = HashMap() private const val START_TIMEOUT_MS = 2_000L private val starting = HashMap() + private val yielded = HashSet() + const val YIELD_GRACE_MS = 5_000L + var onYield: (() -> Unit)? = null + + fun rearm() = yielded.clear() fun guardOtherDisplays(context: Context, sessionDisplay: Int): Boolean { val displays = context.getSystemService(DisplayManager::class.java)?.displays ?: return false var started = false for (display in displays) { val id = display.displayId - if (id == sessionDisplay || display.state != Display.STATE_ON || guards[id]?.isFinishing == false || + if (id == sessionDisplay || id in yielded || display.state != Display.STATE_ON || guards[id]?.isFinishing == false || starting[id]?.let { SystemClock.elapsedRealtime() - it < START_TIMEOUT_MS } == true) continue runCatching { launch(context, id, bounce = false) @@ -73,13 +97,18 @@ class KeepTopActivity : Activity() { const val RETRY_MS = START_TIMEOUT_MS + 250 - fun release() { + fun stepAside() { generation++ guards.values.toList().forEach { it.finishAndRemoveTask() } guards.clear() starting.clear() } + fun release() { + stepAside() + yielded.clear() + } + private fun launch(context: Context, displayId: Int, bounce: Boolean) { val intent = Intent(context, KeepTopActivity::class.java) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_MULTIPLE_TASK or Intent.FLAG_ACTIVITY_NO_ANIMATION) diff --git a/docs/development/memory-guard-ab.md b/docs/development/memory-guard-ab.md new file mode 100644 index 000000000..4ac8d1a21 --- /dev/null +++ b/docs/development/memory-guard-ab.md @@ -0,0 +1,90 @@ +# Keep-top guard A/B on dual screens + +`KeepTopActivity` (#2763) covers every other display while a session is on top, so that +display's home is not its top activity. On the Thor, lmkd kills the bottom home under memory +pressure, Android relaunches it to the front (`wm_task_to_front`), the session loses the top +spot, drops to adj 50 and is killed in lmkd's next pass. + +Android gives a translucent activity that is alone in its task an `ActivityRecordInputSink` +that consumes every touch inside the task bounds, whatever the window's flags say +(`ENABLE_TOUCH_OPAQUE_ACTIVITIES`, on for DroidDeck's target SDK; see +`services/core/java/com/android/server/wm/ActivityRecordInputSink.java`). The guard's +`FLAG_NOT_TOUCHABLE` therefore did nothing: with the dual-screen panel closed, the bottom +screen took no touch until the session ended, and the guard also stayed over it after Home. + +The guard now takes touches itself. A touch removes it from that display. It comes back when +the session is top-resumed again, or after 5 s if the session kept the top spot (one touch on +the bottom screen, then back to the game). Stopping or entering PiP removes the guards. + +## Tool + +`tools/memory_guard_ab.py` drives a rooted dual-screen device over adb (`su` required) with a +build that has the debugging tools: + +```sh +python3 tools/memory_guard_ab.py --label main --apk main.apk states +python3 tools/memory_guard_ab.py --label fix --apk fix.apk states +python3 tools/memory_guard_ab.py --label fix pressure --stop-mb 650 --hold 30 --runs 3 +python3 tools/memory_guard_ab.py --label fix summarize ~/.cache/droiddeck-memory-guard-ab/fix-stop650-1-events.txt +``` + +Results are JSON lines in `~/.cache/droiddeck-memory-guard-ab/