Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 170718b0 authored by Danny Baumann's avatar Danny Baumann Committed by Sam Mortimer
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
parent 23fbf77b
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -57,6 +57,15 @@ public class WebChromeClient {
    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.
+43 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import android.content.pm.PackageInfo;
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;
@@ -67,6 +68,8 @@ import java.io.BufferedWriter;
import java.io.File;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

@@ -354,6 +357,8 @@ public class WebView extends AbsoluteLayout
    // set in the WebView constructor.
    private static volatile boolean sEnforceThreadChecking = false;

    private Method mGetThemeColorMethod;

    /**
     *  Transportation object for returning WebView across thread boundaries.
     */
@@ -655,6 +660,13 @@ public class WebView extends AbsoluteLayout
        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();
@@ -1489,6 +1501,37 @@ public class WebView extends AbsoluteLayout
        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.
     *