aboutsummaryrefslogtreecommitdiff
path: root/src/com/android/tv/menu/MenuView.java
blob: ee0b036e4acc2732e5bb9d423b1218d854a2079e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.tv.menu;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewParent;
import android.view.ViewTreeObserver.OnGlobalFocusChangeListener;
import android.view.ViewTreeObserver.OnGlobalLayoutListener;
import android.widget.FrameLayout;

import com.android.tv.menu.Menu.MenuShowReason;

import java.util.ArrayList;
import java.util.List;

/**
 * A view that represents TV main menu.
 */
public class MenuView extends FrameLayout implements IMenuView {
    static final String TAG = MenuView.class.getSimpleName();
    static final boolean DEBUG = false;

    private final LayoutInflater mLayoutInflater;
    private final List<MenuRow> mMenuRows = new ArrayList<>();
    private final List<MenuRowView> mMenuRowViews = new ArrayList<>();

    @MenuShowReason private int mShowReason = Menu.REASON_NONE;

    private final MenuLayoutManager mLayoutManager;

    public MenuView(Context context) {
        this(context, null, 0);
    }

    public MenuView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MenuView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        mLayoutInflater = LayoutInflater.from(context);
        // Set hardware layer type for smooth animation of lots of views.
        setLayerType(LAYER_TYPE_HARDWARE, null);
        getViewTreeObserver().addOnGlobalFocusChangeListener(new OnGlobalFocusChangeListener() {
            @Override
            public void onGlobalFocusChanged(View oldFocus, View newFocus) {
                MenuRowView newParent = getParentMenuRowView(newFocus);
                if (newParent != null) {
                    if (DEBUG) Log.d(TAG, "Focus changed to " + newParent);
                    // When the row is selected, the row view itself has the focus because the row
                    // is collapsed. To make the child of the row have the focus, requestFocus()
                    // should be called again after the row is expanded. It's done in
                    // setSelectedPosition().
                    setSelectedPositionSmooth(mMenuRowViews.indexOf(newParent));
                }
            }
        });
        mLayoutManager = new MenuLayoutManager(context, this);
    }

    @Override
    public void setMenuRows(List<MenuRow> menuRows) {
        mMenuRows.clear();
        mMenuRows.addAll(menuRows);
        for (MenuRow row : menuRows) {
            MenuRowView view = createMenuRowView(row);
            mMenuRowViews.add(view);
            addView(view);
        }
        mLayoutManager.setMenuRowsAndViews(mMenuRows, mMenuRowViews);
    }

    private MenuRowView createMenuRowView(MenuRow row) {
        MenuRowView view = (MenuRowView) mLayoutInflater.inflate(row.getLayoutResId(), this, false);
        view.onBind(row);
        row.setMenuRowView(view);
        return view;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        mLayoutManager.layout(left, top, right, bottom);
    }

    @Override
    public void onShow(@MenuShowReason int reason, String rowIdToSelect,
            final Runnable runnableAfterShow) {
        if (DEBUG) {
            Log.d(TAG, "onShow(reason=" + reason + ", rowIdToSelect=" + rowIdToSelect + ")");
        }
        mShowReason = reason;
        if (getVisibility() == VISIBLE) {
            if (rowIdToSelect != null) {
                int position = getItemPosition(rowIdToSelect);
                if (position >= 0) {
                    MenuRowView rowView = mMenuRowViews.get(position);
                    rowView.initialize(reason);
                    setSelectedPosition(position);
                }
            }
            return;
        }
        initializeChildren();
        update(true);
        int position = getItemPosition(rowIdToSelect);
        if (position == -1 || !mMenuRows.get(position).isVisible()) {
            // Channels row is always visible.
            position = getItemPosition(ChannelsRow.ID);
        }
        setSelectedPosition(position);
        // Change the visibility as late as possible to avoid the unnecessary animation.
        setVisibility(VISIBLE);
        // Make the selected row have the focus.
        requestFocus();
        if (runnableAfterShow != null) {
            getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    // Start show animation after layout finishes for smooth animation because the
                    // layout can take long time.
                    runnableAfterShow.run();
                }
            });
        }
        mLayoutManager.onMenuShow();
    }

    @Override
    public void onHide() {
        if (getVisibility() == GONE) {
            return;
        }
        mLayoutManager.onMenuHide();
        setVisibility(GONE);
    }

    @Override
    public boolean isVisible() {
        return getVisibility() == VISIBLE;
    }

    @Override
    public boolean update(boolean menuActive) {
        if (menuActive) {
            for (MenuRow row : mMenuRows) {
                row.update();
            }
            mLayoutManager.onMenuRowUpdated();
            return true;
        }
        return false;
    }

    @Override
    public boolean update(String rowId, boolean menuActive) {
        if (menuActive) {
            MenuRow row = getMenuRow(rowId);
            if (row != null) {
                row.update();
                mLayoutManager.onMenuRowUpdated();
                return true;
            }
        }
        return false;
    }

    @Override
    protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
        int selectedPosition = mLayoutManager.getSelectedPosition();
        // When the menu shows up, the selected row should have focus.
        if (selectedPosition >= 0 && selectedPosition < mMenuRowViews.size()) {
            return mMenuRowViews.get(selectedPosition).requestFocus();
        }
        return super.onRequestFocusInDescendants(direction, previouslyFocusedRect);
    }

    @Override
    public void focusableViewAvailable(View v) {
        // Workaround of b/30788222 and b/32074688.
        // The re-layout of RecyclerView gives the focus to the card view even when the menu is not
        // visible. Don't report focusable view when the menu is not visible.
        if (getVisibility() == VISIBLE) {
            super.focusableViewAvailable(v);
        }
    }

    private void setSelectedPosition(int position) {
        mLayoutManager.setSelectedPosition(position);
    }

    private void setSelectedPositionSmooth(int position) {
        mLayoutManager.setSelectedPositionSmooth(position);
    }

    private void initializeChildren() {
        for (MenuRowView view : mMenuRowViews) {
            view.initialize(mShowReason);
        }
    }

    private MenuRow getMenuRow(String rowId) {
        for (MenuRow item : mMenuRows) {
            if (rowId.equals(item.getId())) {
                return item;
            }
        }
        return null;
    }

    private int getItemPosition(String rowIdToSelect) {
        if (rowIdToSelect == null) {
            return -1;
        }
        int position = 0;
        for (MenuRow item : mMenuRows) {
            if (rowIdToSelect.equals(item.getId())) {
                return position;
            }
            ++position;
        }
        return -1;
    }

    @Override
    public View focusSearch(View focused, int direction) {
        // The bounds of the views move and overlap with each other during the animation. In this
        // situation, the framework can't perform the correct focus navigation. So the menu view
        // should search by itself.
        if (direction == View.FOCUS_UP) {
            View newView = super.focusSearch(focused, direction);
            MenuRowView oldfocusedParent = getParentMenuRowView(focused);
            MenuRowView newFocusedParent = getParentMenuRowView(newView);
            int selectedPosition = mLayoutManager.getSelectedPosition();
            if (newFocusedParent != oldfocusedParent) {
                // The focus leaves from the current menu row view.
                for (int i = selectedPosition - 1; i >= 0; --i) {
                    MenuRowView view = mMenuRowViews.get(i);
                    if (view.getVisibility() == View.VISIBLE) {
                        return view;
                    }
                }
            }
            return newView;
        } else if (direction == View.FOCUS_DOWN) {
            View newView = super.focusSearch(focused, direction);
            MenuRowView oldfocusedParent = getParentMenuRowView(focused);
            MenuRowView newFocusedParent = getParentMenuRowView(newView);
            int selectedPosition = mLayoutManager.getSelectedPosition();
            if (newFocusedParent != oldfocusedParent) {
                // The focus leaves from the current menu row view.
                int count = mMenuRowViews.size();
                for (int i = selectedPosition + 1; i < count; ++i) {
                    MenuRowView view = mMenuRowViews.get(i);
                    if (view.getVisibility() == View.VISIBLE) {
                        return view;
                    }
                }
            }
            return newView;
        }
        return super.focusSearch(focused, direction);
    }

    private MenuRowView getParentMenuRowView(View view) {
        if (view == null) {
            return null;
        }
        ViewParent parent = view.getParent();
        if (parent == MenuView.this) {
            return (MenuRowView) view;
        }
        if (parent instanceof View) {
            return getParentMenuRowView((View) parent);
        }
        return null;
    }
}