Search in sources :

Example 6 with Resolution

use of android.print.PrintAttributes.Resolution in project android_frameworks_base by crdroidandroid.

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 7 with Resolution

use of android.print.PrintAttributes.Resolution in project platform_frameworks_base by android.

the class PrintActivity method updatePrintAttributesFromCapabilities.

private void updatePrintAttributesFromCapabilities(PrinterCapabilitiesInfo capabilities) {
    boolean clearRanges = false;
    PrintAttributes defaults = capabilities.getDefaults();
    // Sort the media sizes based on the current locale.
    List<MediaSize> sortedMediaSizes = new ArrayList<>(capabilities.getMediaSizes());
    Collections.sort(sortedMediaSizes, mMediaSizeComparator);
    PrintAttributes attributes = mPrintJob.getAttributes();
    // Media size.
    MediaSize currMediaSize = attributes.getMediaSize();
    if (currMediaSize == null) {
        clearRanges = true;
        attributes.setMediaSize(defaults.getMediaSize());
    } else {
        MediaSize newMediaSize = null;
        boolean isPortrait = currMediaSize.isPortrait();
        // Try to find the current media size in the capabilities as
        // it may be in a different orientation.
        MediaSize currMediaSizePortrait = currMediaSize.asPortrait();
        final int mediaSizeCount = sortedMediaSizes.size();
        for (int i = 0; i < mediaSizeCount; i++) {
            MediaSize mediaSize = sortedMediaSizes.get(i);
            if (currMediaSizePortrait.equals(mediaSize.asPortrait())) {
                newMediaSize = mediaSize;
                break;
            }
        }
        // If we did not find the current media size fall back to default.
        if (newMediaSize == null) {
            clearRanges = true;
            newMediaSize = defaults.getMediaSize();
        }
        if (newMediaSize != null) {
            if (isPortrait) {
                attributes.setMediaSize(newMediaSize.asPortrait());
            } else {
                attributes.setMediaSize(newMediaSize.asLandscape());
            }
        }
    }
    // Color mode.
    final int colorMode = attributes.getColorMode();
    if ((capabilities.getColorModes() & colorMode) == 0) {
        attributes.setColorMode(defaults.getColorMode());
    }
    // Duplex mode.
    final int duplexMode = attributes.getDuplexMode();
    if ((capabilities.getDuplexModes() & duplexMode) == 0) {
        attributes.setDuplexMode(defaults.getDuplexMode());
    }
    // Resolution
    Resolution resolution = attributes.getResolution();
    if (resolution == null || !capabilities.getResolutions().contains(resolution)) {
        attributes.setResolution(defaults.getResolution());
    }
    // Margins.
    if (!Objects.equals(attributes.getMinMargins(), defaults.getMinMargins())) {
        clearRanges = true;
    }
    attributes.setMinMargins(defaults.getMinMargins());
    if (clearRanges) {
        clearPageRanges();
    }
}
Also used : MediaSize(android.print.PrintAttributes.MediaSize) ArrayList(java.util.ArrayList) PrintAttributes(android.print.PrintAttributes) Resolution(android.print.PrintAttributes.Resolution)

Example 8 with Resolution

use of android.print.PrintAttributes.Resolution 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 9 with Resolution

use of android.print.PrintAttributes.Resolution in project android_frameworks_base by AOSPA.

the class PrintActivity method updatePrintAttributesFromCapabilities.

private void updatePrintAttributesFromCapabilities(PrinterCapabilitiesInfo capabilities) {
    boolean clearRanges = false;
    PrintAttributes defaults = capabilities.getDefaults();
    // Sort the media sizes based on the current locale.
    List<MediaSize> sortedMediaSizes = new ArrayList<>(capabilities.getMediaSizes());
    Collections.sort(sortedMediaSizes, mMediaSizeComparator);
    PrintAttributes attributes = mPrintJob.getAttributes();
    // Media size.
    MediaSize currMediaSize = attributes.getMediaSize();
    if (currMediaSize == null) {
        clearRanges = true;
        attributes.setMediaSize(defaults.getMediaSize());
    } else {
        MediaSize newMediaSize = null;
        boolean isPortrait = currMediaSize.isPortrait();
        // Try to find the current media size in the capabilities as
        // it may be in a different orientation.
        MediaSize currMediaSizePortrait = currMediaSize.asPortrait();
        final int mediaSizeCount = sortedMediaSizes.size();
        for (int i = 0; i < mediaSizeCount; i++) {
            MediaSize mediaSize = sortedMediaSizes.get(i);
            if (currMediaSizePortrait.equals(mediaSize.asPortrait())) {
                newMediaSize = mediaSize;
                break;
            }
        }
        // If we did not find the current media size fall back to default.
        if (newMediaSize == null) {
            clearRanges = true;
            newMediaSize = defaults.getMediaSize();
        }
        if (newMediaSize != null) {
            if (isPortrait) {
                attributes.setMediaSize(newMediaSize.asPortrait());
            } else {
                attributes.setMediaSize(newMediaSize.asLandscape());
            }
        }
    }
    // Color mode.
    final int colorMode = attributes.getColorMode();
    if ((capabilities.getColorModes() & colorMode) == 0) {
        attributes.setColorMode(defaults.getColorMode());
    }
    // Duplex mode.
    final int duplexMode = attributes.getDuplexMode();
    if ((capabilities.getDuplexModes() & duplexMode) == 0) {
        attributes.setDuplexMode(defaults.getDuplexMode());
    }
    // Resolution
    Resolution resolution = attributes.getResolution();
    if (resolution == null || !capabilities.getResolutions().contains(resolution)) {
        attributes.setResolution(defaults.getResolution());
    }
    // Margins.
    if (!Objects.equals(attributes.getMinMargins(), defaults.getMinMargins())) {
        clearRanges = true;
    }
    attributes.setMinMargins(defaults.getMinMargins());
    if (clearRanges) {
        clearPageRanges();
    }
}
Also used : MediaSize(android.print.PrintAttributes.MediaSize) ArrayList(java.util.ArrayList) PrintAttributes(android.print.PrintAttributes) Resolution(android.print.PrintAttributes.Resolution)

Example 10 with Resolution

use of android.print.PrintAttributes.Resolution in project android_frameworks_base by AOSPA.

the class IPrintManagerParametersTest method createMockCallbacks.

/**
     * Create mock print service callbacks.
     *
     * @return the callbacks
     */
private PrintServiceCallbacks createMockCallbacks() {
    return createMockPrintServiceCallbacks(new Answer<PrinterDiscoverySessionCallbacks>() {

        @Override
        public PrinterDiscoverySessionCallbacks answer(InvocationOnMock invocation) {
            return createMockPrinterDiscoverySessionCallbacks(new Answer<Void>() {

                @Override
                public Void answer(InvocationOnMock invocation) {
                    // Get the session.
                    StubbablePrinterDiscoverySession session = ((PrinterDiscoverySessionCallbacks) invocation.getMock()).getSession();
                    if (session.getPrinters().isEmpty()) {
                        final String PRINTER_NAME = "good printer";
                        List<PrinterInfo> printers = new ArrayList<>();
                        // Add the printer.
                        mGoodPrinterId = session.getService().generatePrinterId(PRINTER_NAME);
                        PrinterCapabilitiesInfo capabilities = new PrinterCapabilitiesInfo.Builder(mGoodPrinterId).setMinMargins(new Margins(200, 200, 200, 200)).addMediaSize(MediaSize.ISO_A4, true).addResolution(new Resolution("300x300", "300x300", 300, 300), true).setColorModes(PrintAttributes.COLOR_MODE_COLOR, PrintAttributes.COLOR_MODE_COLOR).build();
                        PrinterInfo printer = new PrinterInfo.Builder(mGoodPrinterId, PRINTER_NAME, PrinterInfo.STATUS_IDLE).setCapabilities(capabilities).build();
                        printers.add(printer);
                        session.addPrinters(printers);
                    }
                    onPrinterDiscoverySessionStartCalled();
                    return null;
                }
            }, null, null, null, null, null, null);
        }
    }, null, null);
}
Also used : ArrayList(java.util.ArrayList) PrinterDiscoverySessionCallbacks(android.print.mockservice.PrinterDiscoverySessionCallbacks) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Margins(android.print.PrintAttributes.Margins) StubbablePrinterDiscoverySession(android.print.mockservice.StubbablePrinterDiscoverySession) Resolution(android.print.PrintAttributes.Resolution)

Aggregations

Resolution (android.print.PrintAttributes.Resolution)15 ArrayList (java.util.ArrayList)15 PrintAttributes (android.print.PrintAttributes)10 MediaSize (android.print.PrintAttributes.MediaSize)10 PageRange (android.print.PageRange)5 Margins (android.print.PrintAttributes.Margins)5 PrintDocumentInfo (android.print.PrintDocumentInfo)5 PrintJobInfo (android.print.PrintJobInfo)5 PrinterCapabilitiesInfo (android.print.PrinterCapabilitiesInfo)5 PrinterDiscoverySessionCallbacks (android.print.mockservice.PrinterDiscoverySessionCallbacks)5 StubbablePrinterDiscoverySession (android.print.mockservice.StubbablePrinterDiscoverySession)5 RemotePrintDocumentInfo (com.android.printspooler.model.RemotePrintDocument.RemotePrintDocumentInfo)5 InvocationOnMock (org.mockito.invocation.InvocationOnMock)5 Answer (org.mockito.stubbing.Answer)5