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.content.Context;
19 import android.content.res.TypedArray;
20 import android.graphics.drawable.Drawable;
21 import android.support.annotation.ColorInt;
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 boolean mIsBandSelected;
37 
RadioBandButton(Context context)38     public RadioBandButton(Context context) {
39         super(context);
40         init(context, null);
41     }
42 
RadioBandButton(Context context, AttributeSet attrs)43     public RadioBandButton(Context context, AttributeSet attrs) {
44         super(context, attrs);
45         init(context, attrs);
46     }
47 
RadioBandButton(Context context, AttributeSet attrs, int defStyleAttrs)48     public RadioBandButton(Context context, AttributeSet attrs, int defStyleAttrs) {
49         super(context, attrs, defStyleAttrs);
50         init(context, attrs);
51     }
52 
RadioBandButton(Context context, AttributeSet attrs, int defStyleAttrs, int defStyleRes)53     public RadioBandButton(Context context, AttributeSet attrs, int defStyleAttrs,
54             int defStyleRes) {
55         super(context, attrs, defStyleAttrs, defStyleRes);
56         init(context, attrs);
57     }
58 
59     /**
60      * Initializes whether or not this button is initially selected.
61      */
init(Context context, AttributeSet attrs)62     private void init(Context context, AttributeSet attrs) {
63         mSelectedBackground = context.getDrawable(R.drawable.manual_tuner_band_bg);
64         mNormalBackground = context.getDrawable(R.drawable.radio_control_background);
65 
66         mSelectedColor = context.getColor(R.color.car_grey_50);
67         mNormalColor = context.getColor(R.color.manual_tuner_channel_text);
68 
69         TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RadioBandButton);
70 
71         try {
72             setIsBandSelected(ta.getBoolean(R.styleable.RadioBandButton_isBandSelected, false));
73         } finally {
74             ta.recycle();
75         }
76     }
77 
78     /**
79      * Sets whether or not this button has been selected. This is different from
80      * {@link #setSelected(boolean)}.
81      */
setIsBandSelected(boolean selected)82     public void setIsBandSelected(boolean selected) {
83         mIsBandSelected = selected;
84 
85         if (mIsBandSelected) {
86             setTextColor(mSelectedColor);
87             setBackground(mSelectedBackground);
88         } else {
89             setTextColor(mNormalColor);
90             setBackground(mNormalBackground);
91         }
92     }
93 }
94