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
This commit is contained in:
Adam Bookatz
2021-03-31 18:19:58 -07:00
parent 1b82a33048
commit 60a296d290
2 changed files with 15 additions and 4 deletions

View File

@@ -36,7 +36,6 @@ public class UserPreference extends RestrictedPreference {
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 @@ public class UserPreference extends RestrictedPreference {
// 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);

View File

@@ -49,6 +49,7 @@ import android.view.Menu;
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 @@ public class UserSettings extends SettingsPreferenceFragment
}
}
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 @@ public class UserSettings extends SettingsPreferenceFragment
mAddingUser = false;
mPendingUserIcon = null;
mPendingUserName = null;
ThreadUtils.postOnMainThread(() -> hideUserCreatingDialog());
ThreadUtils.postOnMainThread(() -> onUserCreationFailed());
return;
}
@@ -1082,6 +1090,12 @@ public class UserSettings extends SettingsPreferenceFragment
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;
}