Addressing code-rabbit comments

Signed-off-by: Lance Chant <13349722+lancechant@users.noreply.github.com>
This commit is contained in:
Lance Chant
2026-06-29 14:02:19 +02:00
parent 32ead6d046
commit 2139673a7f

View File

@@ -44,6 +44,11 @@ class PiPController(private val context: Context, private val appContext: AppCon
private var currentPosition: Double = 0.0 private var currentPosition: Double = 0.0
private var currentDuration: Double = 0.0 private var currentDuration: Double = 0.0
private var playbackRate: Double = 1.0 private var playbackRate: Double = 1.0
// Independently tracks whether the system should auto-enter PiP on home
// press. Decoupled from playbackRate so that disabling auto-enter
// (e.g. when the player unmounts) doesn't corrupt the play/pause icon
// state that buildPiPActions() derives from playbackRate.
private var autoEnterEnabled: Boolean = false
private var videoWidth: Int = 0 private var videoWidth: Int = 0
private var videoHeight: Int = 0 private var videoHeight: Int = 0
@@ -106,27 +111,36 @@ class PiPController(private val context: Context, private val appContext: AppCon
} }
fun stopPictureInPicture() { fun stopPictureInPicture() {
// Clear playback rate FIRST so the param rebuild below computes // Disable auto-enter eligibility without touching playbackRate.
// setAutoEnterEnabled=false. Without this, the Activity retains the // playbackRate drives the play/pause icon in buildPiPActions();
// last-set auto-enter=true from when playback was active, and any // mutating it here would cause a stale icon if PiP is re-entered
// home-button press from anywhere in the app triggers PiP — even // before the next playback state callback corrects it.
// after the player has unmounted. autoEnterEnabled = false
playbackRate = 0.0
isInPiPMode = false isInPiPMode = false
pipEntryNotified = false pipEntryNotified = false
unregisterLifecycleCallbacks() unregisterLifecycleCallbacks()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val activity = getActivity() val activity = getActivity() ?: return
// Re-push params with auto-enter disabled so the system stops
// considering this task eligible for auto-PiP on home press. // Push minimal params with just auto-enter disabled. Do NOT call
// buildPiPParams() — it calls ensurePiPReceiverRegistered() and
// setActions(), which would re-register the broadcast receiver
// (just unregistered above) and attach play/pause/skip actions to
// params being torn down. That leaves a live receiver + stale
// actions after the player has unmounted.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
try { try {
activity?.setPictureInPictureParams(buildPiPParams()) activity.setPictureInPictureParams(
PictureInPictureParams.Builder()
.setAutoEnterEnabled(false)
.build()
)
} catch (e: Exception) { } catch (e: Exception) {
Log.e(TAG, "Failed to clear PiP auto-enter params: ${e.message}") Log.e(TAG, "Failed to clear PiP auto-enter params: ${e.message}")
} }
if (activity?.isInPictureInPictureMode == true) { }
activity.moveTaskToBack(false) if (activity.isInPictureInPictureMode) {
} activity.moveTaskToBack(false)
} }
} }
@@ -139,6 +153,7 @@ class PiPController(private val context: Context, private val appContext: AppCon
fun setPlaybackRate(rate: Double) { fun setPlaybackRate(rate: Double) {
playbackRate = rate playbackRate = rate
autoEnterEnabled = rate > 0
if (rate > 0) { if (rate > 0) {
registerLifecycleCallbacks() registerLifecycleCallbacks()
@@ -221,7 +236,7 @@ class PiPController(private val context: Context, private val appContext: AppCon
builder.setActions(buildPiPActions()) builder.setActions(buildPiPActions())
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
builder.setAutoEnterEnabled(forEntering || playbackRate > 0) builder.setAutoEnterEnabled(forEntering || autoEnterEnabled)
} }
return builder.build() return builder.build()