Merge "Define margins for complication overlay."
diff --git a/packages/SystemUI/res/layout/dream_overlay_complication_clock_date.xml b/packages/SystemUI/res/layout/dream_overlay_complication_clock_date.xml
index b6f516f..91d81a2 100644
--- a/packages/SystemUI/res/layout/dream_overlay_complication_clock_date.xml
+++ b/packages/SystemUI/res/layout/dream_overlay_complication_clock_date.xml
@@ -19,8 +19,6 @@
     android:id="@+id/date_view"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
-    android:paddingLeft="@dimen/dream_overlay_complication_clock_date_padding_left"
-    android:paddingBottom="@dimen/dream_overlay_complication_clock_date_padding_bottom"
     android:gravity="center_horizontal"
     android:textColor="@android:color/white"
     android:shadowColor="@color/keyguard_shadow_color"
diff --git a/packages/SystemUI/res/layout/dream_overlay_complication_clock_time.xml b/packages/SystemUI/res/layout/dream_overlay_complication_clock_time.xml
index 82c8d5f..4824f4c 100644
--- a/packages/SystemUI/res/layout/dream_overlay_complication_clock_time.xml
+++ b/packages/SystemUI/res/layout/dream_overlay_complication_clock_time.xml
@@ -19,7 +19,6 @@
     android:id="@+id/time_view"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
-    android:paddingLeft="@dimen/dream_overlay_complication_clock_time_padding_left"
     android:fontFamily="@font/clock"
     android:textColor="@android:color/white"
     android:format12Hour="h:mm"
diff --git a/packages/SystemUI/res/layout/dream_overlay_complication_weather.xml b/packages/SystemUI/res/layout/dream_overlay_complication_weather.xml
index 08f0d67..3900ea5 100644
--- a/packages/SystemUI/res/layout/dream_overlay_complication_weather.xml
+++ b/packages/SystemUI/res/layout/dream_overlay_complication_weather.xml
@@ -19,8 +19,6 @@
     android:id="@+id/weather_view"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
-    android:paddingLeft="@dimen/dream_overlay_complication_weather_padding_left"
-    android:paddingBottom="@dimen/dream_overlay_complication_weather_padding_bottom"
     android:textColor="@android:color/white"
     android:shadowColor="@color/keyguard_shadow_color"
     android:shadowRadius="?attr/shadowRadius"
diff --git a/packages/SystemUI/res/layout/dream_overlay_container.xml b/packages/SystemUI/res/layout/dream_overlay_container.xml
index 3c2183d..330f515 100644
--- a/packages/SystemUI/res/layout/dream_overlay_container.xml
+++ b/packages/SystemUI/res/layout/dream_overlay_container.xml
@@ -25,8 +25,14 @@
         android:id="@+id/dream_overlay_content"
         android:layout_width="match_parent"
         android:layout_height="0dp"
+        android:layout_marginTop="@dimen/dream_overlay_container_margin_top"
+        android:layout_marginEnd="@dimen/dream_overlay_container_margin_end"
+        android:layout_marginBottom="@dimen/dream_overlay_container_margin_bottom"
+        android:layout_marginStart="@dimen/dream_overlay_container_margin_start"
+
         app:layout_constraintTop_toBottomOf="@id/dream_overlay_status_bar"
-        app:layout_constraintBottom_toBottomOf="parent" />
+        app:layout_constraintBottom_toBottomOf="parent"
+        />
 
     <com.android.systemui.dreams.DreamOverlayStatusBarView
         android:id="@+id/dream_overlay_status_bar"
diff --git a/packages/SystemUI/res/values/dimens.xml b/packages/SystemUI/res/values/dimens.xml
index fdb5631..7eb25db 100644
--- a/packages/SystemUI/res/values/dimens.xml
+++ b/packages/SystemUI/res/values/dimens.xml
@@ -1349,13 +1349,8 @@
     <dimen name="dream_overlay_notifications_drag_area_height">100dp</dimen>
 
     <!-- Dream overlay complications related dimensions -->
-    <dimen name="dream_overlay_complication_clock_time_padding_left">50dp</dimen>
     <dimen name="dream_overlay_complication_clock_time_text_size">72sp</dimen>
-    <dimen name="dream_overlay_complication_clock_date_padding_left">60dp</dimen>
-    <dimen name="dream_overlay_complication_clock_date_padding_bottom">50dp</dimen>
     <dimen name="dream_overlay_complication_clock_date_text_size">18sp</dimen>
-    <dimen name="dream_overlay_complication_weather_padding_left">20dp</dimen>
-    <dimen name="dream_overlay_complication_weather_padding_bottom">50dp</dimen>
     <dimen name="dream_overlay_complication_weather_text_size">18sp</dimen>
 
     <!-- The position of the end guide, which dream overlay complications can align their start with
@@ -1390,4 +1385,13 @@
     <item name="dream_overlay_bouncer_start_region_screen_percentage" format="float" type="dimen">
         .2
     </item>
+
+    <!-- The margins applied to the dream overlay container -->
+    <dimen name="dream_overlay_container_margin_start">0dp</dimen>
+    <dimen name="dream_overlay_container_margin_end">0dp</dimen>
+    <dimen name="dream_overlay_container_margin_top">0dp</dimen>
+    <dimen name="dream_overlay_container_margin_bottom">0dp</dimen>
+
+    <!-- The margin applied between complications -->
+    <dimen name="dream_overlay_complication_margin">0dp</dimen>
 </resources>
diff --git a/packages/SystemUI/src/com/android/systemui/dreams/complication/ComplicationLayoutEngine.java b/packages/SystemUI/src/com/android/systemui/dreams/complication/ComplicationLayoutEngine.java
index 5223f37..0b80d8a 100644
--- a/packages/SystemUI/src/com/android/systemui/dreams/complication/ComplicationLayoutEngine.java
+++ b/packages/SystemUI/src/com/android/systemui/dreams/complication/ComplicationLayoutEngine.java
@@ -16,6 +16,7 @@
 
 package com.android.systemui.dreams.complication;
 
+import static com.android.systemui.dreams.complication.dagger.ComplicationHostViewComponent.COMPLICATION_MARGIN;
 import static com.android.systemui.dreams.complication.dagger.ComplicationHostViewComponent.SCOPED_COMPLICATIONS_LAYOUT;
 
 import android.util.Log;
@@ -54,12 +55,14 @@
         private final Parent mParent;
         @Complication.Category
         private final int mCategory;
+        private final int mMargin;
 
         /**
          * Default constructor. {@link Parent} allows for the {@link ViewEntry}'s surrounding
          * view hierarchy to be accessed without traversing the entire view tree.
          */
-        ViewEntry(View view, ComplicationLayoutParams layoutParams, int category, Parent parent) {
+        ViewEntry(View view, ComplicationLayoutParams layoutParams, int category, Parent parent,
+                int margin) {
             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
@@ -69,6 +72,7 @@
             mLayoutParams = layoutParams;
             mCategory = category;
             mParent = parent;
+            mMargin = margin;
         }
 
         /**
@@ -173,6 +177,23 @@
                         }
                         break;
                 }
+
+                if (!isRoot) {
+                    switch(direction) {
+                        case ComplicationLayoutParams.DIRECTION_DOWN:
+                            params.setMargins(0, mMargin, 0, 0);
+                            break;
+                        case ComplicationLayoutParams.DIRECTION_UP:
+                            params.setMargins(0, 0, 0, mMargin);
+                            break;
+                        case ComplicationLayoutParams.DIRECTION_END:
+                            params.setMarginStart(mMargin);
+                            break;
+                        case ComplicationLayoutParams.DIRECTION_START:
+                            params.setMarginEnd(mMargin);
+                            break;
+                    }
+                }
             });
 
             mView.setLayoutParams(params);
@@ -224,6 +245,7 @@
             private final ComplicationLayoutParams mLayoutParams;
             private final int mCategory;
             private Parent mParent;
+            private int mMargin;
 
             Builder(View view, ComplicationLayoutParams lp, @Complication.Category int category) {
                 mView = view;
@@ -257,10 +279,19 @@
             }
 
             /**
+             * Sets the margin that will be applied in the direction the complication is laid out
+             * towards.
+             */
+            Builder setMargin(int margin) {
+                mMargin = margin;
+                return this;
+            }
+
+            /**
              * Builds and returns the resulting {@link ViewEntry}.
              */
             ViewEntry build() {
-                return new ViewEntry(mView, mLayoutParams, mCategory, mParent);
+                return new ViewEntry(mView, mLayoutParams, mCategory, mParent, mMargin);
             }
         }
 
@@ -408,13 +439,16 @@
     }
 
     private final ConstraintLayout mLayout;
+    private final int mMargin;
     private final HashMap<ComplicationId, ViewEntry> mEntries = new HashMap<>();
     private final HashMap<Integer, PositionGroup> mPositions = new HashMap<>();
 
     /** */
     @Inject
-    public ComplicationLayoutEngine(@Named(SCOPED_COMPLICATIONS_LAYOUT) ConstraintLayout layout) {
+    public ComplicationLayoutEngine(@Named(SCOPED_COMPLICATIONS_LAYOUT) ConstraintLayout layout,
+            @Named(COMPLICATION_MARGIN) int margin) {
         mLayout = layout;
+        mMargin = margin;
     }
 
     /**
@@ -434,7 +468,8 @@
             removeComplication(id);
         }
 
-        final ViewEntry.Builder entryBuilder = new ViewEntry.Builder(view, lp, category);
+        final ViewEntry.Builder entryBuilder = new ViewEntry.Builder(view, lp, category)
+                .setMargin(mMargin);
 
         // Add position group if doesn't already exist
         final int position = lp.getPosition();
diff --git a/packages/SystemUI/src/com/android/systemui/dreams/complication/dagger/ComplicationHostViewComponent.java b/packages/SystemUI/src/com/android/systemui/dreams/complication/dagger/ComplicationHostViewComponent.java
index 4cc905e..20b0e50 100644
--- a/packages/SystemUI/src/com/android/systemui/dreams/complication/dagger/ComplicationHostViewComponent.java
+++ b/packages/SystemUI/src/com/android/systemui/dreams/complication/dagger/ComplicationHostViewComponent.java
@@ -18,12 +18,14 @@
 
 import static java.lang.annotation.RetentionPolicy.RUNTIME;
 
+import android.content.res.Resources;
 import android.view.LayoutInflater;
 
 import androidx.constraintlayout.widget.ConstraintLayout;
 
 import com.android.internal.util.Preconditions;
 import com.android.systemui.R;
+import com.android.systemui.dagger.qualifiers.Main;
 import com.android.systemui.dreams.complication.ComplicationHostViewController;
 
 import java.lang.annotation.Documented;
@@ -48,7 +50,7 @@
 @ComplicationHostViewComponent.ComplicationHostViewScope
 public interface ComplicationHostViewComponent {
     String SCOPED_COMPLICATIONS_LAYOUT = "scoped_complications_layout";
-
+    String COMPLICATION_MARGIN = "complication_margin";
     /** Scope annotation for singleton items within {@link ComplicationHostViewComponent}. */
     @Documented
     @Retention(RUNTIME)
@@ -85,5 +87,12 @@
                                     null),
                     "R.layout.dream_overlay_complications_layer did not properly inflated");
         }
+
+        @Provides
+        @Named(COMPLICATION_MARGIN)
+        @ComplicationHostViewScope
+        static int providesComplicationPadding(@Main Resources resources) {
+            return resources.getDimensionPixelSize(R.dimen.dream_overlay_complication_margin);
+        }
     }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/dreams/complication/dagger/DreamClockDateComplicationComponent.java b/packages/SystemUI/src/com/android/systemui/dreams/complication/dagger/DreamClockDateComplicationComponent.java
index eaffb1c..dd7f10c 100644
--- a/packages/SystemUI/src/com/android/systemui/dreams/complication/dagger/DreamClockDateComplicationComponent.java
+++ b/packages/SystemUI/src/com/android/systemui/dreams/complication/dagger/DreamClockDateComplicationComponent.java
@@ -104,8 +104,7 @@
                     ComplicationLayoutParams.POSITION_BOTTOM
                             | ComplicationLayoutParams.POSITION_START,
                     ComplicationLayoutParams.DIRECTION_END,
-                    INSERT_ORDER_WEIGHT,
-                    true);
+                    INSERT_ORDER_WEIGHT);
         }
     }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/dreams/complication/dagger/DreamClockTimeComplicationComponent.java b/packages/SystemUI/src/com/android/systemui/dreams/complication/dagger/DreamClockTimeComplicationComponent.java
index d539f5c..de11b61 100644
--- a/packages/SystemUI/src/com/android/systemui/dreams/complication/dagger/DreamClockTimeComplicationComponent.java
+++ b/packages/SystemUI/src/com/android/systemui/dreams/complication/dagger/DreamClockTimeComplicationComponent.java
@@ -109,8 +109,7 @@
                     ComplicationLayoutParams.POSITION_BOTTOM
                             | ComplicationLayoutParams.POSITION_START,
                     ComplicationLayoutParams.DIRECTION_UP,
-                    INSERT_ORDER_WEIGHT,
-                    true);
+                    INSERT_ORDER_WEIGHT);
         }
     }
 }
diff --git a/packages/SystemUI/src/com/android/systemui/dreams/complication/dagger/DreamWeatherComplicationComponent.java b/packages/SystemUI/src/com/android/systemui/dreams/complication/dagger/DreamWeatherComplicationComponent.java
index a282594..536f3dc 100644
--- a/packages/SystemUI/src/com/android/systemui/dreams/complication/dagger/DreamWeatherComplicationComponent.java
+++ b/packages/SystemUI/src/com/android/systemui/dreams/complication/dagger/DreamWeatherComplicationComponent.java
@@ -104,8 +104,7 @@
                     ComplicationLayoutParams.POSITION_BOTTOM
                             | ComplicationLayoutParams.POSITION_START,
                     ComplicationLayoutParams.DIRECTION_END,
-                    INSERT_ORDER_WEIGHT,
-                    true);
+                    INSERT_ORDER_WEIGHT);
         }
     }
 }
diff --git a/packages/SystemUI/tests/src/com/android/systemui/dreams/complication/ComplicationLayoutEngineTest.java b/packages/SystemUI/tests/src/com/android/systemui/dreams/complication/ComplicationLayoutEngineTest.java
index d5ab708..64b267d 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/dreams/complication/ComplicationLayoutEngineTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/dreams/complication/ComplicationLayoutEngineTest.java
@@ -112,7 +112,7 @@
                 Complication.CATEGORY_STANDARD,
                 mLayout);
 
-        final ComplicationLayoutEngine engine = new ComplicationLayoutEngine(mLayout);
+        final ComplicationLayoutEngine engine = new ComplicationLayoutEngine(mLayout, 0);
         addComplication(engine, firstViewInfo);
 
         // Ensure the view is added to the top end corner
@@ -139,7 +139,7 @@
                 Complication.CATEGORY_STANDARD,
                 mLayout);
 
-        final ComplicationLayoutEngine engine = new ComplicationLayoutEngine(mLayout);
+        final ComplicationLayoutEngine engine = new ComplicationLayoutEngine(mLayout, 0);
         addComplication(engine, firstViewInfo);
 
         // Ensure the view is added to the top end corner
@@ -155,7 +155,7 @@
      */
     @Test
     public void testDirectionLayout() {
-        final ComplicationLayoutEngine engine = new ComplicationLayoutEngine(mLayout);
+        final ComplicationLayoutEngine engine = new ComplicationLayoutEngine(mLayout, 0);
 
         final ViewInfo firstViewInfo = new ViewInfo(
                 new ComplicationLayoutParams(
@@ -203,7 +203,7 @@
      */
     @Test
     public void testPositionLayout() {
-        final ComplicationLayoutEngine engine = new ComplicationLayoutEngine(mLayout);
+        final ComplicationLayoutEngine engine = new ComplicationLayoutEngine(mLayout, 0);
 
         final ViewInfo firstViewInfo = new ViewInfo(
                 new ComplicationLayoutParams(
@@ -285,11 +285,86 @@
     }
 
     /**
+     * Ensures margin is applied
+     */
+    @Test
+    public void testMargin() {
+        final int margin = 5;
+        final ComplicationLayoutEngine engine = new ComplicationLayoutEngine(mLayout, margin);
+
+        final ViewInfo firstViewInfo = new ViewInfo(
+                new ComplicationLayoutParams(
+                        100,
+                        100,
+                        ComplicationLayoutParams.POSITION_TOP
+                                | ComplicationLayoutParams.POSITION_END,
+                        ComplicationLayoutParams.DIRECTION_DOWN,
+                        0),
+                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(margin);
+        });
+
+        // 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(margin);
+        });
+
+        // The third view should be in at the top.
+        verifyChange(thirdViewInfo, true, lp -> {
+            assertThat(lp.topToTop == ConstraintLayout.LayoutParams.PARENT_ID).isTrue();
+            assertThat(lp.endToEnd == ConstraintLayout.LayoutParams.PARENT_ID).isTrue();
+            assertThat(lp.getMarginStart()).isEqualTo(0);
+            assertThat(lp.getMarginEnd()).isEqualTo(0);
+            assertThat(lp.topMargin).isEqualTo(0);
+            assertThat(lp.bottomMargin).isEqualTo(0);
+        });
+    }
+
+    /**
      * Ensures layout in a particular position updates.
      */
     @Test
     public void testRemoval() {
-        final ComplicationLayoutEngine engine = new ComplicationLayoutEngine(mLayout);
+        final ComplicationLayoutEngine engine = new ComplicationLayoutEngine(mLayout, 0);
 
         final ViewInfo firstViewInfo = new ViewInfo(
                 new ComplicationLayoutParams(