diff options
Diffstat (limited to 'main/src')
-rw-r--r-- | main/src/main/java/de/blinkt/openvpn/core/LollipopDeviceStateListener.java | 53 | ||||
-rw-r--r-- | main/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java | 30 |
2 files changed, 83 insertions, 0 deletions
diff --git a/main/src/main/java/de/blinkt/openvpn/core/LollipopDeviceStateListener.java b/main/src/main/java/de/blinkt/openvpn/core/LollipopDeviceStateListener.java new file mode 100644 index 00000000..945d6bd4 --- /dev/null +++ b/main/src/main/java/de/blinkt/openvpn/core/LollipopDeviceStateListener.java @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2012-2014 Arne Schwabe + * Distributed under the GNU GPL v2. For full terms see the file doc/LICENSE.txt + */ + +package de.blinkt.openvpn.core; + +import android.annotation.TargetApi; +import android.net.ConnectivityManager; +import android.net.LinkProperties; +import android.net.Network; +import android.net.NetworkCapabilities; +import android.os.Build; + +/** + * Created by arne on 26.11.14. + */ +@TargetApi(Build.VERSION_CODES.LOLLIPOP) +public class LollipopDeviceStateListener extends ConnectivityManager.NetworkCallback { + + private String mLastConnectedStatus; + private String mLastLinkProperties; + private String mLastNetworkCapabilities; + + @Override + public void onAvailable(Network network) { + super.onAvailable(network); + + if (!network.toString().equals(mLastConnectedStatus)) { + mLastConnectedStatus = network.toString(); + VpnStatus.logDebug("Connected to " + mLastConnectedStatus); + } + } + + @Override + public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) { + super.onLinkPropertiesChanged(network, linkProperties); + + if (!linkProperties.toString().equals(mLastLinkProperties)) { + mLastLinkProperties = linkProperties.toString(); + VpnStatus.logDebug(String.format("Linkproperties of %s: %s", network, linkProperties)); + } + } + + @Override + public void onCapabilitiesChanged(Network network, NetworkCapabilities networkCapabilities) { + super.onCapabilitiesChanged(network, networkCapabilities); + if (!networkCapabilities.toString().equals(mLastNetworkCapabilities)) { + mLastNetworkCapabilities = networkCapabilities.toString(); + VpnStatus.logDebug(String.format("Network capabilities of %s: %s", network, networkCapabilities)); + } + } +} diff --git a/main/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java b/main/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java index 037be5f2..ec668e4d 100644 --- a/main/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java +++ b/main/src/main/java/de/blinkt/openvpn/core/OpenVPNService.java @@ -16,6 +16,8 @@ import android.content.IntentFilter; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.net.ConnectivityManager; +import android.net.NetworkCapabilities; +import android.net.NetworkRequest; import android.net.VpnService; import android.os.Binder; import android.os.Build; @@ -79,6 +81,7 @@ public class OpenVPNService extends VpnService implements StateListener, Callbac private String mLastTunCfg; private String mRemoteGW; private final Object mProcessLock = new Object(); + private LollipopDeviceStateListener mLollipopDeviceStateListener; // From: http://stackoverflow.com/questions/3758606/how-to-convert-byte-size-into-human-readable-format-in-java public static String humanReadableByteCount(long bytes, boolean mbit) { @@ -263,6 +266,9 @@ public class OpenVPNService extends VpnService implements StateListener, Callbac mDeviceStateReceiver = new DeviceStateReceiver(magnagement); registerReceiver(mDeviceStateReceiver, filter); VpnStatus.addByteCountListener(mDeviceStateReceiver); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) + addLollipopCMListener(); } synchronized void unregisterDeviceStateReceiver() { @@ -277,6 +283,10 @@ public class OpenVPNService extends VpnService implements StateListener, Callbac iae.printStackTrace(); } mDeviceStateReceiver = null; + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) + removeLollipopCMListener(); + } public void userPause(boolean shouldBePaused) { @@ -605,6 +615,26 @@ public class OpenVPNService extends VpnService implements StateListener, Callbac } + @TargetApi(Build.VERSION_CODES.LOLLIPOP) + void removeLollipopCMListener() + { + ConnectivityManager cm = (ConnectivityManager) getBaseContext().getSystemService(CONNECTIVITY_SERVICE); + cm.unregisterNetworkCallback(mLollipopDeviceStateListener); + mLollipopDeviceStateListener = null; + } + + @TargetApi(Build.VERSION_CODES.LOLLIPOP) + void addLollipopCMListener() + { + ConnectivityManager cm = (ConnectivityManager) getBaseContext().getSystemService(CONNECTIVITY_SERVICE); + NetworkRequest.Builder nrb = new NetworkRequest.Builder(); + nrb.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VPN); + + mLollipopDeviceStateListener = new LollipopDeviceStateListener(); + cm.requestNetwork(nrb.build(), mLollipopDeviceStateListener); + cm.registerNetworkCallback(nrb.build(), mLollipopDeviceStateListener); + } + private void addLocalNetworksToRoutes() { // Add local network interfaces String[] localRoutes = NativeUtils.getIfconfig(); |