use of android.print.PrintAttributes in project platform_frameworks_base by android.
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();
}
}
use of android.print.PrintAttributes in project platform_frameworks_base by android.
the class PrintActivity method canUpdateDocument.
private boolean canUpdateDocument() {
if (mPrintedDocument.isDestroyed()) {
return false;
}
if (hasErrors()) {
return false;
}
PrintAttributes attributes = mPrintJob.getAttributes();
final int colorMode = attributes.getColorMode();
if (colorMode != PrintAttributes.COLOR_MODE_COLOR && colorMode != PrintAttributes.COLOR_MODE_MONOCHROME) {
return false;
}
if (attributes.getMediaSize() == null) {
return false;
}
if (attributes.getMinMargins() == null) {
return false;
}
if (attributes.getResolution() == null) {
return false;
}
if (mCurrentPrinter == null) {
return false;
}
PrinterCapabilitiesInfo capabilities = mCurrentPrinter.getCapabilities();
if (capabilities == null) {
return false;
}
if (mCurrentPrinter.getStatus() == PrinterInfo.STATUS_UNAVAILABLE) {
return false;
}
return true;
}
use of android.print.PrintAttributes in project platform_frameworks_base by android.
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);
}
}
use of android.print.PrintAttributes in project platform_frameworks_base by android.
the class RemotePrintDocument method update.
public boolean update(PrintAttributes attributes, PageRange[] pages, boolean preview) {
boolean willUpdate;
if (DEBUG) {
Log.i(LOG_TAG, "[CALLED] update()");
}
if (hasUpdateError()) {
throw new IllegalStateException("Cannot update without a clearing the failure");
}
if (mState == STATE_INITIAL || mState == STATE_FINISHED || mState == STATE_DESTROYED) {
throw new IllegalStateException("Cannot update in state:" + stateToString(mState));
}
// We schedule a layout if the constraints changed.
if (!mUpdateSpec.hasSameConstraints(attributes, preview)) {
willUpdate = true;
// cancellation and start over.
if (mCurrentCommand != null && (mCurrentCommand.isRunning() || mCurrentCommand.isPending())) {
mCurrentCommand.cancel(false);
}
// Schedule a layout command.
PrintAttributes oldAttributes = mDocumentInfo.attributes != null ? mDocumentInfo.attributes : new PrintAttributes.Builder().build();
AsyncCommand command = new LayoutCommand(mLooper, mPrintDocumentAdapter, mDocumentInfo, oldAttributes, attributes, preview, mCommandResultCallback);
scheduleCommand(command);
mState = STATE_UPDATING;
// If no layout in progress and we don't have all pages - schedule a write.
} else if ((!(mCurrentCommand instanceof LayoutCommand) || (!mCurrentCommand.isPending() && !mCurrentCommand.isRunning())) && pages != null && !PageRangeUtils.contains(mUpdateSpec.pages, pages, mDocumentInfo.info.getPageCount())) {
willUpdate = true;
// Cancel the current write as a new one is to be scheduled.
if (mCurrentCommand instanceof WriteCommand && (mCurrentCommand.isPending() || mCurrentCommand.isRunning())) {
mCurrentCommand.cancel(false);
}
// Schedule a write command.
AsyncCommand command = new WriteCommand(mContext, mLooper, mPrintDocumentAdapter, mDocumentInfo, mDocumentInfo.info.getPageCount(), pages, mDocumentInfo.fileProvider, mCommandResultCallback);
scheduleCommand(command);
mState = STATE_UPDATING;
} else {
willUpdate = false;
if (DEBUG) {
Log.i(LOG_TAG, "[SKIPPING] No update needed");
}
}
// Keep track of what is requested.
mUpdateSpec.update(attributes, preview, pages);
runPendingCommand();
return willUpdate;
}
use of android.print.PrintAttributes in project android_frameworks_base by ResurrectionRemix.
the class PrintActivity method transformDocumentAndFinish.
private void transformDocumentAndFinish(final Uri writeToUri) {
// If saving to PDF, apply the attibutes as we are acting as a print service.
PrintAttributes attributes = mDestinationSpinnerAdapter.getPdfPrinter() == mCurrentPrinter ? mPrintJob.getAttributes() : null;
new DocumentTransformer(this, mPrintJob, mFileProvider, attributes, new Runnable() {
@Override
public void run() {
if (writeToUri != null) {
mPrintedDocument.writeContent(getContentResolver(), writeToUri);
}
setState(STATE_PRINT_COMPLETED);
doFinish();
}
}).transform();
}
Aggregations