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