AudioPlayer.kt 26.5 KB
Newer Older
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
/*****************************************************************************
 * AudioPlayer.java
 *
 * Copyright © 2011-2014 VLC authors and VideoLAN
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 */

package org.videolan.vlc.gui.audio

import android.Manifest
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.media.AudioManager
import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.preference.PreferenceManager
import android.support.annotation.MainThread
import android.support.annotation.RequiresPermission
import android.support.design.widget.BottomSheetBehavior
import android.support.design.widget.Snackbar
import android.support.v4.app.FragmentActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.PopupMenu
import android.support.v7.widget.helper.ItemTouchHelper
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import kotlinx.coroutines.experimental.CoroutineStart
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.async
import kotlinx.coroutines.experimental.channels.Channel
import kotlinx.coroutines.experimental.channels.actor
import kotlinx.coroutines.experimental.launch
import org.videolan.libvlc.Media
import org.videolan.libvlc.MediaPlayer
import org.videolan.libvlc.util.AndroidUtil
import org.videolan.medialibrary.Tools
import org.videolan.medialibrary.media.MediaWrapper
import org.videolan.vlc.PlaybackService
import org.videolan.vlc.R
import org.videolan.vlc.VLCApplication
import org.videolan.vlc.databinding.AudioPlayerBinding
import org.videolan.vlc.gui.AudioPlayerContainerActivity
import org.videolan.vlc.gui.PlaybackServiceFragment
import org.videolan.vlc.gui.dialogs.AdvOptionsDialog
import org.videolan.vlc.gui.helpers.AudioUtil
import org.videolan.vlc.gui.helpers.SwipeDragItemTouchHelperCallback
import org.videolan.vlc.gui.helpers.UiTools
import org.videolan.vlc.gui.preferences.PreferencesActivity
import org.videolan.vlc.gui.view.AudioMediaSwitcher.AudioMediaSwitcherListener
import org.videolan.vlc.util.AndroidDevices
import org.videolan.vlc.util.Constants

class AudioPlayer : PlaybackServiceFragment(), PlaybackService.Callback, PlaylistAdapter.IPlayer, TextWatcher {

    private lateinit var mBinding: AudioPlayerBinding
    private lateinit var mPlaylistAdapter: PlaylistAdapter
    private lateinit var mSettings: SharedPreferences
    private val mHandler by lazy(LazyThreadSafetyMode.NONE) { Handler() }
    private val updateActor = actor<Unit>(UI, capacity = Channel.CONFLATED) { for (entry in channel) doUpdate() }

    private var mShowRemainingTime = false
    private var mPreviewingSeek = false
    private var mAdvFuncVisible: Boolean = false
    private var mPlaylistSwitchVisible: Boolean = false
    private var mSearchVisible: Boolean = false
    private var mHeaderPlayPauseVisible: Boolean = false
    private var mProgressBarVisible: Boolean = false
    private var mHeaderTimeVisible: Boolean = false
    private var mPlayerState: Int = 0
    private var mCurrentCoverArt: String? = null

    companion object {
        val TAG = "VLC/AudioPlayer"

        private var DEFAULT_BACKGROUND_DARKER_ID: Int = 0
        private var DEFAULT_BACKGROUND_ID: Int = 0
        private val SEARCH_TIMEOUT_MILLIS = 5000
        /**
         * Show the audio player from an intent
         *
         * @param context The context of the activity
         */
        fun start(context: Context) {
            context.applicationContext.sendBroadcast(Intent(Constants.ACTION_SHOW_PLAYER))
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        savedInstanceState?.let { mPlayerState = it.getInt("player_state")}
        mPlaylistAdapter = PlaylistAdapter(this)
        mSettings = PreferenceManager.getDefaultSharedPreferences(activity)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        mBinding = AudioPlayerBinding.inflate(inflater)
        return mBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        if (AndroidUtil.isJellyBeanMR1OrLater) {
            DEFAULT_BACKGROUND_DARKER_ID = UiTools.getResourceFromAttribute(view.context, R.attr.background_default_darker)
            DEFAULT_BACKGROUND_ID = UiTools.getResourceFromAttribute(view.context, R.attr.background_default)
        }
        mBinding.songsList.layoutManager = LinearLayoutManager(view.context)
        mBinding.songsList.adapter = mPlaylistAdapter
        mBinding.audioMediaSwitcher.setAudioMediaSwitcherListener(mHeaderMediaSwitcherListener)
        mBinding.coverMediaSwitcher.setAudioMediaSwitcherListener(mCoverMediaSwitcherListener)
        mBinding.playlistSearchText.editText?.addTextChangedListener(this)

        val callback = SwipeDragItemTouchHelperCallback(mPlaylistAdapter)
        val touchHelper = ItemTouchHelper(callback)
        touchHelper.attachToRecyclerView(mBinding.songsList)

        setHeaderVisibilities(false, false, true, true, true, false)
        mBinding.fragment = this

        mBinding.next.setOnTouchListener(LongSeekListener(true,
                UiTools.getResourceFromAttribute(view.context, R.attr.ic_next),
                R.drawable.ic_next_pressed))
        mBinding.previous.setOnTouchListener(LongSeekListener(false,
                UiTools.getResourceFromAttribute(view.context, R.attr.ic_previous),
                R.drawable.ic_previous_pressed))

        registerForContextMenu(mBinding.songsList)
        activity.volumeControlStream = AudioManager.STREAM_MUSIC
        userVisibleHint = true
        mBinding.showCover = mSettings.getBoolean("audio_player_show_cover", false)
        mBinding.playlistSwitch.setImageResource(UiTools.getResourceFromAttribute(view.context, if (mBinding.showCover) R.attr.ic_playlist else R.attr.ic_playlist_on))
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putInt("player_state", mPlayerState)
    }

    override fun onPopupMenu(anchor: View, position: Int) {
        val activity = activity
        if (activity === null || position >= mPlaylistAdapter.itemCount) return
        val mw = mPlaylistAdapter.getItem(position)
        val popupMenu = PopupMenu(activity, anchor)
        popupMenu.menuInflater.inflate(R.menu.audio_player, popupMenu.menu)

        popupMenu.menu.setGroupVisible(R.id.phone_only, mw!!.type != MediaWrapper.TYPE_VIDEO
                && TextUtils.equals(mw.uri.scheme, "file")
                && AndroidDevices.isPhone)

        popupMenu.setOnMenuItemClickListener(PopupMenu.OnMenuItemClickListener { item ->
            if (item.itemId == R.id.audio_player_mini_remove) {
                if (mService != null) {
                    mService.remove(position)
                    return@OnMenuItemClickListener true
                }
            } else if (item.itemId == R.id.audio_player_set_song) {
                AudioUtil.setRingtone(mw, activity as FragmentActivity)
                return@OnMenuItemClickListener true
            }
            false
        })
        popupMenu.show()
    }

    override fun update() {
        if (!updateActor.isClosedForSend) updateActor.offer(Unit)
    }

    private fun doUpdate() {
        if (mService === null || activity === null) return
        if (mService.hasMedia() && !mService.isVideoPlaying) {
            //Check fragment resumed to not restore video on device turning off
            if (isVisible && mSettings.getBoolean(PreferencesActivity.VIDEO_RESTORE, false)) {
                mSettings.edit().putBoolean(PreferencesActivity.VIDEO_RESTORE, false).apply()
                mService.currentMediaWrapper.removeFlags(MediaWrapper.MEDIA_FORCE_AUDIO)
                mService.switchToVideo()
                return
            } else
                show()
        } else {
            hide()
            return
        }

        mBinding.audioMediaSwitcher.updateMedia(mService)
        mBinding.coverMediaSwitcher.updateMedia(mService)

        mBinding.playlistPlayasaudioOff.visibility = if (mService.videoTracksCount > 0) View.VISIBLE else View.GONE

        val playing = mService.isPlaying
        val imageResId = UiTools.getResourceFromAttribute(activity, if (playing) R.attr.ic_pause else R.attr.ic_play)
        val text = getString(if (playing) R.string.pause else R.string.play)
        mBinding.playPause.setImageResource(imageResId)
        mBinding.playPause.contentDescription = text
        mBinding.headerPlayPause.setImageResource(imageResId)
        mBinding.headerPlayPause.contentDescription = text
        mBinding.shuffle.setImageResource(UiTools.getResourceFromAttribute(activity, if (mService.isShuffling) R.attr.ic_shuffle_on else R.attr.ic_shuffle))
        mBinding.shuffle.contentDescription = resources.getString(if (mService.isShuffling) R.string.shuffle_on else R.string.shuffle)
        when (mService.repeatType) {
            Constants.REPEAT_NONE -> {
                mBinding.repeat.setImageResource(UiTools.getResourceFromAttribute(activity, R.attr.ic_repeat))
                mBinding.repeat.contentDescription = resources.getString(R.string.repeat)
            }
            Constants.REPEAT_ONE -> {
                mBinding.repeat.setImageResource(UiTools.getResourceFromAttribute(activity, R.attr.ic_repeat_one))
                mBinding.repeat.contentDescription = resources.getString(R.string.repeat_single)
            }
            Constants.REPEAT_ALL -> {
                mBinding.repeat.setImageResource(UiTools.getResourceFromAttribute(activity, R.attr.ic_repeat_all))
                mBinding.repeat.contentDescription = resources.getString(R.string.repeat_all)
            }
            else -> {
                mBinding.repeat.setImageResource(UiTools.getResourceFromAttribute(activity, R.attr.ic_repeat_all))
                mBinding.repeat.contentDescription = resources.getString(R.string.repeat_all)
            }
        }
        mBinding.shuffle.visibility = if (mService.canShuffle()) View.VISIBLE else View.INVISIBLE
        mBinding.timeline.setOnSeekBarChangeListener(mTimelineListner)
        updateList()
        updateBackground()
    }

    override fun updateProgress() {
        if (mService === null) return
247
248
        val time = mService.time
        val length = mService.length
249

250
251
252
253
        mBinding.headerTime.text = Tools.millisToString(time)
        mBinding.length.text = Tools.millisToString(length)
        mBinding.timeline.max = length.toInt()
        mBinding.progressBar.max = length.toInt()
254
255

        if (!mPreviewingSeek) {
256
257
258
            mBinding.time.text = Tools.millisToString((if (mShowRemainingTime) time - length else time))
            mBinding.timeline.progress = time.toInt()
            mBinding.progressBar.progress = time.toInt()
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
        }
    }

    override fun onMediaEvent(event: Media.Event) {}

    override fun onMediaPlayerEvent(event: MediaPlayer.Event) {
        when (event.type) {
            MediaPlayer.Event.Opening -> hideSearchField()
            MediaPlayer.Event.Stopped -> hide()
        }
    }

    private fun updateBackground() {
        if (AndroidUtil.isJellyBeanMR1OrLater) {
            launch(UI, CoroutineStart.UNDISPATCHED) {
                val mw = mService.currentMediaWrapper
                if (mw === null || TextUtils.equals(mCurrentCoverArt, mw.artworkMrl)) return@launch
                mCurrentCoverArt = mw.artworkMrl
                if (TextUtils.isEmpty(mw.artworkMrl)) {
                    setDefaultBackground()
                } else {
                    val blurredCover = async { UiTools.blurBitmap(AudioUtil.readCoverBitmap(Uri.decode(mw.artworkMrl), mBinding.contentLayout.width)) }.await()
                    if (blurredCover !== null) {
                        val activity = activity as? AudioPlayerContainerActivity
                        if (activity === null) return@launch
                        mBinding.backgroundView.setColorFilter(UiTools.getColorFromAttribute(activity, R.attr.audio_player_background_tint))
                        mBinding.backgroundView.setImageBitmap(blurredCover)
                        mBinding.backgroundView.visibility = View.VISIBLE
                        mBinding.songsList.setBackgroundResource(0)
                        if (mPlayerState == BottomSheetBehavior.STATE_EXPANDED) mBinding.header.setBackgroundResource(0)
                    } else setDefaultBackground()
                }
            }
        }
        if ((activity as AudioPlayerContainerActivity).isAudioPlayerExpanded)
            setHeaderVisibilities(true, true, false, false, false, true)
    }

    @MainThread
    private fun setDefaultBackground() {
        mBinding.songsList.setBackgroundResource(DEFAULT_BACKGROUND_ID)
        mBinding.header.setBackgroundResource(DEFAULT_BACKGROUND_ID)
        mBinding.backgroundView.visibility = View.INVISIBLE
    }

    override fun updateList() {
        hideSearchField()
        if (mService !== null) mPlaylistAdapter.update(mService.medias)
    }

    override fun onSelectionSet(position: Int) {
        if (mPlayerState != BottomSheetBehavior.STATE_COLLAPSED && mPlayerState != BottomSheetBehavior.STATE_HIDDEN) {
            mBinding.songsList.scrollToPosition(position)
        }
    }

    fun onTimeLabelClick(view: View) {
        mShowRemainingTime = !mShowRemainingTime
        update()
    }

    fun onPlayPauseClick(view: View) {
        if (mService === null) return
        if (mService.isPlaying) mService.pause() else mService.play()
    }

    fun onStopClick(view: View): Boolean {
        if (mService === null) return false
        mService.stop()
        return true
    }

    fun onNextClick(view: View) {
        if (mService === null) return
        if (mService.hasNext())
            mService.next()
        else
            Snackbar.make(mBinding.root, R.string.lastsong, Snackbar.LENGTH_SHORT).show()
    }

    fun onPreviousClick(view: View) {
        if (mService === null) return
        if (mService.hasPrevious() || mService.isSeekable)
            mService.previous(false)
        else
            Snackbar.make(mBinding.root, R.string.firstsong, Snackbar.LENGTH_SHORT).show()
    }

    fun onRepeatClick(view: View) {
        if (mService === null) return
        when (mService.repeatType) {
            Constants.REPEAT_NONE -> mService.repeatType = Constants.REPEAT_ALL
            Constants.REPEAT_ALL -> mService.repeatType = Constants.REPEAT_ONE
            Constants.REPEAT_ONE -> mService.repeatType = Constants.REPEAT_NONE
            else -> mService.repeatType = Constants.REPEAT_NONE
        }
        update()
    }

    fun onPlaylistSwitchClick(view: View) {
        mSettings.edit().putBoolean("audio_player_show_cover", !mBinding.showCover).apply()
        mBinding.showCover = !mBinding.showCover
        mBinding.playlistSwitch.setImageResource(UiTools.getResourceFromAttribute(view.context, if (mBinding.showCover) R.attr.ic_playlist else R.attr.ic_playlist_on))
    }

    fun onShuffleClick(view: View) {
        if (mService === null) return
        mService.shuffle()
        update()
    }

    fun onResumeToVideoClick(v: View) {
        if (mService !== null && mService.hasMedia()) {
            mService.currentMediaWrapper.removeFlags(MediaWrapper.MEDIA_FORCE_AUDIO)
            mService.switchToVideo()
        }
    }


    fun showAdvancedOptions(v: View) {
        if (!isVisible) return
        val advOptionsDialog = AdvOptionsDialog()
        advOptionsDialog.arguments = Bundle().apply { putInt(AdvOptionsDialog.MODE_KEY, AdvOptionsDialog.MODE_AUDIO) }
        advOptionsDialog.show(activity.supportFragmentManager, "fragment_adv_options")
    }

    fun show() {
        val activity = activity as? AudioPlayerContainerActivity
        if (activity !== null && activity.isAudioPlayerReady)
            activity.showAudioPlayer()
    }

    fun hide() {
        val activity = activity as? AudioPlayerContainerActivity
        activity?.hideAudioPlayer()
    }

    private fun setHeaderVisibilities(advFuncVisible: Boolean, playlistSwitchVisible: Boolean,
                                      headerPlayPauseVisible: Boolean, progressBarVisible: Boolean,
                                      headerTimeVisible: Boolean, searchVisible: Boolean) {
        mAdvFuncVisible = advFuncVisible
        mPlaylistSwitchVisible = playlistSwitchVisible
        mHeaderPlayPauseVisible = headerPlayPauseVisible
        mProgressBarVisible = progressBarVisible
        mHeaderTimeVisible = headerTimeVisible
        mSearchVisible = searchVisible
        restoreHeaderButtonVisibilities()
    }

    private fun restoreHeaderButtonVisibilities() {
        mBinding.advFunction.visibility = if (mAdvFuncVisible) View.VISIBLE else View.GONE
        mBinding.playlistSwitch.visibility = if (mPlaylistSwitchVisible) View.VISIBLE else View.GONE
        mBinding.playlistSearch.visibility = if (mSearchVisible) View.VISIBLE else View.GONE
        mBinding.headerPlayPause.visibility = if (mHeaderPlayPauseVisible) View.VISIBLE else View.GONE
        mBinding.progressBar.visibility = if (mProgressBarVisible) View.VISIBLE else View.GONE
        mBinding.headerTime.visibility = if (mHeaderTimeVisible) View.VISIBLE else View.GONE
    }

    private fun hideHeaderButtons() {
        mBinding.advFunction.visibility = View.GONE
        mBinding.playlistSwitch.visibility = View.GONE
        mBinding.playlistSearch.visibility = View.GONE
        mBinding.headerPlayPause.visibility = View.GONE
        mBinding.progressBar.visibility = View.GONE
        mBinding.headerTime.visibility = View.GONE
    }

    fun onSearchClick(v: View) {
        mBinding.playlistSearch.visibility = View.GONE
        mBinding.playlistSearchText.visibility = View.VISIBLE
        if (mBinding.playlistSearchText.editText != null)
            mBinding.playlistSearchText.editText!!.requestFocus()
        val imm = VLCApplication.getAppContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.showSoftInput(mBinding.playlistSearchText.editText, InputMethodManager.SHOW_IMPLICIT)
        mHandler.postDelayed(hideSearchRunnable, SEARCH_TIMEOUT_MILLIS.toLong())
    }

    override fun beforeTextChanged(charSequence: CharSequence, start: Int, before: Int, count: Int) {}

    fun clearSearch(): Boolean {
        mPlaylistAdapter.restoreList()
        return hideSearchField()
    }

    private fun hideSearchField(): Boolean {
        if (mBinding.playlistSearchText.visibility != View.VISIBLE)
            return false
        mBinding.playlistSearchText.editText?.apply {
            removeTextChangedListener(this@AudioPlayer)
            setText("")
            addTextChangedListener(this@AudioPlayer)
        }
        UiTools.setKeyboardVisibility(mBinding.playlistSearchText, false)
        mBinding.playlistSearch.visibility = View.VISIBLE
        mBinding.playlistSearchText.visibility = View.GONE
        return true
    }

    override fun onTextChanged(charSequence: CharSequence, start: Int, before: Int, count: Int) {
        val length = charSequence.length
        if (length > 1) {
            mPlaylistAdapter.filter.filter(charSequence)
            mHandler.removeCallbacks(hideSearchRunnable)
        } else if (length == 0) {
            mPlaylistAdapter.restoreList()
            hideSearchField()
        }
    }

    override fun afterTextChanged(editable: Editable) {}

    override fun onConnected(service: PlaybackService) {
        super.onConnected(service)
        mService.addCallback(this)
        mPlaylistAdapter.setService(service)
        update()
    }

    override fun onStop() {
        /* unregister before super.onStop() since mService is set to null from this call */
        mService?.removeCallback(this)
        super.onStop()
    }

    private inner class LongSeekListener(internal var forward: Boolean, internal var normal: Int, internal var pressed: Int) : View.OnTouchListener {
        internal var length: Long = -1L

        internal var possibleSeek: Int = 0
        internal var vibrated: Boolean = false

        @RequiresPermission(Manifest.permission.VIBRATE)
        internal var seekRunnable: Runnable = object : Runnable {
            override fun run() {
                if (!vibrated) {
                    (VLCApplication.getAppContext().getSystemService(Context.VIBRATOR_SERVICE) as android.os.Vibrator)
                            .vibrate(80)
                    vibrated = true
                }

                if (forward) {
                    if (length <= 0 || possibleSeek < length) possibleSeek += 4000
                } else {
                    if (possibleSeek > 4000) possibleSeek -= 4000
                    else if (possibleSeek <= 4000) possibleSeek = 0
                }

                mBinding.time.text = Tools.millisToString(if (mShowRemainingTime) possibleSeek - length else possibleSeek.toLong())
                mBinding.timeline.progress = possibleSeek
                mBinding.progressBar.progress = possibleSeek
                mHandler.postDelayed(this, 50)
            }
        }

        override fun onTouch(v: View, event: MotionEvent): Boolean {
            if (mService === null) return false
            when (event.action) {
                MotionEvent.ACTION_DOWN -> {
                    (if (forward) mBinding.next else mBinding.previous).setImageResource(this.pressed)
                    possibleSeek = mService.time.toInt()
                    mPreviewingSeek = true
                    vibrated = false
                    length = mService.length
                    mHandler.postDelayed(seekRunnable, 1000)
                    return true
                }

                MotionEvent.ACTION_UP -> {
                    (if (forward) mBinding.next else mBinding.previous).setImageResource(this.normal)
                    mHandler.removeCallbacks(seekRunnable)
                    mPreviewingSeek = false
                    if (event.eventTime - event.downTime < 1000) {
                        if (forward) onNextClick(v) else onPreviousClick(v)
                    } else {
                        if (forward) {
                            if (possibleSeek < mService.length)
                                mService.time = possibleSeek.toLong()
                            else
                                onNextClick(v)
                        } else {
                            if (possibleSeek > 0)
                                mService.time = possibleSeek.toLong()
                            else
                                onPreviousClick(v)
                        }
                    }
                    return true
                }
            }
            return false
        }
    }

    private fun showPlaylistTips() {
        val activity = activity as? AudioPlayerContainerActivity
        activity?.showTipViewIfNeeded(R.id.audio_playlist_tips, Constants.PREF_PLAYLIST_TIPS_SHOWN)
    }

    fun onStateChanged(newState: Int) {
        mPlayerState = newState
        when (newState) {
            BottomSheetBehavior.STATE_COLLAPSED -> {
                mBinding.header.setBackgroundResource(DEFAULT_BACKGROUND_DARKER_ID)
                setHeaderVisibilities(false, false, true, true, true, false)
            }
            BottomSheetBehavior.STATE_EXPANDED -> {
                mBinding.header.setBackgroundResource(0)
                setHeaderVisibilities(true, true, false, false, false, true)
                showPlaylistTips()
                if (mService != null) mPlaylistAdapter.currentIndex = mService.currentMediaPosition
            }
            else -> mBinding.header.setBackgroundResource(0)
        }
    }

    private var mTimelineListner: OnSeekBarChangeListener = object : OnSeekBarChangeListener {

        override fun onStopTrackingTouch(seekBar: SeekBar) {}

        override fun onStartTrackingTouch(seekBar: SeekBar) {}

        override fun onProgressChanged(sb: SeekBar, progress: Int, fromUser: Boolean) {
            if (fromUser && mService !== null) {
                mService.time = progress.toLong()
                mBinding.time.text = Tools.millisToString(if (mShowRemainingTime) progress - mService.length else progress.toLong())
                mBinding.headerTime.text = Tools.millisToString(progress.toLong())
            }
        }
    }

    private val mHeaderMediaSwitcherListener = object : AudioMediaSwitcherListener {

        override fun onMediaSwitching() {}

        override fun onMediaSwitched(position: Int) {
            if (mService === null) return
            when (position) {
                AudioMediaSwitcherListener.PREVIOUS_MEDIA -> mService.previous(true)
                AudioMediaSwitcherListener.NEXT_MEDIA ->  mService.next()
            }
        }

        override fun onTouchDown() {
            hideHeaderButtons()
        }

        override fun onTouchUp() {
            restoreHeaderButtonVisibilities()
        }

        override fun onTouchClick() {
            val activity = activity as AudioPlayerContainerActivity
            activity.slideUpOrDownAudioPlayer()
        }
    }

    private val mCoverMediaSwitcherListener = object : AudioMediaSwitcherListener {

        override fun onMediaSwitching() {}

        override fun onMediaSwitched(position: Int) {
            if (mService === null) return
            when (position) {
                AudioMediaSwitcherListener.PREVIOUS_MEDIA -> mService.previous(true)
                AudioMediaSwitcherListener.NEXT_MEDIA -> mService.next()
            }
        }

        override fun onTouchDown() {}

        override fun onTouchUp() {}

        override fun onTouchClick() {}
    }

    private val hideSearchRunnable: Runnable = Runnable {
        hideSearchField()
        mPlaylistAdapter.restoreList()
    }
}