Convert webview into an activity (#2470)

Based on 65804ebb3a
This commit is contained in:
arkon 2020-01-10 22:22:12 -05:00 committed by GitHub
parent 8f82c8ad3d
commit 797553ce16
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 229 additions and 136 deletions

View File

@ -48,6 +48,9 @@
</activity> </activity>
<activity <activity
android:name=".ui.reader.ReaderActivity" /> android:name=".ui.reader.ReaderActivity" />
<activity
android:name=".ui.webview.WebViewActivity"
android:configChanges="uiMode|orientation|screenSize" />
<activity <activity
android:name=".widget.CustomLayoutPickerActivity" android:name=".widget.CustomLayoutPickerActivity"
android:label="@string/app_name" android:label="@string/app_name"

View File

@ -1,13 +1,29 @@
package eu.kanade.tachiyomi.ui.base.activity package eu.kanade.tachiyomi.ui.base.activity
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.util.LocaleHelper import eu.kanade.tachiyomi.util.LocaleHelper
import uy.kohesive.injekt.injectLazy
abstract class BaseActivity : AppCompatActivity() { abstract class BaseActivity : AppCompatActivity() {
val preferences: PreferencesHelper by injectLazy()
init { init {
@Suppress("LeakingThis") @Suppress("LeakingThis")
LocaleHelper.updateConfiguration(this) LocaleHelper.updateConfiguration(this)
} }
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)
}
} }

View File

@ -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.base.controller.withFadeTransaction
import eu.kanade.tachiyomi.ui.library.ChangeMangaCategoriesDialog import eu.kanade.tachiyomi.ui.library.ChangeMangaCategoriesDialog
import eu.kanade.tachiyomi.ui.manga.MangaController 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.util.*
import eu.kanade.tachiyomi.widget.AutofitRecyclerView import eu.kanade.tachiyomi.widget.AutofitRecyclerView
import kotlinx.android.synthetic.main.catalogue_controller.* import kotlinx.android.synthetic.main.catalogue_controller.*
@ -295,8 +295,9 @@ open class BrowseCatalogueController(bundle: Bundle) :
private fun openInWebView() { private fun openInWebView() {
val source = presenter.source as? HttpSource ?: return val source = presenter.source as? HttpSource ?: return
router.pushController(MangaWebViewController(source.id, source.baseUrl) val activity = activity ?: return
.withFadeTransaction()) val intent = WebViewActivity.newIntent(activity, source.id, source.baseUrl, presenter.source.name)
startActivity(intent)
} }
/** /**

View File

@ -5,14 +5,13 @@ import android.app.SearchManager
import android.content.Intent import android.content.Intent
import android.graphics.Color import android.graphics.Color
import android.os.Bundle import android.os.Bundle
import android.view.ViewGroup
import androidx.appcompat.graphics.drawable.DrawerArrowDrawable
import androidx.core.view.GravityCompat import androidx.core.view.GravityCompat
import androidx.drawerlayout.widget.DrawerLayout import androidx.drawerlayout.widget.DrawerLayout
import androidx.appcompat.graphics.drawable.DrawerArrowDrawable
import android.view.ViewGroup
import com.bluelinelabs.conductor.* import com.bluelinelabs.conductor.*
import eu.kanade.tachiyomi.Migrations import eu.kanade.tachiyomi.Migrations
import eu.kanade.tachiyomi.R 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.activity.BaseActivity
import eu.kanade.tachiyomi.ui.base.controller.* import eu.kanade.tachiyomi.ui.base.controller.*
import eu.kanade.tachiyomi.ui.catalogue.CatalogueController 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.ui.setting.SettingsMainController
import eu.kanade.tachiyomi.util.openInBrowser import eu.kanade.tachiyomi.util.openInBrowser
import kotlinx.android.synthetic.main.main_activity.* import kotlinx.android.synthetic.main.main_activity.*
import uy.kohesive.injekt.injectLazy
class MainActivity : BaseActivity() { class MainActivity : BaseActivity() {
private lateinit var router: Router private lateinit var router: Router
val preferences: PreferencesHelper by injectLazy()
private var drawerArrow: DrawerArrowDrawable? = null private var drawerArrow: DrawerArrowDrawable? = null
private var secondaryDrawer: ViewGroup? = null private var secondaryDrawer: ViewGroup? = null
@ -50,12 +46,6 @@ class MainActivity : BaseActivity() {
lateinit var tabAnimator: TabsAnimator lateinit var tabAnimator: TabsAnimator
override fun onCreate(savedInstanceState: Bundle?) { 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) super.onCreate(savedInstanceState)
// Do not let the launcher create a new activity http://stackoverflow.com/questions/16283079 // Do not let the launcher create a new activity http://stackoverflow.com/questions/16283079

View File

@ -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.library.ChangeMangaCategoriesDialog
import eu.kanade.tachiyomi.ui.main.MainActivity import eu.kanade.tachiyomi.ui.main.MainActivity
import eu.kanade.tachiyomi.ui.manga.MangaController 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.openInBrowser
import eu.kanade.tachiyomi.util.snack import eu.kanade.tachiyomi.util.snack
import eu.kanade.tachiyomi.util.toast import eu.kanade.tachiyomi.util.toast
@ -297,8 +298,9 @@ class MangaInfoController : NucleusController<MangaInfoPresenter>(),
return return
} }
parentController?.router?.pushController(MangaWebViewController(source.id, url) val activity = activity ?: return
.withFadeTransaction()) val intent = WebViewActivity.newIntent(activity, source.id, url, presenter.manga.title)
startActivity(intent)
} }
/** /**

View File

@ -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<SourceManager>()
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"
}
}

View File

@ -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<SourceManager>()
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
}
}
}

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M20,11H7.83l5.59,-5.59L12,4l-8,8 8,8 1.41,-1.41L7.83,13H20v-2z"/>
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M12,4l-1.41,1.41L16.17,11H4v2h12.17l-5.58,5.59L12,20l8,-8z"/>
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFFFF"
android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z"/>
</vector>

View File

@ -20,7 +20,6 @@
<androidx.appcompat.widget.Toolbar <androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar" android:id="@+id/toolbar"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" android:background="?attr/colorPrimary"

View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="?attr/actionBarTheme"
app:navigationIcon="@drawable/ic_close_white_24dp" />
</com.google.android.material.appbar.AppBarLayout>
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

View File

@ -1,19 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_forward"
android:title="@string/action_forward"
app:showAsAction="never" />
<item
android:id="@+id/action_refresh"
android:title="@string/action_refresh"
app:showAsAction="never" />
<item android:id="@+id/action_close"
android:title="@string/action_close"
app:showAsAction="never"/>
</menu>

View File

@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_web_back"
android:icon="@drawable/ic_arrow_back_white_24dp"
android:title="@string/action_webview_back"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_web_forward"
android:icon="@drawable/ic_arrow_forward_white_24dp"
android:title="@string/action_webview_forward"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_web_refresh"
android:icon="@drawable/ic_refresh_white_24dp"
android:title="@string/action_webview_refresh"
app:showAsAction="ifRoom" />
</menu>

View File

@ -95,8 +95,9 @@
<string name="action_restore">Restore</string> <string name="action_restore">Restore</string>
<string name="action_open">Open</string> <string name="action_open">Open</string>
<string name="action_login">Log in</string> <string name="action_login">Log in</string>
<string name="action_forward">Forward</string> <string name="action_webview_back">Back</string>
<string name="action_refresh">Refresh</string> <string name="action_webview_forward">Forward</string>
<string name="action_webview_refresh">Refresh</string>
<!-- Operations --> <!-- Operations -->
<string name="deleting">Deleting…</string> <string name="deleting">Deleting…</string>

View File

@ -44,7 +44,7 @@
<!--===========--> <!--===========-->
<style name="Theme.Tachiyomi" parent="Theme.Base"> <style name="Theme.Tachiyomi" parent="Theme.Base">
<item name="android:windowDrawsSystemBarBackgrounds">true</item> <item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item> <item name="android:statusBarColor">@color/colorPrimaryDark</item>
<item name="android:navigationBarColor">@color/colorPrimaryDark</item> <item name="android:navigationBarColor">@color/colorPrimaryDark</item>
</style> </style>
@ -67,7 +67,7 @@
<item name="android:listDivider">@drawable/line_divider_dark</item> <item name="android:listDivider">@drawable/line_divider_dark</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item> <item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item> <item name="android:statusBarColor">@color/colorDarkPrimaryDark</item>
<item name="android:navigationBarColor">@color/colorDarkPrimaryDark</item> <item name="android:navigationBarColor">@color/colorDarkPrimaryDark</item>
<!-- Themes --> <!-- Themes -->
@ -97,7 +97,7 @@
<item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item> <item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">@android:color/transparent</item> <item name="android:statusBarColor">@color/colorPrimaryDark</item>
<item name="android:navigationBarColor">@color/colorDarkPrimaryDark</item> <item name="android:navigationBarColor">@color/colorDarkPrimaryDark</item>
</style> </style>