Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ internal object CustomTabsPackage {
private const val PACKAGE_CHROME_LOCAL = "com.google.android.apps.chrome"

// Higher priority packages are listed first.
val CHROME_PACKAGES = setOf(
val CHROME_PACKAGES: Set<String> = linkedSetOf(
PACKAGE_CHROME_STABLE,
PACKAGE_CHROME_BETA,
PACKAGE_CHROME_DEV,
PACKAGE_CHROME_LOCAL,
)

fun getNonChromeCustomTabsPackages(context: Context): Set<String> {
fun getNonChromeCustomTabsPackages(context: Context): List<String> {
val activityIntent = Intent(ACTION_VIEW, Uri.parse("http://"))
.addCategory(Intent.CATEGORY_BROWSABLE)
val pm = context.packageManager
Expand All @@ -36,15 +36,11 @@ internal object CustomTabsPackage {
.setPackage(it)
pm.resolveService(serviceIntent, 0) != null
}
.toSet()
.toList()
}

private fun queryIntentActivities(pm: PackageManager, intent: Intent): List<ResolveInfo> {
val flag = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
PackageManager.MATCH_ALL
} else {
PackageManager.MATCH_DEFAULT_ONLY
}
val flag = PackageManager.MATCH_ALL
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
pm.queryIntentActivities(
intent,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import androidx.browser.customtabs.CustomTabsIntent
import com.droibit.android.customtabs.launcher.CustomTabsPackage.CHROME_PACKAGES

/**
* Interface for providing a set of browser package names that support Custom Tabs.
* Interface for providing a list of browser package names that support Custom Tabs.
*
* The [CustomTabsPackageProvider] interface allows you to specify alternative browsers
* that can handle Custom Tabs when launching URLs using [CustomTabsIntent].
Expand All @@ -14,34 +14,36 @@ import com.droibit.android.customtabs.launcher.CustomTabsPackage.CHROME_PACKAGES
*/
fun interface CustomTabsPackageProvider {
/**
* Retrieves the set of browser package names that support Custom Tabs.
* Retrieves the list of browser package names that support Custom Tabs.
*
* @return A [Set] of package names as [String].
* @return A [List] of package names as [String].
*/
operator fun invoke(): Set<String>
operator fun invoke(): List<String>
Comment thread
droibit marked this conversation as resolved.
}

/**
* Provides a set of non-Chrome browser package names that support Custom Tabs.
* Provides a list of non-Chrome browser package names that support Custom Tabs.
*
* This is useful when Chrome is not installed or when you prefer to use a different browser
* that supports Custom Tabs.
*
* @param packages Package list of non-Chrome browsers supporting Custom Tabs. The top of the list is used with the highest priority.
*/
class NonChromeCustomTabs(
private val packages: Set<String>,
private val packages: List<String>,
) : CustomTabsPackageProvider {

constructor(context: Context) : this(
CustomTabsPackage.getNonChromeCustomTabsPackages(context),
)

constructor(packages: Set<String>) : this(packages.toList())

init {
require(packages.none { CHROME_PACKAGES.contains(it) }) {
"Packages must not contain any Chrome packages."
}
}

override operator fun invoke(): Set<String> = packages
override operator fun invoke(): List<String> = packages
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class NonChromeCustomTabsTest {

@Test
fun `invoke returns non-Chrome packages`() {
val packages = setOf(
val packages = listOf(
"com.example.customtabs_1",
"com.example.customtabs_2",
)
Expand Down