mirror of
https://github.com/null2264/yokai.git
synced 2025-06-21 10:44:42 +00:00
force open in browser to open full browser
also force tracking links to open in custom tabs
This commit is contained in:
parent
c2a043c85b
commit
f84ca6930f
3 changed files with 45 additions and 20 deletions
|
@ -45,10 +45,10 @@ class SettingsTrackingController :
|
|||
titleRes = R.string.services
|
||||
|
||||
trackPreference(trackManager.myAnimeList) {
|
||||
activity?.openInBrowser(MyAnimeListApi.authUrl(), trackManager.myAnimeList.getLogoColor())
|
||||
activity?.openInBrowser(MyAnimeListApi.authUrl(), trackManager.myAnimeList.getLogoColor(), true)
|
||||
}
|
||||
trackPreference(trackManager.aniList) {
|
||||
activity?.openInBrowser(AnilistApi.authUrl(), trackManager.aniList.getLogoColor())
|
||||
activity?.openInBrowser(AnilistApi.authUrl(), trackManager.aniList.getLogoColor(), true)
|
||||
}
|
||||
preference {
|
||||
key = "update_anilist_scoring"
|
||||
|
@ -88,10 +88,10 @@ class SettingsTrackingController :
|
|||
dialog.showDialog(router)
|
||||
}
|
||||
trackPreference(trackManager.shikimori) {
|
||||
activity?.openInBrowser(ShikimoriApi.authUrl(), trackManager.shikimori.getLogoColor())
|
||||
activity?.openInBrowser(ShikimoriApi.authUrl(), trackManager.shikimori.getLogoColor(), true)
|
||||
}
|
||||
trackPreference(trackManager.bangumi) {
|
||||
activity?.openInBrowser(BangumiApi.authUrl(), trackManager.bangumi.getLogoColor())
|
||||
activity?.openInBrowser(BangumiApi.authUrl(), trackManager.bangumi.getLogoColor(), true)
|
||||
}
|
||||
infoPreference(R.string.tracking_info)
|
||||
}
|
||||
|
|
|
@ -173,6 +173,6 @@ open class WebViewActivity : BaseWebViewActivity() {
|
|||
}
|
||||
|
||||
private fun openInBrowser() {
|
||||
binding.webview.url?.let { openInBrowser(it) }
|
||||
binding.webview.url?.let { openInBrowser(it, forceBrowser = true, fullBrowser = true) }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -379,11 +379,11 @@ fun Context.isServiceRunning(serviceClass: Class<*>): Boolean {
|
|||
.any { className == it.service.className }
|
||||
}
|
||||
|
||||
fun Context.openInBrowser(url: String, @ColorInt toolbarColor: Int? = null) {
|
||||
fun Context.openInBrowser(url: String, @ColorInt toolbarColor: Int? = null, forceBrowser: Boolean = false) {
|
||||
this.openInBrowser(url.toUri(), toolbarColor)
|
||||
}
|
||||
|
||||
fun Context.openInBrowser(uri: Uri, @ColorInt toolbarColor: Int? = null) {
|
||||
fun Context.openInBrowser(uri: Uri, @ColorInt toolbarColor: Int? = null, forceBrowser: Boolean = false) {
|
||||
try {
|
||||
val intent = CustomTabsIntent.Builder()
|
||||
.setDefaultColorSchemeParams(
|
||||
|
@ -392,6 +392,11 @@ fun Context.openInBrowser(uri: Uri, @ColorInt toolbarColor: Int? = null) {
|
|||
.build(),
|
||||
)
|
||||
.build()
|
||||
if (forceBrowser) {
|
||||
val packages = getCustomTabsPackages().maxByOrNull { it.preferredOrder }
|
||||
val processName = packages?.activityInfo?.processName ?: return
|
||||
intent.intent.`package` = processName
|
||||
}
|
||||
intent.launchUrl(this, uri)
|
||||
} catch (e: Exception) {
|
||||
toast(e.message)
|
||||
|
@ -401,9 +406,15 @@ fun Context.openInBrowser(uri: Uri, @ColorInt toolbarColor: Int? = null) {
|
|||
/**
|
||||
* Opens a URL in a custom tab.
|
||||
*/
|
||||
fun Context.openInBrowser(url: String, forceBrowser: Boolean): Boolean {
|
||||
fun Context.openInBrowser(url: String, forceBrowser: Boolean, fullBrowser: Boolean = true): Boolean {
|
||||
try {
|
||||
val parsedUrl = url.toUri()
|
||||
if (forceBrowser && fullBrowser) {
|
||||
val intent = Intent(Intent.ACTION_VIEW, parsedUrl).apply {
|
||||
defaultBrowserPackageName()?.let { setPackage(it) }
|
||||
}
|
||||
startActivity(intent)
|
||||
} else {
|
||||
val intent = CustomTabsIntent.Builder()
|
||||
.setDefaultColorSchemeParams(
|
||||
CustomTabColorSchemeParams.Builder()
|
||||
|
@ -417,6 +428,7 @@ fun Context.openInBrowser(url: String, forceBrowser: Boolean): Boolean {
|
|||
intent.intent.`package` = processName
|
||||
}
|
||||
intent.launchUrl(this, parsedUrl)
|
||||
}
|
||||
return true
|
||||
} catch (e: Exception) {
|
||||
toast(e.message)
|
||||
|
@ -424,6 +436,19 @@ fun Context.openInBrowser(url: String, forceBrowser: Boolean): Boolean {
|
|||
}
|
||||
}
|
||||
|
||||
fun Context.defaultBrowserPackageName(): String? {
|
||||
val browserIntent = Intent(Intent.ACTION_VIEW, "http://".toUri())
|
||||
val resolveInfo = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
packageManager.resolveActivity(browserIntent, PackageManager.ResolveInfoFlags.of(PackageManager.MATCH_DEFAULT_ONLY.toLong()))
|
||||
} else {
|
||||
@Suppress("DEPRECATION")
|
||||
packageManager.resolveActivity(browserIntent, PackageManager.MATCH_DEFAULT_ONLY)
|
||||
}
|
||||
return resolveInfo
|
||||
?.activityInfo?.packageName
|
||||
?.takeUnless { it in DeviceUtil.invalidDefaultBrowsers }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of packages that support Custom Tabs.
|
||||
*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue