Simplifies show/dismiss logic for the full screen user switcher.

Switcher should only be shown when Cancel is clicked on the bouncer. Otherwise,
the entire keyguard should get dimissed, leading to the bouncer or user's home activity.

Fixes the issue where a keyguard is not dismissed after turning the screen off and back on.

Change-Id: Idd2c8f9f5bf6b490f287b5c5777f39e69437970f
Fixes: 112358232
Test: Manual tests on both multi-user and single-user builds. Repeated boots, switcher and screen off/ons for users with and without pin/password set.
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/car/CarStatusBar.java b/packages/SystemUI/src/com/android/systemui/statusbar/car/CarStatusBar.java
index ed06752..304a00f 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/car/CarStatusBar.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/car/CarStatusBar.java
@@ -495,32 +495,37 @@
     }
 
     @Override
-    public void onUserSwitched(int newUserId) {
-        super.onUserSwitched(newUserId);
-        if (mFullscreenUserSwitcher != null) {
-            mFullscreenUserSwitcher.onUserSwitched(newUserId);
-        }
-    }
-
-    @Override
     public void onStateChanged(int newState) {
         super.onStateChanged(newState);
-        CarUserManagerHelper helper = new CarUserManagerHelper(mContext);
-        if (!helper.isHeadlessSystemUser()) {
-            showUserSwitcher();
+        if (newState == StatusBarState.FULLSCREEN_USER_SWITCHER) {
+            if (!mFullscreenUserSwitcher.isVisible()) {
+                // Current execution path continues to set state after this, thus we deffer the
+                // dismissal to the next execution cycle.
+                postDismissKeyguard(); // Dismiss the keyguard if switcher is not visible.
+            }
+        } else {
+            mFullscreenUserSwitcher.hide();
         }
     }
 
     public void showUserSwitcher() {
-        if (mFullscreenUserSwitcher != null) {
-            if (mState == StatusBarState.FULLSCREEN_USER_SWITCHER) {
-                mFullscreenUserSwitcher.show();
-            } else {
-                mFullscreenUserSwitcher.hide();
-            }
+        if (mFullscreenUserSwitcher != null && mState == StatusBarState.FULLSCREEN_USER_SWITCHER) {
+            mFullscreenUserSwitcher.show(); // Makes the switcher visible.
         }
     }
 
+    public void postDismissKeyguard() {
+        mHandler.post(this::dismissKeyguard);
+    }
+
+    /**
+     * Dismisses the keyguard and shows bouncer if authentication is necessary.
+     */
+    public void dismissKeyguard() {
+        executeRunnableDismissingKeyguard(null/* runnable */, null /* cancelAction */,
+            true /* dismissShade */, true /* afterKeyguardGone */, true /* deferred */);
+    }
+
     @Override
     public void updateMediaMetaData(boolean metaDataChanged, boolean allowEnterAnimation) {
         // Do nothing, we don't want to display media art in the lock screen for a car.
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/car/FullscreenUserSwitcher.java b/packages/SystemUI/src/com/android/systemui/statusbar/car/FullscreenUserSwitcher.java
index 67a76fd..2ebf5eb 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/car/FullscreenUserSwitcher.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/car/FullscreenUserSwitcher.java
@@ -18,7 +18,6 @@
 
 import android.animation.Animator;
 import android.animation.AnimatorListenerAdapter;
-import android.car.user.CarUserManagerHelper;
 import android.content.Context;
 import android.view.View;
 import android.view.ViewStub;
@@ -26,115 +25,87 @@
 import androidx.recyclerview.widget.GridLayoutManager;
 
 import com.android.systemui.R;
-import com.android.systemui.statusbar.phone.StatusBar;
 
 /**
  * Manages the fullscreen user switcher.
  */
 public class FullscreenUserSwitcher {
-    private final View mContainer;
-    private final View mParent;
     private final UserGridRecyclerView mUserGridView;
+    private final View mParent;
     private final int mShortAnimDuration;
-    private final StatusBar mStatusBar;
-    private final CarUserManagerHelper mCarUserManagerHelper;
-    private boolean mShowing;
+    private final CarStatusBar mStatusBar;
 
-    public FullscreenUserSwitcher(StatusBar statusBar, ViewStub containerStub, Context context) {
+    public FullscreenUserSwitcher(CarStatusBar statusBar, ViewStub containerStub, Context context) {
         mStatusBar = statusBar;
         mParent = containerStub.inflate();
-        mContainer = mParent.findViewById(R.id.container);
-        mUserGridView = mContainer.findViewById(R.id.user_grid);
+        mParent.setVisibility(View.VISIBLE);
+        View container = mParent.findViewById(R.id.container);
+
+        // Initialize user grid.
+        mUserGridView = container.findViewById(R.id.user_grid);
         GridLayoutManager layoutManager = new GridLayoutManager(context,
-                context.getResources().getInteger(R.integer.user_fullscreen_switcher_num_col));
+            context.getResources().getInteger(R.integer.user_fullscreen_switcher_num_col));
         mUserGridView.getRecyclerView().setLayoutManager(layoutManager);
         mUserGridView.buildAdapter();
         mUserGridView.setUserSelectionListener(this::onUserSelected);
 
-        mCarUserManagerHelper = new CarUserManagerHelper(context);
+        // Hide the user grid by default. It will only be made visible by clicking on a cancel
+        // button in a bouncer.
+        hide();
 
-        mShortAnimDuration = mContainer.getResources()
+        mShortAnimDuration = container.getResources()
             .getInteger(android.R.integer.config_shortAnimTime);
     }
 
+    /**
+     * Makes user grid visible.
+     */
     public void show() {
-        if (mCarUserManagerHelper.isHeadlessSystemUser()) {
-            showUserGrid();
-        }
-        if (mShowing) {
-            return;
-        }
-        mShowing = true;
-        mParent.setVisibility(View.VISIBLE);
-    }
-
-    public void hide() {
-        mShowing = false;
-        mParent.setVisibility(View.GONE);
-    }
-
-    public void onUserSwitched(int newUserId) {
-        toggleSwitchInProgress(false);
-        mParent.post(this::dismissKeyguard);
-    }
-
-    private void onUserSelected(UserGridRecyclerView.UserRecord record) {
-        if (mCarUserManagerHelper.isHeadlessSystemUser()) {
-            hideUserGrid();
-        }
-
-        if (record.mIsForeground || (record.mIsStartGuestSession
-                && mCarUserManagerHelper.isForegroundUserGuest())) {
-            dismissKeyguard();
-            return;
-        }
-        toggleSwitchInProgress(true);
-    }
-
-    private void showUserGrid() {
         mUserGridView.setVisibility(View.VISIBLE);
     }
 
-    private void hideUserGrid() {
+    /**
+     * Hides the user grid.
+     */
+    public void hide() {
         mUserGridView.setVisibility(View.INVISIBLE);
     }
 
-    // Dismisses the keyguard and shows bouncer if authentication is necessary.
-    private void dismissKeyguard() {
-        mStatusBar.executeRunnableDismissingKeyguard(null/* runnable */, null /* cancelAction */,
-                true /* dismissShade */, true /* afterKeyguardGone */, true /* deferred */);
+    /**
+     * @return {@code true} if user grid is visible, {@code false} otherwise.
+     */
+    public boolean isVisible() {
+        return mUserGridView.getVisibility() == View.VISIBLE;
     }
 
-    private void toggleSwitchInProgress(boolean inProgress) {
-        if (inProgress) {
-            fadeOut(mContainer);
-        } else {
-            fadeIn(mContainer);
+    /**
+     * Every time user clicks on an item in the switcher, we hide the switcher, either
+     * gradually or immediately.
+     *
+     * We dismiss the entire keyguard if user clicked on the foreground user (user we're already
+     * logged in as).
+     */
+    private void onUserSelected(UserGridRecyclerView.UserRecord record) {
+        if (record.mIsForeground) {
+            hide();
+            mStatusBar.dismissKeyguard();
+            return;
         }
+        // Switching is about to happen, since it takes time, fade out the switcher gradually.
+        fadeOut();
     }
 
-    private void fadeOut(View view) {
-        view.animate()
+    private void fadeOut() {
+        mUserGridView.animate()
                 .alpha(0.0f)
                 .setDuration(mShortAnimDuration)
                 .setListener(new AnimatorListenerAdapter() {
                     @Override
                     public void onAnimationEnd(Animator animation) {
-                        view.setVisibility(View.GONE);
+                        hide();
+                        mUserGridView.setAlpha(1.0f);
                     }
                 });
-    }
 
-    private void fadeIn(View view) {
-        view.animate()
-                .alpha(1.0f)
-                .setDuration(mShortAnimDuration)
-                .setListener(new AnimatorListenerAdapter() {
-                    @Override
-                    public void onAnimationStart(Animator animator) {
-                        view.setAlpha(0.0f);
-                        view.setVisibility(View.VISIBLE);
-                    }
-                });
     }
-}
+}
\ No newline at end of file