Merge "Fix direction of page switching in RTL" into jb-mr2-dev
diff --git a/src/com/android/deskclock/DeskClock.java b/src/com/android/deskclock/DeskClock.java
index 0cc0d28..2cdf575 100644
--- a/src/com/android/deskclock/DeskClock.java
+++ b/src/com/android/deskclock/DeskClock.java
@@ -30,6 +30,7 @@
 import android.preference.PreferenceManager;
 import android.support.v13.app.FragmentPagerAdapter;
 import android.support.v4.view.ViewPager;
+import android.text.TextUtils;
 import android.util.Log;
 import android.view.Menu;
 import android.view.MenuItem;
@@ -49,6 +50,7 @@
 
 import java.util.ArrayList;
 import java.util.HashSet;
+import java.util.Locale;
 import java.util.TimeZone;
 
 /**
@@ -76,6 +78,11 @@
     public static final int TIMER_TAB_INDEX = 0;
     public static final int CLOCK_TAB_INDEX = 1;
     public static final int STOPWATCH_TAB_INDEX = 2;
+    // Tabs indices are switched for right-to-left since there is no
+    // native support for RTL in the ViewPager.
+    public static final int RTL_TIMER_TAB_INDEX = 2;
+    public static final int RTL_CLOCK_TAB_INDEX = 1;
+    public static final int RTL_STOPWATCH_TAB_INDEX = 0;
 
     private int mSelectedTab;
 
@@ -319,7 +326,7 @@
 
         @Override
         public Fragment getItem(int position) {
-            TabInfo info = mTabs.get(position);
+            TabInfo info = mTabs.get(getRtlPosition(position));
             DeskClockFragment f = (DeskClockFragment) Fragment.instantiate(
                     mContext, info.clss.getName(), info.args);
             return f;
@@ -346,7 +353,7 @@
 
         @Override
         public void onPageSelected(int position) {
-            mMainActionBar.setSelectedNavigationItem(position);
+            mMainActionBar.setSelectedNavigationItem(getRtlPosition(position));
             notifyPageChanged(position);
         }
 
@@ -363,7 +370,7 @@
         @Override
         public void onTabSelected(Tab tab, FragmentTransaction ft) {
             TabInfo info = (TabInfo)tab.getTag();
-            mPager.setCurrentItem(info.getPosition());
+            mPager.setCurrentItem(getRtlPosition(info.getPosition()));
         }
 
         @Override
@@ -401,6 +408,27 @@
         public void unregisterPageChangedListener(DeskClockFragment frag) {
             mFragmentTags.remove(frag.getTag());
         }
+
+        private boolean isRtl() {
+            return TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()) ==
+                    View.LAYOUT_DIRECTION_RTL;
+        }
+
+        private int getRtlPosition(int position) {
+             if (isRtl()) {
+                 switch (position) {
+                     case TIMER_TAB_INDEX:
+                         return RTL_TIMER_TAB_INDEX;
+                     case CLOCK_TAB_INDEX:
+                         return RTL_CLOCK_TAB_INDEX;
+                     case STOPWATCH_TAB_INDEX:
+                         return RTL_STOPWATCH_TAB_INDEX;
+                     default:
+                         break;
+                }
+            }
+            return position;
+        }
     }
 
     public static abstract class OnTapListener implements OnTouchListener {