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