CarUserSwitchingDialog threw null-ref because Guest didn't have
a default avatar.
1. Adds null guard in case default avatar is not set.
2. Populates default Guest avatar correctly.
Fixes:
77546931
Fixes:
78588087
Test: Manual, observing the default icon when starting a guest session.
Manual, observing the switching dialog icon without a default icon.
Change-Id: Iacbfcc65147fdbc5515c0d96eb613fb17543f6b3
return;
}
- if (userInfo.isGuest()) {
- switchToGuest(userInfo.name);
- return;
- }
-
switchToUserId(userInfo.id);
}
/**
- * Creates a guest session and switches into the guest session.
+ * Creates a new guest session and switches into the guest session.
*
* @param guestName Username for the guest user.
*/
- public void switchToGuest(String guestName) {
+ public void startNewGuestSession(String guestName) {
UserInfo guest = mUserManager.createGuest(mContext, guestName);
if (guest == null) {
// Couldn't create user, most likely because there are too many, but we haven't
Log.w(TAG, "can't create user.");
return;
}
+ assignDefaultIcon(guest);
switchToUserId(guest.id);
}
mUserManager.setUserName(user.id, name);
}
+ /**
+ * Gets a bitmap representing the user's default avatar.
+ *
+ * @param userInfo User whose avatar should be returned.
+ * @return Default user icon
+ */
+ public Bitmap getUserDefaultIcon(UserInfo userInfo) {
+ return UserIcons.convertToBitmap(
+ UserIcons.getDefaultUserIcon(mContext.getResources(), userInfo.id, false));
+ }
+
+ /**
+ * Gets a bitmap representing the default icon for a Guest user.
+ *
+ * @return Degault guest icon
+ */
+ public Bitmap getGuestDefaultIcon() {
+ return UserIcons.convertToBitmap(UserIcons.getDefaultUserIcon(
+ mContext.getResources(), UserHandle.USER_NULL, false));
+ }
+
private void registerReceiver() {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_USER_REMOVED);
* @return Bitmap that has been assigned to the user.
*/
private Bitmap assignDefaultIcon(UserInfo userInfo) {
- Bitmap bitmap = UserIcons.convertToBitmap(
- UserIcons.getDefaultUserIcon(mContext.getResources(), userInfo.id, false));
+ Bitmap bitmap = userInfo.isGuest() ? getGuestDefaultIcon() : getUserDefaultIcon(userInfo);
mUserManager.setUserIcon(userInfo.id, bitmap);
return bitmap;
}
}
@Test
- public void switchToGuest() {
- mHelper.switchToGuest("Test Guest");
+ public void startNewGuestSession() {
+ mHelper.startNewGuestSession("Test Guest");
verify(mUserManager).createGuest(mContext, "Test Guest");
UserInfo guestInfo = new UserInfo(21, "Test Guest", UserInfo.FLAG_GUEST);
when(mUserManager.createGuest(mContext, "Test Guest")).thenReturn(guestInfo);
- mHelper.switchToGuest("Test Guest");
+ mHelper.startNewGuestSession("Test Guest");
verify(mActivityManager).switchUser(21);
}
mHelper.switchToUser(createUserInfoForId(20));
assertThat(ShadowActivityManager.getShadow().getSwitchUserCalled()).isFalse();
- // Switching to Guest calls createGuest.
- UserInfo guestInfo = new UserInfo(21, "Test Guest", UserInfo.FLAG_GUEST);
- mHelper.switchToUser(guestInfo);
- verify(mUserManager).createGuest(mContext, "Test Guest");
-
- // Switching to non-current, non-guest user, simply calls switchUser.
+ // Switching to non-foreground user, simply calls switchUser.
UserInfo userToSwitchTo = new UserInfo(22, "Test User", 0);
mHelper.switchToUser(userToSwitchTo);
assertThat(ShadowActivityManager.getShadow().getSwitchUserCalled()).isTrue();
// If the user selects Guest, start the guest session.
if (userRecord.mIsStartGuestSession) {
- mUserManagerHelper.switchToGuest(mGuestName);
+ mUserManagerHelper.startNewGuestSession(mGuestName);
return;
}
private Bitmap getUserRecordIcon(UserRecord userRecord) {
if (userRecord.mIsStartGuestSession) {
- return UserIcons.convertToBitmap(UserIcons.getDefaultUserIcon(
- mContext.getResources(), UserHandle.USER_NULL, false));
+ return mUserManagerHelper.getGuestDefaultIcon();
}
if (userRecord.mIsAddUser) {
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
+import android.os.UserManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
View view = LayoutInflater.from(getContext()).inflate(R.layout.car_user_switching_dialog,
null);
- FileDescriptor fileDescriptor = UserManagerService.getInstance()
- .getUserIcon(mNewUser.id).getFileDescriptor();
- Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor);
- ((ImageView) view.findViewById(R.id.user_loading_avatar))
- .setImageBitmap(bitmap);
+ UserManager userManager =
+ (UserManager) getContext().getSystemService(Context.USER_SERVICE);
+ Bitmap bitmap = userManager.getUserIcon(mNewUser.id);
+ if (bitmap != null) {
+ ((ImageView) view.findViewById(R.id.user_loading_avatar))
+ .setImageBitmap(bitmap);
+ }
+
((TextView) view.findViewById(R.id.user_loading))
.setText(res.getString(R.string.car_loading_profile));
setView(view);