Code Examples

Check out some useful snippets and code examples for initialization below.

package com.example.testapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.preference.PreferenceManager
import com.liveramp.ats.LRAtsManager
import com.liveramp.ats.LRError
import com.liveramp.ats.callbacks.LRCompletionHandlerCallback
import com.liveramp.ats.callbacks.LREnvelopeCallback
import com.liveramp.ats.model.Configuration
import com.liveramp.ats.model.Envelope
import com.liveramp.ats.model.LRAtsConfiguration
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json

class MainActivity : AppCompatActivity() {

    private val jsonFormat = Json { ignoreUnknownKeys = true }
           
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        // set fake consent (for testing purposes only!), this should be handled by CMP SDK
        setSharedPreferencesKey(
            "IABTCF_VendorConsents",
            "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001"
        )
        setSharedPreferencesKey("IABTCF_PurposeConsents", "1111111111")
        // create config object
        val publisherConfiguration = jsonFormat.decodeFromString<Configuration>(getFallbackConfigurationJson("fallback_configuration.json") ?: "")
        // init ATS SDK
        LRAtsManager.initialize(
            LRAtsConfiguration("40b867f9-93cc-4687-a2c7-d02bed91aaee", publisherConfiguration),
            object : LRCompletionHandlerCallback {
                override fun invoke(success: Boolean, error: LRError?) {
                    if (success) {
                        // get envelope for email
                        LRAtsManager.getEnvelope(LREmailIdentifier("[email protected]"), object : LREnvelopeCallback {
                            override fun invoke(envelope: Envelope?, error: LRError?) {
                                // use envelope here
                            }
                        })
                        // or phone number
                        LRAtsManager.getEnvelope(LRPhoneIdentifier("+14842918944"), object : LREnvelopeCallback {
                            override fun invoke(envelope: Envelope?, error: LRError?) {
                                // use envelope here
                            }
                        })
                        // or for id
                        LRAtsManager.getEnvelope(LRCustomerIdentifier(11), object : LREnvelopeCallback {
                            override fun invoke(envelope: Envelope?, error: LRError?) {
                                // use envelope here
                            }
                        })
                    }
                }
            })
    }

    private fun setSharedPreferencesKey(key: String, value: String) {
        val preferences = PreferenceManager.getDefaultSharedPreferences(applicationContext)
        val editor = preferences.edit()
        editor.putString(key, value)
        editor.apply()
    }
    
    private fun getFallbackConfigurationJson(fileName: String): String? {
        var fileContent: String? = null
        val bufferedReader: BufferedReader = assets.open(fileName).bufferedReader()
        try {
            fileContent = bufferedReader.use { it.readText() }
        } catch (e: FileNotFoundException) {
            logI("File reading failed: ${e.message}.")
        } catch (e: Exception) {
            logE("File reading failed: ${e.message}")
        } finally {
            bufferedReader.close()
        }
        return fileContent
    }
}
package com.liveramp.atstestapp;

import android.content.SharedPreferences;
import android.os.Bundle;

import com.google.gson.Gson;
import com.liveramp.ats.LRAtsManager;
import com.liveramp.ats.LRError;
import com.liveramp.ats.callbacks.LRCompletionHandlerCallback;
import com.liveramp.ats.callbacks.LREnvelopeCallback;
import com.liveramp.ats.model.Configuration;
import com.liveramp.ats.model.Envelope;
import com.liveramp.ats.model.LRAtsConfiguration;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceManager;

public class MainActivity extends AppCompatActivity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // set fake consent (for testing purposes only!), this should be handled by CMP SDK
        setSharedPreferencesKey(
                "IABTCF_VendorConsents",
                "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001"
        );
        setSharedPreferencesKey("IABTCF_PurposeConsents", "1111111111");
        Gson gson = new Gson();
        // create config object
        Configuration fallbackConfiguration = gson.fromJson(getFallbackConfigurationJson("fallback_configuration.json"), Configuration.class);
        // init ATS SDK
        LRAtsManager.INSTANCE.initialize(new LRAtsConfiguration("40b867f9-93cc-4687-a2c7-d02bed91aaee", fallbackConfiguration), new LRCompletionHandlerCallback() {
            @Override
            public void invoke(boolean success, @Nullable LRError lrError) {
                if (success) {
                    // get envelope for email
                    LRAtsManager.INSTANCE.getEnvelope(new LREmailIdentifier("[email protected]"), new LREnvelopeCallback() {
                        @Override
                        public void invoke(@Nullable Envelope envelope, @Nullable LRError lrError) {
                            // use envelope here
                        }
                    });
                    // or phone number
                    LRAtsManager.INSTANCE.getEnvelope(new LRPhoneIdentifier("+14842918944"), new LREnvelopeCallback() {
                        @Override
                        public void invoke(@Nullable Envelope envelope, @Nullable LRError lrError) {
                            // use envelope here
                        }
                    });
                    // or for id
                    LRAtsManager.INSTANCE.getEnvelope(new LRCustomerIdentifier(11), new LREnvelopeCallback() {
                        @Override
                        public void invoke(@Nullable Envelope envelope, @Nullable LRError lrError) {
                            // use envelope here
                        }
                    });
                }
            }
        });
    }

    private void setSharedPreferencesKey(String key, String value) {
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(key, value);
        editor.apply();
    }
    
    private String getFallbackConfigurationJson(String fileName) {
        StringBuilder sb = new StringBuilder();
        try (InputStream is = getAssets().open(fileName);
             BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8))) {
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }
        } catch (Exception e) {
            // log error
        }
        return sb.toString();
    }
}