Search in sources :

Example 41 with PageRange

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

the class PrintActivity method onAdvancedPrintOptionsActivityResult.

private void onAdvancedPrintOptionsActivityResult(int resultCode, Intent data) {
    if (resultCode != RESULT_OK || data == null) {
        return;
    }
    PrintJobInfo printJobInfo = data.getParcelableExtra(PrintService.EXTRA_PRINT_JOB_INFO);
    if (printJobInfo == null) {
        return;
    }
    // Take the advanced options without interpretation.
    mPrintJob.setAdvancedOptions(printJobInfo.getAdvancedOptions());
    if (printJobInfo.getCopies() < 1) {
        Log.w(LOG_TAG, "Cannot apply return value from advanced options activity. Copies " + "must be 1 or more. Actual value is: " + printJobInfo.getCopies() + ". " + "Ignoring.");
    } else {
        mCopiesEditText.setText(String.valueOf(printJobInfo.getCopies()));
        mPrintJob.setCopies(printJobInfo.getCopies());
    }
    PrintAttributes currAttributes = mPrintJob.getAttributes();
    PrintAttributes newAttributes = printJobInfo.getAttributes();
    if (newAttributes != null) {
        // Take the media size only if the current printer supports is.
        MediaSize oldMediaSize = currAttributes.getMediaSize();
        MediaSize newMediaSize = newAttributes.getMediaSize();
        if (newMediaSize != null && !oldMediaSize.equals(newMediaSize)) {
            final int mediaSizeCount = mMediaSizeSpinnerAdapter.getCount();
            MediaSize newMediaSizePortrait = newAttributes.getMediaSize().asPortrait();
            for (int i = 0; i < mediaSizeCount; i++) {
                MediaSize supportedSizePortrait = mMediaSizeSpinnerAdapter.getItem(i).value.asPortrait();
                if (supportedSizePortrait.equals(newMediaSizePortrait)) {
                    currAttributes.setMediaSize(newMediaSize);
                    mMediaSizeSpinner.setSelection(i);
                    if (currAttributes.getMediaSize().isPortrait()) {
                        if (mOrientationSpinner.getSelectedItemPosition() != 0) {
                            mOrientationSpinner.setSelection(0);
                        }
                    } else {
                        if (mOrientationSpinner.getSelectedItemPosition() != 1) {
                            mOrientationSpinner.setSelection(1);
                        }
                    }
                    break;
                }
            }
        }
        // Take the resolution only if the current printer supports is.
        Resolution oldResolution = currAttributes.getResolution();
        Resolution newResolution = newAttributes.getResolution();
        if (!oldResolution.equals(newResolution)) {
            PrinterCapabilitiesInfo capabilities = mCurrentPrinter.getCapabilities();
            if (capabilities != null) {
                List<Resolution> resolutions = capabilities.getResolutions();
                final int resolutionCount = resolutions.size();
                for (int i = 0; i < resolutionCount; i++) {
                    Resolution resolution = resolutions.get(i);
                    if (resolution.equals(newResolution)) {
                        currAttributes.setResolution(resolution);
                        break;
                    }
                }
            }
        }
        // Take the color mode only if the current printer supports it.
        final int currColorMode = currAttributes.getColorMode();
        final int newColorMode = newAttributes.getColorMode();
        if (currColorMode != newColorMode) {
            final int colorModeCount = mColorModeSpinner.getCount();
            for (int i = 0; i < colorModeCount; i++) {
                final int supportedColorMode = mColorModeSpinnerAdapter.getItem(i).value;
                if (supportedColorMode == newColorMode) {
                    currAttributes.setColorMode(newColorMode);
                    mColorModeSpinner.setSelection(i);
                    break;
                }
            }
        }
        // Take the duplex mode only if the current printer supports it.
        final int currDuplexMode = currAttributes.getDuplexMode();
        final int newDuplexMode = newAttributes.getDuplexMode();
        if (currDuplexMode != newDuplexMode) {
            final int duplexModeCount = mDuplexModeSpinner.getCount();
            for (int i = 0; i < duplexModeCount; i++) {
                final int supportedDuplexMode = mDuplexModeSpinnerAdapter.getItem(i).value;
                if (supportedDuplexMode == newDuplexMode) {
                    currAttributes.setDuplexMode(newDuplexMode);
                    mDuplexModeSpinner.setSelection(i);
                    break;
                }
            }
        }
    }
    // Handle selected page changes making sure they are in the doc.
    PrintDocumentInfo info = mPrintedDocument.getDocumentInfo().info;
    final int pageCount = (info != null) ? getAdjustedPageCount(info) : 0;
    PageRange[] pageRanges = printJobInfo.getPages();
    if (pageRanges != null && pageCount > 0) {
        pageRanges = PageRangeUtils.normalize(pageRanges);
        List<PageRange> validatedList = new ArrayList<>();
        final int rangeCount = pageRanges.length;
        for (int i = 0; i < rangeCount; i++) {
            PageRange pageRange = pageRanges[i];
            if (pageRange.getEnd() >= pageCount) {
                final int rangeStart = pageRange.getStart();
                final int rangeEnd = pageCount - 1;
                if (rangeStart <= rangeEnd) {
                    pageRange = new PageRange(rangeStart, rangeEnd);
                    validatedList.add(pageRange);
                }
                break;
            }
            validatedList.add(pageRange);
        }
        if (!validatedList.isEmpty()) {
            PageRange[] validatedArray = new PageRange[validatedList.size()];
            validatedList.toArray(validatedArray);
            updateSelectedPages(validatedArray, pageCount);
        }
    }
    // Update the content if needed.
    if (canUpdateDocument()) {
        updateDocument(false);
    }
}
Also used : PrinterCapabilitiesInfo(android.print.PrinterCapabilitiesInfo) MediaSize(android.print.PrintAttributes.MediaSize) PrintJobInfo(android.print.PrintJobInfo) ArrayList(java.util.ArrayList) PrintAttributes(android.print.PrintAttributes) RemotePrintDocumentInfo(com.android.printspooler.model.RemotePrintDocument.RemotePrintDocumentInfo) PrintDocumentInfo(android.print.PrintDocumentInfo) Resolution(android.print.PrintAttributes.Resolution) PageRange(android.print.PageRange)

Example 42 with PageRange

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

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

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

the class PageRangeUtils method offset.

/**
     * Offsets a the start and end of page ranges with the given value.
     *
     * @param pageRanges The page ranges to offset.
     * @param offset The offset value.
     */
public static void offset(PageRange[] pageRanges, int offset) {
    if (offset == 0) {
        return;
    }
    final int pageRangeCount = pageRanges.length;
    for (int i = 0; i < pageRangeCount; i++) {
        final int start = pageRanges[i].getStart() + offset;
        final int end = pageRanges[i].getEnd() + offset;
        pageRanges[i] = new PageRange(start, end);
    }
}
Also used : PageRange(android.print.PageRange)

Example 44 with PageRange

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

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

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

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)

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