Add ambient status bar to hub

Test: atest AmbientStatusBarViewControllerTest
Bug: 319728690
Flag: com.android.systemui.communal_hub
Change-Id: Ic9119f53170ad012026b0d302d189832b481f313
diff --git a/packages/SystemUI/compose/features/src/com/android/systemui/communal/ui/compose/CommunalContainer.kt b/packages/SystemUI/compose/features/src/com/android/systemui/communal/ui/compose/CommunalContainer.kt
index a1f8f1b..412740f 100644
--- a/packages/SystemUI/compose/features/src/com/android/systemui/communal/ui/compose/CommunalContainer.kt
+++ b/packages/SystemUI/compose/features/src/com/android/systemui/communal/ui/compose/CommunalContainer.kt
@@ -68,6 +68,7 @@
         val Grid = ElementKey("CommunalContent")
         val LockIcon = ElementKey("CommunalLockIcon")
         val IndicationArea = ElementKey("CommunalIndicationArea")
+        val StatusBar = ElementKey("StatusBar")
     }
 }
 
@@ -92,6 +93,7 @@
             fade(Communal.Elements.Grid)
             fade(Communal.Elements.IndicationArea)
             fade(Communal.Elements.LockIcon)
+            fade(Communal.Elements.StatusBar)
         }
         timestampRange(startMillis = 167, endMillis = 334) { fade(Communal.Elements.Scrim) }
     }
diff --git a/packages/SystemUI/compose/features/src/com/android/systemui/communal/ui/compose/CommunalContent.kt b/packages/SystemUI/compose/features/src/com/android/systemui/communal/ui/compose/CommunalContent.kt
index 60b6f62..b353b5a 100644
--- a/packages/SystemUI/compose/features/src/com/android/systemui/communal/ui/compose/CommunalContent.kt
+++ b/packages/SystemUI/compose/features/src/com/android/systemui/communal/ui/compose/CommunalContent.kt
@@ -16,13 +16,16 @@
 
 package com.android.systemui.communal.ui.compose
 
+import androidx.compose.foundation.layout.Box
 import androidx.compose.foundation.layout.fillMaxSize
+import androidx.compose.foundation.layout.fillMaxWidth
 import androidx.compose.runtime.Composable
 import androidx.compose.ui.Modifier
 import androidx.compose.ui.layout.Layout
 import androidx.compose.ui.unit.IntRect
 import com.android.compose.animation.scene.SceneScope
 import com.android.compose.theme.LocalAndroidColorScheme
+import com.android.systemui.communal.ui.compose.section.AmbientStatusBarSection
 import com.android.systemui.communal.ui.viewmodel.CommunalViewModel
 import com.android.systemui.communal.widgets.WidgetInteractionHandler
 import com.android.systemui.keyguard.ui.composable.blueprint.BlueprintAlignmentLines
@@ -38,19 +41,24 @@
     private val interactionHandler: WidgetInteractionHandler,
     private val dialogFactory: SystemUIDialogFactory,
     private val lockSection: LockSection,
+    private val ambientStatusBarSection: AmbientStatusBarSection,
 ) {
-
     @Composable
     fun SceneScope.Content(modifier: Modifier = Modifier) {
         Layout(
             modifier = modifier.fillMaxSize(),
             content = {
-                CommunalHub(
-                    viewModel = viewModel,
-                    interactionHandler = interactionHandler,
-                    dialogFactory = dialogFactory,
-                    modifier = Modifier.element(Communal.Elements.Grid)
-                )
+                Box(modifier = Modifier.fillMaxSize()) {
+                    with(ambientStatusBarSection) {
+                        AmbientStatusBar(modifier = Modifier.fillMaxWidth())
+                    }
+                    CommunalHub(
+                        viewModel = viewModel,
+                        interactionHandler = interactionHandler,
+                        dialogFactory = dialogFactory,
+                        modifier = Modifier.element(Communal.Elements.Grid)
+                    )
+                }
                 with(lockSection) {
                     LockIcon(
                         overrideColor = LocalAndroidColorScheme.current.onPrimaryContainer,
diff --git a/packages/SystemUI/compose/features/src/com/android/systemui/communal/ui/compose/section/AmbientStatusBarSection.kt b/packages/SystemUI/compose/features/src/com/android/systemui/communal/ui/compose/section/AmbientStatusBarSection.kt
new file mode 100644
index 0000000..3b335fa
--- /dev/null
+++ b/packages/SystemUI/compose/features/src/com/android/systemui/communal/ui/compose/section/AmbientStatusBarSection.kt
@@ -0,0 +1,55 @@
+/*
+ * Copyright (C) 2024 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package com.android.systemui.communal.ui.compose.section
+
+import android.view.LayoutInflater
+import android.view.View
+import android.widget.FrameLayout
+import androidx.compose.runtime.Composable
+import androidx.compose.ui.Modifier
+import androidx.compose.ui.viewinterop.AndroidView
+import com.android.compose.animation.scene.SceneScope
+import com.android.systemui.ambient.statusbar.dagger.AmbientStatusBarComponent
+import com.android.systemui.ambient.statusbar.ui.AmbientStatusBarView
+import com.android.systemui.communal.ui.compose.Communal
+import com.android.systemui.res.R
+import javax.inject.Inject
+
+class AmbientStatusBarSection
+@Inject
+constructor(
+    private val factory: AmbientStatusBarComponent.Factory,
+) {
+    @Composable
+    fun SceneScope.AmbientStatusBar(modifier: Modifier = Modifier) {
+        AndroidView(
+            factory = { context ->
+                (LayoutInflater.from(context)
+                        .inflate(
+                            /* resource = */ R.layout.ambient_status_bar_view,
+                            /* root = */ FrameLayout(context),
+                            /* attachToRoot = */ false,
+                        ) as AmbientStatusBarView)
+                    .apply {
+                        visibility = View.VISIBLE
+                        factory.create(this).getController().apply { init() }
+                    }
+            },
+            modifier = modifier.element(Communal.Elements.StatusBar)
+        )
+    }
+}
diff --git a/packages/SystemUI/multivalentTests/src/com/android/systemui/ambient/statusbar/ui/AmbientStatusBarViewControllerTest.java b/packages/SystemUI/multivalentTests/src/com/android/systemui/ambient/statusbar/ui/AmbientStatusBarViewControllerTest.java
index d84d151..201ed00 100644
--- a/packages/SystemUI/multivalentTests/src/com/android/systemui/ambient/statusbar/ui/AmbientStatusBarViewControllerTest.java
+++ b/packages/SystemUI/multivalentTests/src/com/android/systemui/ambient/statusbar/ui/AmbientStatusBarViewControllerTest.java
@@ -146,6 +146,7 @@
                 mDreamOverlayStateController,
                 mUserTracker,
                 mKosmos.getWifiInteractor(),
+                mKosmos.getCommunalSceneInteractor(),
                 mLogBuffer);
     }
 
@@ -272,6 +273,7 @@
                 mDreamOverlayStateController,
                 mUserTracker,
                 mKosmos.getWifiInteractor(),
+                mKosmos.getCommunalSceneInteractor(),
                 mLogBuffer);
         controller.onViewAttached();
         verify(mView, never()).showIcon(
diff --git a/packages/SystemUI/src/com/android/systemui/ambient/statusbar/ui/AmbientStatusBarViewController.java b/packages/SystemUI/src/com/android/systemui/ambient/statusbar/ui/AmbientStatusBarViewController.java
index a242d5a..abdc333 100644
--- a/packages/SystemUI/src/com/android/systemui/ambient/statusbar/ui/AmbientStatusBarViewController.java
+++ b/packages/SystemUI/src/com/android/systemui/ambient/statusbar/ui/AmbientStatusBarViewController.java
@@ -30,6 +30,7 @@
 import androidx.annotation.Nullable;
 import androidx.annotation.VisibleForTesting;
 
+import com.android.systemui.communal.domain.interactor.CommunalSceneInteractor;
 import com.android.systemui.dagger.qualifiers.Main;
 import com.android.systemui.dreams.DreamLogger;
 import com.android.systemui.dreams.DreamOverlayNotificationCountProvider;
@@ -82,9 +83,11 @@
     private final Executor mMainExecutor;
     private final List<DreamOverlayStatusBarItemsProvider.StatusBarItem> mExtraStatusBarItems =
             new ArrayList<>();
+    private final CommunalSceneInteractor mCommunalSceneInteractor;
     private final DreamLogger mLogger;
 
     private boolean mIsAttached;
+    private boolean mCommunalVisible;
 
     // Whether dream entry animations are finished.
     private boolean mEntryAnimationsFinished = false;
@@ -140,6 +143,7 @@
             DreamOverlayStateController dreamOverlayStateController,
             UserTracker userTracker,
             WifiInteractor wifiInteractor,
+            CommunalSceneInteractor communalSceneInteractor,
             @DreamLog LogBuffer logBuffer) {
         super(view);
         mResources = resources;
@@ -155,6 +159,7 @@
         mDreamOverlayStateController = dreamOverlayStateController;
         mUserTracker = userTracker;
         mWifiInteractor = wifiInteractor;
+        mCommunalSceneInteractor = communalSceneInteractor;
         mLogger = new DreamLogger(logBuffer, TAG);
 
         // Register to receive show/hide updates for the system status bar. Our custom status bar
@@ -172,6 +177,12 @@
                 network -> updateWifiUnavailableStatusIcon(
                         network instanceof WifiNetworkModel.Active));
 
+        collectFlow(
+                mView,
+                mCommunalSceneInteractor.isCommunalVisible(),
+                this::onCommunalVisibleChanged
+        );
+
         mNextAlarmController.addCallback(mNextAlarmCallback);
         updateAlarmStatusIcon();
 
@@ -230,9 +241,15 @@
         mView.setTranslationY(translationY);
     }
 
+    private void onCommunalVisibleChanged(boolean visible) {
+        mCommunalVisible = visible;
+        updateVisibility();
+    }
+
     private boolean shouldShowStatusBar() {
-        return !mDreamOverlayStateController.isLowLightActive()
-                && !mStatusBarWindowStateController.windowIsShowing();
+        return (!mDreamOverlayStateController.isLowLightActive()
+                && !mStatusBarWindowStateController.windowIsShowing())
+                || mCommunalVisible;
     }
 
     @VisibleForTesting
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardStatusBarViewController.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardStatusBarViewController.java
index e44edcb..cd59d4e 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardStatusBarViewController.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/KeyguardStatusBarViewController.java
@@ -21,6 +21,7 @@
 
 import static com.android.systemui.Flags.updateUserSwitcherBackground;
 import static com.android.systemui.statusbar.StatusBarState.KEYGUARD;
+import static com.android.systemui.util.kotlin.JavaAdapterKt.collectFlow;
 
 import android.content.res.Configuration;
 import android.content.res.Resources;
@@ -46,6 +47,7 @@
 import com.android.keyguard.KeyguardUpdateMonitorCallback;
 import com.android.keyguard.logging.KeyguardLogger;
 import com.android.systemui.battery.BatteryMeterViewController;
+import com.android.systemui.communal.domain.interactor.CommunalSceneInteractor;
 import com.android.systemui.dagger.qualifiers.Background;
 import com.android.systemui.dagger.qualifiers.Main;
 import com.android.systemui.log.core.LogLevel;
@@ -83,6 +85,7 @@
 import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.Executor;
+import java.util.function.Consumer;
 
 import javax.inject.Inject;
 
@@ -128,6 +131,7 @@
     private final Executor mBackgroundExecutor;
     private final Object mLock = new Object();
     private final KeyguardLogger mLogger;
+    private final CommunalSceneInteractor mCommunalSceneInteractor;
 
     private View mSystemIconsContainer;
     private final StatusOverlayHoverListenerFactory mStatusOverlayHoverListenerFactory;
@@ -241,6 +245,12 @@
                 }
             };
 
+    private boolean mCommunalShowing;
+
+    private final Consumer<Boolean> mCommunalConsumer = (communalShowing) -> {
+        mCommunalShowing = communalShowing;
+        updateViewState();
+    };
 
     private final DisableStateTracker mDisableStateTracker;
 
@@ -298,7 +308,8 @@
             @Main Executor mainExecutor,
             @Background Executor backgroundExecutor,
             KeyguardLogger logger,
-            StatusOverlayHoverListenerFactory statusOverlayHoverListenerFactory
+            StatusOverlayHoverListenerFactory statusOverlayHoverListenerFactory,
+            CommunalSceneInteractor communalSceneInteractor
     ) {
         super(view);
         mCarrierTextController = carrierTextController;
@@ -324,6 +335,7 @@
         mMainExecutor = mainExecutor;
         mBackgroundExecutor = backgroundExecutor;
         mLogger = logger;
+        mCommunalSceneInteractor = communalSceneInteractor;
 
         mFirstBypassAttempt = mKeyguardBypassController.getBypassEnabled();
         mKeyguardStateController.addCallback(
@@ -405,6 +417,7 @@
                 UserHandle.USER_ALL);
         updateUserSwitcher();
         onThemeChanged();
+        collectFlow(mView, mCommunalSceneInteractor.isCommunalVisible(), mCommunalConsumer);
     }
 
     @Override
@@ -559,6 +572,7 @@
                         && !mDozing
                         && !hideForBypass
                         && !mDisableStateTracker.isDisabled()
+                        && !mCommunalShowing
                         ? View.VISIBLE : View.INVISIBLE;
 
         updateViewState(newAlpha, newVisibility);
diff --git a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/KeyguardStatusBarViewControllerTest.java b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/KeyguardStatusBarViewControllerTest.java
index f3d6407..53e643e 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/KeyguardStatusBarViewControllerTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/statusbar/phone/KeyguardStatusBarViewControllerTest.java
@@ -223,7 +223,8 @@
                 mFakeExecutor,
                 mBackgroundExecutor,
                 mLogger,
-                mStatusOverlayHoverListenerFactory
+                mStatusOverlayHoverListenerFactory,
+                mKosmos.getCommunalSceneInteractor()
         );
     }
 
diff --git a/packages/SystemUI/tests/utils/src/com/android/systemui/kosmos/KosmosJavaAdapter.kt b/packages/SystemUI/tests/utils/src/com/android/systemui/kosmos/KosmosJavaAdapter.kt
index 45a14ad..67f8443 100644
--- a/packages/SystemUI/tests/utils/src/com/android/systemui/kosmos/KosmosJavaAdapter.kt
+++ b/packages/SystemUI/tests/utils/src/com/android/systemui/kosmos/KosmosJavaAdapter.kt
@@ -29,6 +29,7 @@
 import com.android.systemui.common.ui.domain.interactor.configurationInteractor
 import com.android.systemui.communal.data.repository.fakeCommunalSceneRepository
 import com.android.systemui.communal.domain.interactor.communalInteractor
+import com.android.systemui.communal.domain.interactor.communalSceneInteractor
 import com.android.systemui.communal.ui.viewmodel.communalTransitionViewModel
 import com.android.systemui.concurrency.fakeExecutor
 import com.android.systemui.deviceentry.domain.interactor.deviceEntryInteractor
@@ -113,6 +114,7 @@
     val deviceEntryUdfpsInteractor by lazy { kosmos.deviceEntryUdfpsInteractor }
     val deviceUnlockedInteractor by lazy { kosmos.deviceUnlockedInteractor }
     val communalInteractor by lazy { kosmos.communalInteractor }
+    val communalSceneInteractor by lazy { kosmos.communalSceneInteractor }
     val sceneContainerPlugin by lazy { kosmos.sceneContainerPlugin }
     val deviceProvisioningInteractor by lazy { kosmos.deviceProvisioningInteractor }
     val fakeDeviceProvisioningRepository by lazy { kosmos.fakeDeviceProvisioningRepository }