From 797553ce1677987c2713407d98159725e0205610 Mon Sep 17 00:00:00 2001 From: arkon Date: Fri, 10 Jan 2020 22:22:12 -0500 Subject: [PATCH] Convert webview into an activity (#2470) Based on https://github.com/Jays2Kings/tachiyomiJ2K/commit/65804ebb3aca0a79377167334ff842c0789440f3 --- app/src/main/AndroidManifest.xml | 3 + .../ui/base/activity/BaseActivity.kt | 16 +++ .../browse/BrowseCatalogueController.kt | 7 +- .../kanade/tachiyomi/ui/main/MainActivity.kt | 14 +-- .../ui/manga/info/MangaInfoController.kt | 6 +- .../ui/manga/info/MangaWebViewController.kt | 94 -------------- .../tachiyomi/ui/webview/WebViewActivity.kt | 115 ++++++++++++++++++ .../res/drawable/ic_arrow_back_white_24dp.xml | 9 ++ .../drawable/ic_arrow_forward_white_24dp.xml | 9 ++ .../main/res/drawable/ic_close_white_24dp.xml | 9 ++ app/src/main/res/layout/main_activity.xml | 1 - app/src/main/res/layout/webview_activity.xml | 29 +++++ app/src/main/res/menu/web_view.xml | 19 --- app/src/main/res/menu/webview.xml | 23 ++++ app/src/main/res/values/strings.xml | 5 +- app/src/main/res/values/themes.xml | 6 +- 16 files changed, 229 insertions(+), 136 deletions(-) delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaWebViewController.kt create mode 100644 app/src/main/java/eu/kanade/tachiyomi/ui/webview/WebViewActivity.kt create mode 100644 app/src/main/res/drawable/ic_arrow_back_white_24dp.xml create mode 100644 app/src/main/res/drawable/ic_arrow_forward_white_24dp.xml create mode 100644 app/src/main/res/drawable/ic_close_white_24dp.xml create mode 100644 app/src/main/res/layout/webview_activity.xml delete mode 100644 app/src/main/res/menu/web_view.xml create mode 100644 app/src/main/res/menu/webview.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index eb3629f7a..b3558ec50 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -48,6 +48,9 @@ + R.style.Theme_Tachiyomi_Dark + 3 -> R.style.Theme_Tachiyomi_Amoled + 4 -> R.style.Theme_Tachiyomi_DarkBlue + else -> R.style.Theme_Tachiyomi + }) + super.onCreate(savedInstanceState) + } + } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/browse/BrowseCatalogueController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/browse/BrowseCatalogueController.kt index 64dc08e23..3ea5eb68f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/browse/BrowseCatalogueController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/catalogue/browse/BrowseCatalogueController.kt @@ -28,7 +28,7 @@ import eu.kanade.tachiyomi.ui.base.controller.SecondaryDrawerController import eu.kanade.tachiyomi.ui.base.controller.withFadeTransaction import eu.kanade.tachiyomi.ui.library.ChangeMangaCategoriesDialog import eu.kanade.tachiyomi.ui.manga.MangaController -import eu.kanade.tachiyomi.ui.manga.info.MangaWebViewController +import eu.kanade.tachiyomi.ui.webview.WebViewActivity import eu.kanade.tachiyomi.util.* import eu.kanade.tachiyomi.widget.AutofitRecyclerView import kotlinx.android.synthetic.main.catalogue_controller.* @@ -295,8 +295,9 @@ open class BrowseCatalogueController(bundle: Bundle) : private fun openInWebView() { val source = presenter.source as? HttpSource ?: return - router.pushController(MangaWebViewController(source.id, source.baseUrl) - .withFadeTransaction()) + val activity = activity ?: return + val intent = WebViewActivity.newIntent(activity, source.id, source.baseUrl, presenter.source.name) + startActivity(intent) } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt index ad159b1eb..12c687943 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/main/MainActivity.kt @@ -5,14 +5,13 @@ import android.app.SearchManager import android.content.Intent import android.graphics.Color import android.os.Bundle +import android.view.ViewGroup +import androidx.appcompat.graphics.drawable.DrawerArrowDrawable import androidx.core.view.GravityCompat import androidx.drawerlayout.widget.DrawerLayout -import androidx.appcompat.graphics.drawable.DrawerArrowDrawable -import android.view.ViewGroup import com.bluelinelabs.conductor.* import eu.kanade.tachiyomi.Migrations import eu.kanade.tachiyomi.R -import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.ui.base.activity.BaseActivity import eu.kanade.tachiyomi.ui.base.controller.* import eu.kanade.tachiyomi.ui.catalogue.CatalogueController @@ -26,15 +25,12 @@ import eu.kanade.tachiyomi.ui.recently_read.RecentlyReadController import eu.kanade.tachiyomi.ui.setting.SettingsMainController import eu.kanade.tachiyomi.util.openInBrowser import kotlinx.android.synthetic.main.main_activity.* -import uy.kohesive.injekt.injectLazy class MainActivity : BaseActivity() { private lateinit var router: Router - val preferences: PreferencesHelper by injectLazy() - private var drawerArrow: DrawerArrowDrawable? = null private var secondaryDrawer: ViewGroup? = null @@ -50,12 +46,6 @@ class MainActivity : BaseActivity() { lateinit var tabAnimator: TabsAnimator override fun onCreate(savedInstanceState: Bundle?) { - setTheme(when (preferences.theme()) { - 2 -> R.style.Theme_Tachiyomi_Dark - 3 -> R.style.Theme_Tachiyomi_Amoled - 4 -> R.style.Theme_Tachiyomi_DarkBlue - else -> R.style.Theme_Tachiyomi - }) super.onCreate(savedInstanceState) // Do not let the launcher create a new activity http://stackoverflow.com/questions/16283079 diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoController.kt index 221eae06a..703c23364 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaInfoController.kt @@ -40,6 +40,7 @@ import eu.kanade.tachiyomi.ui.catalogue.global_search.CatalogueSearchController import eu.kanade.tachiyomi.ui.library.ChangeMangaCategoriesDialog import eu.kanade.tachiyomi.ui.main.MainActivity import eu.kanade.tachiyomi.ui.manga.MangaController +import eu.kanade.tachiyomi.ui.webview.WebViewActivity import eu.kanade.tachiyomi.util.openInBrowser import eu.kanade.tachiyomi.util.snack import eu.kanade.tachiyomi.util.toast @@ -297,8 +298,9 @@ class MangaInfoController : NucleusController(), return } - parentController?.router?.pushController(MangaWebViewController(source.id, url) - .withFadeTransaction()) + val activity = activity ?: return + val intent = WebViewActivity.newIntent(activity, source.id, url, presenter.manga.title) + startActivity(intent) } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaWebViewController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaWebViewController.kt deleted file mode 100644 index 2ae3667e8..000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/info/MangaWebViewController.kt +++ /dev/null @@ -1,94 +0,0 @@ -package eu.kanade.tachiyomi.ui.manga.info - -import android.os.Bundle -import android.view.* -import android.webkit.WebView -import eu.kanade.tachiyomi.R -import eu.kanade.tachiyomi.source.SourceManager -import eu.kanade.tachiyomi.source.online.HttpSource -import eu.kanade.tachiyomi.ui.base.controller.BaseController -import eu.kanade.tachiyomi.util.WebViewClientCompat -import uy.kohesive.injekt.injectLazy - -class MangaWebViewController(bundle: Bundle? = null) : BaseController(bundle) { - - private val sourceManager by injectLazy() - - init { - setHasOptionsMenu(true) - } - - constructor(sourceId: Long, url: String) : this(Bundle().apply { - putLong(SOURCE_KEY, sourceId) - putString(URL_KEY, url) - }) - - override fun inflateView(inflater: LayoutInflater, container: ViewGroup): View { - return inflater.inflate(R.layout.manga_info_web_controller, container, false) - } - - override fun onViewCreated(view: View) { - super.onViewCreated(view) - val source = sourceManager.get(args.getLong(SOURCE_KEY)) as? HttpSource ?: return - val url = args.getString(URL_KEY) ?: return - val headers = source.headers.toMultimap().mapValues { it.value.getOrNull(0) ?: "" } - - val web = view as WebView - web.webViewClient = object : WebViewClientCompat() { - override fun shouldOverrideUrlCompat(view: WebView, url: String): Boolean { - view.loadUrl(url) - return true - } - } - web.settings.javaScriptEnabled = true - web.settings.userAgentString = source.headers["User-Agent"] - web.loadUrl(url, headers) - } - - override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { - inflater.inflate(R.menu.web_view, menu) - } - - override fun onPrepareOptionsMenu(menu: Menu) { - val web = view as WebView - menu.findItem(R.id.action_forward).isVisible = web.canGoForward() - } - - override fun onOptionsItemSelected(item: MenuItem): Boolean { - when (item.itemId) { - R.id.action_forward -> { - val web = view as WebView - if (web.canGoForward()) web.goForward() - } - R.id.action_refresh -> { - val web = view as WebView - web.reload() - } - R.id.action_close -> router.popController(this) - else -> return super.onOptionsItemSelected(item) - } - return true - } - - override fun handleBack(): Boolean { - val web = view as WebView - if (web.canGoBack()) { - web.goBack() - return true - } - return super.handleBack() - } - - override fun onDestroyView(view: View) { - val web = view as WebView - web.stopLoading() - web.destroy() - super.onDestroyView(view) - } - - private companion object { - const val SOURCE_KEY = "source_key" - const val URL_KEY = "url_key" - } - -} diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/webview/WebViewActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/webview/WebViewActivity.kt new file mode 100644 index 000000000..c3f394e1c --- /dev/null +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/webview/WebViewActivity.kt @@ -0,0 +1,115 @@ +package eu.kanade.tachiyomi.ui.webview + +import android.content.Context +import android.content.Intent +import android.graphics.Bitmap +import android.graphics.Color +import android.os.Bundle +import android.view.Menu +import android.view.MenuItem +import android.webkit.WebView +import androidx.core.graphics.ColorUtils +import eu.kanade.tachiyomi.R +import eu.kanade.tachiyomi.source.SourceManager +import eu.kanade.tachiyomi.source.online.HttpSource +import eu.kanade.tachiyomi.ui.base.activity.BaseActivity +import eu.kanade.tachiyomi.util.WebViewClientCompat +import kotlinx.android.synthetic.main.webview_activity.toolbar +import kotlinx.android.synthetic.main.webview_activity.webview +import uy.kohesive.injekt.injectLazy + + +class WebViewActivity : BaseActivity() { + + private val sourceManager by injectLazy() + + private var bundle: Bundle? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.webview_activity) + + title = intent.extras?.getString(TITLE_KEY) + setSupportActionBar(toolbar) + supportActionBar?.setDisplayHomeAsUpEnabled(true) + toolbar.setNavigationOnClickListener { + super.onBackPressed() + } + + if (bundle == null) { + val source = sourceManager.get(intent.extras!!.getLong(SOURCE_KEY)) as? HttpSource ?: return + val url = intent.extras!!.getString(URL_KEY) ?: return + val headers = source.headers.toMultimap().mapValues { it.value.getOrNull(0) ?: "" } + + webview.webViewClient = object : WebViewClientCompat() { + override fun shouldOverrideUrlCompat(view: WebView, url: String): Boolean { + view.loadUrl(url) + return true + } + + override fun onPageFinished(view: WebView?, url: String?) { + super.onPageFinished(view, url) + invalidateOptionsMenu() + title = view?.title + } + + override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) { + super.onPageStarted(view, url, favicon) + invalidateOptionsMenu() + } + } + webview.settings.javaScriptEnabled = true + webview.settings.userAgentString = source.headers["User-Agent"] + webview.loadUrl(url, headers) + } else { + webview.restoreState(bundle) + } + } + + override fun onCreateOptionsMenu(menu: Menu): Boolean { + menuInflater.inflate(R.menu.webview, menu) + return true + } + + override fun onPrepareOptionsMenu(menu: Menu?): Boolean { + val backItem = toolbar.menu.findItem(R.id.action_web_back) + val forwardItem = toolbar.menu.findItem(R.id.action_web_forward) + backItem?.isEnabled = webview.canGoBack() + forwardItem?.isEnabled = webview.canGoForward() + + val translucentWhite = ColorUtils.setAlphaComponent(Color.WHITE, 127) + backItem.icon?.setTint(if (webview.canGoBack()) Color.WHITE else translucentWhite) + forwardItem?.icon?.setTint(if (webview.canGoForward()) Color.WHITE else translucentWhite) + + return super.onPrepareOptionsMenu(menu) + } + + override fun onBackPressed() { + if (webview.canGoBack()) webview.goBack() + else super.onBackPressed() + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean { + when (item.itemId) { + R.id.action_web_back -> webview.goBack() + R.id.action_web_forward -> webview.goForward() + R.id.action_web_refresh -> webview.reload() + } + return super.onOptionsItemSelected(item) + } + + companion object { + private const val SOURCE_KEY = "source_key" + private const val URL_KEY = "url_key" + private const val TITLE_KEY = "title_key" + + fun newIntent(context: Context, sourceId: Long, url: String, title: String?): Intent { + val intent = Intent(context, WebViewActivity::class.java) + intent.putExtra(SOURCE_KEY, sourceId) + intent.putExtra(URL_KEY, url) + intent.putExtra(TITLE_KEY, title) + intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) + return intent + } + } +} diff --git a/app/src/main/res/drawable/ic_arrow_back_white_24dp.xml b/app/src/main/res/drawable/ic_arrow_back_white_24dp.xml new file mode 100644 index 000000000..38fbc261b --- /dev/null +++ b/app/src/main/res/drawable/ic_arrow_back_white_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_arrow_forward_white_24dp.xml b/app/src/main/res/drawable/ic_arrow_forward_white_24dp.xml new file mode 100644 index 000000000..5304b93e1 --- /dev/null +++ b/app/src/main/res/drawable/ic_arrow_forward_white_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/drawable/ic_close_white_24dp.xml b/app/src/main/res/drawable/ic_close_white_24dp.xml new file mode 100644 index 000000000..d11cc5c9c --- /dev/null +++ b/app/src/main/res/drawable/ic_close_white_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/main_activity.xml b/app/src/main/res/layout/main_activity.xml index b0b21d102..23310e519 100644 --- a/app/src/main/res/layout/main_activity.xml +++ b/app/src/main/res/layout/main_activity.xml @@ -20,7 +20,6 @@ + + + + + + + + + + + diff --git a/app/src/main/res/menu/web_view.xml b/app/src/main/res/menu/web_view.xml deleted file mode 100644 index 1e4056460..000000000 --- a/app/src/main/res/menu/web_view.xml +++ /dev/null @@ -1,19 +0,0 @@ - - - - - - - - - - diff --git a/app/src/main/res/menu/webview.xml b/app/src/main/res/menu/webview.xml new file mode 100644 index 000000000..ca02d3dfe --- /dev/null +++ b/app/src/main/res/menu/webview.xml @@ -0,0 +1,23 @@ + + + + + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index cbbe83762..fba4d407f 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -95,8 +95,9 @@ Restore Open Log in - Forward - Refresh + Back + Forward + Refresh Deleting… diff --git a/app/src/main/res/values/themes.xml b/app/src/main/res/values/themes.xml index 1ef10a8d4..4f2f48b96 100644 --- a/app/src/main/res/values/themes.xml +++ b/app/src/main/res/values/themes.xml @@ -44,7 +44,7 @@ @@ -67,7 +67,7 @@ @drawable/line_divider_dark true - @android:color/transparent + @color/colorDarkPrimaryDark @color/colorDarkPrimaryDark @@ -97,7 +97,7 @@ @color/colorPrimaryDark true - @android:color/transparent + @color/colorPrimaryDark @color/colorDarkPrimaryDark