summaryrefslogtreecommitdiff
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
commit9e22aa6aff4627a08cb23f649e323f2b42f0d34d (patch)
tree7853c4afaa5d49815073031fa1a68cd244f1bf59
parent4ddf2ac169fbb4931b5268962b02cdca33323ccd (diff)
Implement reconnect on network change (closes issue #37)
-rw-r--r--AndroidManifest.xml1
-rw-r--r--res/values-de/strings.xml4
-rw-r--r--res/values/strings.xml2
-rw-r--r--res/xml/general_settings.xml9
-rw-r--r--src/de/blinkt/openvpn/LaunchVPN.java2
-rw-r--r--src/de/blinkt/openvpn/NetworkSateReceiver.java59
-rw-r--r--src/de/blinkt/openvpn/OpenVpnManagementThread.java6
-rw-r--r--src/de/blinkt/openvpn/OpenVpnService.java18
8 files changed, 96 insertions, 5 deletions
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index 7adbf359..456f47dc 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -21,6 +21,7 @@
android:versionName="0.5.8" >
<uses-permission android:name="android.permission.INTERNET" />
+ <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-sdk android:minSdkVersion="14" />
diff --git a/res/values-de/strings.xml b/res/values-de/strings.xml
index 97adb922..be0a518d 100644
--- a/res/values-de/strings.xml
+++ b/res/values-de/strings.xml
@@ -164,10 +164,12 @@
<string name="generalsettings">Allgemeine Einstellungen</string>
<string name="no_vpn_support_image">Dieses Android ROM enthält keine VPNService API, sorry :(</string>
<string name="broken_image_cert_title">Fehler beim Anzeigen des Zertifikatsauswahlbildschirmes</string>
- <string name="owner_fix_summary">Versuch den Eigentümer von /dev/tun auf system zu ändern. Einige Cyanogenmod 9 ROM Versionen benötigen diese Option, damit die VPN API funktioniert. Benötigt root.</string>
+ <string name="owner_fix_summary">Versucht den Eigentümer von /dev/tun auf system zu ändern. Einige Cyanogenmod 9 ROM Versionen benötigen diese Option, damit die VPN API funktioniert. Benötigt root.</string>
<string name="owner_fix">/dev/tun Eigentümer setzen.</string>
<string name="generated_config_summary">Zeigt die generierte Konfigurationsdatei</string>
<string name="edit_profile_title">Editiere \"%s\"</string>
<string name="building_configration">Generiere OpenVPN Konfigration…</string>
+ <string name="netchange_summary">Aktivieren dieser Option zwingt OpenVPN dazu beim Wechsel des Netzwerkes (WLAN zu Mobilfunk und umgekehrt) neu zu verbinden.</string>
+ <string name="netchange">Netzwerkänderungen beachten</string>
</resources> \ No newline at end of file
diff --git a/res/values/strings.xml b/res/values/strings.xml
index 42749081..76d71ce3 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -207,4 +207,6 @@
<string name="generated_config_summary">Shows the generated openvpn Configuration File</string>
<string name="edit_profile_title">Editing \"%s\"</string>
<string name="building_configration">Building configration…</string>
+ <string name="netchange_summary">Turning this option on will force a reconnet if the network state is change (WIFI to/from mobile)</string>
+ <string name="netchange">Reconnect on Network change</string>
</resources>
diff --git a/res/xml/general_settings.xml b/res/xml/general_settings.xml
index ecfad493..74c8965f 100644
--- a/res/xml/general_settings.xml
+++ b/res/xml/general_settings.xml
@@ -2,9 +2,14 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<CheckBoxPreference
+ android:defaultValue="true"
+ android:key="netchangereconnect"
+ android:summary="@string/netchange_summary"
+ android:title="@string/netchange" />
+ <CheckBoxPreference
+ android:defaultValue="false"
android:key="useCM9Fix"
android:summary="@string/owner_fix_summary"
- android:title="@string/owner_fix"
- android:defaultValue="false" />
+ android:title="@string/owner_fix" />
</PreferenceScreen> \ No newline at end of file
diff --git a/src/de/blinkt/openvpn/LaunchVPN.java b/src/de/blinkt/openvpn/LaunchVPN.java
index 4b21c959..7b0f6e04 100644
--- a/src/de/blinkt/openvpn/LaunchVPN.java
+++ b/src/de/blinkt/openvpn/LaunchVPN.java
@@ -381,7 +381,7 @@ public class LaunchVPN extends ListActivity implements OnItemClickListener {
OpenVPN.logMessage(0, "", "Error writing minivpn binary");
return;
}
- OpenVPN.logMessage(0, "", "Building configration...");
+ OpenVPN.logMessage(0, "", getString(R.string.building_configration));
Intent startVPN = mSelectedProfile.prepareIntent(getBaseContext());
diff --git a/src/de/blinkt/openvpn/NetworkSateReceiver.java b/src/de/blinkt/openvpn/NetworkSateReceiver.java
new file mode 100644
index 00000000..77e25acf
--- /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;
+ }
+
+}
diff --git a/src/de/blinkt/openvpn/OpenVpnManagementThread.java b/src/de/blinkt/openvpn/OpenVpnManagementThread.java
index 6d67a4e6..723cc4c8 100644
--- a/src/de/blinkt/openvpn/OpenVpnManagementThread.java
+++ b/src/de/blinkt/openvpn/OpenVpnManagementThread.java
@@ -383,4 +383,10 @@ public class OpenVpnManagementThread implements Runnable {
return sendCMD;
}
+
+ public void reconnect() {
+ managmentCommand("signal SIGUSR1\n");
+
+ }
+
}
diff --git a/src/de/blinkt/openvpn/OpenVpnService.java b/src/de/blinkt/openvpn/OpenVpnService.java
index 2b44fe03..8c172115 100644
--- a/src/de/blinkt/openvpn/OpenVpnService.java
+++ b/src/de/blinkt/openvpn/OpenVpnService.java
@@ -24,6 +24,8 @@ import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
+import android.content.IntentFilter;
+import android.net.ConnectivityManager;
import android.net.LocalSocket;
import android.net.LocalSocketAddress;
import android.net.VpnService;
@@ -50,6 +52,8 @@ public class OpenVpnService extends VpnService {
private String mLocalIPv6=null;
private Notification mNotification=null;
+ private NetworkSateReceiver mNetworkStateReceiver;
+
private static final int HELLO_ID = 1;
@Override
@@ -112,6 +116,13 @@ public class OpenVpnService extends VpnService {
return sock;
}
+
+ void registerNetworkStateReceiver() {
+ // Registers BroadcastReceiver to track network connection changes.
+ IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
+ mNetworkStateReceiver = new NetworkSateReceiver(mSocketManager);
+ this.registerReceiver(mNetworkStateReceiver, filter);
+ }
@Override
@@ -161,7 +172,8 @@ public class OpenVpnService extends VpnService {
mServiceThread = new Thread(serviceThread, "OpenVPNServiceThread");
mServiceThread.start();
-
+
+
// Open the Management Interface
mgmtsocket = openManagmentInterface(8);
@@ -170,6 +182,7 @@ public class OpenVpnService extends VpnService {
mSocketManager = new OpenVpnManagementThread(mProfile,mgmtsocket,this);
mSocketManagerThread = new Thread(mSocketManager,"OpenVPNMgmtThread");
mSocketManagerThread.start();
+ registerNetworkStateReceiver();
}
return START_NOT_STICKY;
@@ -182,6 +195,9 @@ public class OpenVpnService extends VpnService {
mServiceThread.interrupt();
}
+ if (mNetworkStateReceiver!= null) {
+ this.unregisterReceiver(mNetworkStateReceiver);
+ }
}