195a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser/* 295a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser * Copyright (C) 2014 Google Inc. 395a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser * Licensed to The Android Open Source Project. 495a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser * 595a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser * Licensed under the Apache License, Version 2.0 (the "License"); 695a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser * you may not use this file except in compliance with the License. 795a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser * You may obtain a copy of the License at 895a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser * 995a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser * http://www.apache.org/licenses/LICENSE-2.0 1095a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser * 1195a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser * Unless required by applicable law or agreed to in writing, software 1295a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser * distributed under the License is distributed on an "AS IS" BASIS, 1395a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1495a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser * See the License for the specific language governing permissions and 1595a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser * limitations under the License. 1695a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser */ 1795a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser 1895a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeserpackage com.android.mail.ui; 1995a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser 2095a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeserimport android.support.v4.view.ViewPager; 2195a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser 2295a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeserimport com.android.mail.utils.ViewUtils; 2395a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser 2495a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser/** 2595a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser * Helper class for adding RTL support to a ViewPager. Note that this also requires a PagerAdapter 2695a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser * that returns the pages in reversed order in the RTL case. 2795a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser */ 2895a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeserpublic class BidiViewPagerHelper { 2995a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser private ViewPager mViewPager; 3095a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser 3195a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser public BidiViewPagerHelper(ViewPager viewPager) { 3295a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser mViewPager = viewPager; 3395a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser } 3495a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser 3595a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser public int getFirstPage() { 3695a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser return getBidiIndex(0); 3795a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser } 3895a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser 3995a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser public int getLastPage() { 4095a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser return getBidiIndex(mViewPager.getAdapter().getCount() - 1); 4195a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser } 4295a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser 4395a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser public int getPreviousPage() { 4495a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser return clampToBounds(getBidiIndex(mViewPager.getCurrentItem() - 1)); 4595a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser } 4695a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser 4795a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser public int getNextPage() { 4895a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser return clampToBounds(getBidiIndex(mViewPager.getCurrentItem() + 1)); 4995a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser } 5095a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser 5195a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser private int clampToBounds(int position) { 5295a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser return Math.max(0, Math.min(mViewPager.getAdapter().getCount() - 1, position)); 5395a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser } 5495a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser 5595a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser private int getBidiIndex(int position) { 5695a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser if (ViewUtils.isViewRtl(mViewPager)) { 5795a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser return mViewPager.getAdapter().getCount() - 1 - position; 5895a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser } else { 5995a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser return position; 6095a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser } 6195a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser } 6295a73baf5ad9d11a889a1ee9b4bd563b12527d8fMichael Kaeser}