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.R.integer;
20 import android.animation.ValueAnimator;
21 import android.content.Context;
22 import android.text.TextUtils;
23 import android.util.AttributeSet;
24 import android.view.View;
25 import android.widget.FrameLayout;
26 import android.widget.ImageView;
27 import android.widget.TextView;
28 
29 import com.android.tv.R;
30 
31 public class PlayControlsButton extends FrameLayout {
32     private static final float ALPHA_ENABLED = 1.0f;
33     private static final float ALPHA_DISABLED = 0.3f;
34 
35     private final ImageView mButton;
36     private final ImageView mIcon;
37     private final TextView mLabel;
38     private final long mFocusAnimationTimeMs;
39     private final int mIconColor;
40     private int mIconFocusedColor;
41 
PlayControlsButton(Context context)42     public PlayControlsButton(Context context) {
43         this(context, null);
44     }
45 
PlayControlsButton(Context context, AttributeSet attrs)46     public PlayControlsButton(Context context, AttributeSet attrs) {
47         this(context, attrs, 0);
48     }
49 
PlayControlsButton(Context context, AttributeSet attrs, int defStyleAttr)50     public PlayControlsButton(Context context, AttributeSet attrs, int defStyleAttr) {
51         this(context, attrs, defStyleAttr, 0);
52     }
53 
PlayControlsButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)54     public PlayControlsButton(Context context, AttributeSet attrs, int defStyleAttr,
55             int defStyleRes) {
56         super(context, attrs, defStyleAttr, defStyleRes);
57         inflate(context, R.layout.play_controls_button, this);
58         mButton = (ImageView) findViewById(R.id.button);
59         mIcon = (ImageView) findViewById(R.id.icon);
60         mLabel = (TextView) findViewById(R.id.label);
61         mFocusAnimationTimeMs = context.getResources().getInteger(integer.config_shortAnimTime);
62         mIconColor = context.getResources().getColor(R.color.play_controls_icon_color);
63         mIconFocusedColor = mIconColor;
64     }
65 
66     /**
67      * Sets the resource ID of the image to be displayed in the center of this control.
68      */
setImageResId(int imageResId)69     public void setImageResId(int imageResId) {
70         mIcon.setImageResource(imageResId);
71         // Since on foucus changing, icons' color should be switched with animation,
72         // as a result, selectors cannot be used to switch colors in this case.
73         mIcon.getDrawable().setTint(hasFocus() ? mIconFocusedColor : mIconColor);
74     }
75 
76     /**
77      * Sets an action which is to be run when the button is clicked.
78      */
setAction(final Runnable clickAction)79     public void setAction(final Runnable clickAction) {
80         mButton.setOnClickListener(new OnClickListener() {
81             @Override
82             public void onClick(View view) {
83                 clickAction.run();
84             }
85         });
86     }
87 
88     /**
89      * Sets the icon's color should change to when the button is on focus.
90      */
setFocusedIconColor(int color)91     public void setFocusedIconColor(int color) {
92         final ValueAnimator valueAnimator = ValueAnimator.ofArgb(mIconColor, color);
93         valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
94             @Override
95             public void onAnimationUpdate(final ValueAnimator animator) {
96                 mIcon.getDrawable().setTint((int) animator.getAnimatedValue());
97             }
98         });
99         valueAnimator.setDuration(mFocusAnimationTimeMs);
100         mButton.setOnFocusChangeListener(new View.OnFocusChangeListener() {
101             @Override
102             public void onFocusChange(View v, boolean hasFocus) {
103                 if (hasFocus) {
104                     valueAnimator.start();
105                 } else {
106                     valueAnimator.reverse();
107                 }
108             }
109         });
110         mIconFocusedColor = color;
111     }
112 
setLabel(String label)113     public void setLabel(String label) {
114         if (TextUtils.isEmpty(label)) {
115             mIcon.setVisibility(View.VISIBLE);
116             mLabel.setVisibility(View.GONE);
117         } else {
118             mIcon.setVisibility(View.GONE);
119             mLabel.setVisibility(View.VISIBLE);
120             mLabel.setText(label);
121         }
122     }
123 
hideRippleAnimation()124     public void hideRippleAnimation() {
125         mButton.getDrawable().jumpToCurrentState();
126     }
127 
128     @Override
setEnabled(boolean enabled)129     public void setEnabled(boolean enabled) {
130         super.setEnabled(enabled);
131         mButton.setEnabled(enabled);
132         mButton.setFocusable(enabled);
133         mIcon.setEnabled(enabled);
134         mIcon.setAlpha(enabled ? ALPHA_ENABLED : ALPHA_DISABLED);
135         mLabel.setEnabled(enabled);
136     }
137 }
138