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

Commit c9885f65 authored by Philip Milne's avatar Philip Milne
Browse files

Future-proofing: change Alignment interface to abstract class and add a...

Future-proofing: change Alignment interface to abstract class and add a parameter for measurementType.

Also included bugfix from Winson: arcs wre not being cleared in invalidateStructure() so
state wasn't properly cleared when children were added after the first layout operation.

Change-Id: I7b46eadd955bd74b7bb3e4dad2f47ed3f092ff63
parent 7ff1c2ae
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -24800,9 +24800,10 @@ package android.widget {
    field public static final int VERTICAL = 1; // 0x1
  }
  public static abstract interface GridLayout.Alignment {
    method public abstract int getAlignmentValue(android.view.View, int);
    method public abstract int getSizeInCell(android.view.View, int, int);
  public static abstract class GridLayout.Alignment {
    ctor public GridLayout.Alignment();
    method public abstract int getAlignmentValue(android.view.View, int, int);
    method public int getSizeInCell(android.view.View, int, int, int);
  }
  public static class GridLayout.Group {
+44 −39
Original line number Diff line number Diff line
@@ -844,8 +844,8 @@ public class GridLayout extends ViewGroup {
            int pWidth = getMeasurement(view, true, PRF);
            int pHeight = getMeasurement(view, false, PRF);

            Alignment hAlignment = columnGroup.alignment;
            Alignment vAlignment = rowGroup.alignment;
            Alignment hAlign = columnGroup.alignment;
            Alignment vAlign = rowGroup.alignment;

            int dx, dy;

@@ -853,8 +853,9 @@ public class GridLayout extends ViewGroup {
            Bounds rowBounds = mVerticalAxis.getGroupBounds().getValue(i);

            // Gravity offsets: the location of the alignment group relative to its cell group.
            int c2ax = protect(hAlignment.getAlignmentValue(null, cellWidth - colBounds.size()));
            int c2ay = protect(vAlignment.getAlignmentValue(null, cellHeight - rowBounds.size()));
            int type = PRF;
            int c2ax = protect(hAlign.getAlignmentValue(null, cellWidth - colBounds.size(), type));
            int c2ay = protect(vAlign.getAlignmentValue(null, cellHeight - rowBounds.size(), type));

            if (mMarginsIncludedInAlignment) {
                int leftMargin = getMargin(view, true, true);
@@ -863,12 +864,12 @@ public class GridLayout extends ViewGroup {
                int bottomMargin = getMargin(view, false, false);

                // Same calculation as getMeasurementIncludingMargin()
                int measuredWidth = leftMargin + pWidth + rightMargin;
                int measuredHeight = topMargin + pHeight + bottomMargin;
                int mWidth = leftMargin + pWidth + rightMargin;
                int mHeight = topMargin + pHeight + bottomMargin;

                // Alignment offsets: the location of the view relative to its alignment group.
                int a2vx = colBounds.before - hAlignment.getAlignmentValue(view, measuredWidth);
                int a2vy = rowBounds.before - vAlignment.getAlignmentValue(view, measuredHeight);
                int a2vx = colBounds.before - hAlign.getAlignmentValue(view, mWidth, type);
                int a2vy = rowBounds.before - vAlign.getAlignmentValue(view, mHeight, type);

                dx = c2ax + a2vx + leftMargin;
                dy = c2ay + a2vy + topMargin;
@@ -877,15 +878,15 @@ public class GridLayout extends ViewGroup {
                cellHeight -= topMargin + bottomMargin;
            } else {
                // Alignment offsets: the location of the view relative to its alignment group.
                int a2vx = colBounds.before - hAlignment.getAlignmentValue(view, pWidth);
                int a2vy = rowBounds.before - vAlignment.getAlignmentValue(view, pHeight);
                int a2vx = colBounds.before - hAlign.getAlignmentValue(view, pWidth, type);
                int a2vy = rowBounds.before - vAlign.getAlignmentValue(view, pHeight, type);

                dx = c2ax + a2vx;
                dy = c2ay + a2vy;
            }

            int width = hAlignment.getSizeInCell(view, pWidth, cellWidth);
            int height = vAlignment.getSizeInCell(view, pHeight, cellHeight);
            int width = hAlign.getSizeInCell(view, pWidth, cellWidth, type);
            int height = vAlign.getSizeInCell(view, pHeight, cellHeight, type);

            int cx = paddingLeft + x1 + dx;
            int cy = paddingTop + y1 + dy;
@@ -1003,7 +1004,7 @@ public class GridLayout extends ViewGroup {

                int size = getMeasurementIncludingMargin(c, horizontal, PRF);
                // todo test this works correctly when the returned value is UNDEFINED
                int before = g.alignment.getAlignmentValue(c, size);
                int before = g.alignment.getAlignmentValue(c, size, PRF);
                bounds.include(before, size - before);
            }
        }
@@ -1459,6 +1460,7 @@ public class GridLayout extends ViewGroup {
            spanSizes = null;
            leadingMargins = null;
            trailingMargins = null;
            arcs = null;
            minima = null;
            weights = null;
            locations = null;
@@ -2156,17 +2158,17 @@ public class GridLayout extends ViewGroup {
     * {@link Group#alignment alignment}. Overall placement of the view in the cell
     * group is specified by the two alignments which act along each axis independently.
     * <p>
     * An Alignment implementation must define the {@link #getAlignmentValue(View, int)}
     * An Alignment implementation must define {@link #getAlignmentValue(View, int, int)},
     * to return the appropriate value for the type of alignment being defined.
     * The enclosing algorithms position the children
     * so that the values returned from the alignment
     * so that the locations defined by the alignmnet values
     * are the same for all of the views in a group.
     * <p>
     *  The GridLayout class defines the most common alignments used in general layout:
     * {@link #TOP}, {@link #LEFT}, {@link #BOTTOM}, {@link #RIGHT}, {@link #CENTER}, {@link
     * #BASELINE} and {@link #FILL}.
     */
    public static interface Alignment {
    public static abstract class Alignment {
        /**
         * Returns an alignment value. In the case of vertical alignments the value
         * returned should indicate the distance from the top of the view to the
@@ -2175,38 +2177,40 @@ public class GridLayout extends ViewGroup {
         *
         * @param view              the view to which this alignment should be applied
         * @param viewSize          the measured size of the view
         * @param measurementType   the type of measurement that should be made
         *
         * @return                  the alignment value
         */
        public int getAlignmentValue(View view, int viewSize);
        public abstract int getAlignmentValue(View view, int viewSize, int measurementType);

        /**
         * Returns the size of the view specified by this alignment.
         * In the case of vertical alignments this method should return a height; for
         * horizontal alignments this method should return the width.
         * <p>
         * The default implementation returns {@code viewSize}.
         *
         * @param view              the view to which this alignment should be applied
         * @param viewSize          the measured size of the view
         * @param cellSize          the size of the cell into which this view will be placed
         * @param measurementType   the type of measurement that should be made
         *
         * @return                  the aligned size
         */
        public int getSizeInCell(View view, int viewSize, int cellSize);
    }

    private static abstract class AbstractAlignment implements Alignment {
        public int getSizeInCell(View view, int viewSize, int cellSize) {
        public int getSizeInCell(View view, int viewSize, int cellSize, int measurementType) {
            return viewSize;
        }
    }

    private static final Alignment LEADING = new AbstractAlignment() {
        public int getAlignmentValue(View view, int viewSize) {
    private static final Alignment LEADING = new Alignment() {
        public int getAlignmentValue(View view, int viewSize, int measurementType) {
            return 0;
        }

    };

    private static final Alignment TRAILING = new AbstractAlignment() {
        public int getAlignmentValue(View view, int viewSize) {
    private static final Alignment TRAILING = new Alignment() {
        public int getAlignmentValue(View view, int viewSize, int measurementType) {
            return viewSize;
        }
    };
@@ -2240,8 +2244,8 @@ public class GridLayout extends ViewGroup {
     * This constant may be used in both {@link LayoutParams#rowGroup rowGroups} and {@link
     * LayoutParams#columnGroup columnGroups}.
     */
    public static final Alignment CENTER = new AbstractAlignment() {
        public int getAlignmentValue(View view, int viewSize) {
    public static final Alignment CENTER = new Alignment() {
        public int getAlignmentValue(View view, int viewSize, int measurementType) {
            return viewSize >> 1;
        }
    };
@@ -2253,8 +2257,8 @@ public class GridLayout extends ViewGroup {
     *
     * @see View#getBaseline()
     */
    public static final Alignment BASELINE = new AbstractAlignment() {
        public int getAlignmentValue(View view, int height) {
    public static final Alignment BASELINE = new Alignment() {
        public int getAlignmentValue(View view, int viewSize, int measurementType) {
            if (view == null) {
                return UNDEFINED;
            }
@@ -2274,11 +2278,12 @@ public class GridLayout extends ViewGroup {
     * {@link LayoutParams#columnGroup columnGroups}.
     */
    public static final Alignment FILL = new Alignment() {
        public int getAlignmentValue(View view, int viewSize) {
        public int getAlignmentValue(View view, int viewSize, int measurementType) {
            return UNDEFINED;
        }

        public int getSizeInCell(View view, int viewSize, int cellSize) {
        @Override
        public int getSizeInCell(View view, int viewSize, int cellSize, int measurementType) {
            return cellSize;
        }
    };