Android系统服务启动完毕的时候,会去启动其他服务,其中就包括WifiService。
以下均参考Android P源码。
1. 启动WifiService流程
private static final String WIFI_SERVICE_CLASS =
"com.android.server.wifi.WifiService";
/**
* The main entry point from zygote.
*/
public static void main(String[] args) {
new SystemServer().run();
}
private void run() {
........
// Start services.
startOtherServices();
}
private void startOtherServices() {
........
mSystemServiceManager.startService(WIFI_SERVICE_CLASS);
mSystemServiceManager.startService(
"com.android.server.wifi.scanner.WifiScanningService");
}
2. WifiService构造函数中会new一个WifiServiceImpl,
public final class WifiService extends SystemService {
private static final String TAG = "WifiService";
final WifiServiceImpl mImpl;
public WifiService(Context context) {
super(context);
mImpl = new WifiServiceImpl(context, new WifiInjector(context), new WifiAsyncChannel(TAG));
}
@Override
public void onStart() {
Log.i(TAG, "Registering " + Context.WIFI_SERVICE);
publishBinderService(Context.WIFI_SERVICE, mImpl);
}
@Override
public void onBootPhase(int phase) {
if (phase == SystemService.PHASE_SYSTEM_SERVICES_READY) {
mImpl.checkAndStartWifi();
}
}
@Override
public void onSwitchUser(int userId) {
mImpl.handleUserSwitch(userId);
}
@Override
public void onUnlockUser(int userId) {
mImpl.handleUserUnlock(userId);
}
@Override
public void onStopUser(int userId) {
mImpl.handleUserStop(userId);
}
}
WifiServiceImpl内部的主要成员:
WifiStateMachine
WifiStateMachinePrime
WifiController
ScanRequestProxy
WifiCountryCode
WifiSettingsStore
WifiApConfigStore
public WifiServiceImpl(Context context, WifiInjector wifiInjector, AsyncChannel asyncChannel) {
mContext = context;
mWifiInjector = wifiInjector;
mClock = wifiInjector.getClock();
mFacade = mWifiInjector.getFrameworkFacade();
mWifiMetrics = mWifiInjector.getWifiMetrics();
mTrafficPoller = mWifiInjector.getWifiTrafficPoller();
mUserManager = mWifiInjector.getUserManager();
mCountryCode = mWifiInjector.getWifiCountryCode();
mWifiStateMachine = mWifiInjector.getWifiStateMachine();
mWifiStateMachinePrime = mWifiInjector.getWifiStateMachinePrime();
mWifiStateMachine.enableRssiPolling(true);
mScanRequestProxy = mWifiInjector.getScanRequestProxy();
mSettingsStore = mWifiInjector.getWifiSettingsStore();
mPowerManager = mContext.getSystemService(PowerManager.class);
mAppOps = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE);
mActivityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE);
mWifiLockManager = mWifiInjector.getWifiLockManager();
mWifiMulticastLockManager = mWifiInjector.getWifiMulticastLockManager();
HandlerThread wifiServiceHandlerThread = mWifiInjector.getWifiServiceHandlerThread();
mClientHandler = new ClientHandler(TAG, wifiServiceHandlerThread.getLooper());
mWifiStateMachineHandler = new WifiStateMachineHandler(TAG,
wifiServiceHandlerThread.getLooper(), asyncChannel);
mWifiController = mWifiInjector.getWifiController();
mWifiBackupRestore = mWifiInjector.getWifiBackupRestore();
mWifiApConfigStore = mWifiInjector.getWifiApConfigStore();
mPermissionReviewRequired = Build.PERMISSIONS_REVIEW_REQUIRED
|| context.getResources().getBoolean(
com.android.internal.R.bool.config_permissionReviewRequired);
mWifiPermissionsUtil = mWifiInjector.getWifiPermissionsUtil();
mLog = mWifiInjector.makeLog(TAG);
mFrameworkFacade = wifiInjector.getFrameworkFacade();
mIfaceIpModes = new ConcurrentHashMap<>();
mLocalOnlyHotspotRequests = new HashMap<>();
enableVerboseLoggingInternal(getVerboseLoggingLevel());
mRegisteredSoftApCallbacks = new HashMap<>();
mWifiInjector.getWifiStateMachinePrime().registerSoftApCallback(new SoftApCallbackImpl());
mPowerProfile = mWifiInjector.getPowerProfile();
}
3. 等待 SystemService.PHASE_SYSTEM_SERVICES_READY的时候,会调用 WifiServiceImpl#checkAndStartWifi(), 获取Wi-Fi开关,持久化存储的值。
存在SettingsProvider中 ?"wifi_on" Settings.Global.getInt(cr, Settings.Global.WIFI_ON) , 如果上次Wi-Fi是开着的,则调用setWifiEnabled,走Wi-Fi打开流程。
具体流程如下图:
如果有需求要求默认每次开机WiFi都要打开,可以在WifiServiceImpl.checkAndStartWifi中添加修改,强制每次都去打开,如下:
/**
* Check if we are ready to start wifi.
*
* First check if we will be restarting system services to decrypt the device. If the device is
* not encrypted, check if Wi-Fi needs to be enabled and start if needed
*
* This function is used only at boot time.
*/
public void checkAndStartWifi() {
// First check if we will end up restarting WifiService
if (mFrameworkFacade.inStorageManagerCryptKeeperBounce()) {
Log.d(TAG, "Device still encrypted. Need to restart SystemServer. Do not start wifi.");
return;
}
// Check if wi-fi needs to be enabled
//boolean wifiEnabled = mSettingsStore.isWifiToggleEnabled();
boolean wifiEnabled = true;
Slog.i(TAG, "WifiService starting up with Wi-Fi " +
(wifiEnabled ? "enabled" : "disabled"));
........
}
|