aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/views/FrostVideoView.kt
blob: 4e3aa097b9d99389e412f52d743cf40f7310f6bf (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
package com.pitchedapps.frost.views

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.PointF
import android.graphics.RectF
import android.util.AttributeSet
import android.view.GestureDetector
import android.view.MotionEvent
import android.view.View
import android.widget.Toast
import ca.allanwang.kau.ui.ProgressAnimator
import ca.allanwang.kau.utils.AnimHolder
import ca.allanwang.kau.utils.dpToPx
import ca.allanwang.kau.utils.scaleXY
import ca.allanwang.kau.utils.toast
import com.devbrackets.android.exomedia.ui.widget.VideoView
import com.pitchedapps.frost.R
import com.pitchedapps.frost.utils.L

/**
 * Created by Allan Wang on 2017-10-13.
 *
 * VideoView with scalability
 * Parent must have layout with both height & width as match_parent
 */
class FrostVideoView @JvmOverloads constructor(
        context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : VideoView(context, attrs, defStyleAttr) {

    /**
     * Shortcut for actual video view
     */
    private inline val v
        get() = videoViewImpl

    var onFinishedListener: () -> Unit = {}
    private lateinit var viewerContract: FrostVideoViewerContract
    lateinit var containerContract: FrostVideoContainerContract
    var repeat: Boolean = false

    private val videoDimensions = PointF(0f, 0f)

    companion object {

        /**
         * Padding between minimized video and the parent borders
         * Note that this is double the actual padding
         * as we are calculating then dividing by 2
         */
        private val MINIMIZED_PADDING = 10.dpToPx
        private val SWIPE_TO_CLOSE_HORIZONTAL_THRESHOLD = 2f.dpToPx
        private val SWIPE_TO_CLOSE_VERTICAL_THRESHOLD = 5f.dpToPx
        private val SWIPE_TO_CLOSE_OFFSET_THRESHOLD = 75f.dpToPx
        const val ANIMATION_DURATION = 200L
        private const val FAST_ANIMATION_DURATION = 100L
    }

    private var videoBounds = RectF()

    var isExpanded: Boolean = true
        set(value) {
            if (field == value) return
            field = value
            val origX = translationX
            val origY = translationY
            val origScale = scaleX
            if (field) {
                ProgressAnimator.ofFloat {
                    duration = ANIMATION_DURATION
                    interpolator = AnimHolder.fastOutSlowInInterpolator(context)
                    withAnimator { viewerContract.onExpand(it) }
                    withAnimator(origScale, 1f) { scaleXY = it }
                    withAnimator(origX, 0f) { translationX = it }
                    withAnimator(origY, 0f) { translationY = it }
                    withEndAction {
                        if (!isPlaying) showControls()
                        else viewerContract.onControlsHidden()
                    }
                }
            } else {
                hideControls()
                val (scale, tX, tY) = mapBounds()
                ProgressAnimator.ofFloat {
                    duration = ANIMATION_DURATION
                    interpolator = AnimHolder.fastOutSlowInInterpolator(context)
                    withAnimator { viewerContract.onExpand(1f - it) }
                    withAnimator(origScale, scale) { scaleXY = it }
                    withAnimator(origX, tX) { translationX = it }
                    withAnimator(origY, tY) { translationY = it }
                }
            }
        }

    /**
     * Store the boundaries of the minimized video,
     * and return the necessary transitions to get there
     */
    private fun mapBounds(): Triple<Float, Float, Float> {
        if (videoDimensions.x <= 0f || videoDimensions.y <= 0f) {
            L.d("Attempted to toggle video expansion when points have not been finalized")
            val dimen = Math.min(height, width).toFloat()
            videoDimensions.set(dimen, dimen)
        }
        val portrait = height > width
        val scale = Math.min(height / (if (portrait) 4f else 2.3f) / videoDimensions.y, width / (if (portrait) 2.3f else 4f) / videoDimensions.x)
        val desiredHeight = scale * videoDimensions.y
        val desiredWidth = scale * videoDimensions.x
        val padding = containerContract.lowerVideoPadding
        val offsetX = width - MINIMIZED_PADDING - desiredWidth
        val offsetY = height - MINIMIZED_PADDING - desiredHeight
        val tX = offsetX / 2 - padding.x
        val tY = offsetY / 2 - padding.y
        videoBounds.set(offsetX, offsetY, width.toFloat(), height.toFloat())
        videoBounds.offset(padding.x, padding.y)
        L.v("Video bounds: fullwidth $width, fullheight $height, scale $scale, tX $tX, tY $tY")
        return Triple(scale, tX, tY)
    }

    fun updateLocation() {
        L.d("Update video location")
        val (scale, tX, tY) = if (isExpanded) Triple(1f, 0f, 0f) else mapBounds()
        scaleXY = scale
        translationX = tX
        translationY = tY
    }

    init {
        setOnPreparedListener {
            start()
            if (isExpanded) showControls()
        }
        setOnErrorListener {
            toast(R.string.video_load_failed, Toast.LENGTH_SHORT)
            destroy()
            true
        }
        setOnCompletionListener {
            if (repeat) restart()
            else viewerContract.onVideoComplete()
        }
        setOnTouchListener(FrameTouchListener(context))
        v.setOnTouchListener(VideoTouchListener(context))
        setOnVideoSizedChangedListener { intrinsicWidth, intrinsicHeight ->
            val ratio = Math.min(width.toFloat() / intrinsicWidth, height.toFloat() / intrinsicHeight.toFloat())
            /**
             * Only remap if not expanded and if dimensions have changed
             */
            val shouldRemap = !isExpanded
                    && (videoDimensions.x != ratio * intrinsicWidth || videoDimensions.y != ratio * intrinsicHeight)
            videoDimensions.set(ratio * intrinsicWidth, ratio * intrinsicHeight)
            if (shouldRemap) updateLocation()
        }
    }

    fun setViewerContract(contract: FrostVideoViewerContract) {
        this.viewerContract = contract
        videoControls?.setVisibilityListener(viewerContract)
    }

    fun jumpToStart() {
        pause()
        v.seekTo(0)
        videoControls?.finishLoading()
    }

    override fun pause() {
        audioFocusHelper.abandonFocus()
        videoViewImpl.pause()
        keepScreenOn = false
        if (isExpanded)
            videoControls?.updatePlaybackState(false)
    }

    override fun restart(): Boolean {
        videoUri ?: return false
        if (videoViewImpl.restart() && isExpanded && !repeat) {
            videoControls?.showLoading(true)
            return true
        }
        return false
    }

    private fun hideControls() {
        if (videoControls?.isVisible == true)
            videoControls?.hide()
    }

    private fun toggleControls() {
        if (videoControls?.isVisible == true)
            hideControls()
        else
            showControls()
    }

    fun shouldParentAcceptTouch(ev: MotionEvent): Boolean {
        if (isExpanded) return true
        return !videoBounds.contains(ev.x, ev.y)
    }

    fun destroy() {
        stopPlayback()
        if (alpha > 0f)
            ProgressAnimator.ofFloat(alpha, 0f) {
                duration = FAST_ANIMATION_DURATION
                withAnimator { alpha = it }
                withEndAction { onFinishedListener() }
            }
        else
            onFinishedListener()
    }

    private fun onHorizontalSwipe(offset: Float) {
        val alpha = Math.max((1f - Math.abs(offset / SWIPE_TO_CLOSE_OFFSET_THRESHOLD)) * 0.5f + 0.5f, 0f)
        this.alpha = alpha
    }

    /*
     * -------------------------------------------------------------------
     * Touch Listeners
     * -------------------------------------------------------------------
     */

    private inner class FrameTouchListener(context: Context) : GestureDetector.SimpleOnGestureListener(), View.OnTouchListener {

        private val gestureDetector: GestureDetector = GestureDetector(context, this)

        @SuppressLint("ClickableViewAccessibility")
        override fun onTouch(view: View, event: MotionEvent): Boolean {
            if (!isExpanded) return false
            gestureDetector.onTouchEvent(event)
            return true
        }

        override fun onSingleTapConfirmed(event: MotionEvent): Boolean {
            if (!viewerContract.onSingleTapConfirmed(event))
                toggleControls()
            return true
        }

        override fun onDoubleTap(e: MotionEvent): Boolean {
            isExpanded = !isExpanded
            return true
        }
    }

    /**
     * Monitors the view click events to show and hide the video controls if they have been specified.
     */
    private inner class VideoTouchListener(context: Context) : GestureDetector.SimpleOnGestureListener(), View.OnTouchListener {

        private val gestureDetector: GestureDetector = GestureDetector(context, this)
        private val downLoc = PointF()
        private var baseSwipeX = -1f
        private var baseTranslateX = -1f
        private var checkForDismiss = true
        private var onSwipe = false

        @SuppressLint("ClickableViewAccessibility")
        override fun onTouch(view: View, event: MotionEvent): Boolean {
            gestureDetector.onTouchEvent(event)
            when (event.actionMasked) {
                MotionEvent.ACTION_DOWN -> {
                    checkForDismiss = !isExpanded
                    onSwipe = false
                    downLoc.x = event.rawX
                    downLoc.y = event.rawY
                }
                MotionEvent.ACTION_MOVE -> {
                    if (onSwipe) {
                        val dx = baseSwipeX - event.rawX
                        translationX = baseTranslateX - dx
                        onHorizontalSwipe(dx)
                    } else if (checkForDismiss) {
                        if (Math.abs(event.rawY - downLoc.y) > SWIPE_TO_CLOSE_VERTICAL_THRESHOLD)
                            checkForDismiss = false
                        else if (Math.abs(event.rawX - downLoc.x) > SWIPE_TO_CLOSE_HORIZONTAL_THRESHOLD) {
                            onSwipe = true
                            baseSwipeX = event.rawX
                            baseTranslateX = translationX
                        }
                    }
                }
                MotionEvent.ACTION_UP -> {
                    if (onSwipe) {
                        if (Math.abs(baseSwipeX - event.rawX) > SWIPE_TO_CLOSE_OFFSET_THRESHOLD)
                            destroy()
                        else
                            animate().translationX(baseTranslateX).setDuration(FAST_ANIMATION_DURATION).withStartAction {
                                animate().alpha(1f)
                            }
                    }
                }
            }
            return true
        }

        override fun onSingleTapConfirmed(event: MotionEvent): Boolean {
            if (viewerContract.onSingleTapConfirmed(event)) return true
            if (!isExpanded) {
                isExpanded = true
                return true
            }
            toggleControls()
            return true
        }

        override fun onDoubleTap(e: MotionEvent): Boolean {
            isExpanded = !isExpanded
            return true
        }
    }
}