由于管理wifi的service(WifiService)是属于java framework的systemserver中的服务,继承于 SystemService。因此,WIfiService也可以在用户切换的过程中,由AMS回调onStartUser和onSwitchUser。
可以看到WifiService实现了onSwitchUser函数
@Override
public void onSwitchUser(int userId) {
mImpl.handleUserSwitch(userId);
}
mImpl是WifiServiceImpl类的实例
public void handleUserSwitch(int userId) {
mWifiStateMachine.handleUserSwitch(userId);
}
mWifiStateMachine是WifiStateMachine类的实例
public void handleUserSwitch(int userId) {
sendMessage(CMD_USER_SWITCH, userId);
}
//下面是DefauleState中处理message的函数
case CMD_USER_SWITCH:
Set<Integer> removedNetworkIds =
mWifiConfigManager.handleUserSwitch(message.arg1);
if (removedNetworkIds.contains(mTargetNetworkId) ||
removedNetworkIds.contains(mLastNetworkId)) {
// Disconnect and let autojoin reselect a new network
sendMessage(CMD_DISCONNECT);
}
break;
mWifiConfigManager是WifiConfigManager类的实例
public Set<Integer> handleUserSwitch(int userId) {
......
Set<Integer> removedNetworkIds = clearInternalUserData(mCurrentUserId);
mConfiguredNetworks.setNewUser(userId);
mCurrentUserId = userId;
if (mUserManager.isUserUnlockingOrUnlocked(mCurrentUserId)) {
handleUserUnlockOrSwitch(mCurrentUserId);
} else {
// Cannot read data from new user's CE store file before they log-in.
mPendingUnlockStoreRead = true;
Log.i(TAG, "Waiting for user unlock to load from store");
}
return removedNetworkIds;
}
private void handleUserUnlockOrSwitch(int userId) {
if (mVerboseLoggingEnabled) {
Log.v(TAG, "Loading from store after user switch/unlock for " + userId);
}
// Switch out the user store file. 下面是加载新用户wifi配置的重点
if (loadFromUserStoreAfterUnlockOrSwitch(userId)) {
saveToStore(true);
mPendingUnlockStoreRead = false;
}
}