多用户切换流程
切换新用户Settings应用流程:
代码路径:packages/apps/Settings/src/com/android/settings/users/UserSettings.java
1.多用户下点击事件
public boolean onPreferenceClick(Preference pref)
2.这段代码机主用户下切换新用户:
else if (pref instanceof UserPreference) {
1061 int userId = ((UserPreference) pref).getUserId();
1062 // Get the latest status of the user
1063 UserInfo user = mUserManager.getUserInfo(userId);
1064 if (!isInitialized(user)) {
未初始化点击新用户,mHandler发送消息,消息id:MESSAGE_SETUP_USER
1065 mHandler.sendMessage(mHandler.obtainMessage(
1066 MESSAGE_SETUP_USER, user.id, user.serialNumber));
1067 }
1068 }
3.mHandler收到 MESSAGE_SETUP_USER消息
mHandler handleMessage(Message msg)
MESSAGE_SETUP_USER:
4.调用创建新用户方法
onUserCreated(int userId)
5.显示对话框
showDialog(DIALOG_SETUP_USER)
6.创建所有显示的对话框
public Dialog onCreateDialog(int dialogId)
对话框id:DIALOG_SETUP_USER
根据userId切换新用户
switchUserNow(int userId)
7.执行代码
ActivityManager.getService().switchUser(userId);
移除用户流程:
对话框id:DIALOG_CONFIRM_REMOVE:
移除其他用户切换机主用户
removeThisUser()
执行代码
ActivityManager.getService().switchUser(UserHandle.USER_SYSTEM);
getContext().getSystemService(UserManager.class).removeUser(UserHandle.myUserId());
owner创建访客流程
packages/apps/Settings/src/com/android/settings/users/UserSettings.java
UserInfo guest = mUserManager.createGuest(getContext(), preference.getTitle().toString());
919 pref.setOnPreferenceClickListener(preference -> {
920 int id = finalGuestId;
921 if (id == UserPreference.USERID_GUEST_DEFAULTS) {
922 UserInfo guest = mUserManager.createGuest(
923 getContext(), preference.getTitle().toString()); //第一次创建访客用户调用FWK的UserManger的创建访客方法。(移除访客后需要走重新创建流程,用户使用上基本未感知到变化,只是弹Dialog稍微慢一点)
924 if (guest != null) {
925 id = guest.id;
926 }
927 }
928 try {
// 已有访客用户才切换访客
929 ActivityManager.getService().switchUser(id);
930 } catch (RemoteException e) {
931 e.rethrowFromSystemServer();
932 }
933 return true;
934 });
// 创建访客用户简单流程:
frameworks/base/core/java/android/os/UserManager.java
public UserInfo createGuest(Context context, String name)
guest = mService.createUser(name, UserInfo.FLAG_GUEST);
SystemUI创建新用户:
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/policy/UserSwitcherController.java
UserInfo user = mUserManager.createUser(mContext.getString(R.string.user_new_user_name), 0 /* flags */);
// 确定创建新用户
frameworks/base/core/java/com/android/internal/app/ConfirmUserCreationActivity.java
public void onClick(DialogInterface dialog, int which)
UserInfo user = mUserManager.createUser(mUserName, 0);
// 创建所有子用户都会走到这里
frameworks/base/core/java/android/os/UserManager.java
public UserInfo createUser(String name, int flags)
user = mService.createUser(name, flags);
frameworks/base/services/core/java/com/android/server/pm/UserManagerService.java
// 构造方法创建 mUsersDir =data/system/users路径
public UserInfo createUser(String name, int flags)
private UserInfo createUserInternal(String name, int flags, int parentId)
private UserInfo createUserInternalUnchecked(String name, int flags, int parentId, String[] disallowedPackages)
private void writeUserLP(UserData userData) //创建用户访客数据文件 data/system/users/12.xml
void writeUserLP(UserData userData, OutputStream os)
serializer.text(userInfo.name); //写用户名字到data/system/users/12.xml
机主owner切子用户FWK流程:
应用层调用接口:
frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java
17587 @Override
17588 public boolean switchUser(final int targetUserId) {
17589 return mUserController.switchUser(targetUserId);
17590 }
frameworks/base/services/core/java/com/android/server/am/UserController.java
切换用户
boolean switchUser(final int targetUserId) {
1324 enforceShellRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES, targetUserId);
1325 int currentUserId = getCurrentUserId();
1326 UserInfo targetUserInfo = getUserInfo(targetUserId);
// 调用mUiHandler发送 START_USER_SWITCH_UI_MSG的message
1347 UserInfo currentUserInfo = getUserInfo(currentUserId);
1348 Pair<UserInfo, UserInfo> userNames = new Pair<>(currentUserInfo, targetUserInfo);
1349 mUiHandler.removeMessages(START_USER_SWITCH_UI_MSG);
1350 mUiHandler.sendMessage(mHandler.obtainMessage(
1351 START_USER_SWITCH_UI_MSG, userNames));
// mUiHandler收到START_USER_SWITCH_UI_MSG的message
2166 case START_USER_SWITCH_UI_MSG:
2167 showUserSwitchDialog((Pair<UserInfo, UserInfo>) msg.obj);
2168 break;
// showUserSwitchDialog方法
1360 private void showUserSwitchDialog(Pair<UserInfo, UserInfo> fromToUserPair) {
1361 // The dialog will show and then initiate the user switch by calling startUserInForeground
1362 mInjector.showUserSwitchingDialog(fromToUserPair.first, fromToUserPair.second,
1363 getSwitchingFromSystemUserMessage(), getSwitchingToSystemUserMessage());
1364 }
// 内部类Injecto的showUserSwitchingDialog方法实现
void showUserSwitchingDialog(UserInfo fromUser, UserInfo toUser,String switchingFromSystemUserMessage, String switchingToSystemUserMessage)
2359 d = new UserSwitchingDialog(mService, mService.mContext, fromUser, toUser,
2360 true /* above system */, switchingFromSystemUserMessage, switchingToSystemUserMessage);
frameworks/base/services/core/java/com/android/server/am/UserSwitchingDialog.java
60 public UserSwitchingDialog(ActivityManagerService service, Context context, UserInfo oldUser,
61 UserInfo newUser, boolean aboveSystem, String switchingFromSystemUserMessage,
62 String switchingToSystemUserMessage)
mNewUser.name是由上边传来的
109 // If switchingFromSystemUserMessage or switchingToSystemUserMessage is null, fallback
110 // to system message.
111 if (viewMessage == null) {
112 viewMessage = res.getString(R.string.user_switching_message, mNewUser.name);
113 }
提示:正在切换XX... ,java显示 mNewUser.name
frameworks/base/core/res/res/values/strings.xml
<!-- Message shown when switching to a user [CHAR LIMIT=none] -->
4525 <string name="user_switching_message">Switching to <xliff:g id="name" example="Bob">%1$s</xliff:g>\u2026</string>