blob: 66e1e157cde6cd9dc8373cdd93ba0a7cf2844921 [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 Spurlockeb2727b2014-07-19 23:11:36 -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 Spurlockeb2727b2014-07-19 23:11:36 -040033 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 Spurlock4291fb72014-09-16 17:02:23 -040035 private static final int LABEL_RES_KEY = R.id.label;
John Spurlockae641c92014-06-30 18:11:40 -040036
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 Spurlockeb2727b2014-07-19 23:11:36 -040062 final TextView c = (TextView) getChildAt(i);
John Spurlockae641c92014-06-30 18:11:40 -040063 final Object tag = c.getTag();
John Spurlockeb2727b2014-07-19 23:11:36 -040064 final boolean selected = Objects.equals(mSelectedValue, tag);
65 c.setSelected(selected);
66 c.setTypeface(selected ? BOLD : MEDIUM);
John Spurlockae641c92014-06-30 18:11:40 -040067 }
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 Spurlock4291fb72014-09-16 17:02:23 -040073 b.setTag(LABEL_RES_KEY, labelResId);
John Spurlockae641c92014-06-30 18:11:40 -040074 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 Spurlock4291fb72014-09-16 17:02:23 -040085 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 Spurlockae641c92014-06-30 18:11:40 -040093 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}