From b356564db136aef2cd6b14e6e135cb028597d8d0 Mon Sep 17 00:00:00 2001 From: Svetoslav Ganov Date: Wed, 30 Oct 2013 18:51:45 -0700 Subject: Update the print from off-screen WebView sample To print we create an off-screen WebView but do not destroy it immeidately after printing. WebViews are somehow expensive and we do not want to keep them around more than needed. Change-Id: Ic9c78994e0c96d4d08c0e318c459acddd2e5a652 --- .../android/apis/app/PrintHtmlOffScreen.java | 55 +++++++++++++++++++--- 1 file changed, 49 insertions(+), 6 deletions(-) diff --git a/samples/ApiDemos/src/com/example/android/apis/app/PrintHtmlOffScreen.java b/samples/ApiDemos/src/com/example/android/apis/app/PrintHtmlOffScreen.java index 3625784cd..9c239b867 100644 --- a/samples/ApiDemos/src/com/example/android/apis/app/PrintHtmlOffScreen.java +++ b/samples/ApiDemos/src/com/example/android/apis/app/PrintHtmlOffScreen.java @@ -19,6 +19,11 @@ package com.example.android.apis.app; import android.app.Activity; import android.content.Context; import android.os.Bundle; +import android.os.CancellationSignal; +import android.os.ParcelFileDescriptor; +import android.print.PageRange; +import android.print.PrintAttributes; +import android.print.PrintDocumentAdapter; import android.print.PrintManager; import android.view.Menu; import android.view.MenuItem; @@ -74,16 +79,54 @@ public class PrintHtmlOffScreen extends Activity { mWebView.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { - // Get the print manager. - PrintManager printManager = (PrintManager) getSystemService( - Context.PRINT_SERVICE); - // Pass in the ViewView's document adapter. - printManager.print("MotoGP stats", mWebView.createPrintDocumentAdapter(), - null); + doPrint(); } }); // Load an HTML page. mWebView.loadUrl("file:///android_res/raw/motogp_stats.html"); } + + private void doPrint() { + // Get the print manager. + PrintManager printManager = (PrintManager) getSystemService( + Context.PRINT_SERVICE); + + // Create a wrapper PrintDocumentAdapter to clean up when done. + PrintDocumentAdapter adapter = new PrintDocumentAdapter() { + private final PrintDocumentAdapter mWrappedInstance = + mWebView.createPrintDocumentAdapter(); + + @Override + public void onStart() { + mWrappedInstance.onStart(); + } + + @Override + public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, + CancellationSignal cancellationSignal, LayoutResultCallback callback, + Bundle extras) { + mWrappedInstance.onLayout(oldAttributes, newAttributes, cancellationSignal, + callback, extras); + } + + @Override + public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, + CancellationSignal cancellationSignal, WriteResultCallback callback) { + mWrappedInstance.onWrite(pages, destination, cancellationSignal, callback); + } + + @Override + public void onFinish() { + mWrappedInstance.onFinish(); + // Intercept the finish call to know when printing is done + // and destroy the WebView as it is expensive to keep around. + mWebView.destroy(); + mWebView = null; + } + }; + + // Pass in the ViewView's document adapter. + printManager.print("MotoGP stats", adapter, null); + } } -- cgit v1.2.3