1 /* 2 * Copyright (C) 2013 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.apis.app; 18 19 import android.app.Activity; 20 import android.content.Context; 21 import android.os.Bundle; 22 import android.os.CancellationSignal; 23 import android.os.ParcelFileDescriptor; 24 import android.print.PageRange; 25 import android.print.PrintAttributes; 26 import android.print.PrintDocumentAdapter; 27 import android.print.PrintManager; 28 import android.view.Menu; 29 import android.view.MenuItem; 30 import android.webkit.WebView; 31 import android.webkit.WebViewClient; 32 33 import com.example.android.apis.R; 34 35 /** 36 * This class demonstrates how to implement HTML content printing 37 * from a {@link WebView} which is not shown on the screen. 38 * <p> 39 * This activity shows a text prompt and when the user chooses the 40 * print option from the overflow menu an HTML page with content that 41 * is not on the screen is printed via an off-screen {@link WebView}. 42 * </p> 43 * 44 * @see PrintManager 45 * @see WebView 46 */ 47 public class PrintHtmlOffScreen extends Activity { 48 49 private WebView mWebView; 50 51 @Override onCreate(Bundle savedInstanceState)52 protected void onCreate(Bundle savedInstanceState) { 53 super.onCreate(savedInstanceState); 54 setContentView(R.layout.print_html_off_screen); 55 } 56 57 @Override onCreateOptionsMenu(Menu menu)58 public boolean onCreateOptionsMenu(Menu menu) { 59 super.onCreateOptionsMenu(menu); 60 getMenuInflater().inflate(R.menu.print_custom_content, menu); 61 return true; 62 } 63 64 @Override onOptionsItemSelected(MenuItem item)65 public boolean onOptionsItemSelected(MenuItem item) { 66 if (item.getItemId() == R.id.menu_print) { 67 print(); 68 return true; 69 } 70 return super.onOptionsItemSelected(item); 71 } 72 print()73 private void print() { 74 // Create a WebView and hold on to it as the printing will start when 75 // load completes and we do not want the WbeView to be garbage collected. 76 mWebView = new WebView(this); 77 78 // Important: Only after the page is loaded we will do the print. 79 mWebView.setWebViewClient(new WebViewClient() { 80 @Override 81 public void onPageFinished(WebView view, String url) { 82 doPrint(); 83 } 84 }); 85 86 // Load an HTML page. 87 mWebView.loadUrl("file:///android_res/raw/motogp_stats.html"); 88 } 89 doPrint()90 private void doPrint() { 91 // Get the print manager. 92 PrintManager printManager = (PrintManager) getSystemService( 93 Context.PRINT_SERVICE); 94 95 // Create a wrapper PrintDocumentAdapter to clean up when done. 96 PrintDocumentAdapter adapter = new PrintDocumentAdapter() { 97 private final PrintDocumentAdapter mWrappedInstance = 98 mWebView.createPrintDocumentAdapter(); 99 100 @Override 101 public void onStart() { 102 mWrappedInstance.onStart(); 103 } 104 105 @Override 106 public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, 107 CancellationSignal cancellationSignal, LayoutResultCallback callback, 108 Bundle extras) { 109 mWrappedInstance.onLayout(oldAttributes, newAttributes, cancellationSignal, 110 callback, extras); 111 } 112 113 @Override 114 public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, 115 CancellationSignal cancellationSignal, WriteResultCallback callback) { 116 mWrappedInstance.onWrite(pages, destination, cancellationSignal, callback); 117 } 118 119 @Override 120 public void onFinish() { 121 mWrappedInstance.onFinish(); 122 // Intercept the finish call to know when printing is done 123 // and destroy the WebView as it is expensive to keep around. 124 mWebView.destroy(); 125 mWebView = null; 126 } 127 }; 128 129 // Pass in the ViewView's document adapter. 130 printManager.print("MotoGP stats", adapter, null); 131 } 132 } 133