blob: f4328089bf0fc49e312af5e468fd49e648d62c3e [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;
38 private 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;
42 private Object mSelectedValue;
43
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);
John Spurlockd9c75db2015-04-28 11:19:13 -040068 c.setTypeface(selected ? MEDIUM : REGULAR);
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
John Spurlocka1c7ffe2015-06-08 15:34:05 -040073 public void addButton(int labelResId, int contentDescriptionResId, Object value) {
John Spurlockae641c92014-06-30 18:11:40 -040074 final Button b = (Button) mInflater.inflate(R.layout.segmented_button, this, false);
John Spurlock4291fb72014-09-16 17:02:23 -040075 b.setTag(LABEL_RES_KEY, labelResId);
John Spurlockae641c92014-06-30 18:11:40 -040076 b.setText(labelResId);
John Spurlocka1c7ffe2015-06-08 15:34:05 -040077 b.setContentDescription(getResources().getString(contentDescriptionResId));
John Spurlockae641c92014-06-30 18:11:40 -040078 final LayoutParams lp = (LayoutParams) b.getLayoutParams();
79 if (getChildCount() == 0) {
80 lp.leftMargin = lp.rightMargin = 0; // first button has no margin
81 }
82 b.setLayoutParams(lp);
83 addView(b);
84 b.setTag(value);
85 b.setOnClickListener(mClick);
John Spurlocka0457c22014-09-26 13:22:08 -040086 Interaction.register(b, new Interaction.Callback() {
87 @Override
88 public void onInteraction() {
89 fireInteraction();
90 }
91 });
John Spurlockd8963232015-06-08 16:26:12 -040092 mSpTexts.add(b);
John Spurlockae641c92014-06-30 18:11:40 -040093 }
94
John Spurlock4291fb72014-09-16 17:02:23 -040095 public void updateLocale() {
96 for (int i = 0; i < getChildCount(); i++) {
97 final Button b = (Button) getChildAt(i);
98 final int labelResId = (Integer) b.getTag(LABEL_RES_KEY);
99 b.setText(labelResId);
100 }
101 }
102
Chris Wren4572cbc2015-06-29 11:27:18 -0400103 private void fireOnSelected(boolean fromClick) {
John Spurlockae641c92014-06-30 18:11:40 -0400104 if (mCallback != null) {
Chris Wren4572cbc2015-06-29 11:27:18 -0400105 mCallback.onSelected(mSelectedValue, fromClick);
John Spurlockae641c92014-06-30 18:11:40 -0400106 }
107 }
108
John Spurlocka0457c22014-09-26 13:22:08 -0400109 private void fireInteraction() {
110 if (mCallback != null) {
111 mCallback.onInteraction();
112 }
113 }
114
John Spurlockae641c92014-06-30 18:11:40 -0400115 private final View.OnClickListener mClick = new View.OnClickListener() {
116 @Override
117 public void onClick(View v) {
Chris Wren4572cbc2015-06-29 11:27:18 -0400118 setSelectedValue(v.getTag(), true /* fromClick */);
John Spurlockae641c92014-06-30 18:11:40 -0400119 }
120 };
121
John Spurlocka0457c22014-09-26 13:22:08 -0400122 public interface Callback extends Interaction.Callback {
Chris Wren4572cbc2015-06-29 11:27:18 -0400123 void onSelected(Object value, boolean fromClick);
John Spurlockae641c92014-06-30 18:11:40 -0400124 }
125}