override fun onCreate()

in customui/src/main/java/com/amazonaws/ivs/player/customui/activities/MainActivity.kt [89:193]


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        App.component.inject(this)
        DataBindingUtil.setContentView<ActivityMainBinding>(this, R.layout.activity_main).apply {
            data = viewModel
            lifecycleOwner = this@MainActivity
        }

        // Surface view listener for rotation handling
        surface_view.addOnLayoutChangeListener(
            object : View.OnLayoutChangeListener {
                override fun onLayoutChange(
                    v: View?,
                    left: Int,
                    top: Int,
                    right: Int,
                    bottom: Int,
                    oldLeft: Int,
                    oldTop: Int,
                    oldRight: Int,
                    oldBottom: Int
                ) {
                    if (left != oldLeft || top != oldTop || right != oldRight || bottom != oldBottom) {
                        val width = viewModel.playerParamsChanged.value?.first
                        val height = viewModel.playerParamsChanged.value?.second
                        if (width != null && height != null) {
                            surface_view.post {
                                Log.d(TAG,"On rotation player layout params changed $width $height")
                                ViewUtil.setLayoutParams(surface_view, width, height)
                            }
                        }
                    }
                }
            }
        )

        sheetBackground.setOnClickListener {
            qualityDialog.dismiss()
            rateDialog.dismiss()
            sourceDialog.dismiss()
        }

        viewModel.playerState.observe(this, Observer { state ->
            when (state) {
                Player.State.BUFFERING -> {
                    // Indicates that the Player is buffering content
                    viewModel.buffering.value = true
                    viewModel.buttonState.value = PlayingState.PLAYING
                    status_text.setTextColor(Color.WHITE)
                    status_text.text = getString(R.string.buffering)
                }
                Player.State.IDLE -> {
                    // Indicates that the Player is idle
                    viewModel.buffering.value = false
                    viewModel.buttonState.value = PlayingState.PAUSED
                    status_text.setTextColor(Color.WHITE)
                    status_text.text = getString(R.string.paused)
                }
                Player.State.READY -> {
                    // Indicates that the Player is ready to play the loaded source
                    viewModel.buffering.value = false
                    viewModel.buttonState.value = PlayingState.PAUSED
                    status_text.setTextColor(Color.WHITE)
                    status_text.text = getString(R.string.paused)
                }
                Player.State.ENDED -> {
                    // Indicates that the Player reached the end of the stream
                    viewModel.buffering.value = false
                    viewModel.buttonState.value = PlayingState.PAUSED
                    status_text.setTextColor(Color.WHITE)
                    status_text.text = getString(R.string.ended)
                }
                Player.State.PLAYING -> {
                    // Indicates that the Player is playing
                    viewModel.buffering.value = false
                    viewModel.buttonState.value = PlayingState.PLAYING
                    status_text.setTextColor(
                        (if (viewModel.liveStream.value != null && viewModel.liveStream.value == true)
                            Color.RED else Color.WHITE)
                    )
                    status_text.text = if (viewModel.liveStream.value != null && viewModel.liveStream.value == true)
                        getString(R.string.live_status) else getString(R.string.vod_status)
                }
                else -> { /* Ignored */ }
            }
        })

        viewModel.buttonState.observe(this, Observer { state ->
            viewModel.isPlaying.value = state == PlayingState.PLAYING
        })

        viewModel.playerParamsChanged.observe(this, Observer {
            Log.d(TAG,"Player layout params changed ${it.first} ${it.second}")
            ViewUtil.setLayoutParams(surface_view, it.first, it.second)
        })

        viewModel.errorHappened.observe(this, Observer {
            Log.d(TAG,"Error dialog is shown")
           showDialog(it.first, it.second)
        })

        initSurface()
        initButtons()
        viewModel.playerStart(surface_view.holder.surface)
    }