-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
253 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package com.uid2 | ||
|
||
import android.content.Context | ||
import com.uid2.UID2Manager.Companion | ||
import com.uid2.UID2Manager.Companion.APPLICATION_ID_DEFAULT | ||
import com.uid2.UID2Manager.Companion.UID2_API_URL_DEFAULT | ||
import com.uid2.network.DefaultNetworkSession | ||
import com.uid2.network.NetworkSession | ||
import com.uid2.storage.FileStorageManager | ||
import com.uid2.storage.FileStorageManager.Store.EUID | ||
import com.uid2.storage.StorageManager | ||
import com.uid2.utils.InputUtils | ||
import com.uid2.utils.Logger | ||
import com.uid2.utils.TimeUtils | ||
import kotlinx.coroutines.Dispatchers | ||
|
||
public class EUIDManager { | ||
|
||
public companion object { | ||
public data class Environment private constructor( | ||
val serverUrl: String | ||
) { | ||
public companion object { | ||
// AWS EU West 2 (London) | ||
public val london: Environment = Environment("https://prod.euid.eu/v2") | ||
|
||
// Equivalent to `london` | ||
public val production: Environment = london | ||
public fun custom(serverUrl: String): Environment { | ||
return Environment(serverUrl) | ||
} | ||
} | ||
} | ||
|
||
private var serverUrl: String = UID2_API_URL_DEFAULT | ||
private var applicationId: String = APPLICATION_ID_DEFAULT | ||
private var networkSession: NetworkSession = DefaultNetworkSession() | ||
private var storageManager: StorageManager? = null | ||
private var isLoggingEnabled: Boolean = false | ||
|
||
private var instance: UID2Manager? = null | ||
|
||
/** | ||
* Initializes the class with the given [Context], along with a [NetworkSession] that will be responsible | ||
* for making any required network calls. | ||
* | ||
* @param context The context to initialise from. This will be used to obtain the package's metadata to extract | ||
* the API URL. | ||
* @param networkSession A custom [NetworkSession] which can be used for making any required network calls. | ||
* The default implementation supported by the SDK can be found as [DefaultNetworkSession]. | ||
*/ | ||
@JvmStatic | ||
@JvmOverloads | ||
@Throws(InitializationException::class) | ||
public fun init( | ||
context: Context, | ||
environment: Environment = Environment.production, | ||
networkSession: NetworkSession = DefaultNetworkSession(), | ||
isLoggingEnabled: Boolean = false, | ||
) { | ||
if (instance != null) { | ||
throw InitializationException() | ||
} | ||
|
||
this.serverUrl = environment.serverUrl | ||
this.applicationId = context.packageName | ||
this.networkSession = networkSession | ||
this.storageManager = FileStorageManager(context.applicationContext, EUID) | ||
this.isLoggingEnabled = isLoggingEnabled | ||
} | ||
|
||
/** | ||
* Returns True if the manager is already initialised, otherwise False. | ||
*/ | ||
@JvmStatic | ||
public fun isInitialized(): Boolean = instance != null | ||
|
||
/** | ||
* Gets the current singleton instance of the manager. | ||
* | ||
* @throws InitializationException Thrown if the manager has not yet been initialised. | ||
*/ | ||
@JvmStatic | ||
public fun getInstance(): UID2Manager { | ||
val storage = storageManager ?: throw InitializationException() | ||
val logger = Logger(isLoggingEnabled) | ||
|
||
return instance ?: UID2Manager( | ||
UID2Client( | ||
apiUrl = serverUrl, | ||
session = networkSession, | ||
applicationId = applicationId, | ||
logger = logger, | ||
), | ||
storage, | ||
TimeUtils, | ||
InputUtils(), | ||
Dispatchers.Default, | ||
true, | ||
logger, | ||
).apply { | ||
instance = this | ||
} | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
securesignals-gma/src/main/java/com/uid2/securesignals/gma/EUIDMediationAdapter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package com.uid2.securesignals.gma | ||
|
||
import android.content.Context | ||
import com.google.android.gms.ads.AdError | ||
import com.google.android.gms.ads.mediation.InitializationCompleteCallback | ||
import com.google.android.gms.ads.mediation.MediationConfiguration | ||
import com.google.android.gms.ads.mediation.rtb.RtbAdapter | ||
import com.google.android.gms.ads.mediation.rtb.RtbSignalData | ||
import com.google.android.gms.ads.mediation.rtb.SignalCallbacks | ||
import com.uid2.EUIDManager | ||
import com.uid2.UID2 | ||
import com.google.android.gms.ads.mediation.VersionInfo as GmaVersionInfo | ||
|
||
/** | ||
* An implementation of Google's GMS RtbAdapter that integrates UID2 tokens, accessed via the UID2Manager. | ||
*/ | ||
public class EUIDMediationAdapter : RtbAdapter() { | ||
|
||
/** | ||
* Gets the version of the UID2 SDK. | ||
*/ | ||
@Suppress("DEPRECATION") | ||
public override fun getSDKVersionInfo(): GmaVersionInfo = UID2.getVersionInfo().let { | ||
GmaVersionInfo(it.major, it.minor, it.patch) | ||
} | ||
|
||
/** | ||
* Gets the version of the UID2 Secure Signals plugin. | ||
*/ | ||
@Suppress("DEPRECATION") | ||
public override fun getVersionInfo(): GmaVersionInfo = PluginVersion.getVersionInfo().let { | ||
GmaVersionInfo(it.major, it.minor, it.patch) | ||
} | ||
|
||
/** | ||
* Initialises the UID2 SDK with the given Context. | ||
*/ | ||
override fun initialize( | ||
context: Context, | ||
initializationCompleteCallback: InitializationCompleteCallback, | ||
mediationConfigurations: MutableList<MediationConfiguration>, | ||
) { | ||
// It's possible that the UID2Manager is already initialised. If so, it's a no-op. | ||
if (!EUIDManager.isInitialized()) { | ||
EUIDManager.init(context) | ||
} | ||
|
||
// After we've asked to initialize the manager, we should wait until it's complete before reporting success. | ||
// This will potentially allow any previously persisted identity to be fully restored before we allow any | ||
// signals to be collected. | ||
EUIDManager.getInstance().addOnInitializedListener(initializationCompleteCallback::onInitializationSucceeded) | ||
} | ||
|
||
/** | ||
* Collects the UID2 advertising token, if available. | ||
*/ | ||
override fun collectSignals(rtbSignalData: RtbSignalData, signalCallbacks: SignalCallbacks) { | ||
EUIDManager.getInstance().let { manager -> | ||
val token = manager.getAdvertisingToken() | ||
if (token != null) { | ||
signalCallbacks.onSuccess(token) | ||
} else { | ||
// We include the IdentityStatus in the "error" to have better visibility on why the Advertising Token | ||
// was not present. There are a number of valid reasons why we don't have a token, but we are still | ||
// required to report these as "failures". | ||
signalCallbacks.onFailure( | ||
AdError( | ||
manager.currentIdentityStatus.value, | ||
"No Advertising Token", | ||
"UID2", | ||
), | ||
) | ||
} | ||
} | ||
} | ||
} |