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.content.Context;
20import android.graphics.Rect;
21import android.util.AttributeSet;
22import android.view.KeyEvent;
23import android.view.View;
24import android.widget.LinearLayout;
25
26/**
27 * A LinearLayout that preserves the focused child view.
28 * @hide
29 */
30class PlaybackControlsRowView extends LinearLayout {
31    public interface OnUnhandledKeyListener {
32        /**
33         * Returns true if the key event should be consumed.
34         */
35        public boolean onUnhandledKey(KeyEvent event);
36    }
37
38    private OnUnhandledKeyListener mOnUnhandledKeyListener;
39
40    public PlaybackControlsRowView(Context context, AttributeSet attrs) {
41        super(context, attrs);
42    }
43
44    public PlaybackControlsRowView(Context context, AttributeSet attrs, int defStyle) {
45        super(context, attrs, defStyle);
46    }
47
48    public void setOnUnhandledKeyListener(OnUnhandledKeyListener listener) {
49         mOnUnhandledKeyListener = listener;
50    }
51
52    public OnUnhandledKeyListener getOnUnhandledKeyListener() {
53        return mOnUnhandledKeyListener;
54    }
55
56    @Override
57    public boolean dispatchKeyEvent(KeyEvent event) {
58        if (super.dispatchKeyEvent(event)) {
59            return true;
60        }
61        if (mOnUnhandledKeyListener != null && mOnUnhandledKeyListener.onUnhandledKey(event)) {
62            return true;
63        }
64        return false;
65    }
66
67    @Override
68    protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
69        final View focused = findFocus();
70        if (focused != null && focused.requestFocus(direction, previouslyFocusedRect)) {
71            return true;
72        }
73        return super.onRequestFocusInDescendants(direction, previouslyFocusedRect);
74    }
75}
76