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 package com.android.car.radio; 17 18 import android.annotation.ColorInt; 19 import android.content.Context; 20 import android.content.res.TypedArray; 21 import android.graphics.drawable.Drawable; 22 import android.util.AttributeSet; 23 import android.widget.Button; 24 25 /** 26 * A button that represents a band the user can select in manual tuning. When this button is 27 * selected, then it draws a rounded pill background around itself. 28 */ 29 public class RadioBandButton extends Button { 30 private Drawable mSelectedBackground; 31 private Drawable mNormalBackground; 32 33 @ColorInt private int mSelectedColor; 34 @ColorInt private int mNormalColor; 35 36 public RadioBandButton(Context context) { 37 super(context); 38 init(context, null); 39 } 40 41 public RadioBandButton(Context context, AttributeSet attrs) { 42 super(context, attrs); 43 init(context, attrs); 44 } 45 46 public RadioBandButton(Context context, AttributeSet attrs, int defStyleAttrs) { 47 super(context, attrs, defStyleAttrs); 48 init(context, attrs); 49 } 50 51 public RadioBandButton(Context context, AttributeSet attrs, int defStyleAttrs, 52 int defStyleRes) { 53 super(context, attrs, defStyleAttrs, defStyleRes); 54 init(context, attrs); 55 } 56 57 /** 58 * Initializes whether or not this button is initially selected. 59 */ 60 private void init(Context context, AttributeSet attrs) { 61 mSelectedBackground = context.getDrawable(R.drawable.manual_tuner_band_bg); 62 mNormalBackground = context.getDrawable(R.drawable.radio_control_background); 63 64 mSelectedColor = context.getColor(R.color.car_grey_50); 65 mNormalColor = context.getColor(R.color.manual_tuner_channel_text); 66 67 TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RadioBandButton); 68 69 try { 70 setIsBandSelected(ta.getBoolean(R.styleable.RadioBandButton_isBandSelected, false)); 71 } finally { 72 ta.recycle(); 73 } 74 } 75 76 /** 77 * Sets whether or not this button has been selected. This is different from 78 * {@link #setSelected(boolean)}. 79 */ 80 public void setIsBandSelected(boolean selected) { 81 if (selected) { 82 setTextColor(mSelectedColor); 83 setBackground(mSelectedBackground); 84 } else { 85 setTextColor(mNormalColor); 86 setBackground(mNormalBackground); 87 } 88 } 89 } 90