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 int LABEL_RES_KEY = R.id.label; 34 private static final Typeface REGULAR = Typeface.create("sans-serif", Typeface.NORMAL); 35 private static final Typeface MEDIUM = Typeface.create("sans-serif-medium", Typeface.NORMAL); 36 37 private final Context mContext; 38 protected final LayoutInflater mInflater; 39 private final ConfigurableTexts mConfigurableTexts; 40 41 private Callback mCallback; 42 protected Object mSelectedValue; 43 SegmentedButtons(Context context, AttributeSet attrs)44 public SegmentedButtons(Context context, AttributeSet attrs) { 45 super(context, attrs); 46 mContext = context; 47 mInflater = LayoutInflater.from(mContext); 48 setOrientation(HORIZONTAL); 49 mConfigurableTexts = new ConfigurableTexts(mContext); 50 } 51 setCallback(Callback callback)52 public void setCallback(Callback callback) { 53 mCallback = callback; 54 } 55 getSelectedValue()56 public Object getSelectedValue() { 57 return mSelectedValue; 58 } 59 setSelectedValue(Object value, boolean fromClick)60 public void setSelectedValue(Object value, boolean fromClick) { 61 if (Objects.equals(value, mSelectedValue)) return; 62 mSelectedValue = value; 63 for (int i = 0; i < getChildCount(); i++) { 64 final TextView c = (TextView) getChildAt(i); 65 final Object tag = c.getTag(); 66 final boolean selected = Objects.equals(mSelectedValue, tag); 67 c.setSelected(selected); 68 setSelectedStyle(c, selected); 69 } 70 fireOnSelected(fromClick); 71 } 72 setSelectedStyle(TextView textView, boolean selected)73 protected void setSelectedStyle(TextView textView, boolean selected) { 74 textView.setTypeface(selected ? MEDIUM : REGULAR); 75 } 76 inflateButton()77 public Button inflateButton() { 78 return (Button) mInflater.inflate(R.layout.segmented_button, this, false); 79 } 80 addButton(int labelResId, int contentDescriptionResId, Object value)81 public void addButton(int labelResId, int contentDescriptionResId, Object value) { 82 final Button b = inflateButton(); 83 b.setTag(LABEL_RES_KEY, labelResId); 84 b.setText(labelResId); 85 b.setContentDescription(getResources().getString(contentDescriptionResId)); 86 final LayoutParams lp = (LayoutParams) b.getLayoutParams(); 87 if (getChildCount() == 0) { 88 lp.leftMargin = lp.rightMargin = 0; // first button has no margin 89 } 90 b.setLayoutParams(lp); 91 addView(b); 92 b.setTag(value); 93 b.setOnClickListener(mClick); 94 Interaction.register(b, new Interaction.Callback() { 95 @Override 96 public void onInteraction() { 97 fireInteraction(); 98 } 99 }); 100 mConfigurableTexts.add(b, labelResId); 101 } 102 update()103 public void update() { 104 mConfigurableTexts.update(); 105 } 106 fireOnSelected(boolean fromClick)107 private void fireOnSelected(boolean fromClick) { 108 if (mCallback != null) { 109 mCallback.onSelected(mSelectedValue, fromClick); 110 } 111 } 112 fireInteraction()113 private void fireInteraction() { 114 if (mCallback != null) { 115 mCallback.onInteraction(); 116 } 117 } 118 119 private final View.OnClickListener mClick = new View.OnClickListener() { 120 @Override 121 public void onClick(View v) { 122 setSelectedValue(v.getTag(), true /* fromClick */); 123 } 124 }; 125 126 public interface Callback extends Interaction.Callback { onSelected(Object value, boolean fromClick)127 void onSelected(Object value, boolean fromClick); 128 } 129 } 130