Permalink
Browse files

Add support for page theme color to WebView and WebChromeClient.

Will only be implemented by system WebView, so users will have to call
the isThemeColorSupported() method in order to check whether the current
WebView implementation supports this.

Change-Id: Iaf4c3b8e4b8b7d8a74303b32d1c17c836dc744da
  • Loading branch information...
maniac103 authored and xlxfoxxlx committed May 18, 2017
1 parent c97150c commit 94c2e3842457664e3e25a0c3d5bc246af7514fd7
Showing with 52 additions and 0 deletions.
  1. +9 −0 core/java/android/webkit/WebChromeClient.java
  2. +43 −0 core/java/android/webkit/WebView.java
@@ -57,6 +57,15 @@ public void onReceivedIcon(WebView view, Bitmap icon) {}
public void onReceivedTouchIconUrl(WebView view, String url,
boolean precomposed) {}
/**
* Notify the host application of a new theme color.
* @param view The WebView that initiated the callback.
* @param color The newly set theme color, which may be partially transparent.
* A value of Color.TRANSPARENT denotes no theme color being set.
* @hide
*/
public void onThemeColorChanged(WebView view, int color) {}
/**
* A callback interface used by the host application to notify
* the current page that its custom view has been dismissed.
@@ -24,6 +24,7 @@
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Picture;
import android.graphics.Rect;
@@ -58,6 +59,8 @@
import java.io.BufferedWriter;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
/**
@@ -338,6 +341,8 @@
// set in the WebView constructor.
private static volatile boolean sEnforceThreadChecking = false;
private Method mGetThemeColorMethod;
/**
* Transportation object for returning WebView across thread boundaries.
*/
@@ -633,6 +638,13 @@ protected WebView(Context context, AttributeSet attrs, int defStyleAttr, int def
checkThread();
ensureProviderCreated();
try {
mGetThemeColorMethod = mProvider.getClass().getMethod("getThemeColor");
} catch (Exception e) {
// ignored, no theme color support
}
mProvider.init(javaScriptInterfaces, privateBrowsing);
// Post condition of creating a webview is the CookieSyncManager.getInstance() is allowed.
CookieSyncManager.setGetInstanceIsAllowed();
@@ -1487,6 +1499,37 @@ public int getProgress() {
return mProvider.getProgress();
}
/**
* Checks whether the WebView implementation has support for fetching
* the theme color set by the page.
*
* @return true if the WebView supports the getThemeColor() method
* @hide
*/
public boolean isThemeColorSupported() {
return mGetThemeColorMethod != null;
}
/**
* Gets the theme color set by the page.
*
* The returned color may not be fully opaque. If the page didn't set
* any theme color, Color.TRANSPARENT is returned.
*
* @return theme color set by the page
* @hide
*/
public int getThemeColor() {
if (mGetThemeColorMethod != null) {
try {
return (Integer) mGetThemeColorMethod.invoke(mProvider);
} catch (IllegalAccessException | InvocationTargetException e) {
// ignored, fall back to returning transparent
}
}
return Color.TRANSPARENT;
}
/**
* Gets the height of the HTML content.
*

0 comments on commit 94c2e38

Please sign in to comment.