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.util.AttributeSet;
21 import android.util.Log;
22 import android.view.accessibility.AccessibilityNodeInfo;
23 import android.widget.ImageView;
24 import android.widget.RelativeLayout;
25 import android.widget.TextView;
26 import com.android.tv.R;
27 
28 /** A view to render an item of TV options. */
29 public class ActionCardView extends RelativeLayout implements ItemListRowView.CardView<MenuAction> {
30     private static final String TAG = MenuView.TAG;
31     private static final boolean DEBUG = MenuView.DEBUG;
32 
33     private static final float OPACITY_DISABLED = 0.3f;
34     private static final float OPACITY_ENABLED = 1.0f;
35 
36     private ImageView mIconView;
37     private TextView mLabelView;
38     private TextView mStateView;
39 
ActionCardView(Context context)40     public ActionCardView(Context context) {
41         this(context, null);
42     }
43 
ActionCardView(Context context, AttributeSet attrs)44     public ActionCardView(Context context, AttributeSet attrs) {
45         this(context, attrs, 0);
46     }
47 
ActionCardView(Context context, AttributeSet attrs, int defStyle)48     public ActionCardView(Context context, AttributeSet attrs, int defStyle) {
49         super(context, attrs, defStyle);
50     }
51 
52     @Override
onFinishInflate()53     protected void onFinishInflate() {
54         super.onFinishInflate();
55         mIconView = (ImageView) findViewById(R.id.action_card_icon);
56         mLabelView = (TextView) findViewById(R.id.action_card_label);
57         mStateView = (TextView) findViewById(R.id.action_card_state);
58     }
59 
60     @Override
onBind(MenuAction action, boolean selected)61     public void onBind(MenuAction action, boolean selected) {
62         if (DEBUG) {
63             Log.d(TAG, "onBind: action=" + action.getActionName(getContext()));
64         }
65         mIconView.setImageDrawable(action.getDrawable(getContext()));
66         mLabelView.setText(action.getActionName(getContext()));
67         mStateView.setText(action.getActionDescription());
68         if (action.isEnabled()) {
69             setEnabled(true);
70             setFocusable(true);
71             mIconView.setAlpha(OPACITY_ENABLED);
72             mLabelView.setAlpha(OPACITY_ENABLED);
73             mStateView.setAlpha(OPACITY_ENABLED);
74         } else {
75             setEnabled(false);
76             setFocusable(false);
77             mIconView.setAlpha(OPACITY_DISABLED);
78             mLabelView.setAlpha(OPACITY_DISABLED);
79             mStateView.setAlpha(OPACITY_DISABLED);
80         }
81     }
82 
83     @Override
onSelected()84     public void onSelected() {
85         if (DEBUG) {
86             Log.d(TAG, "onSelected: action=" + mLabelView.getText());
87         }
88     }
89 
90     @Override
onDeselected()91     public void onDeselected() {
92         if (DEBUG) {
93             Log.d(TAG, "onDeselected: action=" + mLabelView.getText());
94         }
95     }
96 
97     /** Request focus and accessibility focus on card view. */
98     @Override
requestFocusWithAccessibility()99     public boolean requestFocusWithAccessibility() {
100         return requestFocus() &&
101                 performAccessibilityAction(AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS, null);
102     }
103 
104     @Override
onRecycled()105     public void onRecycled() {}
106 }
107