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

Commit 10ca24a9 authored by Philip Milne's avatar Philip Milne
Browse files

Promote layout debugging code from GridLayout to ViewGroup.

Layout debugging code draws rectangles around:

1. Layout insets (red)
2. Bounds (blue)
3. Margins (magenta)

Layout debug mode is enabled with:

adb shell setprop debug.layout true

Change-Id: Ia155a2d0fbf33693a1e3c040f627ea3a534e1aff
parent 6ec0c6af
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -45,6 +45,7 @@ import android.os.Parcel;
import android.os.Parcelable;
import android.os.RemoteException;
import android.os.SystemClock;
import android.os.SystemProperties;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.FloatProperty;
@@ -16913,6 +16914,11 @@ public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Cal
         */
        Drawable mAccessibilityFocusDrawable;
        /**
         * Show where the margins, bounds and layout bounds are for each view.
         */
        final boolean mDebugLayout = SystemProperties.getBoolean("debug.layout", false);
        /**
         * Creates a new set of attachment information with the specified
         * events handler and thread.
+86 −1
Original line number Diff line number Diff line
@@ -22,6 +22,8 @@ import android.content.res.Configuration;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Insets;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PointF;
@@ -420,9 +422,15 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager
        initFromAttributes(context, attrs);
    }

    private boolean debugDraw() {
        return mAttachInfo != null && mAttachInfo.mDebugLayout;
    }

    private void initViewGroup() {
        // ViewGroup doesn't draw by default
        if (!debugDraw()) {
            setFlags(WILL_NOT_DRAW, DRAW_MASK);
        }
        mGroupFlags |= FLAG_CLIP_CHILDREN;
        mGroupFlags |= FLAG_CLIP_TO_PADDING;
        mGroupFlags |= FLAG_ANIMATION_DONE;
@@ -2584,6 +2592,52 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager
        return b;
    }

    private static void drawRect(Canvas canvas, int x1, int y1, int x2, int y2, Paint paint) {
        canvas.drawRect(x1, y1, x2 - 1, y2 - 1, paint);
    }

    /**
     * @hide
     */
    protected void onDebugDrawMargins(Canvas canvas) {
        for (int i = 0; i < getChildCount(); i++) {
            View c = getChildAt(i);
            c.getLayoutParams().onDebugDraw(this, canvas);
        }
    }

    /**
     * @hide
     */
    protected void onDebugDraw(Canvas canvas) {
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);

        // Draw optical bounds
        if (getLayoutMode() == LAYOUT_BOUNDS) {
            paint.setColor(Color.RED);
            for (int i = 0; i < getChildCount(); i++) {
                View c = getChildAt(i);
                Insets insets = c.getLayoutInsets();
                drawRect(canvas,
                        c.getLeft() + insets.left,
                        c.getTop() + insets.top,
                        c.getRight() - insets.right,
                        c.getBottom() - insets.bottom, paint);
            }
        }

        // Draw bounds
        paint.setColor(Color.BLUE);
        for (int i = 0; i < getChildCount(); i++) {
            View c = getChildAt(i);
            drawRect(canvas, c.getLeft(), c.getTop(), c.getRight(), c.getBottom(), paint);
        }

        // Draw margins
        onDebugDrawMargins(canvas);
    }

    /**
     * {@inheritDoc}
     */
@@ -2675,6 +2729,10 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager
            }
        }

        if (debugDraw()) {
            onDebugDraw(canvas);
        }

        if (clipToPadding) {
            canvas.restoreToCount(saveCount);
        }
@@ -5392,6 +5450,17 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager
                    + sizeToString(width) + ", height=" + sizeToString(height) + " }";
        }

        /**
         * Use {@code canvas} to draw suitable debugging annotations for these LayoutParameters.
         *
         * @param view the view that contains these layout parameters
         * @param canvas the canvas on which to draw
         *
         * @hide
         */
        public void onDebugDraw(View view, Canvas canvas) {
        }

        /**
         * Converts the specified size to a readable String.
         *
@@ -5642,6 +5711,22 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager
                    break;
            }
        }

        /**
         * @hide
         */
        @Override
        public void onDebugDraw(View view, Canvas canvas) {
            Paint paint = new Paint();
            paint.setStyle(Paint.Style.STROKE);
            paint.setColor(Color.MAGENTA);

            drawRect(canvas,
                    view.getLeft() - leftMargin,
                    view.getTop() - topMargin,
                    view.getRight() + rightMargin,
                    view.getBottom() + bottomMargin, paint);
        }
    }

    /* Describes a touched view and the ids of the pointers that it has captured.
+39 −46
Original line number Diff line number Diff line
@@ -30,8 +30,8 @@ import android.view.View;
import android.view.ViewGroup;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import com.android.internal.R;
import android.widget.RemoteViews.RemoteView;
import com.android.internal.R;

import java.lang.reflect.Array;
import java.util.ArrayList;
@@ -209,7 +209,6 @@ public class GridLayout extends ViewGroup {
    // Misc constants

    static final String TAG = GridLayout.class.getName();
    static final boolean DEBUG = false;
    static final int MAX_SIZE = 100000;
    static final int DEFAULT_CONTAINER_MARGIN = 0;
    static final int UNINITIALIZED_HASH = 0;
@@ -249,9 +248,6 @@ public class GridLayout extends ViewGroup {
     */
    public GridLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        if (DEBUG) {
            setWillNotDraw(false);
        }
        defaultGap = context.getResources().getDimensionPixelOffset(R.dimen.default_gap);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.GridLayout);
        try {
@@ -772,15 +768,29 @@ public class GridLayout extends ViewGroup {
        }
    }

    private static void drawRect(Canvas canvas, int x1, int y1, int x2, int y2, Paint paint) {
        canvas.drawRect(x1, y1, x2 - 1, y2 - 1, paint);
    /**
     * @hide
     */
    @Override
    protected void onDebugDrawMargins(Canvas canvas) {
        // Apply defaults, so as to remove UNDEFINED values
        LayoutParams lp = new LayoutParams();
        for (int i = 0; i < getChildCount(); i++) {
            View c = getChildAt(i);
            lp.setMargins(
                    getMargin1(c, true, true),
                    getMargin1(c, false, true),
                    getMargin1(c, true, false),
                    getMargin1(c, false, false));
            lp.onDebugDraw(c, canvas);
        }
    }

    /**
     * @hide
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (DEBUG) {
    protected void onDebugDraw(Canvas canvas) {
        int height = getHeight() - getPaddingTop() - getPaddingBottom();
        int width = getWidth() - getPaddingLeft() - getPaddingRight();

@@ -804,24 +814,7 @@ public class GridLayout extends ViewGroup {
            }
        }

            // Draw bounds
            paint.setColor(Color.BLUE);
            for (int i = 0; i < getChildCount(); i++) {
                View c = getChildAt(i);
                drawRect(canvas, c.getLeft(), c.getTop(), c.getRight(), c.getBottom(), paint);
            }

            // Draw margins
            paint.setColor(Color.MAGENTA);
            for (int i = 0; i < getChildCount(); i++) {
                View c = getChildAt(i);
                drawRect(canvas,
                        c.getLeft() - getMargin1(c, true, true),
                        c.getTop() - getMargin1(c, false, true),
                        c.getRight() + getMargin1(c, true, false),
                        c.getBottom() + getMargin1(c, false, false), paint);
            }
        }
        super.onDebugDraw(canvas);
    }

    // Add/remove
+0 −9
Original line number Diff line number Diff line
@@ -2,23 +2,14 @@ package com.android.test.layout;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridLayout;
import android.widget.Space;
import android.widget.TextView;

import static android.text.InputType.TYPE_CLASS_TEXT;
import static android.text.InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS;
import static android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD;
import static android.widget.GridLayout.*;
import static android.widget.GridLayout.FILL;
import static android.widget.GridLayout.spec;

public class LayoutInsetsTest extends Activity {
    public static View create(Context context) {