Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / android / android_api / base / src / main / java / org / iotivity / ca / CaIpInterface.java
1 /******************************************************************\r
2  *\r
3  * Copyright 2014 Samsung Electronics All Rights Reserved.\r
4  *\r
5  *\r
6  *\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  *\r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  *\r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  *\r
19  ******************************************************************/\r
20 \r
21 package org.iotivity.ca;\r
22 \r
23 import android.content.BroadcastReceiver;\r
24 import android.content.Context;\r
25 import android.content.Intent;\r
26 import android.content.IntentFilter;\r
27 import android.net.ConnectivityManager;\r
28 import android.net.NetworkInfo;\r
29 import android.net.wifi.WifiManager;\r
30 \r
31 public class CaIpInterface {\r
32     private static Context mContext;\r
33 \r
34     private CaIpInterface(Context context) {\r
35         mContext = context;\r
36         registerIpStateReceiver();\r
37     }\r
38 \r
39     private void registerIpStateReceiver() {\r
40         IntentFilter intentFilter = new IntentFilter();\r
41         intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);\r
42         intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);\r
43 \r
44         mContext.registerReceiver(mReceiver, intentFilter);\r
45     }\r
46 \r
47     private static BroadcastReceiver mReceiver = new BroadcastReceiver() {\r
48         @Override\r
49         public void onReceive(Context context, Intent intent) {\r
50             if (intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,\r
51                 WifiManager.WIFI_STATE_UNKNOWN) == WifiManager.WIFI_STATE_DISABLED) {\r
52                 caIpStateDisabled();\r
53             } else if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {\r
54                 ConnectivityManager manager = (ConnectivityManager)\r
55                         mContext.getSystemService(Context.CONNECTIVITY_SERVICE);\r
56                 NetworkInfo nwInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);\r
57 \r
58                 if(nwInfo.isConnected()) {\r
59                     caIpStateEnabled();\r
60                 }\r
61             }\r
62         }\r
63     };\r
64 \r
65     private native static void caIpStateEnabled();\r
66 \r
67     private native static void caIpStateDisabled();\r
68 }\r