Home | History | Annotate | Download | only in menu
      1 /*
      2  * Copyright (C) 2015 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.tv.menu;
     18 
     19 import android.content.Context;
     20 import android.text.TextUtils;
     21 import android.util.AttributeSet;
     22 import android.view.View;
     23 import android.widget.FrameLayout;
     24 import android.widget.ImageView;
     25 import android.widget.TextView;
     26 
     27 import com.android.tv.R;
     28 
     29 public class PlayControlsButton extends FrameLayout {
     30     private static final float ALPHA_ENABLED = 1.0f;
     31     private static final float ALPHA_DISABLED = 0.3f;
     32 
     33     private final ImageView mButton;
     34     private final ImageView mIcon;
     35     private final TextView mLabel;
     36 
     37     public PlayControlsButton(Context context) {
     38         this(context, null);
     39     }
     40 
     41     public PlayControlsButton(Context context, AttributeSet attrs) {
     42         this(context, attrs, 0);
     43     }
     44 
     45     public PlayControlsButton(Context context, AttributeSet attrs, int defStyleAttr) {
     46         this(context, attrs, defStyleAttr, 0);
     47     }
     48 
     49     public PlayControlsButton(Context context, AttributeSet attrs, int defStyleAttr,
     50             int defStyleRes) {
     51         super(context, attrs, defStyleAttr, defStyleRes);
     52         inflate(context, R.layout.play_controls_button, this);
     53         mButton = (ImageView) findViewById(R.id.button);
     54         mIcon = (ImageView) findViewById(R.id.icon);
     55         mLabel = (TextView) findViewById(R.id.label);
     56     }
     57 
     58     /**
     59      * Sets the resource ID of the image to be displayed in the center of this control.
     60      */
     61     public void setImageResId(int imageResId) {
     62         mIcon.setImageResource(imageResId);
     63     }
     64 
     65     /**
     66      * Sets an action which is to be run when the button is clicked.
     67      */
     68     public void setAction(final Runnable clickAction) {
     69         mButton.setOnClickListener(new OnClickListener() {
     70             @Override
     71             public void onClick(View view) {
     72                 clickAction.run();
     73             }
     74         });
     75     }
     76 
     77     public void setLabel(String label) {
     78         if (TextUtils.isEmpty(label)) {
     79             mIcon.setVisibility(View.VISIBLE);
     80             mLabel.setVisibility(View.GONE);
     81         } else {
     82             mIcon.setVisibility(View.GONE);
     83             mLabel.setVisibility(View.VISIBLE);
     84             mLabel.setText(label);
     85         }
     86     }
     87 
     88     public void hideRippleAnimation() {
     89         mButton.getDrawable().jumpToCurrentState();
     90     }
     91 
     92     @Override
     93     public void setEnabled(boolean enabled) {
     94         super.setEnabled(enabled);
     95         mButton.setEnabled(enabled);
     96         mIcon.setEnabled(enabled);
     97         mIcon.setAlpha(enabled ? ALPHA_ENABLED : ALPHA_DISABLED);
     98         mLabel.setEnabled(enabled);
     99     }
    100 }
    101