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