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 */
16package com.android.messaging.ui;
17
18import android.content.Context;
19import android.support.v4.view.PagerAdapter;
20import android.support.v4.view.ViewPager;
21import android.util.AttributeSet;
22import android.view.MotionEvent;
23
24import com.android.messaging.util.UiUtils;
25
26/**
27 * A simple extension on the standard ViewPager which lets you turn paging on/off.
28 */
29public class PagingAwareViewPager extends ViewPager {
30    private boolean mPagingEnabled = true;
31
32    public PagingAwareViewPager(Context context, AttributeSet attrs) {
33        super(context, attrs);
34    }
35
36    @Override
37    public void setCurrentItem(int item, boolean smoothScroll) {
38        super.setCurrentItem(getRtlPosition(item), smoothScroll);
39    }
40
41    @Override
42    public void setCurrentItem(int item) {
43        super.setCurrentItem(getRtlPosition(item));
44    }
45
46    @Override
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     */
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
67    public boolean onTouchEvent(final MotionEvent event) {
68        if (!mPagingEnabled) {
69            return false;
70        }
71        return super.onTouchEvent(event);
72    }
73
74    @Override
75    public boolean onInterceptTouchEvent(final MotionEvent event) {
76        if (!mPagingEnabled) {
77            return false;
78        }
79        return super.onInterceptTouchEvent(event);
80    }
81
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
88    public boolean canScrollHorizontally(int direction) {
89        if (mPagingEnabled) {
90            return super.canScrollHorizontally(direction);
91        } else {
92            return false;
93        }
94    }
95}
96