• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 package com.android.messaging.ui;
17 
18 import android.content.Context;
19 import androidx.viewpager.widget.PagerAdapter;
20 import androidx.viewpager.widget.ViewPager;
21 import android.util.AttributeSet;
22 import android.view.MotionEvent;
23 
24 import com.android.messaging.util.UiUtils;
25 
26 /**
27  * A simple extension on the standard ViewPager which lets you turn paging on/off.
28  */
29 public class PagingAwareViewPager extends ViewPager {
30     private boolean mPagingEnabled = true;
31 
PagingAwareViewPager(Context context, AttributeSet attrs)32     public PagingAwareViewPager(Context context, AttributeSet attrs) {
33         super(context, attrs);
34     }
35 
36     @Override
setCurrentItem(int item, boolean smoothScroll)37     public void setCurrentItem(int item, boolean smoothScroll) {
38         super.setCurrentItem(getRtlPosition(item), smoothScroll);
39     }
40 
41     @Override
setCurrentItem(int item)42     public void setCurrentItem(int item) {
43         super.setCurrentItem(getRtlPosition(item));
44     }
45 
46     @Override
getCurrentItem()47     public int getCurrentItem() {
48         int position = super.getCurrentItem();
49         return getRtlPosition(position);
50     }
51 
52     /**
53      * Switches position in pager to be adjusted for if we are in RtL mode
54      *
55      * @param position
56      * @return position adjusted if in rtl mode
57      */
getRtlPosition(final int position)58     protected int getRtlPosition(final int position) {
59         final PagerAdapter adapter = getAdapter();
60         if (adapter != null && UiUtils.isRtlMode()) {
61             return adapter.getCount() - 1 - position;
62         }
63         return position;
64     }
65 
66     @Override
onTouchEvent(final MotionEvent event)67     public boolean onTouchEvent(final MotionEvent event) {
68         if (!mPagingEnabled) {
69             return false;
70         }
71         return super.onTouchEvent(event);
72     }
73 
74     @Override
onInterceptTouchEvent(final MotionEvent event)75     public boolean onInterceptTouchEvent(final MotionEvent event) {
76         if (!mPagingEnabled) {
77             return false;
78         }
79         return super.onInterceptTouchEvent(event);
80     }
81 
setPagingEnabled(final boolean enabled)82     public void setPagingEnabled(final boolean enabled) {
83         this.mPagingEnabled = enabled;
84     }
85 
86     /** This prevents touch-less scrolling eg. while doing accessibility navigation. */
87     @Override
canScrollHorizontally(int direction)88     public boolean canScrollHorizontally(int direction) {
89         if (mPagingEnabled) {
90             return super.canScrollHorizontally(direction);
91         } else {
92             return false;
93         }
94     }
95 }
96