Improve the application delay of night mode CSS.
authorSoren Stoutner <soren@stoutner.com>
Fri, 15 Sep 2017 21:39:37 +0000 (14:39 -0700)
committerSoren Stoutner <soren@stoutner.com>
Fri, 15 Sep 2017 21:39:37 +0000 (14:39 -0700)
app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java

index 008486f..7549f5c 100644 (file)
@@ -77,6 +77,7 @@ import android.webkit.CookieManager;
 import android.webkit.DownloadListener;
 import android.webkit.HttpAuthHandler;
 import android.webkit.SslErrorHandler;
+import android.webkit.ValueCallback;
 import android.webkit.WebBackForwardList;
 import android.webkit.WebChromeClient;
 import android.webkit.WebResourceResponse;
@@ -951,29 +952,35 @@ public class MainWebViewActivity extends AppCompatActivity implements Navigation
                     progressBar.setVisibility(View.GONE);
 
                     // Inject the night mode CSS if night mode is enabled.
-                    if (nightMode) {
+                    if (nightMode) {  // Night mode is enabled.
                         // `background-color: #212121` sets the background to be dark gray.  `color: #BDBDBD` sets the text color to be light gray.  `box-shadow: none` removes a lower underline on links used by WordPress.
                         // `text-decoration: none` removes all text underlines.  `text-shadow: none` removes text shadows, which usually have a hard coded color.  `border: none` removes all borders, which can also be used to underline text.
                         // `a {color: #1565C0}` sets links to be a dark blue.  `!important` takes precedent over any existing sub-settings.
                         mainWebView.evaluateJavascript("(function() {var parent = document.getElementsByTagName('head').item(0); var style = document.createElement('style'); style.type = 'text/css'; style.innerHTML = '" +
                                 "* {background-color: #212121 !important; color: #BDBDBD !important; box-shadow: none !important; text-decoration: none !important; text-shadow: none !important; border: none !important;}" +
                                 "a {color: #1565C0 !important;}" +
-                                "'; parent.appendChild(style)})()", null);
+                                "'; parent.appendChild(style)})()", new ValueCallback<String>() {
+                            @Override
+                            public void onReceiveValue(String value) {
+                                // Initialize a `Handler` to display `mainWebView`.
+                                Handler displayWebViewHandler = new Handler();
+
+                                // Setup a `Runnable` to display `mainWebView` after a delay to allow the CSS to be applied.
+                                Runnable displayWebViewRunnable = new Runnable() {
+                                    public void run() {
+                                        mainWebView.setVisibility(View.VISIBLE);
+                                    }
+                                };
+
+                                // Use `displayWebViewHandler` to delay the displaying of `mainWebView` for 500 milliseconds.
+                                displayWebViewHandler.postDelayed(displayWebViewRunnable, 500);
+                            }
+                        });
+                    } else {  // Night mode is disabled.
+                        // Display `mainWebView` in case it was hidden before loading domain settings.
+                        mainWebView.setVisibility(View.VISIBLE);
                     }
 
-                    // Initialize a `Handler` to display `mainWebView`, which may have been hid by a night mode domain setting even if night mode is not currently enabled.
-                    Handler displayWebViewHandler = new Handler();
-
-                    // Setup a `Runnable` to display `mainWebView` after a delay to allow the CSS to be applied.
-                    Runnable displayWebViewRunnable = new Runnable() {
-                        public void run() {
-                            mainWebView.setVisibility(View.VISIBLE);
-                        }
-                    };
-
-                    // Use `displayWebViewHandler` to delay the displaying of `mainWebView` for 1000 milliseconds.
-                    displayWebViewHandler.postDelayed(displayWebViewRunnable, 1000);
-
                     //Stop the `SwipeToRefresh` indicator if it is running
                     swipeRefreshLayout.setRefreshing(false);
                 }