Skip to content

Commit f5ca722

Browse files
undo android expo updates from gaurav
1 parent b0bda5a commit f5ca722

File tree

7 files changed

+837
-1238
lines changed

7 files changed

+837
-1238
lines changed

android/src/main/java/com/reactnativewebbrowser/InternalActicityProvider.kt renamed to android/src/main/java/com/reactnativewebbrowser/InternalActivityProvider.kt

File renamed without changes.

android/src/main/java/com/reactnativewebbrowser/InternalCustomTabsActivitiesHelper.kt

Lines changed: 64 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -8,116 +8,99 @@ import android.net.Uri
88
import androidx.browser.customtabs.CustomTabsClient
99
import androidx.browser.customtabs.CustomTabsIntent
1010
import androidx.browser.customtabs.CustomTabsService
11-
import com.reactnativewebbrowser.error.CurrentActivityNotFoundException
12-
import com.reactnativewebbrowser.error.PackageManagerNotFoundException
1311
import java.util.ArrayList
12+
import java.util.Collections
1413
import java.util.LinkedHashSet
1514

16-
private const val DUMMY_URL = "https://web3auth.io"
17-
18-
class InternalCustomTabsActivitiesHelper(
19-
private val activityProvider: ActivityProvider?
20-
) {
2115

22-
// region Actual custom tabs activities helper methods
16+
import androidx.browser.customtabs.CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION
17+
import com.reactnativewebbrowser.error.CurrentActivityNotFoundException
18+
import com.reactnativewebbrowser.error.PackageManagerNotFoundException
2319

24-
/**
25-
* @throws CurrentActivityNotFoundException
26-
* @throws PackageManagerNotFoundException
27-
*/
28-
fun canResolveIntent(intent: Intent): Boolean = getResolvingActivities(intent).isNotEmpty()
20+
class InternalCustomTabsActivitiesHelper(val activityProvider: ActivityProvider) :
21+
CustomTabsActivitiesHelper {
2922

30-
/**
31-
* @throws PackageManagerNotFoundException
32-
* @throws CurrentActivityNotFoundException
33-
*/
34-
val customTabsResolvingActivities: ArrayList<String>
35-
get() = getResolvingActivities(createDefaultCustomTabsIntent())
36-
.mapToDistinctArrayList { resolveInfo: ResolveInfo ->
37-
resolveInfo.activityInfo.packageName
38-
}
23+
override val customTabsResolvingActivities: ArrayList<String>
24+
get() = mapCollectionToDistinctArrayList(
25+
getResolvingActivities(
26+
createDefaultCustomTabsIntent()
27+
)
28+
) { resolveInfo -> resolveInfo.activityInfo.packageName }
3929

40-
/**
41-
* @throws PackageManagerNotFoundException
42-
* @throws CurrentActivityNotFoundException
43-
*/
44-
val customTabsResolvingServices: ArrayList<String>
45-
get() = packageManager.queryIntentServices(createDefaultCustomTabsServiceIntent(), 0)
46-
.mapToDistinctArrayList { resolveInfo: ResolveInfo ->
47-
resolveInfo.serviceInfo.packageName
48-
}
30+
override val customTabsResolvingServices: ArrayList<String>
31+
get() {
32+
return mapCollectionToDistinctArrayList(
33+
packageManager.queryIntentServices(
34+
createDefaultCustomTabsServiceIntent(),
35+
0
36+
)
37+
) { resolveInfo -> resolveInfo.serviceInfo.packageName }
38+
}
4939

50-
/**
51-
* @throws PackageManagerNotFoundException
52-
* @throws CurrentActivityNotFoundException
53-
*/
54-
fun getPreferredCustomTabsResolvingActivity(packages: List<String?>?): String? {
55-
val resolvedPackages = packages ?: customTabsResolvingActivities
56-
return CustomTabsClient.getPackageName(currentActivity, resolvedPackages)
40+
override fun getPreferredCustomTabsResolvingActivity(packages: List<String>?): String? {
41+
var packages: List<String>? = packages
42+
if (packages == null) packages = customTabsResolvingActivities
43+
return CustomTabsClient.getPackageName(currentActivity, packages)
5744
}
5845

59-
/**
60-
* @throws PackageManagerNotFoundException
61-
* @throws CurrentActivityNotFoundException
62-
*/
63-
val defaultCustomTabsResolvingActivity: String?
46+
override val defaultCustomTabsResolvingActivity: String?
6447
get() {
65-
val info = packageManager.resolveActivity(createDefaultCustomTabsIntent(), 0)
48+
val info: ResolveInfo? =
49+
packageManager.resolveActivity(createDefaultCustomTabsIntent(), 0)
6650
return info?.activityInfo?.packageName
6751
}
6852

69-
/**
70-
* @throws CurrentActivityNotFoundException
71-
*/
72-
fun startCustomTabs(intent: Intent) {
73-
currentActivity.startActivity(intent)
53+
override fun canResolveIntent(intent: Intent): Boolean {
54+
return getResolvingActivities(intent).isNotEmpty()
7455
}
7556

76-
// endregion
77-
78-
// region Private helpers
57+
override fun startCustomTabs(intent: Intent) {
58+
currentActivity.startActivity(intent)
59+
}
7960

80-
/**
81-
* @throws CurrentActivityNotFoundException
82-
* @throws PackageManagerNotFoundException
83-
*/
8461
private fun getResolvingActivities(intent: Intent): List<ResolveInfo> {
8562
return packageManager.queryIntentActivities(intent, 0)
8663
}
8764

88-
/**
89-
* @throws CurrentActivityNotFoundException
90-
* @throws PackageManagerNotFoundException
91-
*/
9265
private val packageManager: PackageManager
93-
get() = currentActivity.packageManager ?: throw PackageManagerNotFoundException()
66+
get() {
67+
val pm: PackageManager? = currentActivity.packageManager
68+
if (pm == null) throw PackageManagerNotFoundException() else return pm
69+
}
9470

95-
/**
96-
* @throws CurrentActivityNotFoundException
97-
*/
9871
private val currentActivity: Activity
9972
get() {
100-
return activityProvider?.getCurrentActivity() ?: throw CurrentActivityNotFoundException()
73+
return activityProvider.getCurrentActivity()
74+
?: throw CurrentActivityNotFoundException()
10175
}
10276

103-
// endregion
104-
}
105-
106-
private inline fun <T, R> Collection<T>.mapToDistinctArrayList(mapper: (T) -> R): ArrayList<R> {
107-
val resultSet = LinkedHashSet<R>()
108-
for (element in this) {
109-
resultSet.add(mapper.invoke(element))
77+
private fun createDefaultCustomTabsIntent(): Intent {
78+
val builder: CustomTabsIntent.Builder = CustomTabsIntent.Builder()
79+
val customTabsIntent: CustomTabsIntent = builder.build()
80+
val intent: Intent = customTabsIntent.intent
81+
intent.data = Uri.parse(DUMMY_URL)
82+
return intent
11083
}
111-
return ArrayList(resultSet)
112-
}
11384

114-
private fun createDefaultCustomTabsIntent(): Intent {
115-
val customTabsIntent = CustomTabsIntent.Builder().build()
116-
return customTabsIntent.intent.apply {
117-
data = Uri.parse(DUMMY_URL)
85+
private fun createDefaultCustomTabsServiceIntent(): Intent {
86+
val serviceIntent: Intent = Intent()
87+
serviceIntent.action = CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION
88+
return serviceIntent
11889
}
119-
}
12090

121-
private fun createDefaultCustomTabsServiceIntent() = Intent().apply {
122-
action = CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION
91+
fun onDestroy() {}
92+
93+
companion object {
94+
private val DUMMY_URL: String = "https://expo.io"
95+
fun <T, R> mapCollectionToDistinctArrayList(
96+
toMap: Collection<T>,
97+
mapper: (T) -> R
98+
): ArrayList<R> {
99+
val resultSet: LinkedHashSet<R> = LinkedHashSet()
100+
for (element: T in toMap) {
101+
resultSet.add(mapper(element))
102+
}
103+
return ArrayList(resultSet)
104+
}
105+
}
123106
}

android/src/main/java/com/reactnativewebbrowser/InternalCustomTabsConnectionHelper.kt

Lines changed: 64 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -6,92 +6,108 @@ import android.net.Uri
66
import androidx.browser.customtabs.CustomTabsClient
77
import androidx.browser.customtabs.CustomTabsServiceConnection
88
import androidx.browser.customtabs.CustomTabsSession
9+
import com.facebook.react.bridge.LifecycleEventListener
910

10-
class InternalCustomTabsConnectionHelper(
11-
private val context: Context
12-
) : CustomTabsServiceConnection() {
13-
private var currentPackageName: String? = null
14-
private val clientActions = DeferredClientActionsQueue<CustomTabsClient>()
15-
private val sessionActions = DeferredClientActionsQueue<CustomTabsSession?>()
11+
class InternalCustomTabsConnectionHelper internal constructor(private val context: Context) :
12+
CustomTabsServiceConnection(), LifecycleEventListener,
13+
CustomTabsConnectionHelper {
14+
private var mPackageName: String? = null
15+
private val clientActions: DeferredClientActionsQueue<CustomTabsClient> =
16+
DeferredClientActionsQueue()
17+
private val sessionActions: DeferredClientActionsQueue<CustomTabsSession> =
18+
DeferredClientActionsQueue()
1619

17-
// region lifecycle methods
18-
fun destroy() = clearConnection()
19-
// endregion
20-
21-
// region Actual connection helper methods
22-
fun warmUp(packageName: String) {
23-
clientActions.executeOrQueueAction { client: CustomTabsClient -> client.warmup(0) }
20+
override fun warmUp(packageName: String) {
21+
clientActions.executeOrQueueAction { client -> client.warmup(0) }
2422
ensureConnection(packageName)
2523
}
2624

27-
fun mayInitWithUrl(packageName: String, uri: Uri) {
28-
sessionActions.executeOrQueueAction { session: CustomTabsSession? ->
29-
session?.mayLaunchUrl(uri, null, null)
25+
override fun mayInitWithUrl(packageName: String, uri: Uri) {
26+
sessionActions.executeOrQueueAction { session ->
27+
session.mayLaunchUrl(
28+
uri,
29+
null,
30+
null
31+
)
3032
}
3133
ensureConnection(packageName)
3234
ensureSession()
3335
}
3436

35-
fun coolDown(packageName: String): Boolean {
36-
if (isConnectionStarted(packageName)) {
37-
clearConnection()
37+
private fun ensureSession() {
38+
if (!sessionActions.hasClient()) {
39+
clientActions.executeOrQueueAction { client ->
40+
sessionActions.setClient(
41+
client.newSession(null)
42+
)
43+
}
44+
}
45+
}
46+
47+
override fun coolDown(packageName: String): Boolean {
48+
if ((packageName == mPackageName)) {
49+
unbindService()
3850
return true
3951
}
4052
return false
4153
}
42-
// endregion
4354

44-
// region CustomTabsServiceConnection implementation
55+
private fun ensureConnection(packageName: String) {
56+
if (mPackageName != null && !(mPackageName == packageName)) {
57+
clearConnection()
58+
}
59+
if (!connectionStarted(packageName)) {
60+
CustomTabsClient.bindCustomTabsService(context, packageName, this)
61+
mPackageName = packageName
62+
}
63+
}
64+
65+
private fun connectionStarted(packageName: String): Boolean {
66+
return (packageName == mPackageName)
67+
}
68+
4569
override fun onBindingDied(componentName: ComponentName) {
46-
if (isConnectionStarted(componentName.packageName)) {
70+
if ((componentName.packageName == mPackageName)) {
4771
clearConnection()
4872
}
4973
}
5074

51-
override fun onCustomTabsServiceConnected(componentName: ComponentName, client: CustomTabsClient) {
52-
if (isConnectionStarted(componentName.packageName)) {
75+
override fun onCustomTabsServiceConnected(
76+
componentName: ComponentName,
77+
client: CustomTabsClient
78+
) {
79+
if ((componentName.packageName == mPackageName)) {
5380
clientActions.setClient(client)
5481
}
5582
}
5683

5784
override fun onServiceDisconnected(componentName: ComponentName) {
58-
if (isConnectionStarted(componentName.packageName)) {
85+
if ((componentName.packageName == mPackageName)) {
5986
clearConnection()
6087
}
6188
}
62-
// endregion
6389

64-
private fun ensureSession() {
65-
if (sessionActions.hasClient()) {
66-
return
67-
}
90+
override fun onHostResume() {
91+
// do nothing
92+
}
6893

69-
clientActions.executeOrQueueAction { client: CustomTabsClient ->
70-
sessionActions.setClient(client.newSession(null))
71-
}
94+
override fun onHostPause() {
95+
// do nothing
7296
}
7397

74-
private fun ensureConnection(packageName: String) {
75-
if (currentPackageName != null && currentPackageName != packageName) {
76-
clearConnection()
77-
}
78-
if (!isConnectionStarted(packageName)) {
79-
CustomTabsClient.bindCustomTabsService(context, packageName, this)
80-
currentPackageName = packageName
81-
}
98+
override fun onHostDestroy() {
99+
unbindService()
82100
}
83101

84-
private fun isConnectionStarted(packageName: String): Boolean {
85-
return packageName == currentPackageName
102+
private fun unbindService() {
103+
context.unbindService(this)
104+
clearConnection()
86105
}
87106

88107
private fun clearConnection() {
89-
if (currentPackageName != null) {
90-
context.unbindService(this)
91-
}
92-
93-
currentPackageName = null
108+
mPackageName = null
94109
clientActions.clear()
95110
sessionActions.clear()
96111
}
112+
97113
}

0 commit comments

Comments
 (0)