Made the expand button positioning conditional on expanded state

The expand button now allows content to flow below the header
when expanded and is dynamically placed in the layout
to push over the content.
Also made sure that match parent views don't artificially
increase the size of RemeasurableLinearLayouts as otherwise
the content would be stuck too big at times.

Bug: 150905003
Test: Visual, observe expand buttons when expanded / collapsed
Change-Id: I933b8a74ab6cacd48c98b1a9667b0e30c82bda11
diff --git a/core/java/com/android/internal/widget/ConversationLayout.java b/core/java/com/android/internal/widget/ConversationLayout.java
index 7267718..545c8a6 100644
--- a/core/java/com/android/internal/widget/ConversationLayout.java
+++ b/core/java/com/android/internal/widget/ConversationLayout.java
@@ -107,6 +107,7 @@
     private View mConversationIconBadge;
     private Icon mLargeIcon;
     private View mExpandButtonContainer;
+    private ViewGroup mExpandButtonAndContentContainer;
     private NotificationExpandButton mExpandButton;
     private int mExpandButtonExpandedTopMargin;
     private int mBadgedSideMargins;
@@ -114,6 +115,7 @@
     private int mIconSizeCentered;
     private View mIcon;
     private int mExpandedGroupTopMargin;
+    private int mExpandButtonExpandedSize;
 
     public ConversationLayout(@NonNull Context context) {
         super(context);
@@ -153,9 +155,12 @@
         mConversationIconBadge = findViewById(R.id.conversation_icon_badge);
         mHeaderText = findViewById(R.id.header_text);
         mExpandButtonContainer = findViewById(R.id.expand_button_container);
+        mExpandButtonAndContentContainer = findViewById(R.id.expand_button_and_content_container);
         mExpandButton = findViewById(R.id.expand_button);
         mExpandButtonExpandedTopMargin = getResources().getDimensionPixelSize(
                 R.dimen.conversation_expand_button_top_margin_expanded);
+        mExpandButtonExpandedSize = getResources().getDimensionPixelSize(
+                R.dimen.conversation_expand_button_expanded_size);
         mBadgedSideMargins = getResources().getDimensionPixelSize(
                 R.dimen.conversation_badge_side_margin);
         mIconSizeBadged = getResources().getDimensionPixelSize(
@@ -713,19 +718,36 @@
         int contentDescriptionId;
         int gravity;
         int topMargin = 0;
+        ViewGroup newContainer;
+        int newContainerHeight;
         if (mIsCollapsed) {
             drawableId = R.drawable.ic_expand_notification;
             contentDescriptionId = R.string.expand_button_content_description_collapsed;
             gravity = Gravity.CENTER;
+            newContainer = mExpandButtonAndContentContainer;
+            newContainerHeight = LayoutParams.MATCH_PARENT;
         } else {
             drawableId = R.drawable.ic_collapse_notification;
             contentDescriptionId = R.string.expand_button_content_description_expanded;
             gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
             topMargin = mExpandButtonExpandedTopMargin;
+            newContainer = this;
+            newContainerHeight = mExpandButtonExpandedSize;
         }
         mExpandButton.setImageDrawable(getContext().getDrawable(drawableId));
         mExpandButton.setColorFilter(mExpandButton.getOriginalNotificationColor());
 
+        // We need to make sure that the expand button is in the linearlayout pushing over the
+        // content when collapsed, but allows the content to flow under it when expanded.
+        if (newContainer != mExpandButtonContainer.getParent()) {
+            ((ViewGroup) mExpandButtonContainer.getParent()).removeView(mExpandButtonContainer);
+            newContainer.addView(mExpandButtonContainer);
+            MarginLayoutParams layoutParams =
+                    (MarginLayoutParams) mExpandButtonContainer.getLayoutParams();
+            layoutParams.height = newContainerHeight;
+            mExpandButtonContainer.setLayoutParams(layoutParams);
+        }
+
         // update if the expand button is centered
         FrameLayout.LayoutParams layoutParams = (LayoutParams) mExpandButton.getLayoutParams();
         layoutParams.gravity = gravity;
@@ -733,6 +755,7 @@
         mExpandButton.setLayoutParams(layoutParams);
 
         mExpandButtonContainer.setContentDescription(mContext.getText(contentDescriptionId));
+
     }
 
     public void updateExpandability(boolean expandable, @Nullable OnClickListener onClickListener) {