11package co.optable.sdk.core
22
3+ import android.util.Log
34import co.optable.sdk.OptableConfig
45import com.google.android.gms.ads.identifier.AdvertisingIdClient
5- import kotlinx.coroutines.GlobalScope
6- import kotlinx.coroutines.MainScope
7- import kotlinx.coroutines.launch
6+ import kotlinx.coroutines.CompletableDeferred
7+ import kotlinx.coroutines.Dispatchers
8+ import kotlinx.coroutines.withContext
9+ import kotlinx.coroutines.withTimeoutOrNull
810
911internal class GoogleAdIdManager (
1012 val config : OptableConfig ,
@@ -15,20 +17,7 @@ internal class GoogleAdIdManager(
1517 private var limitAdTracking: Boolean? = null
1618 }
1719
18- fun updateAdvertisingId () {
19- GlobalScope .launch {
20- var adInfo: AdvertisingIdClient .Info ? = null
21- try {
22- adInfo = AdvertisingIdClient .getAdvertisingIdInfo(config.context)
23- } catch (_: Exception ) {
24- }
25-
26- MainScope ().launch {
27- adId = adInfo?.id
28- limitAdTracking = adInfo?.isLimitAdTrackingEnabled
29- }
30- }
31- }
20+ val deferredTask = CompletableDeferred <String ?>()
3221
3322 fun getId (): String? {
3423 if (limitAdTracking == true ) {
@@ -37,4 +26,30 @@ internal class GoogleAdIdManager(
3726 return adId
3827 }
3928
29+ suspend fun fetchAdvertisingId () {
30+ if (config.skipAdvertisingIdDetection) {
31+ deferredTask.complete(null )
32+ return
33+ }
34+
35+ val id = withContext(Dispatchers .IO ) {
36+ withTimeoutOrNull(3_000 ) {
37+ fetch()
38+ }
39+ }
40+ deferredTask.complete(id)
41+ }
42+
43+ private fun fetch (): String? {
44+ try {
45+ val adInfo = AdvertisingIdClient .getAdvertisingIdInfo(config.context)
46+ adId = adInfo.id
47+ limitAdTracking = adInfo.isLimitAdTrackingEnabled
48+ return adInfo.id
49+ } catch (exception: Exception ) {
50+ Log .w(" OptableGaidManager" , " Failed to fetch advertising ID: " + exception.message)
51+ }
52+ return null
53+ }
54+
4055}
0 commit comments