Adjust edit state drop targets, workspace scaling, and margins per device type and orientation.

Bug: 202971709
Test: manual
Change-Id: Id1817cd3c7e7bfa5b0aa8aa20565b791ad871f64
diff --git a/src/com/android/launcher3/DropTargetBar.java b/src/com/android/launcher3/DropTargetBar.java
index 9fb14f6..73289fb 100644
--- a/src/com/android/launcher3/DropTargetBar.java
+++ b/src/com/android/launcher3/DropTargetBar.java
@@ -17,8 +17,6 @@
 package com.android.launcher3;
 
 import static com.android.launcher3.ButtonDropTarget.TOOLTIP_DEFAULT;
-import static com.android.launcher3.ButtonDropTarget.TOOLTIP_LEFT;
-import static com.android.launcher3.ButtonDropTarget.TOOLTIP_RIGHT;
 import static com.android.launcher3.anim.AlphaUpdateListener.updateVisibility;
 
 import android.animation.TimeInterpolator;
@@ -41,6 +39,8 @@
 import com.android.launcher3.dragndrop.DragOptions;
 import com.android.launcher3.testing.TestProtocol;
 
+import java.util.Arrays;
+
 /*
  * The top bar containing various drop targets: Delete/App Info/Uninstall.
  */
@@ -94,30 +94,28 @@
         lp.rightMargin = insets.right;
         int tooltipLocation = TOOLTIP_DEFAULT;
 
-        if (grid.isVerticalBarLayout()) {
-            lp.width = grid.dropTargetBarSizePx;
-            lp.height = grid.availableHeightPx - 2 * grid.edgeMarginPx;
-            lp.gravity = grid.isSeascape() ? Gravity.RIGHT : Gravity.LEFT;
-            tooltipLocation = grid.isSeascape() ? TOOLTIP_LEFT : TOOLTIP_RIGHT;
+        int horizontalMargin;
+        if (grid.isTablet) {
+            // XXX: If the icon size changes across orientations, we will have to take
+            //      that into account here too.
+            horizontalMargin = ((grid.widthPx - 2 * grid.edgeMarginPx
+                    - (grid.inv.numColumns * grid.cellWidthPx))
+                    / (2 * (grid.inv.numColumns + 1)))
+                    + grid.edgeMarginPx;
         } else {
-            int gap;
-            if (grid.isTablet) {
-                // XXX: If the icon size changes across orientations, we will have to take
-                //      that into account here too.
-                gap = ((grid.widthPx - 2 * grid.edgeMarginPx
-                        - (grid.inv.numColumns * grid.cellWidthPx))
-                        / (2 * (grid.inv.numColumns + 1)))
-                        + grid.edgeMarginPx;
-            } else {
-                gap = getContext().getResources()
-                        .getDimensionPixelSize(R.dimen.drop_target_bar_margin_horizontal);
-            }
-            lp.width = grid.availableWidthPx - 2 * gap;
-
-            lp.topMargin += grid.edgeMarginPx;
-            lp.height = grid.dropTargetBarSizePx;
-            lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
+            horizontalMargin = getContext().getResources()
+                    .getDimensionPixelSize(R.dimen.drop_target_bar_margin_horizontal);
         }
+        lp.topMargin += grid.dropTargetBarTopMarginPx;
+        lp.bottomMargin += grid.dropTargetBarBottomMarginPx;
+        lp.width = grid.availableWidthPx - 2 * horizontalMargin;
+        if (mIsVertical) {
+            lp.leftMargin = (grid.widthPx - lp.width) / 2;
+            lp.rightMargin = (grid.widthPx - lp.width) / 2;
+        }
+        lp.height = grid.dropTargetBarSizePx;
+        lp.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
+
         setLayoutParams(lp);
         for (ButtonDropTarget button : mDropTargets) {
             button.setTextSize(TypedValue.COMPLEX_UNIT_PX, grid.dropTargetTextSizePx);
@@ -139,19 +137,7 @@
         int height = MeasureSpec.getSize(heightMeasureSpec);
 
         int visibleCount = getVisibleButtonsCount();
-        if (visibleCount == 0) {
-            // do nothing
-        } else if (mIsVertical) {
-            int widthSpec = MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY);
-            int heightSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.AT_MOST);
-
-            for (ButtonDropTarget button : mDropTargets) {
-                if (button.getVisibility() != GONE) {
-                    button.setTextVisible(false);
-                    button.measure(widthSpec, heightSpec);
-                }
-            }
-        } else {
+        if (visibleCount > 0) {
             int availableWidth = width / visibleCount;
             boolean textVisible = true;
             for (ButtonDropTarget buttons : mDropTargets) {
@@ -176,31 +162,91 @@
     protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
         int visibleCount = getVisibleButtonsCount();
         if (visibleCount == 0) {
-            // do nothing
-        } else if (mIsVertical) {
-            int gap = getResources().getDimensionPixelSize(R.dimen.drop_target_vertical_gap);
-            int start = gap;
-            int end;
+            return;
+        }
 
-            for (ButtonDropTarget button : mDropTargets) {
-                if (button.getVisibility() != GONE) {
-                    end = start + button.getMeasuredHeight();
-                    button.layout(0, start, button.getMeasuredWidth(), end);
-                    start = end + gap;
-                }
-            }
-        } else {
-            int frameSize = (right - left) / visibleCount;
+        Launcher launcher = Launcher.getLauncher(getContext());
+        Workspace workspace = launcher.getWorkspace();
+        DeviceProfile dp = launcher.getDeviceProfile();
+        int buttonHorizontalPadding = dp.dropTargetHorizontalPaddingPx;
+        int buttonVerticalPadding = dp.dropTargetVerticalPaddingPx;
+        int barCenter = (right - left) / 2;
 
-            int start = frameSize / 2;
-            int halfWidth;
-            for (ButtonDropTarget button : mDropTargets) {
-                if (button.getVisibility() != GONE) {
-                    halfWidth = button.getMeasuredWidth() / 2;
-                    button.layout(start - halfWidth, 0,
-                            start + halfWidth, button.getMeasuredHeight());
-                    start = start + frameSize;
+        ButtonDropTarget[] visibleButtons = Arrays.stream(mDropTargets)
+                .filter(b -> b.getVisibility() != GONE)
+                .toArray(ButtonDropTarget[]::new);
+        Arrays.stream(visibleButtons).forEach(
+                b -> b.setPadding(buttonHorizontalPadding, buttonVerticalPadding,
+                        buttonHorizontalPadding, buttonVerticalPadding));
+
+        if (visibleCount == 1) {
+            ButtonDropTarget button = visibleButtons[0];
+            button.layout(barCenter - (button.getMeasuredWidth() / 2), 0,
+                    barCenter + (button.getMeasuredWidth() / 2), button.getMeasuredHeight());
+        } else if (visibleCount == 2) {
+            int buttonGap = dp.dropTargetGapPx;
+
+            if (dp.isTwoPanels) {
+                ButtonDropTarget leftButton = visibleButtons[0];
+                leftButton.layout(barCenter - leftButton.getMeasuredWidth() - (buttonGap / 2), 0,
+                        barCenter - (buttonGap / 2), leftButton.getMeasuredHeight());
+
+                ButtonDropTarget rightButton = visibleButtons[1];
+                rightButton.layout(barCenter + (buttonGap / 2), 0,
+                        barCenter + rightButton.getMeasuredWidth() + (buttonGap / 2),
+                        rightButton.getMeasuredHeight());
+            } else if (dp.isTablet) {
+                int numberOfMargins = visibleCount - 1;
+                int buttonWidths = Arrays.stream(mDropTargets)
+                        .filter(b -> b.getVisibility() != GONE)
+                        .mapToInt(ButtonDropTarget::getMeasuredWidth)
+                        .sum();
+                int totalWidth = buttonWidths + (numberOfMargins * buttonGap);
+                int buttonsStartMargin = barCenter - (totalWidth / 2);
+
+                int start = buttonsStartMargin;
+                for (ButtonDropTarget button : visibleButtons) {
+                    int margin = (start != buttonsStartMargin) ? buttonGap : 0;
+                    button.layout(start + margin, 0, start + margin + button.getMeasuredWidth(),
+                            button.getMeasuredHeight());
+                    start += button.getMeasuredWidth() + margin;
                 }
+            } else if (mIsVertical) {
+                // Center buttons over workspace, not screen.
+                int verticalCenter = (workspace.getRight() - workspace.getLeft()) / 2;
+                ButtonDropTarget leftButton = visibleButtons[0];
+                leftButton.layout(verticalCenter - leftButton.getMeasuredWidth() - (buttonGap / 2),
+                        0, verticalCenter - (buttonGap / 2), leftButton.getMeasuredHeight());
+
+                ButtonDropTarget rightButton = visibleButtons[1];
+                rightButton.layout(verticalCenter + (buttonGap / 2), 0,
+                        verticalCenter + rightButton.getMeasuredWidth() + (buttonGap / 2),
+                        rightButton.getMeasuredHeight());
+            } else if (dp.isPhone) {
+                // Buttons aligned to outer edges of scaled workspace.
+                float shrunkTop = dp.getWorkspaceSpringLoadShrunkTop();
+                float shrunkBottom = dp.getWorkspaceSpringLoadShrunkBottom();
+                float scale =
+                        (shrunkBottom - shrunkTop) / launcher.getWorkspace().getNormalChildHeight();
+                int workspaceWidth = (int) (launcher.getWorkspace().getNormalChildWidth() * scale);
+                int start = barCenter - (workspaceWidth / 2);
+                int end = barCenter + (workspaceWidth / 2);
+
+                ButtonDropTarget leftButton = visibleButtons[0];
+                ButtonDropTarget rightButton = visibleButtons[1];
+
+                // If the text within the buttons is too long, the buttons can overlap
+                int overlap = start + leftButton.getMeasuredWidth() + rightButton.getMeasuredWidth()
+                        - end;
+                if (overlap > 0) {
+                    start -= overlap / 2;
+                    end += overlap / 2;
+                }
+
+                leftButton.layout(start, 0, start + leftButton.getMeasuredWidth(),
+                        leftButton.getMeasuredHeight());
+                rightButton.layout(end - rightButton.getMeasuredWidth(), 0, end,
+                        rightButton.getMeasuredHeight());
             }
         }
     }