Since: API level 9
public class

GenericVpnService.Builder

extends Object
java.lang.Object
   ↳ com.samsung.android.knox.net.vpn.serviceprovider.GenericVpnService.Builder

Class Overview

Helper class to create a VPN interface. This class should be always used within the scope of the outer VpnService.

Since
API level 9
KNOX 1.1.0

Summary

Public Constructors
Builder()
Constructor of GenericVpnService.Builder class.
Public Methods
GenericVpnService.Builder addAddress(InetAddress address, int prefixLength)
Add a network address to the VPN interface.
GenericVpnService.Builder addAddress(String address, int prefixLength)
Convenience method to add a network address to the VPN interface using a numeric address string.
GenericVpnService.Builder addDnsServer(String address)
Convenience method to add a DNS server to the VPN connection using a numeric address string.
GenericVpnService.Builder addDnsServer(InetAddress address)
Add a DNS server to the VPN connection.
GenericVpnService.Builder addRoute(InetAddress address, int prefixLength)
Add a network route to the VPN interface.
GenericVpnService.Builder addRoute(String address, int prefixLength)
Convenience method to add a network route to the VPN interface using a numeric address string.
GenericVpnService.Builder addSearchDomain(String domain)
Add a search domain to the DNS resolver.
ParcelFileDescriptor establish()
Create a VPN interface using the parameters supplied to this Builder.
GenericVpnService.Builder setConfigureIntent(PendingIntent intent)
Set the PendingIntent to an activity for users to configure the VPN connection.
GenericVpnService.Builder setMtu(int mtu)
Set the maximum transmission unit (MTU) of the VPN interface.
GenericVpnService.Builder setSession(String session)
Set the name of this session.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public Builder ()

Since: API level 9

Constructor of GenericVpnService.Builder class.

Since
API level 9
KNOX 1.1.0

Public Methods

public GenericVpnService.Builder addAddress (InetAddress address, int prefixLength)

Since: API level 9

Add a network address to the VPN interface. Both IPv4 and IPv6 addresses are supported. At least one address must be set before calling establish().

Parameters
address The InetAddress containing address information to be added.
prefixLength Network prefix.
Returns
  • This Builder object with the InetAddress containing address information added.
Throws
IllegalArgumentException if the address is invalid.
Since
API level 9
KNOX 1.1.0

public GenericVpnService.Builder addAddress (String address, int prefixLength)

Since: API level 9

Convenience method to add a network address to the VPN interface using a numeric address string. See InetAddress for the definitions of numeric address formats.

Parameters
address The address to be added.
prefixLength Network prefix.
Returns
  • This Builder object with the address added.
Throws
IllegalArgumentException if the address is invalid.
Since
API level 9
KNOX 1.1.0

public GenericVpnService.Builder addDnsServer (String address)

Since: API level 9

Convenience method to add a DNS server to the VPN connection using a numeric address string. See InetAddress for the definitions of numeric address formats.

Parameters
address The address of DNS Server to be added.
Returns
  • This Builder object with the DNS Server address added.
Throws
IllegalArgumentException if the address is invalid.
Since
API level 9
KNOX 1.1.0

public GenericVpnService.Builder addDnsServer (InetAddress address)

Since: API level 9

Add a DNS server to the VPN connection. Both IPv4 and IPv6 addresses are supported. If none is set, the DNS servers of the default network will be used.

Parameters
address The InetAddress containing DNS Server information to be added.
Returns
  • This Builder object with the InetAddress containing DNS Server information added.
Throws
IllegalArgumentException if the address is invalid.
Since
API level 9
KNOX 1.1.0

public GenericVpnService.Builder addRoute (InetAddress address, int prefixLength)

Since: API level 9

Add a network route to the VPN interface. Both IPv4 and IPv6 routes are supported.

Parameters
address The InetAddress containing route information to be added.
prefixLength Network prefix.
Returns
  • This Builder object with the InetAddress containing route information added.
Throws
IllegalArgumentException if the route is invalid.
Since
API level 9
KNOX 1.1.0

public GenericVpnService.Builder addRoute (String address, int prefixLength)

Since: API level 9

Convenience method to add a network route to the VPN interface using a numeric address string. See InetAddress for the definitions of numeric address formats.

Parameters
address The route to be added.
prefixLength Network prefix.
Returns
  • This Builder object with the route added.
Throws
IllegalArgumentException if the route is invalid.
Since
API level 9
KNOX 1.1.0

public GenericVpnService.Builder addSearchDomain (String domain)

Since: API level 9

Add a search domain to the DNS resolver.

Parameters
domain The search domain to be added.
Returns
  • This Builder object with the search domain added.
Since
API level 9
KNOX 1.1.0

public ParcelFileDescriptor establish ()

Since: API level 28

Create a VPN interface using the parameters supplied to this Builder. The interface works on IP packets, and a file descriptor is returned for the application to access them. Each read retrieves an outgoing packet which was routed to the interface. Each write injects an incoming packet just like it was received from the interface. The file descriptor is put into non-blocking mode by default to avoid blocking Java threads. To use the file descriptor completely in native space, see detachFd(). The application MUST close the file descriptor when the VPN connection is terminated. The VPN interface will be removed and the network will be restored by the system automatically.

Usually network parameters are never changed during the lifetime of a VPN connection. It is also common for an application to create a new file descriptor after closing the previous one in system VPN. If the new interface cannot be created, the existing interface and its file descriptor remain untouched.

An exception will be thrown if the interface cannot be created for any reason. However, this method returns null if the application is not prepared or is revoked. This helps solve possible race conditions between other VPN applications.

Returns
Throws
IllegalArgumentException If a parameter is not accepted by the operating system.
IllegalStateException If a parameter cannot be applied by the operating system.
SecurityException If the service is not properly declared in AndroidManifest.xml.
See Also

public GenericVpnService.Builder setConfigureIntent (PendingIntent intent)

Since: API level 9

Set the PendingIntent to an activity for users to configure the VPN connection. If it is not set, the button to configure will not be shown in system-managed dialogs.

Parameters
intent The PendingIntent to be set.
Returns
Since
API level 9
KNOX 1.1.0

public GenericVpnService.Builder setMtu (int mtu)

Since: API level 9

Set the maximum transmission unit (MTU) of the VPN interface. If it is not set, the default value in the operating system will be used.

Parameters
mtu The MTU to be set.
Returns
  • This Builder object with the MTU set.
Throws
IllegalArgumentException if the value is not positive.
Since
API level 9
KNOX 1.1.0

public GenericVpnService.Builder setSession (String session)

Since: API level 9

Set the name of this session. It will be displayed in system-managed dialogs and notifications. This is required to be profile name.

Parameters
session The name of the session to be set.
Returns
  • This Builder object with the provided session set.
Since
API level 9
KNOX 1.1.0