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 * A ViewGroup for managing focus behavior between overlapping views. 24 */ 25public class BrowseFrameLayout extends FrameLayout { 26 27 /** 28 * Interface for selecting a focused view in a BrowseFrameLayout when the system focus finder 29 * couldn't find a view 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 View onFocusSearch(View focused, int direction); 37 } 38 39 /** 40 * Interface for managing child focus in a BrowseFrameLayout. 41 */ 42 public interface OnChildFocusListener { 43 /** 44 * See {@link android.view.ViewGroup#onRequestFocusInDescendants( 45 * int, android.graphics.Rect)}. 46 */ 47 boolean onRequestFocusInDescendants(int direction, 48 Rect previouslyFocusedRect); 49 /** 50 * See {@link android.view.ViewGroup#requestChildFocus( 51 * android.view.View, android.view.View)}. 52 */ 53 void onRequestChildFocus(View child, View focused); 54 } 55 56 public BrowseFrameLayout(Context context) { 57 this(context, null, 0); 58 } 59 60 public BrowseFrameLayout(Context context, AttributeSet attrs) { 61 this(context, attrs, 0); 62 } 63 64 public BrowseFrameLayout(Context context, AttributeSet attrs, int defStyle) { 65 super(context, attrs, defStyle); 66 } 67 68 private OnFocusSearchListener mListener; 69 private OnChildFocusListener mOnChildFocusListener; 70 71 /** 72 * Sets a {@link OnFocusSearchListener}. 73 */ 74 public void setOnFocusSearchListener(OnFocusSearchListener listener) { 75 mListener = listener; 76 } 77 78 /** 79 * Returns the {@link OnFocusSearchListener}. 80 */ 81 public OnFocusSearchListener getOnFocusSearchListener() { 82 return mListener; 83 } 84 85 /** 86 * Sets a {@link OnChildFocusListener}. 87 */ 88 public void setOnChildFocusListener(OnChildFocusListener listener) { 89 mOnChildFocusListener = listener; 90 } 91 92 /** 93 * Returns the {@link OnChildFocusListener}. 94 */ 95 public OnChildFocusListener getOnChildFocusListener() { 96 return mOnChildFocusListener; 97 } 98 99 @Override 100 protected boolean onRequestFocusInDescendants(int direction, 101 Rect previouslyFocusedRect) { 102 if (mOnChildFocusListener != null) { 103 return mOnChildFocusListener.onRequestFocusInDescendants(direction, 104 previouslyFocusedRect); 105 } 106 return super.onRequestFocusInDescendants(direction, previouslyFocusedRect); 107 } 108 109 @Override 110 public View focusSearch(View focused, int direction) { 111 if (mListener != null) { 112 View view = mListener.onFocusSearch(focused, direction); 113 if (view != null) { 114 return view; 115 } 116 } 117 return super.focusSearch(focused, direction); 118 } 119 120 @Override 121 public void requestChildFocus(View child, View focused) { 122 super.requestChildFocus(child, focused); 123 if (mOnChildFocusListener != null) { 124 mOnChildFocusListener.onRequestChildFocus(child, focused); 125 } 126 } 127} 128