Change file mode.
[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     public static void destroyIpInterface() {
48         mContext.unregisterReceiver(mReceiver);
49     }
50
51     private static BroadcastReceiver mReceiver = new BroadcastReceiver() {
52         @Override
53         public void onReceive(Context context, Intent intent) {
54             if (intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
55                 WifiManager.WIFI_STATE_UNKNOWN) == WifiManager.WIFI_STATE_DISABLED) {
56                 caIpStateDisabled();
57             } else if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
58                 ConnectivityManager manager = (ConnectivityManager)
59                         mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
60                 NetworkInfo nwInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
61
62                 if(nwInfo.isConnected()) {
63                     caIpStateEnabled();
64                 }
65             }
66         }
67     };
68
69     private native static void caIpStateEnabled();
70
71     private native static void caIpStateDisabled();
72 }