From: Soren Stoutner Date: Fri, 15 Sep 2017 21:39:37 +0000 (-0700) Subject: Improve the application delay of night mode CSS. X-Git-Tag: v2.6~2 X-Git-Url: https://gitweb.stoutner.com/?a=commitdiff_plain;h=65531f439c8cb610d0e3e0f1e4127dc4d8a006d5;p=PrivacyBrowserAndroid.git Improve the application delay of night mode CSS. --- diff --git a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java index 008486f1..7549f5ca 100644 --- a/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java +++ b/app/src/main/java/com/stoutner/privacybrowser/activities/MainWebViewActivity.java @@ -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() { + @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); }