android: Add method to check for connectivity to NetworkManager
[strongswan.git] / src / frontends / android / src / org / strongswan / android / logic / NetworkManager.java
1 /*
2 * Copyright (C) 2012-2015 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 package org.strongswan.android.logic;
17
18 import android.content.BroadcastReceiver;
19 import android.content.Context;
20 import android.content.Intent;
21 import android.content.IntentFilter;
22 import android.net.ConnectivityManager;
23 import android.net.NetworkInfo;
24
25 public class NetworkManager extends BroadcastReceiver
26 {
27 private final Context mContext;
28 private boolean mRegistered;
29
30 public NetworkManager(Context context)
31 {
32 mContext = context;
33 }
34
35 public void Register()
36 {
37 mContext.registerReceiver(this, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
38 }
39
40 public void Unregister()
41 {
42 mContext.unregisterReceiver(this);
43 }
44
45 public boolean isConnected()
46 {
47 ConnectivityManager cm = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
48 NetworkInfo info = null;
49 if (cm != null)
50 {
51 info = cm.getActiveNetworkInfo();
52 }
53 return info != null && info.isConnected();
54 }
55
56 @Override
57 public void onReceive(Context context, Intent intent)
58 {
59 networkChanged(!isConnected());
60 }
61
62 /**
63 * Notify the native parts about a network change
64 *
65 * @param disconnected true if no connection is available at the moment
66 */
67 public native void networkChanged(boolean disconnected);
68 }