Make ViewPagerTabs and FABController RTL aware

* Underline in ViewPagerTabStrips is correctly drawn in RTL mode
* Clicking on ViewPagerTabs propogates correct onPageSelected position
to ViewPager
* FAB is now end aligned instead of right aligned
* ViewPager in Dialer is correctly reversed and behaves correctly
* Call Log groups headers in call log are now correctly aligned in RTL

Bug: 16162264
Bug: 16464471

Change-Id: I2db6b9147a628a68fba5bbbdb7276855d483bad5
diff --git a/src/com/android/contacts/common/list/ViewPagerTabStrip.java b/src/com/android/contacts/common/list/ViewPagerTabStrip.java
index f8a0040..964db5f 100644
--- a/src/com/android/contacts/common/list/ViewPagerTabStrip.java
+++ b/src/com/android/contacts/common/list/ViewPagerTabStrip.java
@@ -65,16 +65,6 @@
         invalidate();
     }
 
-    /**
-     * Notifies this view that a new page has been selected in the view pager. We save the tab
-     * index and reset the selection offset to 0.
-     */
-    void onPageSelected(int position) {
-        mIndexForSelection = position;
-        mSelectionOffset = 0;
-        invalidate();
-    }
-
     @Override
     protected void onDraw(Canvas canvas) {
         int childCount = getChildCount();
@@ -84,10 +74,12 @@
             View selectedTitle = getChildAt(mIndexForSelection);
             int selectedLeft = selectedTitle.getLeft();
             int selectedRight = selectedTitle.getRight();
-            if ((mSelectionOffset > 0.0f) &&
-                    (mIndexForSelection < (getChildCount() - 1))) {
+            final boolean isRtl = isRtl();
+            final boolean hasNextTab = isRtl ? mIndexForSelection > 0
+                    : (mIndexForSelection < (getChildCount() - 1));
+            if ((mSelectionOffset > 0.0f) && hasNextTab) {
                 // Draw the selection partway between the tabs
-                View nextTitle = getChildAt(mIndexForSelection + 1);
+                View nextTitle = getChildAt(mIndexForSelection + (isRtl ? -1 : 1));
                 int nextLeft = nextTitle.getLeft();
                 int nextRight = nextTitle.getRight();
 
@@ -102,4 +94,8 @@
                     selectedRight, height, mSelectedUnderlinePaint);
         }
     }
+
+    private boolean isRtl() {
+        return getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
+    }
 }
\ No newline at end of file
diff --git a/src/com/android/contacts/common/list/ViewPagerTabs.java b/src/com/android/contacts/common/list/ViewPagerTabs.java
index 0bed717..75a00d2 100644
--- a/src/com/android/contacts/common/list/ViewPagerTabs.java
+++ b/src/com/android/contacts/common/list/ViewPagerTabs.java
@@ -157,7 +157,7 @@
         textView.setOnClickListener(new OnClickListener() {
             @Override
             public void onClick(View v) {
-                mPager.setCurrentItem(position);
+                mPager.setCurrentItem(getRtlPosition(position));
             }
         });
 
@@ -186,6 +186,7 @@
 
     @Override
     public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
+        position = getRtlPosition(position);
         int tabStripChildCount = mTabStrip.getChildCount();
         if ((tabStripChildCount == 0) || (position < 0) || (position >= tabStripChildCount)) {
             return;
@@ -197,6 +198,7 @@
 
     @Override
     public void onPageSelected(int position) {
+        position = getRtlPosition(position);
         if (mPrevSelected >= 0) {
             mTabStrip.getChildAt(mPrevSelected).setSelected(false);
         }
@@ -212,5 +214,12 @@
     @Override
     public void onPageScrollStateChanged(int state) {
     }
+
+    private int getRtlPosition(int position) {
+        if (getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
+            return mTabStrip.getChildCount() - 1 - position;
+        }
+        return position;
+    }
 }
 
diff --git a/src/com/android/contacts/common/widget/FloatingActionButtonController.java b/src/com/android/contacts/common/widget/FloatingActionButtonController.java
index 6dd9d05..c048fd0 100644
--- a/src/com/android/contacts/common/widget/FloatingActionButtonController.java
+++ b/src/com/android/contacts/common/widget/FloatingActionButtonController.java
@@ -19,6 +19,7 @@
 import android.app.Activity;
 import android.content.res.Resources;
 import android.os.Bundle;
+import android.util.Log;
 import android.view.animation.AnimationUtils;
 import android.view.animation.Interpolator;
 import android.view.View;
@@ -32,8 +33,8 @@
  */
 public class FloatingActionButtonController {
     public static final int ALIGN_MIDDLE = 0;
-    public static final int ALIGN_QUARTER_RIGHT = 1;
-    public static final int ALIGN_RIGHT = 2;
+    public static final int ALIGN_QUARTER_END = 1;
+    public static final int ALIGN_END = 2;
 
     private final int mAnimationDuration;
     private final int mFloatingActionButtonWidth;
@@ -84,9 +85,7 @@
         // As the page is scrolling, if we're on the first tab, update the FAB position so it
         // moves along with it.
         mFloatingActionButtonContainer.setTranslationX(
-                (int) (positionOffset * (mScreenWidth / 2
-                        - mFloatingActionButtonWidth / 2
-                        - mFloatingActionButtonMarginRight)));
+                (int) (positionOffset * getTranslationXForAlignment(ALIGN_END)));
         mFloatingActionButtonContainer.setTranslationY(0);
     }
 
@@ -114,26 +113,33 @@
     }
 
     /**
-     * Calculates the X offset of the FAB to the given alignment.
+     * Calculates the X offset of the FAB to the given alignment, adjusted for whether or not the
+     * view is in RTL mode.
      *
      * @param align One of ALIGN_MIDDLE, ALIGN_QUARTER_RIGHT, or ALIGN_RIGHT.
      * @return The translationX for the given alignment.
      */
     public int getTranslationXForAlignment(int align) {
+        int result = 0;
         switch (align) {
             case ALIGN_MIDDLE:
                 // Moves the FAB to exactly center screen.
                 return 0;
-            case ALIGN_QUARTER_RIGHT:
+            case ALIGN_QUARTER_END:
                 // Moves the FAB a quarter of the screen width.
-                return mScreenWidth / 4;
-            case ALIGN_RIGHT:
+                result = mScreenWidth / 4;
+                break;
+            case ALIGN_END:
                 // Moves the FAB half the screen width. Same as aligning right with a marginRight.
-                return mScreenWidth / 2
+                result = mScreenWidth / 2
                         - mFloatingActionButtonWidth / 2
                         - mFloatingActionButtonMarginRight;
+                break;
         }
-        return 0;
+        if (isLayoutRtl()) {
+            result *= -1;
+        }
+        return result;
     }
 
     /**
@@ -146,4 +152,8 @@
         mFloatingActionButtonContainer.setTranslationX(translationX);
         mFloatingActionButtonContainer.setTranslationY(translationY);
     }
+
+    private boolean isLayoutRtl() {
+        return mFloatingActionButtonContainer.isLayoutRtl();
+    }
 }