Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(81)

Side by Side Diff: printing/android/java/src/org/chromium/printing/PrintingControllerImpl.java

Issue 740983002: Implement window.print() on Android (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Adressed mounir's comments Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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.printing; 5 package org.chromium.printing;
6 6
7 import android.os.Bundle; 7 import android.os.Bundle;
8 import android.os.CancellationSignal; 8 import android.os.CancellationSignal;
9 import android.os.ParcelFileDescriptor; 9 import android.os.ParcelFileDescriptor;
10 import android.print.PageRange; 10 import android.print.PageRange;
11 import android.print.PrintAttributes; 11 import android.print.PrintAttributes;
12 import android.print.PrintDocumentInfo; 12 import android.print.PrintDocumentInfo;
13 import android.util.Log;
13 14
14 import org.chromium.base.ThreadUtils; 15 import org.chromium.base.ThreadUtils;
15 import org.chromium.printing.PrintDocumentAdapterWrapper.PdfGenerator; 16 import org.chromium.printing.PrintDocumentAdapterWrapper.PdfGenerator;
16 17
17 import java.io.IOException; 18 import java.io.IOException;
18 import java.util.ArrayList; 19 import java.util.ArrayList;
19 import java.util.Iterator; 20 import java.util.Iterator;
20 21
21 /** 22 /**
22 * Controls the interactions with Android framework related to printing. 23 * Controls the interactions with Android framework related to printing.
(...skipping 17 matching lines...) Expand all
40 /** Printing dialog has been dismissed and cleanup has been done. */ 41 /** Printing dialog has been dismissed and cleanup has been done. */
41 private static final int PRINTING_STATE_FINISHED = 3; 42 private static final int PRINTING_STATE_FINISHED = 3;
42 43
43 /** The singleton instance for this class. */ 44 /** The singleton instance for this class. */
44 private static PrintingController sInstance; 45 private static PrintingController sInstance;
45 46
46 private final String mErrorMessage; 47 private final String mErrorMessage;
47 48
48 private PrintingContextInterface mPrintingContext; 49 private PrintingContextInterface mPrintingContext;
49 50
51 /**
52 * The context of a query initiated by window.print(), stored here to allow syncrhonization
53 * with javascript.
54 */
55 private PrintingContextInterface mContextFromScriptInitiation;
56
50 /** The file descriptor into which the PDF will be written. Provided by the framework. */ 57 /** The file descriptor into which the PDF will be written. Provided by the framework. */
51 private int mFileDescriptor; 58 private int mFileDescriptor;
52 59
53 /** Dots per inch, as provided by the framework. */ 60 /** Dots per inch, as provided by the framework. */
54 private int mDpi; 61 private int mDpi;
55 62
56 /** Paper dimensions. */ 63 /** Paper dimensions. */
57 private PrintAttributes.MediaSize mMediaSize; 64 private PrintAttributes.MediaSize mMediaSize;
58 65
59 /** Numbers of pages to be printed, zero indexed. */ 66 /** Numbers of pages to be printed, zero indexed. */
(...skipping 19 matching lines...) Expand all
79 private int mPrintingState = PRINTING_STATE_READY; 86 private int mPrintingState = PRINTING_STATE_READY;
80 87
81 /** Whether layouting parameters have been changed to require a new PDF gene ration. */ 88 /** Whether layouting parameters have been changed to require a new PDF gene ration. */
82 private boolean mNeedNewPdf = false; 89 private boolean mNeedNewPdf = false;
83 90
84 /** Total number of pages to print with initial print dialog settings. */ 91 /** Total number of pages to print with initial print dialog settings. */
85 private int mLastKnownMaxPages = PrintDocumentInfo.PAGE_COUNT_UNKNOWN; 92 private int mLastKnownMaxPages = PrintDocumentInfo.PAGE_COUNT_UNKNOWN;
86 93
87 private boolean mIsBusy = false; 94 private boolean mIsBusy = false;
88 95
96 private PrintManagerDelegate mPrintManager;
97
89 private PrintingControllerImpl(PrintDocumentAdapterWrapper printDocumentAdap terWrapper, 98 private PrintingControllerImpl(PrintDocumentAdapterWrapper printDocumentAdap terWrapper,
90 String errorText) { 99 String errorText) {
91 mErrorMessage = errorText; 100 mErrorMessage = errorText;
92 mPrintDocumentAdapterWrapper = printDocumentAdapterWrapper; 101 mPrintDocumentAdapterWrapper = printDocumentAdapterWrapper;
93 mPrintDocumentAdapterWrapper.setPdfGenerator(this); 102 mPrintDocumentAdapterWrapper.setPdfGenerator(this);
94 } 103 }
95 104
96 /** 105 /**
97 * Creates a controller for handling printing with the framework. 106 * Creates a controller for handling printing with the framework.
98 * 107 *
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 public boolean isBusy() { 170 public boolean isBusy() {
162 return mIsBusy; 171 return mIsBusy;
163 } 172 }
164 173
165 @Override 174 @Override
166 public void setPrintingContext(final PrintingContextInterface printingContex t) { 175 public void setPrintingContext(final PrintingContextInterface printingContex t) {
167 mPrintingContext = printingContext; 176 mPrintingContext = printingContext;
168 } 177 }
169 178
170 @Override 179 @Override
180 public void setPendingPrint(final Printable printable, PrintManagerDelegate printManager) {
181 if (mIsBusy) return;
182 mPrintable = printable;
183 mPrintManager = printManager;
184 }
185
186 @Override
187 public void startPendingPrint(PrintingContextInterface printingContext) {
188 if (mIsBusy || mPrintManager == null) {
189 Log.w(LOG_TAG, "Pending print can't be started. Is might be busy or not initialized.");
190 if (printingContext != null) printingContext.showSystemDialogDone();
191 return;
192 }
193 mContextFromScriptInitiation = printingContext;
194 mIsBusy = true;
195 mPrintDocumentAdapterWrapper.print(mPrintManager, mPrintable.getTitle()) ;
196 mPrintManager = null;
197 }
198
199 @Override
171 public void startPrint(final Printable printable, PrintManagerDelegate print Manager) { 200 public void startPrint(final Printable printable, PrintManagerDelegate print Manager) {
172 if (mIsBusy) return; 201 if (mIsBusy) return;
173 mIsBusy = true; 202 setPendingPrint(printable, printManager);
174 mPrintable = printable; 203 startPendingPrint(null);
175 mPrintDocumentAdapterWrapper.print(printManager, printable.getTitle());
176 } 204 }
177 205
178 @Override 206 @Override
179 public void pdfWritingDone(boolean success) { 207 public void pdfWritingDone(boolean success) {
180 if (mPrintingState == PRINTING_STATE_FINISHED) return; 208 if (mPrintingState == PRINTING_STATE_FINISHED) return;
181 mPrintingState = PRINTING_STATE_READY; 209 mPrintingState = PRINTING_STATE_READY;
182 if (success) { 210 if (success) {
183 PageRange[] pageRanges = convertIntegerArrayToPageRanges(mPages); 211 PageRange[] pageRanges = convertIntegerArrayToPageRanges(mPages);
184 mOnWriteCallback.onWriteFinished(pageRanges); 212 mOnWriteCallback.onWriteFinished(pageRanges);
185 } else { 213 } else {
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
314 // extra). If we complete the PDF generation successfully from o nLayout or onWrite, 342 // extra). If we complete the PDF generation successfully from o nLayout or onWrite,
315 // we already make the state PRINTING_STATE_READY and call askUs erForSettingsReply 343 // we already make the state PRINTING_STATE_READY and call askUs erForSettingsReply
316 // inside pdfWritingDone, thus not entering here. Also, if we g et an extra 344 // inside pdfWritingDone, thus not entering here. Also, if we g et an extra
317 // AskUserForSettings call, it's handled inside {@link 345 // AskUserForSettings call, it's handled inside {@link
318 // PrintingContext#pageCountEstimationDone}. 346 // PrintingContext#pageCountEstimationDone}.
319 mPrintingContext.askUserForSettingsReply(false); 347 mPrintingContext.askUserForSettingsReply(false);
320 } 348 }
321 mPrintingContext.updatePrintingContextMap(mFileDescriptor, true); 349 mPrintingContext.updatePrintingContextMap(mFileDescriptor, true);
322 mPrintingContext = null; 350 mPrintingContext = null;
323 } 351 }
352
353 if (mContextFromScriptInitiation != null) {
354 mContextFromScriptInitiation.showSystemDialogDone();
355 mContextFromScriptInitiation = null;
356 }
357
324 mPrintingState = PRINTING_STATE_FINISHED; 358 mPrintingState = PRINTING_STATE_FINISHED;
325 359
326 closeFileDescriptor(mFileDescriptor); 360 closeFileDescriptor(mFileDescriptor);
327 mFileDescriptor = -1; 361 mFileDescriptor = -1;
328 362
329 resetCallbacks(); 363 resetCallbacks();
330 // The printmanager contract is that onFinish() is always called as the last 364 // The printmanager contract is that onFinish() is always called as the last
331 // callback. We set busy to false here. 365 // callback. We set busy to false here.
332 mIsBusy = false; 366 mIsBusy = false;
333 } 367 }
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
378 412
379 // Convert the list into array. 413 // Convert the list into array.
380 int[] ret = new int[pages.size()]; 414 int[] ret = new int[pages.size()];
381 Iterator<Integer> iterator = pages.iterator(); 415 Iterator<Integer> iterator = pages.iterator();
382 for (int i = 0; i < ret.length; i++) { 416 for (int i = 0; i < ret.length; i++) {
383 ret[i] = iterator.next().intValue(); 417 ret[i] = iterator.next().intValue();
384 } 418 }
385 return ret; 419 return ret;
386 } 420 }
387 } 421 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698