X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=android%2Fandroid_api%2Fbase%2Fsrc%2Fmain%2Fjava%2Forg%2Fiotivity%2Fca%2FCaIpInterface.java;h=e2da3ec419e77c60842c27b51606b0256313370a;hb=3c093548382bb2542c87a67e6e5fa32552c29cb3;hp=1e807d149fda4779e8dc690b38f42c2d32c030df;hpb=6e44d2470f553bccdde26e95cc8cfc19efbcad27;p=platform%2Fupstream%2Fiotivity.git diff --git a/android/android_api/base/src/main/java/org/iotivity/ca/CaIpInterface.java b/android/android_api/base/src/main/java/org/iotivity/ca/CaIpInterface.java index 1e807d1..e2da3ec 100644 --- a/android/android_api/base/src/main/java/org/iotivity/ca/CaIpInterface.java +++ b/android/android_api/base/src/main/java/org/iotivity/ca/CaIpInterface.java @@ -1,68 +1,97 @@ -/****************************************************************** - * - * Copyright 2014 Samsung Electronics All Rights Reserved. - * - * - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - ******************************************************************/ - -package org.iotivity.ca; - -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.WifiManager; - -public class CaIpInterface { - private static Context mContext; - - private CaIpInterface(Context context) { - mContext = context; - registerIpStateReceiver(); - } - - private void registerIpStateReceiver() { - IntentFilter intentFilter = new IntentFilter(); - intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); - intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); - - mContext.registerReceiver(mReceiver, intentFilter); - } - - 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()) { - caIpStateEnabled(); - } - } - } - }; - - private native static void caIpStateEnabled(); - - private native static void caIpStateDisabled(); -} +/****************************************************************** + * + * Copyright 2014 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ******************************************************************/ + +package org.iotivity.ca; + +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.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) { + synchronized(CaIpInterface.class) { + mContext = context; + } + if (!isIpInitialized) { + registerIpStateReceiver(); + isIpInitialized = true; + } + } + + private void registerIpStateReceiver() { + IntentFilter intentFilter = new IntentFilter(); + 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) { + 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"); + } + } + } + } + }; + + private native static void caIpStateEnabled(); + + private native static void caIpStateDisabled(); +}