Merge "Add View.getResolvedLayoutDirection()"

This commit is contained in:
Fabrice Di Meglio
2011-06-14 16:23:06 -07:00
committed by Android (Google) Code Review
18 changed files with 124 additions and 65 deletions

View File

@@ -8512,11 +8512,11 @@ package android.graphics.drawable {
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;
method public void invalidateSelf();
method public boolean isLayoutRtlSelf();
method public boolean isStateful();
method public final boolean isVisible();
method public void jumpToCurrentState();
@@ -8548,7 +8548,7 @@ package android.graphics.drawable {
}
public static abstract interface Drawable.Callback2 implements android.graphics.drawable.Drawable.Callback {
method public abstract boolean isLayoutRtl(android.graphics.drawable.Drawable);
method public abstract int getResolvedLayoutDirection(android.graphics.drawable.Drawable);
}
public static abstract class Drawable.ConstantState {
@@ -20583,7 +20583,7 @@ package android.view {
method public static void apply(int, int, int, android.graphics.Rect, android.graphics.Rect);
method public static void apply(int, int, int, android.graphics.Rect, int, int, android.graphics.Rect);
method public static void applyDisplay(int, android.graphics.Rect, android.graphics.Rect);
method public static int getAbsoluteGravity(int, boolean);
method public static int getAbsoluteGravity(int, int);
method public static boolean isHorizontal(int);
method public static boolean isVertical(int);
field public static final int AXIS_CLIP = 8; // 0x8
@@ -21724,6 +21724,7 @@ package android.view {
method public final android.view.ViewParent getParent();
method public float getPivotX();
method public float getPivotY();
method public int getResolvedLayoutDirection(android.graphics.drawable.Drawable);
method public android.content.res.Resources getResources();
method public final int getRight();
method protected float getRightFadingEdgeStrength();
@@ -21789,8 +21790,6 @@ package android.view {
method public boolean isInEditMode();
method public boolean isInTouchMode();
method public boolean isLayoutRequested();
method public boolean isLayoutRtl();
method public boolean isLayoutRtl(android.graphics.drawable.Drawable);
method public boolean isLongClickable();
method public boolean isOpaque();
method protected boolean isPaddingOffsetRequired();

View File

@@ -151,13 +151,13 @@ public class Gravity
* width and height of the object.
* @param outRect Receives the computed frame of the object in its
* container.
* @param isRtl Whether the layout is right-to-left.
* @param layoutDirection The layout direction.
*
* @hide
*/
public static void apply(int gravity, int w, int h, Rect container,
Rect outRect, boolean isRtl) {
int absGravity = getAbsoluteGravity(gravity, isRtl);
Rect outRect, int layoutDirection) {
int absGravity = getAbsoluteGravity(gravity, layoutDirection);
apply(absGravity, w, h, container, 0, 0, outRect);
}
@@ -347,18 +347,19 @@ public class Gravity
* if horizontal direction is LTR, then START will set LEFT and END will set RIGHT.
* if horizontal direction is RTL, then START will set RIGHT and END will set LEFT.
*
*
* @param gravity The gravity to convert to absolute (horizontal) values.
* @param isRtl Whether the layout is right-to-left.
* @param layoutDirection The layout direction.
* @return gravity converted to absolute (horizontal) values.
*/
public static int getAbsoluteGravity(int gravity, boolean isRtl) {
public static int getAbsoluteGravity(int gravity, int layoutDirection) {
int result = gravity;
// If layout is script specific and gravity is horizontal relative (START or END)
if ((result & RELATIVE_LAYOUT_DIRECTION) > 0) {
if ((result & Gravity.START) == Gravity.START) {
// Remove the START bit
result &= ~START;
if (isRtl) {
if (layoutDirection == View.LAYOUT_DIRECTION_RTL) {
// Set the RIGHT bit
result |= RIGHT;
} else {
@@ -368,7 +369,7 @@ public class Gravity
} else if ((result & Gravity.END) == Gravity.END) {
// Remove the END bit
result &= ~END;
if (isRtl) {
if (layoutDirection == View.LAYOUT_DIRECTION_RTL) {
// Set the LEFT bit
result |= LEFT;
} else {

View File

@@ -4272,6 +4272,7 @@ public class View implements Drawable.Callback2, KeyEvent.Callback, Accessibilit
* {@link #LAYOUT_DIRECTION_INHERIT} or
* {@link #LAYOUT_DIRECTION_LOCALE}.
* @attr ref android.R.styleable#View_layoutDirection
*
* @hide
*/
@ViewDebug.ExportedProperty(category = "layout", mapping = {
@@ -4292,6 +4293,7 @@ public class View implements Drawable.Callback2, KeyEvent.Callback, Accessibilit
* {@link #LAYOUT_DIRECTION_INHERIT} or
* {@link #LAYOUT_DIRECTION_LOCALE}.
* @attr ref android.R.styleable#View_layoutDirection
*
* @hide
*/
@RemotableViewMethod
@@ -4299,6 +4301,37 @@ public class View implements Drawable.Callback2, KeyEvent.Callback, Accessibilit
setFlags(layoutDirection, LAYOUT_DIRECTION_MASK);
}
/**
* Returns the resolved layout direction for this view.
*
* @return {@link #LAYOUT_DIRECTION_RTL} if the layout direction is RTL or returns
* {@link #LAYOUT_DIRECTION_LTR} id the layout direction is not RTL.
*
* @hide
*/
@ViewDebug.ExportedProperty(category = "layout", mapping = {
@ViewDebug.IntToString(from = LAYOUT_DIRECTION_LTR, to = "RESOLVED_DIRECTION_LTR"),
@ViewDebug.IntToString(from = LAYOUT_DIRECTION_RTL, to = "RESOLVED_DIRECTION_RTL")
})
public int getResolvedLayoutDirection() {
resolveLayoutDirection();
return ((mPrivateFlags2 & RESOLVED_LAYOUT_RTL) == RESOLVED_LAYOUT_RTL) ?
LAYOUT_DIRECTION_RTL : LAYOUT_DIRECTION_LTR;
}
/**
* <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
* layout attribute and/or the inherited value from the parent.</p>
*
* @return true if the layout is right-to-left.
*
* @hide
*/
@ViewDebug.ExportedProperty(category = "layout")
public boolean isLayoutRtl() {
return (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL);
}
/**
* If this view doesn't do any drawing on its own, set this flag to
* allow further optimizations. By default, this flag is not set on
@@ -8713,8 +8746,9 @@ public class View implements Drawable.Callback2, KeyEvent.Callback, Accessibilit
switch (getLayoutDirection()) {
case LAYOUT_DIRECTION_INHERIT:
// If this is root view, no need to look at parent's layout dir.
if (mParent != null && mParent instanceof ViewGroup &&
((ViewGroup) mParent).isLayoutRtl()) {
if (mParent != null &&
mParent instanceof ViewGroup &&
((ViewGroup) mParent).getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) {
mPrivateFlags2 |= RESOLVED_LAYOUT_RTL;
}
break;
@@ -10236,17 +10270,6 @@ public class View implements Drawable.Callback2, KeyEvent.Callback, Accessibilit
return (mPrivateFlags & FORCE_LAYOUT) == FORCE_LAYOUT;
}
/**
* <p>Indicates whether or not this view's layout is right-to-left. This is resolved from
* layout attribute and/or the inherited value from the parent.</p>
*
* @return true if the layout is right-to-left.
*/
@ViewDebug.ExportedProperty(category = "layout")
public boolean isLayoutRtl() {
return (mPrivateFlags2 & RESOLVED_LAYOUT_RTL) == RESOLVED_LAYOUT_RTL;
}
/**
* Assign a size and position to a view and all of its
* descendants
@@ -10459,13 +10482,15 @@ public class View implements Drawable.Callback2, KeyEvent.Callback, Accessibilit
}
}
/**
* Check if a given Drawable is in RTL layout direction.
*
* @param who the recipient of the action
*/
public boolean isLayoutRtl(Drawable who) {
return (who == mBGDrawable) && isLayoutRtl();
/**
* Return the layout direction of a given Drawable.
*
* @param who the Drawable to query
*
* @hide
*/
public int getResolvedLayoutDirection(Drawable who) {
return (who == mBGDrawable) ? getResolvedLayoutDirection() : LAYOUT_DIRECTION_DEFAULT;
}
/**

View File

@@ -364,7 +364,8 @@ public class FrameLayout extends ViewGroup {
gravity = DEFAULT_CHILD_GRAVITY;
}
final int absoluteGravity = Gravity.getAbsoluteGravity(gravity, isLayoutRtl());
final int layoutDirection = getResolvedLayoutDirection();
final int absoluteGravity = Gravity.getAbsoluteGravity(gravity, layoutDirection);
final int verticalGravity = gravity & Gravity.VERTICAL_GRAVITY_MASK;
switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
@@ -435,8 +436,10 @@ public class FrameLayout extends ViewGroup {
selfBounds.set(mPaddingLeft, mPaddingTop, w - mPaddingRight, h - mPaddingBottom);
}
final int layoutDirection = getResolvedLayoutDirection();
Gravity.apply(mForegroundGravity, foreground.getIntrinsicWidth(),
foreground.getIntrinsicHeight(), selfBounds, overlayBounds, isLayoutRtl());
foreground.getIntrinsicHeight(), selfBounds, overlayBounds,
layoutDirection);
foreground.setBounds(overlayBounds);
}

View File

@@ -1408,7 +1408,8 @@ public class GridView extends AbsListView {
int childLeft;
final int childTop = flow ? y : y - h;
final int absoluteGravity = Gravity.getAbsoluteGravity(mGravity,isLayoutRtl());
final int layoutDirection = getResolvedLayoutDirection();
final int absoluteGravity = Gravity.getAbsoluteGravity(mGravity, layoutDirection);
switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
case Gravity.LEFT:
childLeft = childrenLeft;

View File

@@ -186,9 +186,13 @@ public class ImageView extends View {
}
}
/**
* @hide
*/
@Override
public boolean isLayoutRtl(Drawable dr) {
return (dr == mDrawable) ? isLayoutRtl() : super.isLayoutRtl(dr);
public int getResolvedLayoutDirection(Drawable dr) {
return (dr == mDrawable) ?
getResolvedLayoutDirection() : super.getResolvedLayoutDirection(dr);
}
@Override

View File

@@ -1446,7 +1446,8 @@ public class LinearLayout extends ViewGroup {
if (gravity < 0) {
gravity = minorGravity;
}
final int absoluteGravity = Gravity.getAbsoluteGravity(gravity, isLayoutRtl());
final int layoutDirection = getResolvedLayoutDirection();
final int absoluteGravity = Gravity.getAbsoluteGravity(gravity, layoutDirection);
switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
case Gravity.CENTER_HORIZONTAL:
childLeft = paddingLeft + ((childSpace - childWidth) / 2)
@@ -1509,7 +1510,8 @@ public class LinearLayout extends ViewGroup {
final int[] maxAscent = mMaxAscent;
final int[] maxDescent = mMaxDescent;
switch (Gravity.getAbsoluteGravity(majorGravity, isLayoutRtl())) {
final int layoutDirection = getResolvedLayoutDirection();
switch (Gravity.getAbsoluteGravity(majorGravity, layoutDirection)) {
case Gravity.RIGHT:
// mTotalLength contains the padding already
childLeft = mPaddingLeft + mRight - mLeft - mTotalLength;

View File

@@ -915,10 +915,13 @@ public class ProgressBar extends View {
}
}
/**
* @hide
*/
@Override
public boolean isLayoutRtl(Drawable who) {
public int getResolvedLayoutDirection(Drawable who) {
return (who == mProgressDrawable || who == mIndeterminateDrawable) ?
isLayoutRtl() : super.isLayoutRtl(who);
getResolvedLayoutDirection() : super.getResolvedLayoutDirection(who);
}
@Override

View File

@@ -495,8 +495,9 @@ public class RelativeLayout extends ViewGroup {
height - mPaddingBottom);
final Rect contentBounds = mContentBounds;
final int layoutDirection = getResolvedLayoutDirection();
Gravity.apply(mGravity, right - left, bottom - top, selfBounds, contentBounds,
isLayoutRtl());
layoutDirection);
final int horizontalOffset = contentBounds.left - left;
final int verticalOffset = contentBounds.top - top;

View File

@@ -224,7 +224,8 @@ public class TableRow extends LinearLayout {
final int childWidth = child.getMeasuredWidth();
lp.mOffset[LayoutParams.LOCATION_NEXT] = columnWidth - childWidth;
final int absoluteGravity = Gravity.getAbsoluteGravity(gravity, isLayoutRtl());
final int layoutDirection = getResolvedLayoutDirection();
final int absoluteGravity = Gravity.getAbsoluteGravity(gravity, layoutDirection);
switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
case Gravity.LEFT:
// don't offset on X axis

View File

@@ -4145,17 +4145,20 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
}
}
/**
* @hide
*/
@Override
public boolean isLayoutRtl(Drawable who) {
if (who == null) return false;
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) {
return isLayoutRtl();
return getResolvedLayoutDirection();
}
}
return super.isLayoutRtl(who);
return super.getResolvedLayoutDirection(who);
}
@Override
@@ -4397,7 +4400,8 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
canvas.translate(compoundPaddingLeft, extendedPaddingTop + voffsetText);
}
final int absoluteGravity = Gravity.getAbsoluteGravity(mGravity, isLayoutRtl());
final int layoutDirection = getResolvedLayoutDirection();
final int absoluteGravity = Gravity.getAbsoluteGravity(mGravity, layoutDirection);
if (mEllipsize == TextUtils.TruncateAt.MARQUEE) {
if (!mSingleLine && getLineCount() == 1 && canMarquee() &&
(absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) != Gravity.LEFT) {
@@ -5545,8 +5549,10 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
hintWidth = 0;
}
final int layoutDirection = getResolvedLayoutDirection();
final int absoluteGravity = Gravity.getAbsoluteGravity(mGravity, layoutDirection);
Layout.Alignment alignment;
final int absoluteGravity = Gravity.getAbsoluteGravity(mGravity, isLayoutRtl());
switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
case Gravity.CENTER_HORIZONTAL:
alignment = Layout.Alignment.ALIGN_CENTER;
@@ -7582,7 +7588,8 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
return 0.0f;
}
} else if (getLineCount() == 1) {
final int absoluteGravity = Gravity.getAbsoluteGravity(mGravity, isLayoutRtl());
final int layoutDirection = getResolvedLayoutDirection();
final int absoluteGravity = Gravity.getAbsoluteGravity(mGravity, layoutDirection);
switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
case Gravity.LEFT:
return 0.0f;
@@ -7606,7 +7613,8 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
final Marquee marquee = mMarquee;
return (marquee.mMaxFadeScroll - marquee.mScroll) / getHorizontalFadingEdgeLength();
} else if (getLineCount() == 1) {
final int absoluteGravity = Gravity.getAbsoluteGravity(mGravity, isLayoutRtl());
final int layoutDirection = getResolvedLayoutDirection();
final int absoluteGravity = Gravity.getAbsoluteGravity(mGravity, layoutDirection);
switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
case Gravity.LEFT:
final int textWidth = (mRight - mLeft) - getCompoundPaddingLeft() -

View File

@@ -281,8 +281,10 @@ public final class IconMenuItemView extends TextView implements MenuView.ItemVie
Rect tmpRect = mPositionIconOutput;
getLineBounds(0, tmpRect);
mPositionIconAvailable.set(0, 0, getWidth(), tmpRect.top);
final int layoutDirection = getResolvedLayoutDirection();
Gravity.apply(Gravity.CENTER_VERTICAL | Gravity.LEFT, mIcon.getIntrinsicWidth(), mIcon
.getIntrinsicHeight(), mPositionIconAvailable, mPositionIconOutput, isLayoutRtl());
.getIntrinsicHeight(), mPositionIconAvailable, mPositionIconOutput,
layoutDirection);
mIcon.setBounds(mPositionIconOutput);
}

View File

@@ -67,6 +67,8 @@ public class GravityTest extends AndroidTestCase {
}
private void assertOneGravity(int expected, int initial, boolean isRtl) {
assertEquals(expected, Gravity.getAbsoluteGravity(initial, isRtl));
final int layoutDirection = isRtl ? View.LAYOUT_DIRECTION_RTL : View.LAYOUT_DIRECTION_LTR;
assertEquals(expected, Gravity.getAbsoluteGravity(initial, layoutDirection));
}
}

View File

@@ -30,6 +30,7 @@ import android.graphics.BitmapShader;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
@@ -386,8 +387,9 @@ public class BitmapDrawable extends Drawable {
Shader shader = state.mPaint.getShader();
if (shader == null) {
if (mApplyGravity) {
final int layoutDirection = getResolvedLayoutDirectionSelf();
Gravity.apply(state.mGravity, mBitmapWidth, mBitmapHeight,
getBounds(), mDstRect, isLayoutRtlSelf());
getBounds(), mDstRect, layoutDirection);
mApplyGravity = false;
}
canvas.drawBitmap(bitmap, null, mDstRect, state.mPaint);

View File

@@ -24,6 +24,7 @@ import android.content.res.TypedArray;
import android.graphics.*;
import android.view.Gravity;
import android.util.AttributeSet;
import android.view.View;
import java.io.IOException;
@@ -209,7 +210,8 @@ public class ClipDrawable extends Drawable implements Drawable.Callback {
if ((mClipState.mOrientation & VERTICAL) != 0) {
h -= (h - ih) * (10000 - level) / 10000;
}
Gravity.apply(mClipState.mGravity, w, h, bounds, r, isLayoutRtlSelf());
final int layoutDirection = getResolvedLayoutDirectionSelf();
Gravity.apply(mClipState.mGravity, w, h, bounds, r, layoutDirection);
if (w > 0 && h > 0) {
canvas.save();

View File

@@ -36,6 +36,7 @@ import android.util.DisplayMetrics;
import android.util.StateSet;
import android.util.TypedValue;
import android.util.Xml;
import android.view.View;
import java.io.IOException;
import java.io.InputStream;
@@ -292,11 +293,11 @@ public abstract class Drawable {
*/
public static interface Callback2 extends Callback {
/**
* A Drawable can call this to know whether the <var>who</var> is in RTL layout direction.
* A Drawable can call this to get the resolved layout direction of the <var>who</var>.
*
* @param who The drawable being unscheduled.
* @param who The drawable being queried.
*/
public boolean isLayoutRtl(Drawable who);
public int getResolvedLayoutDirection(Drawable who);
}
/**
@@ -376,15 +377,15 @@ public abstract class Drawable {
}
/**
* Use the current {@link android.graphics.drawable.Drawable.Callback2} implementation to know
* if this Drawable is having a layout in RTL direction.
* Use the current {@link android.graphics.drawable.Drawable.Callback2} implementation to get
* the resolved layout direction of this Drawable.
*/
public boolean isLayoutRtlSelf() {
public int getResolvedLayoutDirectionSelf() {
final Callback callback = getCallback();
if (callback == null || !(callback instanceof Callback2)) {
return false;
return View.LAYOUT_DIRECTION_LTR;
}
return ((Callback2) callback).isLayoutRtl(this);
return ((Callback2) callback).getResolvedLayoutDirection(this);
}
/**

View File

@@ -24,6 +24,7 @@ import android.content.res.TypedArray;
import android.graphics.*;
import android.view.Gravity;
import android.util.AttributeSet;
import android.view.View;
import java.io.IOException;
@@ -221,7 +222,8 @@ 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);
}
Gravity.apply(mScaleState.mGravity, w, h, bounds, r, isLayoutRtlSelf());
final int layoutDirection = getResolvedLayoutDirectionSelf();
Gravity.apply(mScaleState.mGravity, w, h, bounds, r, layoutDirection);
if (w > 0 && h > 0) {
mScaleState.mDrawable.setBounds(r.left, r.top, r.right, r.bottom);

View File

@@ -21,7 +21,7 @@
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutDirection="ltr"
android:layoutDirection="rtl"
android:background="#FF000000">
<FrameLayout