From c96132ff53e5c26f5b0170edd85072006fb2bc70 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Queru Date: Sat, 2 Jun 2012 05:46:20 -0700 Subject: [PATCH] Revert "Clean up layout direction APIs for Drawable" This reverts commit c1da65187a4b9de8f72bd617ef937030187c0a92. --- api/current.txt | 13 +++--- core/java/android/view/View.java | 43 ++++--------------- core/java/android/widget/ImageView.java | 7 ++- core/java/android/widget/ProgressBar.java | 10 +++-- core/java/android/widget/TextView.java | 41 ++++++++++-------- .../graphics/drawable/BitmapDrawable.java | 2 +- .../graphics/drawable/ClipDrawable.java | 2 +- .../android/graphics/drawable/Drawable.java | 40 ++++++++--------- .../graphics/drawable/ScaleDrawable.java | 2 +- 9 files changed, 72 insertions(+), 88 deletions(-) diff --git a/api/current.txt b/api/current.txt index 4a292ad23d0d0..a7c89fad3fe7b 100644 --- a/api/current.txt +++ b/api/current.txt @@ -9251,12 +9251,12 @@ package android.graphics.drawable { method public android.graphics.drawable.Drawable getCurrent(); method public int getIntrinsicHeight(); method public int getIntrinsicWidth(); - method public int getLayoutDirection(); method public final int getLevel(); method public int getMinimumHeight(); method public int getMinimumWidth(); method public abstract int getOpacity(); method public boolean getPadding(android.graphics.Rect); + method public int getResolvedLayoutDirectionSelf(); method public int[] getState(); method public android.graphics.Region getTransparentRegion(); method public void inflate(android.content.res.Resources, org.xmlpull.v1.XmlPullParser, android.util.AttributeSet) throws java.io.IOException, org.xmlpull.v1.XmlPullParserException; @@ -9279,7 +9279,6 @@ package android.graphics.drawable { method public void setColorFilter(int, android.graphics.PorterDuff.Mode); method public void setDither(boolean); method public void setFilterBitmap(boolean); - method public void setLayoutDirection(int); method public final boolean setLevel(int); method public boolean setState(int[]); method public boolean setVisible(boolean, boolean); @@ -9292,6 +9291,10 @@ package android.graphics.drawable { method public abstract void unscheduleDrawable(android.graphics.drawable.Drawable, java.lang.Runnable); } + public static abstract interface Drawable.Callback2 implements android.graphics.drawable.Drawable.Callback { + method public abstract int getResolvedLayoutDirection(android.graphics.drawable.Drawable); + } + public static abstract class Drawable.ConstantState { ctor public Drawable.ConstantState(); method public abstract int getChangingConfigurations(); @@ -23902,7 +23905,7 @@ package android.view { method public void recycle(); } - public class View implements android.view.accessibility.AccessibilityEventSource android.graphics.drawable.Drawable.Callback android.view.KeyEvent.Callback { + public class View implements android.view.accessibility.AccessibilityEventSource android.graphics.drawable.Drawable.Callback android.graphics.drawable.Drawable.Callback2 android.view.KeyEvent.Callback { ctor public View(android.content.Context); ctor public View(android.content.Context, android.util.AttributeSet); ctor public View(android.content.Context, android.util.AttributeSet, int); @@ -24047,6 +24050,7 @@ package android.view { method public float getPivotX(); method public float getPivotY(); method public int getResolvedLayoutDirection(); + method public int getResolvedLayoutDirection(android.graphics.drawable.Drawable); method public int getResolvedTextAlignment(); method public int getResolvedTextDirection(); method public android.content.res.Resources getResources(); @@ -24178,7 +24182,6 @@ package android.view { method protected void onOverScrolled(int, int, boolean, boolean); method public void onPaddingChanged(int); method public void onPopulateAccessibilityEvent(android.view.accessibility.AccessibilityEvent); - method public void onResolveDrawables(int); method public void onResolvedLayoutDirectionChanged(); method public void onResolvedLayoutDirectionReset(); method public void onResolvedTextAlignmentChanged(); @@ -24230,7 +24233,6 @@ package android.view { method public void resetResolvedLayoutDirection(); method public void resetResolvedTextAlignment(); method public void resetResolvedTextDirection(); - method public void resolveDrawables(); method public void resolveLayoutDirection(); method public void resolvePadding(); method public static int resolveSize(int, int); @@ -28723,6 +28725,7 @@ package android.widget { method public boolean onTextContextMenuItem(int); method public void removeTextChangedListener(android.text.TextWatcher); method protected void resetResolvedDrawables(); + method protected void resolveDrawables(); method public void setAllCaps(boolean); method public final void setAutoLinkMask(int); method public void setCompoundDrawablePadding(int); diff --git a/core/java/android/view/View.java b/core/java/android/view/View.java index f74f9690953be..61b2352371ffb 100644 --- a/core/java/android/view/View.java +++ b/core/java/android/view/View.java @@ -660,7 +660,7 @@ import java.util.concurrent.CopyOnWriteArrayList; * * @see android.view.ViewGroup */ -public class View implements Drawable.Callback, KeyEvent.Callback, +public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Callback, AccessibilityEventSource { private static final boolean DBG = false; @@ -3838,15 +3838,6 @@ public class View implements Drawable.Callback, KeyEvent.Callback, scrollabilityCache.scrollBar.setAlwaysDrawVerticalTrack(true); } - // Apply layout direction to the new Drawables if needed - final int layoutDirection = getResolvedLayoutDirection(); - if (track != null) { - track.setLayoutDirection(layoutDirection); - } - if (thumb != null) { - thumb.setLayoutDirection(layoutDirection); - } - // Re-apply user/background padding so that scrollbar(s) get added resolvePadding(); } @@ -13875,29 +13866,12 @@ public class View implements Drawable.Callback, KeyEvent.Callback, } /** - * Resolve the Drawables depending on the layout direction. This is implicitly supposing - * that the View directionality can and will be resolved before its Drawables. - * - * Will call {@link View#onResolveDrawables} when resolution is done. - */ - public void resolveDrawables() { - if (mBackground != null) { - mBackground.setLayoutDirection(getResolvedLayoutDirection()); - } - onResolveDrawables(getResolvedLayoutDirection()); - } - - /** - * Called when layout direction has been resolved. - * - * The default implementation does nothing. - * - * @param layoutDirection The resolved layout direction. - * - * @see {@link #LAYOUT_DIRECTION_LTR} - * @see {@link #LAYOUT_DIRECTION_RTL} - */ - public void onResolveDrawables(int layoutDirection) { + * Return the layout direction of a given Drawable. + * + * @param who the Drawable to query + */ + public int getResolvedLayoutDirection(Drawable who) { + return (who == mBackground) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT; } /** @@ -14166,9 +14140,8 @@ public class View implements Drawable.Callback, KeyEvent.Callback, padding = new Rect(); sThreadLocal.set(padding); } - background.setLayoutDirection(getResolvedLayoutDirection()); if (background.getPadding(padding)) { - switch (background.getLayoutDirection()) { + switch (background.getResolvedLayoutDirectionSelf()) { case LAYOUT_DIRECTION_RTL: setPadding(padding.right, padding.top, padding.left, padding.bottom); break; diff --git a/core/java/android/widget/ImageView.java b/core/java/android/widget/ImageView.java index f259597b8b41f..7593bffc132a0 100644 --- a/core/java/android/widget/ImageView.java +++ b/core/java/android/widget/ImageView.java @@ -193,6 +193,12 @@ public class ImageView extends View { } } + @Override + public int getResolvedLayoutDirection(Drawable dr) { + return (dr == mDrawable) ? + getResolvedLayoutDirection() : super.getResolvedLayoutDirection(dr); + } + @Override public boolean hasOverlappingRendering() { return (getBackground() != null); @@ -666,7 +672,6 @@ public class ImageView extends View { d.setState(getDrawableState()); } d.setLevel(mLevel); - d.setLayoutDirection(getLayoutDirection()); mDrawableWidth = d.getIntrinsicWidth(); mDrawableHeight = d.getIntrinsicHeight(); applyColorMod(); diff --git a/core/java/android/widget/ProgressBar.java b/core/java/android/widget/ProgressBar.java index 1c6a4062ead03..e26620f55a5d5 100644 --- a/core/java/android/widget/ProgressBar.java +++ b/core/java/android/widget/ProgressBar.java @@ -478,9 +478,6 @@ public class ProgressBar extends View { d.setCallback(this); } mIndeterminateDrawable = d; - if (mIndeterminateDrawable != null) { - mIndeterminateDrawable.setLayoutDirection(getLayoutDirection()); - } if (mIndeterminate) { mCurrentDrawable = d; postInvalidate(); @@ -520,7 +517,6 @@ public class ProgressBar extends View { if (d != null) { d.setCallback(this); - d.setLayoutDirection(getLayoutDirection()); // Make sure the ProgressBar is always tall enough int drawableHeight = d.getMinimumHeight(); @@ -979,6 +975,12 @@ public class ProgressBar extends View { } } + @Override + public int getResolvedLayoutDirection(Drawable who) { + return (who == mProgressDrawable || who == mIndeterminateDrawable) ? + getResolvedLayoutDirection() : super.getResolvedLayoutDirection(who); + } + @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { updateDrawableBounds(w, h); diff --git a/core/java/android/widget/TextView.java b/core/java/android/widget/TextView.java index 91dd29711cf7f..d70bfb1915067 100644 --- a/core/java/android/widget/TextView.java +++ b/core/java/android/widget/TextView.java @@ -4578,6 +4578,20 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener } } + @Override + public int getResolvedLayoutDirection(Drawable who) { + if (who == null) return View.LAYOUT_DIRECTION_LTR; + if (mDrawables != null) { + final Drawables drawables = mDrawables; + if (who == drawables.mDrawableLeft || who == drawables.mDrawableRight || + who == drawables.mDrawableTop || who == drawables.mDrawableBottom || + who == drawables.mDrawableStart || who == drawables.mDrawableEnd) { + return getResolvedLayoutDirection(); + } + } + return super.getResolvedLayoutDirection(who); + } + @Override public boolean hasOverlappingRendering() { return (getBackground() != null || mText instanceof Spannable || hasSelection()); @@ -8189,8 +8203,13 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener } } - @Override - public void onResolveDrawables(int layoutDirection) { + /** + * Subclasses will need to override this method to implement their own way of resolving + * drawables depending on the layout direction. + * + * A call to the super method will be required from the subclasses implementation. + */ + protected void resolveDrawables() { // No need to resolve twice if (mResolvedDrawables) { return; @@ -8206,7 +8225,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener } Drawables dr = mDrawables; - switch(layoutDirection) { + switch(getResolvedLayoutDirection()) { case LAYOUT_DIRECTION_RTL: if (dr.mDrawableStart != null) { dr.mDrawableRight = dr.mDrawableStart; @@ -8238,25 +8257,9 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener } break; } - updateDrawablesLayoutDirection(dr, layoutDirection); mResolvedDrawables = true; } - private void updateDrawablesLayoutDirection(Drawables dr, int layoutDirection) { - if (dr.mDrawableLeft != null) { - dr.mDrawableLeft.setLayoutDirection(layoutDirection); - } - if (dr.mDrawableRight != null) { - dr.mDrawableRight.setLayoutDirection(layoutDirection); - } - if (dr.mDrawableTop != null) { - dr.mDrawableTop.setLayoutDirection(layoutDirection); - } - if (dr.mDrawableBottom != null) { - dr.mDrawableBottom.setLayoutDirection(layoutDirection); - } - } - protected void resetResolvedDrawables() { mResolvedDrawables = false; } diff --git a/graphics/java/android/graphics/drawable/BitmapDrawable.java b/graphics/java/android/graphics/drawable/BitmapDrawable.java index e82ccd4988b1b..87421b187eab4 100644 --- a/graphics/java/android/graphics/drawable/BitmapDrawable.java +++ b/graphics/java/android/graphics/drawable/BitmapDrawable.java @@ -385,7 +385,7 @@ public class BitmapDrawable extends Drawable { Shader shader = state.mPaint.getShader(); if (shader == null) { if (mApplyGravity) { - final int layoutDirection = getLayoutDirection(); + final int layoutDirection = getResolvedLayoutDirectionSelf(); Gravity.apply(state.mGravity, mBitmapWidth, mBitmapHeight, getBounds(), mDstRect, layoutDirection); mApplyGravity = false; diff --git a/graphics/java/android/graphics/drawable/ClipDrawable.java b/graphics/java/android/graphics/drawable/ClipDrawable.java index bade9b4f54b24..c41dd07c2e6f1 100644 --- a/graphics/java/android/graphics/drawable/ClipDrawable.java +++ b/graphics/java/android/graphics/drawable/ClipDrawable.java @@ -209,7 +209,7 @@ public class ClipDrawable extends Drawable implements Drawable.Callback { if ((mClipState.mOrientation & VERTICAL) != 0) { h -= (h - ih) * (10000 - level) / 10000; } - final int layoutDirection = getLayoutDirection(); + final int layoutDirection = getResolvedLayoutDirectionSelf(); Gravity.apply(mClipState.mGravity, w, h, bounds, r, layoutDirection); if (w > 0 && h > 0) { diff --git a/graphics/java/android/graphics/drawable/Drawable.java b/graphics/java/android/graphics/drawable/Drawable.java index 07bcbdcdeaac0..6193ca72baf60 100644 --- a/graphics/java/android/graphics/drawable/Drawable.java +++ b/graphics/java/android/graphics/drawable/Drawable.java @@ -124,8 +124,6 @@ public abstract class Drawable { private WeakReference mCallback = null; private boolean mVisible = true; - private int mLayoutDirection; - /** * Draw in its bounds (set via setBounds) respecting optional effects such * as alpha (set via setAlpha) and color filter (set via setColorFilter). @@ -297,6 +295,18 @@ public abstract class Drawable { public void unscheduleDrawable(Drawable who, Runnable what); } + /** + * Implement this interface if you want to create an drawable that is RTL aware + */ + public static interface Callback2 extends Callback { + /** + * A Drawable can call this to get the resolved layout direction of the who. + * + * @param who The drawable being queried. + */ + public int getResolvedLayoutDirection(Drawable who); + } + /** * Bind a {@link Callback} object to this Drawable. Required for clients * that want to support animated drawables. @@ -374,27 +384,15 @@ public abstract class Drawable { } /** - * Returns the resolved layout direction for this Drawable. - * - * @return One of {@link View#LAYOUT_DIRECTION_LTR}, - * {@link View#LAYOUT_DIRECTION_RTL} + * Use the current {@link android.graphics.drawable.Drawable.Callback2} implementation to get + * the resolved layout direction of this Drawable. */ - public int getLayoutDirection() { - return mLayoutDirection; - } - - /** - * Set the layout direction for this drawable. Should be a resolved direction as the - * Drawable as no capacity to do the resolution on his own. - * - * @param layoutDirection One of {@link View#LAYOUT_DIRECTION_LTR}, - * {@link View#LAYOUT_DIRECTION_RTL}, - * - */ - public void setLayoutDirection(int layoutDirection) { - if (getLayoutDirection() != layoutDirection) { - mLayoutDirection = layoutDirection; + public int getResolvedLayoutDirectionSelf() { + final Callback callback = getCallback(); + if (callback == null || !(callback instanceof Callback2)) { + return View.LAYOUT_DIRECTION_LTR; } + return ((Callback2) callback).getResolvedLayoutDirection(this); } /** diff --git a/graphics/java/android/graphics/drawable/ScaleDrawable.java b/graphics/java/android/graphics/drawable/ScaleDrawable.java index bd2b2f0624a4b..ccad2503f6f98 100644 --- a/graphics/java/android/graphics/drawable/ScaleDrawable.java +++ b/graphics/java/android/graphics/drawable/ScaleDrawable.java @@ -221,7 +221,7 @@ public class ScaleDrawable extends Drawable implements Drawable.Callback { final int ih = min ? mScaleState.mDrawable.getIntrinsicHeight() : 0; h -= (int) ((h - ih) * (10000 - level) * mScaleState.mScaleHeight / 10000); } - final int layoutDirection = getLayoutDirection(); + final int layoutDirection = getResolvedLayoutDirectionSelf(); Gravity.apply(mScaleState.mGravity, w, h, bounds, r, layoutDirection); if (w > 0 && h > 0) {