• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.systemui.media.controls.ui.binder
18 
19 import android.animation.Animator
20 import android.animation.ObjectAnimator
21 import android.text.format.DateUtils
22 import androidx.annotation.UiThread
23 import androidx.lifecycle.Observer
24 import com.android.app.animation.Interpolators
25 import com.android.app.tracing.TraceStateLogger
26 import com.android.internal.annotations.VisibleForTesting
27 import com.android.systemui.media.controls.ui.drawable.SquigglyProgress
28 import com.android.systemui.media.controls.ui.view.MediaViewHolder
29 import com.android.systemui.media.controls.ui.viewmodel.SeekBarViewModel
30 import com.android.systemui.res.R
31 
32 private const val TAG = "SeekBarObserver"
33 
34 /**
35  * Observer for changes from SeekBarViewModel.
36  *
37  * <p>Updates the seek bar views in response to changes to the model.
38  */
39 open class SeekBarObserver(private val holder: MediaViewHolder) :
40     Observer<SeekBarViewModel.Progress> {
41 
42     companion object {
43         @JvmStatic val RESET_ANIMATION_DURATION_MS: Int = 750
44         @JvmStatic val RESET_ANIMATION_THRESHOLD_MS: Int = 250
45     }
46 
47     // Trace state loggers for playing and listening states of progress bar.
48     private val playingStateLogger = TraceStateLogger("$TAG#playing")
49     private val listeningStateLogger = TraceStateLogger("$TAG#listening")
50 
51     val seekBarEnabledMaxHeight =
52         holder.seekBar.context.resources.getDimensionPixelSize(
53             R.dimen.qs_media_enabled_seekbar_height
54         )
55     val seekBarDisabledHeight =
56         holder.seekBar.context.resources.getDimensionPixelSize(
57             R.dimen.qs_media_disabled_seekbar_height
58         )
59     val seekBarEnabledVerticalPadding =
60         holder.seekBar.context.resources.getDimensionPixelSize(
61             R.dimen.qs_media_session_enabled_seekbar_vertical_padding
62         )
63     val seekBarDisabledVerticalPadding =
64         holder.seekBar.context.resources.getDimensionPixelSize(
65             R.dimen.qs_media_session_disabled_seekbar_vertical_padding
66         )
67     var seekBarResetAnimator: Animator? = null
68     var animationEnabled: Boolean = true
69 
70     init {
71         val seekBarProgressWavelength =
72             holder.seekBar.context.resources
73                 .getDimensionPixelSize(R.dimen.qs_media_seekbar_progress_wavelength)
74                 .toFloat()
75         val seekBarProgressAmplitude =
76             holder.seekBar.context.resources
77                 .getDimensionPixelSize(R.dimen.qs_media_seekbar_progress_amplitude)
78                 .toFloat()
79         val seekBarProgressPhase =
80             holder.seekBar.context.resources
81                 .getDimensionPixelSize(R.dimen.qs_media_seekbar_progress_phase)
82                 .toFloat()
83         val seekBarProgressStrokeWidth =
84             holder.seekBar.context.resources
85                 .getDimensionPixelSize(R.dimen.qs_media_seekbar_progress_stroke_width)
86                 .toFloat()
87         val progressDrawable = holder.seekBar.progressDrawable as? SquigglyProgress
<lambda>null88         progressDrawable?.let {
89             it.waveLength = seekBarProgressWavelength
90             it.lineAmplitude = seekBarProgressAmplitude
91             it.phaseSpeed = seekBarProgressPhase
92             it.strokeWidth = seekBarProgressStrokeWidth
93         }
94     }
95 
96     /** Updates seek bar views when the data model changes. */
97     @UiThread
onChangednull98     override fun onChanged(data: SeekBarViewModel.Progress) {
99         val progressDrawable = holder.seekBar.progressDrawable as? SquigglyProgress
100         if (!data.enabled) {
101             if (holder.seekBar.maxHeight != seekBarDisabledHeight) {
102                 holder.seekBar.maxHeight = seekBarDisabledHeight
103                 setVerticalPadding(seekBarDisabledVerticalPadding)
104             }
105             holder.seekBar.isEnabled = false
106             progressDrawable?.animate = false
107             holder.seekBar.thumb.alpha = 0
108             holder.seekBar.progress = 0
109             holder.seekBar.contentDescription = ""
110             holder.scrubbingElapsedTimeView.text = ""
111             holder.scrubbingTotalTimeView.text = ""
112             return
113         }
114 
115         playingStateLogger.log("${data.playing}")
116         listeningStateLogger.log("${data.listening}")
117 
118         holder.seekBar.thumb.alpha = if (data.seekAvailable) 255 else 0
119         holder.seekBar.isEnabled = data.seekAvailable
120         progressDrawable?.animate =
121             data.playing && !data.scrubbing && animationEnabled && data.listening
122         progressDrawable?.transitionEnabled = !data.seekAvailable
123 
124         if (holder.seekBar.maxHeight != seekBarEnabledMaxHeight) {
125             holder.seekBar.maxHeight = seekBarEnabledMaxHeight
126             setVerticalPadding(seekBarEnabledVerticalPadding)
127         }
128 
129         holder.seekBar.setMax(data.duration)
130         if (data.scrubbing) {
131             holder.scrubbingTotalTimeView.text = formatTimeLabel(data.duration)
132         }
133 
134         data.elapsedTime?.let {
135             if (!data.scrubbing && !(seekBarResetAnimator?.isRunning ?: false)) {
136                 if (
137                     it <= RESET_ANIMATION_THRESHOLD_MS &&
138                         holder.seekBar.progress > RESET_ANIMATION_THRESHOLD_MS
139                 ) {
140                     // This animation resets for every additional update to zero.
141                     val animator = buildResetAnimator(it)
142                     animator.start()
143                     seekBarResetAnimator = animator
144                 } else {
145                     holder.seekBar.progress = it
146                 }
147             }
148 
149             if (data.scrubbing) {
150                 holder.scrubbingElapsedTimeView.text = formatTimeLabel(it)
151             }
152         }
153     }
154 
155     /** Returns a time string suitable for display, e.g. "12:34" */
formatTimeLabelnull156     private fun formatTimeLabel(milliseconds: Int): CharSequence {
157         return DateUtils.formatElapsedTime(milliseconds / DateUtils.SECOND_IN_MILLIS)
158     }
159 
160     @UiThread
updateContentDescriptionnull161     fun updateContentDescription(
162         elapsedTimeDescription: CharSequence,
163         durationDescription: CharSequence,
164     ) {
165         holder.seekBar.contentDescription =
166             holder.seekBar.context.getString(
167                 R.string.controls_media_seekbar_description,
168                 elapsedTimeDescription,
169                 durationDescription,
170             )
171     }
172 
173     @VisibleForTesting
buildResetAnimatornull174     open fun buildResetAnimator(targetTime: Int): Animator {
175         val animator =
176             ObjectAnimator.ofInt(
177                 holder.seekBar,
178                 "progress",
179                 holder.seekBar.progress,
180                 targetTime + RESET_ANIMATION_DURATION_MS,
181             )
182         animator.setAutoCancel(true)
183         animator.duration = RESET_ANIMATION_DURATION_MS.toLong()
184         animator.interpolator = Interpolators.EMPHASIZED
185         return animator
186     }
187 
188     @UiThread
setVerticalPaddingnull189     fun setVerticalPadding(padding: Int) {
190         val leftPadding = holder.seekBar.paddingLeft
191         val rightPadding = holder.seekBar.paddingRight
192         val bottomPadding = holder.seekBar.paddingBottom
193         holder.seekBar.setPadding(leftPadding, padding, rightPadding, bottomPadding)
194     }
195 }
196