WifiStateMachine: remove service dependence

The constructor for WifiStateMachine currently depends on other wifi
related services starting in a particular order (ex, p2p starting before
wifi service).  This CL removes this dependence and delays retrieval of
other services until after the boot complete command.

Setting the local WifiApConfigStore variable is also moved to the
constructor for consistency with other locally stored state.

This CL additionally alters the WifiStateMachineTest to send a different
message to test log rec sizes when verbose logging settings are changed
(this set of tests seems out of place for WSM, but this CL is not meant
to address that problem).

Bug: 34137732
Test: frameworks/opt/net/wifi/tests/wifitests/runtests.sh
Test: frameworks/base/wifi/tests/runtests.sh
Test: manually tested wifi connection
Test: manually checked for Wifi Direct peers
Change-Id: I50fcc378bbbcc98ab833a01b9dd6c044c5f85e3d
diff --git a/service/java/com/android/server/wifi/WifiStateMachine.java b/service/java/com/android/server/wifi/WifiStateMachine.java
index 9b62a0c..5786d8b 100644
--- a/service/java/com/android/server/wifi/WifiStateMachine.java
+++ b/service/java/com/android/server/wifi/WifiStateMachine.java
@@ -429,7 +429,6 @@
     // Channel for sending replies.
     private AsyncChannel mReplyChannel = new AsyncChannel();
 
-    private WifiP2pServiceImpl mWifiP2pServiceImpl;
     private WifiAwareManager mWifiAwareManager;
 
     // Used to initiate a connection with WifiP2pService
@@ -876,6 +875,7 @@
                 .hasSystemFeature(PackageManager.FEATURE_WIFI_AWARE);
 
         mWifiConfigManager = mWifiInjector.getWifiConfigManager();
+        mWifiApConfigStore = mWifiInjector.getWifiApConfigStore();
         mWifiSupplicantControl = mWifiInjector.getWifiSupplicantControl();
         mWifiSupplicantControl.setSystemSupportsFastBssTransition(
                 mContext.getResources().getBoolean(R.bool.config_wifi_fast_bss_transition_enabled));
@@ -892,13 +892,6 @@
 
         mLinkProperties = new LinkProperties();
 
-        IBinder s1 = mFacade.getService(Context.WIFI_P2P_SERVICE);
-        mWifiP2pServiceImpl = (WifiP2pServiceImpl) IWifiP2pManager.Stub.asInterface(s1);
-
-        if (mAwareSupported) {
-            mWifiAwareManager = mContext.getSystemService(WifiAwareManager.class);
-        }
-
         mNetworkInfo.setIsAvailable(false);
         mLastBssid = null;
         mLastNetworkId = WifiConfiguration.INVALID_NETWORK_ID;
@@ -3653,6 +3646,31 @@
         }
     }
 
+    /**
+     * WifiStateMachine needs to enable/disable other services when wifi is in client mode.  This
+     * method allows WifiStateMachine to get these additional system services.
+     *
+     * At this time, this method is used to setup variables for P2P service and Wifi Aware.
+     */
+    private void getAdditionalWifiServiceInterfaces() {
+        // First set up Wifi Direct
+        // TODO: b/34193861 determine if we can avoid starting WIFI_P2P_SERVICE when not supported
+        IBinder s1 = mFacade.getService(Context.WIFI_P2P_SERVICE);
+        WifiP2pServiceImpl wifiP2pServiceImpl =
+                (WifiP2pServiceImpl) IWifiP2pManager.Stub.asInterface(s1);
+
+        if (wifiP2pServiceImpl != null) {
+            mWifiP2pChannel = new AsyncChannel();
+            mWifiP2pChannel.connect(mContext, getHandler(),
+                    wifiP2pServiceImpl.getP2pStateMachineMessenger());
+        }
+
+        // Set up Wifi Aware
+        if (mAwareSupported) {
+            mWifiAwareManager = mContext.getSystemService(WifiAwareManager.class);
+        }
+    }
+
     /********************************************************
      * HSM states
      *******************************************************/
@@ -3716,6 +3734,8 @@
                     }
                     break;
                 case CMD_BOOT_COMPLETED:
+                    // get other services that we need to manage
+                    getAdditionalWifiServiceInterfaces();
                     maybeRegisterNetworkFactory();
                     break;
                 case CMD_SCREEN_STATE_CHANGED:
@@ -3944,16 +3964,8 @@
         @Override
         public void enter() {
             cleanup();
-            if (mWifiP2pChannel == null && mWifiP2pServiceImpl != null) {
-                mWifiP2pChannel = new AsyncChannel();
-                mWifiP2pChannel.connect(mContext, getHandler(),
-                    mWifiP2pServiceImpl.getP2pStateMachineMessenger());
-            }
-
-            if (mWifiApConfigStore == null) {
-                mWifiApConfigStore = mWifiInjector.getWifiApConfigStore();
-            }
         }
+
         @Override
         public boolean processMessage(Message message) {
             logStateAndMessage(message, this);
diff --git a/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java b/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java
index 349f269..5fb6513 100644
--- a/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java
+++ b/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java
@@ -837,7 +837,7 @@
     @Test
     public void normalLogRecSizeIsUsedByDefault() {
         for (int i = 0; i < WifiStateMachine.NUM_LOG_RECS_NORMAL * 2; i++) {
-            mWsm.sendMessage(WifiStateMachine.CMD_BOOT_COMPLETED);
+            mWsm.sendMessage(WifiStateMachine.CMD_DISCONNECT);
         }
         mLooper.dispatchAll();
         assertEquals(WifiStateMachine.NUM_LOG_RECS_NORMAL, mWsm.getLogRecSize());
@@ -851,7 +851,7 @@
         assertTrue(LOG_REC_LIMIT_IN_VERBOSE_MODE > WifiStateMachine.NUM_LOG_RECS_NORMAL);
         mWsm.enableVerboseLogging(1);
         for (int i = 0; i < LOG_REC_LIMIT_IN_VERBOSE_MODE * 2; i++) {
-            mWsm.sendMessage(WifiStateMachine.CMD_BOOT_COMPLETED);
+            mWsm.sendMessage(WifiStateMachine.CMD_DISCONNECT);
         }
         mLooper.dispatchAll();
         assertEquals(LOG_REC_LIMIT_IN_VERBOSE_MODE, mWsm.getLogRecSize());
@@ -865,7 +865,7 @@
     public void disablingVerboseLoggingClearsRecordsAndDecreasesLogRecSize() {
         mWsm.enableVerboseLogging(1);
         for (int i = 0; i < LOG_REC_LIMIT_IN_VERBOSE_MODE; i++) {
-            mWsm.sendMessage(WifiStateMachine.CMD_BOOT_COMPLETED);
+            mWsm.sendMessage(WifiStateMachine.CMD_DISCONNECT);
         }
         mLooper.dispatchAll();
         assertEquals(LOG_REC_LIMIT_IN_VERBOSE_MODE, mWsm.getLogRecSize());
@@ -873,7 +873,7 @@
         mWsm.enableVerboseLogging(0);
         assertEquals(0, mWsm.getLogRecSize());
         for (int i = 0; i < LOG_REC_LIMIT_IN_VERBOSE_MODE; i++) {
-            mWsm.sendMessage(WifiStateMachine.CMD_BOOT_COMPLETED);
+            mWsm.sendMessage(WifiStateMachine.CMD_DISCONNECT);
         }
         mLooper.dispatchAll();
         assertEquals(WifiStateMachine.NUM_LOG_RECS_NORMAL, mWsm.getLogRecSize());