Robert Snoeberger | 7dffd37 | 2020-04-01 17:32:44 -0400 | [diff] [blame] | 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 |
| 18 | |
Robert Snoeberger | 7dffd37 | 2020-04-01 17:32:44 -0400 | [diff] [blame] | 19 | import android.text.format.DateUtils |
Robert Snoeberger | 7dffd37 | 2020-04-01 17:32:44 -0400 | [diff] [blame] | 20 | import androidx.annotation.UiThread |
| 21 | import androidx.lifecycle.Observer |
Fabian Kozynski | 8a9516d | 2020-06-19 17:26:54 -0400 | [diff] [blame] | 22 | import com.android.systemui.R |
Robert Snoeberger | 7dffd37 | 2020-04-01 17:32:44 -0400 | [diff] [blame] | 23 | |
Robert Snoeberger | 7dffd37 | 2020-04-01 17:32:44 -0400 | [diff] [blame] | 24 | /** |
| 25 | * Observer for changes from SeekBarViewModel. |
| 26 | * |
| 27 | * <p>Updates the seek bar views in response to changes to the model. |
| 28 | */ |
Robert Snoeberger | 45be3fb | 2020-05-19 02:00:43 -0400 | [diff] [blame] | 29 | class SeekBarObserver(private val holder: PlayerViewHolder) : Observer<SeekBarViewModel.Progress> { |
Robert Snoeberger | 7dffd37 | 2020-04-01 17:32:44 -0400 | [diff] [blame] | 30 | |
Fabian Kozynski | 8a9516d | 2020-06-19 17:26:54 -0400 | [diff] [blame] | 31 | val seekBarDefaultMaxHeight = holder.seekBar.context.resources |
| 32 | .getDimensionPixelSize(R.dimen.qs_media_enabled_seekbar_height) |
| 33 | val seekBarDisabledHeight = holder.seekBar.context.resources |
| 34 | .getDimensionPixelSize(R.dimen.qs_media_disabled_seekbar_height) |
| 35 | |
Robert Snoeberger | 7dffd37 | 2020-04-01 17:32:44 -0400 | [diff] [blame] | 36 | /** Updates seek bar views when the data model changes. */ |
| 37 | @UiThread |
| 38 | override fun onChanged(data: SeekBarViewModel.Progress) { |
Robert Snoeberger | a9b8d7f | 2020-04-07 17:44:51 -0400 | [diff] [blame] | 39 | if (!data.enabled) { |
Fabian Kozynski | 1b9ca1e | 2020-06-22 21:10:39 -0400 | [diff] [blame] | 40 | if (holder.seekBar.maxHeight != seekBarDisabledHeight) { |
Fabian Kozynski | 8a9516d | 2020-06-19 17:26:54 -0400 | [diff] [blame] | 41 | holder.seekBar.maxHeight = seekBarDisabledHeight |
| 42 | } |
Fabian Kozynski | 1b9ca1e | 2020-06-22 21:10:39 -0400 | [diff] [blame] | 43 | holder.seekBar.setEnabled(false) |
Robert Snoeberger | 45be3fb | 2020-05-19 02:00:43 -0400 | [diff] [blame] | 44 | holder.seekBar.getThumb().setAlpha(0) |
| 45 | holder.seekBar.setProgress(0) |
| 46 | holder.elapsedTimeView.setText("") |
| 47 | holder.totalTimeView.setText("") |
Robert Snoeberger | a9b8d7f | 2020-04-07 17:44:51 -0400 | [diff] [blame] | 48 | return |
| 49 | } |
| 50 | |
Robert Snoeberger | 45be3fb | 2020-05-19 02:00:43 -0400 | [diff] [blame] | 51 | holder.seekBar.getThumb().setAlpha(if (data.seekAvailable) 255 else 0) |
| 52 | holder.seekBar.setEnabled(data.seekAvailable) |
Robert Snoeberger | a9b8d7f | 2020-04-07 17:44:51 -0400 | [diff] [blame] | 53 | |
Fabian Kozynski | 1b9ca1e | 2020-06-22 21:10:39 -0400 | [diff] [blame] | 54 | if (holder.seekBar.maxHeight != seekBarDefaultMaxHeight) { |
| 55 | holder.seekBar.maxHeight = seekBarDefaultMaxHeight |
Fabian Kozynski | 8a9516d | 2020-06-19 17:26:54 -0400 | [diff] [blame] | 56 | } |
| 57 | |
Robert Snoeberger | 7dffd37 | 2020-04-01 17:32:44 -0400 | [diff] [blame] | 58 | data.elapsedTime?.let { |
Robert Snoeberger | 45be3fb | 2020-05-19 02:00:43 -0400 | [diff] [blame] | 59 | holder.seekBar.setProgress(it) |
| 60 | holder.elapsedTimeView.setText(DateUtils.formatElapsedTime( |
Robert Snoeberger | 7dffd37 | 2020-04-01 17:32:44 -0400 | [diff] [blame] | 61 | it / DateUtils.SECOND_IN_MILLIS)) |
| 62 | } |
| 63 | |
| 64 | data.duration?.let { |
Robert Snoeberger | 45be3fb | 2020-05-19 02:00:43 -0400 | [diff] [blame] | 65 | holder.seekBar.setMax(it) |
| 66 | holder.totalTimeView.setText(DateUtils.formatElapsedTime( |
Robert Snoeberger | 7dffd37 | 2020-04-01 17:32:44 -0400 | [diff] [blame] | 67 | it / DateUtils.SECOND_IN_MILLIS)) |
| 68 | } |
| 69 | } |
| 70 | } |