diff --git a/app/build.gradle b/app/build.gradle index 4650a16a3..0e8c4aa3d 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -136,6 +136,7 @@ dependencies { implementation 'com.google.android.material:material:1.3.0' implementation 'com.google.android:flexbox:1.1.0' implementation 'com.google.code.gson:gson:2.8.6' + implementation 'androidx.webkit:webkit:1.4.0' //media def media2_version = "1.1.3" diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index c968d3e1f..5d897aaac 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -9,6 +9,8 @@ * 漫画源看书显示乱码,**阅读与其他软件的源并不通用**,请导入阅读的支持的漫画源! **2021/06/26** +* 订阅界面webView支持css prefers-color-scheme: dark 查询,需webView v76或更高版本 +* 如webView低于v76可以用js调用activity.isNightTheme()来获取当前是否暗模式 * 修复一些书籍导出epub失败 by ag2s20150909 **2021/06/22** diff --git a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt index 9570a7678..2a6767be9 100644 --- a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt @@ -12,6 +12,8 @@ import android.view.* import android.webkit.* import androidx.activity.viewModels import androidx.core.view.size +import androidx.webkit.WebSettingsCompat +import androidx.webkit.WebViewFeature import io.legado.app.R import io.legado.app.base.VMBaseActivity import io.legado.app.constant.AppConst @@ -95,6 +97,11 @@ class ReadRssActivity : VMBaseActivity return super.onCompatOptionsItemSelected(item) } + @JavascriptInterface + fun isNightTheme(): Boolean { + return AppConfig.isNightTheme(this) + } + @SuppressLint("SetJavaScriptEnabled") private fun initWebView() { binding.webView.webChromeClient = RssWebChromeClient() @@ -105,6 +112,7 @@ class ReadRssActivity : VMBaseActivity allowContentAccess = true //javaScriptEnabled = true } + binding.webView.addJavascriptInterface(this, "app") upWebViewTheme() binding.webView.setOnLongClickListener { val hitTestResult = binding.webView.hitTestResult @@ -193,8 +201,21 @@ class ReadRssActivity : VMBaseActivity private fun upWebViewTheme() { if (AppConfig.isNightTheme) { - binding.webView - .evaluateJavascript(AppConst.darkWebViewJs, null) + if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK_STRATEGY)) { + WebSettingsCompat.setForceDarkStrategy( + binding.webView.settings, + WebSettingsCompat.DARK_STRATEGY_PREFER_WEB_THEME_OVER_USER_AGENT_DARKENING + ) + } + if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK)) { + WebSettingsCompat.setForceDark( + binding.webView.settings, + WebSettingsCompat.FORCE_DARK_ON + ) + } else { + binding.webView + .evaluateJavascript(AppConst.darkWebViewJs, null) + } } }