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
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 3625784..9c239b8 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 @@
 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 @@
         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);
+    }
 }