1 /******************************************************************
3 * Copyright 2014 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
21 package org.iotivity.ca;
23 import android.content.BroadcastReceiver;
24 import android.content.Context;
25 import android.content.Intent;
26 import android.content.IntentFilter;
27 import android.net.ConnectivityManager;
28 import android.net.NetworkInfo;
29 import android.net.wifi.WifiManager;
30 import android.util.Log;
32 public class CaIpInterface {
33 private static Context mContext;
34 private static volatile boolean isIpInitialized = false;
35 private static String TAG = "OIC_IP_CB_INTERFACE";
37 private CaIpInterface(Context context) {
38 synchronized(CaIpInterface.class) {
41 if (!isIpInitialized) {
42 registerIpStateReceiver();
43 isIpInitialized = true;
47 private void registerIpStateReceiver() {
48 IntentFilter intentFilter = new IntentFilter();
49 intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
50 intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
52 mContext.registerReceiver(mReceiver, intentFilter);
55 public static void destroyIpInterface() {
56 if (isIpInitialized) {
57 mContext.unregisterReceiver(mReceiver);
58 isIpInitialized = false;
62 private static BroadcastReceiver mReceiver = new BroadcastReceiver() {
64 public void onReceive(Context context, Intent intent) {
65 String action = intent.getAction();
66 if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
67 NetworkInfo activeNetwork = ((ConnectivityManager) mContext
68 .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
69 if (activeNetwork != null) {
70 Log.d(TAG, "CONNECTIVITY_ACTION - activeNetwork: "
71 + activeNetwork.getTypeName()
72 + " isConnected: " + activeNetwork.isConnected());
75 Log.d(TAG, "CONNECTIVITY_ACTION - activeNetwork: NONE");
78 } else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
79 NetworkInfo netInfo = (NetworkInfo) intent
80 .getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
81 if (ConnectivityManager.TYPE_WIFI == netInfo.getType()) {
82 NetworkInfo.State netState = netInfo.getState();
83 if (NetworkInfo.State.CONNECTED == netState) {
84 Log.d(TAG, "NETWORK_STATE_CHANGED_ACTION - CONNECTED: TYPE_WIFI");
86 } else if (NetworkInfo.State.DISCONNECTED == netState) {
87 Log.d(TAG, "NETWORK_STATE_CHANGED_ACTION - DISCONNECTED: TYPE_WIFI");
94 private native static void caIpStateEnabled();
96 private native static void caIpStateDisabled();