TitleHelper.java revision e7246ef136ed686d8caf339d4d1fd8e37b499c6a
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
17package android.support.v17.leanback.widget;
18
19import android.support.v17.leanback.transition.LeanbackTransitionHelper;
20import android.support.v17.leanback.transition.TransitionHelper;
21import android.support.v4.view.ViewCompat;
22import android.view.View;
23import android.view.ViewGroup;
24
25/**
26 * Helper for managing {@link android.support.v17.leanback.widget.TitleView}, including
27 * transitions and focus movement.
28 * Assumes the TitleView is overlayed on the topmost portion of the scene root view.
29 */
30public class TitleHelper {
31
32    private ViewGroup mSceneRoot;
33    private TitleView mTitleView;
34    private Object mTitleUpTransition;
35    private Object mTitleDownTransition;
36    private Object mSceneWithTitle;
37    private Object mSceneWithoutTitle;
38
39    static TransitionHelper sTransitionHelper = TransitionHelper.getInstance();
40
41    // When moving focus off the TitleView, this focus search listener assumes that the view that
42    // should take focus comes before the TitleView in a focus search starting at the scene root.
43    private final BrowseFrameLayout.OnFocusSearchListener mOnFocusSearchListener =
44            new BrowseFrameLayout.OnFocusSearchListener() {
45        @Override
46        public View onFocusSearch(View focused, int direction) {
47            if (focused != mTitleView && direction == View.FOCUS_UP) {
48                return mTitleView;
49            }
50            final boolean isRtl = ViewCompat.getLayoutDirection(focused) ==
51                    View.LAYOUT_DIRECTION_RTL;
52            final int forward = isRtl ? View.FOCUS_LEFT : View.FOCUS_RIGHT;
53            if (mTitleView.hasFocus() && direction == View.FOCUS_DOWN || direction == forward) {
54                return mSceneRoot;
55            }
56            return null;
57        }
58    };
59
60    public TitleHelper(ViewGroup sceneRoot, TitleView titleView) {
61        if (sceneRoot == null || titleView == null) {
62            throw new IllegalArgumentException("Views may not be null");
63        }
64        mSceneRoot = sceneRoot;
65        mTitleView = titleView;
66        createTransitions();
67    }
68
69    private void createTransitions() {
70        mTitleUpTransition = LeanbackTransitionHelper.loadTitleOutTransition(
71                mSceneRoot.getContext(), sTransitionHelper);
72        mTitleDownTransition = LeanbackTransitionHelper.loadTitleInTransition(
73                mSceneRoot.getContext(), sTransitionHelper);
74        mSceneWithTitle = sTransitionHelper.createScene(mSceneRoot, new Runnable() {
75            @Override
76            public void run() {
77                mTitleView.setVisibility(View.VISIBLE);
78            }
79        });
80        mSceneWithoutTitle = sTransitionHelper.createScene(mSceneRoot, new Runnable() {
81            @Override
82            public void run() {
83                mTitleView.setVisibility(View.INVISIBLE);
84            }
85        });
86    }
87
88    /**
89     * Shows the title.
90     */
91    public void showTitle(boolean show) {
92        if (show) {
93            sTransitionHelper.runTransition(mSceneWithTitle, mTitleDownTransition);
94        } else {
95            sTransitionHelper.runTransition(mSceneWithoutTitle, mTitleUpTransition);
96        }
97    }
98
99    /**
100     * Returns the scene root ViewGroup.
101     */
102    public ViewGroup getSceneRoot() {
103        return mSceneRoot;
104    }
105
106    /**
107     * Returns the {@link TitleView}
108     */
109    public TitleView getTitleView() {
110        return mTitleView;
111    }
112
113    /**
114     * Returns a
115     * {@link android.support.v17.leanback.widget.BrowseFrameLayout.OnFocusSearchListener} which
116     * may be used to manage focus switching between the title view and scene root.
117     */
118    public BrowseFrameLayout.OnFocusSearchListener getOnFocusSearchListener() {
119        return mOnFocusSearchListener;
120    }
121}
122