Fix theme picker breaking in RTL mode

- Fix the preview screen.
- Fix the theme option layout.

Screenshot:
LTR: https://screenshot.googleplex.com/BwN8J2VKKg5.png
RTL: https://screenshot.googleplex.com/Fm4WQKKrB8i.png

Test: Manually, see screenshot
Bug: 159093562
Change-Id: I03cd05d3f4c85472a2bdc70b621cb2ad1651404e
diff --git a/res/layout/theme_option.xml b/res/layout/theme_option.xml
index 557d532..379c507 100644
--- a/res/layout/theme_option.xml
+++ b/res/layout/theme_option.xml
@@ -46,7 +46,7 @@
             android:layout_width="@dimen/theme_option_icon_sample_width"
             android:layout_height="@dimen/theme_option_icon_sample_height"
             android:layout_alignParentTop="true"
-            android:layout_alignParentLeft="true"
+            android:layout_alignParentStart="true"
             android:tint="?android:colorForeground"/>
         <ImageView
             android:id="@+id/theme_option_shape"
@@ -54,7 +54,7 @@
             android:layout_height="@dimen/theme_option_shape_sample_height"
             android:layout_alignBottom="@+id/theme_option_icon"
             android:layout_toEndOf="@id/theme_option_icon"
-            android:layout_marginLeft="@dimen/theme_option_sample_margin"/>
+            android:layout_marginStart="@dimen/theme_option_sample_margin"/>
         <TextView
             android:id="@+id/theme_option_font"
             android:layout_width="@dimen/theme_option_font_sample_width"
diff --git a/res/layout/theme_preview_color_icons.xml b/res/layout/theme_preview_color_icons.xml
index f1e7522..c8afb99 100644
--- a/res/layout/theme_preview_color_icons.xml
+++ b/res/layout/theme_preview_color_icons.xml
@@ -129,18 +129,18 @@
         </LinearLayout>
 
         <!-- Icons of CheckBox/RadioButton/Switch. -->
-        <LinearLayout
+        <RelativeLayout
             android:id="@+id/button_icons"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
-            android:orientation="horizontal"
             app:layout_constraintStart_toStartOf="parent"
             app:layout_constraintEnd_toEndOf="parent"
             app:layout_constraintTop_toBottomOf="@id/qs_icons"
             app:layout_constraintBottom_toBottomOf="parent">
             <FrameLayout
                 android:layout_width="@dimen/preview_theme_icon_size"
-                android:layout_height="@dimen/preview_theme_icon_size">
+                android:layout_height="@dimen/preview_theme_icon_size"
+                android:layout_alignParentStart="true">
                 <CheckBox
                     android:id="@+id/preview_check_selected"
                     android:layout_width="wrap_content"
@@ -149,13 +149,11 @@
                     android:checked="true"
                     android:enabled="false"/>
             </FrameLayout>
-            <Space
-                android:layout_width="0dp"
-                android:layout_height="wrap_content"
-                android:layout_weight="1" />
+
             <FrameLayout
                 android:layout_width="@dimen/preview_theme_icon_size"
-                android:layout_height="@dimen/preview_theme_icon_size">
+                android:layout_height="@dimen/preview_theme_icon_size"
+                android:layout_centerHorizontal="true">
                 <RadioButton
                     android:id="@+id/preview_radio_selected"
                     android:layout_width="wrap_content"
@@ -164,13 +162,11 @@
                     android:checked="true"
                     android:enabled="false"/>
             </FrameLayout>
-            <Space
-                android:layout_width="0dp"
-                android:layout_height="wrap_content"
-                android:layout_weight="1" />
+
             <FrameLayout
-                android:layout_width="@dimen/preview_theme_icon_size"
-                android:layout_height="@dimen/preview_theme_icon_size">
+                android:layout_width="wrap_content"
+                android:layout_height="@dimen/preview_theme_icon_size"
+                android:layout_alignParentEnd="true">
                 <Switch
                     android:id="@+id/preview_toggle_selected"
                     android:layout_width="wrap_content"
@@ -179,7 +175,7 @@
                     android:checked="true"
                     android:enabled="false"/>
             </FrameLayout>
-        </LinearLayout>
+        </RelativeLayout>
 
     </androidx.constraintlayout.widget.ConstraintLayout>
 </androidx.cardview.widget.CardView>
diff --git a/src/com/android/customization/picker/theme/ThemeOptionPreviewer.java b/src/com/android/customization/picker/theme/ThemeOptionPreviewer.java
index ec98601..d9b7f4d 100644
--- a/src/com/android/customization/picker/theme/ThemeOptionPreviewer.java
+++ b/src/com/android/customization/picker/theme/ThemeOptionPreviewer.java
@@ -21,6 +21,7 @@
 import android.app.WallpaperColors;
 import android.content.Context;
 import android.content.res.ColorStateList;
+import android.content.res.Configuration;
 import android.content.res.Resources;
 import android.graphics.Typeface;
 import android.graphics.drawable.Drawable;
@@ -120,6 +121,8 @@
         updateTime();
         final float screenAspectRatio =
                 ScreenSizeCalculator.getInstance().getScreenAspectRatio(mContext);
+        Configuration config = mContext.getResources().getConfiguration();
+        final boolean directionLTR = config.getLayoutDirection() == View.LAYOUT_DIRECTION_LTR;
         previewContainer.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
             @Override
             public void onLayoutChange(View view, int left, int top, int right, int bottom,
@@ -141,7 +144,7 @@
                 mContentView.setScaleX(scale);
                 mContentView.setScaleY(scale);
                 // The pivot point is centered by default, set to (0, 0).
-                mContentView.setPivotX(0f);
+                mContentView.setPivotX(directionLTR ? 0f : mContentView.getMeasuredWidth());
                 mContentView.setPivotY(0f);
 
                 // Ensure there will be only one content view in the container.