blob: c6e4356aa42010f5b1940a330a481f43c24e82e0 [file] [log] [blame]
John Spurlockae641c92014-06-30 18:11:40 -04001/*
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
17package com.android.systemui.volume;
18
19import android.content.Context;
John Spurlockd9c75db2015-04-28 11:19:13 -040020import android.graphics.Typeface;
John Spurlockae641c92014-06-30 18:11:40 -040021import android.util.AttributeSet;
22import android.view.LayoutInflater;
23import android.view.View;
24import android.widget.Button;
25import android.widget.LinearLayout;
John Spurlockeb2727b2014-07-19 23:11:36 -040026import android.widget.TextView;
John Spurlockae641c92014-06-30 18:11:40 -040027
28import com.android.systemui.R;
29
30import java.util.Objects;
31
32public class SegmentedButtons extends LinearLayout {
John Spurlock4291fb72014-09-16 17:02:23 -040033 private static final int LABEL_RES_KEY = R.id.label;
John Spurlockd9c75db2015-04-28 11:19:13 -040034 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 Spurlockae641c92014-06-30 18:11:40 -040036
37 private final Context mContext;
Muyuan Li29cbe662016-03-29 16:22:05 -070038 protected final LayoutInflater mInflater;
John Spurlockd8963232015-06-08 16:26:12 -040039 private final SpTexts mSpTexts;
John Spurlockae641c92014-06-30 18:11:40 -040040
41 private Callback mCallback;
Muyuan Li29cbe662016-03-29 16:22:05 -070042 protected Object mSelectedValue;
John Spurlockae641c92014-06-30 18:11:40 -040043
44 public SegmentedButtons(Context context, AttributeSet attrs) {
45 super(context, attrs);
46 mContext = context;
47 mInflater = LayoutInflater.from(mContext);
48 setOrientation(HORIZONTAL);
John Spurlockd8963232015-06-08 16:26:12 -040049 mSpTexts = new SpTexts(mContext);
John Spurlockae641c92014-06-30 18:11:40 -040050 }
51
52 public void setCallback(Callback callback) {
53 mCallback = callback;
54 }
55
56 public Object getSelectedValue() {
57 return mSelectedValue;
58 }
59
Chris Wren4572cbc2015-06-29 11:27:18 -040060 public void setSelectedValue(Object value, boolean fromClick) {
John Spurlockae641c92014-06-30 18:11:40 -040061 if (Objects.equals(value, mSelectedValue)) return;
62 mSelectedValue = value;
63 for (int i = 0; i < getChildCount(); i++) {
John Spurlockeb2727b2014-07-19 23:11:36 -040064 final TextView c = (TextView) getChildAt(i);
John Spurlockae641c92014-06-30 18:11:40 -040065 final Object tag = c.getTag();
John Spurlockeb2727b2014-07-19 23:11:36 -040066 final boolean selected = Objects.equals(mSelectedValue, tag);
67 c.setSelected(selected);
Muyuan Li29cbe662016-03-29 16:22:05 -070068 setSelectedStyle(c, selected);
John Spurlockae641c92014-06-30 18:11:40 -040069 }
Chris Wren4572cbc2015-06-29 11:27:18 -040070 fireOnSelected(fromClick);
John Spurlockae641c92014-06-30 18:11:40 -040071 }
72
Muyuan Li29cbe662016-03-29 16:22:05 -070073 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 Spurlocka1c7ffe2015-06-08 15:34:05 -040081 public void addButton(int labelResId, int contentDescriptionResId, Object value) {
Muyuan Li29cbe662016-03-29 16:22:05 -070082 final Button b = inflateButton();
John Spurlock4291fb72014-09-16 17:02:23 -040083 b.setTag(LABEL_RES_KEY, labelResId);
John Spurlockae641c92014-06-30 18:11:40 -040084 b.setText(labelResId);
John Spurlocka1c7ffe2015-06-08 15:34:05 -040085 b.setContentDescription(getResources().getString(contentDescriptionResId));
John Spurlockae641c92014-06-30 18:11:40 -040086 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 Spurlocka0457c22014-09-26 13:22:08 -040094 Interaction.register(b, new Interaction.Callback() {
95 @Override
96 public void onInteraction() {
97 fireInteraction();
98 }
99 });
John Spurlockd8963232015-06-08 16:26:12 -0400100 mSpTexts.add(b);
John Spurlockae641c92014-06-30 18:11:40 -0400101 }
102
John Spurlock4291fb72014-09-16 17:02:23 -0400103 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 Wren4572cbc2015-06-29 11:27:18 -0400111 private void fireOnSelected(boolean fromClick) {
John Spurlockae641c92014-06-30 18:11:40 -0400112 if (mCallback != null) {
Chris Wren4572cbc2015-06-29 11:27:18 -0400113 mCallback.onSelected(mSelectedValue, fromClick);
John Spurlockae641c92014-06-30 18:11:40 -0400114 }
115 }
116
John Spurlocka0457c22014-09-26 13:22:08 -0400117 private void fireInteraction() {
118 if (mCallback != null) {
119 mCallback.onInteraction();
120 }
121 }
122
John Spurlockae641c92014-06-30 18:11:40 -0400123 private final View.OnClickListener mClick = new View.OnClickListener() {
124 @Override
125 public void onClick(View v) {
Chris Wren4572cbc2015-06-29 11:27:18 -0400126 setSelectedValue(v.getTag(), true /* fromClick */);
John Spurlockae641c92014-06-30 18:11:40 -0400127 }
128 };
129
John Spurlocka0457c22014-09-26 13:22:08 -0400130 public interface Callback extends Interaction.Callback {
Chris Wren4572cbc2015-06-29 11:27:18 -0400131 void onSelected(Object value, boolean fromClick);
John Spurlockae641c92014-06-30 18:11:40 -0400132 }
133}