Fix CollapsingToolbarLayout displaying over icons

CTL scales it title which works in most situations.
There are edge cases though where the title can be
drawn on the Toolbars contents, namely icons.

This CL fixes the edge cases where the collapsed
and expanded text sizes are similar in size, which
means that there is limited/no scaling happening
while scrolling. In this instance we need to cap
any available width when expanded, so that it
'scales' to match the collapsed width when collapsed.

BUG: 28972127
Change-Id: I2c56e77d30054aa60d93f7ca3b91813fde1351f5
diff --git a/design/src/android/support/design/widget/CollapsingTextHelper.java b/design/src/android/support/design/widget/CollapsingTextHelper.java
index 4ddbd1d..79ff63d 100644
--- a/design/src/android/support/design/widget/CollapsingTextHelper.java
+++ b/design/src/android/support/design/widget/CollapsingTextHelper.java
@@ -507,26 +507,27 @@
     private void calculateUsingTextSize(final float textSize) {
         if (mText == null) return;
 
+        final float collapsedWidth = mCollapsedBounds.width();
+        final float expandedWidth = mExpandedBounds.width();
+
         final float availableWidth;
         final float newTextSize;
         boolean updateDrawText = false;
 
         if (isClose(textSize, mCollapsedTextSize)) {
-            availableWidth = mCollapsedBounds.width();
             newTextSize = mCollapsedTextSize;
             mScale = 1f;
             if (mCurrentTypeface != mCollapsedTypeface) {
                 mCurrentTypeface = mCollapsedTypeface;
                 updateDrawText = true;
             }
+            availableWidth = collapsedWidth;
         } else {
-            availableWidth = mExpandedBounds.width();
             newTextSize = mExpandedTextSize;
             if (mCurrentTypeface != mExpandedTypeface) {
                 mCurrentTypeface = mExpandedTypeface;
                 updateDrawText = true;
             }
-
             if (isClose(textSize, mExpandedTextSize)) {
                 // If we're close to the expanded text size, snap to it and use a scale of 1
                 mScale = 1f;
@@ -534,6 +535,21 @@
                 // Else, we'll scale down from the expanded text size
                 mScale = textSize / mExpandedTextSize;
             }
+
+            final float textSizeRatio = mCollapsedTextSize / mExpandedTextSize;
+            // This is the size of the expanded bounds when it is scaled to match the
+            // collapsed text size
+            final float scaledDownWidth = expandedWidth * textSizeRatio;
+
+            if (scaledDownWidth > collapsedWidth) {
+                // If the scaled down size is larger than the actual collapsed width, we need to
+                // cap the available width so that when the expanded text scales down, it matches
+                // the collapsed width
+                availableWidth = Math.min(collapsedWidth / textSizeRatio, expandedWidth);
+            } else {
+                // Otherwise we'll just use the expanded width
+                availableWidth = expandedWidth;
+            }
         }
 
         if (availableWidth > 0) {