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.util.AttributeSet;
21 import android.util.Size;
22 import android.widget.ProgressBar;
23 
24 import androidx.annotation.NonNull;
25 import androidx.lifecycle.LifecycleOwner;
26 
27 import com.android.car.apps.common.BackgroundImageView;
28 import com.android.car.apps.common.MinimizedControlBar;
29 import com.android.car.apps.common.imaging.ImageBinder;
30 import com.android.car.media.common.playback.PlaybackViewModel;
31 
32 /**
33  * This is a CarControlBar used for displaying Media content, including metadata for the currently
34  * playing song and basic controls.
35  */
36 public class MinimizedPlaybackControlBar extends MinimizedControlBar {
37 
38     private static final String TAG = "Media.ControlBar";
39 
40     private MediaButtonController mMediaButtonController;
41     private MetadataController mMetadataController;
42     private ProgressBar mLinearProgressBar;
43     private ProgressBar mCircularProgressBar;
44     private ImageBinder<MediaItemMetadata.ArtworkRef> mArtBinder = null;
45     private PlaybackViewModel mPlaybackViewModel;
46 
47     private boolean mShowLinearProgressBar;
48     private boolean mShowCircularProgressBar;
49 
MinimizedPlaybackControlBar(Context context)50     public MinimizedPlaybackControlBar(Context context) {
51         this(context, null);
52     }
53 
MinimizedPlaybackControlBar(Context context, AttributeSet attrs)54     public MinimizedPlaybackControlBar(Context context, AttributeSet attrs) {
55         this(context, attrs, 0);
56     }
57 
MinimizedPlaybackControlBar(Context context, AttributeSet attrs, int defStyleAttrs)58     public MinimizedPlaybackControlBar(Context context, AttributeSet attrs, int defStyleAttrs) {
59         super(context, attrs, defStyleAttrs, R.layout.minimized_playback_control_bar);
60         init(context);
61     }
62 
init(Context context)63     private void init(Context context) {
64         mMediaButtonController = new MediaButtonController(context, this,
65                 R.color.playback_control_color, R.layout.play_pause_stop_button_layout,
66                 R.drawable.ic_skip_previous, R.drawable.ic_skip_next);
67 
68         mShowLinearProgressBar = context.getResources().getBoolean(R.bool.show_linear_progress_bar);
69         mLinearProgressBar = findViewById(R.id.linear_progress_bar);
70 
71         mShowCircularProgressBar = context.getResources().getBoolean(
72                 R.bool.show_circular_progress_bar);
73         mCircularProgressBar = findViewById(R.id.circular_progress_bar);
74 
75         BackgroundImageView artBackground = findViewById(R.id.art_background);
76         if (artBackground != null) {
77             int max = getResources().getInteger(R.integer.media_items_bitmap_max_size_px);
78             Size maxArtSize = new Size(max, max);
79             mArtBinder = new ImageBinder<>(
80                     ImageBinder.PlaceholderType.BACKGROUND, maxArtSize,
81                     drawable -> artBackground.setBackgroundDrawable(drawable));
82         }
83     }
84 
85     /** Connects the bar to the {@link PlaybackViewModel}. */
setModel(@onNull PlaybackViewModel model, @NonNull LifecycleOwner owner, @NonNull Size maxArtSize)86     public void setModel(@NonNull PlaybackViewModel model, @NonNull LifecycleOwner owner,
87             @NonNull Size maxArtSize) {
88         mMediaButtonController.setModel(model, owner);
89         mMetadataController = new MetadataController(owner, model,
90                 mTitle, mSubtitle, null, null, null, null, null, null, mContentTile, maxArtSize);
91         mPlaybackViewModel = model;
92 
93         ControlBarHelper.initProgressBar(getContext(), owner, mPlaybackViewModel,
94                 mLinearProgressBar, mShowLinearProgressBar);
95         ControlBarHelper.initProgressBar(getContext(), owner, mPlaybackViewModel,
96                 mCircularProgressBar, mShowCircularProgressBar);
97 
98         if (mArtBinder != null) {
99             mPlaybackViewModel.getMetadata().observe(owner,
100                     item -> mArtBinder.setImage(getContext(),
101                             item != null ? item.getArtworkKey() : null));
102         }
103     }
104 }
105