@@ -22,6 +22,7 @@ import com.uid2.extensions.getMetadata
2222import com.uid2.network.DefaultNetworkSession
2323import com.uid2.network.NetworkSession
2424import com.uid2.storage.StorageManager
25+ import com.uid2.utils.Logger
2526import com.uid2.utils.TimeUtils
2627import kotlinx.coroutines.CoroutineDispatcher
2728import kotlinx.coroutines.CoroutineScope
@@ -83,6 +84,7 @@ public class UID2Manager internal constructor(
8384 private val timeUtils : TimeUtils ,
8485 defaultDispatcher : CoroutineDispatcher ,
8586 initialAutomaticRefreshEnabled : Boolean ,
87+ private val logger : Logger ,
8688) {
8789 private val scope = CoroutineScope (defaultDispatcher + SupervisorJob ())
8890
@@ -156,6 +158,10 @@ public class UID2Manager internal constructor(
156158 initialized = scope.launch {
157159 // Attempt to load the Identity from storage. If successful, we can notify any observers.
158160 storageManager.loadIdentity().let {
161+ if (it.first != null ) {
162+ logger.i(TAG ) { " Restoring previously persisted identity" }
163+ }
164+
159165 validateAndSetIdentity(it.first, it.second, false )
160166 }
161167 }
@@ -168,6 +174,7 @@ public class UID2Manager internal constructor(
168174 * This will also be persisted locally, so that when the application re-launches, we reload this Identity.
169175 */
170176 public fun setIdentity (identity : UID2Identity ): Unit = afterInitialized {
177+ logger.i(TAG ) { " Setting external identity" }
171178 validateAndSetIdentity(identity, null )
172179 }
173180
@@ -177,6 +184,7 @@ public class UID2Manager internal constructor(
177184 public fun resetIdentity (): Unit = afterInitialized {
178185 currentIdentity ? : return @afterInitialized
179186
187+ logger.i(TAG ) { " Resetting identity" }
180188 setIdentityInternal(null , NO_IDENTITY , true )
181189 }
182190
@@ -185,7 +193,10 @@ public class UID2Manager internal constructor(
185193 */
186194 public fun refreshIdentity (): Unit = afterInitialized {
187195 // If we have a valid Identity, let's refresh it.
188- currentIdentity?.let { refreshIdentityInternal(it) }
196+ currentIdentity?.let {
197+ logger.i(TAG ) { " Refreshing identity" }
198+ refreshIdentityInternal(it)
199+ }
189200 }
190201
191202 /* *
@@ -207,6 +218,8 @@ public class UID2Manager internal constructor(
207218 private fun refreshIdentityInternal (identity : UID2Identity ) = scope.launch {
208219 try {
209220 refreshToken(identity).retryWhen { _, attempt ->
221+ logger.i(TAG ) { " Refreshing (Attempt: $attempt )" }
222+
210223 // The delay between retry attempts is based upon how many attempts we have previously had. After a
211224 // number of sequential failures, we will increase the delay.
212225 val delayMs = if (attempt < REFRESH_TOKEN_FAILURE_RETRY_THRESHOLD ) {
@@ -221,10 +234,12 @@ public class UID2Manager internal constructor(
221234 getIdentityPackage(identity, false ).valid
222235 }.single().let {
223236 result ->
237+ logger.i(TAG ) { " Successfully refreshed identity" }
224238 validateAndSetIdentity(result.identity, result.status)
225239 }
226- } catch (_ : UID2Exception ) {
240+ } catch (ex : UID2Exception ) {
227241 // This will happen after we decide to no longer try to update the identity, e.g. it's no longer valid.
242+ logger.e(TAG , ex) { " Error when trying to refresh identity" }
228243 }
229244 }
230245
@@ -313,6 +328,8 @@ public class UID2Manager internal constructor(
313328 checkRefreshExpiresJob = scope.launch {
314329 val timeToCheck = timeUtils.diffToNow(it.refreshExpires) + EXPIRATION_CHECK_TOLERANCE_MS
315330 delay(timeToCheck)
331+
332+ logger.i(TAG ) { " Detected refresh has expired" }
316333 validateAndSetIdentity(it, null , true )
317334 }
318335 }
@@ -323,6 +340,8 @@ public class UID2Manager internal constructor(
323340 checkIdentityExpiresJob = scope.launch {
324341 val timeToCheck = timeUtils.diffToNow(it.identityExpires) + EXPIRATION_CHECK_TOLERANCE_MS
325342 delay(timeToCheck)
343+
344+ logger.i(TAG ) { " Detected identity has expired" }
326345 validateAndSetIdentity(it, null , true )
327346 }
328347 }
@@ -336,12 +355,18 @@ public class UID2Manager internal constructor(
336355 ) {
337356 // Process Opt Out.
338357 if (status == OPT_OUT ) {
358+ logger.i(TAG ) { " User opt-out detected" }
339359 setIdentityInternal(null , OPT_OUT )
340360 return
341361 }
342362
343363 // Check to see the validity of the Identity, updating our internal state.
344364 val validity = getIdentityPackage(identity, currentIdentity == null )
365+
366+ logger.i(TAG ) {
367+ " Updating identity (Identity: ${validity.identity != null } , Status: ${validity.status} , " +
368+ " Updating Storage: $updateStorage )"
369+ }
345370 setIdentityInternal(validity.identity, validity.status, updateStorage)
346371 }
347372
@@ -396,6 +421,8 @@ public class UID2Manager internal constructor(
396421 }
397422
398423 public companion object {
424+ private const val TAG = " UID2Manager"
425+
399426 // The default API server.
400427 private const val UID2_API_URL_KEY = " uid2_api_url"
401428 private const val UID2_API_URL_DEFAULT = " https://prod.uidapi.com"
@@ -420,15 +447,10 @@ public class UID2Manager internal constructor(
420447 private var api: String = UID2_API_URL_DEFAULT
421448 private var networkSession: NetworkSession = DefaultNetworkSession ()
422449 private var storageManager: StorageManager ? = null
450+ private var isLoggingEnabled: Boolean = false
423451
424452 private var instance: UID2Manager ? = null
425453
426- /* *
427- * Initializes the class with the given [Context].
428- */
429- @JvmStatic
430- public fun init (context : Context ): Unit = init (context, DefaultNetworkSession ())
431-
432454 /* *
433455 * Initializes the class with the given [Context], along with a [NetworkSession] that will be responsible
434456 * for making any required network calls.
@@ -439,8 +461,13 @@ public class UID2Manager internal constructor(
439461 * The default implementation supported by the SDK can be found as [DefaultNetworkSession].
440462 */
441463 @JvmStatic
464+ @JvmOverloads
442465 @Throws(InitializationException ::class )
443- public fun init (context : Context , networkSession : NetworkSession ) {
466+ public fun init (
467+ context : Context ,
468+ networkSession : NetworkSession = DefaultNetworkSession (),
469+ isLoggingEnabled : Boolean = false,
470+ ) {
444471 if (instance != null ) {
445472 throw InitializationException ()
446473 }
@@ -449,7 +476,8 @@ public class UID2Manager internal constructor(
449476
450477 this .api = metadata?.getString(UID2_API_URL_KEY , UID2_API_URL_DEFAULT ) ? : UID2_API_URL_DEFAULT
451478 this .networkSession = networkSession
452- this .storageManager = StorageManager .getInstance(context)
479+ this .storageManager = StorageManager .getInstance(context.applicationContext)
480+ this .isLoggingEnabled = isLoggingEnabled
453481 }
454482
455483 /* *
@@ -466,16 +494,19 @@ public class UID2Manager internal constructor(
466494 @JvmStatic
467495 public fun getInstance (): UID2Manager {
468496 val storage = storageManager ? : throw InitializationException ()
497+ val logger = Logger (isLoggingEnabled)
469498
470499 return instance ? : UID2Manager (
471500 UID2Client (
472501 api,
473502 networkSession,
503+ logger,
474504 ),
475505 storage,
476506 TimeUtils (),
477507 Dispatchers .Default ,
478508 true ,
509+ logger,
479510 ).apply {
480511 instance = this
481512 }
0 commit comments