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/res/layout/call_log_list_item.xml b/res/layout/call_log_list_item.xml
index 1938592..1e61f77 100644
--- a/res/layout/call_log_list_item.xml
+++ b/res/layout/call_log_list_item.xml
@@ -25,8 +25,9 @@
          above a group of call log entries. -->
     <TextView
         android:id="@+id/call_log_day_group_label"
-        android:layout_width="match_parent"
+        android:layout_width="wrap_content"
         android:layout_height="wrap_content"
+        android:layout_gravity="start"
         android:layout_marginStart="@dimen/call_log_start_margin"
         android:layout_marginEnd="@dimen/call_log_outer_margin"
         android:fontFamily="sans-serif-medium"
diff --git a/src/com/android/dialer/DialtactsActivity.java b/src/com/android/dialer/DialtactsActivity.java
index bf2af13..249e0c1 100644
--- a/src/com/android/dialer/DialtactsActivity.java
+++ b/src/com/android/dialer/DialtactsActivity.java
@@ -93,6 +93,7 @@
 
 import java.util.ArrayList;
 import java.util.List;
+import java.util.Locale;
 
 /**
  * The dialer tab's title is 'phone', a more common name (see strings.xml).
@@ -1104,11 +1105,20 @@
 
     @Override
     public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
+        position = mListsFragment.getRtlPosition(position);
         // Only scroll the button when the first tab is selected. The button should scroll from
         // the middle to right position only on the transition from the first tab to the second
         // tab.
-        if (position == ListsFragment.TAB_INDEX_SPEED_DIAL && !mIsLandscape) {
-            mFloatingActionButtonController.onPageScrolled(positionOffset);
+        // If the app is in RTL mode, we need to check against the second tab, rather than the
+        // first. This is because if we are scrolling between the first and second tabs, the
+        // viewpager will report that the starting tab position is 1 rather than 0, due to the
+        // reversal of the order of the tabs.
+        final boolean isLayoutRtl = DialerUtils.isRtl();
+        final boolean shouldScrollButton = position == (isLayoutRtl
+                ? ListsFragment.TAB_INDEX_RECENTS : ListsFragment.TAB_INDEX_SPEED_DIAL);
+        if (shouldScrollButton && !mIsLandscape) {
+            mFloatingActionButtonController.onPageScrolled(
+                    isLayoutRtl ? 1 - positionOffset : positionOffset);
         } else if (position != ListsFragment.TAB_INDEX_SPEED_DIAL) {
             mFloatingActionButtonController.onPageScrolled(1);
         }
@@ -1116,6 +1126,7 @@
 
     @Override
     public void onPageSelected(int position) {
+        position = mListsFragment.getRtlPosition(position);
         mCurrentTabPosition = position;
     }
 
@@ -1163,15 +1174,15 @@
     private void updateFloatingActionButtonControllerAlignment(boolean animate) {
         int align;
         if (mIsDialpadShown) {
-            align = mIsLandscape ? FloatingActionButtonController.ALIGN_QUARTER_RIGHT
+            align = mIsLandscape ? FloatingActionButtonController.ALIGN_QUARTER_END
                     : FloatingActionButtonController.ALIGN_MIDDLE;
         } else {
             if (!mIsLandscape) {
                 align = mCurrentTabPosition == ListsFragment.TAB_INDEX_SPEED_DIAL
                         ? FloatingActionButtonController.ALIGN_MIDDLE
-                            : FloatingActionButtonController.ALIGN_RIGHT;
+                            : FloatingActionButtonController.ALIGN_END;
             } else {
-                align = FloatingActionButtonController.ALIGN_RIGHT;
+                align = FloatingActionButtonController.ALIGN_END;
             }
         }
         mFloatingActionButtonController.align(align,
diff --git a/src/com/android/dialer/calllog/CallLogActivity.java b/src/com/android/dialer/calllog/CallLogActivity.java
index 4e969e1..1129f27 100644
--- a/src/com/android/dialer/calllog/CallLogActivity.java
+++ b/src/com/android/dialer/calllog/CallLogActivity.java
@@ -29,9 +29,9 @@
 import android.view.MenuInflater;
 import android.view.MenuItem;
 
+import com.android.contacts.common.list.ViewPagerTabs;
 import com.android.dialer.DialtactsActivity;
 import com.android.dialer.R;
-import com.android.contacts.common.list.ViewPagerTabs;
 import com.android.dialer.voicemail.VoicemailStatusHelper;
 import com.android.dialer.voicemail.VoicemailStatusHelperImpl;
 
diff --git a/src/com/android/dialer/list/ListsFragment.java b/src/com/android/dialer/list/ListsFragment.java
index 847edd9..4f96d63 100644
--- a/src/com/android/dialer/list/ListsFragment.java
+++ b/src/com/android/dialer/list/ListsFragment.java
@@ -10,7 +10,6 @@
 import android.content.Loader;
 import android.content.SharedPreferences;
 import android.database.Cursor;
-import android.graphics.Rect;
 import android.net.Uri;
 import android.os.Bundle;
 import android.provider.CallLog;
@@ -26,9 +25,6 @@
 import com.android.contacts.common.GeoUtil;
 import com.android.contacts.common.list.ViewPagerTabs;
 import com.android.dialer.DialtactsActivity;
-
-import android.view.View.OnClickListener;
-
 import com.android.dialer.R;
 import com.android.dialer.calllog.CallLogAdapter;
 import com.android.dialer.calllog.CallLogFragment;
@@ -36,6 +32,7 @@
 import com.android.dialer.calllog.CallLogQueryHandler;
 import com.android.dialer.calllog.ContactInfoHelper;
 import com.android.dialer.list.ShortcutCardsAdapter.SwipeableShortcutCard;
+import com.android.dialer.util.DialerUtils;
 import com.android.dialer.widget.OverlappingPaneLayout;
 import com.android.dialer.widget.OverlappingPaneLayout.PanelSlideListener;
 import com.android.dialerbind.ObjectFactory;
@@ -60,7 +57,7 @@
     public static final int TAB_INDEX_RECENTS = 1;
     public static final int TAB_INDEX_ALL_CONTACTS = 2;
 
-    private static final int TAB_INDEX_COUNT = 3;
+    public static final int TAB_INDEX_COUNT = 3;
 
     private static final int MAX_RECENTS_ENTRIES = 20;
     // Oldest recents entry to display is 2 weeks old.
@@ -186,7 +183,7 @@
 
         @Override
         public Fragment getItem(int position) {
-            switch (position) {
+            switch (getRtlPosition(position)) {
                 case TAB_INDEX_SPEED_DIAL:
                     mSpeedDialFragment = new SpeedDialFragment();
                     return mSpeedDialFragment;
@@ -260,6 +257,7 @@
         mViewPager.setAdapter(mViewPagerAdapter);
         mViewPager.setOffscreenPageLimit(2);
         mViewPager.setOnPageChangeListener(this);
+        mViewPager.setCurrentItem(getRtlPosition(TAB_INDEX_SPEED_DIAL));
 
         mTabTitles = new String[TAB_INDEX_COUNT];
         mTabTitles[TAB_INDEX_SPEED_DIAL] = getResources().getString(R.string.tab_speed_dial);
@@ -387,4 +385,11 @@
     public RemoveView getRemoveView() {
         return mRemoveView;
     }
+
+    public int getRtlPosition(int position) {
+        if (DialerUtils.isRtl()) {
+            return TAB_INDEX_COUNT - 1 - position;
+        }
+        return position;
+    }
 }
diff --git a/src/com/android/dialer/util/DialerUtils.java b/src/com/android/dialer/util/DialerUtils.java
index 2b4a74c..07a330e 100644
--- a/src/com/android/dialer/util/DialerUtils.java
+++ b/src/com/android/dialer/util/DialerUtils.java
@@ -34,6 +34,7 @@
 import com.android.dialer.R;
 
 import java.util.List;
+import java.util.Locale;
 
 /**
  * General purpose utility methods for the Dialer.
@@ -138,4 +139,12 @@
         final CharSequence separator = resources.getString(R.string.list_delimeter);
         return TextUtils.join(separator, list);
     }
+
+    /**
+     * @return True if the application is currently in RTL mode.
+     */
+    public static boolean isRtl() {
+        return TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()) ==
+            View.LAYOUT_DIRECTION_RTL;
+    }
 }