<commit_before>package blue.aodev.animeultimetv.presentation.screen.playback

import android.app.Activity
import android.net.Uri
import android.support.v17.leanback.media.PlaybackGlue
import android.support.v17.leanback.media.PlaybackTransportControlGlue
import android.support.v17.leanback.widget.Action
import android.support.v17.leanback.widget.ArrayObjectAdapter
import android.support.v17.leanback.widget.PlaybackControlsRow
import android.widget.Toast
import blue.aodev.animeultimetv.domain.model.Playlist


class PlaylistMediaPlayerGlue(context: Activity, impl: ExoPlayerAdapter, var playlist: Playlist)
    : PlaybackTransportControlGlue<ExoPlayerAdapter>(context, impl) {

    private val previousAction = PlaybackControlsRow.SkipPreviousAction(context)
    private val nextAction = PlaybackControlsRow.SkipNextAction(context)
    private val hdAction = PlaybackControlsRow.HighQualityAction(context)

    init {
        updateCurrentVideo()
        playWhenReady()
    }

    private fun updateCurrentVideo() {
        val currentVideo = playlist.currentVideo
        title = playlist.title
        subtitle = currentVideo.title
        playerAdapter.setDataSource(Uri.parse(currentVideo.hdVideoUrl))
        NetworkPlaybackSeekDataProvider.setDemoSeekProvider(this, currentVideo.hdVideoUrl)
    }

    override fun onCreatePrimaryActions(adapter: ArrayObjectAdapter) {
        super.onCreatePrimaryActions(adapter)
        adapter.add(previousAction)
        adapter.add(nextAction)
    }

    override fun onCreateSecondaryActions(adapter: ArrayObjectAdapter) {
        super.onCreateSecondaryActions(secondaryActionsAdapter)
        adapter.add(hdAction)
    }

    override fun onActionClicked(action: Action) {
        if (shouldDispatchAction(action)) {
            dispatchAction(action)
            return
        }
        super.onActionClicked(action)
    }

    private fun shouldDispatchAction(action: Action): Boolean {
        return action === hdAction
    }

    private fun dispatchAction(action: Action) {
        Toast.makeText(context, action.toString(), Toast.LENGTH_SHORT).show()
        val multiAction = action as PlaybackControlsRow.MultiAction
        multiAction.nextIndex()
        notifyActionChanged(multiAction)
    }

    private fun notifyActionChanged(action: PlaybackControlsRow.MultiAction) {
        var index = -1
        if (primaryActionsAdapter != null) {
            index = primaryActionsAdapter!!.indexOf(action)
        }
        if (index >= 0) {
            primaryActionsAdapter!!.notifyArrayItemRangeChanged(index, 1)
        } else {
            if (secondaryActionsAdapter != null) {
                index = secondaryActionsAdapter!!.indexOf(action)
                if (index >= 0) {
                    secondaryActionsAdapter!!.notifyArrayItemRangeChanged(index, 1)
                }
            }
        }
    }

    private val primaryActionsAdapter: ArrayObjectAdapter?
        get() = controlsRow?.primaryActionsAdapter as ArrayObjectAdapter?

    private val secondaryActionsAdapter: ArrayObjectAdapter?
        get() = controlsRow?.secondaryActionsAdapter as ArrayObjectAdapter?

    fun playWhenReady() {
        if (isPrepared) {
            play()
        } else {
            addPlayerCallback(object : PlayerCallback() {
                override fun onPreparedStateChanged(glue: PlaybackGlue) {
                    if (glue.isPrepared) {
                        glue.removePlayerCallback(this)
                        glue.play()
                    }
                }
            })
        }
    }

    override fun next() {
        if (playlist.hasNextVideo) {
            playlist = playlist.next()
            updateCurrentVideo()
            playWhenReady()
        }
    }<commit_msg>Remove the hd action in playback<commit_after>package blue.aodev.animeultimetv.presentation.screen.playback

import android.app.Activity
import android.net.Uri
import android.support.v17.leanback.media.PlaybackGlue
import android.support.v17.leanback.media.PlaybackTransportControlGlue
import android.support.v17.leanback.widget.ArrayObjectAdapter
import android.support.v17.leanback.widget.PlaybackControlsRow
import blue.aodev.animeultimetv.domain.model.Playlist


class PlaylistMediaPlayerGlue(context: Activity, impl: ExoPlayerAdapter, var playlist: Playlist)
    : PlaybackTransportControlGlue<ExoPlayerAdapter>(context, impl) {

    private val previousAction = PlaybackControlsRow.SkipPreviousAction(context)
    private val nextAction = PlaybackControlsRow.SkipNextAction(context)

    init {
        updateCurrentVideo()
        playWhenReady()
    }

    private fun updateCurrentVideo() {
        val currentVideo = playlist.currentVideo
        title = playlist.title
        subtitle = currentVideo.title
        playerAdapter.setDataSource(Uri.parse(currentVideo.hdVideoUrl))
        NetworkPlaybackSeekDataProvider.setDemoSeekProvider(this, currentVideo.hdVideoUrl)
    }

    override fun onCreatePrimaryActions(adapter: ArrayObjectAdapter) {
        super.onCreatePrimaryActions(adapter)
        adapter.add(previousAction)
        adapter.add(nextAction)
    }

    fun playWhenReady() {
        if (isPrepared) {
            play()
        } else {
            addPlayerCallback(object : PlayerCallback() {
                override fun onPreparedStateChanged(glue: PlaybackGlue) {
                    if (glue.isPrepared) {
                        glue.removePlayerCallback(this)
                        glue.play()
                    }
                }
            })
        }
    }

    override fun next() {
        if (playlist.hasNextVideo) {
            playlist = playlist.next()
            updateCurrentVideo()
            playWhenReady()
        }
    }
{'fraction_non_alphanumeric': 0.07840751043115438, 'fraction_numerical': 0.003998609179415856, 'mean_word_length': 2.661998726925525, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3878247', 'n_tokens_mistral': 1536, 'n_tokens_neox': 1480, 'n_words': 258}