| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.android_webview; | 5 package org.chromium.android_webview; |
| 6 | 6 |
| 7 import android.annotation.SuppressLint; | 7 import android.annotation.SuppressLint; |
| 8 import android.os.Bundle; | 8 import android.os.Bundle; |
| 9 import android.os.CancellationSignal; | 9 import android.os.CancellationSignal; |
| 10 import android.os.ParcelFileDescriptor; | 10 import android.os.ParcelFileDescriptor; |
| 11 import android.print.PageRange; | 11 import android.print.PageRange; |
| 12 import android.print.PrintAttributes; | 12 import android.print.PrintAttributes; |
| 13 import android.print.PrintDocumentAdapter; | 13 import android.print.PrintDocumentAdapter; |
| 14 import android.print.PrintDocumentInfo; | 14 import android.print.PrintDocumentInfo; |
| 15 import android.webkit.ValueCallback; | 15 import android.webkit.ValueCallback; |
| 16 | 16 |
| 17 import java.util.ArrayList; |
| 17 | 18 |
| 18 /** | 19 /** |
| 19 * Adapter for printing Webview. This class implements the abstract | 20 * Adapter for printing Webview. This class implements the abstract |
| 20 * system class PrintDocumentAdapter and hides all printing details from | 21 * system class PrintDocumentAdapter and hides all printing details from |
| 21 * the developer. | 22 * the developer. |
| 22 */ | 23 */ |
| 23 @SuppressLint("NewApi") // Printing requires API level 19. | 24 @SuppressLint("NewApi") // Printing requires API level 19. |
| 24 public class AwPrintDocumentAdapter extends PrintDocumentAdapter { | 25 public class AwPrintDocumentAdapter extends PrintDocumentAdapter { |
| 25 | 26 |
| 26 private AwPdfExporter mPdfExporter; | 27 private AwPdfExporter mPdfExporter; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 58 .Builder(mDocumentName) | 59 .Builder(mDocumentName) |
| 59 .build(); | 60 .build(); |
| 60 // TODO(sgurun) once componentization is done, do layout changes and | 61 // TODO(sgurun) once componentization is done, do layout changes and |
| 61 // generate PDF here, set the page range information to documentinfo | 62 // generate PDF here, set the page range information to documentinfo |
| 62 // and call onLayoutFinished with true/false depending on whether | 63 // and call onLayoutFinished with true/false depending on whether |
| 63 // layout actually changed. | 64 // layout actually changed. |
| 64 callback.onLayoutFinished(documentInfo, true); | 65 callback.onLayoutFinished(documentInfo, true); |
| 65 } | 66 } |
| 66 | 67 |
| 67 @Override | 68 @Override |
| 68 public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, | 69 public void onWrite(final PageRange[] pages, ParcelFileDescriptor destinatio
n, |
| 69 CancellationSignal cancellationSignal, final WriteResultCallback cal
lback) { | 70 CancellationSignal cancellationSignal, final WriteResultCallback cal
lback) { |
| 70 mPdfExporter.exportToPdf(destination, mAttributes, new ValueCallback<Boo
lean>() { | 71 mPdfExporter.exportToPdf( |
| 71 @Override | 72 destination, mAttributes, normalizeRanges(pages), new ValueCallb
ack<Boolean>() { |
| 72 public void onReceiveValue(Boolean value) { | 73 @Override |
| 73 if (value) { | 74 public void onReceiveValue(Boolean value) { |
| 74 callback.onWriteFinished(new PageRange[] { PageRange.ALL_PAG
ES }); | 75 if (value) { |
| 75 } else { | 76 callback.onWriteFinished(pages); |
| 76 // TODO(sgurun) provide a localized error message | 77 } else { |
| 77 callback.onWriteFailed(null); | 78 // TODO(sgurun) provide a localized error message |
| 78 } | 79 callback.onWriteFailed(null); |
| 80 } |
| 81 } |
| 82 }, cancellationSignal); |
| 83 } |
| 84 |
| 85 private int[] normalizeRanges(final PageRange[] ranges) { |
| 86 if (ranges.length == 1 && PageRange.ALL_PAGES.equals(ranges[0])) { |
| 87 return new int[0]; |
| 88 } |
| 89 ArrayList<Integer> pages = new ArrayList<Integer>(); |
| 90 for (PageRange range : ranges) { |
| 91 for (int i = range.getStart(); i <= range.getEnd(); ++i) { |
| 92 pages.add(i); |
| 79 } | 93 } |
| 80 }, cancellationSignal); | 94 } |
| 95 |
| 96 int[] ret = new int[pages.size()]; |
| 97 for (int i = 0; i < pages.size(); ++i) { |
| 98 ret[i] = pages.get(i).intValue(); |
| 99 } |
| 100 return ret; |
| 81 } | 101 } |
| 82 } | 102 } |
| 83 | 103 |
| OLD | NEW |