android.print.PageRange Java Examples

The following examples show how to use android.print.PageRange. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RescueCodePrintDocumentAdapter.java    From secure-quick-reliable-login with MIT License 6 votes vote down vote up
@Override
public void onWrite(
        final PageRange[] pageRanges,
        final ParcelFileDescriptor destination,
        final CancellationSignal cancellationSignal,
        final WriteResultCallback callback
) {
    PdfDocument.Page page = mPdfDocument.startPage(0);

    if (cancellationSignal.isCanceled()) {
        callback.onWriteCancelled();
        mPdfDocument.close();
        mPdfDocument = null;
        return;
    }

    drawPage(page);

    mPdfDocument.finishPage(page);
    try {
        mPdfDocument.writeTo(new FileOutputStream(
                destination.getFileDescriptor()));
    } catch (IOException e) {
        callback.onWriteFailed(e.toString());
        return;
    } finally {
        mPdfDocument.close();
        mPdfDocument = null;
    }
    callback.onWriteFinished(pageRanges);
}
 
Example #2
Source File: PrintingControllerImpl.java    From 365browser with Apache License 2.0 6 votes vote down vote up
/**
 * Gets an array of page ranges and returns an array of integers with all ranges expanded.
 */
private static int[] normalizeRanges(final PageRange[] ranges) {
    // Expand ranges into a list of individual numbers.
    ArrayList<Integer> pages = new ArrayList<Integer>();
    for (PageRange range : ranges) {
        for (int i = range.getStart(); i <= range.getEnd(); i++) {
            pages.add(i);
        }
    }

    // Convert the list into array.
    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;
}
 
Example #3
Source File: IdentityPrintDocumentAdapter.java    From secure-quick-reliable-login with MIT License 5 votes vote down vote up
@Override
public void onWrite(
        final PageRange[] pageRanges,
        final ParcelFileDescriptor destination,
        final CancellationSignal cancellationSignal,
        final WriteResultCallback callback
) {
    PdfDocument.Page page = mPdfDocument.startPage(0);

    if (cancellationSignal.isCanceled()) {
        callback.onWriteCancelled();
        mPdfDocument.close();
        mPdfDocument = null;
        return;
    }

    drawPage(page);

    mPdfDocument.finishPage(page);
    try {
        mPdfDocument.writeTo(new FileOutputStream(
                destination.getFileDescriptor()));
    } catch (IOException e) {
        callback.onWriteFailed(e.toString());
        return;
    } finally {
        mPdfDocument.close();
        mPdfDocument = null;
    }
    callback.onWriteFinished(pageRanges);
}
 
Example #4
Source File: PrintingControllerImpl.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example #5
Source File: PrintingControllerImpl.java    From 365browser with Apache License 2.0 5 votes vote down vote up
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 {
        // null corresponds to all pages in Chromium printing logic.
        pageRanges = new PageRange[] { PageRange.ALL_PAGES };
    }
    return pageRanges;
}
 
Example #6
Source File: PrintDocumentAdapterWrapper.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@Override
public void onWrite(
        final PageRange[] ranges,
        final ParcelFileDescriptor destination,
        final CancellationSignal cancellationSignal,
        final WriteResultCallback callback) {
    mPdfGenerator.onWrite(ranges, destination, cancellationSignal,
            new WriteResultCallbackWrapperImpl(callback));
}
 
Example #7
Source File: PrintingControllerImpl.java    From 365browser with Apache License 2.0 4 votes vote down vote up
@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;
    }

    // TODO(cimamoglu): Make use of CancellationSignal.
    mOnWriteCallback = callback;

    mFileDescriptor = destination.getFd();
    // Update file descriptor to PrintingContext mapping in the owner class.
    mPrintingContext.updatePrintingContextMap(mFileDescriptor, false);

    // We need to convert ranges list into an array of individual numbers for
    // easier JNI passing and compatibility with the native side.
    if (ranges.length == 1 && ranges[0].equals(PageRange.ALL_PAGES)) {
        // null corresponds to all pages in Chromium printing logic.
        mPages = null;
    } else {
        mPages = normalizeRanges(ranges);
    }

    if (mPrintingState == PRINTING_STATE_READY) {
        // If this onWrite is without a preceding onLayout, start Chromium PDF generation here.
        if (mPrintable.print()) {
            mPrintingState = PRINTING_STATE_STARTED_FROM_ONWRITE;
        } else {
            callback.onWriteFailed(mErrorMessage);
            resetCallbacks();
        }
    } else if (mPrintingState == PRINTING_STATE_STARTED_FROM_ONLAYOUT) {
        // Otherwise, continue previously started operation.
        mPrintingContext.askUserForSettingsReply(true);
    }
    // We are guaranteed by the framework that we will not have two onWrite calls at once.
    // We may get a CancellationSignal, after replying it (via WriteResultCallback) we might
    // get another onWrite call.
}
 
Example #8
Source File: PrintDocumentAdapterWrapper.java    From 365browser with Apache License 2.0 4 votes vote down vote up
void onWrite(
final PageRange[] ranges,
final ParcelFileDescriptor destination,
final CancellationSignal cancellationSignal,
final PrintDocumentAdapterWrapper.WriteResultCallbackWrapper callback);
 
Example #9
Source File: PrintDocumentAdapterWrapper.java    From 365browser with Apache License 2.0 4 votes vote down vote up
@Override
public void onWriteFinished(PageRange[] pages) {
    mCallback.onWriteFinished(pages);
}
 
Example #10
Source File: TemplatePrinterActivity.java    From commcare-android with Apache License 2.0 4 votes vote down vote up
@Override
public void onWrite(PageRange[] pages, ParcelFileDescriptor destination,
                    CancellationSignal cancellationSignal,
                    WriteResultCallback callback) {
    delegate.onWrite(pages, destination, cancellationSignal, callback);
}
 
Example #11
Source File: PrintDocumentAdapterWrapper.java    From 365browser with Apache License 2.0 votes vote down vote up
void onWriteFinished(PageRange[] pages);