replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / android / android_api / base / src / main / java / org / iotivity / ca / CaIpInterface.java
1 /******************************************************************
2  *
3  * Copyright 2014 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  ******************************************************************/
20
21 package org.iotivity.ca;
22
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;
31
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";
36
37     private CaIpInterface(Context context) {
38         synchronized(CaIpInterface.class) {
39             mContext = context;
40         }
41         if (!isIpInitialized) {
42             registerIpStateReceiver();
43             isIpInitialized = true;
44         }
45     }
46
47     private void registerIpStateReceiver() {
48         IntentFilter intentFilter = new IntentFilter();
49         intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
50         intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
51
52         mContext.registerReceiver(mReceiver, intentFilter);
53     }
54
55     public static void destroyIpInterface() {
56         if (isIpInitialized) {
57             mContext.unregisterReceiver(mReceiver);
58             isIpInitialized = false;
59         }
60     }
61
62     private static BroadcastReceiver mReceiver = new BroadcastReceiver() {
63         @Override
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());
73                     caIpStateEnabled();
74                 } else {
75                     Log.d(TAG, "CONNECTIVITY_ACTION - activeNetwork: NONE");
76                     caIpStateDisabled();
77                 }
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");
85                         caIpStateEnabled();
86                     } else if (NetworkInfo.State.DISCONNECTED == netState) {
87                         Log.d(TAG, "NETWORK_STATE_CHANGED_ACTION - DISCONNECTED: TYPE_WIFI");
88                     }
89                 }
90             }
91         }
92     };
93
94     private native static void caIpStateEnabled();
95
96     private native static void caIpStateDisabled();
97 }