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