Update main toggle state in User Settings onResume()

The toggle state used to be set on creation and only changed onSwitchToggled(). But the state can also change when restrictions are applied from outside of settings, or when user is added with toggle being off by default.

This change updates the state during execution of onResume() in Users Settings, which also aligns with the update strategy of other Toggles on this Settings page.

Bug: 362706097
Test: atest MultiUserSwitchBarControllerTest
Flag: EXEMPT bugfix
Change-Id: I8a994b2e0ddb672362e69653374b87f85ae1548c
diff --git a/src/com/android/settings/users/MultiUserSwitchBarController.java b/src/com/android/settings/users/MultiUserSwitchBarController.java
index 07c03d7..1d64141 100644
--- a/src/com/android/settings/users/MultiUserSwitchBarController.java
+++ b/src/com/android/settings/users/MultiUserSwitchBarController.java
@@ -53,6 +53,12 @@
         mSwitchBar = switchBar;
         mListener = listener;
         mUserCapabilities = UserCapabilities.create(context);
+        updateState();
+        mSwitchBar.setListener(this);
+    }
+
+    void updateState() {
+        mUserCapabilities.updateAddUserCapabilities(mContext);
         mSwitchBar.setChecked(mUserCapabilities.mUserSwitcherEnabled);
 
         if (Flags.fixDisablingOfMuToggleWhenRestrictionApplied()) {
@@ -74,7 +80,6 @@
                 mSwitchBar.setEnabled(mUserCapabilities.mIsMain);
             }
         }
-        mSwitchBar.setListener(this);
     }
 
     @Override
@@ -92,7 +97,7 @@
         Log.d(TAG, "Toggling multi-user feature enabled state to: " + isChecked);
         final boolean success = Settings.Global.putInt(mContext.getContentResolver(),
                 Settings.Global.USER_SWITCHER_ENABLED, isChecked ? 1 : 0);
-        if (success && mListener != null) {
+        if (success && mListener != null && !Flags.newMultiuserSettingsUx()) {
             mListener.onMultiUserSwitchChanged(isChecked);
         }
         return success;
diff --git a/src/com/android/settings/users/UserSettings.java b/src/com/android/settings/users/UserSettings.java
index c387d9e..a0137df 100644
--- a/src/com/android/settings/users/UserSettings.java
+++ b/src/com/android/settings/users/UserSettings.java
@@ -419,6 +419,7 @@
                 mTimeoutToDockUserPreferenceController.getPreferenceKey()));
         mRemoveGuestOnExitPreferenceController.updateState(screen.findPreference(
                 mRemoveGuestOnExitPreferenceController.getPreferenceKey()));
+        mSwitchBarController.updateState();
         if (mShouldUpdateUserList) {
             updateUI();
         }