This source file includes following definitions.
- create
- getInstance
- hasPrintingFinished
- getDpi
- getFileDescriptor
- getPageHeight
- getPageWidth
- getPageNumbers
- isBusy
- setPrintingContext
- startPrint
- pdfWritingDone
- onStart
- onLayout
- pageCountEstimationDone
- onWrite
- onFinish
- resetCallbacks
- closeFileDescriptor
- convertIntegerArrayToPageRanges
- normalizeRanges
package org.chromium.printing;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.os.ParcelFileDescriptor;
import android.print.PageRange;
import android.print.PrintAttributes;
import android.print.PrintDocumentInfo;
import org.chromium.base.ThreadUtils;
import org.chromium.printing.PrintDocumentAdapterWrapper.PdfGenerator;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
public class PrintingControllerImpl implements PrintingController, PdfGenerator {
private static final String LOG_TAG = "PrintingControllerImpl";
private static final int PRINTING_STATE_READY = 0;
private static final int PRINTING_STATE_STARTED_FROM_ONLAYOUT = 1;
private static final int PRINTING_STATE_STARTED_FROM_ONWRITE = 2;
private static final int PRINTING_STATE_FINISHED = 3;
private static PrintingController sInstance;
private final String mErrorMessage;
private PrintingContextInterface mPrintingContext;
private int mFileDescriptor;
private int mDpi;
private PrintAttributes.MediaSize mMediaSize;
private int[] mPages;
private PrintDocumentAdapterWrapper.WriteResultCallbackWrapper mOnWriteCallback;
private PrintDocumentAdapterWrapper.LayoutResultCallbackWrapper mOnLayoutCallback;
private Printable mPrintable;
private PrintDocumentAdapterWrapper mPrintDocumentAdapterWrapper;
private int mPrintingState = PRINTING_STATE_READY;
private boolean mNeedNewPdf = false;
private int mLastKnownMaxPages = PrintDocumentInfo.PAGE_COUNT_UNKNOWN;
private boolean mIsBusy = false;
private PrintingControllerImpl(PrintDocumentAdapterWrapper printDocumentAdapterWrapper,
String errorText) {
mErrorMessage = errorText;
mPrintDocumentAdapterWrapper = printDocumentAdapterWrapper;
mPrintDocumentAdapterWrapper.setPdfGenerator(this);
}
public static PrintingController create(
PrintDocumentAdapterWrapper printDocumentAdapterWrapper, String errorText) {
ThreadUtils.assertOnUiThread();
if (sInstance == null) {
sInstance = new PrintingControllerImpl(printDocumentAdapterWrapper, errorText);
}
return sInstance;
}
public static PrintingController getInstance() {
return sInstance;
}
@Override
public boolean hasPrintingFinished() {
return mPrintingState == PRINTING_STATE_FINISHED;
}
@Override
public int getDpi() {
return mDpi;
}
@Override
public int getFileDescriptor() {
return mFileDescriptor;
}
@Override
public int getPageHeight() {
return mMediaSize.getHeightMils();
}
@Override
public int getPageWidth() {
return mMediaSize.getWidthMils();
}
@Override
public int[] getPageNumbers() {
return mPages == null ? null : mPages.clone();
}
@Override
public boolean isBusy() {
return mIsBusy;
}
@Override
public void setPrintingContext(final PrintingContextInterface printingContext) {
mPrintingContext = printingContext;
}
@Override
public void startPrint(final Printable printable, PrintManagerDelegate printManager) {
if (mIsBusy) return;
mIsBusy = true;
mPrintable = printable;
mPrintDocumentAdapterWrapper.print(printManager, printable.getTitle());
}
@Override
public void pdfWritingDone(boolean success) {
if (mPrintingState == PRINTING_STATE_FINISHED) return;
mPrintingState = PRINTING_STATE_READY;
if (success) {
PageRange[] pageRanges = convertIntegerArrayToPageRanges(mPages);
mOnWriteCallback.onWriteFinished(pageRanges);
} else {
mOnWriteCallback.onWriteFailed(mErrorMessage);
resetCallbacks();
}
closeFileDescriptor(mFileDescriptor);
mFileDescriptor = -1;
}
@Override
public void onStart() {
mPrintingState = PRINTING_STATE_READY;
}
@Override
public void onLayout(
PrintAttributes oldAttributes,
PrintAttributes newAttributes,
CancellationSignal cancellationSignal,
PrintDocumentAdapterWrapper.LayoutResultCallbackWrapper callback,
Bundle metadata) {
mDpi = newAttributes.getResolution().getHorizontalDpi();
mMediaSize = newAttributes.getMediaSize();
mNeedNewPdf = !newAttributes.equals(oldAttributes);
mOnLayoutCallback = callback;
if (mPrintingState == PRINTING_STATE_STARTED_FROM_ONLAYOUT) {
pageCountEstimationDone(mLastKnownMaxPages);
} else if (mPrintingState == PRINTING_STATE_STARTED_FROM_ONWRITE) {
callback.onLayoutFailed(mErrorMessage);
resetCallbacks();
} else if (mPrintable.print()) {
mPrintingState = PRINTING_STATE_STARTED_FROM_ONLAYOUT;
} else {
callback.onLayoutFailed(mErrorMessage);
resetCallbacks();
}
}
@Override
public void pageCountEstimationDone(final int maxPages) {
if (mPrintingState == PRINTING_STATE_FINISHED) return;
if (maxPages != PrintDocumentInfo.PAGE_COUNT_UNKNOWN) {
mLastKnownMaxPages = maxPages;
}
if (mPrintingState == PRINTING_STATE_STARTED_FROM_ONLAYOUT) {
PrintDocumentInfo info = new PrintDocumentInfo.Builder(mPrintable.getTitle())
.setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
.setPageCount(mLastKnownMaxPages)
.build();
mOnLayoutCallback.onLayoutFinished(info, mNeedNewPdf);
} else if (mPrintingState == PRINTING_STATE_STARTED_FROM_ONWRITE) {
if (mPrintingContext == null) {
mOnWriteCallback.onWriteFailed(mErrorMessage);
resetCallbacks();
return;
}
mPrintingContext.askUserForSettingsReply(true);
}
}
@Override
public void onWrite(
final PageRange[] ranges,
final ParcelFileDescriptor destination,
final CancellationSignal cancellationSignal,
final PrintDocumentAdapterWrapper.WriteResultCallbackWrapper callback) {
if (mPrintingContext == null) {
callback.onWriteFailed(mErrorMessage);
resetCallbacks();
return;
}
mOnWriteCallback = callback;
mFileDescriptor = destination.getFd();
mPrintingContext.updatePrintingContextMap(mFileDescriptor, false);
if (ranges.length == 1 && ranges[0].equals(PageRange.ALL_PAGES)) {
mPages = null;
} else {
mPages = normalizeRanges(ranges);
}
if (mPrintingState == PRINTING_STATE_READY) {
if (mPrintable.print()) {
mPrintingState = PRINTING_STATE_STARTED_FROM_ONWRITE;
} else {
callback.onWriteFailed(mErrorMessage);
resetCallbacks();
}
} else if (mPrintingState == PRINTING_STATE_STARTED_FROM_ONLAYOUT) {
mPrintingContext.askUserForSettingsReply(true);
}
}
@Override
public void onFinish() {
mLastKnownMaxPages = PrintDocumentInfo.PAGE_COUNT_UNKNOWN;
mPages = null;
if (mPrintingContext != null) {
if (mPrintingState != PRINTING_STATE_READY) {
mPrintingContext.askUserForSettingsReply(false);
}
mPrintingContext.updatePrintingContextMap(mFileDescriptor, true);
mPrintingContext = null;
}
mPrintingState = PRINTING_STATE_FINISHED;
closeFileDescriptor(mFileDescriptor);
mFileDescriptor = -1;
resetCallbacks();
mIsBusy = false;
}
private void resetCallbacks() {
mOnWriteCallback = null;
mOnLayoutCallback = null;
}
private static void closeFileDescriptor(int fd) {
if (fd != -1) return;
ParcelFileDescriptor fileDescriptor = ParcelFileDescriptor.adoptFd(fd);
if (fileDescriptor != null) {
try {
fileDescriptor.close();
} catch (IOException ioe) {
}
}
}
private static PageRange[] convertIntegerArrayToPageRanges(int[] pagesArray) {
PageRange[] pageRanges;
if (pagesArray != null) {
pageRanges = new PageRange[pagesArray.length];
for (int i = 0; i < pageRanges.length; i++) {
int page = pagesArray[i];
pageRanges[i] = new PageRange(page, page);
}
} else {
pageRanges = new PageRange[] { PageRange.ALL_PAGES };
}
return pageRanges;
}
private static int[] normalizeRanges(final PageRange[] ranges) {
ArrayList<Integer> pages = new ArrayList<Integer>();
for (PageRange range : ranges) {
for (int i = range.getStart(); i <= range.getEnd(); i++) {
pages.add(i);
}
}
int[] ret = new int[pages.size()];
Iterator<Integer> iterator = pages.iterator();
for (int i = 0; i < ret.length; i++) {
ret[i] = iterator.next().intValue();
}
return ret;
}
}