Fix the size of Add button to 328dp

Added new "Add" or "Select" button for preview on long press. This
button width is fixed to 328dp.

Bug: 207753575
Test: atest PreviewSingleSelectTest#testPreview_addButtonWidth
Test: atest PreviewMultiSelectLongPressTest#testPreview_selectButtonWidth
Change-Id: Iea0976526088f375a353dc6c73e6180423533769
diff --git a/res/layout/fragment_preview.xml b/res/layout/fragment_preview.xml
index 1d6c3f8..2d24db2 100644
--- a/res/layout/fragment_preview.xml
+++ b/res/layout/fragment_preview.xml
@@ -50,6 +50,19 @@
         android:layout_height="@dimen/picker_bottom_bar_size"
         android:paddingHorizontal="@dimen/preview_buttons_padding_horizontal">
 
+        <!-- Buttons for Preview on Long press. Visible by default -->
+        <Button
+            android:id="@+id/preview_add_or_select_button"
+            android:layout_width="@dimen/preview_add_or_select_width"
+            android:layout_height="wrap_content"
+            android:layout_gravity="center"
+            android:backgroundTint="@color/preview_default_blue"
+            android:text="@string/add"
+            android:textAllCaps="false"
+            android:textColor="@color/preview_default_grey"
+            style="@style/MaterialButtonStyle"/>
+
+        <!-- Buttons for Preview on View Selected. Hidden by default -->
         <Button
             android:id="@+id/preview_select_check_button"
             android:layout_width="wrap_content"
@@ -62,10 +75,11 @@
             android:textAllCaps="false"
             android:text="@string/deselect"
             android:textColor="@color/picker_default_white"
+            android:visibility="gone"
             style="@style/MaterialBorderlessButtonStyle"/>
 
         <Button
-            android:id="@+id/preview_add_or_select_button"
+            android:id="@+id/preview_add_button"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:layout_gravity="end|center_vertical"
@@ -73,7 +87,7 @@
             android:text="@string/add"
             android:textAllCaps="false"
             android:textColor="@color/preview_default_grey"
+            android:visibility="gone"
             style="@style/MaterialButtonStyle"/>
-
     </FrameLayout>
 </FrameLayout>
diff --git a/res/values/dimens.xml b/res/values/dimens.xml
index 88fbe53..ebe5ac0 100644
--- a/res/values/dimens.xml
+++ b/res/values/dimens.xml
@@ -67,6 +67,8 @@
     <dimen name="preview_deselect_padding_start">2dp</dimen>
     <dimen name="preview_viewpager_margin">20dp</dimen>
     <dimen name="preview_gif_icon_size">32dp</dimen>
+    <dimen name="preview_add_or_select_width">328dp</dimen>
+
     <!-- PhotoPicker Preview text -->
     <dimen name="preview_special_format_text_size">12sp</dimen>
     <dimen name="preview_special_format_text_margin">8dp</dimen>
diff --git a/src/com/android/providers/media/photopicker/ui/PreviewFragment.java b/src/com/android/providers/media/photopicker/ui/PreviewFragment.java
index 118c90b..a906c89 100644
--- a/src/com/android/providers/media/photopicker/ui/PreviewFragment.java
+++ b/src/com/android/providers/media/photopicker/ui/PreviewFragment.java
@@ -168,13 +168,11 @@
                     + " is not set");
         }
 
-        final Button addOrSelectButton = view.findViewById(R.id.preview_add_or_select_button);
-        final Button selectCheckButton = view.findViewById(R.id.preview_select_check_button);
         final int previewType = args.getInt(PREVIEW_TYPE, -1);
         if (previewType == PREVIEW_ON_LONG_PRESS) {
-            setUpPreviewLayoutForLongPress(addOrSelectButton, selectCheckButton);
+            setUpPreviewLayoutForLongPress(view);
         } else if (previewType == PREVIEW_ON_VIEW_SELECTED) {
-            setUpPreviewLayoutForViewSelected(addOrSelectButton, selectCheckButton);
+            setUpPreviewLayoutForViewSelected(view);
         } else {
             // We are willing to crash PhotoPickerActivity because this error might only happen
             // during development.
@@ -185,15 +183,8 @@
     /**
      * Adjusts the select/add button layout for preview on LongPress
      */
-    private void setUpPreviewLayoutForLongPress(@NonNull Button addOrSelectButton,
-            @NonNull Button selectCheckButton) {
-        final LayoutParams layoutParams
-                = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
-        addOrSelectButton.setLayoutParams(layoutParams);
-
-        // This button won't be visible in Preview on LongPress. Select/Deselect action for multi
-        // select mode is handled by addOrSelect button.
-        selectCheckButton.setVisibility(View.GONE);
+    private void setUpPreviewLayoutForLongPress(@NonNull View view) {
+        final Button addOrSelectButton = view.findViewById(R.id.preview_add_or_select_button);
 
         // Preview on Long Press will reuse AddOrSelect button as
         // * Add button - Button with text "Add" - for single select mode
@@ -222,14 +213,21 @@
     /**
      * Adjusts the layout based on Multi select and adds appropriate onClick listeners
      */
-    private void setUpPreviewLayoutForViewSelected(@NonNull Button addButton,
-            @NonNull Button selectButton) {
+    private void setUpPreviewLayoutForViewSelected(@NonNull View view) {
+        // Hide addOrSelect button of long press, we have a separate add button for view selected
+        final Button addOrSelectButton = view.findViewById(R.id.preview_add_or_select_button);
+        addOrSelectButton.setVisibility(View.GONE);
+
+        final Button viewSelectedAddButton = view.findViewById(R.id.preview_add_button);
+        viewSelectedAddButton.setVisibility(View.VISIBLE);
         // On clicking add button we return the picker result to calling app.
         // This destroys PickerActivity and all fragments.
-        addButton.setOnClickListener(v -> {
+        viewSelectedAddButton.setOnClickListener(v -> {
             ((PhotoPickerActivity) getActivity()).setResultAndFinishSelf();
         });
 
+        final Button selectButton = view.findViewById(R.id.preview_select_check_button);
+        selectButton.setVisibility(View.VISIBLE);
         // Update the select icon and text according to the state of selection while swiping
         // between photos
         mOnPageChangeCallBack = new OnPageChangeCallBack(selectButton);
@@ -237,7 +235,7 @@
 
         // Update add button text to include number of items selected.
         mSelection.getSelectedItemCount().observe(this, selectedItemCount -> {
-            addButton.setText(generateAddButtonString(getContext(), selectedItemCount));
+            viewSelectedAddButton.setText(generateAddButtonString(getContext(), selectedItemCount));
         });
 
         selectButton.setOnClickListener(
diff --git a/tests/src/com/android/providers/media/photopicker/espresso/PhotoPickerBaseTest.java b/tests/src/com/android/providers/media/photopicker/espresso/PhotoPickerBaseTest.java
index 76f0c78..60057c2 100644
--- a/tests/src/com/android/providers/media/photopicker/espresso/PhotoPickerBaseTest.java
+++ b/tests/src/com/android/providers/media/photopicker/espresso/PhotoPickerBaseTest.java
@@ -65,6 +65,10 @@
     protected static final int DRAG_BAR_ID = R.id.drag_bar;
     protected static final int PREVIEW_GIF_ID = R.id.preview_gif;
     protected static final int PREVIEW_MOTION_PHOTO_ID = R.id.preview_motion_photo;
+    protected static final int PREVIEW_ADD_OR_SELECT_BUTTON_ID = R.id.preview_add_or_select_button;
+
+    protected static final int DIMEN_PREVIEW_ADD_OR_SELECT_WIDTH
+            = R.dimen.preview_add_or_select_width;
 
     /**
      * The position of the first image item in the grid on the Photos tab
diff --git a/tests/src/com/android/providers/media/photopicker/espresso/PreviewMultiSelectLongPressTest.java b/tests/src/com/android/providers/media/photopicker/espresso/PreviewMultiSelectLongPressTest.java
index 7bbf482..a59d02b 100644
--- a/tests/src/com/android/providers/media/photopicker/espresso/PreviewMultiSelectLongPressTest.java
+++ b/tests/src/com/android/providers/media/photopicker/espresso/PreviewMultiSelectLongPressTest.java
@@ -16,6 +16,11 @@
 
 package com.android.providers.media.photopicker.espresso;
 
+import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
+import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
+import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
+import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
+
 import static androidx.test.espresso.Espresso.onView;
 import static androidx.test.espresso.action.ViewActions.click;
 import static androidx.test.espresso.assertion.ViewAssertions.doesNotExist;
@@ -31,10 +36,13 @@
 import static com.android.providers.media.photopicker.espresso.RecyclerViewTestUtils.assertItemNotSelected;
 import static com.android.providers.media.photopicker.espresso.RecyclerViewTestUtils.assertItemSelected;
 import static com.android.providers.media.photopicker.espresso.RecyclerViewTestUtils.longClickItem;
+
 import static com.google.common.truth.Truth.assertThat;
 
 import static org.hamcrest.Matchers.not;
 
+import android.widget.Button;
+
 import androidx.lifecycle.ViewModelProvider;
 import androidx.test.espresso.Espresso;
 import androidx.test.espresso.IdlingRegistry;
@@ -114,7 +122,7 @@
 
         registerIdlingResourceAndWaitForIdle();
 
-        final int selectButtonId = R.id.preview_add_or_select_button;
+        final int selectButtonId = PREVIEW_ADD_OR_SELECT_BUTTON_ID;
         // Select the item within Preview
         onView(withId(selectButtonId)).perform(click());
         // Check that button text is changed to "deselect"
@@ -181,6 +189,46 @@
         onView(withId(imageViewId)).check(matches(isDisplayed()));
     }
 
+    @Test
+    public void testPreview_selectButtonWidth() {
+        onView(withId(PICKER_TAB_RECYCLERVIEW_ID)).check(matches(isDisplayed()));
+        // Navigate to preview
+        longClickItem(PICKER_TAB_RECYCLERVIEW_ID, IMAGE_1_POSITION, ICON_THUMBNAIL_ID);
+
+        registerIdlingResourceAndWaitForIdle();
+        // Check that Select button is visible
+        onView(withId(PREVIEW_ADD_OR_SELECT_BUTTON_ID)).check(matches(isDisplayed()));
+        onView(withId(PREVIEW_ADD_OR_SELECT_BUTTON_ID)).check(matches(withText(R.string.select)));
+
+        mRule.getScenario().onActivity(activity -> {
+            activity.setRequestedOrientation(SCREEN_ORIENTATION_PORTRAIT);
+        });
+        mRule.getScenario().onActivity(activity -> {
+            assertThat(activity.getResources().getConfiguration().orientation)
+                    .isEqualTo(ORIENTATION_PORTRAIT);
+            final Button addOrSelectButton
+                    = activity.findViewById(PREVIEW_ADD_OR_SELECT_BUTTON_ID);
+            final int expectedAddOrSelectButtonWidth = activity.getResources()
+                    .getDimensionPixelOffset(DIMEN_PREVIEW_ADD_OR_SELECT_WIDTH);
+            // Check that button width in portrait mode = R.dimen.preview_add_or_select_width
+            assertThat(addOrSelectButton.getWidth()).isEqualTo(expectedAddOrSelectButtonWidth);
+        });
+
+        mRule.getScenario().onActivity(activity -> {
+            activity.setRequestedOrientation(SCREEN_ORIENTATION_LANDSCAPE);
+        });
+        mRule.getScenario().onActivity(activity -> {
+            assertThat(activity.getResources().getConfiguration().orientation)
+                    .isEqualTo(ORIENTATION_LANDSCAPE);
+            final Button addOrSelectButton
+                    = activity.findViewById(PREVIEW_ADD_OR_SELECT_BUTTON_ID);
+            final int expectedAddOrSelectButtonWidth = activity.getResources()
+                    .getDimensionPixelOffset(DIMEN_PREVIEW_ADD_OR_SELECT_WIDTH);
+            // Check that button width in landscape mode is = R.dimen.preview_add_or_select_width
+            assertThat(addOrSelectButton.getWidth()).isEqualTo(expectedAddOrSelectButtonWidth);
+        });
+    }
+
     private void registerIdlingResourceAndWaitForIdle() {
         mRule.getScenario().onActivity((activity -> IdlingRegistry.getInstance().register(
                 new ViewPager2IdlingResource(activity.findViewById(R.id.preview_viewPager)))));
@@ -193,15 +241,17 @@
 
     private void assertMultiSelectLongPressCommonLayoutMatches(boolean isSelected) {
         onView(withId(R.id.preview_viewPager)).check(matches(isDisplayed()));
-        onView(withId(R.id.preview_select_check_button)).check(matches(not(isDisplayed())));
-        onView(withId(R.id.preview_add_or_select_button)).check(matches(isDisplayed()));
+        onView(withId(PREVIEW_ADD_OR_SELECT_BUTTON_ID)).check(matches(isDisplayed()));
         // Verify that the text in AddOrSelect button
         if (isSelected) {
-            onView(withId(R.id.preview_add_or_select_button)).check(
-                    matches(withText(R.string.deselect)));
+            onView(withId(PREVIEW_ADD_OR_SELECT_BUTTON_ID))
+                    .check(matches(withText(R.string.deselect)));
         } else {
-            onView(withId(R.id.preview_add_or_select_button)).check(
-                    matches(withText(R.string.select)));
+            onView(withId(PREVIEW_ADD_OR_SELECT_BUTTON_ID))
+                    .check(matches(withText(R.string.select)));
         }
+
+        onView(withId(R.id.preview_select_check_button)).check(matches(not(isDisplayed())));
+        onView(withId(R.id.preview_add_button)).check(matches(not(isDisplayed())));
     }
 }
\ No newline at end of file
diff --git a/tests/src/com/android/providers/media/photopicker/espresso/PreviewMultiSelectTest.java b/tests/src/com/android/providers/media/photopicker/espresso/PreviewMultiSelectTest.java
index d742180..e30c0eb 100644
--- a/tests/src/com/android/providers/media/photopicker/espresso/PreviewMultiSelectTest.java
+++ b/tests/src/com/android/providers/media/photopicker/espresso/PreviewMultiSelectTest.java
@@ -86,6 +86,8 @@
         onView(withId(DRAG_BAR_ID)).check(matches(not(isDisplayed())));
 
         assertMultiSelectPreviewCommonLayoutDisplayed();
+        onView(withId(PREVIEW_ADD_OR_SELECT_BUTTON_ID)).check(matches(not(isDisplayed())));
+
         // Verify ImageView is displayed
         onView(withId(PREVIEW_IMAGE_VIEW_ID)).check(matches(isCompletelyDisplayed()));
 
@@ -111,7 +113,7 @@
 
         final String addButtonString =
                 getTargetContext().getResources().getString(R.string.add);
-        final int previewAddButtonId = R.id.preview_add_or_select_button;
+        final int previewAddButtonId = R.id.preview_add_button;
         final int previewSelectButtonId = R.id.preview_select_check_button;
         final String deselectString =
                 getTargetContext().getResources().getString(R.string.deselect);
@@ -369,7 +371,7 @@
 
     private void assertMultiSelectPreviewCommonLayoutDisplayed() {
         onView(withId(PREVIEW_VIEW_PAGER_ID)).check(matches(isDisplayed()));
-        onView(withId(R.id.preview_add_or_select_button)).check(matches(isDisplayed()));
+        onView(withId(R.id.preview_add_button)).check(matches(isDisplayed()));
         onView(withId(R.id.preview_select_check_button)).check(matches(isDisplayed()));
         onView(withId(R.id.preview_select_check_button)).check(matches(isSelected()));
     }
diff --git a/tests/src/com/android/providers/media/photopicker/espresso/PreviewSingleSelectTest.java b/tests/src/com/android/providers/media/photopicker/espresso/PreviewSingleSelectTest.java
index 8252d37..d812ee7 100644
--- a/tests/src/com/android/providers/media/photopicker/espresso/PreviewSingleSelectTest.java
+++ b/tests/src/com/android/providers/media/photopicker/espresso/PreviewSingleSelectTest.java
@@ -18,6 +18,8 @@
 
 import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
 import static android.content.pm.ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
+import static android.content.res.Configuration.ORIENTATION_LANDSCAPE;
+import static android.content.res.Configuration.ORIENTATION_PORTRAIT;
 
 import static androidx.test.espresso.Espresso.onView;
 import static androidx.test.espresso.action.ViewActions.click;
@@ -38,10 +40,10 @@
 import static org.hamcrest.Matchers.not;
 
 import android.app.Activity;
-import android.content.res.Configuration;
 import android.graphics.drawable.ColorDrawable;
 import android.graphics.drawable.Drawable;
 import android.view.View;
+import android.widget.Button;
 
 import androidx.appcompat.widget.Toolbar;
 import androidx.test.espresso.Espresso;
@@ -148,8 +150,8 @@
         });
 
         mRule.getScenario().onActivity(activity -> {
-            assertThat(activity.getResources().getConfiguration().orientation).isEqualTo(
-                    Configuration.ORIENTATION_PORTRAIT);
+            assertThat(activity.getResources().getConfiguration().orientation)
+                    .isEqualTo(ORIENTATION_PORTRAIT);
         });
 
         onView(withId(PICKER_TAB_RECYCLERVIEW_ID)).check(matches(isDisplayed()));
@@ -173,8 +175,8 @@
         });
 
         mRule.getScenario().onActivity(activity -> {
-            assertThat(activity.getResources().getConfiguration().orientation).isEqualTo(
-                    Configuration.ORIENTATION_LANDSCAPE);
+            assertThat(activity.getResources().getConfiguration().orientation)
+                    .isEqualTo(ORIENTATION_LANDSCAPE);
         });
 
         onView(withId(PICKER_TAB_RECYCLERVIEW_ID)).check(matches(isDisplayed()));
@@ -192,6 +194,46 @@
         });
     }
 
+    @Test
+    public void testPreview_addButtonWidth() {
+        onView(withId(PICKER_TAB_RECYCLERVIEW_ID)).check(matches(isDisplayed()));
+        // Navigate to preview
+        longClickItem(PICKER_TAB_RECYCLERVIEW_ID, IMAGE_1_POSITION, ICON_THUMBNAIL_ID);
+
+        registerIdlingResourceAndWaitForIdle();
+        // Check that Add button is visible
+        onView(withId(PREVIEW_ADD_OR_SELECT_BUTTON_ID)).check(matches(isDisplayed()));
+        onView(withId(PREVIEW_ADD_OR_SELECT_BUTTON_ID)).check(matches(withText(R.string.add)));
+
+        mRule.getScenario().onActivity(activity -> {
+            activity.setRequestedOrientation(ORIENTATION_PORTRAIT);
+        });
+        mRule.getScenario().onActivity(activity -> {
+            assertThat(activity.getResources().getConfiguration().orientation)
+                    .isEqualTo(ORIENTATION_PORTRAIT);
+            final Button addOrSelectButton
+                    = activity.findViewById(PREVIEW_ADD_OR_SELECT_BUTTON_ID);
+            final int expectedAddOrSelectButtonWidth = activity.getResources()
+                    .getDimensionPixelOffset(DIMEN_PREVIEW_ADD_OR_SELECT_WIDTH);
+            // Check that button width in portrait mode is = R.dimen.preview_add_or_select_width
+            assertThat(addOrSelectButton.getWidth()).isEqualTo(expectedAddOrSelectButtonWidth);
+        });
+
+        mRule.getScenario().onActivity(activity -> {
+            activity.setRequestedOrientation(SCREEN_ORIENTATION_LANDSCAPE);
+        });
+        mRule.getScenario().onActivity(activity -> {
+            assertThat(activity.getResources().getConfiguration().orientation)
+                    .isEqualTo(ORIENTATION_LANDSCAPE);
+            final Button addOrSelectButton
+                    = activity.findViewById(PREVIEW_ADD_OR_SELECT_BUTTON_ID);
+            final int expectedAddOrSelectButtonWidth = activity.getResources()
+                    .getDimensionPixelOffset(DIMEN_PREVIEW_ADD_OR_SELECT_WIDTH);
+            // Check that button width in landscape mode is == R.dimen.preview_add_or_select_width
+            assertThat(addOrSelectButton.getWidth()).isEqualTo(expectedAddOrSelectButtonWidth);
+        });
+    }
+
     private void registerIdlingResourceAndWaitForIdle() {
         mRule.getScenario().onActivity((activity -> IdlingRegistry.getInstance().register(
                 new ViewPager2IdlingResource(activity.findViewById(R.id.preview_viewPager)))));
@@ -216,9 +258,11 @@
 
     private void assertSingleSelectCommonLayoutMatches() {
         onView(withId(R.id.preview_viewPager)).check(matches(isDisplayed()));
-        onView(withId(R.id.preview_select_check_button)).check(matches(not(isDisplayed())));
-        onView(withId(R.id.preview_add_or_select_button)).check(matches(isDisplayed()));
+        onView(withId(PREVIEW_ADD_OR_SELECT_BUTTON_ID)).check(matches(isDisplayed()));
         // Verify that the text in Add button
-        onView(withId(R.id.preview_add_or_select_button)).check(matches(withText(R.string.add)));
+        onView(withId(PREVIEW_ADD_OR_SELECT_BUTTON_ID)).check(matches(withText(R.string.add)));
+
+        onView(withId(R.id.preview_select_check_button)).check(matches(not(isDisplayed())));
+        onView(withId(R.id.preview_add_button)).check(matches(not(isDisplayed())));
     }
 }
diff --git a/tests/src/com/android/providers/media/photopicker/espresso/SpecialFormatSingleSelectTest.java b/tests/src/com/android/providers/media/photopicker/espresso/SpecialFormatSingleSelectTest.java
index 6d7e219..09d6fbb 100644
--- a/tests/src/com/android/providers/media/photopicker/espresso/SpecialFormatSingleSelectTest.java
+++ b/tests/src/com/android/providers/media/photopicker/espresso/SpecialFormatSingleSelectTest.java
@@ -184,9 +184,11 @@
 
     private void assertSingleSelectCommonLayoutMatches() {
         onView(withId(R.id.preview_viewPager)).check(matches(isDisplayed()));
-        onView(withId(R.id.preview_select_check_button)).check(matches(not(isDisplayed())));
-        onView(withId(R.id.preview_add_or_select_button)).check(matches(isDisplayed()));
+        onView(withId(PREVIEW_ADD_OR_SELECT_BUTTON_ID)).check(matches(isDisplayed()));
         // Verify that the text in Add button
-        onView(withId(R.id.preview_add_or_select_button)).check(matches(withText(R.string.add)));
+        onView(withId(PREVIEW_ADD_OR_SELECT_BUTTON_ID)).check(matches(withText(R.string.add)));
+
+        onView(withId(R.id.preview_select_check_button)).check(matches(not(isDisplayed())));
+        onView(withId(R.id.preview_add_button)).check(matches(not(isDisplayed())));
     }
 }