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 | d9c75db | 2015-04-28 11:19:13 -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 | 4291fb7 | 2014-09-16 17:02:23 -0400 | [diff] [blame] | 33 | private static final int LABEL_RES_KEY = R.id.label; |
John Spurlock | d9c75db | 2015-04-28 11:19:13 -0400 | [diff] [blame] | 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); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 36 | |
| 37 | private final Context mContext; |
Muyuan Li | 29cbe66 | 2016-03-29 16:22:05 -0700 | [diff] [blame] | 38 | protected final LayoutInflater mInflater; |
John Spurlock | d896323 | 2015-06-08 16:26:12 -0400 | [diff] [blame] | 39 | private final SpTexts mSpTexts; |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 40 | |
| 41 | private Callback mCallback; |
Muyuan Li | 29cbe66 | 2016-03-29 16:22:05 -0700 | [diff] [blame] | 42 | protected Object mSelectedValue; |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 43 | |
| 44 | public SegmentedButtons(Context context, AttributeSet attrs) { |
| 45 | super(context, attrs); |
| 46 | mContext = context; |
| 47 | mInflater = LayoutInflater.from(mContext); |
| 48 | setOrientation(HORIZONTAL); |
John Spurlock | d896323 | 2015-06-08 16:26:12 -0400 | [diff] [blame] | 49 | mSpTexts = new SpTexts(mContext); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | public void setCallback(Callback callback) { |
| 53 | mCallback = callback; |
| 54 | } |
| 55 | |
| 56 | public Object getSelectedValue() { |
| 57 | return mSelectedValue; |
| 58 | } |
| 59 | |
Chris Wren | 4572cbc | 2015-06-29 11:27:18 -0400 | [diff] [blame] | 60 | public void setSelectedValue(Object value, boolean fromClick) { |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 61 | if (Objects.equals(value, mSelectedValue)) return; |
| 62 | mSelectedValue = value; |
| 63 | for (int i = 0; i < getChildCount(); i++) { |
John Spurlock | eb2727b | 2014-07-19 23:11:36 -0400 | [diff] [blame] | 64 | final TextView c = (TextView) getChildAt(i); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 65 | final Object tag = c.getTag(); |
John Spurlock | eb2727b | 2014-07-19 23:11:36 -0400 | [diff] [blame] | 66 | final boolean selected = Objects.equals(mSelectedValue, tag); |
| 67 | c.setSelected(selected); |
Muyuan Li | 29cbe66 | 2016-03-29 16:22:05 -0700 | [diff] [blame] | 68 | setSelectedStyle(c, selected); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 69 | } |
Chris Wren | 4572cbc | 2015-06-29 11:27:18 -0400 | [diff] [blame] | 70 | fireOnSelected(fromClick); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 71 | } |
| 72 | |
Muyuan Li | 29cbe66 | 2016-03-29 16:22:05 -0700 | [diff] [blame] | 73 | protected void setSelectedStyle(TextView textView, boolean selected) { |
| 74 | textView.setTypeface(selected ? MEDIUM : REGULAR); |
| 75 | } |
| 76 | |
| 77 | public Button inflateButton() { |
| 78 | return (Button) mInflater.inflate(R.layout.segmented_button, this, false); |
| 79 | } |
| 80 | |
John Spurlock | a1c7ffe | 2015-06-08 15:34:05 -0400 | [diff] [blame] | 81 | public void addButton(int labelResId, int contentDescriptionResId, Object value) { |
Muyuan Li | 29cbe66 | 2016-03-29 16:22:05 -0700 | [diff] [blame] | 82 | final Button b = inflateButton(); |
John Spurlock | 4291fb7 | 2014-09-16 17:02:23 -0400 | [diff] [blame] | 83 | b.setTag(LABEL_RES_KEY, labelResId); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 84 | b.setText(labelResId); |
John Spurlock | a1c7ffe | 2015-06-08 15:34:05 -0400 | [diff] [blame] | 85 | b.setContentDescription(getResources().getString(contentDescriptionResId)); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 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); |
John Spurlock | a0457c2 | 2014-09-26 13:22:08 -0400 | [diff] [blame] | 94 | Interaction.register(b, new Interaction.Callback() { |
| 95 | @Override |
| 96 | public void onInteraction() { |
| 97 | fireInteraction(); |
| 98 | } |
| 99 | }); |
John Spurlock | d896323 | 2015-06-08 16:26:12 -0400 | [diff] [blame] | 100 | mSpTexts.add(b); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 101 | } |
| 102 | |
John Spurlock | 4291fb7 | 2014-09-16 17:02:23 -0400 | [diff] [blame] | 103 | public void updateLocale() { |
| 104 | for (int i = 0; i < getChildCount(); i++) { |
| 105 | final Button b = (Button) getChildAt(i); |
| 106 | final int labelResId = (Integer) b.getTag(LABEL_RES_KEY); |
| 107 | b.setText(labelResId); |
| 108 | } |
| 109 | } |
| 110 | |
Chris Wren | 4572cbc | 2015-06-29 11:27:18 -0400 | [diff] [blame] | 111 | private void fireOnSelected(boolean fromClick) { |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 112 | if (mCallback != null) { |
Chris Wren | 4572cbc | 2015-06-29 11:27:18 -0400 | [diff] [blame] | 113 | mCallback.onSelected(mSelectedValue, fromClick); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
John Spurlock | a0457c2 | 2014-09-26 13:22:08 -0400 | [diff] [blame] | 117 | private void fireInteraction() { |
| 118 | if (mCallback != null) { |
| 119 | mCallback.onInteraction(); |
| 120 | } |
| 121 | } |
| 122 | |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 123 | private final View.OnClickListener mClick = new View.OnClickListener() { |
| 124 | @Override |
| 125 | public void onClick(View v) { |
Chris Wren | 4572cbc | 2015-06-29 11:27:18 -0400 | [diff] [blame] | 126 | setSelectedValue(v.getTag(), true /* fromClick */); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 127 | } |
| 128 | }; |
| 129 | |
John Spurlock | a0457c2 | 2014-09-26 13:22:08 -0400 | [diff] [blame] | 130 | public interface Callback extends Interaction.Callback { |
Chris Wren | 4572cbc | 2015-06-29 11:27:18 -0400 | [diff] [blame] | 131 | void onSelected(Object value, boolean fromClick); |
John Spurlock | ae641c9 | 2014-06-30 18:11:40 -0400 | [diff] [blame] | 132 | } |
| 133 | } |