@@ -8,116 +8,99 @@ import android.net.Uri
88import androidx.browser.customtabs.CustomTabsClient
99import androidx.browser.customtabs.CustomTabsIntent
1010import androidx.browser.customtabs.CustomTabsService
11- import com.reactnativewebbrowser.error.CurrentActivityNotFoundException
12- import com.reactnativewebbrowser.error.PackageManagerNotFoundException
1311import java.util.ArrayList
12+ import java.util.Collections
1413import 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}
0 commit comments