Refactor Add new user / restricted profile UX flow.

Incorporate the choose user name / avatar into the
user creation flow so that we don't end up with many
"New User"s.

Bug: 147653252
Test: make -j64 RunSettingsRoboTests ROBOTEST_FILTER="com.android.settings.users.EditUserInfoControllerTest"
Change-Id: Ie19230791d8b50c8ab04df89909606179364ebab
This commit is contained in:
Ben Murdoch
2020-01-15 13:10:35 +00:00
parent 1a3e286d0c
commit 34462c0cb0
4 changed files with 351 additions and 107 deletions

View File

@@ -20,10 +20,8 @@ import android.app.Activity;
import android.app.Dialog; import android.app.Dialog;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.pm.UserInfo;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle; import android.os.Bundle;
import android.os.UserHandle; import android.os.UserHandle;
import android.os.UserManager; import android.os.UserManager;
@@ -39,7 +37,6 @@ import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import com.android.settings.R; import com.android.settings.R;
import com.android.settingslib.Utils;
import com.android.settingslib.drawable.CircleFramedDrawable; import com.android.settingslib.drawable.CircleFramedDrawable;
import java.io.File; import java.io.File;
@@ -59,9 +56,24 @@ public class EditUserInfoController {
private UserManager mUserManager; private UserManager mUserManager;
private boolean mWaitingForActivityResult = false; private boolean mWaitingForActivityResult = false;
/**
* Callback made when either the username text or photo choice changes.
*/
public interface OnContentChangedCallback { public interface OnContentChangedCallback {
public void onPhotoChanged(Drawable photo); /** Photo updated. */
public void onLabelChanged(CharSequence label); void onPhotoChanged(UserHandle user, Drawable photo);
/** Username updated. */
void onLabelChanged(UserHandle user, CharSequence label);
}
/**
* Callback made when the dialog finishes.
*/
public interface OnDialogCompleteCallback {
/** Dialog closed with positive button. */
void onPositive();
/** Dialog closed with negative button or cancelled. */
void onNegativeOrCancel();
} }
public void clear() { public void clear() {
@@ -111,7 +123,8 @@ public class EditUserInfoController {
public Dialog createDialog(final Fragment fragment, final Drawable currentUserIcon, public Dialog createDialog(final Fragment fragment, final Drawable currentUserIcon,
final CharSequence currentUserName, final CharSequence currentUserName,
int titleResId, final OnContentChangedCallback callback, UserHandle user) { String title, final OnContentChangedCallback callback, UserHandle user,
OnDialogCompleteCallback completeCallback) {
Activity activity = fragment.getActivity(); Activity activity = fragment.getActivity();
mUser = user; mUser = user;
if (mUserManager == null) { if (mUserManager == null) {
@@ -120,10 +133,8 @@ public class EditUserInfoController {
LayoutInflater inflater = activity.getLayoutInflater(); LayoutInflater inflater = activity.getLayoutInflater();
View content = inflater.inflate(R.layout.edit_user_info_dialog_content, null); View content = inflater.inflate(R.layout.edit_user_info_dialog_content, null);
UserInfo info = mUserManager.getUserInfo(mUser.getIdentifier());
final EditText userNameView = (EditText) content.findViewById(R.id.user_name); final EditText userNameView = (EditText) content.findViewById(R.id.user_name);
userNameView.setText(info.name); userNameView.setText(currentUserName);
final ImageView userPhotoView = (ImageView) content.findViewById(R.id.user_photo); final ImageView userPhotoView = (ImageView) content.findViewById(R.id.user_photo);
Drawable drawable = null; Drawable drawable = null;
@@ -131,14 +142,11 @@ public class EditUserInfoController {
drawable = CircleFramedDrawable.getInstance(activity, mSavedPhoto); drawable = CircleFramedDrawable.getInstance(activity, mSavedPhoto);
} else { } else {
drawable = currentUserIcon; drawable = currentUserIcon;
if (drawable == null) {
drawable = Utils.getUserIcon(activity, mUserManager, info);
}
} }
userPhotoView.setImageDrawable(drawable); userPhotoView.setImageDrawable(drawable);
mEditUserPhotoController = createEditUserPhotoController(fragment, userPhotoView, drawable); mEditUserPhotoController = createEditUserPhotoController(fragment, userPhotoView, drawable);
mEditUserInfoDialog = new AlertDialog.Builder(activity) mEditUserInfoDialog = new AlertDialog.Builder(activity)
.setTitle(R.string.profile_info_settings_title) .setTitle(title)
.setView(content) .setView(content)
.setCancelable(true) .setCancelable(true)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@@ -149,41 +157,45 @@ public class EditUserInfoController {
CharSequence userName = userNameView.getText(); CharSequence userName = userNameView.getText();
if (!TextUtils.isEmpty(userName)) { if (!TextUtils.isEmpty(userName)) {
if (currentUserName == null if (currentUserName == null
|| !userName.toString().equals(currentUserName.toString())) { || !userName.toString().equals(
currentUserName.toString())) {
if (callback != null) { if (callback != null) {
callback.onLabelChanged(userName.toString()); callback.onLabelChanged(mUser, userName.toString());
} }
mUserManager.setUserName(mUser.getIdentifier(),
userName.toString());
} }
} }
// Update the photo if changed. // Update the photo if changed.
Drawable drawable = mEditUserPhotoController.getNewUserPhotoDrawable(); Drawable drawable = mEditUserPhotoController.getNewUserPhotoDrawable();
Bitmap bitmap = mEditUserPhotoController.getNewUserPhotoBitmap(); if (drawable != null && !drawable.equals(currentUserIcon)) {
if (drawable != null && bitmap != null
&& !drawable.equals(currentUserIcon)) {
if (callback != null) { if (callback != null) {
callback.onPhotoChanged(drawable); callback.onPhotoChanged(mUser, drawable);
} }
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
mUserManager.setUserIcon(mUser.getIdentifier(),
mEditUserPhotoController.getNewUserPhotoBitmap());
return null;
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[]) null);
} }
fragment.getActivity().removeDialog( fragment.getActivity().removeDialog(
RestrictedProfileSettings.DIALOG_ID_EDIT_USER_INFO); RestrictedProfileSettings.DIALOG_ID_EDIT_USER_INFO);
} }
clear(); clear();
if (completeCallback != null) {
completeCallback.onPositive();
}
} }
}) })
.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
clear(); clear();
if (completeCallback != null) {
completeCallback.onNegativeOrCancel();
}
}
})
.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
clear();
if (completeCallback != null) {
completeCallback.onNegativeOrCancel();
}
} }
}) })
.create(); .create();

View File

@@ -23,12 +23,15 @@ import android.content.Intent;
import android.content.pm.UserInfo; import android.content.pm.UserInfo;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.os.Bundle; import android.os.Bundle;
import android.os.UserHandle;
import android.view.View; import android.view.View;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import com.android.internal.util.UserIcons;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.Utils; import com.android.settings.Utils;
import com.android.settingslib.utils.ThreadUtils;
public class RestrictedProfileSettings extends AppRestrictionsFragment public class RestrictedProfileSettings extends AppRestrictionsFragment
implements EditUserInfoController.OnContentChangedCallback { implements EditUserInfoController.OnContentChangedCallback {
@@ -117,8 +120,8 @@ public class RestrictedProfileSettings extends AppRestrictionsFragment
public Dialog onCreateDialog(int dialogId) { public Dialog onCreateDialog(int dialogId) {
if (dialogId == DIALOG_ID_EDIT_USER_INFO) { if (dialogId == DIALOG_ID_EDIT_USER_INFO) {
return mEditUserInfoController.createDialog(this, mUserIconView.getDrawable(), return mEditUserInfoController.createDialog(this, mUserIconView.getDrawable(),
mUserNameView.getText(), R.string.profile_info_settings_title, mUserNameView.getText(), getString(R.string.profile_info_settings_title),
this, mUser); this, mUser, null);
} else if (dialogId == DIALOG_CONFIRM_REMOVE) { } else if (dialogId == DIALOG_CONFIRM_REMOVE) {
Dialog dlg = Dialog dlg =
UserDialogs.createRemoveDialog(getActivity(), mUser.getIdentifier(), UserDialogs.createRemoveDialog(getActivity(), mUser.getIdentifier(),
@@ -156,12 +159,19 @@ public class RestrictedProfileSettings extends AppRestrictionsFragment
} }
@Override @Override
public void onPhotoChanged(Drawable photo) { public void onPhotoChanged(UserHandle user, Drawable photo) {
mUserIconView.setImageDrawable(photo); mUserIconView.setImageDrawable(photo);
ThreadUtils.postOnBackgroundThread(new Runnable() {
@Override
public void run() {
mUserManager.setUserIcon(user.getIdentifier(), UserIcons.convertToBitmap(photo));
}
});
} }
@Override @Override
public void onLabelChanged(CharSequence label) { public void onLabelChanged(UserHandle user, CharSequence label) {
mUserNameView.setText(label); mUserNameView.setText(label);
mUserManager.setUserName(user.getIdentifier(), label.toString());
} }
} }

View File

@@ -16,6 +16,8 @@
package com.android.settings.users; package com.android.settings.users;
import static android.os.Process.myUserHandle;
import android.app.Activity; import android.app.Activity;
import android.app.ActivityManager; import android.app.ActivityManager;
import android.app.Dialog; import android.app.Dialog;
@@ -73,6 +75,7 @@ import com.android.settingslib.RestrictedLockUtilsInternal;
import com.android.settingslib.RestrictedPreference; import com.android.settingslib.RestrictedPreference;
import com.android.settingslib.drawable.CircleFramedDrawable; import com.android.settingslib.drawable.CircleFramedDrawable;
import com.android.settingslib.search.SearchIndexable; import com.android.settingslib.search.SearchIndexable;
import com.android.settingslib.utils.ThreadUtils;
import com.google.android.setupcompat.util.WizardManagerHelper; import com.google.android.setupcompat.util.WizardManagerHelper;
@@ -82,6 +85,7 @@ import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Random;
/** /**
* Screen that manages the list of users on the device. * Screen that manages the list of users on the device.
@@ -95,8 +99,7 @@ import java.util.List;
public class UserSettings extends SettingsPreferenceFragment public class UserSettings extends SettingsPreferenceFragment
implements Preference.OnPreferenceClickListener, View.OnClickListener, implements Preference.OnPreferenceClickListener, View.OnClickListener,
MultiUserSwitchBarController.OnMultiUserSwitchChangedListener, MultiUserSwitchBarController.OnMultiUserSwitchChangedListener,
DialogInterface.OnDismissListener, DialogInterface.OnDismissListener {
EditUserInfoController.OnContentChangedCallback {
private static final String TAG = "UserSettings"; private static final String TAG = "UserSettings";
@@ -125,6 +128,8 @@ public class UserSettings extends SettingsPreferenceFragment
private static final int DIALOG_NEED_LOCKSCREEN = 7; private static final int DIALOG_NEED_LOCKSCREEN = 7;
private static final int DIALOG_CONFIRM_EXIT_GUEST = 8; private static final int DIALOG_CONFIRM_EXIT_GUEST = 8;
private static final int DIALOG_USER_PROFILE_EDITOR = 9; private static final int DIALOG_USER_PROFILE_EDITOR = 9;
private static final int DIALOG_USER_PROFILE_EDITOR_ADD_USER = 10;
private static final int DIALOG_USER_PROFILE_EDITOR_ADD_RESTRICTED_PROFILE = 11;
private static final int MESSAGE_UPDATE_LIST = 1; private static final int MESSAGE_UPDATE_LIST = 1;
private static final int MESSAGE_SETUP_USER = 2; private static final int MESSAGE_SETUP_USER = 2;
@@ -168,6 +173,9 @@ public class UserSettings extends SettingsPreferenceFragment
private AddUserWhenLockedPreferenceController mAddUserWhenLockedPreferenceController; private AddUserWhenLockedPreferenceController mAddUserWhenLockedPreferenceController;
private MultiUserFooterPreferenceController mMultiUserFooterPreferenceController; private MultiUserFooterPreferenceController mMultiUserFooterPreferenceController;
private CharSequence mPendingUserName;
private Drawable mPendingUserIcon;
// A place to cache the generated default avatar // A place to cache the generated default avatar
private Drawable mDefaultIconDrawable; private Drawable mDefaultIconDrawable;
@@ -447,7 +455,7 @@ public class UserSettings extends SettingsPreferenceFragment
break; break;
case USER_TYPE_RESTRICTED_PROFILE: case USER_TYPE_RESTRICTED_PROFILE:
if (hasLockscreenSecurity()) { if (hasLockscreenSecurity()) {
addUserNow(USER_TYPE_RESTRICTED_PROFILE); showDialog(DIALOG_USER_PROFILE_EDITOR_ADD_RESTRICTED_PROFILE);
} else { } else {
showDialog(DIALOG_NEED_LOCKSCREEN); showDialog(DIALOG_NEED_LOCKSCREEN);
} }
@@ -466,22 +474,6 @@ public class UserSettings extends SettingsPreferenceFragment
} }
} }
private UserInfo createRestrictedProfile() {
UserInfo newUserInfo = mUserManager.createRestrictedProfile(mAddingUserName);
if (newUserInfo != null && !assignDefaultPhoto(getActivity(), newUserInfo.id)) {
return null;
}
return newUserInfo;
}
private UserInfo createTrustedUser() {
UserInfo newUserInfo = mUserManager.createUser(mAddingUserName, 0);
if (newUserInfo != null && !assignDefaultPhoto(getActivity(), newUserInfo.id)) {
return null;
}
return newUserInfo;
}
private void onManageUserClicked(int userId, boolean newUser) { private void onManageUserClicked(int userId, boolean newUser) {
mAddingUser = false; mAddingUser = false;
if (userId == UserPreference.USERID_GUEST_DEFAULTS) { if (userId == UserPreference.USERID_GUEST_DEFAULTS) {
@@ -571,15 +563,13 @@ public class UserSettings extends SettingsPreferenceFragment
final int messageResId = longMessageDisplayed final int messageResId = longMessageDisplayed
? R.string.user_add_user_message_short ? R.string.user_add_user_message_short
: R.string.user_add_user_message_long; : R.string.user_add_user_message_long;
final int userType = dialogId == DIALOG_ADD_USER
? USER_TYPE_USER : USER_TYPE_RESTRICTED_PROFILE;
Dialog dlg = new AlertDialog.Builder(context) Dialog dlg = new AlertDialog.Builder(context)
.setTitle(R.string.user_add_user_title) .setTitle(R.string.user_add_user_title)
.setMessage(messageResId) .setMessage(messageResId)
.setPositiveButton(android.R.string.ok, .setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() { new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
addUserNow(userType); showDialog(DIALOG_USER_PROFILE_EDITOR_ADD_USER);
if (!longMessageDisplayed) { if (!longMessageDisplayed) {
preferences.edit().putBoolean( preferences.edit().putBoolean(
KEY_ADD_USER_LONG_MESSAGE_DISPLAYED, KEY_ADD_USER_LONG_MESSAGE_DISPLAYED,
@@ -675,20 +665,96 @@ public class UserSettings extends SettingsPreferenceFragment
return dlg; return dlg;
} }
case DIALOG_USER_PROFILE_EDITOR: { case DIALOG_USER_PROFILE_EDITOR: {
Dialog dlg = mEditUserInfoController.createDialog( UserHandle user = myUserHandle();
UserInfo info = mUserManager.getUserInfo(user.getIdentifier());
return mEditUserInfoController.createDialog(
this, this,
null, Utils.getUserIcon(getPrefContext(), mUserManager, info),
mMePreference.getTitle(), info.name,
R.string.profile_info_settings_title, getString(R.string.profile_info_settings_title),
this /* callback */, new EditUserInfoController.OnContentChangedCallback() {
android.os.Process.myUserHandle()); @Override
return dlg; public void onPhotoChanged(UserHandle user, Drawable photo) {
ThreadUtils.postOnBackgroundThread(new Runnable() {
@Override
public void run() {
mUserManager.setUserIcon(user.getIdentifier(),
UserIcons.convertToBitmap(photo));
}
});
mMePreference.setIcon(photo);
}
@Override
public void onLabelChanged(UserHandle user, CharSequence label) {
mMePreference.setTitle(label.toString());
mUserManager.setUserName(user.getIdentifier(), label.toString());
}
},
user,
null);
}
case DIALOG_USER_PROFILE_EDITOR_ADD_USER: {
synchronized (mUserLock) {
mPendingUserIcon = UserIcons.getDefaultUserIcon(getPrefContext().getResources(),
new Random(System.currentTimeMillis()).nextInt(8), false);
mPendingUserName = getString(R.string.user_new_user_name);
}
return buildAddUserProfileEditorDialog(USER_TYPE_USER);
}
case DIALOG_USER_PROFILE_EDITOR_ADD_RESTRICTED_PROFILE: {
synchronized (mUserLock) {
mPendingUserIcon = UserIcons.getDefaultUserIcon(getPrefContext().getResources(),
new Random(System.currentTimeMillis()).nextInt(8), false);
mPendingUserName = getString(R.string.user_new_profile_name);
}
return buildAddUserProfileEditorDialog(USER_TYPE_RESTRICTED_PROFILE);
} }
default: default:
return null; return null;
} }
} }
private Dialog buildAddUserProfileEditorDialog(int userType) {
Dialog d;
synchronized (mUserLock) {
d = mEditUserInfoController.createDialog(
this,
mPendingUserIcon,
mPendingUserName,
getString(userType == USER_TYPE_USER
? R.string.user_info_settings_title
: R.string.profile_info_settings_title),
new EditUserInfoController.OnContentChangedCallback() {
@Override
public void onPhotoChanged(UserHandle user, Drawable photo) {
mPendingUserIcon = photo;
}
@Override
public void onLabelChanged(UserHandle user, CharSequence label) {
mPendingUserName = label;
}
},
myUserHandle(),
new EditUserInfoController.OnDialogCompleteCallback() {
@Override
public void onPositive() {
addUserNow(userType);
}
@Override
public void onNegativeOrCancel() {
synchronized (mUserLock) {
mPendingUserIcon = null;
mPendingUserName = null;
}
}
});
}
return d;
}
@Override @Override
public int getDialogMetricsCategory(int dialogId) { public int getDialogMetricsCategory(int dialogId) {
switch (dialogId) { switch (dialogId) {
@@ -709,6 +775,8 @@ public class UserSettings extends SettingsPreferenceFragment
case DIALOG_CONFIRM_EXIT_GUEST: case DIALOG_CONFIRM_EXIT_GUEST:
return SettingsEnums.DIALOG_USER_CONFIRM_EXIT_GUEST; return SettingsEnums.DIALOG_USER_CONFIRM_EXIT_GUEST;
case DIALOG_USER_PROFILE_EDITOR: case DIALOG_USER_PROFILE_EDITOR:
case DIALOG_USER_PROFILE_EDITOR_ADD_USER:
case DIALOG_USER_PROFILE_EDITOR_ADD_RESTRICTED_PROFILE:
return SettingsEnums.DIALOG_USER_EDIT_PROFILE; return SettingsEnums.DIALOG_USER_EDIT_PROFILE;
default: default:
return 0; return 0;
@@ -719,14 +787,15 @@ public class UserSettings extends SettingsPreferenceFragment
if (mRemovingUserId == UserHandle.myUserId()) { if (mRemovingUserId == UserHandle.myUserId()) {
removeThisUser(); removeThisUser();
} else { } else {
new Thread() { ThreadUtils.postOnBackgroundThread(new Runnable() {
@Override
public void run() { public void run() {
synchronized (mUserLock) { synchronized (mUserLock) {
mUserManager.removeUser(mRemovingUserId); mUserManager.removeUser(mRemovingUserId);
mHandler.sendEmptyMessage(MESSAGE_UPDATE_LIST); mHandler.sendEmptyMessage(MESSAGE_UPDATE_LIST);
} }
} }
}.start(); });
} }
} }
@@ -746,39 +815,59 @@ public class UserSettings extends SettingsPreferenceFragment
private void addUserNow(final int userType) { private void addUserNow(final int userType) {
synchronized (mUserLock) { synchronized (mUserLock) {
mAddingUser = true; mAddingUser = true;
mAddingUserName = userType == USER_TYPE_USER ? getString(R.string.user_new_user_name) mAddingUserName = userType == USER_TYPE_USER
: getString(R.string.user_new_profile_name); ? (mPendingUserName != null ? mPendingUserName.toString()
//updateUserList(); : getString(R.string.user_new_user_name))
new Thread() { : (mPendingUserName != null ? mPendingUserName.toString()
public void run() { : getString(R.string.user_new_profile_name));
UserInfo user; }
// Could take a few seconds ThreadUtils.postOnBackgroundThread(new Runnable() {
if (userType == USER_TYPE_USER) { @Override
user = createTrustedUser(); public void run() {
} else { UserInfo user;
user = createRestrictedProfile(); String username;
}
synchronized (mUserLock) {
username = mAddingUserName;
}
// Could take a few seconds
if (userType == USER_TYPE_USER) {
user = mUserManager.createUser(username, 0);
} else {
user = mUserManager.createRestrictedProfile(username);
}
synchronized (mUserLock) {
if (user == null) { if (user == null) {
mAddingUser = false; mAddingUser = false;
mPendingUserIcon = null;
mPendingUserName = null;
return; return;
} }
synchronized (mUserLock) {
if (userType == USER_TYPE_USER) { if (mPendingUserIcon != null) {
mHandler.sendEmptyMessage(MESSAGE_UPDATE_LIST); mUserManager.setUserIcon(user.id,
// Skip setting up user which results in user switching when the UserIcons.convertToBitmap(mPendingUserIcon));
// restriction is set.
if (!mUserCaps.mDisallowSwitchUser) {
mHandler.sendMessage(mHandler.obtainMessage(
MESSAGE_SETUP_USER, user.id, user.serialNumber));
}
} else {
mHandler.sendMessage(mHandler.obtainMessage(
MESSAGE_CONFIG_USER, user.id, user.serialNumber));
}
} }
if (userType == USER_TYPE_USER) {
mHandler.sendEmptyMessage(MESSAGE_UPDATE_LIST);
// Skip setting up user which results in user switching when the
// restriction is set.
if (!mUserCaps.mDisallowSwitchUser) {
mHandler.sendMessage(mHandler.obtainMessage(
MESSAGE_SETUP_USER, user.id, user.serialNumber));
}
} else {
mHandler.sendMessage(mHandler.obtainMessage(
MESSAGE_CONFIG_USER, user.id, user.serialNumber));
}
mPendingUserIcon = null;
mPendingUserName = null;
} }
}.start(); }
} });
} }
private void switchUserNow(int userId) { private void switchUserNow(int userId) {
@@ -1123,16 +1212,6 @@ public class UserSettings extends SettingsPreferenceFragment
return R.string.help_url_users; return R.string.help_url_users;
} }
@Override
public void onPhotoChanged(Drawable photo) {
mMePreference.setIcon(photo);
}
@Override
public void onLabelChanged(CharSequence label) {
mMePreference.setTitle(label);
}
/** /**
* Returns a default user icon (as a {@link Bitmap}) for the given user. * Returns a default user icon (as a {@link Bitmap}) for the given user.
* *

View File

@@ -18,13 +18,16 @@ package com.android.settings.users;
import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.same; import static org.mockito.ArgumentMatchers.same;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import android.app.Dialog;
import android.content.Intent; import android.content.Intent;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.widget.EditText; import android.widget.EditText;
@@ -37,9 +40,6 @@ import androidx.fragment.app.FragmentActivity;
import com.android.settings.R; import com.android.settings.R;
import com.android.settings.testutils.shadow.ShadowAlertDialogCompat; import com.android.settings.testutils.shadow.ShadowAlertDialogCompat;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@@ -50,6 +50,9 @@ import org.robolectric.RobolectricTestRunner;
import org.robolectric.android.controller.ActivityController; import org.robolectric.android.controller.ActivityController;
import org.robolectric.annotation.Config; import org.robolectric.annotation.Config;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@RunWith(RobolectricTestRunner.class) @RunWith(RobolectricTestRunner.class)
public class EditUserInfoControllerTest { public class EditUserInfoControllerTest {
private static final int MAX_USER_NAME_LENGTH = 100; private static final int MAX_USER_NAME_LENGTH = 100;
@@ -88,7 +91,8 @@ public class EditUserInfoControllerTest {
@Test @Test
public void photoControllerOnActivityResult_whenWaiting_isCalled() { public void photoControllerOnActivityResult_whenWaiting_isCalled() {
mController.createDialog(mFragment, mCurrentIcon, "test user", mController.createDialog(mFragment, mCurrentIcon, "test user",
R.string.profile_info_settings_title, null, android.os.Process.myUserHandle()); "title", null,
android.os.Process.myUserHandle(), null);
mController.startingActivityForResult(); mController.startingActivityForResult();
Intent resultData = new Intent(); Intent resultData = new Intent();
mController.onActivityResult(0, 0, resultData); mController.onActivityResult(0, 0, resultData);
@@ -104,8 +108,8 @@ public class EditUserInfoControllerTest {
final String longName = Stream.generate( final String longName = Stream.generate(
() -> String.valueOf('A')).limit(200).collect(Collectors.joining()); () -> String.valueOf('A')).limit(200).collect(Collectors.joining());
final AlertDialog dialog = (AlertDialog) mController.createDialog(mFragment, mCurrentIcon, final AlertDialog dialog = (AlertDialog) mController.createDialog(mFragment, mCurrentIcon,
"test user", R.string.profile_info_settings_title, null, "test user", "title", null,
android.os.Process.myUserHandle()); android.os.Process.myUserHandle(), null);
final EditText userName = ShadowAlertDialogCompat.shadowOf(dialog).getView() final EditText userName = ShadowAlertDialogCompat.shadowOf(dialog).getView()
.findViewById(R.id.user_name); .findViewById(R.id.user_name);
@@ -113,4 +117,143 @@ public class EditUserInfoControllerTest {
assertThat(userName.getText().length()).isEqualTo(MAX_USER_NAME_LENGTH); assertThat(userName.getText().length()).isEqualTo(MAX_USER_NAME_LENGTH);
} }
@Test
public void onDialogCompleteCallback_isCalled_whenCancelled() {
EditUserInfoController.OnContentChangedCallback contentChangeCallback = mock(
EditUserInfoController.OnContentChangedCallback.class);
EditUserInfoController.OnDialogCompleteCallback dialogCompleteCallback = mock(
EditUserInfoController.OnDialogCompleteCallback.class);
AlertDialog dialog = (AlertDialog) mController.createDialog(
mFragment, mCurrentIcon, "test",
"title", contentChangeCallback,
android.os.Process.myUserHandle(),
dialogCompleteCallback);
dialog.show();
dialog.cancel();
verify(contentChangeCallback, times(0))
.onLabelChanged(any(), any());
verify(contentChangeCallback, times(0))
.onPhotoChanged(any(), any());
verify(dialogCompleteCallback, times(0)).onPositive();
verify(dialogCompleteCallback, times(1)).onNegativeOrCancel();
}
@Test
public void onDialogCompleteCallback_isCalled_whenPositiveClicked() {
EditUserInfoController.OnContentChangedCallback contentChangeCallback = mock(
EditUserInfoController.OnContentChangedCallback.class);
EditUserInfoController.OnDialogCompleteCallback dialogCompleteCallback = mock(
EditUserInfoController.OnDialogCompleteCallback.class);
AlertDialog dialog = (AlertDialog) mController.createDialog(
mFragment, mCurrentIcon, "test",
"title", contentChangeCallback,
android.os.Process.myUserHandle(),
dialogCompleteCallback);
// No change to the photo.
when(mController.getPhotoController().getNewUserPhotoDrawable()).thenReturn(mCurrentIcon);
dialog.show();
dialog.getButton(Dialog.BUTTON_POSITIVE).performClick();
verify(contentChangeCallback, times(0))
.onLabelChanged(any(), any());
verify(contentChangeCallback, times(0))
.onPhotoChanged(any(), any());
verify(dialogCompleteCallback, times(1)).onPositive();
verify(dialogCompleteCallback, times(0)).onNegativeOrCancel();
}
@Test
public void onDialogCompleteCallback_isCalled_whenNegativeClicked() {
EditUserInfoController.OnContentChangedCallback contentChangeCallback = mock(
EditUserInfoController.OnContentChangedCallback.class);
EditUserInfoController.OnDialogCompleteCallback dialogCompleteCallback = mock(
EditUserInfoController.OnDialogCompleteCallback.class);
AlertDialog dialog = (AlertDialog) mController.createDialog(
mFragment, mCurrentIcon, "test",
"title", contentChangeCallback,
android.os.Process.myUserHandle(),
dialogCompleteCallback);
dialog.show();
dialog.getButton(Dialog.BUTTON_NEGATIVE).performClick();
verify(contentChangeCallback, times(0))
.onLabelChanged(any(), any());
verify(contentChangeCallback, times(0))
.onPhotoChanged(any(), any());
verify(dialogCompleteCallback, times(0)).onPositive();
verify(dialogCompleteCallback, times(1)).onNegativeOrCancel();
}
@Test
public void onContentChangedCallback_isCalled_whenLabelChanges() {
EditUserInfoController.OnContentChangedCallback contentChangeCallback = mock(
EditUserInfoController.OnContentChangedCallback.class);
EditUserInfoController.OnDialogCompleteCallback dialogCompleteCallback = mock(
EditUserInfoController.OnDialogCompleteCallback.class);
AlertDialog dialog = (AlertDialog) mController.createDialog(
mFragment, mCurrentIcon, "test",
"title", contentChangeCallback,
android.os.Process.myUserHandle(),
dialogCompleteCallback);
// No change to the photo.
when(mController.getPhotoController().getNewUserPhotoDrawable()).thenReturn(mCurrentIcon);
dialog.show();
String expectedNewName = "new test user";
EditText editText = (EditText) dialog.findViewById(R.id.user_name);
editText.setText(expectedNewName);
dialog.getButton(Dialog.BUTTON_POSITIVE).performClick();
verify(contentChangeCallback, times(1))
.onLabelChanged(any(), eq(expectedNewName));
verify(contentChangeCallback, times(0))
.onPhotoChanged(any(), any());
verify(dialogCompleteCallback, times(1)).onPositive();
verify(dialogCompleteCallback, times(0)).onNegativeOrCancel();
}
@Test
public void onContentChangedCallback_isCalled_whenPhotoChanges() {
EditUserInfoController.OnContentChangedCallback contentChangeCallback = mock(
EditUserInfoController.OnContentChangedCallback.class);
EditUserInfoController.OnDialogCompleteCallback dialogCompleteCallback = mock(
EditUserInfoController.OnDialogCompleteCallback.class);
AlertDialog dialog = (AlertDialog) mController.createDialog(
mFragment, mCurrentIcon, "test",
"title", contentChangeCallback,
android.os.Process.myUserHandle(),
dialogCompleteCallback);
// A different drawable.
Drawable newPhoto = mock(Drawable.class);
when(mController.getPhotoController().getNewUserPhotoDrawable()).thenReturn(newPhoto);
dialog.show();
dialog.getButton(Dialog.BUTTON_POSITIVE).performClick();
verify(contentChangeCallback, times(0))
.onLabelChanged(any(), any());
verify(contentChangeCallback, times(1))
.onPhotoChanged(any(), eq(newPhoto));
verify(dialogCompleteCallback, times(1)).onPositive();
verify(dialogCompleteCallback, times(0)).onNegativeOrCancel();
}
} }