1 /*
2  * Copyright 2019 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.car.media.common;
18 
19 import android.content.Context;
20 import android.content.res.ColorStateList;
21 import android.view.View;
22 import android.widget.ProgressBar;
23 
24 import androidx.annotation.NonNull;
25 import androidx.annotation.Nullable;
26 import androidx.lifecycle.LifecycleOwner;
27 
28 import com.android.car.media.common.playback.PlaybackViewModel;
29 
30 /**
31  * Helper class for {@link PlaybackControlsActionBar} and {@link MinimizedPlaybackControlBar}.
32  */
33 public class ControlBarHelper {
34 
35     /**
36      * Initializes progress bar, i.e., sets progress tint and progress update listener.
37      */
initProgressBar(@onNull Context context, @NonNull LifecycleOwner owner, @NonNull PlaybackViewModel model, @Nullable ProgressBar progressBar, boolean showProgressBar)38     public static void initProgressBar(@NonNull Context context, @NonNull LifecycleOwner owner,
39             @NonNull PlaybackViewModel model, @Nullable ProgressBar progressBar,
40             boolean showProgressBar) {
41         if (progressBar == null) {
42             return;
43         }
44         if (!showProgressBar) {
45             progressBar.setVisibility(View.GONE);
46             return;
47         }
48         boolean useMediaSourceColor =
49                 context.getResources().getBoolean(
50                         R.bool.use_media_source_color_for_minimized_progress_bar);
51         int defaultColor = context.getResources().getColor(R.color.minimized_progress_bar_highlight,
52                 null);
53         if (useMediaSourceColor) {
54             model.getMediaSourceColors().observe(owner,
55                     sourceColors -> {
56                         int color = sourceColors != null ? sourceColors.getAccentColor(
57                                 defaultColor)
58                                 : defaultColor;
59                         progressBar.setProgressTintList(ColorStateList.valueOf(color));
60                     });
61         } else {
62             progressBar.setProgressTintList(ColorStateList.valueOf(defaultColor));
63         }
64 
65         model.getProgress().observe(owner,
66                 progress -> {
67                     progressBar.setProgress((int) progress.getProgress());
68                     progressBar.setMax((int) progress.getMaxProgress());
69                 });
70     }
71 }
72