From d4c4a2c95b605c17b184e2c69a395a3d2303d50e Mon Sep 17 00:00:00 2001 From: lucaslin Date: Wed, 23 Jun 2021 08:17:55 +0800 Subject: [PATCH] Throw UnsupportedOperationException if startLegacyVpn is called Legacy VPN is deprecated starting from Android S. So this API shouldn't be called if the initial SDK version of device is Android S+. Otherwise, UnsupportedOperationException will be thrown. Bug: 189808606 Test: 1. make gts && gts-tradefed 2. run gts --module GtsNetworkStackHostTestCases Change-Id: I00f49555bbbc8ccf270056f0e4d1bfdd927ef26d --- core/java/android/net/VpnManager.java | 4 ++++ .../core/java/com/android/server/VpnManagerService.java | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/core/java/android/net/VpnManager.java b/core/java/android/net/VpnManager.java index 662ebb356f4c4..5c2855307509c 100644 --- a/core/java/android/net/VpnManager.java +++ b/core/java/android/net/VpnManager.java @@ -389,6 +389,10 @@ public class VpnManager { /** * Starts a legacy VPN. + * + * Legacy VPN is deprecated starting from Android S. So this API shouldn't be called if the + * initial SDK version of device is Android S+. Otherwise, UnsupportedOperationException will be + * thrown. * @hide */ public void startLegacyVpn(VpnProfile profile) { diff --git a/services/core/java/com/android/server/VpnManagerService.java b/services/core/java/com/android/server/VpnManagerService.java index 26ecee8f21aba..70176a0fefeb0 100644 --- a/services/core/java/com/android/server/VpnManagerService.java +++ b/services/core/java/com/android/server/VpnManagerService.java @@ -38,6 +38,7 @@ import android.net.VpnManager; import android.net.VpnService; import android.net.util.NetdService; import android.os.Binder; +import android.os.Build; import android.os.Handler; import android.os.HandlerThread; import android.os.INetworkManagementService; @@ -348,9 +349,17 @@ public class VpnManagerService extends IVpnManager.Stub { /** * Start legacy VPN, controlling native daemons as needed. Creates a * secondary thread to perform connection work, returning quickly. + * + * Legacy VPN is deprecated starting from Android S. So this API shouldn't be called if the + * initial SDK version of device is Android S+. Otherwise, UnsupportedOperationException will be + * thrown. */ + @SuppressWarnings("AndroidFrameworkCompatChange") // This is not an app-visible API. @Override public void startLegacyVpn(VpnProfile profile) { + if (Build.VERSION.DEVICE_INITIAL_SDK_INT >= Build.VERSION_CODES.S) { + throw new UnsupportedOperationException("Legacy VPN is deprecated"); + } int user = UserHandle.getUserId(mDeps.getCallingUid()); // Note that if the caller is not system (uid >= Process.FIRST_APPLICATION_UID), // the code might not work well since getActiveNetwork might return null if the uid is