Closed charcoJP closed 6 years ago
Yes. I made that bug 😭
I think there is a smarter correction method, but I could not think of it😭
Me too 😭
How about this?
fun navigateToExternalBrowser(url: String) {
val customTabsPackageName = CustomTabsHelper.getPackageNameToUse(activity)
if (tryLaunchingSpecificApp(url, customTabsPackageName)) {
return
}
val customTabsIntent = CustomTabsIntent.Builder()
.setShowTitle(true)
.setToolbarColor(ContextCompat.getColor(activity, R.color.primary))
.build()
.apply {
val referrer = Uri.parse("android-app://${activity.packageName}")
intent.putExtra(Intent.EXTRA_REFERRER, referrer)
}
val webUri = Uri.parse(url)
if (tryUsingCustomTabs(customTabsPackageName, customTabsIntent, webUri)) {
return
}
// Cannot use custom tabs.
activity.startActivity(customTabsIntent.intent.setData(webUri))
}
private fun tryUsingCustomTabs(customTabsPackageName: String?, customTabsIntent: CustomTabsIntent, webUri: Uri?): Boolean {
customTabsPackageName?.let {
customTabsIntent.intent.`package` = customTabsPackageName
customTabsIntent.launchUrl(activity, webUri)
return true
}
return false
}
private fun tryLaunchingSpecificApp(url:String, customTabsPackageName: String?): Boolean {
val appUri = run {
val uri = Uri.parse(url)
if (uri.host.contains("facebook")) {
return@run Uri.parse(FACEBOOK_SCHEME + url)
}
uri
}
val appIntent = Intent(Intent.ACTION_VIEW, appUri)
val intentResolveInfo = activity.packageManager.resolveActivity(
appIntent,
PackageManager.MATCH_DEFAULT_ONLY
)
intentResolveInfo?.activityInfo?.packageName?.let {
if (customTabsPackageName != null && it != customTabsPackageName) {
// Open specific app
activity.startActivity(appIntent)
return true
}
}
return false
}
@takahirom Thanks! That's pretty much better!
maybe, Using let
makes it easier to read.
before
val appUri = run {
val uri = Uri.parse(url)
if (uri.host.contains("facebook")) {
return@run Uri.parse(FACEBOOK_SCHEME + url)
}
uri
}
after
val appUri = Uri.parse(url).let {
if (it.host.contains("facebook")) {
Uri.parse(FACEBOOK_SCHEME + url)
} else it
}
LGTM 👍 Thank you for fixing bug and refactoring 🐞 🏭
@takahirom Thank you for review!✨
Issue
Overview (Required)
stackTrace
Links
-
Screenshot