Search in sources :

Example 26 with PageRange

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

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 27 with PageRange

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

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 28 with PageRange

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

the class PageAdapter method setConfirmedPages.

private void setConfirmedPages(PageRange[] pagesInDocument, int documentPageCount) {
    mConfirmedPagesInDocument.clear();
    final int rangeCount = pagesInDocument.length;
    for (int i = 0; i < rangeCount; i++) {
        PageRange pageRange = PageRangeUtils.asAbsoluteRange(pagesInDocument[i], documentPageCount);
        for (int j = pageRange.getStart(); j <= pageRange.getEnd(); j++) {
            mConfirmedPagesInDocument.put(j, null);
        }
    }
}
Also used : PageRange(android.print.PageRange)

Example 29 with PageRange

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

the class PageAdapter method computeBoundPagesInDocument.

private PageRange[] computeBoundPagesInDocument() {
    List<PageRange> pagesInDocumentList = new ArrayList<>();
    int fromPage = INVALID_PAGE_INDEX;
    int toPage = INVALID_PAGE_INDEX;
    final int boundPageCount = mBoundPagesInAdapter.size();
    for (int i = 0; i < boundPageCount; i++) {
        // The container is a sparse array, so keys are sorted in ascending order.
        final int boundPageInAdapter = mBoundPagesInAdapter.keyAt(i);
        final int boundPageInDocument = computePageIndexInDocument(boundPageInAdapter);
        if (fromPage == INVALID_PAGE_INDEX) {
            fromPage = boundPageInDocument;
        }
        if (toPage == INVALID_PAGE_INDEX) {
            toPage = boundPageInDocument;
        }
        if (boundPageInDocument > toPage + 1) {
            PageRange pageRange = new PageRange(fromPage, toPage);
            pagesInDocumentList.add(pageRange);
            fromPage = toPage = boundPageInDocument;
        } else {
            toPage = boundPageInDocument;
        }
    }
    if (fromPage != INVALID_PAGE_INDEX && toPage != INVALID_PAGE_INDEX) {
        PageRange pageRange = new PageRange(fromPage, toPage);
        pagesInDocumentList.add(pageRange);
    }
    PageRange[] pageInDocument = new PageRange[pagesInDocumentList.size()];
    pagesInDocumentList.toArray(pageInDocument);
    if (DEBUG) {
        Log.i(LOG_TAG, "Bound pages: " + Arrays.toString(pageInDocument));
    }
    return pageInDocument;
}
Also used : ArrayList(java.util.ArrayList) PageRange(android.print.PageRange)

Example 30 with PageRange

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

the class PageAdapter method computeSelectedPages.

private PageRange[] computeSelectedPages() {
    ArrayList<PageRange> selectedPagesList = new ArrayList<>();
    int startPageIndex = INVALID_PAGE_INDEX;
    int endPageIndex = INVALID_PAGE_INDEX;
    final int pageCount = mConfirmedPagesInDocument.size();
    for (int i = 0; i < pageCount; i++) {
        final int pageIndex = mConfirmedPagesInDocument.keyAt(i);
        if (startPageIndex == INVALID_PAGE_INDEX) {
            startPageIndex = endPageIndex = pageIndex;
        }
        if (endPageIndex + 1 < pageIndex) {
            PageRange pageRange = new PageRange(startPageIndex, endPageIndex);
            selectedPagesList.add(pageRange);
            startPageIndex = pageIndex;
        }
        endPageIndex = pageIndex;
    }
    if (startPageIndex != INVALID_PAGE_INDEX && endPageIndex != INVALID_PAGE_INDEX) {
        PageRange pageRange = new PageRange(startPageIndex, endPageIndex);
        selectedPagesList.add(pageRange);
    }
    PageRange[] selectedPages = new PageRange[selectedPagesList.size()];
    selectedPagesList.toArray(selectedPages);
    return selectedPages;
}
Also used : ArrayList(java.util.ArrayList) 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