Provide more accurate nav button states

Use a custom ButtonSelectionStateListener to account for display area
visibility changes when determining when to highlight buttons in the
navigation bar.

Bug: 233397890
Test: manual
Change-Id: I981e0f50fbf9ac3911c08c668567181d053ca0d3
diff --git a/car_product/car_ui_portrait/apps/CarUiPortraitSystemUI/res/layout/car_bottom_system_bar.xml b/car_product/car_ui_portrait/apps/CarUiPortraitSystemUI/res/layout/car_bottom_system_bar.xml
index 984d302..f3bfc3f 100644
--- a/car_product/car_ui_portrait/apps/CarUiPortraitSystemUI/res/layout/car_bottom_system_bar.xml
+++ b/car_product/car_ui_portrait/apps/CarUiPortraitSystemUI/res/layout/car_bottom_system_bar.xml
@@ -48,25 +48,23 @@
             android:gravity="center"
             android:layoutDirection="ltr">
 
-            <com.android.systemui.car.systembar.CarUiPortraitSystemBarButton
+            <com.android.systemui.car.systembar.CarSystemBarButton
                 android:id="@+id/grid_nav"
                 style="@style/SystemBarButton"
                 android:background="@drawable/nav_bar_app_grid_button_background"
                 systemui:componentNames="com.android.car.carlauncher/.AppGridActivity"
                 systemui:icon="@android:color/transparent"
                 systemui:highlightWhenSelected="true"
-                systemui:toggleSelected="true"
                 systemui:intent="intent:#Intent;action=com.android.car.carlauncher.ACTION_APP_GRID;package=com.android.car.carlauncher;launchFlags=0x24000000;end"
                 systemui:clearBackStack="true"/>
 
-            <com.android.systemui.car.systembar.CarUiPortraitSystemBarButton
+            <com.android.systemui.car.systembar.CarSystemBarButton
                 android:id="@+id/standalone_notifications"
                 style="@style/SystemBarButton"
                 systemui:componentNames="com.android.car.notification/.CarNotificationCenterActivity"
                 systemui:packages="com.android.car.notification"
                 systemui:icon="@drawable/car_ic_notification"
                 systemui:highlightWhenSelected="true"
-                systemui:toggleSelected="true"
                 systemui:intent="intent:#Intent;component=com.android.car.notification/.CarNotificationCenterActivity;launchFlags=0x24000000;end"
                 systemui:longIntent="intent:#Intent;action=com.android.car.bugreport.action.START_AUDIO_FIRST;component=com.android.car.bugreport/.BugReportActivity;end"/>
 
diff --git a/car_product/car_ui_portrait/apps/CarUiPortraitSystemUI/src/com/android/systemui/CarUiPortraitSystemUIBinder.java b/car_product/car_ui_portrait/apps/CarUiPortraitSystemUI/src/com/android/systemui/CarUiPortraitSystemUIBinder.java
index 416266a..9d3e21a 100644
--- a/car_product/car_ui_portrait/apps/CarUiPortraitSystemUI/src/com/android/systemui/CarUiPortraitSystemUIBinder.java
+++ b/car_product/car_ui_portrait/apps/CarUiPortraitSystemUI/src/com/android/systemui/CarUiPortraitSystemUIBinder.java
@@ -18,6 +18,7 @@
 
 import com.android.systemui.car.displayarea.CarDisplayAreaModule;
 import com.android.systemui.car.displayarea.DisplayAreaComponent;
+import com.android.systemui.car.systembar.CarUiPortraitSystemBarModule;
 import com.android.systemui.car.window.ExtendedOverlayWindowModule;
 
 import dagger.Binds;
@@ -26,7 +27,8 @@
 import dagger.multibindings.IntoMap;
 
 /** Binder for AAECarSystemUI specific {@link CoreStartable} modules and components. */
-@Module(includes = {ExtendedOverlayWindowModule.class, CarDisplayAreaModule.class})
+@Module(includes = {ExtendedOverlayWindowModule.class, CarDisplayAreaModule.class,
+        CarUiPortraitSystemBarModule.class})
 abstract class CarUiPortraitSystemUIBinder extends CarSystemUIBinder {
 
     /** Inject into ClusterDisplayController. */
diff --git a/car_product/car_ui_portrait/apps/CarUiPortraitSystemUI/src/com/android/systemui/car/displayarea/CarDisplayAreaController.java b/car_product/car_ui_portrait/apps/CarUiPortraitSystemUI/src/com/android/systemui/car/displayarea/CarDisplayAreaController.java
index f249168..44471e8 100644
--- a/car_product/car_ui_portrait/apps/CarUiPortraitSystemUI/src/com/android/systemui/car/displayarea/CarDisplayAreaController.java
+++ b/car_product/car_ui_portrait/apps/CarUiPortraitSystemUI/src/com/android/systemui/car/displayarea/CarDisplayAreaController.java
@@ -669,10 +669,6 @@
                             animateToControlBarState((int) y,
                                     mScreenHeightWithoutNavBar + mTitleBarHeight, 0);
                             mCarDisplayAreaTouchHandler.updateTitleBarVisibility(false);
-                            Intent intent = new Intent(DISPLAY_AREA_VISIBILITY_CHANGED);
-                            intent.putExtra(INTENT_EXTRA_IS_DISPLAY_AREA_VISIBLE, false);
-                            LocalBroadcastManager.getInstance(mApplicationContext).sendBroadcast(
-                                    intent);
                         } else {
                             animateToDefaultState((int) y,
                                     mScreenHeightWithoutNavBar - mDefaultDisplayHeight
@@ -978,6 +974,7 @@
                 mScreenHeightWithoutNavBar - mControlBarDisplayHeight;
         animate(fromPos, toPos, CONTROL_BAR, durationMs);
         mIsHostingDefaultApplicationDisplayAreaVisible = false;
+        broadcastForegroundDAVisibilityChange(false);
     }
 
     private void animateToDefaultState(int fromPos, int toPos, int durationMs) {
@@ -988,6 +985,7 @@
         mBackgroundApplicationDisplayBounds.bottom = toPos - mTitleBarHeight;
         animate(fromPos, toPos, DEFAULT, durationMs);
         mIsHostingDefaultApplicationDisplayAreaVisible = true;
+        broadcastForegroundDAVisibilityChange(true);
         if (mCarDisplayAreaTouchHandler != null) {
             mCarDisplayAreaTouchHandler.updateTitleBarVisibility(true);
         }
@@ -1277,4 +1275,11 @@
 
         mIsForegroundDaFullScreen = true;
     }
+
+    private void broadcastForegroundDAVisibilityChange(boolean visible) {
+        Intent intent = new Intent(DISPLAY_AREA_VISIBILITY_CHANGED);
+        intent.putExtra(INTENT_EXTRA_IS_DISPLAY_AREA_VISIBLE, visible);
+        LocalBroadcastManager.getInstance(mApplicationContext).sendBroadcast(
+                intent);
+    }
 }
diff --git a/car_product/car_ui_portrait/apps/CarUiPortraitSystemUI/src/com/android/systemui/car/systembar/CarUiPortraitButtonSelectionStateListener.java b/car_product/car_ui_portrait/apps/CarUiPortraitSystemUI/src/com/android/systemui/car/systembar/CarUiPortraitButtonSelectionStateListener.java
new file mode 100644
index 0000000..93dc33c
--- /dev/null
+++ b/car_product/car_ui_portrait/apps/CarUiPortraitSystemUI/src/com/android/systemui/car/systembar/CarUiPortraitButtonSelectionStateListener.java
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2022 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.car.systembar;
+
+import static android.view.Display.DEFAULT_DISPLAY;
+
+import static com.android.systemui.car.displayarea.DisplayAreaComponent.DISPLAY_AREA_VISIBILITY_CHANGED;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.IntentFilter;
+
+import androidx.localbroadcastmanager.content.LocalBroadcastManager;
+
+import com.android.systemui.car.displayarea.CarDisplayAreaController;
+
+class CarUiPortraitButtonSelectionStateListener extends ButtonSelectionStateListener {
+
+    private final CarDisplayAreaController mDisplayAreaController;
+
+    CarUiPortraitButtonSelectionStateListener(Context context,
+            ButtonSelectionStateController carSystemButtonController,
+            CarDisplayAreaController displayAreaController) {
+        super(carSystemButtonController);
+        mDisplayAreaController = displayAreaController;
+
+        BroadcastReceiver displayAreaVisibilityReceiver = new BroadcastReceiver() {
+            @Override
+            public void onReceive(Context context, Intent intent) {
+                onTaskStackChanged();
+            }
+        };
+        LocalBroadcastManager.getInstance(context).registerReceiver(displayAreaVisibilityReceiver,
+                new IntentFilter(DISPLAY_AREA_VISIBILITY_CHANGED));
+    }
+
+    @Override
+    public void onTaskStackChanged() {
+        if (!mDisplayAreaController.isHostingDefaultApplicationDisplayAreaVisible()) {
+            mButtonSelectionStateController.clearAllSelectedButtons(DEFAULT_DISPLAY);
+            return;
+        }
+        super.onTaskStackChanged();
+    }
+}
diff --git a/car_product/car_ui_portrait/apps/CarUiPortraitSystemUI/src/com/android/systemui/car/systembar/CarUiPortraitSystemBarButton.java b/car_product/car_ui_portrait/apps/CarUiPortraitSystemUI/src/com/android/systemui/car/systembar/CarUiPortraitSystemBarButton.java
deleted file mode 100644
index 0bd70f6..0000000
--- a/car_product/car_ui_portrait/apps/CarUiPortraitSystemUI/src/com/android/systemui/car/systembar/CarUiPortraitSystemBarButton.java
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * Copyright (C) 2022 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.car.systembar;
-
-import static com.android.systemui.car.displayarea.DisplayAreaComponent.DISPLAY_AREA_VISIBILITY_CHANGED;
-import static com.android.systemui.car.displayarea.DisplayAreaComponent.INTENT_EXTRA_IS_DISPLAY_AREA_VISIBLE;
-
-import android.content.BroadcastReceiver;
-import android.content.Context;
-import android.content.Intent;
-import android.content.IntentFilter;
-import android.util.AttributeSet;
-
-import androidx.localbroadcastmanager.content.LocalBroadcastManager;
-
-/** A CarSystemBarButton that controls a display area. */
-public class CarUiPortraitSystemBarButton extends CarSystemBarButton {
-
-    public CarUiPortraitSystemBarButton(Context context, AttributeSet attrs) {
-        super(context, attrs);
-        BroadcastReceiver displayAreaVisibilityReceiver = new BroadcastReceiver() {
-            @Override
-            public void onReceive(Context context, Intent intent) {
-                boolean isDefaultDAVisible = intent.getBooleanExtra(
-                        INTENT_EXTRA_IS_DISPLAY_AREA_VISIBLE, true);
-                if (getSelected() && !isDefaultDAVisible) {
-                    context.getMainExecutor().execute(() -> setSelected(/* selected= */ false));
-                }
-                setSelected(isDefaultDAVisible);
-            }
-        };
-        LocalBroadcastManager.getInstance(context).registerReceiver(displayAreaVisibilityReceiver,
-                new IntentFilter(DISPLAY_AREA_VISIBILITY_CHANGED));
-    }
-}
diff --git a/car_product/car_ui_portrait/apps/CarUiPortraitSystemUI/src/com/android/systemui/car/systembar/CarUiPortraitSystemBarModule.java b/car_product/car_ui_portrait/apps/CarUiPortraitSystemUI/src/com/android/systemui/car/systembar/CarUiPortraitSystemBarModule.java
new file mode 100644
index 0000000..e26046d
--- /dev/null
+++ b/car_product/car_ui_portrait/apps/CarUiPortraitSystemUI/src/com/android/systemui/car/systembar/CarUiPortraitSystemBarModule.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 2022 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.car.systembar;
+
+import android.content.Context;
+
+import com.android.systemui.car.dagger.CarSysUIDynamicOverride;
+import com.android.systemui.car.displayarea.CarDisplayAreaController;
+import com.android.systemui.dagger.SysUISingleton;
+
+import dagger.Module;
+import dagger.Provides;
+
+/**
+ * Dagger injection module for {@link CarSystemBar} in CarUiPortraitSystemUI.
+ */
+@Module(includes = {CarSystemBarModule.class})
+public abstract class CarUiPortraitSystemBarModule {
+    @SysUISingleton
+    @Provides
+    @CarSysUIDynamicOverride
+    static ButtonSelectionStateListener provideButtonSelectionStateListener(Context context,
+            ButtonSelectionStateController buttonSelectionStateController,
+            CarDisplayAreaController displayAreaController) {
+        return new CarUiPortraitButtonSelectionStateListener(context,
+                buttonSelectionStateController, displayAreaController);
+    }
+}