[Work profile]  Fix flickering work mode switch

Since there is no system broadcast with Intent.ACTION_MANAGED_PROFILE_ADDED, UserCache's user list does not get updated when a new profile is installed until launcher is restarted. This change fixes that problem by getting the freshest list of users directly from UserManager.

This change also applies the proper padding to work apps recycler-view after work mode switch is attached.

Test: Manual
Bug: 201191227
Change-Id: I0f570eba44ebb845d0d6f8c5f14a63f9a8c18539
diff --git a/src/com/android/launcher3/allapps/AllAppsContainerView.java b/src/com/android/launcher3/allapps/AllAppsContainerView.java
index 3f1f915..9bb4b25 100644
--- a/src/com/android/launcher3/allapps/AllAppsContainerView.java
+++ b/src/com/android/launcher3/allapps/AllAppsContainerView.java
@@ -491,6 +491,7 @@
             mViewPager.initParentViews(this);
             mViewPager.getPageIndicator().setOnActivePageChangedListener(this);
             mWorkManager.attachWorkModeSwitch();
+            mWorkManager.getWorkModeSwitch().post(() -> mAH[AdapterHolder.WORK].applyPadding());
         } else {
             mWorkManager.detachWorkModeSwitch();
             mViewPager = null;
diff --git a/src/com/android/launcher3/allapps/WorkModeSwitch.java b/src/com/android/launcher3/allapps/WorkModeSwitch.java
index 5d64041..be01581 100644
--- a/src/com/android/launcher3/allapps/WorkModeSwitch.java
+++ b/src/com/android/launcher3/allapps/WorkModeSwitch.java
@@ -113,10 +113,12 @@
     /**
      * Sets the enabled or disabled state of the button
      */
-    public void updateCurrentState(boolean active) {
+    public void updateCurrentState(boolean isEnabled) {
         removeFlag(FLAG_PROFILE_TOGGLE_ONGOING);
-        mWorkEnabled = active;
-        updateVisibility();
+        if (mWorkEnabled != isEnabled) {
+            mWorkEnabled = isEnabled;
+            updateVisibility();
+        }
     }
 
 
@@ -125,7 +127,6 @@
         if (mWorkEnabled && mOnWorkTab) {
             setFlag(FLAG_FADE_ONGOING);
             setVisibility(VISIBLE);
-            setAlpha(0);
             animate().alpha(1).withEndAction(() -> removeFlag(FLAG_FADE_ONGOING)).start();
         } else if (getVisibility() != GONE) {
             setFlag(FLAG_FADE_ONGOING);
diff --git a/src/com/android/launcher3/model/UserManagerState.java b/src/com/android/launcher3/model/UserManagerState.java
index 3a4206c..97a5905 100644
--- a/src/com/android/launcher3/model/UserManagerState.java
+++ b/src/com/android/launcher3/model/UserManagerState.java
@@ -36,7 +36,7 @@
      * Initialises the state values for all users
      */
     public void init(UserCache userCache, UserManager userManager) {
-        for (UserHandle user : userCache.getUserProfiles()) {
+        for (UserHandle user : userManager.getUserProfiles()) {
             long serialNo = userCache.getSerialNumberForUser(user);
             boolean isUserQuiet = userManager.isQuietModeEnabled(user);
             allUsers.put(serialNo, user);