Provide valid width to children of CalculatorScrollView

Bug: 30929825
Bug: 30954953

On platforms prior to M, getChildMeasureSpec will provide 0 for size
if the mode is UNSPECIFIED.

Change-Id: Ief90f476e29b12719e6245ff437bd35c209b0bb0
diff --git a/src/com/android/calculator2/CalculatorScrollView.java b/src/com/android/calculator2/CalculatorScrollView.java
index bcf5650..018ad10 100644
--- a/src/com/android/calculator2/CalculatorScrollView.java
+++ b/src/com/android/calculator2/CalculatorScrollView.java
@@ -22,6 +22,10 @@
 import android.view.ViewGroup;
 import android.widget.HorizontalScrollView;
 
+import static android.view.View.MeasureSpec.UNSPECIFIED;
+import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
+import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
+
 public class CalculatorScrollView extends HorizontalScrollView {
 
     public CalculatorScrollView(Context context) {
@@ -36,17 +40,26 @@
         super(context, attrs, defStyleAttr);
     }
 
+    private static int getChildMeasureSpecCompat(int spec, int padding, int childDimension) {
+        if (MeasureSpec.getMode(spec) == UNSPECIFIED
+                && (childDimension == MATCH_PARENT || childDimension == WRAP_CONTENT)) {
+            final int size = Math.max(0, MeasureSpec.getSize(spec) - padding);
+            return MeasureSpec.makeMeasureSpec(size, UNSPECIFIED);
+        }
+        return ViewGroup.getChildMeasureSpec(spec, padding, childDimension);
+    }
+
     @Override
     protected void measureChild(View child, int parentWidthMeasureSpec,
             int parentHeightMeasureSpec) {
         // Allow child to be as wide as they want.
         parentWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
-                MeasureSpec.getSize(parentWidthMeasureSpec), MeasureSpec.UNSPECIFIED);
+                MeasureSpec.getSize(parentWidthMeasureSpec), UNSPECIFIED);
 
         final ViewGroup.LayoutParams lp = child.getLayoutParams();
-        final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
+        final int childWidthMeasureSpec = getChildMeasureSpecCompat(parentWidthMeasureSpec,
                 0 /* padding */, lp.width);
-        final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
+        final int childHeightMeasureSpec = getChildMeasureSpecCompat(parentHeightMeasureSpec,
                 getPaddingTop() + getPaddingBottom(), lp.height);
 
         child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
@@ -57,12 +70,12 @@
             int parentHeightMeasureSpec, int heightUsed) {
         // Allow child to be as wide as they want.
         parentWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
-                MeasureSpec.getSize(parentWidthMeasureSpec), MeasureSpec.UNSPECIFIED);
+                MeasureSpec.getSize(parentWidthMeasureSpec), UNSPECIFIED);
 
         final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
-        final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
+        final int childWidthMeasureSpec = getChildMeasureSpecCompat(parentWidthMeasureSpec,
                 lp.leftMargin + lp.rightMargin, lp.width);
-        final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
+        final int childHeightMeasureSpec = getChildMeasureSpecCompat(parentHeightMeasureSpec,
                 getPaddingTop() + getPaddingBottom() + lp.topMargin + lp.bottomMargin, lp.height);
 
         child.measure(childWidthMeasureSpec, childHeightMeasureSpec);