Failed guest creation doesn't crash Settings

In the new multiuser settings, there's no logic to handle
what happens when a guest user creation fails, and so Settings
crashed with a NullPointerException.
In the fix, we catch this case to avoid crashing, and display
a toast to the user so that they understand the operation was
not successful.

Bug: 175042193
Test: manually failed to create user and observe toast and no crash
Change-Id: I7dfcaa1eb6d4b792946e18d1632fd2c75e93c168
diff --git a/src/com/android/settings/users/UserPreference.java b/src/com/android/settings/users/UserPreference.java
index 0b78d78..a6427ae 100644
--- a/src/com/android/settings/users/UserPreference.java
+++ b/src/com/android/settings/users/UserPreference.java
@@ -36,7 +36,6 @@
     private static final int ALPHA_DISABLED = 102;
 
     public static final int USERID_UNKNOWN = -10;
-    public static final int USERID_GUEST_DEFAULTS = -11;
     public static final Comparator<UserPreference> SERIAL_NUMBER_COMPARATOR =
             (p1, p2) -> {
 
@@ -93,8 +92,6 @@
             // If the userId is unknown
             if (mUserId == USERID_UNKNOWN) {
                 return Integer.MAX_VALUE;
-            } else if (mUserId == USERID_GUEST_DEFAULTS) {
-                return Integer.MAX_VALUE - 1;
             }
             mSerialNumber = ((UserManager) getContext().getSystemService(Context.USER_SERVICE))
                     .getUserSerialNumber(mUserId);
diff --git a/src/com/android/settings/users/UserSettings.java b/src/com/android/settings/users/UserSettings.java
index 6e82aab..64f9a2c 100644
--- a/src/com/android/settings/users/UserSettings.java
+++ b/src/com/android/settings/users/UserSettings.java
@@ -49,6 +49,7 @@
 import android.view.MenuInflater;
 import android.view.MenuItem;
 import android.widget.SimpleAdapter;
+import android.widget.Toast;
 
 import androidx.annotation.VisibleForTesting;
 import androidx.annotation.WorkerThread;
@@ -493,6 +494,13 @@
         }
     }
 
+    private void onUserCreationFailed() {
+        Toast.makeText(getContext(),
+                com.android.settingslib.R.string.add_user_failed,
+                Toast.LENGTH_SHORT).show();
+        hideUserCreatingDialog();
+    }
+
     private void openUserDetails(UserInfo userInfo, boolean newUser) {
         Bundle extras = new Bundle();
         extras.putInt(UserDetailsSettings.EXTRA_USER_ID, userInfo.id);
@@ -791,7 +799,7 @@
                         mAddingUser = false;
                         mPendingUserIcon = null;
                         mPendingUserName = null;
-                        ThreadUtils.postOnMainThread(() -> hideUserCreatingDialog());
+                        ThreadUtils.postOnMainThread(() -> onUserCreationFailed());
                         return;
                     }
 
@@ -1082,6 +1090,12 @@
             mMetricsFeatureProvider.action(getActivity(), SettingsEnums.ACTION_USER_GUEST_ADD);
             UserInfo guest = mUserManager.createGuest(
                     getContext(), getString(com.android.settingslib.R.string.user_guest));
+            if (guest == null) {
+                Toast.makeText(getContext(),
+                        com.android.settingslib.R.string.add_user_failed,
+                        Toast.LENGTH_SHORT).show();
+                return true;
+            }
             openUserDetails(guest, true);
             return true;
         }