Force immersive whenever SUW is in progress

Bug: 235525806
Test: manual
Change-Id: I3d7b2c5101877f1417ee3d54093a20b6df4a58a1
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 db82d67..784dfa7 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
@@ -81,6 +81,8 @@
 
 import com.android.internal.app.AssistUtils;
 import com.android.systemui.R;
+import com.android.systemui.car.CarDeviceProvisionedController;
+import com.android.systemui.car.CarDeviceProvisionedListener;
 import com.android.systemui.car.CarServiceProvider;
 import com.android.systemui.qs.QSHost;
 import com.android.systemui.statusbar.CommandQueue;
@@ -131,6 +133,7 @@
     private final CarFullscreenTaskListener mCarFullscreenTaskListener;
     private final ComponentName mControlBarActivityComponent;
     private final CarUiPortraitDisplaySystemBarsController mCarUiDisplaySystemBarsController;
+    private final CarDeviceProvisionedController mCarDeviceProvisionedController;
     private final List<ComponentName> mBackgroundActivityComponent;
     private final HashMap<String, Boolean> mForegroundDAComponentsVisibilityMap;
     private final ArraySet<ComponentName> mIgnoreOpeningForegroundDAComponentsSet;
@@ -174,6 +177,7 @@
     private boolean mIsForegroundDaFullScreen = false;
     private boolean mIsForegroundAppRequestingImmersiveMode = false;
     private boolean mIsUiModeNight = false;
+    private boolean mIsUserSetupInProgress;
     // contains the list of activities that will be displayed on feature {@link
     // CarDisplayAreaOrganizer.FEATURE_VOICE_PLATE)
     private final Set<ComponentName> mVoicePlateActivitySet;
@@ -339,6 +343,14 @@
                 }
             };
 
+    private final CarDeviceProvisionedListener mCarDeviceProvisionedListener =
+            new CarDeviceProvisionedListener() {
+                @Override
+                public void onUserSetupInProgressChanged() {
+                    updateUserSetupState();
+                }
+    };
+
     private void relaunchBackgroundApp() {
         logIfDebuggable("relaunching background app...");
         Intent mapsIntent = new Intent();
@@ -366,7 +378,8 @@
             CarServiceProvider carServiceProvider,
             CarDisplayAreaOrganizer organizer,
             CarUiPortraitDisplaySystemBarsController carUiPortraitDisplaySystemBarsController,
-            CommandQueue commandQueue) {
+            CommandQueue commandQueue,
+            CarDeviceProvisionedController deviceProvisionedController) {
         mApplicationContext = applicationContext;
         mSyncQueue = syncQueue;
         mOrganizer = organizer;
@@ -375,6 +388,7 @@
         mConfigurationController = configurationController;
         mCarServiceProvider = carServiceProvider;
         mCarUiDisplaySystemBarsController = carUiPortraitDisplaySystemBarsController;
+        mCarDeviceProvisionedController = deviceProvisionedController;
         mCarUiDisplaySystemBarsController.registerCallback(mApplicationContext.getDisplayId(),
                 mCarUiPortraitDisplaySystemBarsControllerCallback);
         mUiModeManager = host.getUserContext().getSystemService(UiModeManager.class);
@@ -719,6 +733,9 @@
                 mOnActivityRestartAttemptListener);
         // add CarFullscreenTaskListener to control the foreground DA when the task appears.
         mCarFullscreenTaskListener.registerOnTaskChangeListener(mOnTaskChangeListener);
+
+        updateUserSetupState();
+        mCarDeviceProvisionedController.addCallback(mCarDeviceProvisionedListener);
     }
 
     void updateVoicePlateActivityMap() {
@@ -951,6 +968,7 @@
         mCarDisplayAreaTouchHandler.enable(false);
         ActivityTaskManager.getInstance()
                 .unregisterTaskStackListener(mOnActivityRestartAttemptListener);
+        mCarDeviceProvisionedController.removeCallback(mCarDeviceProvisionedListener);
         mTitleBarView.setVisibility(View.GONE);
     }
 
@@ -961,6 +979,10 @@
      * foreground DA hosting default applications will animate to the default set height.
      */
     public void startAnimation(DisplayAreaComponent.FOREGROUND_DA_STATE toState) {
+        if (mIsUserSetupInProgress) {
+            // No animations while in setup
+            return;
+        }
         // TODO: currently the animations are only bottom/up. Make it more generic animations here.
         int fromPos = 0;
         int toPos = 0;
@@ -1314,4 +1336,26 @@
         LocalBroadcastManager.getInstance(mApplicationContext).sendBroadcast(
                 intent);
     }
+
+    private void updateUserSetupState() {
+        boolean userSetupInProgress = mCarDeviceProvisionedController
+                .isCurrentUserSetupInProgress();
+        if (mIsUserSetupInProgress == userSetupInProgress) {
+            return;
+        }
+        mIsUserSetupInProgress = userSetupInProgress;
+        if (mIsUserSetupInProgress) {
+            if (!isForegroundDaVisible()) {
+                hideTitleBar();
+                makeForegroundDaVisible(true);
+            }
+            setControlBarVisibility(false);
+            immersiveForSUW(true);
+        } else {
+            makeForegroundDaVisible(false);
+            immersiveForSUW(false);
+            showTitleBar();
+            setControlBarVisibility(true);
+        }
+    }
 }
diff --git a/car_product/car_ui_portrait/apps/CarUiPortraitSystemUI/src/com/android/systemui/car/displayarea/CarDisplayAreaModule.java b/car_product/car_ui_portrait/apps/CarUiPortraitSystemUI/src/com/android/systemui/car/displayarea/CarDisplayAreaModule.java
index f81b352..f9753f8 100644
--- a/car_product/car_ui_portrait/apps/CarUiPortraitSystemUI/src/com/android/systemui/car/displayarea/CarDisplayAreaModule.java
+++ b/car_product/car_ui_portrait/apps/CarUiPortraitSystemUI/src/com/android/systemui/car/displayarea/CarDisplayAreaModule.java
@@ -19,6 +19,7 @@
 import android.content.Context;
 import android.os.Handler;
 
+import com.android.systemui.car.CarDeviceProvisionedController;
 import com.android.systemui.car.CarServiceProvider;
 import com.android.systemui.dagger.SysUISingleton;
 import com.android.systemui.qs.QSHost;
@@ -54,10 +55,12 @@
             ConfigurationController configurationController, QSHost host,
             ShellExecutor mainExecutor, CarServiceProvider carServiceProvider,
             CarDisplayAreaOrganizer organizer, CarUiPortraitDisplaySystemBarsController
-            carUiPortraitDisplaySystemBarsController, CommandQueue commandQueue) {
+            carUiPortraitDisplaySystemBarsController, CommandQueue commandQueue,
+            CarDeviceProvisionedController deviceProvisionedController) {
         return new CarDisplayAreaController(context, syncQueue, carFullscreenTaskListener,
                 mainExecutor, configurationController, host, carServiceProvider, organizer,
-                carUiPortraitDisplaySystemBarsController, commandQueue);
+                carUiPortraitDisplaySystemBarsController, commandQueue,
+                deviceProvisionedController);
     }
 
     @Provides
diff --git a/car_product/car_ui_portrait/apps/CarUiPortraitSystemUI/src/com/android/systemui/car/displayarea/DisplayAreaComponent.java b/car_product/car_ui_portrait/apps/CarUiPortraitSystemUI/src/com/android/systemui/car/displayarea/DisplayAreaComponent.java
index 7e317c6..d14287b 100644
--- a/car_product/car_ui_portrait/apps/CarUiPortraitSystemUI/src/com/android/systemui/car/displayarea/DisplayAreaComponent.java
+++ b/car_product/car_ui_portrait/apps/CarUiPortraitSystemUI/src/com/android/systemui/car/displayarea/DisplayAreaComponent.java
@@ -16,9 +16,6 @@
 
 package com.android.systemui.car.displayarea;
 
-import static android.car.settings.CarSettings.Secure.KEY_SETUP_WIZARD_IN_PROGRESS;
-
-import android.app.ActivityManager;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
@@ -26,8 +23,6 @@
 import android.os.Build;
 import android.os.Handler;
 import android.os.Looper;
-import android.os.UserHandle;
-import android.provider.Settings;
 import android.util.Log;
 
 import com.android.systemui.CoreStartable;
@@ -50,49 +45,7 @@
 
     private final CarDisplayAreaController mCarDisplayAreaController;
     private final Context mContext;
-    private boolean mIsDefaultTdaFullScreen;
     final Handler mHandler = new Handler(Looper.myLooper());
-    // When SUW is in progress, make foregroundDA fullscreen.
-    private final Runnable mUserSwitchedRunnable = new Runnable() {
-        @Override
-        public void run() {
-            try {
-                int currentUser = ActivityManager.getCurrentUser();
-                // ignore user 0 -> USER_SYSTEM and USER_ALL for suw
-                if (currentUser != UserHandle.USER_ALL
-                        && currentUser != UserHandle.USER_SYSTEM) {
-                    int res = Settings.Secure.getIntForUser(mContext.getContentResolver(),
-                            KEY_SETUP_WIZARD_IN_PROGRESS, currentUser);
-                    logIfDebuggable("SUW in progress: " + (res == 1));
-                    // res == 1 -> SUW in progress
-                    if (res == 1 && !mIsDefaultTdaFullScreen) {
-                        if (!mCarDisplayAreaController.isForegroundDaVisible()) {
-                            mCarDisplayAreaController.hideTitleBar();
-                            mCarDisplayAreaController.makeForegroundDaVisible(true);
-                        }
-                        mCarDisplayAreaController.setControlBarVisibility(false);
-                        mCarDisplayAreaController.immersiveForSUW(true);
-                        mIsDefaultTdaFullScreen = true;
-                    } else if (res == 0 && mIsDefaultTdaFullScreen) {
-                        // reset
-                        mCarDisplayAreaController.makeForegroundDaVisible(false);
-                        mCarDisplayAreaController.immersiveForSUW(false);
-                        mCarDisplayAreaController.showTitleBar();
-                        mCarDisplayAreaController.setControlBarVisibility(true);
-                        mIsDefaultTdaFullScreen = false;
-                    }
-                }
-            } catch (Exception e) {
-                Log.e(TAG, " error finding SETUP_WIZARD_IN_PROGRESS ", e);
-            } finally {
-                if (mIsDefaultTdaFullScreen) {
-                    // only poll this when default TDA is full screen. We want to check
-                    // the progress of suw every second until the user exits the suw.
-                    mHandler.postDelayed(this, 1000);
-                }
-            }
-        }
-    };
 
     @Inject
     public DisplayAreaComponent(Context context,
@@ -109,36 +62,6 @@
             // Register the DA's
             mCarDisplayAreaController.register();
 
-            IntentFilter filter = new IntentFilter();
-            // add a receiver to listen to ACTION_USER_SWITCHED when user is switching. We would
-            // make the default foreground DA full screen when SUW is being presented to the user.
-            filter.addAction(Intent.ACTION_USER_SWITCHED);
-            // add a receiver to listen to ACTION_BOOT_COMPLETED where we will perform tasks that
-            // require system to be ready. For example, search list of activities with a specific
-            // Intent. This cannot be done while the component is created as that is too early in
-            // the lifecycle of system starting and the results returned by package manager is
-            // not reliable. So we want to wait until system is ready before we query for list of
-            // activities.
-            filter.addAction(Intent.ACTION_BOOT_COMPLETED);
-            mContext.registerReceiverForAllUsers(new BroadcastReceiver() {
-                @Override
-                public void onReceive(Context context, Intent intent) {
-                    if (Intent.ACTION_USER_SWITCHED.equals(intent.getAction())) {
-                        int user = intent.getIntExtra(Intent.EXTRA_USER_HANDLE,
-                                UserHandle.USER_ALL);
-                        logIfDebuggable(
-                                "ACTION_USER_SWITCHED received current user: " + user);
-                        mHandler.post(mUserSwitchedRunnable);
-                        return;
-                    }
-
-                    if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
-                        mCarDisplayAreaController.updateVoicePlateActivityMap();
-                        return;
-                    }
-                }
-            }, filter, /* broadcastPermission= */ null, /* scheduler= */ null);
-
             IntentFilter packageChangeFilter = new IntentFilter();
             // add a receiver to listen to ACTION_PACKAGE_ADDED to perform any action when a new
             // application is installed on the system.
@@ -151,7 +74,6 @@
                     mCarDisplayAreaController.updateVoicePlateActivityMap();
                 }
             }, packageChangeFilter, null, null);
-            mHandler.post(mUserSwitchedRunnable);
         }
     }