1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5  * in compliance with the License. You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software distributed under the License
10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11  * or implied. See the License for the specific language governing permissions and limitations under
12  * the License.
13  */
14 package androidx.leanback.app;
15 
16 import android.content.Context;
17 import android.util.AttributeSet;
18 import android.view.View;
19 import android.view.ViewGroup;
20 import android.widget.LinearLayout;
21 
22 import androidx.leanback.widget.Util;
23 
24 /**
25  * Utility class used by GuidedStepFragment to disable focus out left/right.
26  */
27 class GuidedStepRootLayout extends LinearLayout {
28 
29     private boolean mFocusOutStart = false;
30     private boolean mFocusOutEnd = false;
31 
GuidedStepRootLayout(Context context, AttributeSet attrs)32     public GuidedStepRootLayout(Context context, AttributeSet attrs) {
33         super(context, attrs);
34     }
35 
GuidedStepRootLayout(Context context, AttributeSet attrs, int defStyleAttr)36     public GuidedStepRootLayout(Context context, AttributeSet attrs, int defStyleAttr) {
37         super(context, attrs, defStyleAttr);
38     }
39 
setFocusOutStart(boolean focusOutStart)40     public void setFocusOutStart(boolean focusOutStart) {
41         mFocusOutStart = focusOutStart;
42     }
43 
setFocusOutEnd(boolean focusOutEnd)44     public void setFocusOutEnd(boolean focusOutEnd) {
45         mFocusOutEnd = focusOutEnd;
46     }
47 
48     @Override
focusSearch(View focused, int direction)49     public View focusSearch(View focused, int direction) {
50         View newFocus = super.focusSearch(focused, direction);
51         if (direction == FOCUS_LEFT || direction == FOCUS_RIGHT) {
52             if (Util.isDescendant(this, newFocus)) {
53                 return newFocus;
54             }
55             if (getLayoutDirection() == ViewGroup.LAYOUT_DIRECTION_LTR
56                     ? direction == FOCUS_LEFT : direction == FOCUS_RIGHT) {
57                 if (!mFocusOutStart) {
58                     return focused;
59                 }
60             } else {
61                 if (!mFocusOutEnd) {
62                     return focused;
63                 }
64             }
65         }
66         return newFocus;
67     }
68 }
69