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 = presetsView.findViewById(R.id.preset_station_channel); 75 mPresetItemMetadata = presetsView.findViewById(R.id.preset_item_metadata); 76 mEqualizer = presetsView.findViewById(R.id.preset_equalizer); 77 78 mPresetItemChannelBg = (GradientDrawable) mPresetItemChannel.getBackground(); 79 } 80 81 @Override onClick(View view)82 public void onClick(View view) { 83 if (Log.isLoggable(TAG, Log.DEBUG)) { 84 Log.d(TAG, "onClick() for view at position: " + getAdapterPosition()); 85 } 86 87 mPresetClickListener.onPresetClicked(getAdapterPosition()); 88 } 89 90 /** 91 * Binds the given {@link RadioStation} to this View within this ViewHolder. 92 */ bindPreset(RadioStation preset, boolean isActiveStation, int itemCount)93 public void bindPreset(RadioStation preset, boolean isActiveStation, int itemCount) { 94 // If the preset is null, clear any existing text. 95 if (preset == null) { 96 mPresetItemChannel.setText(null); 97 mPresetItemMetadata.setText(null); 98 mPresetItemChannelBg.setColor(mColorMapper.getDefaultColor()); 99 return; 100 } 101 102 setPresetCardBackground(itemCount); 103 104 String channelNumber = RadioChannelFormatter.formatRadioChannel(preset.getRadioBand(), 105 preset.getChannelNumber()); 106 107 mPresetItemChannel.setText(channelNumber); 108 109 mEqualizer.setVisibility(isActiveStation ? View.VISIBLE : View.GONE); 110 111 mPresetItemChannelBg.setColor(mColorMapper.getColorForStation(preset)); 112 113 String metadata = preset.getRds() == null ? null : preset.getRds().getProgramService(); 114 115 if (TextUtils.isEmpty(metadata)) { 116 // If there is no metadata text, then use text to indicate the favorite number to the 117 // user so that list does not appear empty. 118 mPresetItemMetadata.setText(mContext.getString( 119 R.string.radio_default_preset_metadata_text, getAdapterPosition() + 1)); 120 } else { 121 mPresetItemMetadata.setText(metadata.trim()); 122 } 123 } 124 125 /** 126 * Sets the appropriate background on the card containing the preset information. The cards 127 * need to have rounded corners depending on its position in the list and the number of items 128 * in the list. 129 */ setPresetCardBackground(int itemCount)130 private void setPresetCardBackground(int itemCount) { 131 int position = getAdapterPosition(); 132 133 // Correctly set the background for each card. Only the top and last card should 134 // have rounded corners. 135 if (itemCount == 1) { 136 // One card - all corners are rounded 137 mPresetsCard.setBackgroundResource(R.drawable.preset_item_card_rounded_bg); 138 } else if (position == 0) { 139 // First card gets rounded top 140 mPresetsCard.setBackgroundResource(R.drawable.preset_item_card_rounded_top_bg); 141 } else if (position == itemCount - 1) { 142 // Last one has a rounded bottom 143 mPresetsCard.setBackgroundResource(R.drawable.preset_item_card_rounded_bottom_bg); 144 } else { 145 // Middle have no rounded corners 146 mPresetsCard.setBackgroundResource(R.color.car_card); 147 } 148 } 149 } 150