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;
}
}
+ /** 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 =
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.
mObserver = observer;
mContext = context.getApplicationContext();
mConnectivityManagerDelegate = new ConnectivityManagerDelegate(context);
+ mWifiManagerDelegate = new WifiManagerDelegate(context);
mConnectionType = getCurrentConnectionType();
+ mWifiSSID = getCurrentWifiSSID();
ActivityStatus.registerStateListener(this);
}
mConnectivityManagerDelegate = delegate;
}
+ /**
+ * Allows overriding the WifiManagerDelegate for tests.
+ */
+ void setWifiManagerDelegateForTests(WifiManagerDelegate delegate) {
+ mWifiManagerDelegate = delegate;
+ }
+
public void destroy() {
unregisterReceiver();
}
}
}
+ private String getCurrentWifiSSID() {
+ if (getCurrentConnectionType() != NetworkChangeNotifier.CONNECTION_WIFI)
+ return "";
+ return mWifiManagerDelegate.getWifiSSID();
+ }
+
// BroadcastReceiver
@Override
public void onReceive(Context context, Intent intent) {
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);
}