From 61e5440b7c9c01dcb4a373d407da4872a4f38de2 Mon Sep 17 00:00:00 2001 From: arkon Date: Fri, 12 Feb 2021 17:02:37 -0500 Subject: [PATCH] Avoid crash when device fails to handle opening a URL --- .../ui/base/controller/ConductorExtensions.kt | 12 ++++++++++ .../details/ExtensionDetailsController.kt | 5 ++-- .../ui/main/WhatsNewDialogController.kt | 7 ++---- .../tachiyomi/ui/manga/track/TrackSheet.kt | 5 ++-- .../tachiyomi/ui/more/AboutController.kt | 23 ++++++------------- 5 files changed, 25 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/base/controller/ConductorExtensions.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/base/controller/ConductorExtensions.kt index f5c2277ace..ce9fbb586a 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/base/controller/ConductorExtensions.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/base/controller/ConductorExtensions.kt @@ -1,11 +1,14 @@ package eu.kanade.tachiyomi.ui.base.controller +import android.content.Intent import android.content.pm.PackageManager.PERMISSION_GRANTED import android.os.Build import androidx.core.content.ContextCompat +import androidx.core.net.toUri import com.bluelinelabs.conductor.Controller import com.bluelinelabs.conductor.Router import com.bluelinelabs.conductor.RouterTransaction +import eu.kanade.tachiyomi.util.system.toast fun Router.popControllerWithTag(tag: String): Boolean { val controller = getControllerWithTag(tag) @@ -32,3 +35,12 @@ fun Controller.withFadeTransaction(): RouterTransaction { .pushChangeHandler(OneWayFadeChangeHandler()) .popChangeHandler(OneWayFadeChangeHandler()) } + +fun Controller.openInBrowser(url: String) { + try { + val intent = Intent(Intent.ACTION_VIEW, url.toUri()) + startActivity(intent) + } catch (e: Throwable) { + activity?.toast(e.message) + } +} diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/ExtensionDetailsController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/ExtensionDetailsController.kt index 67245bd62c..49a981e4f6 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/ExtensionDetailsController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/browse/extension/details/ExtensionDetailsController.kt @@ -14,7 +14,6 @@ import android.view.MenuItem import android.view.View import android.view.ViewGroup import androidx.appcompat.view.ContextThemeWrapper -import androidx.core.net.toUri import androidx.core.os.bundleOf import androidx.preference.Preference import androidx.preference.PreferenceGroupAdapter @@ -36,6 +35,7 @@ import eu.kanade.tachiyomi.source.Source import eu.kanade.tachiyomi.source.getPreferenceKey import eu.kanade.tachiyomi.ui.base.controller.NucleusController import eu.kanade.tachiyomi.ui.base.controller.ToolbarLiftOnScrollController +import eu.kanade.tachiyomi.ui.base.controller.openInBrowser import eu.kanade.tachiyomi.ui.base.controller.withFadeTransaction import eu.kanade.tachiyomi.util.preference.DSL import eu.kanade.tachiyomi.util.preference.onChange @@ -213,8 +213,7 @@ class ExtensionDetailsController(bundle: Bundle? = null) : !pkgFactory.isNullOrEmpty() -> "$URL_EXTENSION_COMMITS/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/$pkgFactory" else -> "$URL_EXTENSION_COMMITS/src/${pkgName.replace(".", "/")}" } - val intent = Intent(Intent.ACTION_VIEW, url.toUri()) - startActivity(intent) + openInBrowser(url) } private fun openInSettings() { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/main/WhatsNewDialogController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/main/WhatsNewDialogController.kt index 7497d42ea3..b1b5dd335e 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/main/WhatsNewDialogController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/main/WhatsNewDialogController.kt @@ -1,13 +1,12 @@ package eu.kanade.tachiyomi.ui.main import android.app.Dialog -import android.content.Intent import android.os.Bundle -import androidx.core.net.toUri import com.afollestad.materialdialogs.MaterialDialog import eu.kanade.tachiyomi.BuildConfig import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.ui.base.controller.DialogController +import eu.kanade.tachiyomi.ui.base.controller.openInBrowser class WhatsNewDialogController(bundle: Bundle? = null) : DialogController(bundle) { @@ -16,9 +15,7 @@ class WhatsNewDialogController(bundle: Bundle? = null) : DialogController(bundle .title(text = activity!!.getString(R.string.updated_version, BuildConfig.VERSION_NAME)) .positiveButton(android.R.string.ok) .neutralButton(R.string.whats_new) { - val url = "https://github.com/tachiyomiorg/tachiyomi/releases/tag/v${BuildConfig.VERSION_NAME}" - val intent = Intent(Intent.ACTION_VIEW, url.toUri()) - startActivity(intent) + openInBrowser("https://github.com/tachiyomiorg/tachiyomi/releases/tag/v${BuildConfig.VERSION_NAME}") } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/track/TrackSheet.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/track/TrackSheet.kt index cd5c24778e..5699bcef68 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/track/TrackSheet.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/track/TrackSheet.kt @@ -1,13 +1,12 @@ package eu.kanade.tachiyomi.ui.manga.track -import android.content.Intent import android.os.Bundle import android.view.ViewGroup -import androidx.core.net.toUri import androidx.recyclerview.widget.LinearLayoutManager import com.google.android.material.bottomsheet.BottomSheetBehavior import eu.kanade.tachiyomi.data.database.models.Manga import eu.kanade.tachiyomi.databinding.TrackControllerBinding +import eu.kanade.tachiyomi.ui.base.controller.openInBrowser import eu.kanade.tachiyomi.ui.manga.MangaController import eu.kanade.tachiyomi.util.system.copyToClipboard import eu.kanade.tachiyomi.widget.sheet.BaseBottomSheetDialog @@ -65,7 +64,7 @@ class TrackSheet( val track = adapter.getItem(position)?.track ?: return if (track.tracking_url.isNotBlank()) { - controller.activity?.startActivity(Intent(Intent.ACTION_VIEW, track.tracking_url.toUri())) + controller.openInBrowser(track.tracking_url) } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/more/AboutController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/more/AboutController.kt index ca83ab4df2..6f48aff70f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/more/AboutController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/more/AboutController.kt @@ -1,10 +1,8 @@ package eu.kanade.tachiyomi.ui.more import android.app.Dialog -import android.content.Intent import android.os.Build import android.os.Bundle -import androidx.core.net.toUri import androidx.core.os.bundleOf import androidx.preference.PreferenceScreen import com.afollestad.materialdialogs.MaterialDialog @@ -15,6 +13,7 @@ import eu.kanade.tachiyomi.data.updater.UpdateResult import eu.kanade.tachiyomi.data.updater.UpdaterService import eu.kanade.tachiyomi.data.updater.github.GithubUpdateChecker import eu.kanade.tachiyomi.ui.base.controller.DialogController +import eu.kanade.tachiyomi.ui.base.controller.openInBrowser import eu.kanade.tachiyomi.ui.setting.SettingsController import eu.kanade.tachiyomi.util.lang.launchNow import eu.kanade.tachiyomi.util.lang.toDateTimestampString @@ -76,19 +75,15 @@ class AboutController : SettingsController() { } else { "https://github.com/tachiyomiorg/tachiyomi/releases/tag/v${BuildConfig.VERSION_NAME}" } - - val intent = Intent(Intent.ACTION_VIEW, url.toUri()) - startActivity(intent) + openInBrowser(url) } } if (BuildConfig.DEBUG) { preference { key = "pref_about_notices" titleRes = R.string.notices - onClick { - val intent = Intent(Intent.ACTION_VIEW, "https://github.com/tachiyomiorg/tachiyomi/blob/master/PREVIEW_RELEASE_NOTES.md".toUri()) - startActivity(intent) + openInBrowser("https://github.com/tachiyomiorg/tachiyomi/blob/master/PREVIEW_RELEASE_NOTES.md") } } } @@ -100,8 +95,7 @@ class AboutController : SettingsController() { val url = "https://tachiyomi.org" summary = url onClick { - val intent = Intent(Intent.ACTION_VIEW, url.toUri()) - startActivity(intent) + openInBrowser(url) } } preference { @@ -110,8 +104,7 @@ class AboutController : SettingsController() { val url = "https://discord.gg/tachiyomi" summary = url onClick { - val intent = Intent(Intent.ACTION_VIEW, url.toUri()) - startActivity(intent) + openInBrowser(url) } } preference { @@ -120,8 +113,7 @@ class AboutController : SettingsController() { val url = "https://github.com/tachiyomiorg/tachiyomi" summary = url onClick { - val intent = Intent(Intent.ACTION_VIEW, url.toUri()) - startActivity(intent) + openInBrowser(url) } } preference { @@ -130,8 +122,7 @@ class AboutController : SettingsController() { val url = "https://github.com/tachiyomiorg/tachiyomi-extensions" summary = url onClick { - val intent = Intent(Intent.ACTION_VIEW, url.toUri()) - startActivity(intent) + openInBrowser(url) } } preference {