mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-21 13:55:58 +00:00
fix(session): keep the session the top app on dual screens under memory pressure (#2763)
This commit is contained in:
1 parent
b88b5f6b1d
commit
fa95df37d0
9 files changed
+296
-5
No files matched your search
@@ -0,0 +1,91 @@
|
||||
package com.droiddeck.launcher.session
|
||||
|
||||
import android.app.Activity
|
||||
import android.app.ActivityOptions
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.hardware.display.DisplayManager
|
||||
import android.os.Bundle
|
||||
import android.os.SystemClock
|
||||
import android.util.Log
|
||||
import android.view.Display
|
||||
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.
|
||||
class KeepTopActivity : Activity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
@Suppress("DEPRECATION") overridePendingTransition(0, 0)
|
||||
if (intent.getBooleanExtra(EXTRA_BOUNCE, false)) {
|
||||
bounces++
|
||||
finish()
|
||||
return
|
||||
}
|
||||
if (intent.getIntExtra(EXTRA_GENERATION, -1) != generation) {
|
||||
finishAndRemoveTask()
|
||||
return
|
||||
}
|
||||
window.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE or WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE)
|
||||
val id = display?.displayId ?: Display.INVALID_DISPLAY
|
||||
starting.remove(id)
|
||||
guards[id]?.takeIf { it !== this && !it.isFinishing }?.finishAndRemoveTask()
|
||||
guards[id] = this
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
guards.values.remove(this)
|
||||
super.onDestroy()
|
||||
}
|
||||
|
||||
companion object {
|
||||
private const val TAG = "KeepTop"
|
||||
private const val EXTRA_BOUNCE = "bounce"
|
||||
private const val EXTRA_GENERATION = "generation"
|
||||
private var generation = 0
|
||||
var bounces = 0
|
||||
private set
|
||||
private val guards = HashMap<Int, KeepTopActivity>()
|
||||
private const val START_TIMEOUT_MS = 2_000L
|
||||
private val starting = HashMap<Int, Long>()
|
||||
|
||||
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 ||
|
||||
starting[id]?.let { SystemClock.elapsedRealtime() - it < START_TIMEOUT_MS } == true) continue
|
||||
runCatching {
|
||||
launch(context, id, bounce = false)
|
||||
starting[id] = SystemClock.elapsedRealtime()
|
||||
started = true
|
||||
Log.i(TAG, "guarding display $id from a home launcher restart")
|
||||
}.onFailure { Log.w(TAG, "could not guard display $id", it) }
|
||||
}
|
||||
if (!started) return false
|
||||
return runCatching { launch(context, sessionDisplay, bounce = true) }
|
||||
.onFailure { Log.w(TAG, "could not return focus to display $sessionDisplay", it) }.isSuccess
|
||||
}
|
||||
|
||||
fun hasPendingStarts() = starting.isNotEmpty()
|
||||
|
||||
const val RETRY_MS = START_TIMEOUT_MS + 250
|
||||
|
||||
fun release() {
|
||||
generation++
|
||||
guards.values.toList().forEach { it.finishAndRemoveTask() }
|
||||
guards.clear()
|
||||
starting.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)
|
||||
.putExtra(EXTRA_BOUNCE, bounce)
|
||||
.putExtra(EXTRA_GENERATION, generation)
|
||||
context.startActivity(intent, ActivityOptions.makeBasic().setLaunchDisplayId(displayId).toBundle())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.droiddeck.launcher.session
|
||||
|
||||
// Tracks a pause that skipped the session's departure cleanup for the keep-top focus bounce.
|
||||
internal class LightPause {
|
||||
private var pending = false
|
||||
private var bouncesAtPause = 0
|
||||
|
||||
fun paused(bounces: Int) {
|
||||
pending = true
|
||||
bouncesAtPause = bounces
|
||||
}
|
||||
|
||||
// The bounce resumes the session within tens of milliseconds; still paused at the timeout means
|
||||
// something else holds the pause.
|
||||
fun timedOut(): Boolean = take()
|
||||
|
||||
fun stopped(): Boolean = take()
|
||||
|
||||
fun resumed(bounces: Int): Resume {
|
||||
if (!pending) return Resume.NORMAL
|
||||
pending = false
|
||||
return if (bounces != bouncesAtPause) Resume.LIGHT else Resume.CLEANUP_FIRST
|
||||
}
|
||||
|
||||
private fun take(): Boolean {
|
||||
val was = pending
|
||||
pending = false
|
||||
return was
|
||||
}
|
||||
|
||||
enum class Resume { NORMAL, LIGHT, CLEANUP_FIRST }
|
||||
}
|
||||
@@ -2347,7 +2347,8 @@ class SessionService : Service() {
|
||||
override fun onTaskRemoved(rootIntent: Intent?) {
|
||||
if (rootIntent?.component?.className in setOf(
|
||||
com.droiddeck.launcher.ui.DualScreenActivity::class.java.name,
|
||||
com.droiddeck.launcher.ui.SessionTypingActivity::class.java.name)) return
|
||||
com.droiddeck.launcher.ui.SessionTypingActivity::class.java.name,
|
||||
KeepTopActivity::class.java.name)) return
|
||||
if (pipTask || xrActivityVisible) {
|
||||
Log.i(TAG, "panel task dismissed - retaining the session under its active surface policy")
|
||||
activityVisible = false
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.droiddeck.launcher.session
|
||||
|
||||
import android.content.ComponentCallbacks2
|
||||
|
||||
internal object TopUnderPressure {
|
||||
const val WINDOW_MS = 5_000L
|
||||
|
||||
fun isPressure(level: Int) =
|
||||
level == ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL || level == ComponentCallbacks2.TRIM_MEMORY_RUNNING_LOW
|
||||
|
||||
fun shouldReclaim(nowMs: Long, pressureAtMs: Long, sessionRunning: Boolean, finishing: Boolean) =
|
||||
sessionRunning && !finishing && pressureAtMs > 0 && nowMs - pressureAtMs in 0..WINDOW_MS
|
||||
}
|
||||
Reference in new issue
Block a user