From 95a0c2b05a7e475db094011094424794cb5ac292 Mon Sep 17 00:00:00 2001 From: Michael Bai Date: Fri, 14 Jan 2022 12:37:19 -0800 Subject: [PATCH] WebView: Add set/getAlgorithmicDarkening APIs This patch also deprecates the force dark mode API. WebView can't add CTS test right now because the API's implementation is in Chromium, WebView needs to first define the API in framework, then land the change and drop the APK, afterward, CTS test will be added. Add compat-framework support for this evolution behavior. Bug: 214741472 Test: m, m offline-sdk-docs CTS-Coverage-Bug: 214742132 Change-Id: I5d254deb1f27655ea909fceaa5b09a081d71c763 --- core/api/current.txt | 12 ++-- core/api/system-current.txt | 1 + core/java/android/webkit/WebSettings.java | 70 +++++++++++++++++++++++ 3 files changed, 78 insertions(+), 5 deletions(-) diff --git a/core/api/current.txt b/core/api/current.txt index d66f4a45bfb5b..ee31c326db959 100644 --- a/core/api/current.txt +++ b/core/api/current.txt @@ -54679,6 +54679,7 @@ package android.webkit { public abstract class WebSettings { ctor public WebSettings(); method @Deprecated public abstract boolean enableSmoothTransition(); + method public boolean getAllowAlgorithmicDarkening(); method public abstract boolean getAllowContentAccess(); method public abstract boolean getAllowFileAccess(); method public abstract boolean getAllowFileAccessFromFileURLs(); @@ -54700,7 +54701,7 @@ package android.webkit { method public abstract boolean getDomStorageEnabled(); method public abstract String getFantasyFontFamily(); method public abstract String getFixedFontFamily(); - method public int getForceDark(); + method @Deprecated public int getForceDark(); method public abstract boolean getJavaScriptCanOpenWindowsAutomatically(); method public abstract boolean getJavaScriptEnabled(); method public abstract android.webkit.WebSettings.LayoutAlgorithm getLayoutAlgorithm(); @@ -54723,6 +54724,7 @@ package android.webkit { method public abstract int getTextZoom(); method public abstract boolean getUseWideViewPort(); method public abstract String getUserAgentString(); + method public void setAllowAlgorithmicDarkening(boolean); method public abstract void setAllowContentAccess(boolean); method public abstract void setAllowFileAccess(boolean); method @Deprecated public abstract void setAllowFileAccessFromFileURLs(boolean); @@ -54744,7 +54746,7 @@ package android.webkit { method @Deprecated public abstract void setEnableSmoothTransition(boolean); method public abstract void setFantasyFontFamily(String); method public abstract void setFixedFontFamily(String); - method public void setForceDark(int); + method @Deprecated public void setForceDark(int); method @Deprecated public abstract void setGeolocationDatabasePath(String); method public abstract void setGeolocationEnabled(boolean); method public abstract void setJavaScriptCanOpenWindowsAutomatically(boolean); @@ -54775,9 +54777,9 @@ package android.webkit { method public abstract void setUserAgentString(@Nullable String); method public abstract boolean supportMultipleWindows(); method public abstract boolean supportZoom(); - field public static final int FORCE_DARK_AUTO = 1; // 0x1 - field public static final int FORCE_DARK_OFF = 0; // 0x0 - field public static final int FORCE_DARK_ON = 2; // 0x2 + field @Deprecated public static final int FORCE_DARK_AUTO = 1; // 0x1 + field @Deprecated public static final int FORCE_DARK_OFF = 0; // 0x0 + field @Deprecated public static final int FORCE_DARK_ON = 2; // 0x2 field public static final int LOAD_CACHE_ELSE_NETWORK = 1; // 0x1 field public static final int LOAD_CACHE_ONLY = 3; // 0x3 field public static final int LOAD_DEFAULT = -1; // 0xffffffff diff --git a/core/api/system-current.txt b/core/api/system-current.txt index b869116fc10b4..471af9d6f6d22 100644 --- a/core/api/system-current.txt +++ b/core/api/system-current.txt @@ -15407,6 +15407,7 @@ package android.webkit { method @Deprecated public abstract void setUseWebViewBackgroundForOverscrollBackground(boolean); method @Deprecated public abstract void setUserAgent(int); method public abstract void setVideoOverlayForEmbeddedEncryptedVideoEnabled(boolean); + field public static final long ENABLE_SIMPLIFIED_DARK_MODE = 214741472L; // 0xcccb1e0L } public class WebStorage { diff --git a/core/java/android/webkit/WebSettings.java b/core/java/android/webkit/WebSettings.java index 364ba505f7b0e..f14c25140d105 100644 --- a/core/java/android/webkit/WebSettings.java +++ b/core/java/android/webkit/WebSettings.java @@ -19,6 +19,8 @@ package android.webkit; import android.annotation.IntDef; import android.annotation.Nullable; import android.annotation.SystemApi; +import android.compat.annotation.ChangeId; +import android.compat.annotation.EnabledSince; import android.compat.annotation.UnsupportedAppUsage; import android.content.Context; @@ -134,6 +136,20 @@ public abstract class WebSettings { @Retention(RetentionPolicy.SOURCE) public @interface CacheMode {} + /** + * Enable web content to apply light or dark style according to the app's theme + * and WebView to attempt to darken web content by algorithmic darkening when + * appropriate. + * + * Refer to {@link #setAllowAlgorithmicDarkening} for detail. + * + * @hide + */ + @ChangeId + @EnabledSince(targetSdkVersion = android.os.Build.VERSION_CODES.TIRAMISU) + @SystemApi + public static final long ENABLE_SIMPLIFIED_DARK_MODE = 214741472L; + /** * Default cache usage mode. If the navigation type doesn't impose any * specific behavior, use cached resources when they are available @@ -239,6 +255,7 @@ public abstract class WebSettings { * automatically darkened. * * @see #setForceDark + * @deprecated refer to {@link #setForceDark} */ public static final int FORCE_DARK_OFF = 0; @@ -250,6 +267,7 @@ public abstract class WebSettings { * be inverted. * * @see #setForceDark + * @deprecated refer to {@link #setForceDark} */ public static final int FORCE_DARK_AUTO = 1; @@ -258,6 +276,7 @@ public abstract class WebSettings { * as to emulate a dark theme. * * @see #setForceDark + * @deprecated refer to {@link #setForceDark} */ public static final int FORCE_DARK_ON = 2; @@ -1533,6 +1552,13 @@ public abstract class WebSettings { * * @param forceDark the force dark mode to set. * @see #getForceDark + * @deprecated The "force dark" model previously implemented by WebView was complex + * and didn't interoperate well with current Web standards for + * prefers-color-scheme and color-scheme. In apps with + * {@code targetSdkVersion} ≥ {@link android.os.Build.VERSION_CODES#TIRAMISU} + * this API is a no-op and WebView will always use the dark style defined by web content + * authors if the app's theme is dark. To customize the behavior, refer to + * {@link #setAllowAlgorithmicDarkening}. */ public void setForceDark(@ForceDark int forceDark) { // Stub implementation to satisfy Roboelectrc shadows that don't override this yet. @@ -1544,12 +1570,56 @@ public abstract class WebSettings { * * @return the currently set force dark mode. * @see #setForceDark + * @deprecated refer to {@link #setForceDark}. */ public @ForceDark int getForceDark() { // Stub implementation to satisfy Roboelectrc shadows that don't override this yet. return FORCE_DARK_AUTO; } + /** + * Control whether algorithmic darkening is allowed. + * + *

+ * Note: This API and the behaviour described only apply to apps with + * {@code targetSdkVersion} ≥ {@link android.os.Build.VERSION_CODES#TIRAMISU}. + * + *

+ * WebView always sets the media query {@code prefers-color-scheme} according to the app's + * theme attribute {@link android.R.styleable#Theme_isLightTheme isLightTheme}, i.e. + * {@code prefers-color-scheme} is {@code light} if isLightTheme is true or not specified, + * otherwise it is {@code dark}. This means that the web content's light or dark style will + * be applied automatically to match the app's theme if the content supports it. + * + *

+ * Algorithmic darkening is disallowed by default. + *

+ * If the app's theme is dark and it allows algorithmic darkening, WebView will attempt to + * darken web content using an algorithm, if the content doesn't define its own dark styles + * and doesn't explicitly disable darkening. + * + *

+ * If Android is applying Force Dark to WebView then WebView will ignore the value of + * this setting and behave as if it were set to true. + * + * @param allow allow algorithmic darkening or not. + */ + public void setAllowAlgorithmicDarkening(boolean allow) { + // Stub implementation to satisfy Roboelectrc shadows that don't override this yet. + } + + /** + * Get if algorithmic darkening is allowed or not for this WebView. + * The default is false. + * + * @return if the algorithmic darkening is allowed or not. + * @see #setAllowAlgorithmicDarkening + */ + public boolean getAllowAlgorithmicDarkening() { + // Stub implementation to satisfy Roboelectrc shadows that don't override this yet. + return false; + } + /** * @hide */