• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.android_webview;
6 
7 import android.os.Bundle;
8 import android.os.CancellationSignal;
9 import android.os.ParcelFileDescriptor;
10 import android.print.PageRange;
11 import android.print.PrintAttributes;
12 import android.print.PrintDocumentAdapter;
13 import android.print.PrintDocumentInfo;
14 import android.webkit.ValueCallback;
15 
16 
17 /**
18  * Adapter for printing Webview. This class implements the abstract
19  * system class PrintDocumentAdapter and hides all printing details from
20  * the developer.
21  */
22 public class AwPrintDocumentAdapter extends PrintDocumentAdapter {
23 
24     private AwPdfExporter mPdfExporter;
25     private PrintAttributes mAttributes;
26     private String mDocumentName;
27 
28     /**
29      * Constructor.
30      * TODO(sgurun) remove in favor of constructor below once the AOSP changes are in.
31      *
32      * @param pdfExporter The PDF exporter to export the webview contents to a PDF file.
33      */
AwPrintDocumentAdapter(AwPdfExporter pdfExporter)34     public AwPrintDocumentAdapter(AwPdfExporter pdfExporter) {
35         this(pdfExporter, "default");
36     }
37 
38     /**
39      * Constructor.
40      *
41      * @param pdfExporter The PDF exporter to export the webview contents to a PDF file.
42      * @param documentName  The name of the pdf document.
43      */
AwPrintDocumentAdapter(AwPdfExporter pdfExporter, String documentName)44     public AwPrintDocumentAdapter(AwPdfExporter pdfExporter, String documentName) {
45         mPdfExporter = pdfExporter;
46         mDocumentName = documentName;
47     }
48 
49 
50     @Override
onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle metadata)51     public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes,
52             CancellationSignal cancellationSignal, LayoutResultCallback callback,
53             Bundle metadata) {
54         mAttributes = newAttributes;
55         PrintDocumentInfo documentInfo = new PrintDocumentInfo
56                 .Builder(mDocumentName)
57                 .build();
58         // TODO(sgurun) once componentization is done, do layout changes and
59         // generate PDF here, set the page range information to documentinfo
60         // and call onLayoutFinished with true/false depending on whether
61         // layout actually changed.
62         callback.onLayoutFinished(documentInfo, true);
63     }
64 
65     @Override
onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, final WriteResultCallback callback)66     public void onWrite(PageRange[] pages, ParcelFileDescriptor destination,
67             CancellationSignal cancellationSignal, final WriteResultCallback callback) {
68         mPdfExporter.exportToPdf(destination, mAttributes, new ValueCallback<Boolean>() {
69             @Override
70             public void onReceiveValue(Boolean value) {
71                 if (value) {
72                     callback.onWriteFinished(new PageRange[] { PageRange.ALL_PAGES });
73                 } else {
74                     // TODO(sgurun) provide a localized error message
75                     callback.onWriteFailed(null);
76                 }
77             }
78         }, cancellationSignal);
79     }
80 }
81 
82