This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 475
/
MediaSessionFullscreenFeature.kt
73 lines (65 loc) · 2.91 KB
/
MediaSessionFullscreenFeature.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package mozilla.components.feature.media.fullscreen
import android.app.Activity
import android.content.pm.ActivityInfo
import android.os.Build
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.map
import mozilla.components.browser.state.selector.findCustomTabOrSelectedTab
import mozilla.components.browser.state.state.SessionState
import mozilla.components.browser.state.store.BrowserStore
import mozilla.components.lib.state.ext.flowScoped
import mozilla.components.support.base.feature.LifecycleAwareFeature
import mozilla.components.support.ktx.kotlinx.coroutines.flow.ifChanged
/**
* Feature that will auto-rotate the device to the correct orientation for the media aspect ratio.
*/
class MediaSessionFullscreenFeature(
private val activity: Activity,
private val store: BrowserStore,
private val tabId: String?,
) : LifecycleAwareFeature {
private var scope: CoroutineScope? = null
override fun start() {
scope = store.flowScoped { flow ->
flow.map {
it.tabs + it.customTabs
}.map { tab ->
tab.filter { it.mediaSessionState != null && it.mediaSessionState!!.fullscreen }
}.ifChanged().collect { states ->
processFullscreen(states)
}
}
}
@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
}
if (store.state.findCustomTabOrSelectedTab(tabId)?.id == activeState.id) {
when (activeState.mediaSessionState?.elementMetadata?.portrait) {
true ->
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
}
}
}
override fun stop() {
scope?.cancel()
}
}