Disable tethering to enable Wifi

We need to explicitly disable wifi while enabling
tethering and disable tethering while enabling wifi

Bug: 2539071
Change-Id: I7fda6e4d9d1bb804e81561d52b5f3a982a674b0e
diff --git a/src/com/android/settings/widget/SettingsAppWidgetProvider.java b/src/com/android/settings/widget/SettingsAppWidgetProvider.java
index 2fd618c..872d303 100644
--- a/src/com/android/settings/widget/SettingsAppWidgetProvider.java
+++ b/src/com/android/settings/widget/SettingsAppWidgetProvider.java
@@ -248,6 +248,15 @@
             if (withToast) {
                 Toast.makeText(context, R.string.gadget_toggle_wifi, Toast.LENGTH_SHORT).show();
             }
+            /**
+             * Disable tethering if enabling Wifi
+             */
+            int wifiApState = wifiManager.getWifiApState();
+            if (desiredState && ((wifiApState == WifiManager.WIFI_AP_STATE_ENABLING) ||
+                        (wifiApState == WifiManager.WIFI_AP_STATE_ENABLED))) {
+                wifiManager.setWifiApEnabled(null, false);
+            }
+
             wifiManager.setWifiEnabled(desiredState);
         }
 
diff --git a/src/com/android/settings/wifi/WifiApEnabler.java b/src/com/android/settings/wifi/WifiApEnabler.java
index 3d8d282..c2f8395 100644
--- a/src/com/android/settings/wifi/WifiApEnabler.java
+++ b/src/com/android/settings/wifi/WifiApEnabler.java
@@ -97,6 +97,15 @@
 
     public boolean onPreferenceChange(Preference preference, Object enable) {
 
+        /**
+         * Disable Wifi if enabling tethering
+         */
+        int wifiState = mWifiManager.getWifiState();
+        if ((Boolean)enable && ((wifiState == WifiManager.WIFI_STATE_ENABLING) ||
+                    (wifiState == WifiManager.WIFI_STATE_ENABLED))) {
+            mWifiManager.setWifiEnabled(false);
+        }
+
         if (mWifiManager.setWifiApEnabled(null, (Boolean)enable)) {
             /* Disable here, enabled on receiving success broadcast */
             mCheckBox.setEnabled(false);
diff --git a/src/com/android/settings/wifi/WifiEnabler.java b/src/com/android/settings/wifi/WifiEnabler.java
index 6c55136..ef9f346 100644
--- a/src/com/android/settings/wifi/WifiEnabler.java
+++ b/src/com/android/settings/wifi/WifiEnabler.java
@@ -92,6 +92,14 @@
             return false;
         }
 
+        /**
+         * Disable tethering if enabling Wifi
+         */
+        int wifiApState = mWifiManager.getWifiApState();
+        if (enable && ((wifiApState == WifiManager.WIFI_AP_STATE_ENABLING) ||
+                (wifiApState == WifiManager.WIFI_AP_STATE_ENABLED))) {
+            mWifiManager.setWifiApEnabled(null, false);
+        }
         if (mWifiManager.setWifiEnabled(enable)) {
             mCheckBox.setEnabled(false);
         } else {