BrowseFrameLayout.java revision e7246ef136ed686d8caf339d4d1fd8e37b499c6a
1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14package android.support.v17.leanback.widget; 15 16import android.content.Context; 17import android.graphics.Rect; 18import android.util.AttributeSet; 19import android.view.View; 20import android.widget.FrameLayout; 21 22/** 23 * Top level implementation viewgroup for managing focus behavior between overlapping views. 24 */ 25public class BrowseFrameLayout extends FrameLayout { 26 27 /** 28 * Interface for selecting a focused view when the system focus finder couldn't find a view 29 * to focus. 30 */ 31 public interface OnFocusSearchListener { 32 /** 33 * Returns the view where focus should be requested given the current focused view and 34 * the direction of focus search. 35 */ 36 public View onFocusSearch(View focused, int direction); 37 } 38 39 /** 40 * Interface for managing child focus. 41 */ 42 public interface OnChildFocusListener { 43 public boolean onRequestFocusInDescendants(int direction, 44 Rect previouslyFocusedRect); 45 public void onRequestChildFocus(View child, View focused); 46 } 47 48 public BrowseFrameLayout(Context context) { 49 this(context, null, 0); 50 } 51 52 public BrowseFrameLayout(Context context, AttributeSet attrs) { 53 this(context, attrs, 0); 54 } 55 56 public BrowseFrameLayout(Context context, AttributeSet attrs, int defStyle) { 57 super(context, attrs, defStyle); 58 } 59 60 private OnFocusSearchListener mListener; 61 private OnChildFocusListener mOnChildFocusListener; 62 63 /** 64 * Sets an {@link OnFocusSearchListener}. 65 */ 66 public void setOnFocusSearchListener(OnFocusSearchListener listener) { 67 mListener = listener; 68 } 69 70 /** 71 * Returns the {@link OnFocusSearchListener}. 72 */ 73 public OnFocusSearchListener getOnFocusSearchListener() { 74 return mListener; 75 } 76 77 /** 78 * Sets an {@link OnChildFocusListener}. 79 */ 80 public void setOnChildFocusListener(OnChildFocusListener listener) { 81 mOnChildFocusListener = listener; 82 } 83 84 /** 85 * Returns the {@link OnChildFocusListener}. 86 */ 87 public OnChildFocusListener getOnChildFocusListener() { 88 return mOnChildFocusListener; 89 } 90 91 @Override 92 protected boolean onRequestFocusInDescendants(int direction, 93 Rect previouslyFocusedRect) { 94 if (mOnChildFocusListener != null) { 95 return mOnChildFocusListener.onRequestFocusInDescendants(direction, 96 previouslyFocusedRect); 97 } 98 return super.onRequestFocusInDescendants(direction, previouslyFocusedRect); 99 } 100 101 @Override 102 public View focusSearch(View focused, int direction) { 103 if (mListener != null) { 104 View view = mListener.onFocusSearch(focused, direction); 105 if (view != null) { 106 return view; 107 } 108 } 109 return super.focusSearch(focused, direction); 110 } 111 112 @Override 113 public void requestChildFocus(View child, View focused) { 114 super.requestChildFocus(child, focused); 115 if (mOnChildFocusListener != null) { 116 mOnChildFocusListener.onRequestChildFocus(child, focused); 117 } 118 } 119} 120