TV
import com.sfbx.appconsent.AppConsent
import com.sfbx.appconsent.tv.AppConsentSDK
import com.sfbx.appconsent.tv.listener.AppConsentErrorListener
import com.sfbx.appconsent.tv.listener.AppConsentLogListener
import com.sfbx.appconsent.tv.listener.AppConsentNavigationListener
import com.sfbx.appconsent.tv.listener.OnPresentNoticeListener
import com.sfbx.appconsent.tv.model.ACConfiguration
import com.sfbx.appconsent.tv.model.AppConsentTVError
import com.sfbx.appconsent.tv.model.AppConsentTVLog
import com.sfbx.appconsent.tv.model.error.ACError
class MainActivity : AppCompatActivity() {
private var appConsent: AppConsent? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
/*
Shows whether the SDK has already been initialized
*/
if (!AppConsentSDK.isSdkInitialized() || AppConsentSDK.getInstance() == null) {
initCmpModule()
} else {
if (appConsent == null) {
/*
Retrieves the AppConsent instance or null if it has not yet been instantiated, for example
*/
appConsent = AppConsentSDK.getInstance()
}
tryToDisplayCmpAndCheckUpdateIfNeeded()
}
}
/**
* The only conditions for using this method would be if:
* - you have configured your Notice by selecting this option: When saving changes to the notice, display the notice to all visitors.
* - you plan to update your Notice often (more often than the : Consent retention period configurable in your Notice)
* - Your users rarely restart your application.
*
* In which case, using this method from time to time may be a solution.
*
* But we encourage you to let the SDK handle this part on its own.
*/
private fun checkIfNoticeHasBeenUpdated() {
appConsent?.checkForUpdate(
{ isNeedToPresentTheCmp: Boolean ->
// Your Notice has been updated, you must represent the CMP to your users
if (true == isNeedToPresentTheCmp) {
/*
Deletes old user consent locally.
This step is not mandatory, but it avoids the need to make another network call
to check whether the Notice has been updated,
as no consent will be present on the user's device.
*/
appConsent?.clearConsent()
/*
Remember to re-register for callbacks if you have previously removed them,
otherwise you will not know when the user has given their consent.
*/
registerCallbacks()
if (false == tryToDisplayCMP()) {
/*
At this stage, if the CMP is not displayed, it is possible, for example:
that the user is no longer in a geographical area subject to GDPR
*/
removeCMPCallback()
}
} else {
/*
The Notice is the same as when it was last checked
(you have made no changes since the board or
it has not been updated internally by us, e.g. by updating a vendor)
*/
removeCMPCallback()
}
}) { _: Throwable? ->
/*
An error has occurred
*/
removeCMPCallback()
}
}
/**
* Try to display the CMP
*
* @return true, if the CMP displaying, false otherwise
*/
private fun tryToDisplayCMP(): Boolean {
/*
Try to display the CMP according to certain rules.
*/
return appConsent?.tryToDisplayNotice(false) == true
}
private fun tryToDisplayCmpAndCheckUpdateIfNeeded() {
/*
Try to display the CMP according to certain rules first.
*/
if (false == tryToDisplayCMP()) {
/*
The user has already given consent;
The user is not part of an area subject to the application of GDPR;
etc.
*/
/*
The Notice has not been displayed,
so we'll have to check whether it has been updated
and whether it needs to be shown to users again.
*/
checkIfNoticeHasBeenUpdated()
}
}
/*
Initializes the consent management platform module when the activity is created
*/
private fun initCmpModule() {
/*
Add an error listener logger
*/
AppConsentSDK.setErrorLogListener(object : AppConsentErrorListener {
override fun onError(error: AppConsentTVError) {
Log.e(
MainActivity::class.java.simpleName,
error.toString()
)
}
})
/*
Add an information listener logger
*/
AppConsentSDK.setInformationLogListener(object : AppConsentLogListener {
override fun onLogReceived(log: AppConsentTVLog) {
Log.i(
MainActivity::class.java.simpleName,
log.toString()
)
}
})
/*
Add a navigation listener logger (historical)
*/
AppConsentSDK.setNavigationLogListener(object : AppConsentNavigationListener {
override fun onNoticeBackPressed() {
Log.i(
MainActivity::class.java.simpleName,
"onNoticeBackPressed"
)
}
})
/*
ACConfiguration is used to configure the CMP.
In this example:
- We set forceApplyGDPR to true to display the CMP regardless of the user's region.
- We set setNeedToReplaceUrlViewerByQrCode to true to display QR codes instead of the embedded webview for hyperlinks.
*/
val acConfiguration = ACConfiguration.Builder()
.setForceApplyGDPR(true)
.setNeedToReplaceUrlViewerByQrCode(true)
.build()
AppConsentSDK.initialize(
appKey = "YOUR_APPKEY",
configuration = acConfiguration
) { appConsentInitialized ->
/*
To avoid certain problems, use the instance received by the onReady callback
This has been successfully initialized
*/
appConsent = appConsentInitialized
registerCallbacks()
tryToDisplayCmpAndCheckUpdateIfNeeded()
}
}
private fun registerCallbacks() {
/*
Registers with CMP callback to know when the user
has given consent, or if an error has occurred
*/
appConsent?.setOnPresentNoticeListener(object : OnPresentNoticeListener {
override fun presentConsentError(error: ACError) {
/*
An error has occurred
*/
removeCMPCallback()
}
override fun presentConsentGiven() {
/*
The user has given his consent
*/
removeCMPCallback()
appConsent?.let { appConsentNN ->
updateFirebaseConsent(appConsentNN)
}
}
})
}
private fun removeCMPCallback() {
/*
Not mandatory, but avoids keeping a local reference to the callback.
Of course, it all depends on how your project is implemented.
For example, with an SOP Activity, a singleton monitored by a flow, etc.
*/
appConsent?.setOnPresentNoticeListener(null)
}
private fun updateFirebaseConsent(appConsent: AppConsent) {
// Consent has just been validated by the user.
// Recovers current GCM status (following user consent).
val gcmConsentStatus = appConsent.getGCMConsentStatus()
val newAnalyticsStorage =
if (gcmConsentStatus.isAnalyticsStorageGranted) GRANTED else DENIED
val newAdStorage =
if (gcmConsentStatus.isAdStorageGranted) GRANTED else DENIED
val newAdUserData =
if (gcmConsentStatus.isAdUserDataGranted) GRANTED else DENIED
val newAdPersonalization =
if (gcmConsentStatus.isAdPersonalizationGranted) GRANTED else DENIED
/*
* We do not guarantee that Firebase will work with this example.
* Please refer to the official documentation for details of initialization, re-initialization, reboot and other conditions.
*
* This example is only intended to show how to retrieve ConsentStatus and set it to your Firebase instance.
*/
// Update Firebase status with information provided by the SDK
Firebase.analytics.setConsent {
analyticsStorage = newAnalyticsStorage
adStorage = newAdStorage
adUserData = newAdUserData
adPersonalization = newAdPersonalization
}
}
}Last updated