Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fe8b06cf authored by Fabrice Di Meglio's avatar Fabrice Di Meglio Committed by Android (Google) Code Review
Browse files

Merge "Support control of text alignment."

parents 169abcf6 c0ccf0c4
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -22395,6 +22395,7 @@ package android.view {
    method public void requestDisallowInterceptTouchEvent(boolean);
    method public boolean requestSendAccessibilityEvent(android.view.View, android.view.accessibility.AccessibilityEvent);
    method public void requestTransparentRegion(android.view.View);
    method protected void resetLayoutDirectionResolution();
    method public void scheduleLayoutAnimation();
    method public void setAddStatesFromChildren(boolean);
    method public void setAlwaysDrawnWithCacheEnabled(boolean);
@@ -26063,6 +26064,7 @@ package android.widget {
    method protected void onTextChanged(java.lang.CharSequence, int, int, int);
    method public boolean onTextContextMenuItem(int);
    method public void removeTextChangedListener(android.text.TextWatcher);
    method protected void resetLayoutDirectionResolution();
    method public final void setAutoLinkMask(int);
    method public void setCompoundDrawablePadding(int);
    method public void setCompoundDrawables(android.graphics.drawable.Drawable, android.graphics.drawable.Drawable, android.graphics.drawable.Drawable, android.graphics.drawable.Drawable);
+34 −10
Original line number Diff line number Diff line
@@ -266,7 +266,7 @@ public abstract class Layout {
            }
        }

        Alignment align = mAlignment;
        Alignment paraAlign = mAlignment;
        TabStops tabStops = null;
        boolean tabStopsIsInitialized = false;

@@ -310,10 +310,10 @@ public abstract class Layout {
                                                    ParagraphStyle.class);
                    spans = getParagraphSpans(sp, start, spanEnd, ParagraphStyle.class);

                    align = mAlignment;
                    paraAlign = mAlignment;
                    for (int n = spans.length-1; n >= 0; n--) {
                        if (spans[n] instanceof AlignmentSpan) {
                            align = ((AlignmentSpan) spans[n]).getAlignment();
                            paraAlign = ((AlignmentSpan) spans[n]).getAlignment();
                            break;
                        }
                    }
@@ -360,6 +360,16 @@ public abstract class Layout {
                tabStopsIsInitialized = true;
            }

            // Determine whether the line aligns to normal, opposite, or center.
            Alignment align = paraAlign;
            if (align == Alignment.ALIGN_LEFT) {
                align = (dir == DIR_LEFT_TO_RIGHT) ?
                    Alignment.ALIGN_NORMAL : Alignment.ALIGN_OPPOSITE;
            } else if (align == Alignment.ALIGN_RIGHT) {
                align = (dir == DIR_LEFT_TO_RIGHT) ?
                    Alignment.ALIGN_OPPOSITE : Alignment.ALIGN_NORMAL;
            }

            int x;
            if (align == Alignment.ALIGN_NORMAL) {
                if (dir == DIR_LEFT_TO_RIGHT) {
@@ -411,7 +421,9 @@ public abstract class Layout {
        int dir = getParagraphDirection(line);

        int x;
        if (align == Alignment.ALIGN_NORMAL) {
        if (align == Alignment.ALIGN_LEFT) {
            x = left;
        } else if (align == Alignment.ALIGN_NORMAL) {
            if (dir == DIR_LEFT_TO_RIGHT) {
                x = left;
            } else {
@@ -430,7 +442,9 @@ public abstract class Layout {
                }
            }
            int max = (int)getLineExtent(line, tabStops, false);
            if (align == Alignment.ALIGN_OPPOSITE) {
            if (align == Alignment.ALIGN_RIGHT) {
                x = right - max;
            } else if (align == Alignment.ALIGN_OPPOSITE) {
                if (dir == DIR_LEFT_TO_RIGHT) {
                    x = right - max;
                } else {
@@ -738,11 +752,15 @@ public abstract class Layout {
        int dir = getParagraphDirection(line);
        Alignment align = getParagraphAlignment(line);

        if (align == Alignment.ALIGN_NORMAL) {
        if (align == Alignment.ALIGN_LEFT) {
            return 0;
        } else if (align == Alignment.ALIGN_NORMAL) {
            if (dir == DIR_RIGHT_TO_LEFT)
                return getParagraphRight(line) - getLineMax(line);
            else
                return 0;
        } else if (align == Alignment.ALIGN_RIGHT) {
            return mWidth - getLineMax(line);
        } else if (align == Alignment.ALIGN_OPPOSITE) {
            if (dir == DIR_RIGHT_TO_LEFT)
                return 0;
@@ -765,11 +783,15 @@ public abstract class Layout {
        int dir = getParagraphDirection(line);
        Alignment align = getParagraphAlignment(line);

        if (align == Alignment.ALIGN_NORMAL) {
        if (align == Alignment.ALIGN_LEFT) {
            return getParagraphLeft(line) + getLineMax(line);
        } else if (align == Alignment.ALIGN_NORMAL) {
            if (dir == DIR_RIGHT_TO_LEFT)
                return mWidth;
            else
                return getParagraphLeft(line) + getLineMax(line);
        } else if (align == Alignment.ALIGN_RIGHT) {
            return mWidth;
        } else if (align == Alignment.ALIGN_OPPOSITE) {
            if (dir == DIR_RIGHT_TO_LEFT)
                return getLineMax(line);
@@ -1765,8 +1787,10 @@ public abstract class Layout {
        ALIGN_NORMAL,
        ALIGN_OPPOSITE,
        ALIGN_CENTER,
        // XXX ALIGN_LEFT,
        // XXX ALIGN_RIGHT,
        /** @hide */
        ALIGN_LEFT,
        /** @hide */
        ALIGN_RIGHT,
    }

    private static final int TAB_INCREMENT = 20;
+8 −2
Original line number Diff line number Diff line
@@ -9121,9 +9121,15 @@ public class View implements Drawable.Callback2, KeyEvent.Callback, Accessibilit
    }

    /**
     * Reset the resolved layout direction by clearing the corresponding flag
     * Reset the resolved layout direction.
     *
     * Subclasses need to override this method to clear cached information that depends on the
     * resolved layout direction, or to inform child views that inherit their layout direction.
     * Overrides must also call the superclass implementation at the start of their implementation.
     *
     * @hide
     */
    void resetLayoutDirectionResolution() {
    protected void resetLayoutDirectionResolution() {
        // Reset the current View resolution
        mPrivateFlags2 &= ~LAYOUT_DIRECTION_RESOLVED;
    }
+1 −5
Original line number Diff line number Diff line
@@ -4999,12 +4999,8 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager
        viewAncestor.requestTransitionStart(transition);
    }

    /**
     * This method will be called when we need to reset the layout direction resolution flag
     *
     */
    @Override
    void resetLayoutDirectionResolution() {
    protected void resetLayoutDirectionResolution() {
        super.resetLayoutDirectionResolution();

        // Take care of resetting the children resolution too
+78 −19
Original line number Diff line number Diff line
@@ -340,6 +340,16 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener

    private WordIterator mWordIterator;

    // The alignment to pass to Layout, or null if not resolved.
    private Layout.Alignment mLayoutAlignment;

    // The default value for mTextAlign.
    private TextAlign mTextAlign = TextAlign.INHERIT;

    private static enum TextAlign {
        INHERIT, GRAVITY, TEXT_START, TEXT_END, CENTER, VIEW_START, VIEW_END;
    }

    /*
     * Kick-start the font cache for the zygote process (to pay the cost of
     * initializing freetype for our default font only once).
@@ -5532,6 +5542,73 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
                      physicalWidth, false);
    }

    @Override
    protected void resetLayoutDirectionResolution() {
        super.resetLayoutDirectionResolution();

        if (mLayoutAlignment != null &&
                (mTextAlign == TextAlign.VIEW_START ||
                mTextAlign == TextAlign.VIEW_END)) {
            mLayoutAlignment = null;
        }
    }

    private Layout.Alignment getLayoutAlignment() {
        if (mLayoutAlignment == null) {
            Layout.Alignment alignment;
            TextAlign textAlign = mTextAlign;
            switch (textAlign) {
                case INHERIT:
                    // fall through to gravity temporarily
                    // intention is to inherit value through view hierarchy.
                case GRAVITY:
                    switch (mGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK) {
                        case Gravity.START:
                            alignment = Layout.Alignment.ALIGN_NORMAL;
                            break;
                        case Gravity.END:
                            alignment = Layout.Alignment.ALIGN_OPPOSITE;
                            break;
                        case Gravity.LEFT:
                            alignment = Layout.Alignment.ALIGN_LEFT;
                            break;
                        case Gravity.RIGHT:
                            alignment = Layout.Alignment.ALIGN_RIGHT;
                            break;
                        case Gravity.CENTER_HORIZONTAL:
                            alignment = Layout.Alignment.ALIGN_CENTER;
                            break;
                        default:
                            alignment = Layout.Alignment.ALIGN_NORMAL;
                            break;
                    }
                    break;
                case TEXT_START:
                    alignment = Layout.Alignment.ALIGN_NORMAL;
                    break;
                case TEXT_END:
                    alignment = Layout.Alignment.ALIGN_OPPOSITE;
                    break;
                case CENTER:
                    alignment = Layout.Alignment.ALIGN_CENTER;
                    break;
                case VIEW_START:
                    alignment = (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
                            Layout.Alignment.ALIGN_RIGHT : Layout.Alignment.ALIGN_LEFT;
                    break;
                case VIEW_END:
                    alignment = (getResolvedLayoutDirection() == LAYOUT_DIRECTION_RTL) ?
                            Layout.Alignment.ALIGN_LEFT : Layout.Alignment.ALIGN_RIGHT;
                    break;
                default:
                    alignment = Layout.Alignment.ALIGN_NORMAL;
                    break;
            }
            mLayoutAlignment = alignment;
        }
        return mLayoutAlignment;
    }

    /**
     * The width passed in is now the desired layout width,
     * not the full view width with padding.
@@ -5552,25 +5629,7 @@ public class TextView extends View implements ViewTreeObserver.OnPreDrawListener
            hintWidth = 0;
        }

        final int layoutDirection = getResolvedLayoutDirection();
        final int absoluteGravity = Gravity.getAbsoluteGravity(mGravity, layoutDirection);

        Layout.Alignment alignment;
        switch (absoluteGravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
            case Gravity.CENTER_HORIZONTAL:
                alignment = Layout.Alignment.ALIGN_CENTER;
                break;

            case Gravity.RIGHT:
                // Note, Layout resolves ALIGN_OPPOSITE to left or
                // right based on the paragraph direction.
                alignment = Layout.Alignment.ALIGN_OPPOSITE;
                break;

            default:
                alignment = Layout.Alignment.ALIGN_NORMAL;
        }

        Layout.Alignment alignment = getLayoutAlignment();
        boolean shouldEllipsize = mEllipsize != null && mInput == null;

        if (mText instanceof Spannable) {