Fix LOHS CTS failure when STA+AP not supported

When testing WifiManagerTest on devices which don't support STA+AP
concurrency, STA may be enabled by previous test cases using
`invokeWithShellPermissions`. The WorkSource of this STA creation will
be treated as `WorkSourceHelper.PRIORITY_FG_SERVICE` in
`HalDeviceManager.allowedToDeleteForNoStaApConcurrencyLohs` and AP iface
creation request will be refused by
`HalDeviceManager.allowedToDeleteForNoStaApConcurrencyLohs` and causes
CTS test failure in the following cases:

* testStartLocalOnlyHotspotSuccess
* testStartLocalOnlyHotspotSingleRequestByApps
* testIsStaApConcurrencySupported

This patch uses `SystemUtil.runShellCommand` to toggle Wi-Fi off/on to
satisfy checks in
`HalDeviceManager.allowedToDeleteForNoStaApConcurrencyLohs`.

Test: run cts-on-gsi -m CtsWifiTestCases -t android.net.wifi.cts.WifiManagerTest
Bug: 299873419
Merged-In: I65498ae4161f9daf68b8ac923a6242f2139719ba
Change-Id: I65498ae4161f9daf68b8ac923a6242f2139719ba
(cherry picked from commit db286a7ab8ec009f0dec8986d4c62cf4ac7442ad)
diff --git a/tests/tests/wifi/src/android/net/wifi/cts/WifiManagerTest.java b/tests/tests/wifi/src/android/net/wifi/cts/WifiManagerTest.java
index c85f06c..11b8966 100644
--- a/tests/tests/wifi/src/android/net/wifi/cts/WifiManagerTest.java
+++ b/tests/tests/wifi/src/android/net/wifi/cts/WifiManagerTest.java
@@ -110,6 +110,7 @@
 import com.android.compatibility.common.util.PollingCheck;
 import com.android.compatibility.common.util.PropertyUtil;
 import com.android.compatibility.common.util.ShellIdentityUtils;
+import com.android.compatibility.common.util.SystemUtil;
 import com.android.compatibility.common.util.ThrowingRunnable;
 import com.android.modules.utils.build.SdkLevel;
 import com.android.net.module.util.MacAddressUtils;
@@ -1150,7 +1151,14 @@
         }
 
         boolean wifiEnabled = mWifiManager.isWifiEnabled();
-
+        if (wifiEnabled) {
+            // Re-enabled Wi-Fi as shell for HalDeviceManager legacy LOHS behavior when there's no
+            // STA+AP concurrency.
+            ShellIdentityUtils.invokeWithShellPermissions(() -> mWifiManager.setWifiEnabled(false));
+            PollingCheck.check("Wifi turn off failed!", 2_000, () -> !mWifiManager.isWifiEnabled());
+            SystemUtil.runShellCommand("cmd wifi set-wifi-enabled enabled");
+            PollingCheck.check("Wifi turn on failed!", 2_000, () -> mWifiManager.isWifiEnabled());
+        }
         TestLocalOnlyHotspotCallback callback = startLocalOnlyHotspot();
 
         // add sleep to avoid calling stopLocalOnlyHotspot before TetherController initialization.
@@ -1998,6 +2006,14 @@
         boolean caughtException = false;
 
         boolean wifiEnabled = mWifiManager.isWifiEnabled();
+        if (wifiEnabled) {
+            // Re-enabled Wi-Fi as shell for HalDeviceManager legacy LOHS behavior when there's no
+            // STA+AP concurrency.
+            ShellIdentityUtils.invokeWithShellPermissions(() -> mWifiManager.setWifiEnabled(false));
+            PollingCheck.check("Wifi turn off failed!", 2_000, () -> !mWifiManager.isWifiEnabled());
+            SystemUtil.runShellCommand("cmd wifi set-wifi-enabled enabled");
+            PollingCheck.check("Wifi turn on failed!", 2_000, () -> mWifiManager.isWifiEnabled());
+        }
 
         TestLocalOnlyHotspotCallback callback = startLocalOnlyHotspot();
 
@@ -3812,7 +3828,7 @@
     }
 
     /**
-     * Tests {@link WifiManager#isStaApConcurrencySupported().
+     * Tests {@link WifiManager#isStaApConcurrencySupported()}.
      */
     public void testIsStaApConcurrencySupported() throws Exception {
         if (!WifiFeature.isWifiSupported(getContext())) {
@@ -3825,6 +3841,13 @@
         }
         assertTrue(mWifiManager.isWifiEnabled());
 
+        // Re-enabled Wi-Fi as shell for HalDeviceManager legacy LOHS behavior when there's no
+        // STA+AP concurrency.
+        ShellIdentityUtils.invokeWithShellPermissions(() -> mWifiManager.setWifiEnabled(false));
+        PollingCheck.check("Wifi turn off failed!", 2_000, () -> !mWifiManager.isWifiEnabled());
+        SystemUtil.runShellCommand("cmd wifi set-wifi-enabled enabled");
+        PollingCheck.check("Wifi turn on failed!", 2_000, () -> mWifiManager.isWifiEnabled());
+
         boolean isStaApConcurrencySupported = mWifiManager.isStaApConcurrencySupported();
         // start local only hotspot.
         TestLocalOnlyHotspotCallback callback = startLocalOnlyHotspot();