From 9288f9cdcb85f00918614595a1b81775a8d9e607 Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Wed, 29 May 2013 19:52:41 +0200 Subject: Allow pausing the VPN also without persistent-tun. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I don't like the concept but I don't to educate people …. --- res/values/strings.xml | 4 +- .../blinkt/openvpn/core/DeviceStateReceiver.java | 45 +++++++++++----------- 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/res/values/strings.xml b/res/values/strings.xml index db6305e3..b7589271 100755 --- a/res/values/strings.xml +++ b/res/values/strings.xml @@ -278,9 +278,9 @@ No app allowed to use external API Allowed apps: %s Clear list of allowed external apps?\nCurrent list of allowed apps:\n\n%s - "Pause VPN when screen is off and less than 64 kB transferred data in 60s. Pausing the VPN the is only enabled if the \"Persistent Tun\" option is enabled on a VPN. When the VPN is paused your device has NO network connectivity." + "Pause VPN when screen is off and less than 64 kB transferred data in 60s. When the \"Persistent Tun\" option is enabled pausing the VPN will leave your device with NO network connectivity. Without the \"Persistent Tun\" option the device will have no VPN connection/protection. Pause VPN connection after screen off Pausing connection in screen off state: less than %1$s in %2$ss - ERROR: Persistent tun not enabled for this VPN. Ignoring option to pause VPN when screen is off + Warning: Persistent tun not enabled for this VPN. Traffic will use the normal Internet connection when the screen is off. \ No newline at end of file diff --git a/src/de/blinkt/openvpn/core/DeviceStateReceiver.java b/src/de/blinkt/openvpn/core/DeviceStateReceiver.java index 11096f48..479860bc 100644 --- a/src/de/blinkt/openvpn/core/DeviceStateReceiver.java +++ b/src/de/blinkt/openvpn/core/DeviceStateReceiver.java @@ -68,12 +68,12 @@ public class DeviceStateReceiver extends BroadcastReceiver implements ByteCountL if(windowtraffic < TRAFFIC_LIMIT) { screen = connectState.DISCONNECTED; - OpenVPN.logInfo(R.string.screenoff_pause, + OpenVPN.logInfo(R.string.screenoff_pause, OpenVpnService.humanReadableByteCount(TRAFFIC_LIMIT, false), TRAFFIC_WINDOW); mManangement.pause(); } Log.i("OpenVPN", String.format("State: %s %s total %d last %d time %d",network.name(), screen.name(),windowtraffic/1024, - total, + total, System.currentTimeMillis()/1024)); } @@ -88,25 +88,26 @@ public class DeviceStateReceiver extends BroadcastReceiver implements ByteCountL @Override public void onReceive(Context context, Intent intent) { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); - - - if(ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) { - networkStateChange(context); - } else if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) { - boolean screenoff = prefs.getBoolean("screenoff", false); - - if(screenoff && ! ProfileManager.getLastConnectedVpn().mPersistTun) { - OpenVPN.logError(R.string.screen_nopersistenttun); - } else if(screenoff) { - screen = connectState.PENDINGDISCONNECT; - fillTrafficData(); - if (network == connectState.DISCONNECTED) - screen = connectState.DISCONNECTED; - } + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + + + if(ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) { + networkStateChange(context); + } else if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) { + boolean screenoff = prefs.getBoolean("screenoff", false); + + if(screenoff) + if(!ProfileManager.getLastConnectedVpn().mPersistTun) + OpenVPN.logError(R.string.screen_nopersistenttun); + + screen = connectState.PENDINGDISCONNECT; + fillTrafficData(); + if (network == connectState.DISCONNECTED) + screen = connectState.DISCONNECTED; + } } else if (Intent.ACTION_SCREEN_ON.equals(intent.getAction())) { - // Network was disabled because screen off + // Network was disabled because screen off if (screen == connectState.DISCONNECTED && network == connectState.SHOULDBECONNECTED) { mManangement.resume(); @@ -123,7 +124,7 @@ public class DeviceStateReceiver extends BroadcastReceiver implements ByteCountL public void networkStateChange(Context context) { NetworkInfo networkInfo = getCurrentNetworkInfo(context); - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); boolean sendusr1 = prefs.getBoolean("netchangereconnect", true); @@ -132,7 +133,7 @@ public class DeviceStateReceiver extends BroadcastReceiver implements ByteCountL netstatestring = "not connected"; else { String subtype = networkInfo.getSubtypeName(); - if(subtype==null) + if(subtype==null) subtype = ""; String extrainfo = networkInfo.getExtraInfo(); if(extrainfo==null) @@ -140,7 +141,7 @@ public class DeviceStateReceiver extends BroadcastReceiver implements ByteCountL /* if(networkInfo.getType()==android.net.ConnectivityManager.TYPE_WIFI) { - WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); + WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiinfo = wifiMgr.getConnectionInfo(); extrainfo+=wifiinfo.getBSSID(); -- cgit v1.2.3