summaryrefslogtreecommitdiff
path: root/src/de/blinkt/openvpn/NetworkSateReceiver.java
diff options
context:
space:
mode:
authorArne Schwabe <arne@rfc2549.org>2012-06-03 14:57:16 +0200
committerArne Schwabe <arne@rfc2549.org>2012-06-03 14:57:16 +0200
commit12151bce34453ab1540c50a4dd143fb42028541d (patch)
treeb4824e9c96fe2a5302e31ddb8a1fa83eab6c2f50 /src/de/blinkt/openvpn/NetworkSateReceiver.java
parentec2008e9e53b99ba1725d8398e21b9cb04f6b8ca (diff)
Implement reconnect on network change (closes issue #37)
Diffstat (limited to 'src/de/blinkt/openvpn/NetworkSateReceiver.java')
-rw-r--r--src/de/blinkt/openvpn/NetworkSateReceiver.java59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/de/blinkt/openvpn/NetworkSateReceiver.java b/src/de/blinkt/openvpn/NetworkSateReceiver.java
new file mode 100644
index 0000000..77e25ac
--- /dev/null
+++ b/src/de/blinkt/openvpn/NetworkSateReceiver.java
@@ -0,0 +1,59 @@
+package de.blinkt.openvpn;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.SharedPreferences;
+import android.net.ConnectivityManager;
+import android.net.NetworkInfo;
+import android.net.NetworkInfo.State;
+import android.preference.PreferenceManager;
+
+public class NetworkSateReceiver extends BroadcastReceiver {
+ private int lastNetwork=-1;
+ private OpenVpnManagementThread mManangement;
+
+ private String lastStateMsg=null;
+
+ public NetworkSateReceiver(OpenVpnManagementThread managementThread) {
+ super();
+ mManangement = managementThread;
+ }
+
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ NetworkInfo networkInfo = getCurrentNetworkInfo(context);
+ SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
+ boolean sendusr1 = prefs.getBoolean("netchangereconnect", true);
+
+
+ String netstatestring;
+ if(networkInfo==null)
+ netstatestring = "null";
+ else
+ netstatestring = String.format("%2$s %4$s to %1$s %3$s",networkInfo.getTypeName(),
+ networkInfo.getDetailedState(),networkInfo.getExtraInfo(), networkInfo.getSubtypeName());
+
+ if(networkInfo!=null && networkInfo.getState() == State.CONNECTED) {
+ int newnet = networkInfo.getType();
+
+ if(sendusr1 && lastNetwork!=-1 && (lastNetwork!=newnet))
+ mManangement.reconnect();
+
+ lastNetwork = newnet;
+ }
+ if(!netstatestring.equals(lastStateMsg))
+ OpenVPN.logMessage(0, "Network:", netstatestring);
+ lastStateMsg=netstatestring;
+
+ }
+
+ private NetworkInfo getCurrentNetworkInfo(Context context) {
+ ConnectivityManager conn = (ConnectivityManager)
+ context.getSystemService(Context.CONNECTIVITY_SERVICE);
+
+ NetworkInfo networkInfo = conn.getActiveNetworkInfo();
+ return networkInfo;
+ }
+
+}