John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 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; |
John Spurlock | eb2727b | 2014-07-19 23:11:36 -0400 | [diff] [blame] | 20 | import android.graphics.Typeface; |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 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; |
John Spurlock | eb2727b | 2014-07-19 23:11:36 -0400 | [diff] [blame] | 26 | import android.widget.TextView; |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 27 | |
| 28 | import com.android.systemui.R; |
| 29 | |
| 30 | import java.util.Objects; |
| 31 | |
| 32 | public class SegmentedButtons extends LinearLayout { |
John Spurlock | eb2727b | 2014-07-19 23:11:36 -0400 | [diff] [blame] | 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); |
John Spurlock | 4291fb7 | 2014-09-16 17:02:23 -0400 | [diff] [blame] | 35 | private static final int LABEL_RES_KEY = R.id.label; |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 36 | |
| 37 | private final Context mContext; |
| 38 | private final LayoutInflater mInflater; |
| 39 | |
| 40 | private Callback mCallback; |
| 41 | private Object mSelectedValue; |
| 42 | |
| 43 | public SegmentedButtons(Context context, AttributeSet attrs) { |
| 44 | super(context, attrs); |
| 45 | mContext = context; |
| 46 | mInflater = LayoutInflater.from(mContext); |
| 47 | setOrientation(HORIZONTAL); |
| 48 | } |
| 49 | |
| 50 | public void setCallback(Callback callback) { |
| 51 | mCallback = callback; |
| 52 | } |
| 53 | |
| 54 | public Object getSelectedValue() { |
| 55 | return mSelectedValue; |
| 56 | } |
| 57 | |
| 58 | public void setSelectedValue(Object value) { |
| 59 | if (Objects.equals(value, mSelectedValue)) return; |
| 60 | mSelectedValue = value; |
| 61 | for (int i = 0; i < getChildCount(); i++) { |
John Spurlock | eb2727b | 2014-07-19 23:11:36 -0400 | [diff] [blame] | 62 | final TextView c = (TextView) getChildAt(i); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 63 | final Object tag = c.getTag(); |
John Spurlock | eb2727b | 2014-07-19 23:11:36 -0400 | [diff] [blame] | 64 | final boolean selected = Objects.equals(mSelectedValue, tag); |
| 65 | c.setSelected(selected); |
| 66 | c.setTypeface(selected ? BOLD : MEDIUM); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 67 | } |
| 68 | fireOnSelected(); |
| 69 | } |
| 70 | |
| 71 | public void addButton(int labelResId, Object value) { |
| 72 | final Button b = (Button) mInflater.inflate(R.layout.segmented_button, this, false); |
John Spurlock | 4291fb7 | 2014-09-16 17:02:23 -0400 | [diff] [blame] | 73 | b.setTag(LABEL_RES_KEY, labelResId); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 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 | } |
| 84 | |
John Spurlock | 4291fb7 | 2014-09-16 17:02:23 -0400 | [diff] [blame] | 85 | public void updateLocale() { |
| 86 | for (int i = 0; i < getChildCount(); i++) { |
| 87 | final Button b = (Button) getChildAt(i); |
| 88 | final int labelResId = (Integer) b.getTag(LABEL_RES_KEY); |
| 89 | b.setText(labelResId); |
| 90 | } |
| 91 | } |
| 92 | |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 93 | private void fireOnSelected() { |
| 94 | if (mCallback != null) { |
| 95 | mCallback.onSelected(mSelectedValue); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | private final View.OnClickListener mClick = new View.OnClickListener() { |
| 100 | @Override |
| 101 | public void onClick(View v) { |
| 102 | setSelectedValue(v.getTag()); |
| 103 | } |
| 104 | }; |
| 105 | |
| 106 | public interface Callback { |
| 107 | void onSelected(Object value); |
| 108 | } |
| 109 | } |