|
| 1 | +package com.uid2 |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import com.uid2.UID2Manager.Companion.APPLICATION_ID_DEFAULT |
| 5 | +import com.uid2.network.DefaultNetworkSession |
| 6 | +import com.uid2.network.NetworkSession |
| 7 | +import com.uid2.storage.FileStorageManager |
| 8 | +import com.uid2.storage.FileStorageManager.Store.EUID |
| 9 | +import com.uid2.storage.StorageManager |
| 10 | +import com.uid2.utils.InputUtils |
| 11 | +import com.uid2.utils.Logger |
| 12 | +import com.uid2.utils.TimeUtils |
| 13 | +import kotlinx.coroutines.Dispatchers |
| 14 | + |
| 15 | +public object EUIDManager { |
| 16 | + |
| 17 | + public sealed interface Environment { |
| 18 | + public val serverUrl: String |
| 19 | + |
| 20 | + /** |
| 21 | + * AWS EU West 2 (London). |
| 22 | + */ |
| 23 | + public data object London : Environment { |
| 24 | + override val serverUrl: String = EUID_API_URL_PRODUCTION |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * The default Environment, equivalent to [London]. |
| 29 | + */ |
| 30 | + public data object Production : Environment { |
| 31 | + override val serverUrl: String = EUID_API_URL_PRODUCTION |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * An Environment with its own API endpoint, such as for integration testing. |
| 36 | + */ |
| 37 | + public data class Custom( |
| 38 | + override val serverUrl: String, |
| 39 | + ) : Environment |
| 40 | + } |
| 41 | + |
| 42 | + // The default API server. |
| 43 | + internal const val EUID_API_URL_PRODUCTION = "https://prod.euid.eu/v2" |
| 44 | + |
| 45 | + private var serverUrl: String = EUID_API_URL_PRODUCTION |
| 46 | + private var applicationId: String = APPLICATION_ID_DEFAULT |
| 47 | + private var networkSession: NetworkSession = DefaultNetworkSession() |
| 48 | + private var storageManager: StorageManager? = null |
| 49 | + private var isLoggingEnabled: Boolean = false |
| 50 | + |
| 51 | + private var instance: UID2Manager? = null |
| 52 | + |
| 53 | + /** |
| 54 | + * Initializes the class with the given [Context], along with a [NetworkSession] that will be responsible |
| 55 | + * for making any required network calls. |
| 56 | + * |
| 57 | + * @param context The context to initialise from. This will be used to obtain the package's metadata to extract |
| 58 | + * the API URL. |
| 59 | + * @param environment The API Environment to use. |
| 60 | + * @param networkSession A custom [NetworkSession] which can be used for making any required network calls. |
| 61 | + * The default implementation supported by the SDK can be found as [DefaultNetworkSession]. |
| 62 | + */ |
| 63 | + @JvmStatic |
| 64 | + @JvmOverloads |
| 65 | + @Throws(InitializationException::class) |
| 66 | + public fun init( |
| 67 | + context: Context, |
| 68 | + environment: Environment = Environment.Production, |
| 69 | + networkSession: NetworkSession = DefaultNetworkSession(), |
| 70 | + isLoggingEnabled: Boolean = false, |
| 71 | + ) { |
| 72 | + if (instance != null) { |
| 73 | + throw InitializationException() |
| 74 | + } |
| 75 | + |
| 76 | + this.serverUrl = environment.serverUrl |
| 77 | + this.applicationId = context.packageName |
| 78 | + this.networkSession = networkSession |
| 79 | + this.storageManager = FileStorageManager(context.applicationContext, EUID) |
| 80 | + this.isLoggingEnabled = isLoggingEnabled |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Returns True if the manager is already initialised, otherwise False. |
| 85 | + */ |
| 86 | + @JvmStatic |
| 87 | + public fun isInitialized(): Boolean = instance != null |
| 88 | + |
| 89 | + /** |
| 90 | + * Gets the current singleton instance of the manager. |
| 91 | + * |
| 92 | + * @throws InitializationException Thrown if the manager has not yet been initialised. |
| 93 | + */ |
| 94 | + @JvmStatic |
| 95 | + public fun getInstance(): UID2Manager { |
| 96 | + if (storageManager == null) { |
| 97 | + throw InitializationException() |
| 98 | + } |
| 99 | + val storage = storageManager ?: throw InitializationException() |
| 100 | + val logger = Logger(isLoggingEnabled) |
| 101 | + |
| 102 | + return instance ?: UID2Manager( |
| 103 | + UID2Client( |
| 104 | + apiUrl = serverUrl, |
| 105 | + session = networkSession, |
| 106 | + applicationId = applicationId, |
| 107 | + logger = logger, |
| 108 | + ), |
| 109 | + storage, |
| 110 | + TimeUtils, |
| 111 | + InputUtils(), |
| 112 | + Dispatchers.Default, |
| 113 | + true, |
| 114 | + logger, |
| 115 | + ).apply { |
| 116 | + instance = this |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments