Left align toolbar popup when larger than viewport

If the popup is wider than the viewport, the clamp becomes invalid since
the minimum bound will be greater than the maximum bound. In this case,
the best that can be done is aligning the popup to the leftmost location.

Bug: 380683878
Test: manual
Flag: NONE bugfix
Change-Id: I277376b40de8ad17c006b82f2160a665ccdc38a2
diff --git a/core/java/com/android/internal/widget/floatingtoolbar/LocalFloatingToolbarPopup.java b/core/java/com/android/internal/widget/floatingtoolbar/LocalFloatingToolbarPopup.java
index 38685b6..5177a03 100644
--- a/core/java/com/android/internal/widget/floatingtoolbar/LocalFloatingToolbarPopup.java
+++ b/core/java/com/android/internal/widget/floatingtoolbar/LocalFloatingToolbarPopup.java
@@ -435,10 +435,15 @@
     private void refreshCoordinatesAndOverflowDirection(Rect contentRectOnScreen) {
         refreshViewPort();
 
-        // Initialize x ensuring that the toolbar isn't rendered behind the nav bar in
-        // landscape.
-        final int x = Math.clamp(contentRectOnScreen.centerX() - mPopupWindow.getWidth() / 2,
-                mViewPortOnScreen.left, mViewPortOnScreen.right - mPopupWindow.getWidth());
+        final int x;
+        if (mPopupWindow.getWidth() > mViewPortOnScreen.width()) {
+            // Not enough space - prefer to position as far left as possible
+            x = mViewPortOnScreen.left;
+        } else {
+            // Initialize x ensuring that the toolbar isn't rendered behind the system bar insets
+            x = Math.clamp(contentRectOnScreen.centerX() - mPopupWindow.getWidth() / 2,
+                    mViewPortOnScreen.left, mViewPortOnScreen.right - mPopupWindow.getWidth());
+        }
 
         final int y;