1 /* 2 * Copyright (C) 2014 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.systemui.volume; 18 19 import android.content.Context; 20 import android.graphics.Typeface; 21 import android.util.AttributeSet; 22 import android.view.LayoutInflater; 23 import android.view.View; 24 import android.widget.Button; 25 import android.widget.LinearLayout; 26 import android.widget.TextView; 27 28 import com.android.systemui.R; 29 30 import java.util.Objects; 31 32 public class SegmentedButtons extends LinearLayout { 33 private static final Typeface MEDIUM = Typeface.create("sans-serif-medium", Typeface.NORMAL); 34 private static final Typeface BOLD = Typeface.create("sans-serif", Typeface.BOLD); 35 private static final int LABEL_RES_KEY = R.id.label; 36 37 private final Context mContext; 38 private final LayoutInflater mInflater; 39 40 private Callback mCallback; 41 private Object mSelectedValue; 42 SegmentedButtons(Context context, AttributeSet attrs)43 public SegmentedButtons(Context context, AttributeSet attrs) { 44 super(context, attrs); 45 mContext = context; 46 mInflater = LayoutInflater.from(mContext); 47 setOrientation(HORIZONTAL); 48 } 49 setCallback(Callback callback)50 public void setCallback(Callback callback) { 51 mCallback = callback; 52 } 53 getSelectedValue()54 public Object getSelectedValue() { 55 return mSelectedValue; 56 } 57 setSelectedValue(Object value)58 public void setSelectedValue(Object value) { 59 if (Objects.equals(value, mSelectedValue)) return; 60 mSelectedValue = value; 61 for (int i = 0; i < getChildCount(); i++) { 62 final TextView c = (TextView) getChildAt(i); 63 final Object tag = c.getTag(); 64 final boolean selected = Objects.equals(mSelectedValue, tag); 65 c.setSelected(selected); 66 c.setTypeface(selected ? BOLD : MEDIUM); 67 } 68 fireOnSelected(); 69 } 70 addButton(int labelResId, Object value)71 public void addButton(int labelResId, Object value) { 72 final Button b = (Button) mInflater.inflate(R.layout.segmented_button, this, false); 73 b.setTag(LABEL_RES_KEY, labelResId); 74 b.setText(labelResId); 75 final LayoutParams lp = (LayoutParams) b.getLayoutParams(); 76 if (getChildCount() == 0) { 77 lp.leftMargin = lp.rightMargin = 0; // first button has no margin 78 } 79 b.setLayoutParams(lp); 80 addView(b); 81 b.setTag(value); 82 b.setOnClickListener(mClick); 83 Interaction.register(b, new Interaction.Callback() { 84 @Override 85 public void onInteraction() { 86 fireInteraction(); 87 } 88 }); 89 } 90 updateLocale()91 public void updateLocale() { 92 for (int i = 0; i < getChildCount(); i++) { 93 final Button b = (Button) getChildAt(i); 94 final int labelResId = (Integer) b.getTag(LABEL_RES_KEY); 95 b.setText(labelResId); 96 } 97 } 98 fireOnSelected()99 private void fireOnSelected() { 100 if (mCallback != null) { 101 mCallback.onSelected(mSelectedValue); 102 } 103 } 104 fireInteraction()105 private void fireInteraction() { 106 if (mCallback != null) { 107 mCallback.onInteraction(); 108 } 109 } 110 111 private final View.OnClickListener mClick = new View.OnClickListener() { 112 @Override 113 public void onClick(View v) { 114 setSelectedValue(v.getTag()); 115 } 116 }; 117 118 public interface Callback extends Interaction.Callback { onSelected(Object value)119 void onSelected(Object value); 120 } 121 } 122