Add standard session picture in picture (#156)

* Add session picture in picture controls

* Simplify PiP to standard Android controls

* Remove PiP testing document
This commit is contained in:
Kurt Himebauch authored and GitHub committed 2026-10-03 15:06:16 -04:00
1 parent b04900b1b2
commit e60eae3646
14 files changed
+368 -11

No files matched your search

@@ -0,0 +1,15 @@
package com.droiddeck.launcher.session
/** Platform-independent eligibility; a service existing alone does not mean it has a picture. */
object PipPolicy {
fun eligible(running: Boolean, frameSeen: Boolean, phase: SessionPhase, suspended: Boolean, automatic: Boolean): Boolean =
running && frameSeen && phase in setOf(SessionPhase.READY, SessionPhase.SUSPENDED) &&
(!automatic || !suspended)
fun aspect(width: Int, height: Int): Pair<Int, Int> = when {
width <= 0 || height <= 0 -> 16 to 9
width.toDouble() / height > 2.39 -> 239 to 100
height.toDouble() / width > 2.39 -> 100 to 239
else -> width to height
}
}
@@ -0,0 +1,126 @@
package com.droiddeck.launcher.session
import android.app.AppOpsManager
import android.app.PictureInPictureParams
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Rect
import android.net.Uri
import android.os.Build
import android.util.Rational
import android.widget.Toast
import com.droiddeck.launcher.R
import com.droiddeck.launcher.SessionActivity
/** Owns Android's PiP parameters, while the service continues to own the session. */
class SessionPipController(
private val activity: SessionActivity,
private val bounds: () -> Rect,
private val showPipUi: (Boolean) -> Unit,
) {
var transitioning = false
private set
private var suppressAuto = false
private var visit = false
private var lastParams = ""
val floating get() = transitioning || activity.isInPictureInPictureMode
fun eligible(automatic: Boolean = false) = supported(activity) && !activity.isFinishing &&
PipPolicy.eligible(SessionState.running, SessionState.firstFrameSeen, SessionState.phase,
SessionState.suspended, automatic)
fun automatic() = !suppressAuto && SessionPrefs.pipAutoEnter(activity) && eligible(true)
fun refresh(force: Boolean = false) {
if (!supported(activity)) return
val rect = bounds()
val key = "$rect:${SessionState.outputSize}:${automatic()}:${SessionState.suspended}"
if (!force && key == lastParams) return
lastParams = key
runCatching { activity.setPictureInPictureParams(params(rect)) }
}
private fun params(rect: Rect): PictureInPictureParams {
val (w, h) = PipPolicy.aspect(SessionState.outputSize.first, SessionState.outputSize.second)
val builder = PictureInPictureParams.Builder().setAspectRatio(Rational(w, h))
if (!rect.isEmpty) builder.setSourceRectHint(rect)
if (Build.VERSION.SDK_INT >= 31) builder.setAutoEnterEnabled(automatic()).setSeamlessResizeEnabled(false)
return builder.build()
}
fun enter() {
if (!eligible()) {
Toast.makeText(activity, R.string.pip_not_ready, Toast.LENGTH_SHORT).show()
return
}
if (!allowed()) {
Toast.makeText(activity, R.string.pip_permission, Toast.LENGTH_SHORT).show()
runCatching { activity.startActivity(Intent("android.settings.PICTURE_IN_PICTURE_SETTINGS",
Uri.parse("package:${activity.packageName}"))) }
return
}
prepare()
val entered = runCatching { activity.enterPictureInPictureMode(params(bounds())) }.getOrDefault(false)
if (!entered) {
restore()
Toast.makeText(activity, R.string.pip_unavailable, Toast.LENGTH_SHORT).show()
}
}
fun leaving() {
if (!automatic() || !allowed()) return
if (Build.VERSION.SDK_INT < 31) enter() else prepare()
}
private fun prepare() {
if (floating) return
transitioning = true
showPipUi(true)
}
fun changed(inPip: Boolean) {
if (inPip) {
transitioning = false
visit = true
SessionState.pipActive = true
showPipUi(true)
SessionService.beginPip(activity)
SessionService.setActivityVisible(activity, true)
} else restore()
refresh(true)
}
private fun restore() {
val needsRestore = transitioning || visit
transitioning = false
if (visit) {
SessionState.pipActive = false
visit = false
}
if (needsRestore) showPipUi(false)
}
fun resumed() {
suppressAuto = false
if (!activity.isInPictureInPictureMode) {
restore()
if (SessionState.running) SessionService.setActivityVisible(activity, true)
}
refresh(true)
}
fun background() { suppressAuto = true; refresh(true) }
fun destroyed() { if (visit) restore() }
private fun allowed(): Boolean {
val ops = activity.getSystemService(AppOpsManager::class.java)
@Suppress("DEPRECATION")
return ops.checkOpNoThrow(AppOpsManager.OPSTR_PICTURE_IN_PICTURE,
android.os.Process.myUid(), activity.packageName) == AppOpsManager.MODE_ALLOWED
}
companion object {
fun supported(context: android.content.Context) = context.packageManager
.hasSystemFeature(PackageManager.FEATURE_PICTURE_IN_PICTURE)
}
}
@@ -627,6 +627,12 @@ object SessionPrefs {
*/
fun prefMode(mode: String): String = if (mode == SessionService.MODE_RUN) SessionService.MODE_STEAM else mode
fun pipAutoEnter(context: Context): Boolean = prefs(context).getBoolean("pipAutoEnter", false)
fun setPipAutoEnter(context: Context, enabled: Boolean) {
prefs(context).edit().putBoolean("pipAutoEnter", enabled).apply()
}
fun suspendPolicy(context: Context, mode: String): String =
prefs(context).getString("suspendPolicy.${prefMode(mode)}", SUSPEND_MANUAL)
?.takeIf { it == SUSPEND_AUTO || it == SUSPEND_MANUAL || it == SUSPEND_NEVER }
@@ -78,6 +78,7 @@ class SessionService : Service() {
/** An explicit Steam sleep request is independent of the background policy. */
private var steamSleepToken: String? = null
private var suspendOperationPending = false
private var pipTask = false
private var suspendAttemptFailed = false
/** One validated game request waiting for this Steam session to reach a usable state. */
private var pendingSteamGameId: String? = null
@@ -151,7 +152,12 @@ class SessionService : Service() {
stopSession(0)
return START_NOT_STICKY
}
ACTION_PIP_BEGIN -> {
pipTask = true
return START_NOT_STICKY
}
ACTION_ACTIVITY_VISIBLE -> {
if (!SessionState.pipActive) pipTask = false
activityVisible = true
resumeSteamSleepOnReturn()
suspendAttemptFailed = false
@@ -193,6 +199,8 @@ class SessionService : Service() {
suspendAttemptFailed = false
suspendController = null
SessionState.suspended = false
SessionState.pipActive = false
pipTask = false
SessionState.program = intent?.getStringExtra(EXTRA_PROGRAM)
SessionState.programArgs = intent?.getStringArrayExtra(EXTRA_PROGRAM_ARGS)?.toList().orEmpty()
SessionState.steamUi = intent?.getStringExtra(EXTRA_STEAM_UI)
@@ -1158,6 +1166,8 @@ class SessionService : Service() {
return@post
}
SessionState.suspended = false
SessionState.pipActive = false
pipTask = false
SessionState.guestPid = -1
releaseLocks()
if (status == 0) {
@@ -1267,6 +1277,12 @@ class SessionService : Service() {
* guest would survive as an orphan holding the rootfs and the GPU. Treat the swipe as "quit".
*/
override fun onTaskRemoved(rootIntent: Intent?) {
if (pipTask) {
Log.i(TAG, "PiP task dismissed - retaining the session under its background policy")
activityVisible = false
updateSuspendPolicy()
return
}
Log.i(TAG, "task removed - ending the session")
stopSession(0)
super.onTaskRemoved(rootIntent)
@@ -1407,6 +1423,7 @@ class SessionService : Service() {
private const val NOTIFICATION_ID = 1001
const val ACTION_STOP = "com.droiddeck.launcher.STOP_SESSION"
const val ACTION_RESUME = "com.droiddeck.launcher.RESUME_SESSION"
private const val ACTION_PIP_BEGIN = "com.droiddeck.launcher.PIP_BEGIN"
const val ACTION_LAUNCH_GAME = "com.droiddeck.launcher.LAUNCH_STEAM_GAME"
const val ACTION_HOME_GUIDE = "com.droiddeck.launcher.HOME_GUIDE"
const val ACTION_AGENT_START = "com.droiddeck.launcher.AGENT_START"
@@ -1467,6 +1484,10 @@ class SessionService : Service() {
context.startService(Intent(context, SessionService::class.java).setAction(ACTION_STOP))
}
fun beginPip(context: Context) {
context.startService(Intent(context, SessionService::class.java).setAction(ACTION_PIP_BEGIN))
}
fun setActivityVisible(context: Context, visible: Boolean) {
if (!SessionState.running) return
val action = if (visible) ACTION_ACTIVITY_VISIBLE else ACTION_ACTIVITY_HIDDEN
@@ -47,6 +47,7 @@ object SessionState {
@Volatile
var running = false
var suspended by mutableStateOf(false)
@Volatile var pipActive = false
/** MODE_RUN: the program inside the runtime the session was started for. */
@Volatile
var program: String? = null