Restored CarSetupWizardUiUtils

Fixed disable immersive mode switching to flags for auto and logic to read the color values from the resources when restoring and moved code back to CarSetupWizardUiUtils since there is no more state.
Bug: 153369700
Test: `atest CarSetupWizardUiUtils`

Change-Id: I24f16da9ac2ea8f979ec83e57cf28e7acc005358
Merged-In: I24f16da9ac2ea8f979ec83e57cf28e7acc005358
diff --git a/library/main/src/com/android/car/setupwizardlib/util/CarSetupWizardUiUtils.java b/library/main/src/com/android/car/setupwizardlib/util/CarSetupWizardUiUtils.java
index 0e56893..30540cc 100644
--- a/library/main/src/com/android/car/setupwizardlib/util/CarSetupWizardUiUtils.java
+++ b/library/main/src/com/android/car/setupwizardlib/util/CarSetupWizardUiUtils.java
@@ -17,7 +17,10 @@
 package com.android.car.setupwizardlib.util;
 
 import android.app.Activity;
+import android.content.Context;
+import android.content.res.TypedArray;
 import android.graphics.Color;
+import android.util.Log;
 import android.view.View;
 import android.view.Window;
 
@@ -25,11 +28,7 @@
 
 /**
  * Utilities to aid in UI for car setup wizard flow.
- *
- * @deprecated This class has been deprecated in favour of {@link ImmersiveModeHelper} that offers
- * the same functionalities without the static approach.
  */
-@Deprecated
 public final class CarSetupWizardUiUtils {
     private static final String TAG = CarSetupWizardUiUtils.class.getSimpleName();
 
@@ -52,6 +51,10 @@
      * @param window to apply immersive mode.
      */
     public static void enableImmersiveMode(Window window) {
+        if (Log.isLoggable(TAG, Log.INFO)) {
+            Log.i(TAG, "enableImmersiveMode");
+        }
+
         Preconditions.checkNotNull(window);
 
         // See https://developer.android.com/training/system-ui/immersive#EnableFullscreen
@@ -73,6 +76,45 @@
         window.setStatusBarColor(Color.TRANSPARENT);
     }
 
+    /**
+     * Disables immersive mode hiding system UI and restores the previous colors.
+     *
+     * @param window the current window instance.
+     */
+    public static void disableImmersiveMode(Window window) {
+        if (Log.isLoggable(TAG, Log.INFO)) {
+            Log.i(TAG, "disableImmersiveMode");
+        }
+
+        Preconditions.checkNotNull(window);
+
+        // Restores the decor view flags to disable the immersive mode.
+        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
+
+        // Tries to restore colors for nav and status bar from resources.
+        Context context = window.getContext();
+        if (context == null) {
+            if (Log.isLoggable(TAG, Log.WARN)) {
+                Log.w(TAG, "Can't restore colors for navigation and status bar.");
+            }
+            return;
+        }
+
+        // Reads the colors for navigation and status bar from resources.
+        final TypedArray typedArray =
+                context.obtainStyledAttributes(
+                        new int[]{
+                                android.R.attr.statusBarColor,
+                                android.R.attr.navigationBarColor});
+        int statusBarColor = typedArray.getColor(0, 0);
+        int navigationBarColor = typedArray.getColor(1, 0);
+
+        window.setStatusBarColor(statusBarColor);
+        window.setNavigationBarColor(navigationBarColor);
+
+        typedArray.recycle();
+    }
+
     private CarSetupWizardUiUtils() {
     }
 }
diff --git a/library/main/src/com/android/car/setupwizardlib/util/ImmersiveModeHelper.java b/library/main/src/com/android/car/setupwizardlib/util/ImmersiveModeHelper.java
deleted file mode 100644
index fd6aad3..0000000
--- a/library/main/src/com/android/car/setupwizardlib/util/ImmersiveModeHelper.java
+++ /dev/null
@@ -1,98 +0,0 @@
-/*
- * Copyright (C) 2020 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.car.setupwizardlib.util;
-
-import android.graphics.Color;
-import android.util.Log;
-import android.view.View;
-import android.view.Window;
-
-import androidx.core.util.Preconditions;
-
-import java.lang.ref.WeakReference;
-
-/**
- * Immersive mode helper handles the immersive mode for a given window.
- * Internally it applies the flag specified at
- * https://developer.android.com/training/system-ui/immersive#EnableFullscreen and sets
- * window navigation and status bar colors to transparent when applying immersive mode.
- * Disabling immersive mode restores the previous colors.
- */
-public class ImmersiveModeHelper {
-
-    private static final String TAG = ImmersiveModeHelper.class.getSimpleName();
-
-    private WeakReference<Window> mWindowWeakRef;
-    private int mNavigationBarColor;
-    private int mStatusBarColor;
-
-    /**
-     * Constructor.
-     *
-     * @param window to apply immersive mode to.
-     */
-    public ImmersiveModeHelper(Window window) {
-        Preconditions.checkNotNull(window);
-        this.mWindowWeakRef = new WeakReference<>(window);
-        this.mNavigationBarColor = window.getNavigationBarColor();
-        this.mStatusBarColor = window.getStatusBarColor();
-    }
-
-    /**
-     * Enables immersive mode hiding system UI. This function also sets navigation and status bar
-     * colors as transparent.
-     */
-    public void enable() {
-        Window window = mWindowWeakRef.get();
-        if (window == null) {
-            Log.w(TAG, "Can't enable immersive move. Window reference is lost.");
-            return;
-        }
-
-        window.getDecorView().setSystemUiVisibility(
-                View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
-                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
-                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
-                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
-                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
-                        | View.SYSTEM_UI_FLAG_FULLSCREEN);
-
-        // Workaround for the issue, StatusBar background hides the buttons (b/154227638).
-        window.setNavigationBarColor(Color.TRANSPARENT);
-        window.setStatusBarColor(Color.TRANSPARENT);
-    }
-
-    /**
-     * Disables immersive mode hiding system UI and restores the previous colors.
-     */
-    public void disable() {
-        Window window = mWindowWeakRef.get();
-        if (window == null) {
-            Log.w(TAG, "Can't disable immersive move. Window reference is lost.");
-            return;
-        }
-
-        window.getDecorView().setSystemUiVisibility(
-                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
-                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
-                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
-
-        // Restores the original window colors.
-        window.setNavigationBarColor(mNavigationBarColor);
-        window.setStatusBarColor(mStatusBarColor);
-    }
-}
diff --git a/library/main/tests/robotests/res/values/styles.xml b/library/main/tests/robotests/res/values/styles.xml
new file mode 100644
index 0000000..7b867c7
--- /dev/null
+++ b/library/main/tests/robotests/res/values/styles.xml
@@ -0,0 +1,22 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+  ~ Copyright (C) 2020 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.
+  -->
+<resources>
+    <style name="NavAndStatusBarTestTheme" parent="Theme.AppCompat">
+        <item name="android:statusBarColor">#00000001</item>
+        <item name="android:navigationBarColor">#00000002</item>
+    </style>
+</resources>
diff --git a/library/main/tests/robotests/src/com/android/car/setupwizardlib/util/CarSetupWizardUiUtilsTest.java b/library/main/tests/robotests/src/com/android/car/setupwizardlib/util/CarSetupWizardUiUtilsTest.java
index 2fcf0b6..f931670 100644
--- a/library/main/tests/robotests/src/com/android/car/setupwizardlib/util/CarSetupWizardUiUtilsTest.java
+++ b/library/main/tests/robotests/src/com/android/car/setupwizardlib/util/CarSetupWizardUiUtilsTest.java
@@ -19,9 +19,11 @@
 import static com.google.common.truth.Truth.assertThat;
 
 import android.app.Activity;
+import android.graphics.Color;
 import android.view.View;
+import android.view.Window;
 
-import com.android.car.setupwizardlib.BaseDesignActivity;
+import com.android.car.setupwizardlib.robotests.R;
 
 import org.junit.Before;
 import org.junit.Test;
@@ -40,24 +42,58 @@
                     | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                     | View.SYSTEM_UI_FLAG_FULLSCREEN;
 
+    private static final int NON_IMMERSIVE_MODE_FLAGS =
+            View.SYSTEM_UI_FLAG_VISIBLE;
+
+    private static final int[] RES_ID_NAV_AND_STATUS_BARS = new int[]{
+            android.R.attr.statusBarColor,
+            android.R.attr.navigationBarColor};
+
+    // Note that these colors are defined in the test theme
+    private static final int TEST_THEME = R.style.NavAndStatusBarTestTheme;
+    private static final int EXPECTED_COLOR_STATUS_BAR = Color.getHtmlColor("#001");
+    private static final int EXPECTED_COLOR_NAVIGATION_BAR = Color.getHtmlColor("#002");
+
     private Activity mActivity;
+    private Window mWindow;
 
     @Before
     public void setup() {
-        mActivity = Robolectric.buildActivity(BaseDesignActivity.class).create().get();
+        mActivity = Robolectric
+                .buildActivity(Activity.class)
+                .create()
+                .get();
+        mActivity.setTheme(TEST_THEME);
+        mWindow = mActivity.getWindow();
     }
 
     @Test
     public void maybeHideSystemUI() {
         CarSetupWizardUiUtils.maybeHideSystemUI(mActivity);
-        assertThat(mActivity.getWindow().getDecorView().getSystemUiVisibility())
+        assertThat(mWindow.getDecorView().getSystemUiVisibility())
                 .isEqualTo(IMMERSIVE_MODE_FLAGS);
     }
 
     @Test
     public void enableImmersiveMode() {
-        CarSetupWizardUiUtils.enableImmersiveMode(mActivity.getWindow());
-        assertThat(mActivity.getWindow().getDecorView().getSystemUiVisibility())
+        CarSetupWizardUiUtils.enableImmersiveMode(mWindow);
+        assertThat(mWindow.getDecorView().getSystemUiVisibility())
                 .isEqualTo(IMMERSIVE_MODE_FLAGS);
     }
+
+    @Test
+    public void disableImmersiveMode() {
+        // Resetting the status bar colors.
+        mWindow.setNavigationBarColor(Color.TRANSPARENT);
+        mWindow.setStatusBarColor(Color.TRANSPARENT);
+
+        CarSetupWizardUiUtils.disableImmersiveMode(mWindow);
+
+        assertThat(mWindow.getDecorView().getSystemUiVisibility())
+                .isEqualTo(NON_IMMERSIVE_MODE_FLAGS);
+        assertThat(mWindow.getNavigationBarColor())
+                .isEqualTo(EXPECTED_COLOR_NAVIGATION_BAR);
+        assertThat(mWindow.getStatusBarColor())
+                .isEqualTo(EXPECTED_COLOR_STATUS_BAR);
+    }
 }
diff --git a/library/main/tests/robotests/src/com/android/car/setupwizardlib/util/ImmersiveModeHelperTest.java b/library/main/tests/robotests/src/com/android/car/setupwizardlib/util/ImmersiveModeHelperTest.java
deleted file mode 100644
index fb3a21d..0000000
--- a/library/main/tests/robotests/src/com/android/car/setupwizardlib/util/ImmersiveModeHelperTest.java
+++ /dev/null
@@ -1,98 +0,0 @@
-/*
- * Copyright (C) 2020 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.car.setupwizardlib.util;
-
-import static com.google.common.truth.Truth.assertThat;
-
-import android.graphics.Color;
-import android.view.View;
-import android.view.Window;
-
-import com.android.car.setupwizardlib.BaseDesignActivity;
-
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.robolectric.Robolectric;
-import org.robolectric.RobolectricTestRunner;
-
-@RunWith(RobolectricTestRunner.class)
-public class ImmersiveModeHelperTest {
-
-    private static final int IMMERSIVE_MODE_FLAGS =
-            View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
-                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
-                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
-                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
-                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
-                    | View.SYSTEM_UI_FLAG_FULLSCREEN;
-
-    private static final int NON_IMMERSIVE_MODE_FLAGS =
-            View.SYSTEM_UI_FLAG_LAYOUT_STABLE
-                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
-                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN;
-
-    private Window mWindow;
-    private ImmersiveModeHelper mHelper;
-
-    @Before
-    public void setup() {
-        this.mWindow = Robolectric
-                .buildActivity(BaseDesignActivity.class)
-                .create()
-                .get()
-                .getWindow();
-        this.mHelper = new ImmersiveModeHelper(mWindow);
-    }
-
-    @Test
-    public void enable_shouldSetImmersiveModeFlagsAndWindowBarsColors() {
-        mHelper.enable();
-        assertThat(mWindow.getDecorView().getSystemUiVisibility()).isEqualTo(IMMERSIVE_MODE_FLAGS);
-        assertThat(mWindow.getNavigationBarColor()).isEqualTo(Color.TRANSPARENT);
-        assertThat(mWindow.getStatusBarColor()).isEqualTo(Color.TRANSPARENT);
-    }
-
-    @Test
-    public void disable_shouldSetNonImmersiveModeFlagsAndKeepWindowBarsColors() {
-        int navigationBarColor = mWindow.getNavigationBarColor();
-        int statusBarColor = mWindow.getStatusBarColor();
-
-        mHelper.disable();
-
-        assertThat(mWindow.getDecorView().getSystemUiVisibility())
-                .isEqualTo(NON_IMMERSIVE_MODE_FLAGS);
-        assertThat(mWindow.getNavigationBarColor())
-                .isEqualTo(navigationBarColor);
-        assertThat(mWindow.getStatusBarColor())
-                .isEqualTo(statusBarColor);
-    }
-
-    @Test
-    public void enablingAndDisablingImmersiveMode_shouldPreserveColors() {
-        int navigationBarColor = mWindow.getNavigationBarColor();
-        int statusBarColor = mWindow.getStatusBarColor();
-
-        mHelper.enable();
-        assertThat(mWindow.getNavigationBarColor()).isEqualTo(Color.TRANSPARENT);
-        assertThat(mWindow.getStatusBarColor()).isEqualTo(Color.TRANSPARENT);
-
-        mHelper.disable();
-        assertThat(mWindow.getNavigationBarColor()).isEqualTo(navigationBarColor);
-        assertThat(mWindow.getStatusBarColor()).isEqualTo(statusBarColor);
-    }
-}