Refactor CustomTabsIntent creation

This commit is contained in:
arkon 2020-12-13 22:30:27 -05:00
parent c87ba6231d
commit 2cc2a90941
2 changed files with 24 additions and 20 deletions

View File

@ -2,7 +2,6 @@ package eu.kanade.tachiyomi.ui.setting
import android.app.Activity
import android.content.Intent
import androidx.browser.customtabs.CustomTabsIntent
import androidx.preference.PreferenceScreen
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.track.TrackManager
@ -20,7 +19,7 @@ import eu.kanade.tachiyomi.util.preference.onClick
import eu.kanade.tachiyomi.util.preference.preferenceCategory
import eu.kanade.tachiyomi.util.preference.switchPreference
import eu.kanade.tachiyomi.util.preference.titleRes
import eu.kanade.tachiyomi.util.system.getResourceColor
import eu.kanade.tachiyomi.util.system.openInBrowser
import eu.kanade.tachiyomi.widget.preference.LoginPreference
import uy.kohesive.injekt.injectLazy
import eu.kanade.tachiyomi.data.preference.PreferenceKeys as Keys
@ -47,11 +46,9 @@ class SettingsTrackingController :
startActivity(MyAnimeListLoginActivity.newIntent(activity!!))
}
trackPreference(trackManager.aniList) {
val tabsIntent = CustomTabsIntent.Builder()
.setToolbarColor(context.getResourceColor(R.attr.colorPrimary))
.build()
tabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
tabsIntent.launchUrl(activity!!, AnilistApi.authUrl())
activity?.openInBrowser(AnilistApi.authUrl(), trackManager.aniList.getLogoColor()) {
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
}
}
trackPreference(trackManager.kitsu) {
val dialog = TrackLoginDialog(trackManager.kitsu, R.string.email)
@ -59,18 +56,14 @@ class SettingsTrackingController :
dialog.showDialog(router)
}
trackPreference(trackManager.shikimori) {
val tabsIntent = CustomTabsIntent.Builder()
.setToolbarColor(context.getResourceColor(R.attr.colorPrimary))
.build()
tabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
tabsIntent.launchUrl(activity!!, ShikimoriApi.authUrl())
activity?.openInBrowser(ShikimoriApi.authUrl(), trackManager.shikimori.getLogoColor()) {
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
}
}
trackPreference(trackManager.bangumi) {
val tabsIntent = CustomTabsIntent.Builder()
.setToolbarColor(context.getResourceColor(R.attr.colorPrimary))
.build()
tabsIntent.intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
tabsIntent.launchUrl(activity!!, BangumiApi.authUrl())
activity?.openInBrowser(BangumiApi.authUrl(), trackManager.bangumi.getLogoColor()) {
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
}
}
}
preferenceCategory {

View File

@ -13,12 +13,14 @@ import android.content.pm.PackageManager
import android.content.res.Resources
import android.graphics.Color
import android.net.ConnectivityManager
import android.net.Uri
import android.os.PowerManager
import android.view.View
import android.widget.Toast
import androidx.annotation.AttrRes
import androidx.annotation.ColorInt
import androidx.annotation.StringRes
import androidx.browser.customtabs.CustomTabColorSchemeParams
import androidx.browser.customtabs.CustomTabsIntent
import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat
@ -224,12 +226,21 @@ fun Context.isServiceRunning(serviceClass: Class<*>): Boolean {
/**
* Opens a URL in a custom tab.
*/
fun Context.openInBrowser(url: String) {
fun Context.openInBrowser(url: String, @ColorInt toolbarColor: Int? = null, block: CustomTabsIntent.() -> Unit = {}) {
this.openInBrowser(url.toUri(), toolbarColor, block)
}
fun Context.openInBrowser(uri: Uri, @ColorInt toolbarColor: Int? = null, block: CustomTabsIntent.() -> Unit = {}) {
try {
val intent = CustomTabsIntent.Builder()
.setToolbarColor(getResourceColor(R.attr.colorPrimary))
.setDefaultColorSchemeParams(
CustomTabColorSchemeParams.Builder()
.setToolbarColor(toolbarColor ?: getResourceColor(R.attr.colorPrimary))
.build()
intent.launchUrl(this, url.toUri())
)
.build()
block(intent)
intent.launchUrl(this, uri)
} catch (e: Exception) {
toast(e.message)
}