Merge "Add NPE check for method assignDefaultPhoto in Utils.java"

This commit is contained in:
Lei Yu
2016-12-28 22:28:10 +00:00
committed by Android (Google) Code Review
3 changed files with 23 additions and 4 deletions

View File

@@ -368,10 +368,21 @@ public final class Utils extends com.android.settingslib.Utils {
} catch (IOException ioe) { }
}
public static void assignDefaultPhoto(Context context, int userId) {
/**
* Assign the default photo to user with {@paramref userId}
* @param context used to get the {@link UserManager}
* @param userId used to get the icon bitmap
* @return true if assign photo successfully, false if failed
*/
public static boolean assignDefaultPhoto(Context context, int userId) {
if (context == null) {
return false;
}
UserManager um = (UserManager) context.getSystemService(Context.USER_SERVICE);
Bitmap bitmap = getDefaultUserIconAsBitmap(userId);
um.setUserIcon(userId, bitmap);
return true;
}
public static String getMeProfileName(Context context, boolean full) {

View File

@@ -422,14 +422,16 @@ public class UserSettings extends SettingsPreferenceFragment
private UserInfo createRestrictedProfile() {
UserInfo newUserInfo = mUserManager.createRestrictedProfile(mAddingUserName);
Utils.assignDefaultPhoto(getActivity(), newUserInfo.id);
if (newUserInfo != null && !Utils.assignDefaultPhoto(getActivity(), newUserInfo.id)) {
return null;
}
return newUserInfo;
}
private UserInfo createTrustedUser() {
UserInfo newUserInfo = mUserManager.createUser(mAddingUserName, 0);
if (newUserInfo != null) {
Utils.assignDefaultPhoto(getActivity(), newUserInfo.id);
if (newUserInfo != null && !Utils.assignDefaultPhoto(getActivity(), newUserInfo.id)) {
return null;
}
return newUserInfo;
}

View File

@@ -65,4 +65,10 @@ public class UtilsTest {
assertThat(Utils.getWifiIpAddresses(mContext)).isNull();
}
@Test
public void testAssignDefaultPhoto_ContextNull_ReturnFalseAndNotCrash() {
// Should not crash here
assertThat(Utils.assignDefaultPhoto(null, 0)).isFalse();
}
}