From 87acb935056cc463c2531b8e1815fd95a94aafd5 Mon Sep 17 00:00:00 2001 From: Nalla Kartheek Date: Tue, 28 Jul 2015 19:03:41 +0530 Subject: Wi-Fi: Fix for SoftAP turn ON/OFF fails issue. Turnig ON/OFF SAP repeatedly when WIFI is ON results in to command CMD_SET_AP being received in WifiStaEnabledState of the state machine, which will not be handled and because of this SoftAP is not turned ON and we receive no events thus the check box remains greyed out/disabled. To resolve this wait for WIFI_STATE_CHANGED intent before re-enabling the SAP. Change-Id: Ifc330309c7919f2efc2916573353d0764b5e1f9e --- src/com/android/settings/wifi/WifiApEnabler.java | 44 +++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/src/com/android/settings/wifi/WifiApEnabler.java b/src/com/android/settings/wifi/WifiApEnabler.java index 741c4a7..92d605a 100644 --- a/src/com/android/settings/wifi/WifiApEnabler.java +++ b/src/com/android/settings/wifi/WifiApEnabler.java @@ -41,6 +41,8 @@ public class WifiApEnabler { ConnectivityManager mCm; private String[] mWifiRegexs; + /* Indicates if we have to wait for WIFI_STATE_CHANGED intent */ + private boolean mWaitForWifiStateChange = false; private final BroadcastReceiver mReceiver = new BroadcastReceiver() { @Override @@ -56,6 +58,11 @@ public class WifiApEnabler { } else { handleWifiApStateChanged(state, WifiManager.SAP_START_FAILURE_GENERAL); } + } else if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(action)) { + if (mWaitForWifiStateChange == true) { + handleWifiStateChanged(intent.getIntExtra( + WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN)); + } } else if (ConnectivityManager.ACTION_TETHER_STATE_CHANGED.equals(action)) { ArrayList available = intent.getStringArrayListExtra( ConnectivityManager.EXTRA_AVAILABLE_TETHER); @@ -84,6 +91,7 @@ public class WifiApEnabler { mIntentFilter = new IntentFilter(WifiManager.WIFI_AP_STATE_CHANGED_ACTION); mIntentFilter.addAction(ConnectivityManager.ACTION_TETHER_STATE_CHANGED); mIntentFilter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED); + mIntentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); } public void resume() { @@ -107,6 +115,27 @@ public class WifiApEnabler { } public void setSoftapEnabled(boolean enable) { + int wifiSavedState = 0; + /** + * Check if we have to wait for the WIFI_STATE_CHANGED intent + * before we re-enable the Checkbox. + */ + if (!enable) { + try { + wifiSavedState = Settings.Global.getInt(mContext.getContentResolver(), + Settings.Global.WIFI_SAVED_STATE); + } catch (Settings.SettingNotFoundException e) { + ; + } + /** + * If Wi-Fi is turned of as part of SoftAp turn on process, + * we need to restore, Wi-Fi state after SoftAp turn Off. + * WIFI_SAVED_STATE inficates the state. + */ + if (wifiSavedState == 1) { + mWaitForWifiStateChange = true; + } + } if (TetherUtil.setWifiTethering(enable, mContext)) { /* Disable here, enabled on receiving success broadcast */ mSwitch.setEnabled(false); @@ -172,7 +201,9 @@ public class WifiApEnabler { case WifiManager.WIFI_AP_STATE_DISABLED: mSwitch.setChecked(false); mSwitch.setSummary(mOriginalSummary); - enableWifiSwitch(); + if (mWaitForWifiStateChange == false) { + enableWifiSwitch(); + } break; default: mSwitch.setChecked(false); @@ -184,4 +215,15 @@ public class WifiApEnabler { enableWifiSwitch(); } } + + private void handleWifiStateChanged(int state) { + switch (state) { + case WifiManager.WIFI_STATE_ENABLED: + case WifiManager.WIFI_STATE_UNKNOWN: + enableWifiSwitch(); + mWaitForWifiStateChange = false; + break; + default: + } + } } -- cgit v1.1