The zerodata sdk allows businesses to serve content to their users data free over the internet.
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Add the following dependency to your app's build.gradle file:
dependencies {
implementation 'com.github.simplifyd-systems:android-zerodata-sdk:Tag' //find the tag for the latest version above
}
Add the following dependency to your app's build.gradle file:
- Download the SDK artifact from latest version on the releases page.
- Add the
zerodata-sdk-release.aar
file to the root directory of your project - Add the following dependency to your app's build.gradle file:
dependencies {
implementation path(":zerodata-sdk-release.aar")
}
- Obtain the
appToken
from the Simplifyd dashboard - Obtain the
apiKey
from the Simplifyd dashboard
- In your root activity class or application class, initialize the SDK using the following lines of code:
ZeroData.sdk.initialize(apiKey, isProduction) //isProduction should be set to true or false depending on if the app is in the production enviroment
You need to register the current user of the app so their traffic and zerodata balance can be uniquely identified and measured on zerodata hence the need for the userId
and sessionToken
.
- The userId is used to identify the current user on zerodata's servers, you can keep this in encryptedSharedPreferences or in a singleton in memory store that is available app wide.
- The sessionToken is used for all subsequent interactions with the zerodata servers, you can keep this in encryptedSharedPreferences or in a singleton in memory store that is available app wide.
To obtain these values; invoke the following lines of code in your application class, root activity class or immediately after your user is logged in (in case your app contains a login/registration flow):
ZeroData.sdk.registerUser(
USER_IDENTIFIER, //USER_IDENTIFIER can be anything that uniquely identifies this user on your systems e.g phonenumber, email, userId
APP_TOKEN, //APP_TOKEN can be obtained from the zero data dashboard during setup. Make sure to keep this securely, it is a private key that is used to ensure traffic on your account is from your app
object : ZeroDataRegistrationListener {
override fun onSuccess(uuid: UUID?, sessionToken: String) {
runOnUiThread {
Toast.makeText(
this@MainActivity,
"UserId is available $uuid",
Toast.LENGTH_SHORT
).show()
}
this@MainActivity.userId = uuid
this@MainActivity.sessionToken = sessionToken
}
override fun onFailure(throwable: Throwable?) {
throwable?.let {
throwable.printStackTrace()
showErrorToastMessage(throwable)
}
binding.simpleSwitch.isChecked = false
}
})
- At the point where you need to connect to the SDK, add the following snippet, lets assume your root activity is called MainActivity.
ZeroData.sdk.startConnection(this, MainActivity::class.java, userId, sessionToken)
Remember to change MainActivity to match the name of the Activity that is initializing the SDK
- To get updates on connection status changes, add the following snippets
ZeroData.sdk.addListener(object : ZeroDataConnectionListener {
override fun onConnected() {
// show connected UI
}
override fun onStartConnection() {
//this line should remain unchanged,
// it enables the SDK to request for VPN permissions
// from the system during first launch
startService(ZeroData.sdk.getConnectServiceIntent(this@MainActivity))
}
override fun onTimeTicked(time: String?) {
ZeroData.sdk.connectionStatus.getOrDefault(CONNECTED).let {
// update ticker UI
}
}
override fun onConnecting() {
// show connecting UI
}
override fun onUrlAvailable(url: String?) {
// open browser URL
}
override fun onDisconnected() {
ZeroData.sdk.connectionStatus.getOrDefault(DISCONNECTED).let {
// show disconnected UI
}
}
override fun onFailure(throwable: Throwable) {
// show failed UI
}
})
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == ZeroData.VPN_REQUEST_CODE && resultCode == RESULT_OK) {
startService(ZeroData.sdk.getConnectServiceIntent(this))
} else {
super.onActivityResult(requestCode, resultCode, data)
}
}
- To disconnect from the VPN, add the following snippet
ZeroData.sdk.stopConnection(this, sessionToken)
- Why do I need to pass the sessionToken everytime I call an SDK function? : Some apps rely on constantly rotating tokens that are generated at runtime for security, hence a new token has to be supplied for each call to zerodata, if this is not the case for your implementation, feel free to use the same token all through the session.
- What is the Zerodata is connected notification that shows up when a session is ongoing? : This is a notification indicating a foreground service that keeps the VPN connection alive for as long as needed, no extra battery power is drawn as a result.