1 /** 2 * Copyright (C) 2018 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.widget; 18 19 import android.content.Context; 20 import android.util.AttributeSet; 21 import android.view.LayoutInflater; 22 import android.widget.Button; 23 import android.widget.LinearLayout; 24 25 import androidx.annotation.NonNull; 26 import androidx.annotation.Nullable; 27 28 import com.android.car.radio.R; 29 import com.android.car.radio.bands.ProgramType; 30 31 import java.util.ArrayList; 32 import java.util.List; 33 34 /** 35 * A band selector that shows a flat list of band buttons. 36 */ 37 public class BandSelectorFlat extends LinearLayout { 38 private final Object mLock = new Object(); 39 @Nullable private Callback mCallback; 40 41 private final List<Button> mButtons = new ArrayList<>(); 42 43 /** 44 * Widget's onClick event translated to band callback. 45 */ 46 public interface Callback { 47 /** 48 * Called when user uses this button to switch the band. 49 * 50 * @param pt ProgramType to switch to 51 */ onSwitchTo(@onNull ProgramType pt)52 void onSwitchTo(@NonNull ProgramType pt); 53 } 54 BandSelectorFlat(Context context, AttributeSet attrs)55 public BandSelectorFlat(Context context, AttributeSet attrs) { 56 this(context, attrs, 0); 57 } 58 BandSelectorFlat(Context context, AttributeSet attrs, int defStyleAttr)59 public BandSelectorFlat(Context context, AttributeSet attrs, int defStyleAttr) { 60 super(context, attrs, defStyleAttr); 61 } 62 switchTo(@onNull ProgramType ptype)63 private void switchTo(@NonNull ProgramType ptype) { 64 synchronized (mLock) { 65 if (mCallback != null) mCallback.onSwitchTo(ptype); 66 } 67 } 68 69 /** 70 * Sets band selection callback. 71 */ setCallback(@ullable Callback callback)72 public void setCallback(@Nullable Callback callback) { 73 synchronized (mLock) { 74 mCallback = callback; 75 } 76 } 77 78 /** 79 * Updates the list of supported program types. 80 */ setSupportedProgramTypes(@onNull List<ProgramType> supported)81 public void setSupportedProgramTypes(@NonNull List<ProgramType> supported) { 82 synchronized (mLock) { 83 final LayoutInflater inflater = LayoutInflater.from(getContext()); 84 85 mButtons.clear(); 86 removeAllViews(); 87 for (ProgramType pt : supported) { 88 Button btn = (Button) inflater.inflate(R.layout.band_selector_flat_button, null); 89 btn.setText(pt.getLocalizedName()); 90 btn.setTag(pt); 91 btn.setOnClickListener(v -> switchTo(pt)); 92 addView(btn); 93 mButtons.add(btn); 94 } 95 } 96 } 97 98 /** 99 * Updates the selected button based on the given program type. 100 * 101 * @param ptype The program type that needs to be selected. 102 */ setType(@onNull ProgramType ptype)103 public void setType(@NonNull ProgramType ptype) { 104 synchronized (mLock) { 105 Context ctx = getContext(); 106 for (Button btn : mButtons) { 107 boolean active = btn.getTag() == ptype; 108 btn.setTextColor(ctx.getColor(active 109 ? R.color.band_selector_flat_text_color_selected 110 : R.color.band_selector_flat_text_color)); 111 btn.setBackground(ctx.getDrawable(active 112 ? R.drawable.manual_tuner_button_background 113 : R.drawable.radio_control_background)); 114 } 115 } 116 } 117 } 118