Status Bar - Fix privacy dot being in the wrong corner after fold/unfold

When unfolding and folding, the PrivacyDotViewController will be reused
and #initialize will be called with new views.

For the views to be in the correct position, their gravity has to be
set.
The fix is to make sure the gravity is updated when we have new views.

Fixes: 339335643
Test: Unit tests in this CL
Test: Manually
Flag: com.android.systemui.privacy_dot_unfold_wrong_corner_fix
Change-Id: Id37da91b04cf5fa956f3d6d7f254e9c2634a2563
diff --git a/packages/SystemUI/aconfig/systemui.aconfig b/packages/SystemUI/aconfig/systemui.aconfig
index 2cb297a..723959a 100644
--- a/packages/SystemUI/aconfig/systemui.aconfig
+++ b/packages/SystemUI/aconfig/systemui.aconfig
@@ -984,6 +984,16 @@
 }
 
 flag {
+  namespace: "systemui"
+  name: "privacy_dot_unfold_wrong_corner_fix"
+  description: "Fixes an issue where the privacy dot is at the wrong corner after unfolding/folding."
+  bug: "339335643"
+  metadata {
+    purpose: PURPOSE_BUGFIX
+  }
+}
+
+flag {
   name: "validate_keyboard_shortcut_helper_icon_uri"
   namespace: "systemui"
   description: "Adds a check that the caller can access the content URI of an icon in the shortcut helper."
diff --git a/packages/SystemUI/multivalentTests/src/com/android/systemui/statusbar/events/PrivacyDotViewControllerTest.kt b/packages/SystemUI/multivalentTests/src/com/android/systemui/statusbar/events/PrivacyDotViewControllerTest.kt
index 4340971..f126432 100644
--- a/packages/SystemUI/multivalentTests/src/com/android/systemui/statusbar/events/PrivacyDotViewControllerTest.kt
+++ b/packages/SystemUI/multivalentTests/src/com/android/systemui/statusbar/events/PrivacyDotViewControllerTest.kt
@@ -18,14 +18,19 @@
 
 import android.graphics.Point
 import android.graphics.Rect
+import android.platform.test.annotations.DisableFlags
+import android.platform.test.annotations.EnableFlags
 import android.testing.TestableLooper.RunWithLooper
 import android.view.Display
 import android.view.DisplayAdjustments
 import android.view.View
 import android.widget.FrameLayout
+import android.widget.FrameLayout.LayoutParams.UNSPECIFIED_GRAVITY
 import androidx.test.ext.junit.runners.AndroidJUnit4
 import androidx.test.filters.SmallTest
+import com.android.systemui.Flags
 import com.android.systemui.SysuiTestCase
+import com.android.systemui.res.R
 import com.android.systemui.statusbar.FakeStatusBarStateController
 import com.android.systemui.statusbar.phone.StatusBarContentInsetsProvider
 import com.android.systemui.statusbar.policy.FakeConfigurationController
@@ -291,14 +296,61 @@
         assertThat(controller.currentViewState.designatedCorner).isEqualTo(bottomRightView)
     }
 
+    @Test
+    @EnableFlags(Flags.FLAG_PRIVACY_DOT_UNFOLD_WRONG_CORNER_FIX)
+    fun initialize_newViews_fixFlagEnabled_gravityIsUpdated() {
+        val newTopLeftView = initDotView()
+        val newTopRightView = initDotView()
+        val newBottomLeftView = initDotView()
+        val newBottomRightView = initDotView()
+        setRotation(ROTATION_LANDSCAPE) // Bottom right used in landscape
+
+        val controller = createAndInitializeController()
+        // Re-init with different views, but same rotation
+        controller.initialize(
+            newTopLeftView,
+            newTopRightView,
+            newBottomLeftView,
+            newBottomRightView
+        )
+
+        assertThat((newBottomRightView.layoutParams as FrameLayout.LayoutParams).gravity)
+            .isNotEqualTo(UNSPECIFIED_GRAVITY)
+    }
+
+    @Test
+    @DisableFlags(Flags.FLAG_PRIVACY_DOT_UNFOLD_WRONG_CORNER_FIX)
+    fun initialize_newViews_fixFlagDisabled_gravityIsNotUpdated() {
+        val newTopLeftView = initDotView()
+        val newTopRightView = initDotView()
+        val newBottomLeftView = initDotView()
+        val newBottomRightView = initDotView()
+        setRotation(ROTATION_LANDSCAPE) // Bottom right used in landscape
+
+        val controller = createAndInitializeController()
+        // Re-init with different views, but same rotation
+        controller.initialize(
+            newTopLeftView,
+            newTopRightView,
+            newBottomLeftView,
+            newBottomRightView
+        )
+
+        assertThat((newBottomRightView.layoutParams as FrameLayout.LayoutParams).gravity)
+            .isEqualTo(UNSPECIFIED_GRAVITY)
+    }
+
     private fun setRotation(rotation: Int) {
         whenever(mockDisplay.rotation).thenReturn(rotation)
     }
 
-    private fun initDotView(): View =
-        View(context).also {
+    private fun initDotView(): View {
+        val privacyDot = View(context).also { it.id = R.id.privacy_dot }
+        return FrameLayout(context).also {
             it.layoutParams = FrameLayout.LayoutParams(/* width = */ 0, /* height = */ 0)
+            it.addView(privacyDot)
         }
+    }
 
     private fun enableRtl() {
         configurationController.notifyLayoutDirectionChanged(isRtl = true)
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/events/PrivacyDotViewController.kt b/packages/SystemUI/src/com/android/systemui/statusbar/events/PrivacyDotViewController.kt
index 6586259..9eb9ed5 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/events/PrivacyDotViewController.kt
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/events/PrivacyDotViewController.kt
@@ -26,6 +26,7 @@
 import androidx.core.animation.Animator
 import com.android.app.animation.Interpolators
 import com.android.internal.annotations.GuardedBy
+import com.android.systemui.Flags.privacyDotUnfoldWrongCornerFix
 import com.android.systemui.dagger.SysUISingleton
 import com.android.systemui.dagger.qualifiers.Application
 import com.android.systemui.dagger.qualifiers.Main
@@ -505,7 +506,9 @@
             return
         }
 
-        if (state.rotation != currentViewState.rotation) {
+        val designatedCornerChanged = state.designatedCorner != currentViewState.designatedCorner
+        val rotationChanged = state.rotation != currentViewState.rotation
+        if (rotationChanged || (designatedCornerChanged && privacyDotUnfoldWrongCornerFix())) {
             // A rotation has started, hide the views to avoid flicker
             updateRotations(state.rotation, state.paddingTop)
         }
@@ -515,7 +518,7 @@
             views.forEach { it.requestLayout() }
         }
 
-        if (state.designatedCorner != currentViewState.designatedCorner) {
+        if (designatedCornerChanged) {
             currentViewState.designatedCorner?.contentDescription = null
             state.designatedCorner?.contentDescription = state.contentDescription