Skip to content

Commit

Permalink
Merge pull request #20 from droibit/feature/renaming
Browse files Browse the repository at this point in the history
Rename to `set(Chrome)CustomTabsPackage` for better clarity
  • Loading branch information
droibit authored Dec 12, 2023
2 parents a2a42ed + 4de6cc5 commit df899e9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 32 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,24 +33,26 @@ implementation 'com.github.droibit:customtabslauncher:LATEST_VERSION'

## Usage

### Launch in Chrome Custom Tabs
### Basic Usage

#### Launch in Chrome Custom Tabs

```kotlin
try {
val customTabsIntent = buildCustomTabsIntent()
.ensureChromeCustomTabsPackage(context)
.setChromeCustomTabsPackage(context)
customTabsIntent.launchUrl(context, Uri.parse("https://example.com"))
} catch (e: ActivityNotFoundException) {
// Launch WebView, display a toast, etc.
}
```

### Launch in the default browser that supports Custom Tabs
#### Launch in the default browser that supports Custom Tabs

```kotlin
try {
val customTabsIntent = buildCustomTabsIntent()
.ensureCustomTabsPackage(context)
.setCustomTabsPackage(context)
customTabsIntent.launchUrl(context, Uri.parse("https://example.com"))
} catch (e: ActivityNotFoundException) {
// Launch WebView, display a toast, etc.
Expand All @@ -66,7 +68,7 @@ val activityLauncher = registerForActivityResult(StartActivityForResult()) {

try {
val customTabsIntent = build().apply {
ensureChromeCustomTabsPackage(context) // or ensureCustomTabsPackage(context)
setChromeCustomTabsPackage(context) // or setCustomTabsPackage(context)
intent.data = Uri.parse("https://example.com")
}
activityLauncher.launch(customTabsIntent.intent)
Expand All @@ -79,9 +81,9 @@ try {

```kotlin
buildCustomTabsIntent()
.ensureChromeCustomTabsPackage( // or .ensureCustomTabsPackage(
.setChromeCustomTabsPackage( // or .setCustomTabsPackage(
context,
// Launch a browser that supports Custom Tabs (not Chrome).
// Launch a browser that supports Custom Tabs (non-Chrome).
NonChromeCustomTabs(context)
// or launch a specific browser that supports Custom Tabs.
// NonChromeCustomTabs(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import androidx.browser.customtabs.CustomTabColorSchemeParams
import androidx.browser.customtabs.CustomTabsIntent
import androidx.core.content.ContextCompat
import com.droibit.android.customtabs.launcher.NonChromeCustomTabs
import com.droibit.android.customtabs.launcher.ensureChromeCustomTabsPackage
import com.droibit.android.customtabs.launcher.ensureCustomTabsPackage
import com.droibit.android.customtabs.launcher.setChromeCustomTabsPackage
import com.droibit.android.customtabs.launcher.setCustomTabsPackage

@Suppress("UNUSED_PARAMETER")
class MainActivity : AppCompatActivity(R.layout.activity_main) {
Expand All @@ -33,7 +33,7 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
try {
val customTabsIntent = customTabsBuilder()
.build().also {
it.ensureChromeCustomTabsPackage(this)
it.setChromeCustomTabsPackage(this)
}
customTabsIntent.launchUrl(this, URI_GOOGLE)
} catch (e: ActivityNotFoundException) {
Expand All @@ -45,7 +45,7 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
try {
val customTabsIntent = customTabsBuilder()
.build().also {
it.ensureCustomTabsPackage(this)
it.setCustomTabsPackage(this)
}
customTabsIntent.launchUrl(this, URI_GOOGLE)
} catch (e: ActivityNotFoundException) {
Expand All @@ -57,7 +57,7 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
try {
val customTabsIntent = customTabsBuilder()
.build().also {
it.ensureChromeCustomTabsPackage(this, NonChromeCustomTabs(this))
it.setChromeCustomTabsPackage(this, NonChromeCustomTabs(this))
}
customTabsIntent.launchUrl(this, URI_GOOGLE)
} catch (e: ActivityNotFoundException) {
Expand All @@ -77,7 +77,7 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
)
.setInitialActivityHeightPx(400)
.build().apply {
ensureChromeCustomTabsPackage(this@MainActivity)
setChromeCustomTabsPackage(this@MainActivity)
intent.data = URI_GOOGLE
}
activityLauncher.launch(customTabsIntent.intent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import com.droibit.android.customtabs.launcher.CustomTabsPackage.CHROME_PACKAGES
* - Basic usage:
* ```
* val customTabsIntent = build()
* customTabsIntent.ensureChromeCustomTabsPackage(context)
* customTabsIntent.setChromeCustomTabsPackage(context)
* customTabsIntent.launchUrl(context, Uri)
* ```
*
Expand All @@ -32,7 +32,7 @@ import com.droibit.android.customtabs.launcher.CustomTabsPackage.CHROME_PACKAGES
* }
*
* val customTabsIntent = build().apply {
* ensureChromeCustomTabsPackage(context)
* setChromeCustomTabsPackage(context)
* intent.data = Uri
* }
* activityLauncher.launch(customTabsIntent.intent)
Expand All @@ -42,7 +42,7 @@ import com.droibit.android.customtabs.launcher.CustomTabsPackage.CHROME_PACKAGES
* @param fallback A [CustomTabsPackageFallback] to be used if Chrome is not available.
*/
@JvmOverloads
fun CustomTabsIntent.ensureChromeCustomTabsPackage(
fun CustomTabsIntent.setChromeCustomTabsPackage(
context: Context,
fallback: CustomTabsPackageFallback? = null,
): CustomTabsIntent {
Expand All @@ -66,7 +66,7 @@ fun CustomTabsIntent.ensureChromeCustomTabsPackage(
* - Basic usage:
* ```
* val customTabsIntent = build()
* customTabsIntent.ensureCustomTabsPackage(context)
* customTabsIntent.setCustomTabsPackage(context)
* customTabsIntent.launchUrl(context, Uri)
* ```
*
Expand All @@ -77,7 +77,7 @@ fun CustomTabsIntent.ensureChromeCustomTabsPackage(
* }
*
* val customTabsIntent = build().apply {
* ensureCustomTabsPackage(context)
* setCustomTabsPackage(context)
* intent.data = Uri
* }
* activityLauncher.launch(customTabsIntent.intent)
Expand All @@ -87,11 +87,11 @@ fun CustomTabsIntent.ensureChromeCustomTabsPackage(
* @param fallback A [CustomTabsPackageFallback] to be used if the default browser or Chrome are not available.
*/
@JvmOverloads
fun CustomTabsIntent.ensureCustomTabsPackage(
fun CustomTabsIntent.setCustomTabsPackage(
context: Context,
fallback: CustomTabsPackageFallback? = null,
): CustomTabsIntent {
setCustomTabsPackage(context, CHROME_PACKAGES, false, fallback)
this.setCustomTabsPackage(context, CHROME_PACKAGES, false, fallback)
return this
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class CustomTabsIntentHelperTest {
private lateinit var context: Context

@Test
fun `ensureChromeCustomTabsPackage uses Chrome if found`() {
fun `setChromeCustomTabsPackage uses Chrome if found`() {
mockStatic(CustomTabsClient::class.java).use { mocked ->
val chromePackage = "com.android.chrome"
mocked.`when`<String?> {
Expand All @@ -37,7 +37,7 @@ class CustomTabsIntentHelperTest {

val customTabsIntent = CustomTabsIntent.Builder()
.build()
.ensureChromeCustomTabsPackage(context)
.setChromeCustomTabsPackage(context)
assertThat(customTabsIntent.intent.`package`).isEqualTo(chromePackage)

mocked.verify {
Expand All @@ -47,15 +47,15 @@ class CustomTabsIntentHelperTest {
}

@Test
fun `ensureChromeCustomTabsPackage sets null if Chrome not found`() {
fun `setChromeCustomTabsPackage sets null if Chrome not found`() {
mockStatic(CustomTabsClient::class.java).use { mocked ->
mocked.`when`<String?> {
CustomTabsClient.getPackageName(any(), any(), any())
}.thenReturn(null)

val customTabsIntent = CustomTabsIntent.Builder()
.build()
.ensureChromeCustomTabsPackage(context)
.setChromeCustomTabsPackage(context)
assertThat(customTabsIntent.intent.`package`).isNull()

mocked.verify {
Expand All @@ -65,7 +65,7 @@ class CustomTabsIntentHelperTest {
}

@Test
fun `ensureChromeCustomTabsPackage falls back to non-Chrome Custom Tab if Chrome not found`() {
fun `setChromeCustomTabsPackage falls back to non-Chrome Custom Tab if Chrome not found`() {
mockStatic(CustomTabsClient::class.java).use { mocked ->
val noneChromePackage = "com.example.customtabs"
mocked.`when`<String?> {
Expand All @@ -75,7 +75,7 @@ class CustomTabsIntentHelperTest {
val fallback = NonChromeCustomTabs(listOf(noneChromePackage))
val customTabsIntent = CustomTabsIntent.Builder()
.build()
.ensureChromeCustomTabsPackage(context, fallback)
.setChromeCustomTabsPackage(context, fallback)
assertThat(customTabsIntent.intent.`package`).isEqualTo(noneChromePackage)

inOrder(CustomTabsClient::class.java) {
Expand All @@ -90,7 +90,7 @@ class CustomTabsIntentHelperTest {
}

@Test
fun `ensureCustomTabsPackage uses default browser if found`() {
fun `setCustomTabsPackage uses default browser if found`() {
mockStatic(CustomTabsClient::class.java).use { mocked ->
val chromePackage = "com.chrome.beta"
mocked.`when`<String?> {
Expand All @@ -99,7 +99,7 @@ class CustomTabsIntentHelperTest {

val customTabsIntent = CustomTabsIntent.Builder()
.build()
.ensureCustomTabsPackage(context)
.setCustomTabsPackage(context)
assertThat(customTabsIntent.intent.`package`).isEqualTo(chromePackage)

mocked.verify {
Expand All @@ -109,15 +109,15 @@ class CustomTabsIntentHelperTest {
}

@Test
fun `ensureCustomTabsPackage sets null if no default browser or Chrome found`() {
fun `setCustomTabsPackage sets null if no default browser or Chrome found`() {
mockStatic(CustomTabsClient::class.java).use { mocked ->
mocked.`when`<String?> {
CustomTabsClient.getPackageName(any(), any(), any())
}.thenReturn(null)

val customTabsIntent = CustomTabsIntent.Builder()
.build()
.ensureCustomTabsPackage(context)
.setCustomTabsPackage(context)
assertThat(customTabsIntent.intent.`package`).isNull()

mocked.verify {
Expand All @@ -127,7 +127,7 @@ class CustomTabsIntentHelperTest {
}

@Test
fun `ensureCustomTabsPackage falls back to non-Chrome Custom Tab if none found`() {
fun `setCustomTabsPackage falls back to non-Chrome Custom Tab if none found`() {
mockStatic(CustomTabsClient::class.java).use { mocked ->
val noneChromePackage = "com.example.customtabs"
mocked.`when`<String?> {
Expand All @@ -137,7 +137,7 @@ class CustomTabsIntentHelperTest {
val fallback = NonChromeCustomTabs(listOf(noneChromePackage))
val customTabsIntent = CustomTabsIntent.Builder()
.build()
.ensureCustomTabsPackage(context, fallback)
.setCustomTabsPackage(context, fallback)
assertThat(customTabsIntent.intent.`package`).isEqualTo(noneChromePackage)

mocked.verify({
Expand Down

0 comments on commit df899e9

Please sign in to comment.