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