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

Commit da7d6422 authored by Selim Gurun's avatar Selim Gurun
Browse files

Publish Print Api for Webview

Bug:10652036

Change-Id: Ibcc7e48f4e4ce1bc437c0d3fb89d3d66a90dca64

This is a cherry-pick of a5c86c64
to prevent a automerger failure.

Conflicts:
	core/java/android/webkit/WebView.java
	core/java/android/webkit/WebViewClassic.java
	core/java/android/webkit/WebViewProvider.java
parent f4f613b1
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -30229,6 +30229,7 @@ package android.webkit {
    method public void clearSslPreferences();
    method public deprecated void clearView();
    method public android.webkit.WebBackForwardList copyBackForwardList();
    method public android.print.PrintDocumentAdapter createPrintDocumentAdapter();
    method public void destroy();
    method public void documentHasImages(android.os.Message);
    method public void evaluateJavascript(java.lang.String, android.webkit.ValueCallback<java.lang.String>);
+12 −22
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ import android.os.Bundle;
import android.os.Looper;
import android.os.Message;
import android.os.StrictMode;
import android.print.PrintDocumentAdapter;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
@@ -1108,32 +1109,21 @@ public class WebView extends AbsoluteLayout
    }

    /**
     * Exports the contents of this Webview as PDF. Only supported for API levels
     * Creates a PrintDocumentAdapter that provides the content of this Webview for printing.
     * Only supported for API levels
     * {@link android.os.Build.VERSION_CODES#KITKAT} and above.
     *
     * @param out            The stream to export the PDF contents to. Cannot be null.
     * @param width          The page width. Should be larger than 0.
     * @param height         The page height. Should be larger than 0.
     * @param resultCallback A callback to be invoked when the PDF content is exported.
     *                       A true indicates success, and a false failure.
     *
     * TODO: explain method parameters, margins, consider making the callback
     * return more meaningful information, explain any threading concerns, HW
     * draw limitations, and make it public.
     * TODO: at the moment we are asking app to provide paper size information (width
     * and height). This is likely not ideal (I think need margin info too).
     * Another approach would be using PrintAttributes. This is to be clarified later.
     *
     * TODO: explain this webview will not draw during export (onDraw will clear to
     * background color) so recommend taking it offscreen, or putting in a layer with an
     * overlaid progress UI / spinner.
     * @hide
     * The adapter works by converting the Webview contents to a PDF stream. The Webview cannot
     * be drawn during the conversion process - any such draws are undefined. It is recommended
     * to use a dedicated off screen Webview for the printing. If necessary, an application may
     * temporarily hide a visible WebView by using a custom PrintDocumentAdapter instance
     * wrapped around the object returned and observing the onStart and onFinish methods. See
     * {@link android.print.PrintDocumentAdapter} for more information.
     */
    public void exportToPdf(OutputStream out, int width, int height,
            ValueCallback<Boolean> resultCallback) {
    public PrintDocumentAdapter createPrintDocumentAdapter() {
        checkThread();
        if (DebugFlags.TRACE_API) Log.d(LOGTAG, "exportToPdf");
        mProvider.exportToPdf(out, width, height, resultCallback);
        if (DebugFlags.TRACE_API) Log.d(LOGTAG, "createPrintDocumentAdapter");
        return mProvider.createPrintDocumentAdapter();
    }

    /**
+3 −3
Original line number Diff line number Diff line
@@ -61,6 +61,7 @@ import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.print.PrintDocumentAdapter;
import android.security.KeyChain;
import android.text.Editable;
import android.text.InputType;
@@ -2896,11 +2897,10 @@ public final class WebViewClassic implements WebViewProvider, WebViewProvider.Sc
    }

    /**
     * See {@link WebView#exportToPdf()}
     * See {@link WebView#createPrintDocumentAdapter()}
     */
    @Override
    public void exportToPdf(java.io.OutputStream out, int width, int height,
            ValueCallback<Boolean> resultCallback) {
    public PrintDocumentAdapter createPrintDocumentAdapter() {
        // K-only API not implemented in WebViewClassic.
        throw new IllegalStateException("This API not supported on Android 4.3 and earlier");

+2 −2
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import android.graphics.drawable.Drawable;
import android.net.http.SslCertificate;
import android.os.Bundle;
import android.os.Message;
import android.print.PrintDocumentAdapter;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
@@ -147,8 +148,7 @@ public interface WebViewProvider {

    public Picture capturePicture();

    public void exportToPdf(OutputStream out, int width, int height,
            ValueCallback<Boolean> resultCallback);
    public PrintDocumentAdapter createPrintDocumentAdapter();

    public float getScale();