add Z-order mode for player

This commit is contained in:
ggys
2023-04-13 08:23:39 +03:00
parent 6589c1e2cb
commit 3dbd9ec3ae
3 changed files with 26 additions and 6 deletions
@@ -8,16 +8,21 @@ private const val VIDEO_URL_KEY = "url"
private const val REPEAT_KEY = "repeat"
private const val STUB_IMAGE_URL_KEY = "stub_image_url"
private const val START_MODE_KEY = "start_mode"
private const val Z_ORDER_MODE_KEY = "z_order_mode"
private const val START_MODE_WHEN_READY = "when_ready"
private const val START_MODE_BY_DEEPLINK = "by_deeplink"
private const val Z_ORDER_MODE_ON_TOP = "on_top"
private const val Z_ORDER_MODE_MEDIA_OVERLAY = "media_overlay"
internal data class VideoConfig(
val id: String?,
val url: String,
val stubImageUrl: String?,
val repeat: Boolean,
val startMode: VideoStartMode
val startMode: VideoStartMode,
val zOrderMode: ZOrderMode,
)
internal enum class VideoStartMode {
@@ -25,13 +30,19 @@ internal enum class VideoStartMode {
BY_DEEPLINK
}
internal enum class ZOrderMode {
ON_TOP,
MEDIA_OVERLAY
}
internal val DivCustom.videoConfig: VideoConfig
get() = VideoConfig(
id = id,
url = customProps?.getStringOrNull(VIDEO_URL_KEY) ?: "",
stubImageUrl = customProps?.getStringOrNull(STUB_IMAGE_URL_KEY),
repeat = customProps?.optBoolean(REPEAT_KEY, false) ?: false,
startMode = customProps?.getStringOrNull(START_MODE_KEY).resolveStartMode()
startMode = customProps?.getStringOrNull(START_MODE_KEY).resolveStartMode(),
zOrderMode = customProps?.getStringOrNull(Z_ORDER_MODE_KEY).resolveZOrderMode(),
)
internal val VideoConfig.dataSpec: DataSpec
@@ -46,6 +57,12 @@ private fun String?.resolveStartMode(): VideoStartMode = when (this) {
else -> VideoStartMode.WHEN_READY
}
private fun String?.resolveZOrderMode(): ZOrderMode = when (this) {
Z_ORDER_MODE_ON_TOP -> ZOrderMode.ON_TOP
Z_ORDER_MODE_MEDIA_OVERLAY -> ZOrderMode.MEDIA_OVERLAY
else -> ZOrderMode.ON_TOP
}
internal fun isValid(config: VideoConfig): Boolean {
val requiredId = config.startMode == VideoStartMode.BY_DEEPLINK
val hasIdIfRequired = !requiredId || config.id != null
@@ -25,7 +25,7 @@ class VideoCustomAdapter(
}
override fun createView(div: DivCustom, divView: Div2View): View {
return VideoView(divView.context)
return VideoView(divView.context, div.videoConfig.zOrderMode)
}
override fun bindView(view: View, div: DivCustom, divView: Div2View) {
@@ -22,7 +22,8 @@ import kotlinx.coroutines.cancelChildren
import kotlinx.coroutines.launch
internal class VideoView(
context: Context
context: Context,
zOrderMode: ZOrderMode = ZOrderMode.ON_TOP,
) : FrameLayout(context) {
private val coroutineScope: CoroutineScope = MainScope()
private var stubViewObservationJob: Job? = null
@@ -34,7 +35,10 @@ internal class VideoView(
useController = false
setShutterBackgroundColor(Color.TRANSPARENT)
(videoSurfaceView as? SurfaceView)?.apply {
setZOrderOnTop(true)
when (zOrderMode) {
ZOrderMode.ON_TOP -> setZOrderOnTop(true)
ZOrderMode.MEDIA_OVERLAY -> setZOrderMediaOverlay(true)
}
setBackgroundColor(Color.TRANSPARENT)
holder.setFormat(PixelFormat.TRANSPARENT)
}
@@ -126,7 +130,6 @@ internal class VideoView(
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
assertBoundToViewModel()
viewModel?.let { pauseObservingViewModel(it) }
}