Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / resource / csdk / connectivity / samples / android / casample / cAInterface / 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
31 public class CaIpInterface {
32     private static Context mContext;
33
34     private CaIpInterface(Context context) {
35         mContext = context;
36         registerIpStateReceiver();
37     }
38
39     private void registerIpStateReceiver() {
40         IntentFilter intentFilter = new IntentFilter();
41         intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
42         intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
43
44         mContext.registerReceiver(mReceiver, intentFilter);
45     }
46
47     private static BroadcastReceiver mReceiver = new BroadcastReceiver() {
48         @Override
49         public void onReceive(Context context, Intent intent) {
50             if (intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
51                 WifiManager.WIFI_STATE_UNKNOWN) == WifiManager.WIFI_STATE_DISABLED) {
52                 caIpStateDisabled();
53             } else if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
54                 ConnectivityManager manager = (ConnectivityManager)
55                         mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
56                 NetworkInfo nwInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
57
58                 if(nwInfo.isConnected()) {
59                     caIpStateEnabled();
60                 }
61             }
62         }
63     };
64
65     private native static void caIpStateEnabled();
66
67     private native static void caIpStateDisabled();
68 }