Properly handle RTL on the preview pager

Bug: 129147364
Change-Id: I9021399a6615543f559121aca67bc1b62255b4d1
diff --git a/res/layout/preview_pager.xml b/res/layout/preview_pager.xml
index cddf647..43f8fb6 100644
--- a/res/layout/preview_pager.xml
+++ b/res/layout/preview_pager.xml
@@ -17,6 +17,7 @@
 -->
 <LinearLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:orientation="vertical">
@@ -43,11 +44,12 @@
             android:id="@+id/arrow_previous"
             android:layout_width="@dimen/indicator_arrow_touch_area_size"
             android:layout_height="@dimen/indicator_arrow_touch_area_size"
-            android:layout_gravity="center_vertical|start"
+            android:layout_gravity="center_vertical|left"
             android:layout_marginStart="@dimen/indicator_arrow_container_margin_horizontal"
             android:background="?android:attr/selectableItemBackgroundBorderless"
             android:contentDescription="@string/previous_page_content_description"
-            android:visibility="gone">
+            android:visibility="gone"
+            tools:ignore="RtlHardcoded">
 
             <ImageView
                 android:layout_width="@dimen/indicator_arrow_size"
@@ -62,11 +64,12 @@
             android:id="@+id/arrow_next"
             android:layout_width="@dimen/indicator_arrow_touch_area_size"
             android:layout_height="@dimen/indicator_arrow_touch_area_size"
-            android:layout_gravity="center_vertical|end"
+            android:layout_gravity="center_vertical|right"
             android:layout_marginEnd="@dimen/indicator_arrow_container_margin_horizontal"
             android:background="?android:attr/selectableItemBackgroundBorderless"
             android:contentDescription="@string/next_page_content_description"
-            android:visibility="gone">
+            android:visibility="gone"
+            tools:ignore="RtlHardcoded">
 
             <ImageView
                 android:layout_width="@dimen/indicator_arrow_size"
diff --git a/src/com/android/customization/widget/PreviewPager.java b/src/com/android/customization/widget/PreviewPager.java
index 7819b7c..c80396d 100644
--- a/src/com/android/customization/widget/PreviewPager.java
+++ b/src/com/android/customization/widget/PreviewPager.java
@@ -23,16 +23,19 @@
 import android.util.DisplayMetrics;
 import android.view.LayoutInflater;
 import android.view.View;
-import android.view.ViewGroup;
 import android.widget.LinearLayout;
 
 import androidx.annotation.Nullable;
+import androidx.core.text.TextUtilsCompat;
+import androidx.core.view.ViewCompat;
 import androidx.viewpager.widget.PagerAdapter;
 import androidx.viewpager.widget.ViewPager;
 import androidx.viewpager.widget.ViewPager.OnPageChangeListener;
 
 import com.android.wallpaper.R;
 
+import java.util.Locale;
+
 /**
  * A Widget consisting of a ViewPager linked to a PageIndicator and previous/next arrows that can be
  * used to page over that ViewPager.
@@ -72,6 +75,8 @@
 
         mPageStyle = a.getInteger(R.styleable.PreviewPager_card_style, STYLE_PEEKING);
 
+        a.recycle();
+
         mViewPager = findViewById(R.id.preview_viewpager);
         mViewPager.setPageMargin(res.getDimensionPixelOffset(R.dimen.preview_page_gap));
         mViewPager.setClipToPadding(false);
@@ -148,7 +153,16 @@
     public void setAdapter(PagerAdapter adapter) {
         mAdapter = adapter;
         mViewPager.setAdapter(adapter);
-
+        if (ViewCompat.isLayoutDirectionResolved(mViewPager)) {
+            if (ViewCompat.getLayoutDirection(mViewPager) == ViewCompat.LAYOUT_DIRECTION_RTL) {
+                mViewPager.setCurrentItem(mAdapter.getCount() - 1);
+            }
+        } else {
+            if (TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault())
+                    == ViewCompat.LAYOUT_DIRECTION_RTL) {
+                mViewPager.setCurrentItem(mAdapter.getCount() - 1);
+            }
+        }
         mAdapter.registerDataSetObserver(new DataSetObserver() {
             @Override
             public void onChanged() {
@@ -178,7 +192,7 @@
                      int position, float positionOffset, int positionOffsetPixels) {
                  // For certain sizes, positionOffset never makes it to 1, so round it as we don't
                  // need that much precision
-                 float location = (float)Math.round((position + positionOffset) * 100) / 100;
+                 float location = (float) Math.round((position + positionOffset) * 100) / 100;
                  mPageIndicator.setLocation(location);
                  if (mExternalPageListener != null) {
                      mExternalPageListener.onPageScrolled(position, positionOffset,