summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSvetoslav Ganov <svetoslavganov@google.com>2013-10-31 01:56:29 +0000
committerAndroid (Google) Code Review <android-gerrit@google.com>2013-10-31 01:56:29 +0000
commit7c833d7164cc8689da9495dc2d3c5a3185296e07 (patch)
treed7793a4a587264f60a5ee896001813b8294f5e40
parentddba6b876843c72e49b812d028f19e0be656ab9f (diff)
parentb356564db136aef2cd6b14e6e135cb028597d8d0 (diff)
downloadandroid_development-7c833d7164cc8689da9495dc2d3c5a3185296e07.tar.gz
android_development-7c833d7164cc8689da9495dc2d3c5a3185296e07.tar.bz2
android_development-7c833d7164cc8689da9495dc2d3c5a3185296e07.zip
Merge "Update the print from off-screen WebView sample" into klp-dev
-rw-r--r--samples/ApiDemos/src/com/example/android/apis/app/PrintHtmlOffScreen.java55
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);
+ }
}