Consent Manager

Code examples:

1. Import SDK

Apps can import the Stack Consent SDK with a Gradle dependency that points to the Maven repository. In order to use that repository, you need to reference it in the app's project-level build.gradle file. Open yours and look for an allprojects section:

Add the Stack Consent SDK maven repository

Example project-level build.gradle:

repositories {
    // ... other project repositories
    maven { 
        url "https://artifactory.appodeal.com/appodeal" 
    }
}

Add maven dependencies

Next, open the app-level build.gradle file for your app, and look for the dependencies section.

Example app-level build.gradle:

dependencies {
    // ... other project dependencies
	
    //Consent SDK
    implementation 'com.explorestack:consent:1.0.1'
}

Add Google Play Services Identifier dependencies (15.0.0 and up only):

dependencies {
    // ... other project dependencies
    implementation 'com.google.android.gms:play-services-ads-identifier:15.0.0'
}

2. Request Consent Info Update 

Default

Consent manager SDK can be synchronized at any moment of the application lifecycle. We recommend to synchronize it at application launch. Multiple synchronization calls are allowed.

Required parameter is appKey - Appodeal API Key and ConsentInfoUpdateListener - listener for result request.

Optional parameter is url  (string) - Custom consent manager server URL for the request.

/// Initialisation 
ConsentManager.getInstance(context).requestConsentInfoUpdate(
    "APPODEAL_KEY", 
    new ConsentInfoUpdateListener() {
        @Override
        public void onConsentInfoUpdated(Consent consent) {
        }

        @Override
        public void onFailedToUpdateConsentInfo(ConsentManagerException exception) {
        }
});

After request completion, you can receive information about the previous user consent and regulation zone. Before request these parameters are undefined

// Get manager
ConsentManager consentManager = ConsentManager.getInstance(this);

// Get consent object for Appodeal SDK
Consent consent = consentManager.getConsent();

// Check regulation
Consent.Zone consentZone = consentManager.getConsentZone();

// Check consent status
Consent.Status consentStatus = consentManager.getConsentStatus();

// Get consent string
String iabString = consentManager.getIabConsentString();

You can check whether to show a Consent Dialog or not. Before request these parameters are undefined

// Get manager
ConsentManager consentManager = ConsentManager.getInstance(this);

// Get current ShouldShow status
Consent.ShouldShow consentShouldShow = consentManager.shouldShowConsentDialog();

if (consentShouldShow == Consent.ShouldShow.TRUE){
    // show dialog
}

Advanced

You can force consent manager to write iAB keys in SharedPreference by setting up storage property before the request to ConsentManager.Storage

SDK does not remove iAB keys from SharedPreference and only overrides them

// Store IAB strings in SharedPreference
// NOTE: should be called before request

// Get manager
ConsentManager consentManager = ConsentManager.getInstance(this);

// Set storage
consentManager.setStorage(ConsentManager.Storage.SHARED_PREFERENCE);

// Get storage
ConsentManager.Storage iabStorage = consentManager.getStorage();


You can register yourself as a vendor before request. 

ParameterTypeDescription
nameStringDisplay name. Will be displayed in the consent window
bundleString Custom string to check consent result for the vendor
policyUrlStringPolicy URL
purposeIdsArray of integersiAB purposes ids array
featureIdsArray of integersiAB features ids array
legitimateInterestPurposeIdsArray of integersiAB leg int purposes ids array
// Register custom vendor (will be displayed on consent window)
// should be called before request

// Get manager
ConsentManager consentManager = ConsentManager.getInstance(this);

// Create custom vendor
Vendor.Builder builder = new Vendor.Builder(
                "Custom Vendor",
                "customvendor",
                "https://customvendor.com")
                .setPurposeIds(new ArrayList<Integer>() {{
                    add(1);
                    add(2);
                    add(3);
                }})
                .setLegitimateInterestPurposeIds(new ArrayList<Integer>() {{
                    add(1);
                    add(2);
                    add(4);
                }})
                .setFeatureIds(new ArrayList<Integer>() {{
                    add(1);
                    add(2);
                }});

// Set custom vendor
consentManager.setCustomVendor(builder.build());

// Get by bundle
consentManager.getCustomVendor("customvendor");

You can check Vender has consent or not.

// Get manager
ConsentManager consentManager = ConsentManager.getInstance(this);

// Get consent status for vendor
Consent.HasConsent vendorStatus = consentManager.hasConsentForVendor("vendorBundle"); //by bundle

3. Show Consent window

SDK allows calling consent window API only after request 

After the SDK requests, you can build and load the consent window. Loading allowed in any regulation zone and independent from previous consent.

The required parameter is Context.

Optional parameter is ConsentFormListener.

// Create new Consent form listener
ConsentFormListener consentFormListener = new ConsentFormListener() {
    @Override
    public void onConsentFormLoaded() {
        // Consent form was loaded. Now you can display consent form as activity or as dialog
    }

    @Override
    public void onConsentFormError(ConsentManagerException error) {
        // Consent form loading or showing failed. More info can be found in 'error' object
    }

    @Override
    public void onConsentFormOpened() {
        // Conset form was shown 
    }

    @Override
    public void onConsentFormClosed(Consent consent) {
        // Consent form was closed
    }
};

// Create new Consent form instance
ConsentForm consentForm = new ConsentForm.Builder(this)
	.withListener(consentFormListener)
    .build();

//
consentForm.load();

You can check that the consent window is ready or not

// Indicates that consent window ready to present
boolean loaded = consentForm.isLoaded();

You can check that the consent window is showing or not

// Indicates that consent window is showing
boolean showing = consentForm.isShowing();

After consent window Is ready you can show it as Activity or Dialog

// Show consent dialog as Activity
consentForm.showAsActivity();

// Show consent dialog as Dialog
consentForm.showAsDialog();

Handling presentation callbacks

new ConsentFormListener() {
    @Override
    public void onConsentFormLoaded() {
        // Consent form loaded successfully.
    }
	
    @Override
    public void onConsentFormOpened() {
        // Consent form was displayed.
    }
	
    @Override
    public void onConsentFormClosed(Consent consent) {
        // Consent form was closed.
    }

    @Override
    public void onConsentFormError(ConsentManagerException exception) {
    	// Consent form error.
    }
};

4. Error handling

All returned errors are Exception instances with custom codes:

ConsentManagerErrorCode:
CodeDescription
INTERNAL(1)Error on the SDK side. Includes JS-bridge or encoding/decoding errors
NETWORKING(2)HTTP errors, parse request/response 
INCONSISTENT(3)Incorrect SDK API usage
@Override
public void onFailedToUpdateConsentInfo(ConsentManagerException error) {
    int errorCode = error.getCode();
    Log.d("APPLICATION_LOG", error.getReason());
}