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     private final LayoutInflater mInflater;
     39     private final SpTexts mSpTexts;
     40 
     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);
     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             c.setTypeface(selected ? MEDIUM : REGULAR);
     69         }
     70         fireOnSelected(fromClick);
     71     }
     72 
     73     public void addButton(int labelResId, int contentDescriptionResId, Object value) {
     74         final Button b = (Button) mInflater.inflate(R.layout.segmented_button, this, false);
     75         b.setTag(LABEL_RES_KEY, labelResId);
     76         b.setText(labelResId);
     77         b.setContentDescription(getResources().getString(contentDescriptionResId));
     78         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);
     86         Interaction.register(b, new Interaction.Callback() {
     87             @Override
     88             public void onInteraction() {
     89                 fireInteraction();
     90             }
     91         });
     92         mSpTexts.add(b);
     93     }
     94 
     95     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 
    103     private void fireOnSelected(boolean fromClick) {
    104         if (mCallback != null) {
    105             mCallback.onSelected(mSelectedValue, fromClick);
    106         }
    107     }
    108 
    109     private void fireInteraction() {
    110         if (mCallback != null) {
    111             mCallback.onInteraction();
    112         }
    113     }
    114 
    115     private final View.OnClickListener mClick = new View.OnClickListener() {
    116         @Override
    117         public void onClick(View v) {
    118             setSelectedValue(v.getTag(), true /* fromClick */);
    119         }
    120     };
    121 
    122     public interface Callback extends Interaction.Callback {
    123         void onSelected(Object value, boolean fromClick);
    124     }
    125 }
    126