|
| 1 | +package com.uid2.securesignals.gma |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import com.google.android.gms.ads.AdError |
| 5 | +import com.google.android.gms.ads.mediation.InitializationCompleteCallback |
| 6 | +import com.google.android.gms.ads.mediation.MediationConfiguration |
| 7 | +import com.google.android.gms.ads.mediation.rtb.RtbAdapter |
| 8 | +import com.google.android.gms.ads.mediation.rtb.RtbSignalData |
| 9 | +import com.google.android.gms.ads.mediation.rtb.SignalCallbacks |
| 10 | +import com.uid2.EUIDManager |
| 11 | +import com.uid2.UID2 |
| 12 | +import com.google.android.gms.ads.mediation.VersionInfo as GmaVersionInfo |
| 13 | + |
| 14 | +/** |
| 15 | + * An implementation of Google's GMS RtbAdapter that integrates UID2 tokens, accessed via the UID2Manager. |
| 16 | + */ |
| 17 | +public class EUIDMediationAdapter : RtbAdapter() { |
| 18 | + |
| 19 | + /** |
| 20 | + * Gets the version of the UID2 SDK. |
| 21 | + */ |
| 22 | + @Suppress("DEPRECATION") |
| 23 | + public override fun getSDKVersionInfo(): GmaVersionInfo = UID2.getVersionInfo().let { |
| 24 | + GmaVersionInfo(it.major, it.minor, it.patch) |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Gets the version of the UID2 Secure Signals plugin. |
| 29 | + */ |
| 30 | + @Suppress("DEPRECATION") |
| 31 | + public override fun getVersionInfo(): GmaVersionInfo = PluginVersion.getVersionInfo().let { |
| 32 | + GmaVersionInfo(it.major, it.minor, it.patch) |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * Initialises the UID2 SDK with the given Context. |
| 37 | + */ |
| 38 | + override fun initialize( |
| 39 | + context: Context, |
| 40 | + initializationCompleteCallback: InitializationCompleteCallback, |
| 41 | + mediationConfigurations: MutableList<MediationConfiguration>, |
| 42 | + ) { |
| 43 | + // It's possible that the UID2Manager is already initialised. If so, it's a no-op. |
| 44 | + if (!EUIDManager.isInitialized()) { |
| 45 | + EUIDManager.init(context) |
| 46 | + } |
| 47 | + |
| 48 | + // After we've asked to initialize the manager, we should wait until it's complete before reporting success. |
| 49 | + // This will potentially allow any previously persisted identity to be fully restored before we allow any |
| 50 | + // signals to be collected. |
| 51 | + EUIDManager.getInstance().addOnInitializedListener(initializationCompleteCallback::onInitializationSucceeded) |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Collects the UID2 advertising token, if available. |
| 56 | + */ |
| 57 | + override fun collectSignals(rtbSignalData: RtbSignalData, signalCallbacks: SignalCallbacks) { |
| 58 | + EUIDManager.getInstance().let { manager -> |
| 59 | + val token = manager.getAdvertisingToken() |
| 60 | + if (token != null) { |
| 61 | + signalCallbacks.onSuccess(token) |
| 62 | + } else { |
| 63 | + // We include the IdentityStatus in the "error" to have better visibility on why the Advertising Token |
| 64 | + // was not present. There are a number of valid reasons why we don't have a token, but we are still |
| 65 | + // required to report these as "failures". |
| 66 | + signalCallbacks.onFailure( |
| 67 | + AdError( |
| 68 | + manager.currentIdentityStatus.value, |
| 69 | + "No Advertising Token", |
| 70 | + "UID2", |
| 71 | + ), |
| 72 | + ) |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments