Move status update to WifiApEnabler

Bug: 2528202
Change-Id: I093699ac171279af9df5f3b3b4ae359aab79088b
diff --git a/src/com/android/settings/TetherSettings.java b/src/com/android/settings/TetherSettings.java
index 5770482..4095e86 100644
--- a/src/com/android/settings/TetherSettings.java
+++ b/src/com/android/settings/TetherSettings.java
@@ -26,8 +26,6 @@
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.net.ConnectivityManager;
-import android.net.wifi.WifiManager;
-import android.net.wifi.WifiConfiguration;
 import android.os.Environment;
 import android.preference.CheckBoxPreference;
 import android.preference.Preference;
@@ -56,7 +54,6 @@
     private PreferenceScreen mWifiApSettings;
     private WifiApEnabler mWifiApEnabler;
     private PreferenceScreen mTetherHelp;
-    private WifiManager mWifiManager;
 
     private BroadcastReceiver mTetherChangeReceiver;
 
@@ -64,7 +61,6 @@
     private ArrayList mUsbIfaces;
 
     private String[] mWifiRegexs;
-    private ArrayList mWifiIfaces;
 
     @Override
     protected void onCreate(Bundle icicle) {
@@ -79,7 +75,6 @@
 
         ConnectivityManager cm =
                 (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
-        mWifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
 
         mUsbRegexs = cm.getTetherableUsbRegexs();
         if (mUsbRegexs.length == 0) {
@@ -157,12 +152,8 @@
         boolean usbAvailable = false;
         int usbError = ConnectivityManager.TETHER_ERROR_NO_ERROR;
         boolean usbErrored = false;
-        boolean wifiTethered = false;
-        boolean wifiAvailable = false;
-        int wifiError = ConnectivityManager.TETHER_ERROR_NO_ERROR;
         boolean massStorageActive =
                 Environment.MEDIA_SHARED.equals(Environment.getExternalStorageState());
-        boolean wifiErrored = false;
         for (Object o : available) {
             String s = (String)o;
             for (String regex : mUsbRegexs) {
@@ -173,32 +164,18 @@
                     }
                 }
             }
-            for (String regex : mWifiRegexs) {
-                if (s.matches(regex)) {
-                    wifiAvailable = true;
-                    if (wifiError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
-                        wifiError = cm.getLastTetherError(s);
-                    }
-                }
-            }
         }
         for (Object o : tethered) {
             String s = (String)o;
             for (String regex : mUsbRegexs) {
                 if (s.matches(regex)) usbTethered = true;
             }
-            for (String regex : mWifiRegexs) {
-                if (s.matches(regex)) wifiTethered = true;
-            }
         }
         for (Object o: errored) {
             String s = (String)o;
             for (String regex : mUsbRegexs) {
                 if (s.matches(regex)) usbErrored = true;
             }
-            for (String regex : mWifiRegexs) {
-                if (s.matches(regex)) wifiErrored = true;
-            }
         }
 
         if (usbTethered) {
@@ -226,17 +203,6 @@
             mUsbTether.setEnabled(false);
             mUsbTether.setChecked(false);
         }
-
-        if (wifiTethered) {
-            WifiConfiguration mWifiConfig = mWifiManager.getWifiApConfiguration();
-            String s = getString(com.android.internal.R.string.wifi_tether_configure_ssid_default);
-            mEnableWifiAp.setSummary(String.format(getString(R.string.wifi_tether_enabled_subtext),
-                                                   (mWifiConfig == null) ? s : mWifiConfig.SSID));
-        }
-
-        if (wifiErrored) {
-            mEnableWifiAp.setSummary(R.string.wifi_error);
-        }
     }
 
     @Override
diff --git a/src/com/android/settings/wifi/WifiApEnabler.java b/src/com/android/settings/wifi/WifiApEnabler.java
index fc52a7a..6049623 100644
--- a/src/com/android/settings/wifi/WifiApEnabler.java
+++ b/src/com/android/settings/wifi/WifiApEnabler.java
@@ -19,13 +19,17 @@
 import com.android.settings.R;
 import com.android.settings.WirelessSettings;
 
+import java.util.ArrayList;
+
 import android.app.AlertDialog;
 import android.content.BroadcastReceiver;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
+import android.net.ConnectivityManager;
 import android.net.NetworkInfo;
 import android.net.wifi.SupplicantState;
+import android.net.wifi.WifiConfiguration;
 import android.net.wifi.WifiInfo;
 import android.net.wifi.WifiManager;
 import android.preference.Preference;
@@ -43,6 +47,9 @@
     private WifiManager mWifiManager;
     private final IntentFilter mIntentFilter;
 
+    ConnectivityManager mCm;
+    private String[] mWifiRegexs;
+
     private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {
@@ -50,7 +57,16 @@
             if (WifiManager.WIFI_AP_STATE_CHANGED_ACTION.equals(action)) {
                 handleWifiApStateChanged(intent.getIntExtra(
                         WifiManager.EXTRA_WIFI_AP_STATE, WifiManager.WIFI_AP_STATE_FAILED));
+            } else if (ConnectivityManager.ACTION_TETHER_STATE_CHANGED.equals(action)) {
+                ArrayList<String> available = intent.getStringArrayListExtra(
+                        ConnectivityManager.EXTRA_AVAILABLE_TETHER);
+                ArrayList<String> active = intent.getStringArrayListExtra(
+                        ConnectivityManager.EXTRA_ACTIVE_TETHER);
+                ArrayList<String> errored = intent.getStringArrayListExtra(
+                        ConnectivityManager.EXTRA_ERRORED_TETHER);
+                updateTetherState(available.toArray(), active.toArray(), errored.toArray());
             }
+
         }
     };
 
@@ -61,7 +77,12 @@
         checkBox.setPersistent(false);
 
         mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
+        mCm = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
+
+        mWifiRegexs = mCm.getTetherableWifiRegexs();
+
         mIntentFilter = new IntentFilter(WifiManager.WIFI_AP_STATE_CHANGED_ACTION);
+        mIntentFilter.addAction(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
     }
 
     public void resume() {
@@ -86,6 +107,34 @@
         return false;
     }
 
+    private void updateTetherState(Object[] available, Object[] tethered, Object[] errored) {
+        boolean wifiTethered = false;
+        boolean wifiErrored = false;
+
+        for (Object o : tethered) {
+            String s = (String)o;
+            for (String regex : mWifiRegexs) {
+                if (s.matches(regex)) wifiTethered = true;
+            }
+        }
+        for (Object o: errored) {
+            String s = (String)o;
+            for (String regex : mWifiRegexs) {
+                if (s.matches(regex)) wifiErrored = true;
+            }
+        }
+
+        if (wifiTethered) {
+            WifiConfiguration mWifiConfig = mWifiManager.getWifiApConfiguration();
+            String s = mContext.getString(
+                    com.android.internal.R.string.wifi_tether_configure_ssid_default);
+            mCheckBox.setSummary(String.format(
+                    mContext.getString(R.string.wifi_tether_enabled_subtext),
+                    (mWifiConfig == null) ? s : mWifiConfig.SSID));
+        } else if (wifiErrored) {
+            mCheckBox.setSummary(R.string.wifi_error);
+        }
+    }
 
     private void handleWifiApStateChanged(int state) {
         switch (state) {