Merge "Update the size of the center checkmark" into sc-dev
diff --git a/res/layout/fragment_grid_picker.xml b/res/layout/fragment_grid_picker.xml
index 7264d14..aec8e49 100644
--- a/res/layout/fragment_grid_picker.xml
+++ b/res/layout/fragment_grid_picker.xml
@@ -52,7 +52,8 @@
                 android:layout_width="match_parent"
                 android:layout_height="@dimen/options_container_height"
                 android:layout_marginBottom="@dimen/grid_options_container_bottom_margin"
-                android:layout_marginHorizontal="@dimen/grid_options_container_horizontal_margin"
+                android:paddingHorizontal="@dimen/grid_options_container_horizontal_margin"
+                android:clipToPadding="false"
                 app:layout_constraintBottom_toBottomOf="parent"
                 app:layout_constraintEnd_toEndOf="parent"
                 app:layout_constraintStart_toStartOf="parent" />
diff --git a/res/layout/grid_option.xml b/res/layout/grid_option.xml
index 221f353..0931dea 100644
--- a/res/layout/grid_option.xml
+++ b/res/layout/grid_option.xml
@@ -15,9 +15,8 @@
      limitations under the License.
 -->
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
-    android:layout_width="match_parent"
+    android:layout_width="wrap_content"
     android:layout_height="wrap_content"
-    android:paddingHorizontal="@dimen/option_padding_horizontal"
     android:paddingBottom="@dimen/option_bottom_margin"
     android:clipChildren="false"
     android:clipToPadding="false"
diff --git a/src/com/android/customization/picker/grid/GridFragment.java b/src/com/android/customization/picker/grid/GridFragment.java
index b0802cf..2aeb348 100644
--- a/src/com/android/customization/picker/grid/GridFragment.java
+++ b/src/com/android/customization/picker/grid/GridFragment.java
@@ -46,6 +46,7 @@
 import com.android.customization.picker.WallpaperPreviewer;
 import com.android.customization.util.LaunchUtils;
 import com.android.customization.widget.OptionSelectorController;
+import com.android.customization.widget.OptionSelectorController.CheckmarkStyle;
 import com.android.wallpaper.R;
 import com.android.wallpaper.model.WallpaperInfo;
 import com.android.wallpaper.module.CurrentWallpaperInfoFactory;
@@ -205,8 +206,7 @@
             public void onOptionsLoaded(List<GridOption> options) {
                 mLoading.hide();
                 mOptionsController = new OptionSelectorController<>(
-                        mOptionsContainer, options, true,
-                        OptionSelectorController.CheckmarkStyle.CENTER);
+                        mOptionsContainer, options, /* useGrid= */ false, CheckmarkStyle.CENTER);
                 mOptionsController.initOptions(mGridManager);
 
                 // Find the selected Grid option.
diff --git a/src/com/android/customization/widget/OptionSelectorController.java b/src/com/android/customization/widget/OptionSelectorController.java
index f1d14c5..9ecc0d2 100644
--- a/src/com/android/customization/widget/OptionSelectorController.java
+++ b/src/com/android/customization/widget/OptionSelectorController.java
@@ -15,8 +15,11 @@
  */
 package com.android.customization.widget;
 
+import static com.android.internal.util.Preconditions.checkNotNull;
+
 import android.content.Context;
 import android.content.res.Resources;
+import android.graphics.Rect;
 import android.graphics.drawable.Drawable;
 import android.graphics.drawable.LayerDrawable;
 import android.text.TextUtils;
@@ -72,6 +75,8 @@
         int CENTER = 2;
     }
 
+    private static final float LINEAR_LAYOUT_HORIZONTAL_DISPLAY_OPTIONS_MAX = 4.35f;
+
     private final RecyclerView mContainer;
     private final List<T> mOptions;
     private final boolean mUseGrid;
@@ -296,10 +301,10 @@
             availableWidth = fixWidth;
         }
         int totalWidth = mContainer.getMeasuredWidth();
+        int widthPerItem = res.getDimensionPixelOffset(R.dimen.option_tile_width);
 
         if (mUseGrid) {
             int numColumns = res.getInteger(R.integer.options_grid_num_columns);
-            int widthPerItem = res.getDimensionPixelOffset(R.dimen.option_tile_width);
             int extraSpace = availableWidth - widthPerItem * numColumns;
             while (extraSpace < 0) {
                 numColumns -= 1;
@@ -316,12 +321,19 @@
         if (extraSpace >= 0) {
             mContainer.setOverScrollMode(View.OVER_SCROLL_NEVER);
         }
-        int itemSideMargin = res.getDimensionPixelOffset(R.dimen.option_tile_margin_horizontal);
-        int defaultTotalPadding = itemSideMargin * (mAdapter.getItemCount() * 2 + 2);
-        if (extraSpace > defaultTotalPadding) {
-            int spaceBetweenItems = extraSpace / (mAdapter.getItemCount() + 1);
-            itemSideMargin = spaceBetweenItems / 2;
+
+        if (mAdapter.getItemCount() >= LINEAR_LAYOUT_HORIZONTAL_DISPLAY_OPTIONS_MAX) {
+            int spaceBetweenItems = availableWidth
+                    - Math.round(widthPerItem * LINEAR_LAYOUT_HORIZONTAL_DISPLAY_OPTIONS_MAX)
+                    - mContainer.getPaddingLeft();
+            mContainer.addItemDecoration(new HorizontalBehindSpaceItemDecoration(
+                    mContainer.getContext(),
+                    spaceBetweenItems / (int) LINEAR_LAYOUT_HORIZONTAL_DISPLAY_OPTIONS_MAX));
+            return;
         }
+
+        int spaceBetweenItems = extraSpace / (mAdapter.getItemCount() + 1);
+        int itemSideMargin = spaceBetweenItems / 2;
         mContainer.addItemDecoration(new HorizontalSpacerItemDecoration(itemSideMargin));
     }
 
@@ -423,4 +435,35 @@
             return super.onRequestSendAccessibilityEvent(host, child, event);
         }
     }
+
+    /** Custom ItemDecorator to add specific spacing between items in the list. */
+    private static final class HorizontalBehindSpaceItemDecoration
+            extends RecyclerView.ItemDecoration {
+        private final int mHorizontalSpacePx;
+        private final boolean mDirectionLTR;
+
+        private HorizontalBehindSpaceItemDecoration(Context context, int horizontalSpacePx) {
+            mDirectionLTR = context.getResources().getConfiguration().getLayoutDirection()
+                    == View.LAYOUT_DIRECTION_LTR;
+            mHorizontalSpacePx = horizontalSpacePx;
+        }
+
+        @Override
+        public void getItemOffsets(Rect outRect, View view, RecyclerView recyclerView,
+                RecyclerView.State state) {
+            if (recyclerView.getAdapter() == null) {
+                return;
+            }
+
+            if (recyclerView.getChildAdapterPosition(view)
+                    != checkNotNull(recyclerView.getAdapter()).getItemCount() - 1) {
+                // Don't add spacing behind the last item
+                if (mDirectionLTR) {
+                    outRect.right = mHorizontalSpacePx;
+                } else {
+                    outRect.left = mHorizontalSpacePx;
+                }
+            }
+        }
+    }
 }