11package tech.httptoolkit.android
22
33import android.app.Application
4- import android.content.SharedPreferences
54import android.util.Log
65import com.android.installreferrer.api.InstallReferrerClient
76import com.android.installreferrer.api.InstallReferrerClient.InstallReferrerResponse
@@ -12,6 +11,7 @@ import com.google.android.gms.analytics.HitBuilders
1211import com.google.android.gms.analytics.Tracker
1312import io.sentry.Sentry
1413import io.sentry.android.AndroidSentryClientFactory
14+ import java.util.concurrent.atomic.AtomicBoolean
1515import kotlin.coroutines.resume
1616import kotlin.coroutines.suspendCoroutine
1717
@@ -61,6 +61,12 @@ class HttpToolkitApplication : Application() {
6161 // Get & return the actual referrer and return it
6262 Log .i(TAG , " Getting first run referrer..." )
6363 return suspendCoroutine { cont ->
64+ var wasResumed = AtomicBoolean ()
65+ val resume = { value: String? ->
66+ if (wasResumed.getAndSet(true )) {
67+ cont.resume(value)
68+ }
69+ }
6470 val referrerClient = InstallReferrerClient .newBuilder(this ).build()
6571 referrerClient.startConnection(object : InstallReferrerStateListener {
6672
@@ -69,18 +75,18 @@ class HttpToolkitApplication : Application() {
6975 InstallReferrerResponse .OK -> {
7076 val referrer = referrerClient.installReferrer.installReferrer
7177 Log .i(TAG , " Returning first run referrer: $referrer " )
72- cont. resume(referrer)
78+ resume(referrer)
7379 }
7480 else -> {
7581 Log .w(TAG , " Couldn't get install referrer, skipping: $responseCode " )
76- cont. resume(null )
82+ resume(null )
7783 }
7884 }
7985 }
8086
8187 override fun onInstallReferrerServiceDisconnected () {
8288 Log .w(TAG , " Couldn't get install referrer due to disconnection" )
83- cont. resume(null )
89+ resume(null )
8490 }
8591 })
8692 }
0 commit comments