Home | History | Annotate | Download | only in volume
      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;
     20 import android.graphics.Typeface;
     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;
     26 import android.widget.TextView;
     27 
     28 import com.android.systemui.R;
     29 
     30 import java.util.Objects;
     31 
     32 public class SegmentedButtons extends LinearLayout {
     33     private static final int LABEL_RES_KEY = R.id.label;
     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);
     36 
     37     private final Context mContext;
     38     protected final LayoutInflater mInflater;
     39     private final SpTexts mSpTexts;
     40 
     41     private Callback mCallback;
     42     protected 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);
     49         mSpTexts = new SpTexts(mContext);
     50     }
     51 
     52     public void setCallback(Callback callback) {
     53         mCallback = callback;
     54     }
     55 
     56     public Object getSelectedValue() {
     57         return mSelectedValue;
     58     }
     59 
     60     public void setSelectedValue(Object value, boolean fromClick) {
     61         if (Objects.equals(value, mSelectedValue)) return;
     62         mSelectedValue = value;
     63         for (int i = 0; i < getChildCount(); i++) {
     64             final TextView c = (TextView) getChildAt(i);
     65             final Object tag = c.getTag();
     66             final boolean selected = Objects.equals(mSelectedValue, tag);
     67             c.setSelected(selected);
     68             setSelectedStyle(c, selected);
     69         }
     70         fireOnSelected(fromClick);
     71     }
     72 
     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 
     81     public void addButton(int labelResId, int contentDescriptionResId, Object value) {
     82         final Button b = inflateButton();
     83         b.setTag(LABEL_RES_KEY, labelResId);
     84         b.setText(labelResId);
     85         b.setContentDescription(getResources().getString(contentDescriptionResId));
     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);
     94         Interaction.register(b, new Interaction.Callback() {
     95             @Override
     96             public void onInteraction() {
     97                 fireInteraction();
     98             }
     99         });
    100         mSpTexts.add(b);
    101     }
    102 
    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 
    111     private void fireOnSelected(boolean fromClick) {
    112         if (mCallback != null) {
    113             mCallback.onSelected(mSelectedValue, fromClick);
    114         }
    115     }
    116 
    117     private void fireInteraction() {
    118         if (mCallback != null) {
    119             mCallback.onInteraction();
    120         }
    121     }
    122 
    123     private final View.OnClickListener mClick = new View.OnClickListener() {
    124         @Override
    125         public void onClick(View v) {
    126             setSelectedValue(v.getTag(), true /* fromClick */);
    127         }
    128     };
    129 
    130     public interface Callback extends Interaction.Callback {
    131         void onSelected(Object value, boolean fromClick);
    132     }
    133 }
    134