Merge "Fix bug #8335710 TextView can't clear CompoundDrawables" into jb-mr2-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
2d18e52b00
@@ -316,7 +316,7 @@ public class Editor {
|
||||
private void setErrorIcon(Drawable icon) {
|
||||
Drawables dr = mTextView.mDrawables;
|
||||
if (dr == null) {
|
||||
mTextView.mDrawables = dr = new Drawables();
|
||||
mTextView.mDrawables = dr = new Drawables(mTextView.getContext());
|
||||
}
|
||||
dr.setErrorDrawable(icon, mTextView);
|
||||
|
||||
|
||||
@@ -136,6 +136,8 @@ import java.util.ArrayList;
|
||||
import java.util.Locale;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR1;
|
||||
|
||||
/**
|
||||
* Displays text to the user and optionally allows them to edit it. A TextView
|
||||
* is a complete text editor, however the basic class is configured to not
|
||||
@@ -294,6 +296,10 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
||||
Drawable mDrawableTop, mDrawableBottom, mDrawableLeft, mDrawableRight,
|
||||
mDrawableStart, mDrawableEnd, mDrawableError, mDrawableTemp;
|
||||
|
||||
Drawable mDrawableLeftInitial, mDrawableRightInitial;
|
||||
boolean mIsRtlCompatibilityMode;
|
||||
boolean mOverride;
|
||||
|
||||
int mDrawableSizeTop, mDrawableSizeBottom, mDrawableSizeLeft, mDrawableSizeRight,
|
||||
mDrawableSizeStart, mDrawableSizeEnd, mDrawableSizeError, mDrawableSizeTemp;
|
||||
|
||||
@@ -304,38 +310,64 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
||||
|
||||
int mDrawableSaved = DRAWABLE_NONE;
|
||||
|
||||
public Drawables(Context context) {
|
||||
final int targetSdkVersion = context.getApplicationInfo().targetSdkVersion;
|
||||
mIsRtlCompatibilityMode = (targetSdkVersion < JELLY_BEAN_MR1 ||
|
||||
!context.getApplicationInfo().hasRtlSupport());
|
||||
mOverride = false;
|
||||
}
|
||||
|
||||
public void resolveWithLayoutDirection(int layoutDirection) {
|
||||
switch(layoutDirection) {
|
||||
case LAYOUT_DIRECTION_RTL:
|
||||
if (mDrawableStart != null) {
|
||||
mDrawableRight = mDrawableStart;
|
||||
// First reset "left" and "right" drawables to their initial values
|
||||
mDrawableLeft = mDrawableLeftInitial;
|
||||
mDrawableRight = mDrawableRightInitial;
|
||||
|
||||
mDrawableSizeRight = mDrawableSizeStart;
|
||||
mDrawableHeightRight = mDrawableHeightStart;
|
||||
}
|
||||
if (mDrawableEnd != null) {
|
||||
mDrawableLeft = mDrawableEnd;
|
||||
if (mIsRtlCompatibilityMode) {
|
||||
// Use "start" drawable as "left" drawable if the "left" drawable was not defined
|
||||
if (mDrawableStart != null && mDrawableLeft == null) {
|
||||
mDrawableLeft = mDrawableStart;
|
||||
mDrawableSizeLeft = mDrawableSizeStart;
|
||||
mDrawableHeightLeft = mDrawableHeightStart;
|
||||
}
|
||||
// Use "end" drawable as "right" drawable if the "right" drawable was not defined
|
||||
if (mDrawableEnd != null && mDrawableRight == null) {
|
||||
mDrawableRight = mDrawableEnd;
|
||||
mDrawableSizeRight = mDrawableSizeEnd;
|
||||
mDrawableHeightRight = mDrawableHeightEnd;
|
||||
}
|
||||
} else {
|
||||
// JB-MR1+ normal case: "start" / "end" drawables are overriding "left" / "right"
|
||||
// drawable if and only if they have been defined
|
||||
switch(layoutDirection) {
|
||||
case LAYOUT_DIRECTION_RTL:
|
||||
if (mOverride) {
|
||||
mDrawableRight = mDrawableStart;
|
||||
mDrawableSizeRight = mDrawableSizeStart;
|
||||
mDrawableHeightRight = mDrawableHeightStart;
|
||||
}
|
||||
|
||||
mDrawableSizeLeft = mDrawableSizeEnd;
|
||||
mDrawableHeightLeft = mDrawableHeightEnd;
|
||||
}
|
||||
break;
|
||||
if (mOverride) {
|
||||
mDrawableLeft = mDrawableEnd;
|
||||
mDrawableSizeLeft = mDrawableSizeEnd;
|
||||
mDrawableHeightLeft = mDrawableHeightEnd;
|
||||
}
|
||||
break;
|
||||
|
||||
case LAYOUT_DIRECTION_LTR:
|
||||
default:
|
||||
if (mDrawableStart != null) {
|
||||
mDrawableLeft = mDrawableStart;
|
||||
case LAYOUT_DIRECTION_LTR:
|
||||
default:
|
||||
if (mOverride) {
|
||||
mDrawableLeft = mDrawableStart;
|
||||
mDrawableSizeLeft = mDrawableSizeStart;
|
||||
mDrawableHeightLeft = mDrawableHeightStart;
|
||||
}
|
||||
|
||||
mDrawableSizeLeft = mDrawableSizeStart;
|
||||
mDrawableHeightLeft = mDrawableHeightStart;
|
||||
}
|
||||
if (mDrawableEnd != null) {
|
||||
mDrawableRight = mDrawableEnd;
|
||||
|
||||
mDrawableSizeRight = mDrawableSizeEnd;
|
||||
mDrawableHeightRight = mDrawableHeightEnd;
|
||||
}
|
||||
break;
|
||||
if (mOverride) {
|
||||
mDrawableRight = mDrawableEnd;
|
||||
mDrawableSizeRight = mDrawableSizeEnd;
|
||||
mDrawableHeightRight = mDrawableHeightEnd;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
applyErrorDrawableIfNeeded(layoutDirection);
|
||||
updateDrawablesLayoutDirection(layoutDirection);
|
||||
@@ -1154,6 +1186,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
||||
bufferType = BufferType.SPANNABLE;
|
||||
}
|
||||
|
||||
// This call will save the initial left/right drawables
|
||||
setCompoundDrawablesWithIntrinsicBounds(
|
||||
drawableLeft, drawableTop, drawableRight, drawableBottom);
|
||||
setRelativeDrawablesIfNeeded(drawableStart, drawableEnd);
|
||||
@@ -1302,8 +1335,9 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
||||
if (hasRelativeDrawables) {
|
||||
Drawables dr = mDrawables;
|
||||
if (dr == null) {
|
||||
mDrawables = dr = new Drawables();
|
||||
mDrawables = dr = new Drawables(getContext());
|
||||
}
|
||||
mDrawables.mOverride = true;
|
||||
final Rect compoundRect = dr.mCompoundRect;
|
||||
int[] state = getDrawableState();
|
||||
if (start != null) {
|
||||
@@ -1876,9 +1910,11 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
||||
}
|
||||
} else {
|
||||
if (dr == null) {
|
||||
mDrawables = dr = new Drawables();
|
||||
mDrawables = dr = new Drawables(getContext());
|
||||
}
|
||||
|
||||
mDrawables.mOverride = false;
|
||||
|
||||
if (dr.mDrawableLeft != left && dr.mDrawableLeft != null) {
|
||||
dr.mDrawableLeft.setCallback(null);
|
||||
}
|
||||
@@ -1945,6 +1981,12 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
||||
}
|
||||
}
|
||||
|
||||
// Save initial left/right drawables
|
||||
if (dr != null) {
|
||||
dr.mDrawableLeftInitial = left;
|
||||
dr.mDrawableRightInitial = right;
|
||||
}
|
||||
|
||||
invalidate();
|
||||
requestLayout();
|
||||
}
|
||||
@@ -2045,9 +2087,11 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
||||
}
|
||||
} else {
|
||||
if (dr == null) {
|
||||
mDrawables = dr = new Drawables();
|
||||
mDrawables = dr = new Drawables(getContext());
|
||||
}
|
||||
|
||||
mDrawables.mOverride = true;
|
||||
|
||||
if (dr.mDrawableStart != start && dr.mDrawableStart != null) {
|
||||
dr.mDrawableStart.setCallback(null);
|
||||
}
|
||||
@@ -2114,6 +2158,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
||||
}
|
||||
}
|
||||
|
||||
resetResolvedDrawables();
|
||||
resolveDrawables();
|
||||
invalidate();
|
||||
requestLayout();
|
||||
@@ -2138,7 +2183,6 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
||||
@android.view.RemotableViewMethod
|
||||
public void setCompoundDrawablesRelativeWithIntrinsicBounds(int start, int top, int end,
|
||||
int bottom) {
|
||||
resetResolvedDrawables();
|
||||
final Resources resources = getContext().getResources();
|
||||
setCompoundDrawablesRelativeWithIntrinsicBounds(
|
||||
start != 0 ? resources.getDrawable(start) : null,
|
||||
@@ -2161,7 +2205,6 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
||||
public void setCompoundDrawablesRelativeWithIntrinsicBounds(Drawable start, Drawable top,
|
||||
Drawable end, Drawable bottom) {
|
||||
|
||||
resetResolvedDrawables();
|
||||
if (start != null) {
|
||||
start.setBounds(0, 0, start.getIntrinsicWidth(), start.getIntrinsicHeight());
|
||||
}
|
||||
@@ -2230,7 +2273,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
|
||||
}
|
||||
} else {
|
||||
if (dr == null) {
|
||||
mDrawables = dr = new Drawables();
|
||||
mDrawables = dr = new Drawables(getContext());
|
||||
}
|
||||
dr.mDrawablePadding = pad;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user