Fix automatic Wi-fi toggle

It is happening because there previous view doesn't disconnect
itself from setting change; and then two views alternatively
enable/disable wifi.

Bug: 17157005

Change-Id: I42916a7bbd735960a26efbae670c9b927ec8574d
diff --git a/src/com/android/settings/wifi/WifiEnabler.java b/src/com/android/settings/wifi/WifiEnabler.java
index ffc10a0..8426439 100644
--- a/src/com/android/settings/wifi/WifiEnabler.java
+++ b/src/com/android/settings/wifi/WifiEnabler.java
@@ -40,6 +40,7 @@
 public class WifiEnabler implements SwitchBar.OnSwitchChangeListener  {
     private Context mContext;
     private SwitchBar mSwitchBar;
+    private boolean mListeningToOnSwitchChange = false;
     private AtomicBoolean mConnected = new AtomicBoolean(false);
 
     private final WifiManager mWifiManager;
@@ -99,23 +100,41 @@
     public void setupSwitchBar() {
         final int state = mWifiManager.getWifiState();
         handleWifiStateChanged(state);
-        mSwitchBar.addOnSwitchChangeListener(this);
+        if (!mListeningToOnSwitchChange) {
+            mSwitchBar.addOnSwitchChangeListener(this);
+            mListeningToOnSwitchChange = true;
+        }
         mSwitchBar.show();
+
+        mContext.registerReceiver(mReceiver, mIntentFilter);
     }
 
     public void teardownSwitchBar() {
-        mSwitchBar.removeOnSwitchChangeListener(this);
+        if (mListeningToOnSwitchChange) {
+            mSwitchBar.removeOnSwitchChangeListener(this);
+            mListeningToOnSwitchChange = false;
+        }
         mSwitchBar.hide();
+
+        mContext.unregisterReceiver(mReceiver);
     }
 
     public void resume(Context context) {
         mContext = context;
         // Wi-Fi state is sticky, so just let the receiver update UI
         mContext.registerReceiver(mReceiver, mIntentFilter);
+        if (!mListeningToOnSwitchChange) {
+            mSwitchBar.addOnSwitchChangeListener(this);
+            mListeningToOnSwitchChange = true;
+        }
     }
 
     public void pause() {
         mContext.unregisterReceiver(mReceiver);
+        if (mListeningToOnSwitchChange) {
+            mSwitchBar.removeOnSwitchChangeListener(this);
+            mListeningToOnSwitchChange = false;
+        }
     }
 
     private void handleWifiStateChanged(int state) {