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