Search in sources :

Example 36 with PageRange

use of android.print.PageRange in project android_frameworks_base by ResurrectionRemix.

the class PageRangeUtils method readRange.

/**
     * Read a page range character from a string at a certain position.
     *
     * @param s             The string to parse
     * @param pos           The starting position
     * @param maxPageNumber The highest page number to accept.
     *
     * @return The position after the page range + the page range read or null if the page range was
     *         not found
     */
private static Pair<Integer, PageRange> readRange(CharSequence s, int pos, int maxPageNumber) {
    Pair<Integer, Integer> retInt;
    Pair<Integer, Character> retChar;
    Character comma;
    if (pos == 0) {
        // When we reading the first range, we do not want to have a comma
        comma = ',';
    } else {
        retChar = readChar(s, pos, ',');
        pos = retChar.first;
        comma = retChar.second;
    }
    pos = readWhiteSpace(s, pos);
    retInt = readNumber(s, pos);
    pos = retInt.first;
    Integer start = retInt.second;
    pos = readWhiteSpace(s, pos);
    retChar = readChar(s, pos, '-');
    pos = retChar.first;
    Character separator = retChar.second;
    pos = readWhiteSpace(s, pos);
    retInt = readNumber(s, pos);
    pos = retInt.first;
    Integer end = retInt.second;
    pos = readWhiteSpace(s, pos);
    if (comma != null && // range, maybe unbounded
    ((separator != null && (start != null || end != null)) || // single page
    (separator == null && start != null && end == null))) {
        if (start == null) {
            start = 1;
        }
        if (end == null) {
            if (separator == null) {
                end = start;
            } else {
                end = maxPageNumber;
            }
        }
        if (start <= end && start >= 1 && end <= maxPageNumber) {
            return new Pair<>(pos, new PageRange(start - 1, end - 1));
        }
    }
    return new Pair<>(pos, null);
}
Also used : Pair(android.util.Pair) PageRange(android.print.PageRange)

Example 37 with PageRange

use of android.print.PageRange in project android_frameworks_base by ResurrectionRemix.

the class PageRangeUtils method contains.

/**
     * Checks whether one page range array contains another one.
     *
     * @param ourRanges The container page ranges.
     * @param otherRanges The contained page ranges.
     * @param pageCount The total number of pages.
     * @return Whether the container page ranges contains the contained ones.
     */
public static boolean contains(PageRange[] ourRanges, PageRange[] otherRanges, int pageCount) {
    if (ourRanges == null || otherRanges == null) {
        return false;
    }
    if (Arrays.equals(ourRanges, ALL_PAGES_RANGE)) {
        return true;
    }
    if (Arrays.equals(otherRanges, ALL_PAGES_RANGE)) {
        otherRanges[0] = new PageRange(0, pageCount - 1);
    }
    ourRanges = normalize(ourRanges);
    otherRanges = normalize(otherRanges);
    // Note that the code below relies on the ranges being normalized
    // which is they contain monotonically increasing non-intersecting
    // sub-ranges whose start is less that or equal to the end.
    int otherRangeIdx = 0;
    final int ourRangeCount = ourRanges.length;
    final int otherRangeCount = otherRanges.length;
    for (int ourRangeIdx = 0; ourRangeIdx < ourRangeCount; ourRangeIdx++) {
        PageRange ourRange = ourRanges[ourRangeIdx];
        for (; otherRangeIdx < otherRangeCount; otherRangeIdx++) {
            PageRange otherRange = otherRanges[otherRangeIdx];
            if (otherRange.getStart() > ourRange.getEnd()) {
                break;
            }
            if (otherRange.getStart() < ourRange.getStart() || otherRange.getEnd() > ourRange.getEnd()) {
                return false;
            }
        }
    }
    return (otherRangeIdx >= otherRangeCount);
}
Also used : PageRange(android.print.PageRange)

Example 38 with PageRange

use of android.print.PageRange in project platform_frameworks_base by android.

the class PageAdapter method computeRequestedPages.

private PageRange[] computeRequestedPages(int pageInDocument) {
    if (mRequestedPages != null && PageRangeUtils.contains(mRequestedPages, pageInDocument)) {
        return mRequestedPages;
    }
    List<PageRange> pageRangesList = new ArrayList<>();
    int remainingPagesToRequest = MAX_PREVIEW_PAGES_BATCH;
    final int selectedPagesCount = mSelectedPages.length;
    // We always request the pages that are bound, i.e. shown on screen.
    PageRange[] boundPagesInDocument = computeBoundPagesInDocument();
    final int boundRangeCount = boundPagesInDocument.length;
    for (int i = 0; i < boundRangeCount; i++) {
        PageRange boundRange = boundPagesInDocument[i];
        pageRangesList.add(boundRange);
    }
    remainingPagesToRequest -= PageRangeUtils.getNormalizedPageCount(boundPagesInDocument, mDocumentPageCount);
    final boolean requestFromStart = mRequestedPages == null || pageInDocument > mRequestedPages[mRequestedPages.length - 1].getEnd();
    if (!requestFromStart) {
        if (DEBUG) {
            Log.i(LOG_TAG, "Requesting from end");
        }
        // Reminder that ranges are always normalized.
        for (int i = selectedPagesCount - 1; i >= 0; i--) {
            if (remainingPagesToRequest <= 0) {
                break;
            }
            PageRange selectedRange = PageRangeUtils.asAbsoluteRange(mSelectedPages[i], mDocumentPageCount);
            if (pageInDocument < selectedRange.getStart()) {
                continue;
            }
            PageRange pagesInRange;
            int rangeSpan;
            if (selectedRange.contains(pageInDocument)) {
                rangeSpan = pageInDocument - selectedRange.getStart() + 1;
                rangeSpan = Math.min(rangeSpan, remainingPagesToRequest);
                final int fromPage = Math.max(pageInDocument - rangeSpan - 1, 0);
                rangeSpan = Math.max(rangeSpan, 0);
                pagesInRange = new PageRange(fromPage, pageInDocument);
            } else {
                rangeSpan = selectedRange.getSize();
                rangeSpan = Math.min(rangeSpan, remainingPagesToRequest);
                rangeSpan = Math.max(rangeSpan, 0);
                final int fromPage = Math.max(selectedRange.getEnd() - rangeSpan - 1, 0);
                final int toPage = selectedRange.getEnd();
                pagesInRange = new PageRange(fromPage, toPage);
            }
            pageRangesList.add(pagesInRange);
            remainingPagesToRequest -= rangeSpan;
        }
    } else {
        if (DEBUG) {
            Log.i(LOG_TAG, "Requesting from start");
        }
        // Reminder that ranges are always normalized.
        for (int i = 0; i < selectedPagesCount; i++) {
            if (remainingPagesToRequest <= 0) {
                break;
            }
            PageRange selectedRange = PageRangeUtils.asAbsoluteRange(mSelectedPages[i], mDocumentPageCount);
            if (pageInDocument > selectedRange.getEnd()) {
                continue;
            }
            PageRange pagesInRange;
            int rangeSpan;
            if (selectedRange.contains(pageInDocument)) {
                rangeSpan = selectedRange.getEnd() - pageInDocument + 1;
                rangeSpan = Math.min(rangeSpan, remainingPagesToRequest);
                final int toPage = Math.min(pageInDocument + rangeSpan - 1, mDocumentPageCount - 1);
                pagesInRange = new PageRange(pageInDocument, toPage);
            } else {
                rangeSpan = selectedRange.getSize();
                rangeSpan = Math.min(rangeSpan, remainingPagesToRequest);
                final int fromPage = selectedRange.getStart();
                final int toPage = Math.min(selectedRange.getStart() + rangeSpan - 1, mDocumentPageCount - 1);
                pagesInRange = new PageRange(fromPage, toPage);
            }
            if (DEBUG) {
                Log.i(LOG_TAG, "computeRequestedPages() Adding range:" + pagesInRange);
            }
            pageRangesList.add(pagesInRange);
            remainingPagesToRequest -= rangeSpan;
        }
    }
    PageRange[] pageRanges = new PageRange[pageRangesList.size()];
    pageRangesList.toArray(pageRanges);
    return PageRangeUtils.normalize(pageRanges);
}
Also used : ArrayList(java.util.ArrayList) PageRange(android.print.PageRange)

Example 39 with PageRange

use of android.print.PageRange in project platform_frameworks_base by android.

the class PageRangeUtils method getNormalizedPageCount.

/**
     * Gets the number of pages in a normalized range array.
     *
     * @param pageRanges Normalized page ranges.
     * @param layoutPageCount Page count after reported after layout pass.
     * @return The page count in the ranges.
     */
public static int getNormalizedPageCount(PageRange[] pageRanges, int layoutPageCount) {
    int pageCount = 0;
    if (pageRanges != null) {
        final int pageRangeCount = pageRanges.length;
        for (int i = 0; i < pageRangeCount; i++) {
            PageRange pageRange = pageRanges[i];
            if (PageRange.ALL_PAGES.equals(pageRange)) {
                return layoutPageCount;
            }
            pageCount += pageRange.getSize();
        }
    }
    return pageCount;
}
Also used : PageRange(android.print.PageRange)

Example 40 with PageRange

use of android.print.PageRange in project platform_frameworks_base by android.

the class PageRangeUtils method readRange.

/**
     * Read a page range character from a string at a certain position.
     *
     * @param s             The string to parse
     * @param pos           The starting position
     * @param maxPageNumber The highest page number to accept.
     *
     * @return The position after the page range + the page range read or null if the page range was
     *         not found
     */
private static Pair<Integer, PageRange> readRange(CharSequence s, int pos, int maxPageNumber) {
    Pair<Integer, Integer> retInt;
    Pair<Integer, Character> retChar;
    Character comma;
    if (pos == 0) {
        // When we reading the first range, we do not want to have a comma
        comma = ',';
    } else {
        retChar = readChar(s, pos, ',');
        pos = retChar.first;
        comma = retChar.second;
    }
    pos = readWhiteSpace(s, pos);
    retInt = readNumber(s, pos);
    pos = retInt.first;
    Integer start = retInt.second;
    pos = readWhiteSpace(s, pos);
    retChar = readChar(s, pos, '-');
    pos = retChar.first;
    Character separator = retChar.second;
    pos = readWhiteSpace(s, pos);
    retInt = readNumber(s, pos);
    pos = retInt.first;
    Integer end = retInt.second;
    pos = readWhiteSpace(s, pos);
    if (comma != null && // range, maybe unbounded
    ((separator != null && (start != null || end != null)) || // single page
    (separator == null && start != null && end == null))) {
        if (start == null) {
            start = 1;
        }
        if (end == null) {
            if (separator == null) {
                end = start;
            } else {
                end = maxPageNumber;
            }
        }
        if (start <= end && start >= 1 && end <= maxPageNumber) {
            return new Pair<>(pos, new PageRange(start - 1, end - 1));
        }
    }
    return new Pair<>(pos, null);
}
Also used : Pair(android.util.Pair) PageRange(android.print.PageRange)

Aggregations

PageRange (android.print.PageRange)60 ArrayList (java.util.ArrayList)20 PrintAttributes (android.print.PrintAttributes)5 MediaSize (android.print.PrintAttributes.MediaSize)5 Resolution (android.print.PrintAttributes.Resolution)5 PrintDocumentInfo (android.print.PrintDocumentInfo)5 PrintJobInfo (android.print.PrintJobInfo)5 PrinterCapabilitiesInfo (android.print.PrinterCapabilitiesInfo)5 Pair (android.util.Pair)5 RemotePrintDocumentInfo (com.android.printspooler.model.RemotePrintDocument.RemotePrintDocumentInfo)5