1/* 2 * Copyright (C) 2017 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 static android.support.annotation.RestrictTo.Scope.LIBRARY_GROUP; 20 21import android.content.Context; 22import android.graphics.Rect; 23import android.support.annotation.RestrictTo; 24import android.support.v17.leanback.R; 25import android.util.AttributeSet; 26import android.view.FocusFinder; 27import android.view.KeyEvent; 28import android.view.View; 29import android.view.ViewGroup; 30import android.widget.LinearLayout; 31 32/** 33 * View for PlaybackTransportRowPresenter that has a custom focusSearch. 34 * @hide 35 */ 36@RestrictTo(LIBRARY_GROUP) 37public class PlaybackTransportRowView extends LinearLayout { 38 39 /** 40 * @hide 41 */ 42 @RestrictTo(LIBRARY_GROUP) 43 public interface OnUnhandledKeyListener { 44 /** 45 * Returns true if the key event should be consumed. 46 */ 47 boolean onUnhandledKey(KeyEvent event); 48 } 49 50 private OnUnhandledKeyListener mOnUnhandledKeyListener; 51 52 public PlaybackTransportRowView(Context context, AttributeSet attrs) { 53 super(context, attrs); 54 } 55 56 public PlaybackTransportRowView(Context context, AttributeSet attrs, int defStyle) { 57 super(context, attrs, defStyle); 58 } 59 60 void setOnUnhandledKeyListener(OnUnhandledKeyListener listener) { 61 mOnUnhandledKeyListener = listener; 62 } 63 64 OnUnhandledKeyListener getOnUnhandledKeyListener() { 65 return mOnUnhandledKeyListener; 66 } 67 68 @Override 69 public boolean dispatchKeyEvent(KeyEvent event) { 70 if (super.dispatchKeyEvent(event)) { 71 return true; 72 } 73 return mOnUnhandledKeyListener != null && mOnUnhandledKeyListener.onUnhandledKey(event); 74 } 75 76 @Override 77 protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) { 78 final View focused = findFocus(); 79 if (focused != null && focused.requestFocus(direction, previouslyFocusedRect)) { 80 return true; 81 } 82 View progress = findViewById(R.id.playback_progress); 83 if (progress != null && progress.isFocusable()) { 84 if (progress.requestFocus(direction, previouslyFocusedRect)) { 85 return true; 86 } 87 } 88 return super.onRequestFocusInDescendants(direction, previouslyFocusedRect); 89 } 90 91 @Override 92 public View focusSearch(View focused, int direction) { 93 // when focusSearch vertically, return the next immediate focusable child 94 if (focused != null) { 95 if (direction == View.FOCUS_UP) { 96 int index = indexOfChild(getFocusedChild()); 97 for (index = index - 1; index >= 0; index--) { 98 View view = getChildAt(index); 99 if (view.hasFocusable()) { 100 return view; 101 } 102 } 103 } else if (direction == View.FOCUS_DOWN) { 104 int index = indexOfChild(getFocusedChild()); 105 for (index = index + 1; index < getChildCount(); index++) { 106 View view = getChildAt(index); 107 if (view.hasFocusable()) { 108 return view; 109 } 110 } 111 } else if (direction == View.FOCUS_LEFT || direction == View.FOCUS_RIGHT) { 112 if (getFocusedChild() instanceof ViewGroup) { 113 return FocusFinder.getInstance().findNextFocus( 114 (ViewGroup) getFocusedChild(), focused, direction); 115 } 116 } 117 } 118 return super.focusSearch(focused, direction); 119 } 120 121 @Override 122 public boolean hasOverlappingRendering() { 123 return false; 124 } 125} 126