Search in sources :

Example 6 with PrintDocumentInfo

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

the class PrintActivity method onUpdateCompleted.

@Override
public void onUpdateCompleted(RemotePrintDocumentInfo document) {
    if (DEBUG) {
        Log.i(LOG_TAG, "onUpdateCompleted()");
    }
    setState(mProgressMessageController.cancel());
    ensurePreviewUiShown();
    // Update the print job with the info for the written document. The page
    // count we get from the remote document is the pages in the document from
    // the app perspective but the print job should contain the page count from
    // print service perspective which is the pages in the written PDF not the
    // pages in the printed document.
    PrintDocumentInfo info = document.info;
    if (info != null) {
        final int pageCount = PageRangeUtils.getNormalizedPageCount(document.writtenPages, getAdjustedPageCount(info));
        PrintDocumentInfo adjustedInfo = new PrintDocumentInfo.Builder(info.getName()).setContentType(info.getContentType()).setPageCount(pageCount).build();
        File file = mFileProvider.acquireFile(null);
        try {
            adjustedInfo.setDataSize(file.length());
        } finally {
            mFileProvider.releaseFile();
        }
        mPrintJob.setDocumentInfo(adjustedInfo);
        mPrintJob.setPages(document.printedPages);
    }
    switch(mState) {
        case STATE_PRINT_CONFIRMED:
            {
                requestCreatePdfFileOrFinish();
            }
            break;
        case STATE_CREATE_FILE_FAILED:
        case STATE_PRINT_COMPLETED:
        case STATE_PRINT_CANCELED:
            {
                updateOptionsUi();
                doFinish();
            }
            break;
        default:
            {
                updatePrintPreviewController(document.changed);
                setState(STATE_CONFIGURING);
                updateOptionsUi();
            }
            break;
    }
}
Also used : RemotePrintDocumentInfo(com.android.printspooler.model.RemotePrintDocument.RemotePrintDocumentInfo) PrintDocumentInfo(android.print.PrintDocumentInfo) File(java.io.File)

Example 7 with PrintDocumentInfo

use of android.print.PrintDocumentInfo in project android_frameworks_base by DirtyUnicorns.

the class PrintActivity method updateOptionsUi.

void updateOptionsUi() {
    if (!mIsOptionsUiBound) {
        return;
    }
    // Always update the summary.
    updateSummary();
    if (mState == STATE_PRINT_CONFIRMED || mState == STATE_PRINT_COMPLETED || mState == STATE_PRINT_CANCELED || mState == STATE_UPDATE_FAILED || mState == STATE_CREATE_FILE_FAILED || mState == STATE_PRINTER_UNAVAILABLE || mState == STATE_UPDATE_SLOW) {
        if (mState != STATE_PRINTER_UNAVAILABLE) {
            mDestinationSpinner.setEnabled(false);
        }
        disableOptionsUi();
        return;
    }
    // available, we disable all print options except the destination.
    if (mCurrentPrinter == null || !canPrint(mCurrentPrinter)) {
        disableOptionsUi();
        return;
    }
    PrinterCapabilitiesInfo capabilities = mCurrentPrinter.getCapabilities();
    PrintAttributes defaultAttributes = capabilities.getDefaults();
    // Destination.
    mDestinationSpinner.setEnabled(true);
    // Media size.
    mMediaSizeSpinner.setEnabled(true);
    List<MediaSize> mediaSizes = new ArrayList<>(capabilities.getMediaSizes());
    // Sort the media sizes based on the current locale.
    Collections.sort(mediaSizes, mMediaSizeComparator);
    PrintAttributes attributes = mPrintJob.getAttributes();
    // If the media sizes changed, we update the adapter and the spinner.
    boolean mediaSizesChanged = false;
    final int mediaSizeCount = mediaSizes.size();
    if (mediaSizeCount != mMediaSizeSpinnerAdapter.getCount()) {
        mediaSizesChanged = true;
    } else {
        for (int i = 0; i < mediaSizeCount; i++) {
            if (!mediaSizes.get(i).equals(mMediaSizeSpinnerAdapter.getItem(i).value)) {
                mediaSizesChanged = true;
                break;
            }
        }
    }
    if (mediaSizesChanged) {
        // Remember the old media size to try selecting it again.
        int oldMediaSizeNewIndex = AdapterView.INVALID_POSITION;
        MediaSize oldMediaSize = attributes.getMediaSize();
        // Rebuild the adapter data.
        mMediaSizeSpinnerAdapter.clear();
        for (int i = 0; i < mediaSizeCount; i++) {
            MediaSize mediaSize = mediaSizes.get(i);
            if (oldMediaSize != null && mediaSize.asPortrait().equals(oldMediaSize.asPortrait())) {
                // Update the index of the old selection.
                oldMediaSizeNewIndex = i;
            }
            mMediaSizeSpinnerAdapter.add(new SpinnerItem<>(mediaSize, mediaSize.getLabel(getPackageManager())));
        }
        if (oldMediaSizeNewIndex != AdapterView.INVALID_POSITION) {
            // Select the old media size - nothing really changed.
            if (mMediaSizeSpinner.getSelectedItemPosition() != oldMediaSizeNewIndex) {
                mMediaSizeSpinner.setSelection(oldMediaSizeNewIndex);
            }
        } else {
            // Select the first or the default.
            final int mediaSizeIndex = Math.max(mediaSizes.indexOf(defaultAttributes.getMediaSize()), 0);
            if (mMediaSizeSpinner.getSelectedItemPosition() != mediaSizeIndex) {
                mMediaSizeSpinner.setSelection(mediaSizeIndex);
            }
            // Respect the orientation of the old selection.
            if (oldMediaSize != null) {
                if (oldMediaSize.isPortrait()) {
                    attributes.setMediaSize(mMediaSizeSpinnerAdapter.getItem(mediaSizeIndex).value.asPortrait());
                } else {
                    attributes.setMediaSize(mMediaSizeSpinnerAdapter.getItem(mediaSizeIndex).value.asLandscape());
                }
            }
        }
    }
    // Color mode.
    mColorModeSpinner.setEnabled(true);
    final int colorModes = capabilities.getColorModes();
    // If the color modes changed, we update the adapter and the spinner.
    boolean colorModesChanged = false;
    if (Integer.bitCount(colorModes) != mColorModeSpinnerAdapter.getCount()) {
        colorModesChanged = true;
    } else {
        int remainingColorModes = colorModes;
        int adapterIndex = 0;
        while (remainingColorModes != 0) {
            final int colorBitOffset = Integer.numberOfTrailingZeros(remainingColorModes);
            final int colorMode = 1 << colorBitOffset;
            remainingColorModes &= ~colorMode;
            if (colorMode != mColorModeSpinnerAdapter.getItem(adapterIndex).value) {
                colorModesChanged = true;
                break;
            }
            adapterIndex++;
        }
    }
    if (colorModesChanged) {
        // Remember the old color mode to try selecting it again.
        int oldColorModeNewIndex = AdapterView.INVALID_POSITION;
        final int oldColorMode = attributes.getColorMode();
        // Rebuild the adapter data.
        mColorModeSpinnerAdapter.clear();
        String[] colorModeLabels = getResources().getStringArray(R.array.color_mode_labels);
        int remainingColorModes = colorModes;
        while (remainingColorModes != 0) {
            final int colorBitOffset = Integer.numberOfTrailingZeros(remainingColorModes);
            final int colorMode = 1 << colorBitOffset;
            if (colorMode == oldColorMode) {
                // Update the index of the old selection.
                oldColorModeNewIndex = mColorModeSpinnerAdapter.getCount();
            }
            remainingColorModes &= ~colorMode;
            mColorModeSpinnerAdapter.add(new SpinnerItem<>(colorMode, colorModeLabels[colorBitOffset]));
        }
        if (oldColorModeNewIndex != AdapterView.INVALID_POSITION) {
            // Select the old color mode - nothing really changed.
            if (mColorModeSpinner.getSelectedItemPosition() != oldColorModeNewIndex) {
                mColorModeSpinner.setSelection(oldColorModeNewIndex);
            }
        } else {
            // Select the default.
            final int selectedColorMode = colorModes & defaultAttributes.getColorMode();
            final int itemCount = mColorModeSpinnerAdapter.getCount();
            for (int i = 0; i < itemCount; i++) {
                SpinnerItem<Integer> item = mColorModeSpinnerAdapter.getItem(i);
                if (selectedColorMode == item.value) {
                    if (mColorModeSpinner.getSelectedItemPosition() != i) {
                        mColorModeSpinner.setSelection(i);
                    }
                    attributes.setColorMode(selectedColorMode);
                    break;
                }
            }
        }
    }
    // Duplex mode.
    mDuplexModeSpinner.setEnabled(true);
    final int duplexModes = capabilities.getDuplexModes();
    // If the duplex modes changed, we update the adapter and the spinner.
    // Note that we use bit count +1 to account for the no duplex option.
    boolean duplexModesChanged = false;
    if (Integer.bitCount(duplexModes) != mDuplexModeSpinnerAdapter.getCount()) {
        duplexModesChanged = true;
    } else {
        int remainingDuplexModes = duplexModes;
        int adapterIndex = 0;
        while (remainingDuplexModes != 0) {
            final int duplexBitOffset = Integer.numberOfTrailingZeros(remainingDuplexModes);
            final int duplexMode = 1 << duplexBitOffset;
            remainingDuplexModes &= ~duplexMode;
            if (duplexMode != mDuplexModeSpinnerAdapter.getItem(adapterIndex).value) {
                duplexModesChanged = true;
                break;
            }
            adapterIndex++;
        }
    }
    if (duplexModesChanged) {
        // Remember the old duplex mode to try selecting it again. Also the fallback
        // is no duplexing which is always the first item in the dropdown.
        int oldDuplexModeNewIndex = AdapterView.INVALID_POSITION;
        final int oldDuplexMode = attributes.getDuplexMode();
        // Rebuild the adapter data.
        mDuplexModeSpinnerAdapter.clear();
        String[] duplexModeLabels = getResources().getStringArray(R.array.duplex_mode_labels);
        int remainingDuplexModes = duplexModes;
        while (remainingDuplexModes != 0) {
            final int duplexBitOffset = Integer.numberOfTrailingZeros(remainingDuplexModes);
            final int duplexMode = 1 << duplexBitOffset;
            if (duplexMode == oldDuplexMode) {
                // Update the index of the old selection.
                oldDuplexModeNewIndex = mDuplexModeSpinnerAdapter.getCount();
            }
            remainingDuplexModes &= ~duplexMode;
            mDuplexModeSpinnerAdapter.add(new SpinnerItem<>(duplexMode, duplexModeLabels[duplexBitOffset]));
        }
        if (oldDuplexModeNewIndex != AdapterView.INVALID_POSITION) {
            // Select the old duplex mode - nothing really changed.
            if (mDuplexModeSpinner.getSelectedItemPosition() != oldDuplexModeNewIndex) {
                mDuplexModeSpinner.setSelection(oldDuplexModeNewIndex);
            }
        } else {
            // Select the default.
            final int selectedDuplexMode = defaultAttributes.getDuplexMode();
            final int itemCount = mDuplexModeSpinnerAdapter.getCount();
            for (int i = 0; i < itemCount; i++) {
                SpinnerItem<Integer> item = mDuplexModeSpinnerAdapter.getItem(i);
                if (selectedDuplexMode == item.value) {
                    if (mDuplexModeSpinner.getSelectedItemPosition() != i) {
                        mDuplexModeSpinner.setSelection(i);
                    }
                    attributes.setDuplexMode(selectedDuplexMode);
                    break;
                }
            }
        }
    }
    mDuplexModeSpinner.setEnabled(mDuplexModeSpinnerAdapter.getCount() > 1);
    // Orientation
    mOrientationSpinner.setEnabled(true);
    MediaSize mediaSize = attributes.getMediaSize();
    if (mediaSize != null) {
        if (mediaSize.isPortrait() && mOrientationSpinner.getSelectedItemPosition() != 0) {
            mOrientationSpinner.setSelection(0);
        } else if (!mediaSize.isPortrait() && mOrientationSpinner.getSelectedItemPosition() != 1) {
            mOrientationSpinner.setSelection(1);
        }
    }
    // Range options
    PrintDocumentInfo info = mPrintedDocument.getDocumentInfo().info;
    final int pageCount = getAdjustedPageCount(info);
    if (pageCount > 0) {
        if (info != null) {
            if (pageCount == 1) {
                mRangeOptionsSpinner.setEnabled(false);
            } else {
                mRangeOptionsSpinner.setEnabled(true);
                if (mRangeOptionsSpinner.getSelectedItemPosition() > 0) {
                    if (!mPageRangeEditText.isEnabled()) {
                        mPageRangeEditText.setEnabled(true);
                        mPageRangeEditText.setVisibility(View.VISIBLE);
                        mPageRangeTitle.setVisibility(View.VISIBLE);
                        mPageRangeEditText.requestFocus();
                        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.showSoftInput(mPageRangeEditText, 0);
                    }
                } else {
                    mPageRangeEditText.setEnabled(false);
                    mPageRangeEditText.setVisibility(View.INVISIBLE);
                    mPageRangeTitle.setVisibility(View.INVISIBLE);
                }
            }
        } else {
            if (mRangeOptionsSpinner.getSelectedItemPosition() != 0) {
                mRangeOptionsSpinner.setSelection(0);
                mPageRangeEditText.setText("");
            }
            mRangeOptionsSpinner.setEnabled(false);
            mPageRangeEditText.setEnabled(false);
            mPageRangeEditText.setVisibility(View.INVISIBLE);
            mPageRangeTitle.setVisibility(View.INVISIBLE);
        }
    }
    final int newPageCount = getAdjustedPageCount(info);
    if (newPageCount != mCurrentPageCount) {
        mCurrentPageCount = newPageCount;
        updatePageRangeOptions(newPageCount);
    }
    // Advanced print options
    if (mAdvancedPrintOptionsActivity != null) {
        mMoreOptionsButton.setVisibility(View.VISIBLE);
        mMoreOptionsButton.setEnabled(true);
    } else {
        mMoreOptionsButton.setVisibility(View.GONE);
        mMoreOptionsButton.setEnabled(false);
    }
    // Print
    if (mDestinationSpinnerAdapter.getPdfPrinter() != mCurrentPrinter) {
        mPrintButton.setImageResource(com.android.internal.R.drawable.ic_print);
        mPrintButton.setContentDescription(getString(R.string.print_button));
    } else {
        mPrintButton.setImageResource(R.drawable.ic_menu_savetopdf);
        mPrintButton.setContentDescription(getString(R.string.savetopdf_button));
    }
    if (!mPrintedDocument.getDocumentInfo().laidout || (mRangeOptionsSpinner.getSelectedItemPosition() == 1 && (TextUtils.isEmpty(mPageRangeEditText.getText()) || hasErrors())) || (mRangeOptionsSpinner.getSelectedItemPosition() == 0 && (mPrintedDocument.getDocumentInfo() == null || hasErrors()))) {
        mPrintButton.setVisibility(View.GONE);
    } else {
        mPrintButton.setVisibility(View.VISIBLE);
    }
    // Copies
    if (mDestinationSpinnerAdapter.getPdfPrinter() != mCurrentPrinter) {
        mCopiesEditText.setEnabled(true);
        mCopiesEditText.setFocusableInTouchMode(true);
    } else {
        CharSequence text = mCopiesEditText.getText();
        if (TextUtils.isEmpty(text) || !MIN_COPIES_STRING.equals(text.toString())) {
            mCopiesEditText.setText(MIN_COPIES_STRING);
        }
        mCopiesEditText.setEnabled(false);
        mCopiesEditText.setFocusable(false);
    }
    if (mCopiesEditText.getError() == null && TextUtils.isEmpty(mCopiesEditText.getText())) {
        mCopiesEditText.setText(MIN_COPIES_STRING);
        mCopiesEditText.requestFocus();
    }
    if (mShowDestinationPrompt) {
        disableOptionsUi();
    }
}
Also used : PrinterCapabilitiesInfo(android.print.PrinterCapabilitiesInfo) MediaSize(android.print.PrintAttributes.MediaSize) ArrayList(java.util.ArrayList) InputMethodManager(android.view.inputmethod.InputMethodManager) PrintAttributes(android.print.PrintAttributes) RemotePrintDocumentInfo(com.android.printspooler.model.RemotePrintDocument.RemotePrintDocumentInfo) PrintDocumentInfo(android.print.PrintDocumentInfo)

Example 8 with PrintDocumentInfo

use of android.print.PrintDocumentInfo in project android_frameworks_base by DirtyUnicorns.

the class PrintActivity method startCreateDocumentActivity.

private void startCreateDocumentActivity() {
    if (!isResumed()) {
        return;
    }
    PrintDocumentInfo info = mPrintedDocument.getDocumentInfo().info;
    if (info == null) {
        return;
    }
    Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
    intent.setType("application/pdf");
    intent.putExtra(Intent.EXTRA_TITLE, info.getName());
    intent.putExtra(DocumentsContract.EXTRA_PACKAGE_NAME, mCallingPackageName);
    try {
        startActivityForResult(intent, ACTIVITY_REQUEST_CREATE_FILE);
    } catch (Exception e) {
        Log.e(LOG_TAG, "Could not create file", e);
        Toast.makeText(this, getString(R.string.could_not_create_file), Toast.LENGTH_SHORT).show();
        onStartCreateDocumentActivityResult(RESULT_CANCELED, null);
    }
}
Also used : Intent(android.content.Intent) RemotePrintDocumentInfo(com.android.printspooler.model.RemotePrintDocument.RemotePrintDocumentInfo) PrintDocumentInfo(android.print.PrintDocumentInfo) RemoteException(android.os.RemoteException) IOException(java.io.IOException) ActivityNotFoundException(android.content.ActivityNotFoundException) NameNotFoundException(android.content.pm.PackageManager.NameNotFoundException)

Example 9 with PrintDocumentInfo

use of android.print.PrintDocumentInfo in project android_frameworks_base by DirtyUnicorns.

the class PrintActivity method computeSelectedPages.

private PageRange[] computeSelectedPages() {
    if (hasErrors()) {
        return null;
    }
    if (mRangeOptionsSpinner.getSelectedItemPosition() > 0) {
        PrintDocumentInfo info = mPrintedDocument.getDocumentInfo().info;
        final int pageCount = (info != null) ? getAdjustedPageCount(info) : 0;
        return PageRangeUtils.parsePageRanges(mPageRangeEditText.getText(), pageCount);
    }
    return PageRange.ALL_PAGES_ARRAY;
}
Also used : RemotePrintDocumentInfo(com.android.printspooler.model.RemotePrintDocument.RemotePrintDocumentInfo) PrintDocumentInfo(android.print.PrintDocumentInfo)

Example 10 with PrintDocumentInfo

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

the class PrintActivity method updateOptionsUi.

void updateOptionsUi() {
    if (!mIsOptionsUiBound) {
        return;
    }
    // Always update the summary.
    updateSummary();
    if (mState == STATE_PRINT_CONFIRMED || mState == STATE_PRINT_COMPLETED || mState == STATE_PRINT_CANCELED || mState == STATE_UPDATE_FAILED || mState == STATE_CREATE_FILE_FAILED || mState == STATE_PRINTER_UNAVAILABLE || mState == STATE_UPDATE_SLOW) {
        if (mState != STATE_PRINTER_UNAVAILABLE) {
            mDestinationSpinner.setEnabled(false);
        }
        disableOptionsUi();
        return;
    }
    // available, we disable all print options except the destination.
    if (mCurrentPrinter == null || !canPrint(mCurrentPrinter)) {
        disableOptionsUi();
        return;
    }
    PrinterCapabilitiesInfo capabilities = mCurrentPrinter.getCapabilities();
    PrintAttributes defaultAttributes = capabilities.getDefaults();
    // Destination.
    mDestinationSpinner.setEnabled(true);
    // Media size.
    mMediaSizeSpinner.setEnabled(true);
    List<MediaSize> mediaSizes = new ArrayList<>(capabilities.getMediaSizes());
    // Sort the media sizes based on the current locale.
    Collections.sort(mediaSizes, mMediaSizeComparator);
    PrintAttributes attributes = mPrintJob.getAttributes();
    // If the media sizes changed, we update the adapter and the spinner.
    boolean mediaSizesChanged = false;
    final int mediaSizeCount = mediaSizes.size();
    if (mediaSizeCount != mMediaSizeSpinnerAdapter.getCount()) {
        mediaSizesChanged = true;
    } else {
        for (int i = 0; i < mediaSizeCount; i++) {
            if (!mediaSizes.get(i).equals(mMediaSizeSpinnerAdapter.getItem(i).value)) {
                mediaSizesChanged = true;
                break;
            }
        }
    }
    if (mediaSizesChanged) {
        // Remember the old media size to try selecting it again.
        int oldMediaSizeNewIndex = AdapterView.INVALID_POSITION;
        MediaSize oldMediaSize = attributes.getMediaSize();
        // Rebuild the adapter data.
        mMediaSizeSpinnerAdapter.clear();
        for (int i = 0; i < mediaSizeCount; i++) {
            MediaSize mediaSize = mediaSizes.get(i);
            if (oldMediaSize != null && mediaSize.asPortrait().equals(oldMediaSize.asPortrait())) {
                // Update the index of the old selection.
                oldMediaSizeNewIndex = i;
            }
            mMediaSizeSpinnerAdapter.add(new SpinnerItem<>(mediaSize, mediaSize.getLabel(getPackageManager())));
        }
        if (oldMediaSizeNewIndex != AdapterView.INVALID_POSITION) {
            // Select the old media size - nothing really changed.
            if (mMediaSizeSpinner.getSelectedItemPosition() != oldMediaSizeNewIndex) {
                mMediaSizeSpinner.setSelection(oldMediaSizeNewIndex);
            }
        } else {
            // Select the first or the default.
            final int mediaSizeIndex = Math.max(mediaSizes.indexOf(defaultAttributes.getMediaSize()), 0);
            if (mMediaSizeSpinner.getSelectedItemPosition() != mediaSizeIndex) {
                mMediaSizeSpinner.setSelection(mediaSizeIndex);
            }
            // Respect the orientation of the old selection.
            if (oldMediaSize != null) {
                if (oldMediaSize.isPortrait()) {
                    attributes.setMediaSize(mMediaSizeSpinnerAdapter.getItem(mediaSizeIndex).value.asPortrait());
                } else {
                    attributes.setMediaSize(mMediaSizeSpinnerAdapter.getItem(mediaSizeIndex).value.asLandscape());
                }
            }
        }
    }
    // Color mode.
    mColorModeSpinner.setEnabled(true);
    final int colorModes = capabilities.getColorModes();
    // If the color modes changed, we update the adapter and the spinner.
    boolean colorModesChanged = false;
    if (Integer.bitCount(colorModes) != mColorModeSpinnerAdapter.getCount()) {
        colorModesChanged = true;
    } else {
        int remainingColorModes = colorModes;
        int adapterIndex = 0;
        while (remainingColorModes != 0) {
            final int colorBitOffset = Integer.numberOfTrailingZeros(remainingColorModes);
            final int colorMode = 1 << colorBitOffset;
            remainingColorModes &= ~colorMode;
            if (colorMode != mColorModeSpinnerAdapter.getItem(adapterIndex).value) {
                colorModesChanged = true;
                break;
            }
            adapterIndex++;
        }
    }
    if (colorModesChanged) {
        // Remember the old color mode to try selecting it again.
        int oldColorModeNewIndex = AdapterView.INVALID_POSITION;
        final int oldColorMode = attributes.getColorMode();
        // Rebuild the adapter data.
        mColorModeSpinnerAdapter.clear();
        String[] colorModeLabels = getResources().getStringArray(R.array.color_mode_labels);
        int remainingColorModes = colorModes;
        while (remainingColorModes != 0) {
            final int colorBitOffset = Integer.numberOfTrailingZeros(remainingColorModes);
            final int colorMode = 1 << colorBitOffset;
            if (colorMode == oldColorMode) {
                // Update the index of the old selection.
                oldColorModeNewIndex = mColorModeSpinnerAdapter.getCount();
            }
            remainingColorModes &= ~colorMode;
            mColorModeSpinnerAdapter.add(new SpinnerItem<>(colorMode, colorModeLabels[colorBitOffset]));
        }
        if (oldColorModeNewIndex != AdapterView.INVALID_POSITION) {
            // Select the old color mode - nothing really changed.
            if (mColorModeSpinner.getSelectedItemPosition() != oldColorModeNewIndex) {
                mColorModeSpinner.setSelection(oldColorModeNewIndex);
            }
        } else {
            // Select the default.
            final int selectedColorMode = colorModes & defaultAttributes.getColorMode();
            final int itemCount = mColorModeSpinnerAdapter.getCount();
            for (int i = 0; i < itemCount; i++) {
                SpinnerItem<Integer> item = mColorModeSpinnerAdapter.getItem(i);
                if (selectedColorMode == item.value) {
                    if (mColorModeSpinner.getSelectedItemPosition() != i) {
                        mColorModeSpinner.setSelection(i);
                    }
                    attributes.setColorMode(selectedColorMode);
                    break;
                }
            }
        }
    }
    // Duplex mode.
    mDuplexModeSpinner.setEnabled(true);
    final int duplexModes = capabilities.getDuplexModes();
    // If the duplex modes changed, we update the adapter and the spinner.
    // Note that we use bit count +1 to account for the no duplex option.
    boolean duplexModesChanged = false;
    if (Integer.bitCount(duplexModes) != mDuplexModeSpinnerAdapter.getCount()) {
        duplexModesChanged = true;
    } else {
        int remainingDuplexModes = duplexModes;
        int adapterIndex = 0;
        while (remainingDuplexModes != 0) {
            final int duplexBitOffset = Integer.numberOfTrailingZeros(remainingDuplexModes);
            final int duplexMode = 1 << duplexBitOffset;
            remainingDuplexModes &= ~duplexMode;
            if (duplexMode != mDuplexModeSpinnerAdapter.getItem(adapterIndex).value) {
                duplexModesChanged = true;
                break;
            }
            adapterIndex++;
        }
    }
    if (duplexModesChanged) {
        // Remember the old duplex mode to try selecting it again. Also the fallback
        // is no duplexing which is always the first item in the dropdown.
        int oldDuplexModeNewIndex = AdapterView.INVALID_POSITION;
        final int oldDuplexMode = attributes.getDuplexMode();
        // Rebuild the adapter data.
        mDuplexModeSpinnerAdapter.clear();
        String[] duplexModeLabels = getResources().getStringArray(R.array.duplex_mode_labels);
        int remainingDuplexModes = duplexModes;
        while (remainingDuplexModes != 0) {
            final int duplexBitOffset = Integer.numberOfTrailingZeros(remainingDuplexModes);
            final int duplexMode = 1 << duplexBitOffset;
            if (duplexMode == oldDuplexMode) {
                // Update the index of the old selection.
                oldDuplexModeNewIndex = mDuplexModeSpinnerAdapter.getCount();
            }
            remainingDuplexModes &= ~duplexMode;
            mDuplexModeSpinnerAdapter.add(new SpinnerItem<>(duplexMode, duplexModeLabels[duplexBitOffset]));
        }
        if (oldDuplexModeNewIndex != AdapterView.INVALID_POSITION) {
            // Select the old duplex mode - nothing really changed.
            if (mDuplexModeSpinner.getSelectedItemPosition() != oldDuplexModeNewIndex) {
                mDuplexModeSpinner.setSelection(oldDuplexModeNewIndex);
            }
        } else {
            // Select the default.
            final int selectedDuplexMode = defaultAttributes.getDuplexMode();
            final int itemCount = mDuplexModeSpinnerAdapter.getCount();
            for (int i = 0; i < itemCount; i++) {
                SpinnerItem<Integer> item = mDuplexModeSpinnerAdapter.getItem(i);
                if (selectedDuplexMode == item.value) {
                    if (mDuplexModeSpinner.getSelectedItemPosition() != i) {
                        mDuplexModeSpinner.setSelection(i);
                    }
                    attributes.setDuplexMode(selectedDuplexMode);
                    break;
                }
            }
        }
    }
    mDuplexModeSpinner.setEnabled(mDuplexModeSpinnerAdapter.getCount() > 1);
    // Orientation
    mOrientationSpinner.setEnabled(true);
    MediaSize mediaSize = attributes.getMediaSize();
    if (mediaSize != null) {
        if (mediaSize.isPortrait() && mOrientationSpinner.getSelectedItemPosition() != 0) {
            mOrientationSpinner.setSelection(0);
        } else if (!mediaSize.isPortrait() && mOrientationSpinner.getSelectedItemPosition() != 1) {
            mOrientationSpinner.setSelection(1);
        }
    }
    // Range options
    PrintDocumentInfo info = mPrintedDocument.getDocumentInfo().info;
    final int pageCount = getAdjustedPageCount(info);
    if (pageCount > 0) {
        if (info != null) {
            if (pageCount == 1) {
                mRangeOptionsSpinner.setEnabled(false);
            } else {
                mRangeOptionsSpinner.setEnabled(true);
                if (mRangeOptionsSpinner.getSelectedItemPosition() > 0) {
                    if (!mPageRangeEditText.isEnabled()) {
                        mPageRangeEditText.setEnabled(true);
                        mPageRangeEditText.setVisibility(View.VISIBLE);
                        mPageRangeTitle.setVisibility(View.VISIBLE);
                        mPageRangeEditText.requestFocus();
                        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.showSoftInput(mPageRangeEditText, 0);
                    }
                } else {
                    mPageRangeEditText.setEnabled(false);
                    mPageRangeEditText.setVisibility(View.INVISIBLE);
                    mPageRangeTitle.setVisibility(View.INVISIBLE);
                }
            }
        } else {
            if (mRangeOptionsSpinner.getSelectedItemPosition() != 0) {
                mRangeOptionsSpinner.setSelection(0);
                mPageRangeEditText.setText("");
            }
            mRangeOptionsSpinner.setEnabled(false);
            mPageRangeEditText.setEnabled(false);
            mPageRangeEditText.setVisibility(View.INVISIBLE);
            mPageRangeTitle.setVisibility(View.INVISIBLE);
        }
    }
    final int newPageCount = getAdjustedPageCount(info);
    if (newPageCount != mCurrentPageCount) {
        mCurrentPageCount = newPageCount;
        updatePageRangeOptions(newPageCount);
    }
    // Advanced print options
    if (mAdvancedPrintOptionsActivity != null) {
        mMoreOptionsButton.setVisibility(View.VISIBLE);
        mMoreOptionsButton.setEnabled(true);
    } else {
        mMoreOptionsButton.setVisibility(View.GONE);
        mMoreOptionsButton.setEnabled(false);
    }
    // Print
    if (mDestinationSpinnerAdapter.getPdfPrinter() != mCurrentPrinter) {
        mPrintButton.setImageResource(com.android.internal.R.drawable.ic_print);
        mPrintButton.setContentDescription(getString(R.string.print_button));
    } else {
        mPrintButton.setImageResource(R.drawable.ic_menu_savetopdf);
        mPrintButton.setContentDescription(getString(R.string.savetopdf_button));
    }
    if (!mPrintedDocument.getDocumentInfo().laidout || (mRangeOptionsSpinner.getSelectedItemPosition() == 1 && (TextUtils.isEmpty(mPageRangeEditText.getText()) || hasErrors())) || (mRangeOptionsSpinner.getSelectedItemPosition() == 0 && (mPrintedDocument.getDocumentInfo() == null || hasErrors()))) {
        mPrintButton.setVisibility(View.GONE);
    } else {
        mPrintButton.setVisibility(View.VISIBLE);
    }
    // Copies
    if (mDestinationSpinnerAdapter.getPdfPrinter() != mCurrentPrinter) {
        mCopiesEditText.setEnabled(true);
        mCopiesEditText.setFocusableInTouchMode(true);
    } else {
        CharSequence text = mCopiesEditText.getText();
        if (TextUtils.isEmpty(text) || !MIN_COPIES_STRING.equals(text.toString())) {
            mCopiesEditText.setText(MIN_COPIES_STRING);
        }
        mCopiesEditText.setEnabled(false);
        mCopiesEditText.setFocusable(false);
    }
    if (mCopiesEditText.getError() == null && TextUtils.isEmpty(mCopiesEditText.getText())) {
        mCopiesEditText.setText(MIN_COPIES_STRING);
        mCopiesEditText.requestFocus();
    }
    if (mShowDestinationPrompt) {
        disableOptionsUi();
    }
}
Also used : PrinterCapabilitiesInfo(android.print.PrinterCapabilitiesInfo) MediaSize(android.print.PrintAttributes.MediaSize) ArrayList(java.util.ArrayList) InputMethodManager(android.view.inputmethod.InputMethodManager) PrintAttributes(android.print.PrintAttributes) RemotePrintDocumentInfo(com.android.printspooler.model.RemotePrintDocument.RemotePrintDocumentInfo) PrintDocumentInfo(android.print.PrintDocumentInfo)

Aggregations

PrintDocumentInfo (android.print.PrintDocumentInfo)25 RemotePrintDocumentInfo (com.android.printspooler.model.RemotePrintDocument.RemotePrintDocumentInfo)25 PrintAttributes (android.print.PrintAttributes)10 MediaSize (android.print.PrintAttributes.MediaSize)10 PrinterCapabilitiesInfo (android.print.PrinterCapabilitiesInfo)10 ArrayList (java.util.ArrayList)10 ActivityNotFoundException (android.content.ActivityNotFoundException)5 Intent (android.content.Intent)5 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)5 RemoteException (android.os.RemoteException)5 PageRange (android.print.PageRange)5 Resolution (android.print.PrintAttributes.Resolution)5 PrintJobInfo (android.print.PrintJobInfo)5 InputMethodManager (android.view.inputmethod.InputMethodManager)5 File (java.io.File)5 IOException (java.io.IOException)5