Since: API level 13
public class

GenericVpnContext

extends ContextWrapper
java.lang.Object
   ↳ android.content.Context
     ↳ android.content.ContextWrapper
       ↳ com.samsung.android.knox.net.vpn.serviceprovider.GenericVpnContext

Class Overview

GenericVpnContext is a class for applications to create custom VPN context object and achieve Knox VPN functionality.

GenericVpnContext class is basically extended from ContextWrapper class and accepts a context in its constructor. In general, this custom context creates a type matched object to pass context to Android`s prepare() API. This custom context facilitates passing extra parameters to achieve per-application VPN and meta-data passing with VPN traffic.

Please refer to Android`s VpnService documentation for further details on VPN solution creation.

Following are some key points:

  • VPN clients can integrate with Knox VPN framework either by using the VPN service provided by Android (calling prepare function with GenericVpnContext) or by using GenericVpnService.
  • When creating Knox VPN profile using GenericVpnContext, APIs available in VpnService.Builder class from API level 21 are not supported.
  • When a VPN client needs to start a Knox VPN profile, the client can create object of GenericVpnContext.
    
     GenericVpnContext vpnContext = new GenericVpnContext(this);
    
     vpnContext.setGenericVpnParams(profileName, GenericVpnContext.CONNECTING);
    
     VpnService.prepare(vpnContext);
    
     
  • When client needs to start a Knox VPN profile and get meta-data along with the traffic, then the client can create object of GenericVpnContext.
    
     GenericVpnContext vpnContext = new GenericVpnContext(this);
    
     vpnContext.setGenericVpnParams(profileName, GenericVpnContext.CONNECTING);
    
     vpnContext.enableMetaHeader(true);
    
     VpnService.prepare(vpnContext);
    
     
  • When establishing the VPN connection for Knox profile, set the setSession value of the VpnService.Builder class to the profile name of the connection.
  • When establishing the VPN connection for Knox profile, the addRoute value of the VpnService.Builder class is set to null by default.
  • If the VPN client plans to support multiple VPN interfaces per user, they can achieve it by using the default VpnService provided by Android along with the changes mentioned above.

    Since
    API level 13
    KNOX 2.2.0

Summary

Constants
boolean CONNECTING This field indicates that the connection is starting.
boolean DISCONNECTING This field indicates that the connection is stopping.
boolean META_DISABLED This field indicates that the meta-data (UID/PID of application generating the packet) passing is disabled.
boolean META_ENABLED This field indicates that the meta-data (UID/PID of application generating the packet) passing is enabled.
[Expand]
Inherited Constants
From class android.content.Context
Public Constructors
GenericVpnContext(Context base)
Constructor of GenericVpnContext class.
Public Methods
void enableMetaData(boolean enable)
Function used to enable/disable meta-data passing with Knox VPN traffic.
String getVPNProfile()
Function to get profile name set in setGenericVpnParams(String, boolean).
boolean getVPNState()
Function to get VPN state set in setGenericVpnParams(String, boolean).
boolean isMetaEnabled()
Function to get the status of meta-data appending for profile set in setGenericVpnParams(String, boolean).
void setGenericVpnParams(String profile, boolean isConnecting)
Set the parameters required to achieve Knox VPN.
[Expand]
Inherited Methods
From class android.content.ContextWrapper
From class android.content.Context
From class java.lang.Object

Constants

public static final boolean CONNECTING

Since: API level 13

This field indicates that the connection is starting.

Since
API level 13
KNOX 2.2.0
Constant Value: true

public static final boolean DISCONNECTING

Since: API level 13

This field indicates that the connection is stopping.

Since
API level 13
KNOX 2.2.0
Constant Value: false

public static final boolean META_DISABLED

Since: API level 13

This field indicates that the meta-data (UID/PID of application generating the packet) passing is disabled.

Since
API level 13
KNOX 2.2.0
Constant Value: false

public static final boolean META_ENABLED

Since: API level 13

This field indicates that the meta-data (UID/PID of application generating the packet) passing is enabled.

Since
API level 13
KNOX 2.2.0
Constant Value: true

Public Constructors

public GenericVpnContext (Context base)

Since: API level 13

Constructor of GenericVpnContext class.

Since
API level 13
KNOX 2.2.0

Public Methods

public void enableMetaData (boolean enable)

Since: API level 13

Function used to enable/disable meta-data passing with Knox VPN traffic. If the VPN client does not have privilege in enabling meta-data along with network traffic, VPN client will receive a SecurityException when invoking prepare API.

Parameters
enable Specifies whether to enable or disable meta-data passing along with VPN traffic.
Since
API level 13
KNOX 2.2.0
See Also

public String getVPNProfile ()

Since: API level 13

Function to get profile name set in setGenericVpnParams(String, boolean).

Returns
  • String with the Knox VPN profile name.
Since
API level 13
KNOX 2.2.0

public boolean getVPNState ()

Since: API level 13

Function to get VPN state set in setGenericVpnParams(String, boolean).

Returns
  • Knox VPN state - true for Connecting and false for Disconnecting.
Since
API level 13
KNOX 2.2.0

public boolean isMetaEnabled ()

Since: API level 13

Function to get the status of meta-data appending for profile set in setGenericVpnParams(String, boolean).

Returns
  • True if meta-data passing is enable, false otherwise.
Since
API level 13
KNOX 2.2.0

public void setGenericVpnParams (String profile, boolean isConnecting)

Since: API level 13

Set the parameters required to achieve Knox VPN.

Parameters
profile Specifies the profile name for which Knox VPN has to be set.
isConnecting Specifies the state of connection - Connecting or Disconnecting.
Since
API level 13
KNOX 2.2.0