mirror of
https://github.com/Droid-Deck/DroidDeck.git
synced 2026-11-21 13:55:58 +00:00
fix: "DroidDeck isn't responding" on dual-screen handhelds after the bottom screen takes focus (#1871)
This commit is contained in:
1 parent
6673a892e2
commit
8088e47d14
4 files changed
+98
-11
No files matched your search
@@ -70,6 +70,7 @@ object DualScreenPanel {
|
||||
set(value) { field = value; available = value || activity != null }
|
||||
private var ownerTask = -1
|
||||
private var ownerYielded = false
|
||||
private var ownerPaused = false
|
||||
|
||||
var typing by mutableStateOf<TypingRequest?>(null)
|
||||
private set
|
||||
@@ -120,6 +121,7 @@ object DualScreenPanel {
|
||||
displayId = display
|
||||
ownerTask = task
|
||||
ownerYielded = false
|
||||
ownerPaused = false
|
||||
launching = true
|
||||
val intent = Intent(context, DualScreenActivity::class.java)
|
||||
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_MULTIPLE_TASK or Intent.FLAG_ACTIVITY_NO_ANIMATION)
|
||||
@@ -153,15 +155,27 @@ object DualScreenPanel {
|
||||
if (ownerTask == task) ownerYielded = app
|
||||
}
|
||||
|
||||
internal fun returnFocus(context: Context) {
|
||||
if (ownerYielded) return
|
||||
val task = ownerTask.takeIf { it >= 0 } ?: return
|
||||
fun ownerPaused(task: Int, paused: Boolean) {
|
||||
if (ownerTask == task) ownerPaused = paused
|
||||
}
|
||||
|
||||
internal fun focusTask(fromPanel: Boolean): Int? {
|
||||
if (ownerYielded || (ownerPaused && !fromPanel)) return null
|
||||
return ownerTask.takeIf { it >= 0 }
|
||||
}
|
||||
|
||||
internal fun tookFocus(fresh: Boolean, touchedAt: Long, now: Long): Boolean =
|
||||
fresh || now - touchedAt in 0..TOUCH_FOCUS_MS
|
||||
|
||||
private const val TOUCH_FOCUS_MS = 1000L
|
||||
|
||||
internal fun returnFocus(context: Context, fromPanel: Boolean = false) {
|
||||
val task = focusTask(fromPanel) ?: return
|
||||
runCatching { (context.getSystemService(Context.ACTIVITY_SERVICE) as android.app.ActivityManager).moveTaskToFront(task, 0) }
|
||||
}
|
||||
|
||||
internal fun reclaimFocus(context: Context) {
|
||||
if (ownerYielded) return
|
||||
val task = ownerTask.takeIf { it >= 0 } ?: return
|
||||
val task = focusTask(false) ?: return
|
||||
val manager = context.getSystemService(Context.ACTIVITY_SERVICE) as android.app.ActivityManager
|
||||
runCatching {
|
||||
manager.appTasks.firstOrNull {
|
||||
@@ -209,10 +223,10 @@ class DualScreenActivity : ComponentActivity() {
|
||||
runCatching { (getSystemService(ACTIVITY_SERVICE) as android.app.ActivityManager).moveTaskToFront(taskId, 0) }
|
||||
} else {
|
||||
unfocusPending = true
|
||||
DualScreenPanel.returnFocus(this)
|
||||
DualScreenPanel.returnFocus(this, fromPanel = true)
|
||||
window.decorView.postDelayed({
|
||||
if (!unfocusPending || DualScreenPanel.typingShown) return@postDelayed
|
||||
if (window.decorView.hasWindowFocus()) DualScreenPanel.returnFocus(this) else dropFocus()
|
||||
if (window.decorView.hasWindowFocus()) DualScreenPanel.returnFocus(this, fromPanel = true) else dropFocus()
|
||||
}, 400)
|
||||
}
|
||||
}
|
||||
@@ -228,9 +242,24 @@ class DualScreenActivity : ComponentActivity() {
|
||||
if (!DualScreenPanel.typingShown) DualScreenPanel.returnFocus(this)
|
||||
}
|
||||
|
||||
private var topResumed = false
|
||||
private var fresh = true
|
||||
private var touchedAt = Long.MIN_VALUE
|
||||
|
||||
override fun onTopResumedActivityChanged(isTopResumedActivity: Boolean) {
|
||||
super.onTopResumedActivityChanged(isTopResumedActivity)
|
||||
if (isTopResumedActivity && !DualScreenPanel.typingShown) DualScreenPanel.returnFocus(this)
|
||||
topResumed = isTopResumedActivity
|
||||
val took = DualScreenPanel.tookFocus(fresh, touchedAt, android.os.SystemClock.uptimeMillis())
|
||||
fresh = false
|
||||
if (isTopResumedActivity && !DualScreenPanel.typingShown) DualScreenPanel.returnFocus(this, fromPanel = took)
|
||||
}
|
||||
|
||||
override fun dispatchTouchEvent(event: android.view.MotionEvent): Boolean {
|
||||
if (event.actionMasked == android.view.MotionEvent.ACTION_DOWN) {
|
||||
touchedAt = event.downTime
|
||||
if (topResumed && !DualScreenPanel.typingShown) DualScreenPanel.returnFocus(this, fromPanel = true)
|
||||
}
|
||||
return super.dispatchTouchEvent(event)
|
||||
}
|
||||
|
||||
override fun onWindowFocusChanged(hasFocus: Boolean) {
|
||||
@@ -251,6 +280,7 @@ class DualScreenActivity : ComponentActivity() {
|
||||
if (event.action == KeyEvent.ACTION_UP && !event.isCanceled) DualScreenPanel.finishTyping()
|
||||
return true
|
||||
}
|
||||
if (topResumed && !DualScreenPanel.typingShown && event.action == KeyEvent.ACTION_DOWN) DualScreenPanel.returnFocus(this, fromPanel = true)
|
||||
return super.dispatchKeyEvent(event)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user