Configurable consent SDK for Android

Overview

Consent SDK for Android

Obtaining explicit user consent regarding the gathering analytics data in an app, or with processing user’s personal data is an important part of establishing user trust and seamless user experience.

Although implementing some form to obtain user consents and store them for further reference seems pretty straightforward, digging into it reveals (as usual with “simple tasks”) many programming and design details that must be implemented, which are not the core functionality of your app.

Consent SDK main functionality

  • Provides configurable consent form that can be displayed as:
    • Dialog
    • FragmentDialog(persists orientation changes)
    • Activity
    • Fragment
  • Stores consent results and provides access methods.

Installation

Add the following dependency in your app's build.gradle:

implementation 'com.smartlook:consent:1.0'

And add the following in your project's build.gradle:

allprojects {
    repositories {
        maven {
            url "https://sdk.smartlook.com/android/release"
        }
    }
}

How to use

Firstly you need to instantiate ConsentSDK with applicationContext.

val consentSDK = ConsentSDK(applicationContext)

This object is going to be used for all interactions with ConsentSDK.

Consent form data

Before you can display consent form you need to prepare consent form data.

companion object {
    const val CONSENT_1_KEY = "consent_1_key"
    const val CONSENT_2_KEY = "consent_2_key"
}

...

val consentFormItems = arrayOf(
    ConsentFormItem(
        consentKey = CONSENT_1_KEY,
        required = true,
        description = getString(R.string.consent_1_description),
        link = null
    ),
    ConsentFormItem(
        consentKey = CONSENT_2_KEY,
        required = false,
        description = getString(R.string.consent_2_description),
        link = getString(R.string.consent_2_link)
    )
)

val consentFormData = ConsentFormData(
    titleText = getString(R.string.consent_form_title),
    descriptionText = getString(R.string.consent_form_description),
    confirmButtonText = getString(R.string.consent_form_confirm_button_text),
    consentFormItems = consentFormItems)

Array consentFormItems represents consents we want the user to grant us. Every item needs to have:

  • unique consentKey that represents it and can be used to obtain grant result for this consent.
  • required flag. If this flag is set to true user cannot successfully finish the consent form without granting this consent.
  • descriptionText informing the user about the consent.
  • link (optional) that lets the user open a web page (URL) with more info.

Object consentFormData provides all needed data for displaying consent form.

Showing consent form on Dialog

A most simple and straight-forward way of displaying consent form is on Dialog. It has one drawback, this way we cannot properly persist user data on orientation change. Use this if you have locked screen orientation.

consentSDK.showConsentFormDialog(consentFormData, object : ConsentResultsListener {
    override fun onConsentResults(consentResults: HashMap<String, Boolean>) {
        // consent form result here
    }
})

Showing consent form on DialogFragment

By using DialogFragment SDK can properly handle orientation changes.

consentSDK.showConsentFormDialogFragment(<activity>/<fragment>, consentFormData)

The first parameter of showConsentFormDialogFragment accepts Activity or Fragment reference so you can call it from both. Your calling Activity or Fragment must implement ConsentResultsListener.

class SampleActivity : AppCompatActivity(), ConsentResultsListener {

...

  override fun onConsentResults(consentResults: HashMap<String, Boolean>) {
      // consent form result here
  }
}

Starting consent form Activity

class SampleActivity : AppCompatActivity() {

  companion object {
      const val CONSENT_REQUEST_CODE = 10001
  }

  ...

  consentSDK.startConsentFormActivity(this, consentFormData, CONSENT_REQUEST_CODE)

  ...

  override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
      if (requestCode == CONSENT_REQUEST_CODE) {
          if (resultCode == Activity.RESULT_OK) {
              val consentResults = consentSDK.parseOutConsentResults(data)
          } else {
              // user didnt confirm the form (back press)
          }
      }
  }
}

Consent form Activity is started "for a result" so to get a result you need to implement onActivityResult method in your Activity.

Creating conset form Fragment

Method createConsentFormFragment lets you create Fragment with consent form. Example usage might look something like this:

const val TAG = "unique_fragment_tag"

...

with(supportFragmentManager) {
    beginTransaction()
        .replace(R.id.fragment_placeholder, consentSDK.createConsentFormFragment(consentFormData), TAG)
        .commit()
    executePendingTransactions()
}

ConsentResultsListener can be registered like this:

val consentFormFragment = supportFragmentManager.findFragmentByTag(TAG) as ConsentFormFragment
consentFormFragment.registerConsentResultsListener(object : ConsentResultsListener {
            override fun onConsentResults(consentResults: HashMap<String, Boolean>) {
                // Consent form result here
            }
        })

Consent results

When user sucessfully finishes consent form you gonna get consentResult. It is a HashMap<String,Boolean> in which:

  • key == consentKey
  • value represents consentResult:
    • true consent was granted.
    • false consent was rejected.

Are consent results stored?

SDK method areConsentResultsStored() can be used to determine if the user has already successfully filled consent form and results were stored.

Obtaining consent

If you want to obtain a grant result for given conset (identified by unique consentKey) you can do it like this:

val consentResult = consentSDK.loadConsetResult(consentKey)

If consentResult is:

  • true consent was granted.
  • false consent was rejected.
  • null not defined.

Styling

You can define custom style for the consent form. All configurable attributes are listed in the table below.

Attribute Description
colorAccent Confirm button, link icons and Switches color.
cf_textColor Description text and form item texts color.
cf_titleTextColor Title text color.
cf_confirmButtonTextColor Confirm button text color.
cf_backgroundColor Form background color.
cf_dividerColor Form item list divider color.

Dialog/FragmentDialog

In styles.xml define custom Dialog style:

<style name="DialogStyle" parent="Base.Theme.AppCompat.Light.Dialog">
    <item name="colorAccent">#35E6A5</item>
    <item name="cf_textColor">#F4F4F4</item>
    <item name="cf_titleTextColor">#F4F4F4</item>
    <item name="cf_confirmButtonTextColor">#F4F4F4</item>
    <item name="cf_backgroundColor">#26262E</item>
    <item name="cf_dividerColor">#F4F4F4</item>
</style>

Then add the style reference to showConsentFormDialog/showConsentFormDialogFragment method like this:

// Dialog
consentSDK.showConsentFormDialog(this, consentFormData, R.style.DialogStyle, listener)

// DialogFragment
consentSDK.showConsentFormDialogFragment(this, consentFormData, R.style.DialogStyle)

Activity

In styles.xml define custom Activity style:

<style name="ActivityStyle" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorAccent">#21A76A</item>
    <item name="cf_textColor">#F4F4F4</item>
    <item name="cf_titleTextColor">#F4F4F4</item>
    <item name="cf_confirmButtonTextColor">#F4F4F4</item>
    <item name="cf_backgroundColor">#26262E</item>
    <item name="cf_dividerColor">#F4F4F4</item>
</style>

Then add the style reference to startConsentFormActivity method like this:

consentSDK.startConsentFormActivity(this, consentFormData, CONSENT_REQUEST_CODE, R.style.ActivityStyle)

Fragment

In styles.xml define custom Fragment style:

<style name="FragmentStyle" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorAccent">#21A76A</item>
    <item name="cf_textColor">#F4F4F4</item>
    <item name="cf_titleTextColor">#F4F4F4</item>
    <item name="cf_confirmButtonTextColor">#F4F4F4</item>
    <item name="cf_backgroundColor">#26262E</item>
    <item name="cf_dividerColor">#F4F4F4</item>
</style>

Then add the style reference to startConsentFormActivity method like this:

consentSDK.createConsentFormFragment(consentFormData, R.style.FragmentStyle)
You might also like...
Oratio Library for Android Studio helps you simplify your Android TTS codes

Oratio Oratio is a library for Android Studio. This library is useful to a number of developers who are currently making apps using android TTS(Text-T

This is a demo android app representing implementation of SE principles in android app development

Articles Demo This repository contains a sample Android App that shows most popular articles data from NY Times API. This is a sample app that shows h

Android-Boilerplate - Base project for android development with new technology

Android-Boilerplate Base project for android development with new technology, in

Gits-android-extensions - A collection of Kotlin extensions to simplify Android development

gits-android-extensions A collection of Kotlin extensions to simplify Android de

Android Clean Architecture in Rorty is a sample project that presents modern, approach to Android application development using Kotlin and latest tech-stack.
Android Clean Architecture in Rorty is a sample project that presents modern, approach to Android application development using Kotlin and latest tech-stack.

Android Clean Architecture in Rorty is a sample project that presents modern, approach to Android application development using Kotlin and latest tech-stack.

Android Ptrace Inject for all ABIs and all APIs. Help you inject Shared Library on Android.

Android Ptrace Inject 中文可以参考我的注释内容进行理解 我写的注释相对来说比较全面了 How to build Make sure you have CMake and Ninja in your PATH Edit CMakeLists.txt. Set ANDROID_ND

Pleasant Android application development
Pleasant Android application development

⚠️ Anko is deprecated. Please see this page for more information. Anko is a Kotlin library which makes Android application development faster and easi

YouTube Player library for Android and Chromecast, stable and customizable.
YouTube Player library for Android and Chromecast, stable and customizable.

android-youtube-player android-youtube-player is a stable and customizable open source YouTube player for Android. It provides a simple View that can

A highly customizable calendar library for Android, powered by RecyclerView.
A highly customizable calendar library for Android, powered by RecyclerView.

CalendarView A highly customizable calendar library for Android, powered by RecyclerView. With this library, your calendar will look however you want

Comments
  • how to use it with react-native?

    how to use it with react-native?

    Hi, I've noticed that there is a react native integration possibility, but there is no consent sdk for the react native app (no information about required permissions in the documentation either). How can this be integrated into a react native app? If not, what permissions should be requested for the react native? Thank you.

    opened by cassde 1
Webtrekk Android SDK V5

Webtrekk Android SDK v5 Site | Docs | Support Webtrekk Android SDK is used to integrate Webtrekk tracking systems with your Android apps. Collect mean

Webtrekk GmbH 13 Apr 26, 2022
Blog implemented via the Storyblok Kotlin Multiplatform SDK (Android, JVM)

storyblok-mp-SDK-blog ... a showcase of using the Storyblok Kotlin Multiplatform Client to build a blog application (Android, JVM) What's included ??

Mike Penz 5 Sep 28, 2022
Kotlin Multiplatform is an SDK for cross-platform mobile development, which enables teams to use the same business logic in both Android and iOS client applications.

Kotlin Multiplatform is an SDK for cross-platform mobile development, which enables teams to use the same business logic in both Android and iOS client applications.

Chris Russell 1 Feb 11, 2022
Android sample app for mobile-sdk integration

Dyte Android Sample App An example app in kotlin using the Dyte Mobile Core SDK Explore the docs » View Demo · Report Bug · Request Feature Table of C

Dyte 5 Jul 30, 2022
io.github.jakob.AgonesClient - Kotlin client library for sdk.proto

io.github.jakob.AgonesClient - Kotlin client library for sdk.proto Requires Kotlin 1.3.41 Gradle 4.9 Build First, create the gradle wrapper script: gr

KunJakob 3 Jun 24, 2022
Unofficial Actions on Google SDK for Kotlin and Java

Actions On Google Client Library This is a port of the official Node.js SDK to Kotlin. This can also be used from Java and any JVM language. Quick Fac

Ticketmaster® & Live Nation Entertainment® 118 Oct 3, 2022
Account-lib - A suite of libraries to facilitate the usage of account-sdk

Usage Clone this repository (skip this step if the repo is on your local machine). The default branch is fine. git clone https://github.com/AFBlockcha

null 0 May 24, 2022
The AppMetrica Push SDK is a set of libraries for working with push notifications.

Flutter AppMetrica Push AppMetrica Push SDK — это набор библиотек для работы с push-уведомлениями. Подключив AppMetrica Push SDK, вы можете создать и

Mad Brains 6 Oct 12, 2022
Android MVVM framework write in kotlin, develop Android has never been so fun.

KBinding 中文版 Android MVVM framework write in kotlin, base on anko, simple but powerful. It depends on my another project AutoAdapter(A library for sim

Benny 413 Dec 5, 2022
Klimatic is an android app built using Kotlin. It try to showcase all the latest technologies used in android.

Klimatic Klimatic is an android app built using Kotlin. It try to showcase all the latest technologies used in android. Built using Android Architectu

Shivam Satija 34 Oct 11, 2022