@@ -8,99 +8,116 @@ 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
1113import java.util.ArrayList
12- import java.util.Collections
1314import java.util.LinkedHashSet
1415
16+ private const val DUMMY_URL = " https://web3auth.io"
1517
16- import androidx.browser.customtabs.CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION
17- import com.reactnativewebbrowser.error.CurrentActivityNotFoundException
18- import com.reactnativewebbrowser.error.PackageManagerNotFoundException
18+ class InternalCustomTabsActivitiesHelper (
19+ private val activityProvider : ActivityProvider ?
20+ ) {
1921
20- class InternalCustomTabsActivitiesHelper (val activityProvider : ActivityProvider ) :
21- CustomTabsActivitiesHelper {
22+ // region Actual custom tabs activities helper methods
2223
23- override val customTabsResolvingActivities: ArrayList <String >
24- get() = mapCollectionToDistinctArrayList(
25- getResolvingActivities(
26- createDefaultCustomTabsIntent()
27- )
28- ) { resolveInfo -> resolveInfo.activityInfo.packageName }
24+ /* *
25+ * @throws CurrentActivityNotFoundException
26+ * @throws PackageManagerNotFoundException
27+ */
28+ fun canResolveIntent (intent : Intent ): Boolean = getResolvingActivities(intent).isNotEmpty()
2929
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- }
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+ }
39+
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+ }
3949
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)
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)
4457 }
4558
46- override val defaultCustomTabsResolvingActivity: String?
59+ /* *
60+ * @throws PackageManagerNotFoundException
61+ * @throws CurrentActivityNotFoundException
62+ */
63+ val defaultCustomTabsResolvingActivity: String?
4764 get() {
48- val info: ResolveInfo ? =
49- packageManager.resolveActivity(createDefaultCustomTabsIntent(), 0 )
65+ val info = packageManager.resolveActivity(createDefaultCustomTabsIntent(), 0 )
5066 return info?.activityInfo?.packageName
5167 }
5268
53- override fun canResolveIntent (intent : Intent ): Boolean {
54- return getResolvingActivities(intent).isNotEmpty()
55- }
56-
57- override fun startCustomTabs (intent : Intent ) {
69+ /* *
70+ * @throws CurrentActivityNotFoundException
71+ */
72+ fun startCustomTabs (intent : Intent ) {
5873 currentActivity.startActivity(intent)
5974 }
6075
76+ // endregion
77+
78+ // region Private helpers
79+
80+ /* *
81+ * @throws CurrentActivityNotFoundException
82+ * @throws PackageManagerNotFoundException
83+ */
6184 private fun getResolvingActivities (intent : Intent ): List <ResolveInfo > {
6285 return packageManager.queryIntentActivities(intent, 0 )
6386 }
6487
88+ /* *
89+ * @throws CurrentActivityNotFoundException
90+ * @throws PackageManagerNotFoundException
91+ */
6592 private val packageManager: PackageManager
66- get() {
67- val pm: PackageManager ? = currentActivity.packageManager
68- if (pm == null ) throw PackageManagerNotFoundException () else return pm
69- }
93+ get() = currentActivity.packageManager ? : throw PackageManagerNotFoundException ()
7094
95+ /* *
96+ * @throws CurrentActivityNotFoundException
97+ */
7198 private val currentActivity: Activity
7299 get() {
73- return activityProvider.getCurrentActivity()
74- ? : throw CurrentActivityNotFoundException ()
100+ return activityProvider?.getCurrentActivity() ? : throw CurrentActivityNotFoundException ()
75101 }
76102
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
83- }
103+ // endregion
104+ }
84105
85- private fun createDefaultCustomTabsServiceIntent (): Intent {
86- val serviceIntent : Intent = Intent ()
87- serviceIntent.action = CustomTabsService . ACTION_CUSTOM_TABS_CONNECTION
88- return serviceIntent
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))
89110 }
111+ return ArrayList (resultSet)
112+ }
90113
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- }
114+ private fun createDefaultCustomTabsIntent (): Intent {
115+ val customTabsIntent = CustomTabsIntent .Builder ().build()
116+ return customTabsIntent.intent.apply {
117+ data = Uri .parse(DUMMY_URL )
105118 }
106119}
120+
121+ private fun createDefaultCustomTabsServiceIntent () = Intent ().apply {
122+ action = CustomTabsService .ACTION_CUSTOM_TABS_CONNECTION
123+ }
0 commit comments