-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTWALaunchWithGAID.kt
51 lines (45 loc) · 1.82 KB
/
TWALaunchWithGAID.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class LauncherActivity : AppCompatActivity() {
var mClient: CustomTabsClient? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
lifecycleScope.launch(Dispatchers.IO) { // Launches a coroutine in the IO dispatcher and gets GA_ID
retrieveGAID()?.let { gaid ->
launch(Dispatchers.Main) {
CustomTabsClient.bindCustomTabsService(
this@LauncherActivity,
"com.android.chrome",
getConnection("https://domain.example?gps_adid=$gaid")
);
}
}
}
}
private fun retrieveGAID(): String? { // To retrieve GA_ID using AdvertisingIdClient dependency
return try {
val adInfo = AdvertisingIdClient.getAdvertisingIdInfo(this)
adInfo.id
} catch (e: Exception) {
e.printStackTrace()
null
}
}
private fun getConnection(url: String): CustomTabsServiceConnection {
return object : CustomTabsServiceConnection() {
override fun onServiceDisconnected(componentName: ComponentName) {
mClient = null
}
override fun onCustomTabsServiceConnected(
name: ComponentName,
client: CustomTabsClient
) {
mClient = client
mClient!!.warmup(0)
val customTabsSession: CustomTabsSession =
mClient!!.newSession(CustomTabsCallback()) ?: return
val intentBuilder = TrustedWebActivityIntentBuilder(Uri.parse(url))
val intent = intentBuilder.build(customTabsSession)
intent.launchTrustedWebActivity(this@LauncherActivity)
}
}
}
}