summaryrefslogtreecommitdiff
path: root/packages/SystemUI/src/com/android/systemui/media/PlayerViewHolder.kt
blob: 600fdc27ef89bd71947eeb4e727041cf55a26675 (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
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.media

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageButton
import android.widget.ImageView
import android.widget.SeekBar
import android.widget.TextView
import com.android.systemui.R
import com.android.systemui.util.animation.TransitionLayout

/**
 * ViewHolder for a media player.
 */
class PlayerViewHolder private constructor(itemView: View) {

    val player = itemView as TransitionLayout

    // Player information
    val appIcon = itemView.requireViewById<ImageView>(R.id.icon)
    val appName = itemView.requireViewById<TextView>(R.id.app_name)
    val albumView = itemView.requireViewById<ImageView>(R.id.album_art)
    val titleText = itemView.requireViewById<TextView>(R.id.header_title)
    val artistText = itemView.requireViewById<TextView>(R.id.header_artist)

    // Output switcher
    val seamless = itemView.requireViewById<ViewGroup>(R.id.media_seamless)
    val seamlessIcon = itemView.requireViewById<ImageView>(R.id.media_seamless_image)
    val seamlessText = itemView.requireViewById<TextView>(R.id.media_seamless_text)
    val seamlessFallback = itemView.requireViewById<ImageView>(R.id.media_seamless_fallback)

    // Seek bar
    val seekBar = itemView.requireViewById<SeekBar>(R.id.media_progress_bar)
    val progressTimes = itemView.requireViewById<ViewGroup>(R.id.notification_media_progress_time)
    val elapsedTimeView = itemView.requireViewById<TextView>(R.id.media_elapsed_time)
    val totalTimeView = itemView.requireViewById<TextView>(R.id.media_total_time)

    // Action Buttons
    val action0 = itemView.requireViewById<ImageButton>(R.id.action0)
    val action1 = itemView.requireViewById<ImageButton>(R.id.action1)
    val action2 = itemView.requireViewById<ImageButton>(R.id.action2)
    val action3 = itemView.requireViewById<ImageButton>(R.id.action3)
    val action4 = itemView.requireViewById<ImageButton>(R.id.action4)

    init {
        (player.background as IlluminationDrawable).let {
            it.registerLightSource(seamless)
            it.registerLightSource(action0)
            it.registerLightSource(action1)
            it.registerLightSource(action2)
            it.registerLightSource(action3)
            it.registerLightSource(action4)
        }
    }

    fun getAction(id: Int): ImageButton {
        return when (id) {
            R.id.action0 -> action0
            R.id.action1 -> action1
            R.id.action2 -> action2
            R.id.action3 -> action3
            R.id.action4 -> action4
            else -> {
                throw IllegalArgumentException()
            }
        }
    }

    // Settings screen
    val options = itemView.requireViewById<View>(R.id.qs_media_controls_options)

    companion object {
        /**
         * Creates a PlayerViewHolder.
         *
         * @param inflater LayoutInflater to use to inflate the layout.
         * @param parent Parent of inflated view.
         */
        @JvmStatic fun create(inflater: LayoutInflater, parent: ViewGroup): PlayerViewHolder {
            val mediaView = inflater.inflate(R.layout.media_view, parent, false)
            // Because this media view (a TransitionLayout) is used to measure and layout the views
            // in various states before being attached to its parent, we can't depend on the default
            // LAYOUT_DIRECTION_INHERIT to correctly resolve the ltr direction.
            mediaView.layoutDirection = View.LAYOUT_DIRECTION_LOCALE
            return PlayerViewHolder(mediaView).apply {
                // Media playback is in the direction of tape, not time, so it stays LTR
                seekBar.layoutDirection = View.LAYOUT_DIRECTION_LTR
                progressTimes.layoutDirection = View.LAYOUT_DIRECTION_LTR
            }
        }
    }
}