Skip to content

Commit

Permalink
Bug 1838541: Changed distinctUntilChanged for MediaSessionFullScreenF…
Browse files Browse the repository at this point in the history
…eature

Co-authored-by: Zac McKenney <[email protected]>
  • Loading branch information
2 people authored and mergify[bot] committed Oct 2, 2023
1 parent dd354dc commit 5870dca
Showing 1 changed file with 36 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,58 +35,64 @@ class MediaSessionFullscreenFeature(
flow.map {
it.tabs + it.customTabs
}.map { tab ->
tab.filter { it.mediaSessionState != null && it.mediaSessionState!!.fullscreen }
}.distinctUntilChanged().collect { states ->
processFullscreen(states)
processDeviceSleepMode(states)
tab.firstOrNull { it.mediaSessionState?.fullscreen == true }
}.distinctUntilChanged { old, new ->
old.hasSameOrientationInformationAs(new)
}.collect { state ->
// There should only be one fullscreen session.
if (state == null) {
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER
activity.window?.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
return@collect
}

if (store.state.findCustomTabOrSelectedTab(tabId)?.id == state.id) {
setOrientationForTabState(state)
}
setDeviceSleepModeForTabState(state)
}
}
}

@Suppress("SourceLockedOrientationActivity") // We deliberately want to lock the orientation here.
private fun processFullscreen(sessionStates: List<SessionState>) {
/* there should only be one fullscreen session */
val activeState = sessionStates.firstOrNull()
if (activeState == null || activeState.mediaSessionState?.fullscreen != true) {
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER
return
}
private fun setOrientationForTabState(activeTabState: SessionState) {
when (activeTabState.mediaSessionState?.elementMetadata?.portrait) {
true ->
activity.requestedOrientation =
ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT

if (store.state.findCustomTabOrSelectedTab(tabId)?.id == activeState.id) {
when (activeState.mediaSessionState?.elementMetadata?.portrait) {
true ->
false ->
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && activity.isInPictureInPictureMode) {
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
} else {
activity.requestedOrientation =
ActivityInfo.SCREEN_ORIENTATION_USER_PORTRAIT
false ->
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && activity.isInPictureInPictureMode) {
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
} else {
activity.requestedOrientation =
ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
}
else -> activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER
}
ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE
}

null -> activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER
}
}

private fun processDeviceSleepMode(sessionStates: List<SessionState>) {
val activeTabState = sessionStates.firstOrNull()
if (activeTabState == null || activeTabState.mediaSessionState?.fullscreen != true) {
activity.window?.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
return
}
private fun setDeviceSleepModeForTabState(activeTabState: SessionState) {
activeTabState.mediaSessionState?.let {
when (activeTabState.mediaSessionState?.playbackState) {
MediaSession.PlaybackState.PLAYING -> {
activity.window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}

else -> {
activity.window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)
}
}
}
}

private fun SessionState?.hasSameOrientationInformationAs(other: SessionState?): Boolean =
this?.mediaSessionState?.fullscreen == other?.mediaSessionState?.fullscreen &&
this?.mediaSessionState?.playbackState == other?.mediaSessionState?.playbackState &&
this?.mediaSessionState?.elementMetadata == other?.mediaSessionState?.elementMetadata &&
this?.content?.pictureInPictureEnabled == other?.content?.pictureInPictureEnabled

override fun stop() {
scope?.cancel()
}
Expand Down

0 comments on commit 5870dca

Please sign in to comment.