From ee10b26ab92dd2f8a58687391e17f47e450b03bc Mon Sep 17 00:00:00 2001 From: Grace Coleman Date: Tue, 14 Jul 2020 10:30:20 -0400 Subject: [PATCH 01/50] setup --- WebView/app/src/main/AndroidManifest.xml | 2 +- .../samples/webviewdemo/MainActivity.kt | 12 ++++++++++ .../app/src/main/res/values-night/colors.xml | 22 +++++++++++++++++++ WebView/app/src/main/res/values/styles.xml | 2 +- 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 WebView/app/src/main/res/values-night/colors.xml diff --git a/WebView/app/src/main/AndroidManifest.xml b/WebView/app/src/main/AndroidManifest.xml index ec71c32b..019312dd 100644 --- a/WebView/app/src/main/AndroidManifest.xml +++ b/WebView/app/src/main/AndroidManifest.xml @@ -25,7 +25,7 @@ android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" - android:theme="@style/AppTheme"> + android:theme="@style/Theme.MyApp"> diff --git a/WebView/app/src/main/java/com/android/samples/webviewdemo/MainActivity.kt b/WebView/app/src/main/java/com/android/samples/webviewdemo/MainActivity.kt index 1b722863..a966afec 100644 --- a/WebView/app/src/main/java/com/android/samples/webviewdemo/MainActivity.kt +++ b/WebView/app/src/main/java/com/android/samples/webviewdemo/MainActivity.kt @@ -33,6 +33,7 @@ import androidx.core.content.ContextCompat.startActivity import androidx.webkit.JavaScriptReplyProxy import androidx.webkit.WebMessageCompat import androidx.webkit.WebSettingsCompat +import androidx.webkit.WebSettingsCompat.DARK_STRATEGY_WEB_THEME_DARKENING_ONLY import androidx.webkit.WebViewAssetLoader import androidx.webkit.WebViewClientCompat import androidx.webkit.WebViewCompat @@ -130,6 +131,8 @@ class MainActivity : AppCompatActivity() { val jsObjName = "jsObject" val allowedOriginRules = setOf("https://gcoleman799.github.io") + + // FORCE DARK // Check if the system is set to light or dark mode val nightModeFlag = resources.configuration.uiMode.and(Configuration.UI_MODE_NIGHT_MASK) if (nightModeFlag == Configuration.UI_MODE_NIGHT_YES) { @@ -142,6 +145,15 @@ class MainActivity : AppCompatActivity() { } } + // Custom Dark Theme + // DARK_STRATEGY_WEB_THEME_DARKENING_ONLY - it never applies automatic darkening and uses web page only darkening. + if (WebViewFeature.isFeatureSupported(WebViewFeature.FORCE_DARK_STRATEGY)) { + WebSettingsCompat.setForceDarkStrategy( + binding.webview.settings, + DARK_STRATEGY_WEB_THEME_DARKENING_ONLY + ) + } + // Configure asset loader with custom domain // * NOTE THAT *: // The assets path handler is set with the sub path /Asset-Loader/ here because we are tyring to ensure diff --git a/WebView/app/src/main/res/values-night/colors.xml b/WebView/app/src/main/res/values-night/colors.xml new file mode 100644 index 00000000..375d5b2d --- /dev/null +++ b/WebView/app/src/main/res/values-night/colors.xml @@ -0,0 +1,22 @@ + + + + + #6200EE + #3700B3 + #03DAC5 + \ No newline at end of file diff --git a/WebView/app/src/main/res/values/styles.xml b/WebView/app/src/main/res/values/styles.xml index e3b8d403..278ded4e 100644 --- a/WebView/app/src/main/res/values/styles.xml +++ b/WebView/app/src/main/res/values/styles.xml @@ -17,7 +17,7 @@ -