-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathOSIABCustomTabsSessionHelper.kt
More file actions
136 lines (125 loc) · 5.33 KB
/
OSIABCustomTabsSessionHelper.kt
File metadata and controls
136 lines (125 loc) · 5.33 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
@file:OptIn(com.outsystems.plugins.inappbrowser.osinappbrowserlib.RequiresEventBridgeRegistration::class)
package com.outsystems.plugins.inappbrowser.osinappbrowserlib.helpers
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Bundle
import androidx.browser.customtabs.CustomTabsCallback
import androidx.browser.customtabs.CustomTabsClient
import androidx.browser.customtabs.CustomTabsServiceConnection
import androidx.browser.customtabs.CustomTabsSession
import com.outsystems.plugins.inappbrowser.osinappbrowserlib.OSIABEvents
import com.outsystems.plugins.inappbrowser.osinappbrowserlib.views.OSIABCustomTabsControllerActivity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
class OSIABCustomTabsSessionHelper: OSIABCustomTabsSessionHelperInterface {
private fun getDefaultCustomTabsPackageName(context: Context): String? {
val activityIntent = Intent(Intent.ACTION_VIEW, Uri.parse("http://"))
val resolvedActivityList = context.packageManager.queryIntentActivities(activityIntent, PackageManager.MATCH_ALL)
return CustomTabsClient.getPackageName(
context,
resolvedActivityList.map { it.activityInfo.packageName },
false
)
}
private fun initializeCustomTabsSession(
browserId: String,
context: Context,
packageName: String,
lifecycleScope: CoroutineScope,
flowHelper: OSIABFlowHelperInterface,
customTabsSessionCallback: (CustomTabsSession?) -> Unit
) {
CustomTabsClient.bindCustomTabsService(
context,
packageName,
object : CustomTabsServiceConnection() {
override fun onCustomTabsServiceConnected(name: ComponentName, client: CustomTabsClient) {
client.warmup(0L)
customTabsSessionCallback(
client.newSession(CustomTabsCallbackImpl(browserId, lifecycleScope, flowHelper))
)
}
override fun onServiceDisconnected(name: ComponentName) {
context.unbindService(this)
customTabsSessionCallback(null)
}
}
)
}
private inner class CustomTabsCallbackImpl(
private val browserId: String,
private val lifecycleScope: CoroutineScope,
flowHelper: OSIABFlowHelperInterface,
) : CustomTabsCallback() {
private var isCustomTabsActivityOnTop = false
private var pendingTabHiddenEvent = false
init {
var browserEventsJob: Job? = null
browserEventsJob = flowHelper.listenToEvents(browserId, lifecycleScope) { event ->
if(event is OSIABEvents.OSIABCustomTabsEvent) {
when (event.action) {
OSIABCustomTabsControllerActivity.EVENT_CUSTOM_TABS_RESUMED -> {
isCustomTabsActivityOnTop = true
if (pendingTabHiddenEvent) {
pendingTabHiddenEvent = false
lifecycleScope.launch {
OSIABEvents.postEvent(OSIABEvents.BrowserFinished(browserId))
}
}
}
OSIABCustomTabsControllerActivity.EVENT_CUSTOM_TABS_PAUSED -> {
isCustomTabsActivityOnTop = false
pendingTabHiddenEvent = false
}
OSIABCustomTabsControllerActivity.EVENT_CUSTOM_TABS_DESTROYED -> {
browserEventsJob?.cancel()
}
}
}
}
}
override fun onNavigationEvent(navigationEvent: Int, extras: Bundle?) {
super.onNavigationEvent(navigationEvent, extras)
val browserEvent = when (navigationEvent) {
NAVIGATION_FINISHED -> OSIABEvents.BrowserPageLoaded(browserId)
TAB_HIDDEN -> {
if(isCustomTabsActivityOnTop) {
OSIABEvents.BrowserFinished(browserId)
}
else {
// App not open but custom tabs is hidden (home button, recent apps, etc.)
pendingTabHiddenEvent = true
return
}
}
else -> return
}
lifecycleScope.launch {
OSIABEvents.postEvent(browserEvent)
}
}
}
override suspend fun generateNewCustomTabsSession(
browserId: String,
context: Context,
lifecycleScope: CoroutineScope,
flowHelper: OSIABFlowHelperInterface,
customTabsSessionCallback: (CustomTabsSession?) -> Unit
) {
val packageName = getDefaultCustomTabsPackageName(context)
packageName?.let {
initializeCustomTabsSession(
browserId,
context,
it,
lifecycleScope,
flowHelper,
customTabsSessionCallback
)
} ?: customTabsSessionCallback(null)
}
}