• 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.content.Context;
20 import android.content.res.Resources;
21 import android.graphics.Rect;
22 import android.support.annotation.NonNull;
23 import android.util.AttributeSet;
24 import android.util.Log;
25 import android.util.TypedValue;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.view.accessibility.AccessibilityEvent;
29 import android.widget.LinearLayout;
30 import android.widget.TextView;
31 import com.android.tv.R;
32 import com.android.tv.menu.Menu.MenuShowReason;
33 
34 public abstract class MenuRowView extends LinearLayout {
35     private static final String TAG = "MenuRowView";
36     private static final boolean DEBUG = false;
37 
38     private TextView mTitleView;
39     private View mContentsView;
40 
41     private final float mTitleViewAlphaDeselected;
42     private final float mTitleViewScaleSelected;
43 
44     /**
45      * The lastly focused view. It is used to keep the focus while navigating the menu rows and
46      * reset when the menu is popped up.
47      */
48     private View mLastFocusView;
49 
50     private MenuRow mRow;
51 
52     private final OnFocusChangeListener mOnFocusChangeListener =
53             new OnFocusChangeListener() {
54                 @Override
55                 public void onFocusChange(View v, boolean hasFocus) {
56                     onChildFocusChange(v, hasFocus);
57                 }
58             };
59 
60     /** Returns the alpha value of the title view when it's deselected. */
getTitleViewAlphaDeselected()61     public float getTitleViewAlphaDeselected() {
62         return mTitleViewAlphaDeselected;
63     }
64 
65     /** Returns the scale value of the title view when it's selected. */
getTitleViewScaleSelected()66     public float getTitleViewScaleSelected() {
67         return mTitleViewScaleSelected;
68     }
69 
MenuRowView(Context context)70     public MenuRowView(Context context) {
71         this(context, null);
72     }
73 
MenuRowView(Context context, AttributeSet attrs)74     public MenuRowView(Context context, AttributeSet attrs) {
75         this(context, attrs, 0);
76     }
77 
MenuRowView(Context context, AttributeSet attrs, int defStyleAttr)78     public MenuRowView(Context context, AttributeSet attrs, int defStyleAttr) {
79         this(context, attrs, defStyleAttr, 0);
80     }
81 
MenuRowView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)82     public MenuRowView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
83         super(context, attrs, defStyleAttr, defStyleRes);
84         Resources res = context.getResources();
85         TypedValue outValue = new TypedValue();
86         res.getValue(R.dimen.menu_row_title_alpha_deselected, outValue, true);
87         mTitleViewAlphaDeselected = outValue.getFloat();
88         float textSizeSelected =
89                 res.getDimensionPixelSize(R.dimen.menu_row_title_text_size_selected);
90         float textSizeDeselected =
91                 res.getDimensionPixelSize(R.dimen.menu_row_title_text_size_deselected);
92         mTitleViewScaleSelected = textSizeSelected / textSizeDeselected;
93         this.setAccessibilityDelegate(
94                 new AccessibilityDelegate() {
95                     @Override
96                     public void sendAccessibilityEvent(View host, int eventType) {
97                         super.sendAccessibilityEvent(host, eventType);
98                         if ((eventType == AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED ||
99                                 eventType == AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED) &&
100                                 !mRow.isReselected()) {
101                             requestChildFocus();
102                         }
103                     }
104                 }
105         );
106     }
107 
108     @Override
onFinishInflate()109     protected void onFinishInflate() {
110         super.onFinishInflate();
111         mTitleView = (TextView) findViewById(R.id.title);
112         mContentsView = findViewById(getContentsViewId());
113         if (mContentsView.isFocusable()) {
114             mContentsView.setOnFocusChangeListener(mOnFocusChangeListener);
115         }
116         if (mContentsView instanceof ViewGroup) {
117             setOnFocusChangeListenerToChildren((ViewGroup) mContentsView);
118         }
119         // Make contents view invisible in order that the view participates in the initial layout.
120         // The visibility is set to GONE after the first layout finishes.
121         // If not, we can't see the contents view animation for the first time it is shown.
122         // TODO: Find a better way to resolve this issue.
123         mContentsView.setVisibility(INVISIBLE);
124     }
125 
setOnFocusChangeListenerToChildren(ViewGroup parent)126     private void setOnFocusChangeListenerToChildren(ViewGroup parent) {
127         int childCount = parent.getChildCount();
128         for (int i = 0; i < childCount; ++i) {
129             View child = parent.getChildAt(i);
130             if (child.isFocusable()) {
131                 child.setOnFocusChangeListener(mOnFocusChangeListener);
132             }
133             if (child instanceof ViewGroup) {
134                 setOnFocusChangeListenerToChildren((ViewGroup) child);
135             }
136         }
137     }
138 
getContentsViewId()139     protected abstract int getContentsViewId();
140 
141     /** Returns the title view. */
getTitleView()142     public final TextView getTitleView() {
143         return mTitleView;
144     }
145 
146     /** Returns the contents view. */
getContentsView()147     public final View getContentsView() {
148         return mContentsView;
149     }
150 
151     /**
152      * Initialize this view. e.g. Set the initial selection. This method is called when the main
153      * menu is visible. Subclass of {@link MenuRowView} should override this to set correct
154      * mLastFocusView.
155      *
156      * @param reason A reason why this is initialized. See {@link MenuShowReason}
157      */
initialize(@enuShowReason int reason)158     public void initialize(@MenuShowReason int reason) {
159         mLastFocusView = null;
160     }
161 
getMenu()162     protected Menu getMenu() {
163         return mRow == null ? null : mRow.getMenu();
164     }
165 
onBind(MenuRow row)166     public void onBind(MenuRow row) {
167         if (DEBUG) Log.d(TAG, "onBind: row=" + row);
168         mRow = row;
169         mTitleView.setText(row.getTitle());
170     }
171 
172     @Override
onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect)173     protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
174         // Expand view here so initial focused item can be shown.
175         return getInitialFocusView().requestFocus();
176     }
177 
178     @NonNull
getInitialFocusView()179     private View getInitialFocusView() {
180         if (mLastFocusView == null) {
181             return mContentsView;
182         }
183         return mLastFocusView;
184     }
185 
186     /**
187      * Sets the view which needs to have focus when this row appears. Subclasses should call this in
188      * {@link #initialize} if needed.
189      */
setInitialFocusView(@onNull View v)190     protected void setInitialFocusView(@NonNull View v) {
191         mLastFocusView = v;
192     }
193 
194     /** Subclasses should implement this to request focus on child. */
requestChildFocus()195     protected abstract void requestChildFocus();
196 
197     /**
198      * Called when the focus of a child view is changed. The inherited class should override this
199      * method instead of calling {@link
200      * android.view.View#setOnFocusChangeListener(android.view.View.OnFocusChangeListener)}.
201      */
onChildFocusChange(View v, boolean hasFocus)202     protected void onChildFocusChange(View v, boolean hasFocus) {
203         if (hasFocus) {
204             mLastFocusView = v;
205         }
206     }
207 
208     /** Returns the ID of row object bound to this view. */
getRowId()209     public String getRowId() {
210         return mRow == null ? null : mRow.getId();
211     }
212 
213     /**
214      * Called when this row is selected.
215      *
216      * @param showTitle If {@code true}, the title is not hidden immediately after the row is
217      *     selected even though hideTitleWhenSelected() is {@code true}.
218      */
onSelected(boolean showTitle)219     public void onSelected(boolean showTitle) {
220         if (mRow.hideTitleWhenSelected() && !showTitle) {
221             // Title view should participate in the layout even though it is not visible.
222             mTitleView.setVisibility(INVISIBLE);
223         } else {
224             mTitleView.setVisibility(VISIBLE);
225             mTitleView.setAlpha(1.0f);
226             mTitleView.setScaleX(mTitleViewScaleSelected);
227             mTitleView.setScaleY(mTitleViewScaleSelected);
228         }
229         // Making the content view visible will cause it to set a focus item
230         // So we store mLastFocusView and reset it
231         View lastFocusView = mLastFocusView;
232         mContentsView.setVisibility(VISIBLE);
233         mLastFocusView = lastFocusView;
234     }
235 
236     /** Called when this row is deselected. */
onDeselected()237     public void onDeselected() {
238         mTitleView.setVisibility(VISIBLE);
239         mTitleView.setAlpha(mTitleViewAlphaDeselected);
240         mTitleView.setScaleX(1.0f);
241         mTitleView.setScaleY(1.0f);
242         mContentsView.setVisibility(GONE);
243     }
244 
245     /** Returns the preferred height of the contents view. The top/bottom padding is excluded. */
getPreferredContentsHeight()246     public int getPreferredContentsHeight() {
247         return mRow.getHeight();
248     }
249 }
250