[Android] Util method to check VPN connection

Geoffrey Métais git at videolan.org
Wed Feb 8 15:38:46 CET 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Wed Feb  8 15:37:40 2017 +0100| [39c97890b7320e412b2345746cc266f7991eb49a] | committer: Geoffrey Métais

Util method to check VPN connection

> https://code.videolan.org/videolan/vlc-android/commit/39c97890b7320e412b2345746cc266f7991eb49a
---

 .../src/org/videolan/vlc/util/AndroidDevices.java  | 30 +++++++++++++++++++++-
 1 file changed, 29 insertions(+), 1 deletion(-)

diff --git a/vlc-android/src/org/videolan/vlc/util/AndroidDevices.java b/vlc-android/src/org/videolan/vlc/util/AndroidDevices.java
index 8713436..ad6cfff 100644
--- a/vlc-android/src/org/videolan/vlc/util/AndroidDevices.java
+++ b/vlc-android/src/org/videolan/vlc/util/AndroidDevices.java
@@ -25,6 +25,8 @@ import android.content.ComponentName;
 import android.content.Context;
 import android.content.pm.PackageManager;
 import android.net.ConnectivityManager;
+import android.net.Network;
+import android.net.NetworkCapabilities;
 import android.net.NetworkInfo;
 import android.os.Build;
 import android.os.Build.VERSION;
@@ -36,18 +38,21 @@ import android.view.InputDevice;
 import android.view.MotionEvent;
 
 import org.videolan.libvlc.util.AndroidUtil;
+import org.videolan.medialibrary.media.MediaWrapper;
 import org.videolan.vlc.R;
 import org.videolan.vlc.RemoteControlClientReceiver;
 import org.videolan.vlc.VLCApplication;
-import org.videolan.medialibrary.media.MediaWrapper;
 
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.IOException;
+import java.net.NetworkInterface;
+import java.net.SocketException;
 import java.util.ArrayList;
 import java.util.Arrays;
+import java.util.Enumeration;
 import java.util.HashSet;
 import java.util.LinkedList;
 import java.util.List;
@@ -219,6 +224,29 @@ public class AndroidDevices {
         return false;
     }
 
+    public static boolean isVPNActive() {
+        if (AndroidUtil.isLolliPopOrLater()) {
+            ConnectivityManager cm = (ConnectivityManager)VLCApplication.getAppContext().getSystemService(Context.CONNECTIVITY_SERVICE);
+
+            for (Network network : cm.getAllNetworks()) {
+                if (cm.getNetworkCapabilities(network).hasTransport(NetworkCapabilities.TRANSPORT_VPN))
+                    return true;
+            }
+            return false;
+        } else {
+            try {
+                Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
+                while (networkInterfaces.hasMoreElements()) {
+                    NetworkInterface networkInterface = networkInterfaces.nextElement();
+                    String name = networkInterface.getDisplayName();
+                    if (name.startsWith("ppp") || name.startsWith("tun") || name.startsWith("tap"))
+                        return true;
+                }
+            } catch (SocketException ignored) {}
+            return false;
+        }
+    }
+
     public static boolean hasLANConnection() {
         boolean networkEnabled = false;
         ConnectivityManager connectivity = (ConnectivityManager) (VLCApplication.getAppContext().getSystemService(Context.CONNECTIVITY_SERVICE));



More information about the Android mailing list