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

Commit 1be4370b authored by Danny Baumann's avatar Danny Baumann Committed by Luca Stefani
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
(cherry picked from commit 8a47a2fe)
parent 760bdcd0
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -58,6 +58,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
@@ -29,6 +29,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;
@@ -69,6 +70,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;
import java.util.concurrent.Executor;
@@ -114,6 +117,8 @@ public class WebView extends AbsoluteLayout
    @UnsupportedAppUsage
    private static volatile boolean sEnforceThreadChecking = false;

    private Method mGetThemeColorMethod;

    /**
     *  Transportation object for returning WebView across thread boundaries.
     */
@@ -426,6 +431,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();
@@ -1313,6 +1325,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.
     *