[Android] License: Android TV license page based on about_licence xml

diegofn git at videolan.org
Fri Jun 5 10:16:40 CEST 2020


vlc-android | branch: master | diegofn <diegofn at me.com> | Sat May 30 16:58:16 2020 -0500| [68657c084c40901781cb23e366f1887f2c295728] | committer: Nicolas Pomepuy

License: Android TV license page based on about_licence xml
Inject CSS to dark mode in Android TV

(cherry picked from commit 66560b945cca63c3d35d3817671fa79ec81f0a67)

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

 .../org/videolan/television/ui/LicenceActivity.kt  | 39 +++++++++++++++++-----
 1 file changed, 30 insertions(+), 9 deletions(-)

diff --git a/application/television/src/main/java/org/videolan/television/ui/LicenceActivity.kt b/application/television/src/main/java/org/videolan/television/ui/LicenceActivity.kt
index 4df284ab4..bf7334783 100644
--- a/application/television/src/main/java/org/videolan/television/ui/LicenceActivity.kt
+++ b/application/television/src/main/java/org/videolan/television/ui/LicenceActivity.kt
@@ -1,10 +1,8 @@
 package org.videolan.television.ui
 
 import android.annotation.SuppressLint
-import android.content.res.Configuration
-import android.graphics.Color
 import android.os.Bundle
-import android.view.View
+import android.util.Base64
 import android.webkit.WebView
 import android.webkit.WebViewClient
 import androidx.fragment.app.FragmentActivity
@@ -16,15 +14,18 @@ class LicenceActivity : FragmentActivity() {
 
     override fun onCreate(savedInstanceState: Bundle?) {
         super.onCreate(savedInstanceState)
-        val revision = getString(R.string.build_revision)
-        val licence = WebView(this)
+        setContentView(org.videolan.television.R.layout.about_licence)
 
-        licence.loadUrl("file:///android_asset/licence.htm")
+        val webView = findViewById<WebView>(R.id.webview)
+        val revision = getString(R.string.build_revision)
+        webView.loadUrl("file:///android_asset/licence.htm")
 
-        licence.webViewClient = object : WebViewClient() {
+        webView.webViewClient = object : WebViewClient() {
 
             override fun onPageFinished(view: WebView, url: String) {
                 if (url.startsWith("file:///android_asset")) {
+                    // Inject CSS when page is done loading
+                    injectCSS(view, "licence_dark.css")
                     injectCommitRevision(view, revision)
                 }
                 super.onPageFinished(view, url)
@@ -32,11 +33,31 @@ class LicenceActivity : FragmentActivity() {
             }
         }
 
-        setContentView(licence)
-        (licence.parent as View).setBackgroundColor(Color.LTGRAY)
         applyOverscanMargin(this)
     }
 
+    @SuppressLint("SetJavaScriptEnabled")
+    private fun injectCSS(webView: WebView, cssAsset: String) {
+        try {
+            webView.settings.javaScriptEnabled = true
+
+            val buffer = Util.readAsset(cssAsset, "")
+            val encoded = Base64.encodeToString(buffer.toByteArray(), 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()
+        }
+    }
+
     @SuppressLint("SetJavaScriptEnabled")
     private fun injectCommitRevision(webView: WebView, revision: String) {
         try {



More information about the Android mailing list