1b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout/*
2b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout * Copyright (C) 2015 The Android Open Source Project
3b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout *
4b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout * Licensed under the Apache License, Version 2.0 (the "License");
5b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout * you may not use this file except in compliance with the License.
6b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout * You may obtain a copy of the License at
7b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout *
8b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout *      http://www.apache.org/licenses/LICENSE-2.0
9b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout *
10b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout * Unless required by applicable law or agreed to in writing, software
11b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout * distributed under the License is distributed on an "AS IS" BASIS,
12b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout * See the License for the specific language governing permissions and
14b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout * limitations under the License
15b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout */
16b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout
17b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stoutpackage android.support.v17.leanback.widget;
18b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout
19b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stoutimport android.content.Context;
20b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stoutimport android.graphics.Rect;
21b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stoutimport android.util.AttributeSet;
2260bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stoutimport android.view.KeyEvent;
23b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stoutimport android.view.View;
24b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stoutimport android.widget.LinearLayout;
25b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout
26b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout/**
27b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout * A LinearLayout that preserves the focused child view.
28b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout */
29b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stoutclass PlaybackControlsRowView extends LinearLayout {
3060bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout    public interface OnUnhandledKeyListener {
3160bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout        /**
3260bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout         * Returns true if the key event should be consumed.
3360bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout         */
3460bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout        public boolean onUnhandledKey(KeyEvent event);
3560bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout    }
3660bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout
3760bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout    private OnUnhandledKeyListener mOnUnhandledKeyListener;
3860bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout
39b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout    public PlaybackControlsRowView(Context context, AttributeSet attrs) {
40b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout        super(context, attrs);
41b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout    }
42b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout
43b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout    public PlaybackControlsRowView(Context context, AttributeSet attrs, int defStyle) {
44b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout        super(context, attrs, defStyle);
45b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout    }
46b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout
4760bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout    public void setOnUnhandledKeyListener(OnUnhandledKeyListener listener) {
4860bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout         mOnUnhandledKeyListener = listener;
4960bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout    }
5060bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout
5160bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout    public OnUnhandledKeyListener getOnUnhandledKeyListener() {
5260bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout        return mOnUnhandledKeyListener;
5360bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout    }
5460bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout
5560bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout    @Override
5660bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout    public boolean dispatchKeyEvent(KeyEvent event) {
5760bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout        if (super.dispatchKeyEvent(event)) {
5860bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout            return true;
5960bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout        }
6060bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout        if (mOnUnhandledKeyListener != null && mOnUnhandledKeyListener.onUnhandledKey(event)) {
6160bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout            return true;
6260bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout        }
6360bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout        return false;
6460bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout    }
6560bb6af2e336072921f5d3c3861e86b3cc6241b3Craig Stout
66b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout    @Override
67b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout    protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
68b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout        final View focused = findFocus();
69b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout        if (focused != null && focused.requestFocus(direction, previouslyFocusedRect)) {
70b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout            return true;
71b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout        }
72b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout        return super.onRequestFocusInDescendants(direction, previouslyFocusedRect);
73b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout    }
74b682c5a20aa0a383ea2b3e7d639e51b3e7771bcdCraig Stout}
75