From cb8488822cc34c933b8e7f6f0907f4cbb33485d9 Mon Sep 17 00:00:00 2001 From: Chet Haase Date: Wed, 21 Mar 2018 14:09:44 -0700 Subject: Add targetSdk check around new LinearLayout weighted measure behavior Bug: 73827180 Test: Existing CTS tests in LinearLayoutTest Change-Id: I88dfde3743d0f954cd275be6a0032fe30ef55c03 (cherry picked from commit 28230e5efa86aa324fef3e95395d61a4518fa804) --- core/java/android/os/Build.java | 2 ++ core/java/android/widget/LinearLayout.java | 26 ++++++++++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) (limited to 'core/java/android') diff --git a/core/java/android/os/Build.java b/core/java/android/os/Build.java index 8378a829ed66..7162b8ada997 100644 --- a/core/java/android/os/Build.java +++ b/core/java/android/os/Build.java @@ -907,6 +907,8 @@ public class Build { *
  • {@link android.app.Service#startForeground Service.startForeground} requires * that apps hold the permission * {@link android.Manifest.permission#FOREGROUND_SERVICE}.
  • + *
  • {@link android.widget.LinearLayout} will always remeasure weighted children, + * even if there is no excess space.
  • * */ public static final int P = CUR_DEVELOPMENT; // STOPSHIP Replace with the real version. diff --git a/core/java/android/widget/LinearLayout.java b/core/java/android/widget/LinearLayout.java index d32e93c7a862..40f9652c4c9b 100644 --- a/core/java/android/widget/LinearLayout.java +++ b/core/java/android/widget/LinearLayout.java @@ -217,6 +217,17 @@ public class LinearLayout extends ViewGroup { private int mLayoutDirection = View.LAYOUT_DIRECTION_UNDEFINED; + /** + * Signals that compatibility booleans have been initialized according to + * target SDK versions. + */ + private static boolean sCompatibilityDone = false; + + /** + * Behavior change in P; always remeasure weighted children, regardless of excess space. + */ + private static boolean sRemeasureWeightedChildren = true; + public LinearLayout(Context context) { this(context, null); } @@ -232,6 +243,15 @@ public class LinearLayout extends ViewGroup { public LinearLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); + if (!sCompatibilityDone && context != null) { + final int targetSdkVersion = context.getApplicationInfo().targetSdkVersion; + + // Older apps only remeasure non-zero children + sRemeasureWeightedChildren = targetSdkVersion >= Build.VERSION_CODES.P; + + sCompatibilityDone = true; + } + final TypedArray a = context.obtainStyledAttributes( attrs, com.android.internal.R.styleable.LinearLayout, defStyleAttr, defStyleRes); @@ -917,7 +937,8 @@ public class LinearLayout extends ViewGroup { // measurement on any children, we need to measure them now. int remainingExcess = heightSize - mTotalLength + (mAllowInconsistentMeasurement ? 0 : consumedExcessSpace); - if (skippedMeasure || totalWeight > 0.0f) { + if (skippedMeasure + || ((sRemeasureWeightedChildren || remainingExcess != 0) && totalWeight > 0.0f)) { float remainingWeightSum = mWeightSum > 0.0f ? mWeightSum : totalWeight; mTotalLength = 0; @@ -1300,7 +1321,8 @@ public class LinearLayout extends ViewGroup { // measurement on any children, we need to measure them now. int remainingExcess = widthSize - mTotalLength + (mAllowInconsistentMeasurement ? 0 : usedExcessSpace); - if (skippedMeasure || totalWeight > 0.0f) { + if (skippedMeasure + || ((sRemeasureWeightedChildren || remainingExcess != 0) && totalWeight > 0.0f)) { float remainingWeightSum = mWeightSum > 0.0f ? mWeightSum : totalWeight; maxAscent[0] = maxAscent[1] = maxAscent[2] = maxAscent[3] = -1; -- cgit v1.2.3