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