• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 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.radio;
18 
19 import android.content.Context;
20 import android.graphics.drawable.GradientDrawable;
21 import android.support.annotation.NonNull;
22 import android.support.v7.widget.RecyclerView;
23 import android.text.TextUtils;
24 import android.util.Log;
25 import android.view.View;
26 import android.widget.TextView;
27 import com.android.car.radio.service.RadioStation;
28 
29 /**
30  * A {@link RecyclerView.ViewHolder} that can bind a {@link RadioStation} to the layout
31  * {@code R.layout.radio_preset_item}.
32  */
33 public class PresetsViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
34     private static final String TAG = "Em.PresetVH";
35 
36     private final RadioChannelColorMapper mColorMapper;
37 
38     private final OnPresetClickListener mPresetClickListener;
39 
40     private final Context mContext;
41     private final View mPresetsCard;
42     private GradientDrawable mPresetItemChannelBg;
43     private final TextView mPresetItemChannel;
44     private final TextView mPresetItemMetadata;
45     private final View mEqualizer;
46 
47     /**
48      * Interface for a listener when the View held by this ViewHolder has been clicked.
49      */
50     public interface OnPresetClickListener {
51         /**
52          * Method to be called when the View in this ViewHolder has been clicked.
53          *
54          * @param position The position of the View within the RecyclerView this ViewHolder is
55          *                 populating.
56          */
onPresetClicked(int position)57         void onPresetClicked(int position);
58     }
59 
60     /**
61      * @param presetsView A view that contains the layout {@code R.layout.radio_preset_item}.
62      */
PresetsViewHolder(@onNull View presetsView, @NonNull OnPresetClickListener listener)63     public PresetsViewHolder(@NonNull View presetsView, @NonNull OnPresetClickListener listener) {
64         super(presetsView);
65 
66         mContext = presetsView.getContext();
67 
68         mPresetsCard = presetsView.findViewById(R.id.preset_card);;
69         mPresetsCard.setOnClickListener(this);
70 
71         mColorMapper = RadioChannelColorMapper.getInstance(mContext);
72         mPresetClickListener = listener;
73 
74         mPresetItemChannel =
75                 (TextView) presetsView.findViewById(R.id.preset_station_channel);
76         mPresetItemMetadata =
77                 (TextView) presetsView.findViewById(R.id.preset_item_metadata);
78         mEqualizer = presetsView.findViewById(R.id.preset_equalizer);
79 
80         mPresetItemChannelBg = (GradientDrawable) mPresetItemChannel.getBackground();
81     }
82 
83     @Override
onClick(View view)84     public void onClick(View view) {
85         if (Log.isLoggable(TAG, Log.DEBUG)) {
86             Log.d(TAG, "onClick() for view at position: " + getAdapterPosition());
87         }
88 
89         mPresetClickListener.onPresetClicked(getAdapterPosition());
90     }
91 
92     /**
93      * Binds the given {@link RadioStation} to this View within this ViewHolder.
94      */
bindPreset(RadioStation preset, boolean isActiveStation, int itemCount)95     public void bindPreset(RadioStation preset, boolean isActiveStation, int itemCount) {
96         // If the preset is null, clear any existing text.
97         if (preset == null) {
98             mPresetItemChannel.setText(null);
99             mPresetItemMetadata.setText(null);
100             mPresetItemChannelBg.setColor(mColorMapper.getDefaultColor());
101             return;
102         }
103 
104         setPresetCardBackground(itemCount);
105 
106         String channelNumber = RadioChannelFormatter.formatRadioChannel(preset.getRadioBand(),
107                 preset.getChannelNumber());
108 
109         mPresetItemChannel.setText(channelNumber);
110 
111         mEqualizer.setVisibility(isActiveStation ? View.VISIBLE : View.GONE);
112 
113         mPresetItemChannelBg.setColor(mColorMapper.getColorForStation(preset));
114 
115         String metadata = preset.getRds() == null ? null : preset.getRds().getProgramService();
116 
117         if (TextUtils.isEmpty(metadata)) {
118             // If there is no metadata text, then use text to indicate the favorite number to the
119             // user so that list does not appear empty.
120             mPresetItemMetadata.setText(mContext.getString(
121                     R.string.radio_default_preset_metadata_text, getAdapterPosition() + 1));
122         } else {
123             mPresetItemMetadata.setText(metadata.trim());
124         }
125     }
126 
127     /**
128      * Sets the appropriate background on the card containing the preset information. The cards
129      * need to have rounded corners depending on its position in the list and the number of items
130      * in the list.
131      */
setPresetCardBackground(int itemCount)132     private void setPresetCardBackground(int itemCount) {
133         int position = getAdapterPosition();
134 
135         // Correctly set the background for each card. Only the top and last card should
136         // have rounded corners.
137         if (itemCount == 1) {
138             // One card - all corners are rounded
139             mPresetsCard.setBackgroundResource(R.drawable.preset_item_card_rounded_bg);
140         } else if (position == 0) {
141             // First card gets rounded top
142             mPresetsCard.setBackgroundResource(R.drawable.preset_item_card_rounded_top_bg);
143         } else if (position == itemCount - 1) {
144             // Last one has a rounded bottom
145             mPresetsCard.setBackgroundResource(R.drawable.preset_item_card_rounded_bottom_bg);
146         } else {
147             // Middle have no rounded corners
148             mPresetsCard.setBackgroundResource(R.color.car_card);
149         }
150     }
151 }
152