replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / android / android_api / base / src / main / java / org / iotivity / ca / CaIpInterface.java
index 040ebbd..e2da3ec 100644 (file)
@@ -27,36 +27,65 @@ import android.content.IntentFilter;
 import android.net.ConnectivityManager;
 import android.net.NetworkInfo;
 import android.net.wifi.WifiManager;
+import android.util.Log;
 
 public class CaIpInterface {
     private static Context mContext;
+    private static volatile boolean isIpInitialized = false;
+    private static String TAG          = "OIC_IP_CB_INTERFACE";
 
     private CaIpInterface(Context context) {
-        mContext = context;
-        registerIpStateReceiver();
+        synchronized(CaIpInterface.class) {
+            mContext = context;
+        }
+        if (!isIpInitialized) {
+            registerIpStateReceiver();
+            isIpInitialized = true;
+        }
     }
 
     private void registerIpStateReceiver() {
         IntentFilter intentFilter = new IntentFilter();
-        intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
         intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
+        intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
 
         mContext.registerReceiver(mReceiver, intentFilter);
     }
 
+    public static void destroyIpInterface() {
+        if (isIpInitialized) {
+            mContext.unregisterReceiver(mReceiver);
+            isIpInitialized = false;
+        }
+    }
+
     private static BroadcastReceiver mReceiver = new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {
-            if (intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
-                WifiManager.WIFI_STATE_UNKNOWN) == WifiManager.WIFI_STATE_DISABLED) {
-                caIpStateDisabled();
-            } else if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
-                ConnectivityManager manager = (ConnectivityManager)
-                        mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
-                NetworkInfo nwInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
-
-                if(nwInfo.isConnected()) {
+            String action = intent.getAction();
+            if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
+                NetworkInfo activeNetwork = ((ConnectivityManager) mContext
+                        .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
+                if (activeNetwork != null) {
+                    Log.d(TAG, "CONNECTIVITY_ACTION - activeNetwork: "
+                        + activeNetwork.getTypeName()
+                        + " isConnected: " + activeNetwork.isConnected());
                     caIpStateEnabled();
+                } else {
+                    Log.d(TAG, "CONNECTIVITY_ACTION - activeNetwork: NONE");
+                    caIpStateDisabled();
+                }
+            } else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
+                NetworkInfo netInfo = (NetworkInfo) intent
+                        .getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
+                if (ConnectivityManager.TYPE_WIFI == netInfo.getType()) {
+                    NetworkInfo.State netState = netInfo.getState();
+                    if (NetworkInfo.State.CONNECTED == netState) {
+                        Log.d(TAG, "NETWORK_STATE_CHANGED_ACTION - CONNECTED: TYPE_WIFI");
+                        caIpStateEnabled();
+                    } else if (NetworkInfo.State.DISCONNECTED == netState) {
+                        Log.d(TAG, "NETWORK_STATE_CHANGED_ACTION - DISCONNECTED: TYPE_WIFI");
+                    }
                 }
             }
         }