Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / net / android / java / src / org / chromium / net / NetworkChangeNotifierAutoDetect.java
index 53bb1ec..981e3dc 100644 (file)
@@ -9,6 +9,8 @@ import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
 import android.net.ConnectivityManager;
+import android.net.wifi.WifiInfo;
+import android.net.wifi.WifiManager;
 import android.telephony.TelephonyManager;
 import android.util.Log;
 
@@ -54,6 +56,29 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
         }
     }
 
+    /** Queries the WifiManager for SSID of the current Wifi connection. */
+    static class WifiManagerDelegate {
+        private final WifiManager mWifiManager;
+
+        WifiManagerDelegate(Context context) {
+            mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
+        }
+
+        // For testing.
+        WifiManagerDelegate() {
+            // All the methods below should be overridden.
+            mWifiManager = null;
+        }
+
+        String getWifiSSID() {
+            WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
+            if (wifiInfo == null)
+                return "";
+            String ssid = wifiInfo.getSSID();
+            return ssid == null ? "" : ssid;
+        }
+    }
+
     private static final String TAG = "NetworkChangeNotifierAutoDetect";
 
     private final NetworkConnectivityIntentFilter mIntentFilter =
@@ -63,8 +88,10 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
 
     private final Context mContext;
     private ConnectivityManagerDelegate mConnectivityManagerDelegate;
+    private WifiManagerDelegate mWifiManagerDelegate;
     private boolean mRegistered;
     private int mConnectionType;
+    private String mWifiSSID;
 
     /**
      * Observer notified on the UI thread whenever a new connection type was detected.
@@ -77,7 +104,9 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
         mObserver = observer;
         mContext = context.getApplicationContext();
         mConnectivityManagerDelegate = new ConnectivityManagerDelegate(context);
+        mWifiManagerDelegate = new WifiManagerDelegate(context);
         mConnectionType = getCurrentConnectionType();
+        mWifiSSID = getCurrentWifiSSID();
         ActivityStatus.registerStateListener(this);
     }
 
@@ -88,6 +117,13 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
         mConnectivityManagerDelegate = delegate;
     }
 
+    /**
+     * Allows overriding the WifiManagerDelegate for tests.
+     */
+    void setWifiManagerDelegateForTests(WifiManagerDelegate delegate) {
+        mWifiManagerDelegate = delegate;
+    }
+
     public void destroy() {
         unregisterReceiver();
     }
@@ -155,6 +191,12 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
         }
     }
 
+    private String getCurrentWifiSSID() {
+        if (getCurrentConnectionType() != NetworkChangeNotifier.CONNECTION_WIFI)
+            return "";
+        return mWifiManagerDelegate.getWifiSSID();
+    }
+
     // BroadcastReceiver
     @Override
     public void onReceive(Context context, Intent intent) {
@@ -180,9 +222,12 @@ public class NetworkChangeNotifierAutoDetect extends BroadcastReceiver
 
     private void connectionTypeChanged() {
         int newConnectionType = getCurrentConnectionType();
-        if (newConnectionType == mConnectionType) return;
+        String newWifiSSID = getCurrentWifiSSID();
+        if (newConnectionType == mConnectionType && newWifiSSID.equals(mWifiSSID))
+            return;
 
         mConnectionType = newConnectionType;
+        mWifiSSID = newWifiSSID;
         Log.d(TAG, "Network connectivity changed, type is: " + mConnectionType);
         mObserver.onConnectionTypeChanged(newConnectionType);
     }