summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRebecca Silberstein <silberst@google.com>2018-04-24 16:39:04 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2018-04-24 16:39:04 +0000
commitd306f961e6cf39e5edc30de519f7ebebb1157c9b (patch)
tree175b2246e4715fde47d9e4b0d18e43e7a7e48633
parentc1446c32c8784809ac81f35a1794771eeb6d57cc (diff)
parent51e701cd43459ed946e46e306d4156e4f9aed73e (diff)
Merge "ClientModeManager: set wifi state" into pi-dev
-rw-r--r--service/java/com/android/server/wifi/ClientModeManager.java2
-rw-r--r--service/java/com/android/server/wifi/WifiStateMachine.java58
-rw-r--r--tests/wifitests/src/com/android/server/wifi/ClientModeManagerTest.java2
-rw-r--r--tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java39
4 files changed, 71 insertions, 30 deletions
diff --git a/service/java/com/android/server/wifi/ClientModeManager.java b/service/java/com/android/server/wifi/ClientModeManager.java
index 4fd3c6e27..f45623663 100644
--- a/service/java/com/android/server/wifi/ClientModeManager.java
+++ b/service/java/com/android/server/wifi/ClientModeManager.java
@@ -111,6 +111,8 @@ public class ClientModeManager implements ActiveModeManager {
return;
}
+ mWifiStateMachine.setWifiStateForApiCalls(newState);
+
final Intent intent = new Intent(WifiManager.WIFI_STATE_CHANGED_ACTION);
intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
intent.putExtra(WifiManager.EXTRA_WIFI_STATE, newState);
diff --git a/service/java/com/android/server/wifi/WifiStateMachine.java b/service/java/com/android/server/wifi/WifiStateMachine.java
index bae8f3a3b..84db81b39 100644
--- a/service/java/com/android/server/wifi/WifiStateMachine.java
+++ b/service/java/com/android/server/wifi/WifiStateMachine.java
@@ -1379,7 +1379,34 @@ public class WifiStateMachine extends StateMachine {
}
/**
- * TODO: doc
+ * Temporary method that allows the active ClientModeManager to set the wifi state that is
+ * retrieved by API calls. This will be removed when WifiServiceImpl no longer directly calls
+ * this class (b/31479117).
+ *
+ * @param newState new state to set, invalid states are ignored.
+ */
+ public void setWifiStateForApiCalls(int newState) {
+ switch (newState) {
+ case WIFI_STATE_DISABLING:
+ case WIFI_STATE_DISABLED:
+ case WIFI_STATE_ENABLING:
+ case WIFI_STATE_ENABLED:
+ case WIFI_STATE_UNKNOWN:
+ if (mVerboseLoggingEnabled) {
+ Log.d(TAG, "setting wifi state to: " + newState);
+ }
+ mWifiState.set(newState);
+ return;
+ default:
+ Log.d(TAG, "attempted to set an invalid state: " + newState);
+ return;
+ }
+ }
+
+ /**
+ * Method used by WifiServiceImpl to get the current state of Wifi (in client mode) for API
+ * calls. This will be removed when WifiService no longer directly calls this class
+ * (b/31479117).
*/
public int syncGetWifiState() {
return mWifiState.get();
@@ -2451,31 +2478,6 @@ public class WifiStateMachine extends StateMachine {
if (mVerboseLoggingEnabled) log("mSuspendOptNeedsDisabled " + mSuspendOptNeedsDisabled);
}
- private void setWifiState(int wifiState) {
- final int previousWifiState = mWifiState.get();
-
- mWifiState.set(wifiState);
-
- if (mVerboseLoggingEnabled) log("setWifiState: " + syncGetWifiStateByName());
-
- // first let WifiController know what is going on
- if (mClientModeCallback != null) {
- mClientModeCallback.onStateChanged(wifiState);
- // once this instance of client mode is complete, remove the callback so we don't
- // confuse ourselves
- if (wifiState == WifiManager.WIFI_STATE_UNKNOWN
- || wifiState == WifiManager.WIFI_STATE_DISABLED) {
- mClientModeCallback = null;
- }
- }
-
- final Intent intent = new Intent(WifiManager.WIFI_STATE_CHANGED_ACTION);
- intent.addFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
- intent.putExtra(WifiManager.EXTRA_WIFI_STATE, wifiState);
- intent.putExtra(WifiManager.EXTRA_PREVIOUS_WIFI_STATE, previousWifiState);
- mContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL);
- }
-
/*
* Fetch RSSI, linkspeed, and frequency on current connection
*/
@@ -3814,7 +3816,6 @@ public class WifiStateMachine extends StateMachine {
if (mNetworkAgent != null) mNetworkAgent.sendNetworkInfo(mNetworkInfo);
mCountryCode.setReadyForChange(false);
mInterfaceName = null;
- setWifiState(WIFI_STATE_DISABLED);
}
void registerConnected() {
@@ -3886,8 +3887,6 @@ public class WifiStateMachine extends StateMachine {
mScanRequestProxy.enableScanningForHiddenNetworks(true);
mWifiInfo.reset();
mWifiInfo.setSupplicantState(SupplicantState.DISCONNECTED);
- // Let the system know that wifi is available in client mode.
- setWifiState(WIFI_STATE_ENABLED);
mWifiInjector.getWakeupController().reset();
@@ -3925,7 +3924,6 @@ public class WifiStateMachine extends StateMachine {
mScanRequestProxy.clearScanResults();
mWifiInfo.reset();
mWifiInfo.setSupplicantState(SupplicantState.DISCONNECTED);
- setWifiState(WIFI_STATE_DISABLED);
stopClientMode();
}
diff --git a/tests/wifitests/src/com/android/server/wifi/ClientModeManagerTest.java b/tests/wifitests/src/com/android/server/wifi/ClientModeManagerTest.java
index c227f7ce8..9953843f4 100644
--- a/tests/wifitests/src/com/android/server/wifi/ClientModeManagerTest.java
+++ b/tests/wifitests/src/com/android/server/wifi/ClientModeManagerTest.java
@@ -128,6 +128,8 @@ public class ClientModeManagerTest {
assertEquals(expectedCurrentState, currentState);
int prevState = intent.getIntExtra(EXTRA_PREVIOUS_WIFI_STATE, WIFI_STATE_UNKNOWN);
assertEquals(expectedPrevState, prevState);
+
+ verify(mWifiStateMachine, atLeastOnce()).setWifiStateForApiCalls(expectedCurrentState);
}
diff --git a/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java b/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java
index 4729dd841..1907c56b7 100644
--- a/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java
+++ b/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java
@@ -612,6 +612,7 @@ public class WifiStateMachineTest {
reset(mContext);
// now disable wifi and verify the reported wifi state
+ mWsm.setWifiStateForApiCalls(WifiManager.WIFI_STATE_DISABLED);
mWsm.setOperationalMode(WifiStateMachine.DISABLED_MODE, null);
mLooper.dispatchAll();
assertEquals(WifiStateMachine.DISABLED_MODE, mWsm.getOperationalModeForTest());
@@ -784,6 +785,7 @@ public class WifiStateMachineTest {
* Helper method to move through startup states.
*/
private void startSupplicantAndDispatchMessages() throws Exception {
+ mWsm.setWifiStateForApiCalls(WifiManager.WIFI_STATE_ENABLED);
mWsm.setOperationalMode(WifiStateMachine.CONNECT_MODE, WIFI_IFACE_NAME);
mLooper.dispatchAll();
@@ -1823,6 +1825,7 @@ public class WifiStateMachineTest {
// Set WSM to DISABLED_MODE, verify state and wifi disabled in ConnectivityManager, and
// WifiInfo is reset() and state set to DISCONNECTED
+ mWsm.setWifiStateForApiCalls(WifiManager.WIFI_STATE_DISABLED);
mWsm.setOperationalMode(WifiStateMachine.DISABLED_MODE, null);
mLooper.dispatchAll();
@@ -2412,4 +2415,40 @@ public class WifiStateMachineTest {
mWsm.setOperationalMode(WifiStateMachine.DISABLED_MODE, null);
mLooper.dispatchAll();
}
+
+ /**
+ * Verify that valid calls to set the current wifi state are returned when requested.
+ */
+ @Test
+ public void verifySetAndGetWifiStateCallsWorking() throws Exception {
+ // we start off disabled
+ assertEquals(WifiManager.WIFI_STATE_DISABLED, mWsm.syncGetWifiState());
+
+ // now check after updating
+ mWsm.setWifiStateForApiCalls(WifiManager.WIFI_STATE_UNKNOWN);
+ assertEquals(WifiManager.WIFI_STATE_UNKNOWN, mWsm.syncGetWifiState());
+
+ // check after two updates
+ mWsm.setWifiStateForApiCalls(WifiManager.WIFI_STATE_ENABLING);
+ mWsm.setWifiStateForApiCalls(WifiManager.WIFI_STATE_ENABLED);
+ assertEquals(WifiManager.WIFI_STATE_ENABLED, mWsm.syncGetWifiState());
+ }
+
+ /**
+ * Verify that invalid states do not change the saved wifi state.
+ */
+ @Test
+ public void verifyInvalidStatesDoNotChangeSavedWifiState() throws Exception {
+ int invalidStateNegative = -1;
+ int invalidStatePositive = 5;
+
+ // we start off disabled
+ assertEquals(WifiManager.WIFI_STATE_DISABLED, mWsm.syncGetWifiState());
+
+ mWsm.setWifiStateForApiCalls(invalidStateNegative);
+ assertEquals(WifiManager.WIFI_STATE_DISABLED, mWsm.syncGetWifiState());
+
+ mWsm.setWifiStateForApiCalls(invalidStatePositive);
+ assertEquals(WifiManager.WIFI_STATE_DISABLED, mWsm.syncGetWifiState());
+ }
}