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

Commit cdf38ba2 authored by Vadim Tryshev's avatar Vadim Tryshev
Browse files

Visualize view focus in debug mode.

See the bug for motivation.

Bug: 32099818
Test: Manual check with enabling the dev setting.
Change-Id: I77262f77ac709639e5b1314a0be45612bde70ad6
parent 170219c5
Loading
Loading
Loading
Loading
+56 −0
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Insets;
import android.graphics.Interpolator;
import android.graphics.LinearGradient;
@@ -757,6 +758,9 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
        AccessibilityEventSource {
    private static final boolean DBG = false;
    /** @hide */
    public static boolean DEBUG_DRAW = false;
    /**
     * The logging tag used by this class with android.util.Log.
     */
@@ -1184,6 +1188,8 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
     */
    static final int PARENT_SAVE_DISABLED_MASK = 0x20000000;
    private static Paint sDebugPaint;
    /** @hide */
    @IntDef(flag = true,
            value = {
@@ -1655,6 +1661,10 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
            | AccessibilityEvent.TYPE_VIEW_ACCESSIBILITY_FOCUSED
            | AccessibilityEvent.TYPE_VIEW_TEXT_TRAVERSED_AT_MOVEMENT_GRANULARITY;
    static final int DEBUG_CORNERS_COLOR = Color.rgb(63, 127, 255);
    static final int DEBUG_CORNERS_SIZE_DIP = 8;
    /**
     * Temporary Rect currently for use in setBackground().  This will probably
     * be extended in the future to hold our own class with more than just
@@ -4743,6 +4753,10 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
        mRenderNode = RenderNode.create(getClass().getName(), this);
    }
    final boolean debugDraw() {
        return DEBUG_DRAW || mAttachInfo != null && mAttachInfo.mDebugLayout;
    }
    private static SparseArray<String> getAttributeMap() {
        if (mAttributeMap == null) {
            mAttributeMap = new SparseArray<>();
@@ -16137,6 +16151,9 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
                        if (mOverlay != null && !mOverlay.isEmpty()) {
                            mOverlay.getOverlayView().draw(canvas);
                        }
                        if (debugDraw()) {
                            debugDrawFocus(canvas);
                        }
                    } else {
                        draw(canvas);
                    }
@@ -17115,6 +17132,41 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
        return more;
    }
    static Paint getDebugPaint() {
        if (sDebugPaint == null) {
            sDebugPaint = new Paint();
            sDebugPaint.setAntiAlias(false);
        }
        return sDebugPaint;
    }
    final int dipsToPixels(int dips) {
        float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dips * scale + 0.5f);
    }
    final private void debugDrawFocus(Canvas canvas) {
        if (isFocused()) {
            final int cornerSquareSize = dipsToPixels(DEBUG_CORNERS_SIZE_DIP);
            final int w = getWidth();
            final int h = getHeight();
            final Paint paint = getDebugPaint();
            paint.setColor(DEBUG_CORNERS_COLOR);
            // Draw squares in corners.
            paint.setStyle(Paint.Style.FILL);
            canvas.drawRect(0, 0, cornerSquareSize, cornerSquareSize, paint);
            canvas.drawRect(w - cornerSquareSize, 0, w, cornerSquareSize, paint);
            canvas.drawRect(0, h - cornerSquareSize, cornerSquareSize, h, paint);
            canvas.drawRect(w - cornerSquareSize, h - cornerSquareSize, w, h, paint);
            // Draw big X across the view.
            paint.setStyle(Paint.Style.STROKE);
            canvas.drawLine(0, 0, getWidth(), getHeight(), paint);
            canvas.drawLine(0, getHeight(), getWidth(), 0, paint);
        }
    }
    /**
     * Manually render this view (and all of its children) to the given Canvas.
     * The view must have already done a full layout before this function is
@@ -17169,6 +17221,10 @@ public class View implements Drawable.Callback, KeyEvent.Callback,
            // Step 6, draw decorations (foreground, scrollbars)
            onDrawForeground(canvas);
            if (debugDraw()) {
                debugDrawFocus(canvas);
            }
            // we're done...
            return;
        }
+2 −22
Original line number Diff line number Diff line
@@ -116,8 +116,6 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager
    private static final String TAG = "ViewGroup";

    private static final boolean DBG = false;
    /** @hide */
    public static boolean DEBUG_DRAW = false;

    /**
     * Views which have been hidden or removed which need to be animated on
@@ -476,7 +474,6 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager
    private static final int ARRAY_INITIAL_CAPACITY = 12;
    private static final int ARRAY_CAPACITY_INCREMENT = 12;

    private static Paint sDebugPaint;
    private static float[] sDebugLines;

    // Used to draw cached views
@@ -586,10 +583,6 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager
        initFromAttributes(context, attrs, defStyleAttr, defStyleRes);
    }

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

    private void initViewGroup() {
        // ViewGroup doesn't draw by default
        if (!debugDraw()) {
@@ -3375,11 +3368,6 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager
        fillRect(c, paint, x1, y1, x1 + lw * sign(dx), y1 + dy);
    }

    private int dipsToPixels(int dips) {
        float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dips * scale + 0.5f);
    }

    private static void drawRectCorners(Canvas canvas, int x1, int y1, int x2, int y2, Paint paint,
            int lineLength, int lineWidth) {
        drawCorner(canvas, paint, x1, y1, lineLength, lineLength, lineWidth);
@@ -3448,10 +3436,10 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager

        // Draw clip bounds
        {
            paint.setColor(Color.rgb(63, 127, 255));
            paint.setColor(DEBUG_CORNERS_COLOR);
            paint.setStyle(Paint.Style.FILL);

            int lineLength = dipsToPixels(8);
            int lineLength = dipsToPixels(DEBUG_CORNERS_SIZE_DIP);
            int lineWidth = dipsToPixels(1);
            for (int i = 0; i < getChildCount(); i++) {
                View c = getChildAt(i);
@@ -7926,14 +7914,6 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager
        }
    }

    private static Paint getDebugPaint() {
        if (sDebugPaint == null) {
            sDebugPaint = new Paint();
            sDebugPaint.setAntiAlias(false);
        }
        return sDebugPaint;
    }

    private static void drawRect(Canvas canvas, Paint paint, int x1, int y1, int x2, int y2) {
        if (sDebugLines== null) {
            // TODO: This won't work with multiple UI threads in a single process