Prevent overlap of color views in landscape

Removes overlap from the color views which resulted in subotimal looks
when both color views were translucent and the nav bar was on the right
edge.

Also fixes a bug introduced in I2df7092a91eceeb815367ef917dd7289f4f2b27e
where the navigation-bar-on-right-side case got forgotten and caused
flickering in landscape when IMMERSIVE_STICKY was set but the navigation bar
was visible.

Bug: 22876533
Change-Id: I449a82eb3dc3f7b5051f26b37b362a196b4ff63a
diff --git a/core/java/com/android/internal/policy/PhoneWindow.java b/core/java/com/android/internal/policy/PhoneWindow.java
index dd7ea45..a7bdbe0 100644
--- a/core/java/com/android/internal/policy/PhoneWindow.java
+++ b/core/java/com/android/internal/policy/PhoneWindow.java
@@ -2907,13 +2907,18 @@
                     mLastHasRightStableInset = hasRightStableInset;
                 }
 
-                updateColorViewInt(mStatusColorViewState, sysUiVisibility, mStatusBarColor,
-                        mLastTopInset, false /* matchVertical */, animate && !disallowAnimate);
-
                 boolean navBarToRightEdge = mLastBottomInset == 0 && mLastRightInset > 0;
                 int navBarSize = navBarToRightEdge ? mLastRightInset : mLastBottomInset;
                 updateColorViewInt(mNavigationColorViewState, sysUiVisibility, mNavigationBarColor,
-                        navBarSize, navBarToRightEdge, animate && !disallowAnimate);
+                        navBarSize, navBarToRightEdge, 0 /* rightInset */,
+                        animate && !disallowAnimate);
+
+                boolean statusBarNeedsRightInset = navBarToRightEdge
+                        && mNavigationColorViewState.present;
+                int statusBarRightInset = statusBarNeedsRightInset ? mLastRightInset : 0;
+                updateColorViewInt(mStatusColorViewState, sysUiVisibility, mStatusBarColor,
+                        mLastTopInset, false /* matchVertical */, statusBarRightInset,
+                        animate && !disallowAnimate);
             }
 
             // When we expand the window with FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS, we still need
@@ -2966,15 +2971,17 @@
          * @param size the current size in the non-parent-matching dimension.
          * @param verticalBar if true the view is attached to a vertical edge, otherwise to a
          *                    horizontal edge,
+         * @param rightMargin rightMargin for the color view.
          * @param animate if true, the change will be animated.
          */
         private void updateColorViewInt(final ColorViewState state, int sysUiVis, int color,
-                int size, boolean verticalBar, boolean animate) {
-            boolean show = size > 0 && (sysUiVis & state.systemUiHideFlag) == 0
+                int size, boolean verticalBar, int rightMargin, boolean animate) {
+            state.present = size > 0 && (sysUiVis & state.systemUiHideFlag) == 0
                     && (getAttributes().flags & state.hideWindowFlag) == 0
-                    && (getAttributes().flags & state.translucentFlag) == 0
-                    && (color & Color.BLACK) != 0
                     && (getAttributes().flags & FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) != 0;
+            boolean show = state.present
+                    && (color & Color.BLACK) != 0
+                    && (getAttributes().flags & state.translucentFlag) == 0;
 
             boolean visibilityChanged = false;
             View view = state.view;
@@ -2993,7 +3000,10 @@
                     view.setVisibility(INVISIBLE);
                     state.targetVisibility = VISIBLE;
 
-                    addView(view, new LayoutParams(resolvedWidth, resolvedHeight, resolvedGravity));
+                    LayoutParams lp = new LayoutParams(resolvedWidth, resolvedHeight,
+                            resolvedGravity);
+                    lp.rightMargin = rightMargin;
+                    addView(view, lp);
                     updateColorViewTranslations();
                 }
             } else {
@@ -3003,10 +3013,11 @@
                 if (show) {
                     LayoutParams lp = (LayoutParams) view.getLayoutParams();
                     if (lp.height != resolvedHeight || lp.width != resolvedWidth
-                            || lp.gravity != resolvedGravity) {
+                            || lp.gravity != resolvedGravity || lp.rightMargin != rightMargin) {
                         lp.height = resolvedHeight;
                         lp.width = resolvedWidth;
                         lp.gravity = resolvedGravity;
+                        lp.rightMargin = rightMargin;
                         view.setLayoutParams(lp);
                     }
                     view.setBackgroundColor(color);
@@ -5022,6 +5033,7 @@
     private static class ColorViewState {
         View view = null;
         int targetVisibility = View.INVISIBLE;
+        boolean present = false;
 
         final int id;
         final int systemUiHideFlag;
diff --git a/services/core/java/com/android/server/policy/PhoneWindowManager.java b/services/core/java/com/android/server/policy/PhoneWindowManager.java
index c4ff277..2a79a47 100644
--- a/services/core/java/com/android/server/policy/PhoneWindowManager.java
+++ b/services/core/java/com/android/server/policy/PhoneWindowManager.java
@@ -3617,7 +3617,8 @@
                         // We currently want to hide the navigation UI.
                         mNavigationBarController.setBarShowingLw(false);
                     }
-                    if (navVisible && !navTranslucent && !mNavigationBar.isAnimatingLw()
+                    if (navVisible && !navTranslucent && !navAllowedHidden
+                            && !mNavigationBar.isAnimatingLw()
                             && !mNavigationBarController.wasRecentlyTranslucent()) {
                         // If the nav bar is currently requested to be visible,
                         // and not in the process of animating on or off, then