Fix status bar window height not being updated on certain rotations

This was causing status bar icons being truncated.

Certain rotation events don't trigger an application wide configuration
change event. An example is when going from portrait to upside down, or
when going from landscape to seascape.
It triggers a configuration change on the PhoneStatusBarView though.

The status bar window height was being updated on application level
configuration changes. For that reason it was not being updated always.

The fix is to update the height based on configuration change events
from the PhoneStatusBarView instead of the application.

Test: PhoneStatusBarViewTest.kt
Test: CentralSurfacesImplTest.java
Test: Manually
Fixes: 323299264
Flag: ACONFIG com.android.systemui.truncated_status_bar_icons_fix DISABLED

Change-Id: I37dd1b6b446b6975a8aa6ba8d597cd95a990f74c
diff --git a/packages/SystemUI/aconfig/systemui.aconfig b/packages/SystemUI/aconfig/systemui.aconfig
index 2ad7192..6a4f92c 100644
--- a/packages/SystemUI/aconfig/systemui.aconfig
+++ b/packages/SystemUI/aconfig/systemui.aconfig
@@ -227,6 +227,17 @@
 }
 
 flag {
+    name: "truncated_status_bar_icons_fix"
+    namespace: "systemui"
+    description: "Fixes the status bar icons being trunacted due to the status bar window height "
+        "not being updated after certain rotations"
+    bug: "323299264"
+    metadata {
+        purpose: PURPOSE_BUGFIX
+    }
+}
+
+flag {
     name: "compose_bouncer"
     namespace: "systemui"
     description: "Use the new compose bouncer in SystemUI"
diff --git a/packages/SystemUI/src/com/android/systemui/Dependency.java b/packages/SystemUI/src/com/android/systemui/Dependency.java
index 1a34cc4..91157d7 100644
--- a/packages/SystemUI/src/com/android/systemui/Dependency.java
+++ b/packages/SystemUI/src/com/android/systemui/Dependency.java
@@ -59,6 +59,7 @@
 import com.android.systemui.statusbar.policy.BluetoothController;
 import com.android.systemui.statusbar.policy.DeviceProvisionedController;
 import com.android.systemui.statusbar.policy.FlashlightController;
+import com.android.systemui.statusbar.window.StatusBarWindowController;
 import com.android.systemui.tuner.TunablePadding.TunablePaddingService;
 import com.android.systemui.tuner.TunerService;
 
@@ -172,6 +173,7 @@
     @Inject Lazy<SystemUIDialogManager> mSystemUIDialogManagerLazy;
     @Inject Lazy<DialogLaunchAnimator> mDialogLaunchAnimatorLazy;
     @Inject Lazy<UserTracker> mUserTrackerLazy;
+    @Inject Lazy<StatusBarWindowController> mStatusBarWindowControllerLazy;
 
     @Inject
     public Dependency() {
@@ -226,6 +228,7 @@
         mProviders.put(SystemUIDialogManager.class, mSystemUIDialogManagerLazy::get);
         mProviders.put(DialogLaunchAnimator.class, mDialogLaunchAnimatorLazy::get);
         mProviders.put(UserTracker.class, mUserTrackerLazy::get);
+        mProviders.put(StatusBarWindowController.class, mStatusBarWindowControllerLazy::get);
 
         Dependency.setInstance(this);
     }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/CentralSurfacesImpl.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/CentralSurfacesImpl.java
index 2099361..3eb41f8 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/CentralSurfacesImpl.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/CentralSurfacesImpl.java
@@ -29,6 +29,7 @@
 import static com.android.systemui.Dependency.TIME_TICK_HANDLER_NAME;
 import static com.android.systemui.Flags.lightRevealMigration;
 import static com.android.systemui.Flags.predictiveBackSysui;
+import static com.android.systemui.Flags.truncatedStatusBarIconsFix;
 import static com.android.systemui.charging.WirelessChargingAnimation.UNKNOWN_BATTERY_LEVEL;
 import static com.android.systemui.statusbar.NotificationLockscreenUserManager.PERMISSION_SELF;
 import static com.android.systemui.statusbar.StatusBarState.SHADE;
@@ -1905,10 +1906,11 @@
             mQSPanelController.updateResources();
         }
 
-        if (mStatusBarWindowController != null) {
-            mStatusBarWindowController.refreshStatusBarHeight();
+        if (!truncatedStatusBarIconsFix()) {
+            if (mStatusBarWindowController != null) {
+                mStatusBarWindowController.refreshStatusBarHeight();
+            }
         }
-
         if (mShadeSurface != null) {
             mShadeSurface.updateResources();
         }
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarView.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarView.java
index cb7bc25..45bdae8 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarView.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/PhoneStatusBarView.java
@@ -17,6 +17,8 @@
 package com.android.systemui.statusbar.phone;
 
 
+import static com.android.systemui.Flags.truncatedStatusBarIconsFix;
+
 import android.annotation.Nullable;
 import android.content.Context;
 import android.content.res.Configuration;
@@ -41,6 +43,7 @@
 import com.android.systemui.plugins.DarkIconDispatcher.DarkReceiver;
 import com.android.systemui.statusbar.phone.userswitcher.StatusBarUserSwitcherContainer;
 import com.android.systemui.statusbar.policy.Clock;
+import com.android.systemui.statusbar.window.StatusBarWindowController;
 import com.android.systemui.user.ui.binder.StatusBarUserChipViewBinder;
 import com.android.systemui.user.ui.viewmodel.StatusBarUserChipViewModel;
 import com.android.systemui.util.leak.RotationUtils;
@@ -50,6 +53,7 @@
 public class PhoneStatusBarView extends FrameLayout {
     private static final String TAG = "PhoneStatusBarView";
     private final StatusBarContentInsetsProvider mContentInsetsProvider;
+    private final StatusBarWindowController mStatusBarWindowController;
 
     private DarkReceiver mBattery;
     private Clock mClock;
@@ -72,6 +76,7 @@
     public PhoneStatusBarView(Context context, AttributeSet attrs) {
         super(context, attrs);
         mContentInsetsProvider = Dependency.get(StatusBarContentInsetsProvider.class);
+        mStatusBarWindowController = Dependency.get(StatusBarWindowController.class);
     }
 
     void setTouchEventHandler(Gefingerpoken handler) {
@@ -101,6 +106,9 @@
         Dependency.get(DarkIconDispatcher.class).addDarkReceiver(mClock);
         if (updateDisplayParameters()) {
             updateLayoutForCutout();
+            if (truncatedStatusBarIconsFix()) {
+                updateWindowHeight();
+            }
         }
     }
 
@@ -124,6 +132,9 @@
         if (updateDisplayParameters()) {
             updateLayoutForCutout();
             requestLayout();
+            if (truncatedStatusBarIconsFix()) {
+                updateWindowHeight();
+            }
         }
     }
 
@@ -279,4 +290,8 @@
                 insets.right,
                 getPaddingBottom());
     }
+
+    private void updateWindowHeight() {
+        mStatusBarWindowController.refreshStatusBarHeight();
+    }
 }
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/CentralSurfacesImplTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/CentralSurfacesImplTest.java
index b048949..fe29917 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/CentralSurfacesImplTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/CentralSurfacesImplTest.java
@@ -1053,6 +1053,24 @@
         verify(mBubbles).onStatusBarVisibilityChanged(true);
     }
 
+    @Test
+    public void updateResources_flagEnabled_doesNotUpdateStatusBarWindowHeight() {
+        mSetFlagsRule.enableFlags(com.android.systemui.Flags.FLAG_TRUNCATED_STATUS_BAR_ICONS_FIX);
+
+        mCentralSurfaces.updateResources();
+
+        verify(mStatusBarWindowController, never()).refreshStatusBarHeight();
+    }
+
+    @Test
+    public void updateResources_flagDisabled_updatesStatusBarWindowHeight() {
+        mSetFlagsRule.disableFlags(com.android.systemui.Flags.FLAG_TRUNCATED_STATUS_BAR_ICONS_FIX);
+
+        mCentralSurfaces.updateResources();
+
+        verify(mStatusBarWindowController).refreshStatusBarHeight();
+    }
+
     /**
      * Configures the appropriate mocks and then calls {@link CentralSurfacesImpl#updateIsKeyguard}
      * to reconfigure the keyguard to reflect the requested showing/occluded states.
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/PhoneStatusBarViewTest.kt b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/PhoneStatusBarViewTest.kt
index 65d71f8..6eb1c1a 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/PhoneStatusBarViewTest.kt
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/PhoneStatusBarViewTest.kt
@@ -29,16 +29,20 @@
 import android.view.WindowInsets
 import android.widget.FrameLayout
 import androidx.test.filters.SmallTest
+import com.android.systemui.Flags
 import com.android.systemui.Gefingerpoken
 import com.android.systemui.SysuiTestCase
 import com.android.systemui.plugins.DarkIconDispatcher
 import com.android.systemui.res.R
+import com.android.systemui.statusbar.window.StatusBarWindowController
 import com.android.systemui.util.mockito.mock
 import com.android.systemui.util.mockito.whenever
 import com.google.common.truth.Truth.assertThat
 import org.junit.Before
 import org.junit.Test
+import org.mockito.Mockito.never
 import org.mockito.Mockito.spy
+import org.mockito.Mockito.verify
 
 @SmallTest
 @RunWithLooper(setAsMainLooper = true)
@@ -47,6 +51,7 @@
     private lateinit var view: PhoneStatusBarView
 
     private val contentInsetsProvider = mock<StatusBarContentInsetsProvider>()
+    private val windowController = mock<StatusBarWindowController>()
 
     @Before
     fun setUp() {
@@ -55,8 +60,11 @@
             contentInsetsProvider
         )
         mDependency.injectTestDependency(DarkIconDispatcher::class.java, mock<DarkIconDispatcher>())
+        mDependency.injectTestDependency(StatusBarWindowController::class.java, windowController)
         view = spy(createStatusBarView())
         whenever(view.rootWindowInsets).thenReturn(emptyWindowInsets())
+        whenever(contentInsetsProvider.getStatusBarContentInsetsForCurrentRotation())
+            .thenReturn(Insets.NONE)
     }
 
     @Test
@@ -110,6 +118,42 @@
     }
 
     @Test
+    fun onAttachedToWindow_flagEnabled_updatesWindowHeight() {
+        mSetFlagsRule.enableFlags(Flags.FLAG_TRUNCATED_STATUS_BAR_ICONS_FIX)
+
+        view.onAttachedToWindow()
+
+        verify(windowController).refreshStatusBarHeight()
+    }
+
+    @Test
+    fun onAttachedToWindow_flagDisabled_doesNotUpdateWindowHeight() {
+        mSetFlagsRule.disableFlags(Flags.FLAG_TRUNCATED_STATUS_BAR_ICONS_FIX)
+
+        view.onAttachedToWindow()
+
+        verify(windowController, never()).refreshStatusBarHeight()
+    }
+
+    @Test
+    fun onConfigurationChanged_flagEnabled_updatesWindowHeight() {
+        mSetFlagsRule.enableFlags(Flags.FLAG_TRUNCATED_STATUS_BAR_ICONS_FIX)
+
+        view.onConfigurationChanged(Configuration())
+
+        verify(windowController).refreshStatusBarHeight()
+    }
+
+    @Test
+    fun onConfigurationChanged_flagDisabled_doesNotUpdateWindowHeight() {
+        mSetFlagsRule.disableFlags(Flags.FLAG_TRUNCATED_STATUS_BAR_ICONS_FIX)
+
+        view.onConfigurationChanged(Configuration())
+
+        verify(windowController, never()).refreshStatusBarHeight()
+    }
+
+    @Test
     fun onAttachedToWindow_updatesLeftTopRightPaddingsBasedOnInsets() {
         val insets = Insets.of(/* left = */ 10, /* top = */ 20, /* right = */ 30, /* bottom = */ 40)
         whenever(contentInsetsProvider.getStatusBarContentInsetsForCurrentRotation())