parent
407fd016bc
commit
33edf5bda1
@ -1,94 +1,35 @@ |
||||
package io.legado.app.ui.book.login |
||||
|
||||
import android.annotation.SuppressLint |
||||
import android.graphics.Bitmap |
||||
import android.os.Bundle |
||||
import android.view.Menu |
||||
import android.view.MenuItem |
||||
import android.webkit.CookieManager |
||||
import android.webkit.WebView |
||||
import android.webkit.WebViewClient |
||||
import androidx.activity.viewModels |
||||
import io.legado.app.R |
||||
import io.legado.app.base.BaseActivity |
||||
import io.legado.app.data.entities.BookSource |
||||
import io.legado.app.databinding.ActivitySourceLoginBinding |
||||
import io.legado.app.help.http.CookieStore |
||||
import io.legado.app.utils.snackbar |
||||
import io.legado.app.utils.viewbindingdelegate.viewBinding |
||||
|
||||
|
||||
class SourceLoginActivity : BaseActivity<ActivitySourceLoginBinding>() { |
||||
|
||||
override val binding by viewBinding(ActivitySourceLoginBinding::inflate) |
||||
var sourceUrl: String? = null |
||||
var loginUrl: String? = null |
||||
var userAgent: String? = null |
||||
var checking = false |
||||
private val viewModel by viewModels<SourceLoginViewModel>() |
||||
|
||||
override fun onActivityCreated(savedInstanceState: Bundle?) { |
||||
sourceUrl = intent.getStringExtra("sourceUrl") |
||||
loginUrl = intent.getStringExtra("loginUrl") |
||||
userAgent = intent.getStringExtra("userAgent") |
||||
title = getString(R.string.login_source, sourceUrl) |
||||
initWebView() |
||||
intent.getStringExtra("sourceUrl")?.let { |
||||
viewModel.initData(it) { bookSource -> |
||||
initView(bookSource) |
||||
} |
||||
|
||||
@SuppressLint("SetJavaScriptEnabled") |
||||
private fun initWebView() { |
||||
val settings = binding.webView.settings |
||||
settings.setSupportZoom(true) |
||||
settings.builtInZoomControls = true |
||||
settings.javaScriptEnabled = true |
||||
userAgent?.let { |
||||
settings.userAgentString = it |
||||
} |
||||
val cookieManager = CookieManager.getInstance() |
||||
binding.webView.webViewClient = object : WebViewClient() { |
||||
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) { |
||||
val cookie = cookieManager.getCookie(url) |
||||
sourceUrl?.let { |
||||
CookieStore.setCookie(it, cookie) |
||||
} |
||||
super.onPageStarted(view, url, favicon) |
||||
} |
||||
|
||||
override fun onPageFinished(view: WebView?, url: String?) { |
||||
val cookie = cookieManager.getCookie(url) |
||||
sourceUrl?.let { |
||||
CookieStore.setCookie(it, cookie) |
||||
} |
||||
if (checking) { |
||||
finish() |
||||
} |
||||
super.onPageFinished(view, url) |
||||
private fun initView(bookSource: BookSource) { |
||||
if (bookSource.loginUi.isNullOrEmpty()) { |
||||
supportFragmentManager.beginTransaction() |
||||
.replace(R.id.fragment, WebViewLoginFragment()) |
||||
.commit() |
||||
} else { |
||||
RuleUiLoginDialog().show(supportFragmentManager, "ruleUiLogin") |
||||
} |
||||
} |
||||
loginUrl?.let { |
||||
binding.webView.loadUrl(it) |
||||
} |
||||
} |
||||
|
||||
override fun onCompatCreateOptionsMenu(menu: Menu): Boolean { |
||||
menuInflater.inflate(R.menu.source_login, menu) |
||||
return super.onCompatCreateOptionsMenu(menu) |
||||
} |
||||
|
||||
override fun onCompatOptionsItemSelected(item: MenuItem): Boolean { |
||||
when (item.itemId) { |
||||
R.id.menu_ok -> { |
||||
if (!checking) { |
||||
checking = true |
||||
binding.titleBar.snackbar(R.string.check_host_cookie) |
||||
loginUrl?.let { |
||||
binding.webView.loadUrl(it) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
return super.onCompatOptionsItemSelected(item) |
||||
} |
||||
|
||||
override fun onDestroy() { |
||||
super.onDestroy() |
||||
binding.webView.destroy() |
||||
} |
||||
} |
@ -0,0 +1,26 @@ |
||||
package io.legado.app.ui.book.login |
||||
|
||||
import android.app.Application |
||||
import io.legado.app.base.BaseViewModel |
||||
import io.legado.app.data.appDb |
||||
import io.legado.app.data.entities.BookSource |
||||
import io.legado.app.utils.toastOnUi |
||||
|
||||
class SourceLoginViewModel(application: Application) : BaseViewModel(application) { |
||||
|
||||
var bookSource: BookSource? = null |
||||
|
||||
fun initData(sourceUrl: String, success: (bookSource: BookSource) -> Unit) { |
||||
execute { |
||||
bookSource = appDb.bookSourceDao.getBookSource(sourceUrl) |
||||
bookSource |
||||
}.onSuccess { |
||||
if (it != null) { |
||||
success.invoke(it) |
||||
} else { |
||||
context.toastOnUi("未找到书源") |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,90 @@ |
||||
package io.legado.app.ui.book.login |
||||
|
||||
import android.annotation.SuppressLint |
||||
import android.graphics.Bitmap |
||||
import android.os.Bundle |
||||
import android.view.Menu |
||||
import android.view.MenuItem |
||||
import android.view.View |
||||
import android.webkit.CookieManager |
||||
import android.webkit.WebView |
||||
import android.webkit.WebViewClient |
||||
import androidx.fragment.app.activityViewModels |
||||
import io.legado.app.R |
||||
import io.legado.app.base.BaseFragment |
||||
import io.legado.app.constant.AppConst |
||||
import io.legado.app.data.entities.BookSource |
||||
import io.legado.app.databinding.FragmentWebViewLoginBinding |
||||
import io.legado.app.help.http.CookieStore |
||||
import io.legado.app.utils.snackbar |
||||
import io.legado.app.utils.viewbindingdelegate.viewBinding |
||||
|
||||
class WebViewLoginFragment : BaseFragment(R.layout.fragment_web_view_login) { |
||||
|
||||
private val binding by viewBinding(FragmentWebViewLoginBinding::bind) |
||||
private val viewModel by activityViewModels<SourceLoginViewModel>() |
||||
|
||||
private var checking = false |
||||
|
||||
override fun onFragmentCreated(view: View, savedInstanceState: Bundle?) { |
||||
viewModel.bookSource?.let { |
||||
binding.titleBar.title = getString(R.string.login_source, it.bookSourceName) |
||||
initWebView(it) |
||||
} |
||||
} |
||||
|
||||
override fun onCompatCreateOptionsMenu(menu: Menu) { |
||||
menuInflater.inflate(R.menu.source_login, menu) |
||||
} |
||||
|
||||
override fun onCompatOptionsItemSelected(item: MenuItem) { |
||||
when (item.itemId) { |
||||
R.id.menu_ok -> { |
||||
if (!checking) { |
||||
checking = true |
||||
binding.titleBar.snackbar(R.string.check_host_cookie) |
||||
viewModel.bookSource?.loginUrl?.let { |
||||
binding.webView.loadUrl(it) |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
@SuppressLint("SetJavaScriptEnabled") |
||||
private fun initWebView(bookSource: BookSource) { |
||||
val settings = binding.webView.settings |
||||
settings.setSupportZoom(true) |
||||
settings.builtInZoomControls = true |
||||
settings.javaScriptEnabled = true |
||||
bookSource.getHeaderMap()[AppConst.UA_NAME]?.let { |
||||
settings.userAgentString = it |
||||
} |
||||
val cookieManager = CookieManager.getInstance() |
||||
binding.webView.webViewClient = object : WebViewClient() { |
||||
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) { |
||||
val cookie = cookieManager.getCookie(url) |
||||
CookieStore.setCookie(bookSource.bookSourceUrl, cookie) |
||||
super.onPageStarted(view, url, favicon) |
||||
} |
||||
|
||||
override fun onPageFinished(view: WebView?, url: String?) { |
||||
val cookie = cookieManager.getCookie(url) |
||||
CookieStore.setCookie(bookSource.bookSourceUrl, cookie) |
||||
if (checking) { |
||||
activity?.finish() |
||||
} |
||||
super.onPageFinished(view, url) |
||||
} |
||||
} |
||||
bookSource.loginUrl?.let { |
||||
binding.webView.loadUrl(it) |
||||
} |
||||
} |
||||
|
||||
override fun onDestroy() { |
||||
super.onDestroy() |
||||
binding.webView.destroy() |
||||
} |
||||
|
||||
} |
@ -0,0 +1,19 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" |
||||
xmlns:app="http://schemas.android.com/apk/res-auto" |
||||
android:orientation="vertical"> |
||||
|
||||
<io.legado.app.ui.widget.TitleBar |
||||
android:id="@+id/title_bar" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
app:title="@string/login" /> |
||||
|
||||
<WebView |
||||
android:id="@+id/web_view" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="match_parent" /> |
||||
|
||||
</LinearLayout> |
Loading…
Reference in new issue