Custom sdk dependencies

Customize the SDK internal logic

  1. Create a Configuration File. Define a raw resource file named sdk_deps.json to specify custom dependencies. General structure of dependencies config may look like:

{
  "dependency 1 key": {
    "type": "class 1 name for dependency to use"
  },
  "dependency 2 key": {
    "type": "class 2 name for dependency to use"
  }
}
  1. Set up metadata in AndroidManifest.xml. Add the following code to the <application> section of your AndroidManifest.xml to link the configuration file:

<meta-data
         android:name="unified.vpn.sdk.deps"
         android:resource="@raw/sdk_deps"/>
  1. Configure ProGuard. Make sure that your custom dependency classes are not renamed or removed by ProGuard or other code obfuscation tools.

Possible dependencies overrides

You can override various SDK dependencies by creating custom classes and specifying them in the sdk_deps.json file. Below is an example of overriding the SDK's logging functionality.

Custom logger

  1. Define a class that extends unified.vpn.sdk.UnifiedLogDelegate. This class will handle logging for the SDK:

@Keep
public class CustomLogger extends UnifiedLogDelegate {
    @Override
    public void log(final int priority, Throwable throwable, String tag, String format, Object... args) {

    }

    @Override
    public void setLogLevel(final int logLevel) {

    }
}

Configure the SDK to Use the Custom Logger: Add the following entry in your sdk_deps.json file to specify the custom logger class:

{
    "logger": {
        "type": "com.sdk.sample.CustomLogger"
    }
}

Additional configuration of OkHttpClient used in sdk

  1. Define class that implements OkHttpNetworkLayer.HttpClientConfigurer

@Keep
public class SampleOkHttpConfigurer implements OkHttpNetworkLayer.HttpClientConfigurer {
    @Override
    public void configure(@NonNull final OkHttpClient.Builder builder) {
        //perform custom configuration of OkHttpClient.Builder
    }
}
  1. Setup SDK dependency override in sdk_deps

 {
     "okHttpConfigurer": {
        "type": "com.sdk.sample.SampleOkHttpConfigurer"
     }
 }

Custom backend URL rotator

  1. Create a Custom URL Rotator: Define a class that implements the UrlRotator interface. This class controls the behavior for rotating backend.

public class SampleUrlRotator implements UrlRotator {
    @Override
    public void failure(String httpUrl, PartnerRequestException error) {
        //mark url as failed
    }

    @Override
    public void success(String httpUrl) {
        //mark url as success
    }

    @Override
    public String provide() {
        // return url for backend calls
        return null;
    }

    @Override
    public int size() {
        // return number of possible urls for rotation
        return 1;
    }
}
  1. Create a URL Rotator Factory: Define a class that implements the UrlRotatorFactory interface, which creates instances of your custom UrlRotator

@Keep
public class SampleUrlRotatorFactory implements UrlRotatorFactory {
    @Override
    public BackendUrlRotator create(Context context, ClientInfo clientInfo) {
        return new SampleUrlRotator();
    }
}
  1. Configure the SDK to Use the Custom URL Rotator: Update the sdk_deps.json file to specify the custom URL rotator factory.

 {
     "urlRotator": {
        "type": "com.sdk.sample.SampleUrlRotatorFactory"
     }
 }

Custom notification builder

You can customize how notifications are displayed by defining your own notification builder. Follow these steps to set up a custom notification delegate:

  1. Create a Custom Notification Delegate Class: Define a class that implements the NotificationDelegate interface. This class controls the appearance and behavior of notifications related to VPN activity.

@Keep
public class SampleNotificationDelegate implements NotificationDelegate {
    @Nullable
    @Override
    public Notification create(
            @NonNull Context context, 
            @Nullable SdkNotificationConfig config, 
            @NonNull VpnState state, 
            long trafficRx, 
            long trafficTx, 
            long speedRx, 
            long speedTx, 
            @Nullable NotificationDelegate fallback
    ) {
        return null;
    }
}
  1. Configure the SDK to Use the Custom Notification Delegate: Update the sdk_deps.json file to specify your custom notification delegate class.

 {
     "notificationDelegate": {
        "type": "com.sdk.sample.SampleNotificationDelegate"
     }
 }

Last updated

Was this helpful?