• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
14 package android.support.v17.leanback.widget;
15 
16 import android.content.Context;
17 import android.graphics.Rect;
18 import android.util.AttributeSet;
19 import android.view.View;
20 import android.widget.FrameLayout;
21 
22 /**
23  * A ViewGroup for managing focus behavior between overlapping views.
24  */
25 public 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          */
onFocusSearch(View focused, int direction)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          */
onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect)47         boolean onRequestFocusInDescendants(int direction,
48                 Rect previouslyFocusedRect);
49         /**
50          * See {@link android.view.ViewGroup#requestChildFocus(
51          * android.view.View, android.view.View)}.
52          */
onRequestChildFocus(View child, View focused)53         void onRequestChildFocus(View child, View focused);
54     }
55 
BrowseFrameLayout(Context context)56     public BrowseFrameLayout(Context context) {
57         this(context, null, 0);
58     }
59 
BrowseFrameLayout(Context context, AttributeSet attrs)60     public BrowseFrameLayout(Context context, AttributeSet attrs) {
61         this(context, attrs, 0);
62     }
63 
BrowseFrameLayout(Context context, AttributeSet attrs, int defStyle)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      */
setOnFocusSearchListener(OnFocusSearchListener listener)74     public void setOnFocusSearchListener(OnFocusSearchListener listener) {
75         mListener = listener;
76     }
77 
78     /**
79      * Returns the {@link OnFocusSearchListener}.
80      */
getOnFocusSearchListener()81     public OnFocusSearchListener getOnFocusSearchListener() {
82         return mListener;
83     }
84 
85     /**
86      * Sets a {@link OnChildFocusListener}.
87      */
setOnChildFocusListener(OnChildFocusListener listener)88     public void setOnChildFocusListener(OnChildFocusListener listener) {
89         mOnChildFocusListener = listener;
90     }
91 
92     /**
93      * Returns the {@link OnChildFocusListener}.
94      */
getOnChildFocusListener()95     public OnChildFocusListener getOnChildFocusListener() {
96         return mOnChildFocusListener;
97     }
98 
99     @Override
onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect)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
focusSearch(View focused, int direction)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
requestChildFocus(View child, View focused)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