DO NOT MERGE Set CarSysUI unprovisioned state depending on SUW progress. am: d185a842c1

Change-Id: If3c51ad180a84ab656b3d823c7e83d62f09e398c
diff --git a/packages/CarSystemUI/src/com/android/systemui/car/SUWProgressController.java b/packages/CarSystemUI/src/com/android/systemui/car/SUWProgressController.java
new file mode 100644
index 0000000..d952939
--- /dev/null
+++ b/packages/CarSystemUI/src/com/android/systemui/car/SUWProgressController.java
@@ -0,0 +1,129 @@
+/*
+ * Copyright (C) 2019 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;
+
+import android.app.ActivityManager;
+import android.car.settings.CarSettings;
+import android.content.ContentResolver;
+import android.content.Context;
+import android.database.ContentObserver;
+import android.net.Uri;
+import android.provider.Settings;
+
+import com.android.systemui.Dependency;
+
+import java.util.ArrayList;
+
+/**
+ * A controller that monitors the status of SUW progress for each user.
+ */
+public class SUWProgressController {
+    private static final Uri USER_SETUP_IN_PROGRESS_URI = Settings.Secure.getUriFor(
+            CarSettings.Secure.KEY_SETUP_WIZARD_IN_PROGRESS);
+    private final ArrayList<SUWProgressListener> mListeners = new ArrayList<>();
+    private final ContentObserver mCarSettingsObserver = new ContentObserver(
+            Dependency.get(Dependency.MAIN_HANDLER)) {
+        @Override
+        public void onChange(boolean selfChange, Uri uri, int userId) {
+            if (USER_SETUP_IN_PROGRESS_URI.equals(uri)) {
+                notifyUserSetupInProgressChanged();
+            }
+        }
+    };
+    private final ContentResolver mContentResolver;
+
+    public SUWProgressController(Context context) {
+        mContentResolver = context.getContentResolver();
+    }
+
+    /**
+     * Returns {@code true} then SUW is in progress for the given user.
+     */
+    public boolean isUserSetupInProgress(int user) {
+        return Settings.Secure.getIntForUser(mContentResolver,
+                CarSettings.Secure.KEY_SETUP_WIZARD_IN_PROGRESS, /* def= */ 0, user) != 0;
+    }
+
+    /**
+     * Returns {@code true} then SUW is in progress for the current user.
+     */
+    public boolean isCurrentUserSetupInProgress() {
+        return isUserSetupInProgress(ActivityManager.getCurrentUser());
+    }
+
+    /**
+     * Adds a {@link SUWProgressListener} callback.
+     */
+    public void addCallback(SUWProgressListener listener) {
+        mListeners.add(listener);
+        if (mListeners.size() == 1) {
+            startListening(ActivityManager.getCurrentUser());
+        }
+        listener.onUserSetupInProgressChanged();
+    }
+
+    /**
+     * Removes a {@link SUWProgressListener} callback.
+     */
+    public void removeCallback(SUWProgressListener listener) {
+        mListeners.remove(listener);
+        if (mListeners.size() == 0) {
+            stopListening();
+        }
+    }
+
+    private void startListening(int user) {
+        mContentResolver.registerContentObserver(
+                USER_SETUP_IN_PROGRESS_URI, /* notifyForDescendants= */ true, mCarSettingsObserver,
+                user);
+    }
+
+    private void stopListening() {
+        mContentResolver.unregisterContentObserver(mCarSettingsObserver);
+    }
+
+    /**
+     * Allows SUWProgressController to switch its listeners to observe SUW progress for new user.
+     */
+    public void onUserSwitched() {
+        if (mListeners.size() == 0) {
+            return;
+        }
+
+        mContentResolver.unregisterContentObserver(mCarSettingsObserver);
+        mContentResolver.registerContentObserver(
+                USER_SETUP_IN_PROGRESS_URI, /* notifyForDescendants= */ true, mCarSettingsObserver,
+                ActivityManager.getCurrentUser());
+    }
+
+    private void notifyUserSetupInProgressChanged() {
+        for (int i = mListeners.size() - 1; i >= 0; --i) {
+            mListeners.get(i).onUserSetupInProgressChanged();
+        }
+    }
+
+    /**
+     * A listener that listens for changes in SUW progress for a user.
+     */
+    public interface SUWProgressListener {
+        /**
+         * A callback for when a change occurs in SUW progress for a user.
+         */
+        default void onUserSetupInProgressChanged() {
+        }
+    }
+}
diff --git a/packages/CarSystemUI/src/com/android/systemui/statusbar/car/CarStatusBar.java b/packages/CarSystemUI/src/com/android/systemui/statusbar/car/CarStatusBar.java
index ec341460..ef24a41 100644
--- a/packages/CarSystemUI/src/com/android/systemui/statusbar/car/CarStatusBar.java
+++ b/packages/CarSystemUI/src/com/android/systemui/statusbar/car/CarStatusBar.java
@@ -72,6 +72,7 @@
 import com.android.systemui.Prefs;
 import com.android.systemui.R;
 import com.android.systemui.SystemUIFactory;
+import com.android.systemui.car.SUWProgressController;
 import com.android.systemui.classifier.FalsingLog;
 import com.android.systemui.colorextraction.SysuiColorExtractor;
 import com.android.systemui.fragments.FragmentHostManager;
@@ -155,7 +156,9 @@
     private CarFacetButtonController mCarFacetButtonController;
     private ActivityManagerWrapper mActivityManagerWrapper;
     private DeviceProvisionedController mDeviceProvisionedController;
+    private SUWProgressController mSUWProgressController;
     private boolean mDeviceIsSetUpForUser = true;
+    private boolean mIsUserSetupInProgress = false;
     private HvacController mHvacController;
     private DrivingStateHelper mDrivingStateHelper;
     private PowerManagerHelper mPowerManagerHelper;
@@ -273,7 +276,9 @@
         // get the provisioned state before calling the parent class since it's that flow that
         // builds the nav bar
         mDeviceProvisionedController = Dependency.get(DeviceProvisionedController.class);
+        mSUWProgressController = new SUWProgressController(mContext);
         mDeviceIsSetUpForUser = mDeviceProvisionedController.isCurrentUserSetup();
+        mIsUserSetupInProgress = mSUWProgressController.isCurrentUserSetupInProgress();
 
         // Keyboard related setup, before nav bars are created.
         mHideNavBarForKeyboard = mContext.getResources().getBoolean(
@@ -326,6 +331,13 @@
 
         mHvacController.connectToCarService();
 
+        mSUWProgressController.addCallback(
+                new SUWProgressController.SUWProgressListener() {
+                    @Override
+                    public void onUserSetupInProgressChanged() {
+                        mHandler.post(() -> restartNavBarsIfNecessary());
+                    }
+                });
         mDeviceProvisionedController.addCallback(
                 new DeviceProvisionedController.DeviceProvisionedListener() {
                     @Override
@@ -335,6 +347,7 @@
 
                     @Override
                     public void onUserSwitched() {
+                        mSUWProgressController.onUserSwitched();
                         mHandler.post(() -> restartNavBarsIfNecessary());
                     }
                 });
@@ -367,14 +380,6 @@
                 });
     }
 
-    private void restartNavBarsIfNecessary() {
-        boolean currentUserSetup = mDeviceProvisionedController.isCurrentUserSetup();
-        if (mDeviceIsSetUpForUser != currentUserSetup) {
-            mDeviceIsSetUpForUser = currentUserSetup;
-            restartNavBars();
-        }
-    }
-
     @Override
     protected void getDependencies() {
         // Keyguard
@@ -424,6 +429,17 @@
         mContext.unregisterReceiver(mBootCompletedReceiver);
     }
 
+    private void restartNavBarsIfNecessary() {
+        boolean currentUserSetup = mDeviceProvisionedController.isCurrentUserSetup();
+        boolean currentUserSetupInProgress = mSUWProgressController.isCurrentUserSetupInProgress();
+        if (mIsUserSetupInProgress != currentUserSetupInProgress
+                || mDeviceIsSetUpForUser != currentUserSetup) {
+            mDeviceIsSetUpForUser = currentUserSetup;
+            mIsUserSetupInProgress = currentUserSetupInProgress;
+            restartNavBars();
+        }
+    }
+
     /**
      * Remove all content from navbars and rebuild them. Used to allow for different nav bars
      * before and after the device is provisioned. . Also for change of density and font size.
@@ -592,7 +608,7 @@
                 new HandleBarCloseNotificationGestureListener());
 
         mTopNavBarNotificationTouchListener = (v, event) -> {
-            if (!mDeviceIsSetUpForUser) {
+            if (!mDeviceIsSetUpForUser || mIsUserSetupInProgress) {
                 return true;
             }
             boolean consumed = openGestureDetector.onTouchEvent(event);
@@ -932,22 +948,24 @@
     }
 
     private void buildNavBarContent() {
+        boolean shouldBuildNavBarContent = mDeviceIsSetUpForUser && !mIsUserSetupInProgress;
+
         // Always build top bar.
-        buildTopBar((mDeviceIsSetUpForUser) ? R.layout.car_top_navigation_bar :
+        buildTopBar(shouldBuildNavBarContent ? R.layout.car_top_navigation_bar :
                 R.layout.car_top_navigation_bar_unprovisioned);
 
         if (mShowBottom) {
-            buildBottomBar((mDeviceIsSetUpForUser) ? R.layout.car_navigation_bar :
+            buildBottomBar(shouldBuildNavBarContent ? R.layout.car_navigation_bar :
                     R.layout.car_navigation_bar_unprovisioned);
         }
 
         if (mShowLeft) {
-            buildLeft((mDeviceIsSetUpForUser) ? R.layout.car_left_navigation_bar :
+            buildLeft(shouldBuildNavBarContent ? R.layout.car_left_navigation_bar :
                     R.layout.car_left_navigation_bar_unprovisioned);
         }
 
         if (mShowRight) {
-            buildRight((mDeviceIsSetUpForUser) ? R.layout.car_right_navigation_bar :
+            buildRight(shouldBuildNavBarContent ? R.layout.car_right_navigation_bar :
                     R.layout.car_right_navigation_bar_unprovisioned);
         }
     }
@@ -1570,8 +1588,10 @@
 
         @Override
         protected void setHeadsUpVisible() {
-            // if the Notifications panel is showing don't show the Heads up
-            if (!mEnableHeadsUpNotificationWhenNotificationShadeOpen && mPanelExpanded) {
+            // if the Notifications panel is showing or SUW for user is in progress then don't show
+            // heads up notifications
+            if ((!mEnableHeadsUpNotificationWhenNotificationShadeOpen && mPanelExpanded)
+                    || !mDeviceIsSetUpForUser || mIsUserSetupInProgress) {
                 return;
             }