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

Commit add6c8c2 authored by Will Leshner's avatar Will Leshner
Browse files

Provide a way to specify complication margin.

In particular, this is the margin between complications in the
layout direction. This allows a complication to specify a margin
of 0 in order to be closer to the compication next to it.

Bug: 241251638
Test: ComplicationLayoutEngineTest, CompliationLayoutParmsTest
Change-Id: I40e2bffbc021cf3b9f4112d508f3d117c7916556
parent 67cf47bb
Loading
Loading
Loading
Loading
+17 −16
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@ package com.android.systemui.dreams.complication;

import static com.android.systemui.dreams.complication.dagger.ComplicationHostViewModule.COMPLICATIONS_FADE_IN_DURATION;
import static com.android.systemui.dreams.complication.dagger.ComplicationHostViewModule.COMPLICATIONS_FADE_OUT_DURATION;
import static com.android.systemui.dreams.complication.dagger.ComplicationHostViewModule.COMPLICATION_MARGIN;
import static com.android.systemui.dreams.complication.dagger.ComplicationHostViewModule.COMPLICATION_MARGIN_DEFAULT;
import static com.android.systemui.dreams.complication.dagger.ComplicationHostViewModule.SCOPED_COMPLICATIONS_LAYOUT;

import android.animation.Animator;
@@ -67,7 +67,7 @@ public class ComplicationLayoutEngine implements Complication.VisibilityControll
        private final Parent mParent;
        @Complication.Category
        private final int mCategory;
        private final int mMargin;
        private final int mDefaultMargin;

        /**
         * Default constructor. {@link Parent} allows for the {@link ViewEntry}'s surrounding
@@ -75,7 +75,7 @@ public class ComplicationLayoutEngine implements Complication.VisibilityControll
         */
        ViewEntry(View view, ComplicationLayoutParams layoutParams,
                TouchInsetManager.TouchInsetSession touchSession, int category, Parent parent,
                int margin) {
                int defaultMargin) {
            mView = view;
            // Views that are generated programmatically do not have a unique id assigned to them
            // at construction. A new id is assigned here to enable ConstraintLayout relative
@@ -86,7 +86,7 @@ public class ComplicationLayoutEngine implements Complication.VisibilityControll
            mTouchInsetSession = touchSession;
            mCategory = category;
            mParent = parent;
            mMargin = margin;
            mDefaultMargin = defaultMargin;

            touchSession.addViewToTracking(mView);
        }
@@ -195,18 +195,19 @@ public class ComplicationLayoutEngine implements Complication.VisibilityControll
                }

                if (!isRoot) {
                    final int margin = mLayoutParams.getMargin(mDefaultMargin);
                    switch(direction) {
                        case ComplicationLayoutParams.DIRECTION_DOWN:
                            params.setMargins(0, mMargin, 0, 0);
                            params.setMargins(0, margin, 0, 0);
                            break;
                        case ComplicationLayoutParams.DIRECTION_UP:
                            params.setMargins(0, 0, 0, mMargin);
                            params.setMargins(0, 0, 0, margin);
                            break;
                        case ComplicationLayoutParams.DIRECTION_END:
                            params.setMarginStart(mMargin);
                            params.setMarginStart(margin);
                            break;
                        case ComplicationLayoutParams.DIRECTION_START:
                            params.setMarginEnd(mMargin);
                            params.setMarginEnd(margin);
                            break;
                    }
                }
@@ -263,7 +264,7 @@ public class ComplicationLayoutEngine implements Complication.VisibilityControll
            private final ComplicationLayoutParams mLayoutParams;
            private final int mCategory;
            private Parent mParent;
            private int mMargin;
            private int mDefaultMargin;

            Builder(View view, TouchInsetManager.TouchInsetSession touchSession,
                    ComplicationLayoutParams lp, @Complication.Category int category) {
@@ -302,8 +303,8 @@ public class ComplicationLayoutEngine implements Complication.VisibilityControll
             * Sets the margin that will be applied in the direction the complication is laid out
             * towards.
             */
            Builder setMargin(int margin) {
                mMargin = margin;
            Builder setDefaultMargin(int margin) {
                mDefaultMargin = margin;
                return this;
            }

@@ -312,7 +313,7 @@ public class ComplicationLayoutEngine implements Complication.VisibilityControll
             */
            ViewEntry build() {
                return new ViewEntry(mView, mLayoutParams, mTouchSession, mCategory, mParent,
                        mMargin);
                        mDefaultMargin);
            }
        }

@@ -472,7 +473,7 @@ public class ComplicationLayoutEngine implements Complication.VisibilityControll
    }

    private final ConstraintLayout mLayout;
    private final int mMargin;
    private final int mDefaultMargin;
    private final HashMap<ComplicationId, ViewEntry> mEntries = new HashMap<>();
    private final HashMap<Integer, PositionGroup> mPositions = new HashMap<>();
    private final TouchInsetManager.TouchInsetSession mSession;
@@ -483,12 +484,12 @@ public class ComplicationLayoutEngine implements Complication.VisibilityControll
    /** */
    @Inject
    public ComplicationLayoutEngine(@Named(SCOPED_COMPLICATIONS_LAYOUT) ConstraintLayout layout,
            @Named(COMPLICATION_MARGIN) int margin,
            @Named(COMPLICATION_MARGIN_DEFAULT) int defaultMargin,
            TouchInsetManager.TouchInsetSession session,
            @Named(COMPLICATIONS_FADE_IN_DURATION) int fadeInDuration,
            @Named(COMPLICATIONS_FADE_OUT_DURATION) int fadeOutDuration) {
        mLayout = layout;
        mMargin = margin;
        mDefaultMargin = defaultMargin;
        mSession = session;
        mFadeInDuration = fadeInDuration;
        mFadeOutDuration = fadeOutDuration;
@@ -537,7 +538,7 @@ public class ComplicationLayoutEngine implements Complication.VisibilityControll
        }

        final ViewEntry.Builder entryBuilder = new ViewEntry.Builder(view, mSession, lp, category)
                .setMargin(mMargin);
                .setDefaultMargin(mDefaultMargin);

        // Add position group if doesn't already exist
        final int position = lp.getPosition();
+55 −1
Original line number Diff line number Diff line
@@ -51,6 +51,8 @@ public class ComplicationLayoutParams extends ViewGroup.LayoutParams {
    private static final int FIRST_POSITION = POSITION_TOP;
    private static final int LAST_POSITION = POSITION_END;

    private static final int MARGIN_UNSPECIFIED = 0xFFFFFFFF;

    @Retention(RetentionPolicy.SOURCE)
    @IntDef(flag = true, prefix = { "DIRECTION_" }, value = {
            DIRECTION_UP,
@@ -77,6 +79,8 @@ public class ComplicationLayoutParams extends ViewGroup.LayoutParams {

    private final int mWeight;

    private final int mMargin;

    private final boolean mSnapToGuide;

    // Do not allow specifying opposite positions
@@ -106,7 +110,24 @@ public class ComplicationLayoutParams extends ViewGroup.LayoutParams {
     */
    public ComplicationLayoutParams(int width, int height, @Position int position,
            @Direction int direction, int weight) {
        this(width, height, position, direction, weight, false);
        this(width, height, position, direction, weight, MARGIN_UNSPECIFIED, false);
    }

    /**
     * Constructs a {@link ComplicationLayoutParams}.
     * @param width The width {@link android.view.View.MeasureSpec} for the view.
     * @param height The height {@link android.view.View.MeasureSpec} for the view.
     * @param position The place within the parent container where the view should be positioned.
     * @param direction The direction the view should be laid out from either the parent container
     *                  or preceding view.
     * @param weight The weight that should be considered for this view when compared to other
     *               views. This has an impact on the placement of the view but not the rendering of
     *               the view.
     * @param margin The margin to apply between complications.
     */
    public ComplicationLayoutParams(int width, int height, @Position int position,
            @Direction int direction, int weight, int margin) {
        this(width, height, position, direction, weight, margin, false);
    }

    /**
@@ -127,6 +148,28 @@ public class ComplicationLayoutParams extends ViewGroup.LayoutParams {
     */
    public ComplicationLayoutParams(int width, int height, @Position int position,
            @Direction int direction, int weight, boolean snapToGuide) {
        this(width, height, position, direction, weight, MARGIN_UNSPECIFIED, snapToGuide);
    }

    /**
     * Constructs a {@link ComplicationLayoutParams}.
     * @param width The width {@link android.view.View.MeasureSpec} for the view.
     * @param height The height {@link android.view.View.MeasureSpec} for the view.
     * @param position The place within the parent container where the view should be positioned.
     * @param direction The direction the view should be laid out from either the parent container
     *                  or preceding view.
     * @param weight The weight that should be considered for this view when compared to other
     *               views. This has an impact on the placement of the view but not the rendering of
     *               the view.
     * @param margin The margin to apply between complications.
     * @param snapToGuide When set to {@code true}, the dimension perpendicular to the direction
     *                    will be automatically set to align with a predetermined guide for that
     *                    side. For example, if the complication is aligned to the top end and
     *                    direction is down, then the width of the complication will be set to span
     *                    from the end of the parent to the guide.
     */
    public ComplicationLayoutParams(int width, int height, @Position int position,
            @Direction int direction, int weight, int margin, boolean snapToGuide) {
        super(width, height);

        if (!validatePosition(position)) {
@@ -142,6 +185,8 @@ public class ComplicationLayoutParams extends ViewGroup.LayoutParams {

        mWeight = weight;

        mMargin = margin;

        mSnapToGuide = snapToGuide;
    }

@@ -153,6 +198,7 @@ public class ComplicationLayoutParams extends ViewGroup.LayoutParams {
        mPosition = source.mPosition;
        mDirection = source.mDirection;
        mWeight = source.mWeight;
        mMargin = source.mMargin;
        mSnapToGuide = source.mSnapToGuide;
    }

@@ -214,6 +260,14 @@ public class ComplicationLayoutParams extends ViewGroup.LayoutParams {
        return mWeight;
    }

    /**
     * Returns the margin to apply between complications, or the given default if no margin is
     * specified.
     */
    public int getMargin(int defaultMargin) {
        return mMargin == MARGIN_UNSPECIFIED ? defaultMargin : mMargin;
    }

    /**
     * Returns whether the complication's dimension perpendicular to direction should be
     * automatically set.
+2 −2
Original line number Diff line number Diff line
@@ -37,7 +37,7 @@ import dagger.Provides;
@Module
public abstract class ComplicationHostViewModule {
    public static final String SCOPED_COMPLICATIONS_LAYOUT = "scoped_complications_layout";
    public static final String COMPLICATION_MARGIN = "complication_margin";
    public static final String COMPLICATION_MARGIN_DEFAULT = "complication_margin_default";
    public static final String COMPLICATIONS_FADE_OUT_DURATION = "complications_fade_out_duration";
    public static final String COMPLICATIONS_FADE_IN_DURATION = "complications_fade_in_duration";
    public static final String COMPLICATIONS_RESTORE_TIMEOUT = "complication_restore_timeout";
@@ -58,7 +58,7 @@ public abstract class ComplicationHostViewModule {
    }

    @Provides
    @Named(COMPLICATION_MARGIN)
    @Named(COMPLICATION_MARGIN_DEFAULT)
    @DreamOverlayComponent.DreamOverlayScope
    static int providesComplicationPadding(@Main Resources resources) {
        return resources.getDimensionPixelSize(R.dimen.dream_overlay_complication_margin);
+18 −17
Original line number Diff line number Diff line
@@ -108,6 +108,7 @@ public interface RegisteredComplicationsModule {
                        | ComplicationLayoutParams.POSITION_START,
                ComplicationLayoutParams.DIRECTION_DOWN,
                DREAM_SMARTSPACE_COMPLICATION_WEIGHT,
                0,
                true /*snapToGuide*/);
    }
}
+70 −2
Original line number Diff line number Diff line
@@ -297,10 +297,10 @@ public class ComplicationLayoutEngineTest extends SysuiTestCase {
    }

    /**
     * Ensures margin is applied
     * Ensures default margin is applied
     */
    @Test
    public void testMargin() {
    public void testDefaultMargin() {
        final int margin = 5;
        final ComplicationLayoutEngine engine =
                new ComplicationLayoutEngine(mLayout, margin, mTouchSession, 0, 0);
@@ -372,6 +372,74 @@ public class ComplicationLayoutEngineTest extends SysuiTestCase {
        });
    }

    /**
     * Ensures complication margin is applied
     */
    @Test
    public void testComplicationMargin() {
        final int defaultMargin = 5;
        final int complicationMargin = 10;
        final ComplicationLayoutEngine engine =
                new ComplicationLayoutEngine(mLayout, defaultMargin, mTouchSession, 0, 0);

        final ViewInfo firstViewInfo = new ViewInfo(
                new ComplicationLayoutParams(
                        100,
                        100,
                        ComplicationLayoutParams.POSITION_TOP
                                | ComplicationLayoutParams.POSITION_END,
                        ComplicationLayoutParams.DIRECTION_DOWN,
                        0,
                        complicationMargin),
                Complication.CATEGORY_STANDARD,
                mLayout);

        addComplication(engine, firstViewInfo);

        final ViewInfo secondViewInfo = new ViewInfo(
                new ComplicationLayoutParams(
                        100,
                        100,
                        ComplicationLayoutParams.POSITION_TOP
                                | ComplicationLayoutParams.POSITION_END,
                        ComplicationLayoutParams.DIRECTION_START,
                        0),
                Complication.CATEGORY_SYSTEM,
                mLayout);

        addComplication(engine, secondViewInfo);

        firstViewInfo.clearInvocations();
        secondViewInfo.clearInvocations();

        final ViewInfo thirdViewInfo = new ViewInfo(
                new ComplicationLayoutParams(
                        100,
                        100,
                        ComplicationLayoutParams.POSITION_TOP
                                | ComplicationLayoutParams.POSITION_END,
                        ComplicationLayoutParams.DIRECTION_START,
                        1),
                Complication.CATEGORY_SYSTEM,
                mLayout);

        addComplication(engine, thirdViewInfo);

        // The first added view should now be underneath the second view.
        verifyChange(firstViewInfo, false, lp -> {
            assertThat(lp.topToBottom == thirdViewInfo.view.getId()).isTrue();
            assertThat(lp.endToEnd == ConstraintLayout.LayoutParams.PARENT_ID).isTrue();
            assertThat(lp.topMargin).isEqualTo(complicationMargin);
        });

        // The second view should be in underneath the third view.
        verifyChange(secondViewInfo, false, lp -> {
            assertThat(lp.endToStart == thirdViewInfo.view.getId()).isTrue();
            assertThat(lp.topToTop == ConstraintLayout.LayoutParams.PARENT_ID).isTrue();
            assertThat(lp.getMarginEnd()).isEqualTo(defaultMargin);
        });
    }

    /**
     * Ensures layout in a particular position updates.
     */
Loading