[Android] Theme About webview

Nicolas Pomepuy git at videolan.org
Tue Aug 13 10:27:09 CEST 2019


vlc-android | branch: 3.2.x | Nicolas Pomepuy <nicolas.pomepuy at gmail.com> | Mon Aug 12 10:17:03 2019 +0200| [c7f2929ceb81bc4cf57c81a219a2bca6d7c8d39b] | committer: Geoffrey Métais

Theme About webview

(cherry picked from commit 10c837e1f47adc5c638951eafa4f8b2846f5c70b)

> https://code.videolan.org/videolan/vlc-android/commit/c7f2929ceb81bc4cf57c81a219a2bca6d7c8d39b
---

 vlc-android/assets/licence.htm                     |  2 +-
 vlc-android/assets/licence_dark.css                |  1 +
 vlc-android/assets/licence_light.css               |  1 +
 .../src/org/videolan/vlc/gui/AboutFragment.kt      | 65 ++++++++++++++++++----
 4 files changed, 58 insertions(+), 11 deletions(-)

diff --git a/vlc-android/assets/licence.htm b/vlc-android/assets/licence.htm
index b662e9f70..ab9821b57 100644
--- a/vlc-android/assets/licence.htm
+++ b/vlc-android/assets/licence.htm
@@ -7,7 +7,7 @@
     <meta http-equiv="content-type" content="text/html;charset=utf-8" />
 </head>
 
-<body id="top" style="background-color: #EBEBEE">
+<body id="top">
     <h1>VLC for Android</h1>
     <p>Copyleft © 1996-2019 VideoLAN, VLC authors</p>
     <div>
diff --git a/vlc-android/assets/licence_dark.css b/vlc-android/assets/licence_dark.css
new file mode 100644
index 000000000..f13165282
--- /dev/null
+++ b/vlc-android/assets/licence_dark.css
@@ -0,0 +1 @@
+html,body{color:#fafafa;background:#323232;}a:link {color: #ff7d00;}
\ No newline at end of file
diff --git a/vlc-android/assets/licence_light.css b/vlc-android/assets/licence_light.css
new file mode 100644
index 000000000..39634dc78
--- /dev/null
+++ b/vlc-android/assets/licence_light.css
@@ -0,0 +1 @@
+html,body{color:#212121;background:#fafafa;}a:link {color: #ff7d00;}
\ No newline at end of file
diff --git a/vlc-android/src/org/videolan/vlc/gui/AboutFragment.kt b/vlc-android/src/org/videolan/vlc/gui/AboutFragment.kt
index 456ce7bf8..b6482fc08 100644
--- a/vlc-android/src/org/videolan/vlc/gui/AboutFragment.kt
+++ b/vlc-android/src/org/videolan/vlc/gui/AboutFragment.kt
@@ -20,11 +20,15 @@
 
 package org.videolan.vlc.gui
 
+import android.annotation.SuppressLint
+import android.content.res.Configuration
 import android.os.Bundle
+import android.util.Base64
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
 import android.webkit.WebView
+import android.webkit.WebViewClient
 import android.widget.ScrollView
 import androidx.appcompat.app.AppCompatActivity
 import androidx.fragment.app.Fragment
@@ -33,7 +37,6 @@ import com.google.android.material.tabs.TabLayout
 import kotlinx.coroutines.*
 import org.videolan.tools.coroutineScope
 import org.videolan.vlc.BuildConfig
-import org.videolan.vlc.R
 import org.videolan.vlc.gui.audio.AudioPagerAdapter
 import org.videolan.vlc.gui.helpers.UiTools
 import org.videolan.vlc.util.Util
@@ -46,27 +49,25 @@ private const val MODE_TOTAL = 2 // Number of audio browser modes
 class AboutFragment : Fragment() {
 
     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
-        return inflater.inflate(R.layout.about, container, false)
+        return inflater.inflate(org.videolan.vlc.R.layout.about, container, false)
     }
 
     override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
         super.onViewCreated(view, savedInstanceState)
 
         (activity as? AppCompatActivity)?.supportActionBar?.title = "VLC ${BuildConfig.VERSION_NAME}"
-        //Fix android 7 Locale problem with webView
-        //https://stackoverflow.com/questions/40398528/android-webview-locale-changes-abruptly-on-android-n
 
-        val aboutMain = view.findViewById<ScrollView>(R.id.about_main)
-        val webView = view.findViewById<WebView>(R.id.webview)
-        val revision = getString(R.string.build_revision)
+        val aboutMain = view.findViewById<ScrollView>(org.videolan.vlc.R.id.about_main)
+        val webView = view.findViewById<WebView>(org.videolan.vlc.R.id.webview)
+        val revision = getString(org.videolan.vlc.R.string.build_revision)
 
         val lists = arrayOf(aboutMain, webView)
-        val titles = arrayOf(getString(R.string.about), getString(R.string.licence))
-        val viewPager = view.findViewById<ViewPager>(R.id.pager).apply {
+        val titles = arrayOf(getString(org.videolan.vlc.R.string.about), getString(org.videolan.vlc.R.string.licence))
+        val viewPager = view.findViewById<ViewPager>(org.videolan.vlc.R.id.pager).apply {
             offscreenPageLimit = MODE_TOTAL - 1
             adapter = AudioPagerAdapter(lists as Array<View>, titles)
         }
-        requireActivity().findViewById<TabLayout>(R.id.sliding_tabs).apply {
+        requireActivity().findViewById<TabLayout>(org.videolan.vlc.R.id.sliding_tabs).apply {
             visibility = View.VISIBLE
             setupWithViewPager(viewPager)
         }
@@ -76,7 +77,51 @@ class AboutFragment : Fragment() {
             }
             UiTools.fillAboutView(view)
             webView.loadData(asset, "text/html", "UTF8")
+
+            webView.webViewClient = object : WebViewClient() {
+
+                override fun onPageFinished(view: WebView, url: String) {
+
+                    // Inject CSS when page is done loading
+                    injectCSS(webView, when (context?.resources?.configuration?.uiMode?.and(Configuration.UI_MODE_NIGHT_MASK)) {
+                        Configuration.UI_MODE_NIGHT_YES -> {
+                            "licence_dark.css"
+                        }
+                        Configuration.UI_MODE_NIGHT_NO -> {
+                            "licence_light.css"
+                        }
+                        else -> {
+                            "licence_light.css"
+                        }
+                    })
+                    super.onPageFinished(view, url)
+                }
+            }
+
+
         }
     }
 
+    @SuppressLint("SetJavaScriptEnabled")
+    private fun injectCSS(webView: WebView, cssAsset: String) {
+        try {
+            webView.settings.javaScriptEnabled = true
+            val inputStream = requireActivity().assets.open(cssAsset)
+            val buffer = ByteArray(inputStream.available())
+            inputStream.read(buffer)
+            inputStream.close()
+            val encoded = Base64.encodeToString(buffer, Base64.NO_WRAP)
+            webView.loadUrl("javascript:(function() {" +
+                    "var parent = document.getElementsByTagName('head').item(0);" +
+                    "var style = document.createElement('style');" +
+                    "style.type = 'text/css';" +
+                    // Tell the browser to BASE64-decode the string into your script !!!
+                    "style.innerHTML = window.atob('" + encoded + "');" +
+                    "parent.appendChild(style)" +
+                    "})()")
+            webView.settings.javaScriptEnabled = false
+        } catch (e: Exception) {
+            e.printStackTrace()
+        }
+    }
 }



More information about the Android mailing list