diff options
author | Svetoslav Ganov <svetoslavganov@google.com> | 2013-10-30 18:51:45 -0700 |
---|---|---|
committer | Svetoslav Ganov <svetoslavganov@google.com> | 2013-10-30 18:51:49 -0700 |
commit | b356564db136aef2cd6b14e6e135cb028597d8d0 (patch) | |
tree | fea91a8edf5455e30973304ef651f1744e90511d | |
parent | 42c6250c9db71d1a1d0c36c560954831fc1dacbc (diff) | |
download | android_development-b356564db136aef2cd6b14e6e135cb028597d8d0.tar.gz android_development-b356564db136aef2cd6b14e6e135cb028597d8d0.tar.bz2 android_development-b356564db136aef2cd6b14e6e135cb028597d8d0.zip |
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
-rw-r--r-- | samples/ApiDemos/src/com/example/android/apis/app/PrintHtmlOffScreen.java | 55 |
1 files 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); + } } |