-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from droibit/feature/renewal
Improve integration with `CustomTabsIntent`
- Loading branch information
Showing
9 changed files
with
199 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 0 additions & 17 deletions
17
launcher/src/main/java/com/droibit/android/customtabs/launcher/CustomTabsFallback.kt
This file was deleted.
Oops, something went wrong.
63 changes: 63 additions & 0 deletions
63
launcher/src/main/java/com/droibit/android/customtabs/launcher/CustomTabsIntentHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
@file:JvmName("CustomTabsIntentHelper") | ||
|
||
package com.droibit.android.customtabs.launcher | ||
|
||
import android.content.Context | ||
import androidx.browser.customtabs.CustomTabsClient | ||
import androidx.browser.customtabs.CustomTabsIntent | ||
import com.droibit.android.customtabs.launcher.internal.CustomTabsPackage.CHROME_PACKAGES | ||
|
||
/** | ||
* Sets the package name of the [CustomTabsIntent] to directly launch a browser that supports Custom Tabs. | ||
* | ||
* Chrome is prioritized as the launch target, but if it is not installed, | ||
* other browsers can be provided as the target using the [CustomTabsPackageFallback]. | ||
* (If no browser that supports Custom Tabs is installed on the device, the CustomTabsIntent is not affected.) | ||
* | ||
* * Basic usage: | ||
* ``` | ||
* val customTabsIntent = build() | ||
* customTabsIntent.ensureCustomTabsPackage(context) | ||
* customTabsIntent.launchUrl(context, Uri) | ||
* ``` | ||
* | ||
* * Present the custom tab as bottom sheet | ||
* ``` | ||
* val activityLauncher = registerForActivityResult(StartActivityForResult()) { | ||
* // Do something. | ||
* } | ||
* | ||
* val customTabsIntent = build().apply { | ||
* ensureCustomTabsPackage(context) | ||
* intent.data = Uri | ||
* } | ||
* activityLauncher.launch(customTabsIntent.intent) | ||
* ``` | ||
* | ||
* @param context The source Context | ||
* @param fallback A [CustomTabsPackageFallback] to be used if Chrome Custom Tabs is not available. | ||
*/ | ||
@JvmOverloads | ||
fun CustomTabsIntent.ensureCustomTabsPackage( | ||
context: Context, | ||
fallback: CustomTabsPackageFallback? = null, | ||
): CustomTabsIntent { | ||
setCustomTabsPackage(context, CHROME_PACKAGES, fallback) | ||
return this | ||
} | ||
|
||
internal fun CustomTabsIntent.setCustomTabsPackage( | ||
context: Context, | ||
customTabsPackages: List<String>, | ||
fallback: CustomTabsPackageFallback? = null, | ||
) { | ||
val customTabsPackage = | ||
CustomTabsClient.getPackageName(context, customTabsPackages, true) | ||
if (customTabsPackage == null && fallback != null) { | ||
with(fallback) { | ||
setCustomTabsPackage(context) | ||
} | ||
return | ||
} | ||
intent.setPackage(customTabsPackage) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 0 additions & 35 deletions
35
launcher/src/main/java/com/droibit/android/customtabs/launcher/CustomTabsLauncherExt.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.