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 browse to manage transitions between
24 * browse sub fragments.
25 * @hide
26 */
27public class BrowseFrameLayout extends FrameLayout {
28
29    public interface OnFocusSearchListener {
30        public View onFocusSearch(View focused, int direction);
31    }
32
33    public interface OnChildFocusListener {
34        public boolean onRequestFocusInDescendants(int direction,
35                Rect previouslyFocusedRect);
36        public void onRequestChildFocus(View child, View focused);
37    }
38
39    public BrowseFrameLayout(Context context) {
40        this(context, null, 0);
41    }
42
43    public BrowseFrameLayout(Context context, AttributeSet attrs) {
44        this(context, attrs, 0);
45    }
46
47    public BrowseFrameLayout(Context context, AttributeSet attrs, int defStyle) {
48        super(context, attrs, defStyle);
49    }
50
51    private OnFocusSearchListener mListener;
52    private OnChildFocusListener mOnChildFocusListener;
53
54    public void setOnFocusSearchListener(OnFocusSearchListener listener) {
55        mListener = listener;
56    }
57
58    public void setOnChildFocusListener(OnChildFocusListener listener) {
59        mOnChildFocusListener = listener;
60    }
61
62    @Override
63    protected boolean onRequestFocusInDescendants(int direction,
64            Rect previouslyFocusedRect) {
65        if (mOnChildFocusListener != null) {
66            return mOnChildFocusListener.onRequestFocusInDescendants(direction,
67                    previouslyFocusedRect);
68        }
69        return super.onRequestFocusInDescendants(direction, previouslyFocusedRect);
70    }
71
72    @Override
73    public View focusSearch(View focused, int direction) {
74        if (mListener != null) {
75            View view = mListener.onFocusSearch(focused, direction);
76            if (view != null) {
77                return view;
78            }
79        }
80        return super.focusSearch(focused, direction);
81    }
82
83    @Override
84    public void requestChildFocus(View child, View focused) {
85        super.requestChildFocus(child, focused);
86        if (mOnChildFocusListener != null) {
87            mOnChildFocusListener.onRequestChildFocus(child, focused);
88        }
89    }
90}
91