/**
* 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();
Slog.i(TAG, "WifiService starting up with Wi-Fi " +
(wifiEnabled ? "enabled" : "disabled"));
registerForScanModeChange();
mContext.registerReceiver(
new BroadcastReceiver() {
public void onReceive(Context context, Intent intent){
if (mSettingsStore.handleAirplaneModeToggled()) {
mWifiController.sendMessage(CMD_AIRPLANE_TOGGLED);
}
if (mSettingsStore.isAirplaneModeOn()) {
Log.d(TAG, "resetting country code because Airplane mode is ON");
mCountryCode.airplaneModeEnabled();
}
}
},
new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED));
mContext.registerReceiver(
new BroadcastReceiver() {
public void onReceive(Context context, Intent intent){
String state = intent.getStringExtra(IccCardConstants.INTENT_KEY_ICC_STATE);
if (IccCardConstants.INTENT_VALUE_ICC_ABSENT.equals(state)) {
Log.d(TAG, "resetting networks because SIM was removed");
mWifiStateMachine.resetSimAuthNetworks(false);
} else if (IccCardConstants.INTENT_VALUE_ICC_LOADED.equals(state)) {
Log.d(TAG, "resetting networks because SIM was loaded");
mWifiStateMachine.resetSimAuthNetworks(true);
}
}
},
new IntentFilter(TelephonyIntents.ACTION_SIM_STATE_CHANGED));
mContext.registerReceiver(
new BroadcastReceiver() {
public void onReceive(Context context, Intent intent){
final int currState = intent.getIntExtra(EXTRA_WIFI_AP_STATE,
WIFI_AP_STATE_DISABLED);
final int prevState = intent.getIntExtra(EXTRA_PREVIOUS_WIFI_AP_STATE,
WIFI_AP_STATE_DISABLED);
final int errorCode = intent.getIntExtra(EXTRA_WIFI_AP_FAILURE_REASON,
HOTSPOT_NO_ERROR);
final String ifaceName =
intent.getStringExtra(EXTRA_WIFI_AP_INTERFACE_NAME);
final int mode = intent.getIntExtra(EXTRA_WIFI_AP_MODE,
WifiManager.IFACE_IP_MODE_UNSPECIFIED);
handleWifiApStateChange(currState, prevState, errorCode, ifaceName, mode);
}
},
new IntentFilter(WifiManager.WIFI_AP_STATE_CHANGED_ACTION));
// Adding optimizations of only receiving broadcasts when wifi is enabled
// can result in race conditions when apps toggle wifi in the background
// without active user involvement. Always receive broadcasts.
registerForBroadcasts();
mInIdleMode = mPowerManager.isDeviceIdleMode();
if (!mWifiStateMachine.syncInitialize(mWifiStateMachineChannel)) {
Log.wtf(TAG, "Failed to initialize WifiStateMachine");
}
mWifiController.start();
// If we are already disabled (could be due to airplane mode), avoid changing persist
// state here
if (wifiEnabled) {
try {
setWifiEnabled(mContext.getPackageName(), wifiEnabled);
} catch (RemoteException e) {
/* ignore - local call */
}
}
}