Skip to content

Commit

Permalink
Merge "Remove unnecessary targetSdk check in View" into main
Browse files Browse the repository at this point in the history
  • Loading branch information
George Mount authored and Android (Google) Code Review committed Jun 4, 2024
2 parents cbaffa7 + 373b179 commit c2e9ad2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 125 deletions.
87 changes: 14 additions & 73 deletions core/java/android/view/View.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@
import android.view.translation.ViewTranslationRequest;
import android.view.translation.ViewTranslationResponse;
import android.widget.Checkable;
import android.widget.FrameLayout;
import android.widget.ScrollBarDrawable;
import android.window.OnBackInvokedDispatcher;

Expand Down Expand Up @@ -961,21 +960,6 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
/** @hide */
public HapticScrollFeedbackProvider mScrollFeedbackProvider = null;

/**
* Use the old (broken) way of building MeasureSpecs.
*/
private static boolean sUseBrokenMakeMeasureSpec = false;

/**
* Always return a size of 0 for MeasureSpec values with a mode of UNSPECIFIED
*/
static boolean sUseZeroUnspecifiedMeasureSpec = false;

/**
* Ignore any optimizations using the measure cache.
*/
private static boolean sIgnoreMeasureCache = false;

/**
* Ignore an optimization that skips unnecessary EXACTLY layout passes.
*/
Expand Down Expand Up @@ -5845,20 +5829,6 @@ public View(Context context) {
if (!sCompatibilityDone && context != null) {
final int targetSdkVersion = context.getApplicationInfo().targetSdkVersion;

// Older apps may need this compatibility hack for measurement.
sUseBrokenMakeMeasureSpec = targetSdkVersion <= Build.VERSION_CODES.JELLY_BEAN_MR1;

// Older apps expect onMeasure() to always be called on a layout pass, regardless
// of whether a layout was requested on that View.
sIgnoreMeasureCache = targetSdkVersion < Build.VERSION_CODES.KITKAT;

// In M and newer, our widgets can pass a "hint" value in the size
// for UNSPECIFIED MeasureSpecs. This lets child views of scrolling containers
// know what the expected parent size is going to be, so e.g. list items can size
// themselves at 1/3 the size of their container. It breaks older apps though,
// specifically apps that use some popular open source libraries.
sUseZeroUnspecifiedMeasureSpec = targetSdkVersion < Build.VERSION_CODES.M;

// Old versions of the platform would give different results from
// LinearLayout measurement passes using EXACTLY and non-EXACTLY
// modes, so we always need to run an additional EXACTLY pass.
Expand Down Expand Up @@ -6035,8 +6005,6 @@ public View(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int
boolean leftPaddingDefined = false;
boolean rightPaddingDefined = false;

final int targetSdkVersion = context.getApplicationInfo().targetSdkVersion;

// Set default values.
viewFlagValues |= FOCUSABLE_AUTO;
viewFlagMasks |= FOCUSABLE_AUTO;
Expand Down Expand Up @@ -6257,11 +6225,7 @@ public View(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int
break;
//noinspection deprecation
case R.styleable.View_fadingEdge:
if (targetSdkVersion >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
// Ignore the attribute starting with ICS
break;
}
// With builds < ICS, fall through and apply fading edges
break;
case R.styleable.View_requiresFadingEdge:
final int fadingEdge = a.getInt(attr, FADING_EDGE_NONE);
if (fadingEdge != FADING_EDGE_NONE) {
Expand Down Expand Up @@ -6399,35 +6363,25 @@ public View(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int
PROVIDER_BACKGROUND));
break;
case R.styleable.View_foreground:
if (targetSdkVersion >= Build.VERSION_CODES.M || this instanceof FrameLayout) {
setForeground(a.getDrawable(attr));
}
setForeground(a.getDrawable(attr));
break;
case R.styleable.View_foregroundGravity:
if (targetSdkVersion >= Build.VERSION_CODES.M || this instanceof FrameLayout) {
setForegroundGravity(a.getInt(attr, Gravity.NO_GRAVITY));
}
setForegroundGravity(a.getInt(attr, Gravity.NO_GRAVITY));
break;
case R.styleable.View_foregroundTintMode:
if (targetSdkVersion >= Build.VERSION_CODES.M || this instanceof FrameLayout) {
setForegroundTintBlendMode(
Drawable.parseBlendMode(a.getInt(attr, -1),
null));
}
setForegroundTintBlendMode(
Drawable.parseBlendMode(a.getInt(attr, -1),
null));
break;
case R.styleable.View_foregroundTint:
if (targetSdkVersion >= Build.VERSION_CODES.M || this instanceof FrameLayout) {
setForegroundTintList(a.getColorStateList(attr));
}
setForegroundTintList(a.getColorStateList(attr));
break;
case R.styleable.View_foregroundInsidePadding:
if (targetSdkVersion >= Build.VERSION_CODES.M || this instanceof FrameLayout) {
if (mForegroundInfo == null) {
mForegroundInfo = new ForegroundInfo();
}
mForegroundInfo.mInsidePadding = a.getBoolean(attr,
mForegroundInfo.mInsidePadding);
if (mForegroundInfo == null) {
mForegroundInfo = new ForegroundInfo();
}
mForegroundInfo.mInsidePadding = a.getBoolean(attr,
mForegroundInfo.mInsidePadding);
break;
case R.styleable.View_scrollIndicators:
final int scrollIndicators =
Expand Down Expand Up @@ -13905,11 +13859,6 @@ public void setLayoutDirection(@LayoutDir int layoutDirection) {
})
@ResolvedLayoutDir
public int getLayoutDirection() {
final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
if (targetSdkVersion < Build.VERSION_CODES.JELLY_BEAN_MR1) {
mPrivateFlags2 |= PFLAG2_LAYOUT_DIRECTION_RESOLVED;
return LAYOUT_DIRECTION_RESOLVED_DEFAULT;
}
return ((mPrivateFlags2 & PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) ==
PFLAG2_LAYOUT_DIRECTION_RESOLVED_RTL) ? LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
}
Expand Down Expand Up @@ -22480,8 +22429,7 @@ private boolean hasRtlSupport() {
* RTL not supported)
*/
private boolean isRtlCompatibilityMode() {
final int targetSdkVersion = getContext().getApplicationInfo().targetSdkVersion;
return targetSdkVersion < Build.VERSION_CODES.JELLY_BEAN_MR1 || !hasRtlSupport();
return !hasRtlSupport();
}

/**
Expand Down Expand Up @@ -28149,7 +28097,7 @@ public final void measure(int widthMeasureSpec, int heightMeasureSpec) {
cacheIndex = forceLayout ? -1 : mMeasureCache.indexOfKey(key);
}

if (cacheIndex < 0 || sIgnoreMeasureCache) {
if (cacheIndex < 0) {
if (isTraversalTracingEnabled()) {
Trace.beginSection(mTracingStrings.onMeasure);
}
Expand Down Expand Up @@ -31135,11 +31083,7 @@ public static class MeasureSpec {
*/
public static int makeMeasureSpec(@IntRange(from = 0, to = (1 << MeasureSpec.MODE_SHIFT) - 1) int size,
@MeasureSpecMode int mode) {
if (sUseBrokenMakeMeasureSpec) {
return size + mode;
} else {
return (size & ~MODE_MASK) | (mode & MODE_MASK);
}
return (size & ~MODE_MASK) | (mode & MODE_MASK);
}

/**
Expand All @@ -31150,9 +31094,6 @@ public static int makeMeasureSpec(@IntRange(from = 0, to = (1 << MeasureSpec.MOD
*/
@UnsupportedAppUsage
public static int makeSafeMeasureSpec(int size, int mode) {
if (sUseZeroUnspecifiedMeasureSpec && mode == UNSPECIFIED) {
return 0;
}
return makeMeasureSpec(size, mode);
}

Expand Down
57 changes: 5 additions & 52 deletions core/java/android/view/ViewGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package android.view;

import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR1;
import static android.view.WindowInsetsAnimation.Callback.DISPATCH_MODE_CONTINUE_ON_SUBTREE;
import static android.view.WindowInsetsAnimation.Callback.DISPATCH_MODE_STOP;

Expand Down Expand Up @@ -55,7 +54,6 @@
import android.util.Pools;
import android.util.Pools.SynchronizedPool;
import android.util.SparseArray;
import android.util.SparseBooleanArray;
import android.view.WindowInsetsAnimation.Bounds;
import android.view.WindowInsetsAnimation.Callback.DispatchMode;
import android.view.accessibility.AccessibilityEvent;
Expand Down Expand Up @@ -718,10 +716,7 @@ private void initViewGroup() {
mGroupFlags |= FLAG_ANIMATION_DONE;
mGroupFlags |= FLAG_ANIMATION_CACHE;
mGroupFlags |= FLAG_ALWAYS_DRAWN_WITH_CACHE;

if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB) {
mGroupFlags |= FLAG_SPLIT_MOTION_EVENTS;
}
mGroupFlags |= FLAG_SPLIT_MOTION_EVENTS;

setDescendantFocusability(FOCUS_BEFORE_DESCENDANTS);

Expand Down Expand Up @@ -3599,48 +3594,7 @@ public void dispatchProvideStructure(ViewStructure structure) {
childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder);
} catch (IndexOutOfBoundsException e) {
childIndex = i;
if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.M) {
Log.w(TAG, "Bad getChildDrawingOrder while collecting assist @ "
+ i + " of " + childrenCount, e);
// At least one app is failing when we call getChildDrawingOrder
// at this point, so deal semi-gracefully with it by falling back
// on the basic order.
customOrder = false;
if (i > 0) {
// If we failed at the first index, there really isn't
// anything to do -- we will just proceed with the simple
// sequence order.
// Otherwise, we failed in the middle, so need to come up
// with an order for the remaining indices and use that.
// Failed at the first one, easy peasy.
int[] permutation = new int[childrenCount];
SparseBooleanArray usedIndices = new SparseBooleanArray();
// Go back and collected the indices we have done so far.
for (int j = 0; j < i; j++) {
permutation[j] = getChildDrawingOrder(childrenCount, j);
usedIndices.put(permutation[j], true);
}
// Fill in the remaining indices with indices that have not
// yet been used.
int nextIndex = 0;
for (int j = i; j < childrenCount; j++) {
while (usedIndices.get(nextIndex, false)) {
nextIndex++;
}
permutation[j] = nextIndex;
nextIndex++;
}
// Build the final view list.
preorderedList = new ArrayList<>(childrenCount);
for (int j = 0; j < childrenCount; j++) {
final int index = permutation[j];
final View child = mChildren[index];
preorderedList.add(child);
}
}
} else {
throw e;
}
throw e;
}
final View child = getAndVerifyPreorderedView(preorderedList, mChildren,
childIndex);
Expand Down Expand Up @@ -7109,12 +7063,12 @@ public static int getChildMeasureSpec(int spec, int padding, int childDimension)
} else if (childDimension == LayoutParams.MATCH_PARENT) {
// Child wants to be our size... find out how big it should
// be
resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
resultSize = size;
resultMode = MeasureSpec.UNSPECIFIED;
} else if (childDimension == LayoutParams.WRAP_CONTENT) {
// Child wants to determine its own size.... find out how
// big it should be
resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size;
resultSize = size;
resultMode = MeasureSpec.UNSPECIFIED;
}
break;
Expand Down Expand Up @@ -8662,8 +8616,7 @@ public MarginLayoutParams(Context c, AttributeSet attrs) {
}

final boolean hasRtlSupport = c.getApplicationInfo().hasRtlSupport();
final int targetSdkVersion = c.getApplicationInfo().targetSdkVersion;
if (targetSdkVersion < JELLY_BEAN_MR1 || !hasRtlSupport) {
if (!hasRtlSupport) {
mMarginFlags |= RTL_COMPATIBILITY_MODE_MASK;
}

Expand Down

0 comments on commit c2e9ad2

Please sign in to comment.