1 /*
2  * Copyright 2020 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.tab;
18 
19 import android.content.Context;
20 import android.util.AttributeSet;
21 import android.view.KeyEvent;
22 import android.view.MotionEvent;
23 
24 import androidx.viewpager.widget.ViewPager;
25 
26 import org.jspecify.annotations.NonNull;
27 import org.jspecify.annotations.Nullable;
28 
29 /**
30  * A viewpager with touch and key event handling disabled by default.
31  *
32  * <p>Key events handling is disabled by default as with the behaviour of viewpager the fragments
33  * can change when DPAD keys are pressed and focus is on the content inside the {@link ViewPager}.
34  * This is not desirable for a top navigation bar. The fragments should preferably change only
35  * when the focused tab changes.
36  */
37 public class LeanbackViewPager extends ViewPager {
38 
39     private boolean mTouchEnabled = false;
40     private boolean mEnableKeyEvent = false;
41 
42     /**
43      * Constructs LeanbackViewPager
44      * @param context
45      */
LeanbackViewPager(@onNull Context context)46     public LeanbackViewPager(@NonNull Context context) {
47         super(context);
48     }
49 
50     /**
51      * Constructs LeanbackViewPager
52      * @param context
53      * @param attrs
54      */
LeanbackViewPager(@onNull Context context, @Nullable AttributeSet attrs)55     public LeanbackViewPager(@NonNull Context context,
56             @Nullable AttributeSet attrs) {
57         super(context, attrs);
58     }
59 
60     @Override
onTouchEvent(@onNull MotionEvent event)61     public boolean onTouchEvent(@NonNull MotionEvent event) {
62         return mTouchEnabled && super.onTouchEvent(event);
63     }
64 
65     @Override
onInterceptTouchEvent(@onNull MotionEvent event)66     public boolean onInterceptTouchEvent(@NonNull MotionEvent event) {
67         return mTouchEnabled && super.onInterceptTouchEvent(event);
68     }
69 
70     @Override
executeKeyEvent(@onNull KeyEvent event)71     public boolean executeKeyEvent(@NonNull KeyEvent event) {
72         return mEnableKeyEvent && super.executeKeyEvent(event);
73     }
74 
75     /**
76      * Setter for enabling/disabling touch events
77      * @param enableTouch
78      */
setTouchEnabled(boolean enableTouch)79     public void setTouchEnabled(boolean enableTouch) {
80         mTouchEnabled = enableTouch;
81     }
82 
83     /**
84      * Setter for enabling/disabling key events
85      * @param enableKeyEvent
86      */
setKeyEventsEnabled(boolean enableKeyEvent)87     public void setKeyEventsEnabled(boolean enableKeyEvent) {
88         mEnableKeyEvent = enableKeyEvent;
89     }
90 }
91