summaryrefslogtreecommitdiffstats
path: root/packages/PrintSpooler/src/com/android
diff options
context:
space:
mode:
authorSvetoslav <svetoslavganov@google.com>2013-09-03 14:01:43 -0700
committerSvetoslav <svetoslavganov@google.com>2013-09-04 21:06:12 -0700
commit773f54de3de9bce7b6f915aa47ed686b161d77aa (patch)
treef5849053ebf63d993ff9e3c629b1f8df6e95f4b9 /packages/PrintSpooler/src/com/android
parentad0ec16fbe27e69e22b90ad9f1b9e322d8c20e8d (diff)
downloadframeworks_base-773f54de3de9bce7b6f915aa47ed686b161d77aa.zip
frameworks_base-773f54de3de9bce7b6f915aa47ed686b161d77aa.tar.gz
frameworks_base-773f54de3de9bce7b6f915aa47ed686b161d77aa.tar.bz2
Print API updated.
1. Removed the updatePrinters API on PrinterDiscoverySession. Now re-adding a printer updates it. 2. Added getTrackedPrinters() API to allow a print service to figure out which printers' state should be tracked by the service. 3. Removed the APIs on PrintDocumentInfo that describe the layout of the content as they are not needed. For example, if the print attributes passed in layout require landscape mode, the the app should just create a PDF page with the wider side up and draw its content in portrait. The printer will then rotate the page. Change-Id: Idd72c6e9c129e8b17eef1236573a99773f8ff0a2
Diffstat (limited to 'packages/PrintSpooler/src/com/android')
-rw-r--r--packages/PrintSpooler/src/com/android/printspooler/PrintJobConfigActivity.java178
-rw-r--r--packages/PrintSpooler/src/com/android/printspooler/PrintSpoolerService.java118
2 files changed, 72 insertions, 224 deletions
diff --git a/packages/PrintSpooler/src/com/android/printspooler/PrintJobConfigActivity.java b/packages/PrintSpooler/src/com/android/printspooler/PrintJobConfigActivity.java
index 5361a1e..929a04e 100644
--- a/packages/PrintSpooler/src/com/android/printspooler/PrintJobConfigActivity.java
+++ b/packages/PrintSpooler/src/com/android/printspooler/PrintJobConfigActivity.java
@@ -42,7 +42,6 @@ import android.print.IPrintDocumentAdapter;
import android.print.IWriteResultCallback;
import android.print.PageRange;
import android.print.PrintAttributes;
-import android.print.PrintAttributes.Margins;
import android.print.PrintAttributes.MediaSize;
import android.print.PrintAttributes.Resolution;
import android.print.PrintDocumentAdapter;
@@ -75,8 +74,6 @@ import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
-import libcore.io.IoUtils;
-
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
@@ -92,6 +89,8 @@ import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import libcore.io.IoUtils;
+
/**
* Activity for configuring a print job.
*/
@@ -111,6 +110,9 @@ public class PrintJobConfigActivity extends Activity {
private static final int LOADER_ID_PRINTERS_LOADER = 1;
+ private static final int ORIENTATION_PORTRAIT = 0;
+ private static final int ORIENTATION_LANDSCAPE = 1;
+
private static final int DEST_ADAPTER_MAX_ITEM_COUNT = 9;
private static final int DEST_ADAPTER_ITEM_ID_SAVE_AS_PDF = Integer.MAX_VALUE;
@@ -343,10 +345,11 @@ public class PrintJobConfigActivity extends Activity {
if (!mController.hasStarted()) {
mController.start();
}
- if (!printAttributesChanged()) {
- // If the attributes changed, then we do not do a layout but may
- // have to ask the app to write some pages. Hence, pretend layout
- // completed and nothing changed, so we handle writing as usual.
+ if (!printAttributesChanged() && mDocument.info != null) {
+ // If the attributes didn't change and we have done a layout, then
+ // we do not do a layout but may have to ask the app to write some
+ // pages. Hence, pretend layout completed and nothing changed, so
+ // we handle writing as usual.
handleOnLayoutFinished(mDocument.info, false, mRequestCounter.get());
} else {
PrintSpoolerService.peekInstance().setPrintJobAttributesNoPersistence(
@@ -394,9 +397,6 @@ public class PrintJobConfigActivity extends Activity {
mPrintJobId, info);
}
- // Update the fitting mode based on the document type.
- updateCurrentFittingMode(info);
-
// If the document info or the layout changed, then
// drop the pages since we have to fetch them again.
if (infoChanged || layoutChanged) {
@@ -526,10 +526,6 @@ public class PrintJobConfigActivity extends Activity {
}
if (mEditor.isDone()) {
- // Update the print attributes based on whether the application
- // handled some of the print attribute constraints, e.g. rotation.
- updateAndSaveCurrentPrintAttributes(mDocument.info);
-
if (mEditor.isPrintingToPdf()) {
PrintJobInfo printJob = PrintSpoolerService.peekInstance()
.getPrintJobInfo(mPrintJobId, PrintManager.APP_ID_ANY);
@@ -552,58 +548,6 @@ public class PrintJobConfigActivity extends Activity {
PrintJobConfigActivity.this.finish();
}
- private void updateCurrentFittingMode(PrintDocumentInfo document) {
- // Update the fitting mode based on content type.
- switch (document.getContentType()) {
- case PrintDocumentInfo.CONTENT_TYPE_DOCUMENT: {
- mCurrPrintAttributes.setFittingMode(
- PrintAttributes.FITTING_MODE_SCALE_TO_FIT);
- } break;
-
- case PrintDocumentInfo.CONTENT_TYPE_PHOTO: {
- mCurrPrintAttributes.setFittingMode(
- PrintAttributes.FITTING_MODE_SCALE_TO_FILL);
- }
- }
- }
-
- private void updateAndSaveCurrentPrintAttributes(PrintDocumentInfo document) {
- PrintAttributes attributes = mTempPrintAttributes;
- attributes.copyFrom(mCurrPrintAttributes);
-
- // Update the orientation
- if (document.getOrientation() == PrintAttributes.ORIENTATION_LANDSCAPE) {
- if (attributes.getOrientation() == PrintAttributes.ORIENTATION_LANDSCAPE) {
- // If the document is in landscape and we want to print it in
- // landscape, then we do not need to rotate, so portrait.
- attributes.setOrientation(PrintAttributes.ORIENTATION_PORTRAIT);
- } else {
- // If the document is in landscape and we want to print it in
- // portrait, then we have to rotate the content, so landscape.
- attributes.setOrientation(PrintAttributes.ORIENTATION_LANDSCAPE);
- }
- }
-
- // Update margins.
- Margins documentMargins = document.getMargins();
- if (documentMargins.getLeftMils() != 0
- || documentMargins.getTopMils() != 0
- || documentMargins.getRightMils() != 0
- || documentMargins.getBottomMils() != 0) {
- // If the application has applied some of the margins, then
- // the printer should only apply the difference.
- Margins oldMargins = attributes.getMargins();
- attributes.setMargins(new Margins(
- oldMargins.getLeftMils() - documentMargins.getLeftMils(),
- oldMargins.getTopMils() - documentMargins.getTopMils(),
- oldMargins.getRightMils() - documentMargins.getRightMils(),
- oldMargins.getBottomMils() - documentMargins.getBottomMils()));
- }
-
- PrintSpoolerService.peekInstance().setPrintJobAttributesNoPersistence(
- mPrintJobId, attributes);
- }
-
private final class ControllerHandler extends Handler {
public static final int MSG_ON_LAYOUT_FINISHED = 1;
public static final int MSG_ON_LAYOUT_FAILED = 2;
@@ -878,7 +822,16 @@ public class PrintJobConfigActivity extends Activity {
}
SpinnerItem<Integer> orientationItem =
mOrientationSpinnerAdapter.getItem(position);
- mCurrPrintAttributes.setOrientation(orientationItem.value);
+ MediaSize mediaSize = mCurrPrintAttributes.getMediaSize();
+ if (orientationItem.value == ORIENTATION_PORTRAIT) {
+ if (!mediaSize.isPortrait()) {
+ mCurrPrintAttributes.setMediaSize(mediaSize.asPortrait());
+ }
+ } else {
+ if (mediaSize.isPortrait()) {
+ mCurrPrintAttributes.setMediaSize(mediaSize.asLandscape());
+ }
+ }
if (!hasErrors()) {
mController.update();
}
@@ -1108,6 +1061,12 @@ public class PrintJobConfigActivity extends Activity {
mOrientationSpinnerAdapter = new ArrayAdapter<SpinnerItem<Integer>>(
PrintJobConfigActivity.this,
R.layout.spinner_dropdown_item, R.id.title);
+ String[] orientationLabels = getResources().getStringArray(
+ R.array.orientation_labels);
+ mOrientationSpinnerAdapter.add(new SpinnerItem<Integer>(
+ ORIENTATION_PORTRAIT, orientationLabels[0]));
+ mOrientationSpinnerAdapter.add(new SpinnerItem<Integer>(
+ ORIENTATION_LANDSCAPE, orientationLabels[1]));
// Range options
mRangeOptionsSpinnerAdapter = new ArrayAdapter<SpinnerItem<Integer>>(
@@ -1604,7 +1563,7 @@ public class PrintJobConfigActivity extends Activity {
oldMediaSizeNewIndex = i;
}
mMediaSizeSpinnerAdapter.add(new SpinnerItem<MediaSize>(
- mediaSize, mediaSize.getLabel()));
+ mediaSize, mediaSize.getLabel(getPackageManager())));
}
if (mediaSizeCount <= 0) {
@@ -1693,68 +1652,7 @@ public class PrintJobConfigActivity extends Activity {
}
mColorModeSpinner.setEnabled(true);
- // Orientation.
- final int orientations = capabilities.getOrientations();
-
- // If the orientations changed, we update the adapter and the spinner.
- boolean orientationsChanged = false;
- if (Integer.bitCount(orientations) != mOrientationSpinnerAdapter.getCount()) {
- orientationsChanged = true;
- } else {
- int remainingOrientations = orientations;
- int adapterIndex = 0;
- while (remainingOrientations != 0) {
- final int orientationBitOffset = Integer.numberOfTrailingZeros(
- remainingOrientations);
- final int orientation = 1 << orientationBitOffset;
- remainingOrientations &= ~orientation;
- if (orientation != mOrientationSpinnerAdapter.getItem(
- adapterIndex).value) {
- orientationsChanged = true;
- break;
- }
- adapterIndex++;
- }
- }
- if (orientationsChanged) {
- // Remember the old orientation to try selecting it again.
- int oldOrientationNewIndex = AdapterView.INVALID_POSITION;
- final int oldOrientation = mCurrPrintAttributes.getOrientation();
-
- mOrientationSpinnerAdapter.clear();
- String[] orientationLabels = getResources().getStringArray(
- R.array.orientation_labels);
- int remainingOrientations = orientations;
- while (remainingOrientations != 0) {
- final int orientationBitOffset = Integer.numberOfTrailingZeros(
- remainingOrientations);
- final int orientation = 1 << orientationBitOffset;
- if (orientation == oldOrientation) {
- // Update the index of the old selection.
- oldOrientationNewIndex = orientationBitOffset;
- }
- remainingOrientations &= ~orientation;
- mOrientationSpinnerAdapter.add(new SpinnerItem<Integer>(orientation,
- orientationLabels[orientationBitOffset]));
- }
- final int orientationCount = Integer.bitCount(orientations);
- if (orientationCount <= 0) {
- mOrientationSpinner.setEnabled(false);
- mOrientationSpinner.setSelection(AdapterView.INVALID_POSITION);
- } else {
- mOrientationSpinner.setEnabled(true);
- if (oldOrientationNewIndex != AdapterView.INVALID_POSITION) {
- // Select the old orientation - nothing really changed.
- setOrientationSpinnerSelectionNoCallback(oldOrientationNewIndex);
- } else {
- final int selectedOrientationIndex = Integer.numberOfTrailingZeros(
- (orientations & defaultAttributes.getOrientation()));
- someAttributeSelectionChanged =
- setOrientationSpinnerSelectionNoCallback(
- selectedOrientationIndex);
- }
- }
- }
+ // Orientation
mOrientationSpinner.setEnabled(true);
// Range options
@@ -1858,15 +1756,6 @@ public class PrintJobConfigActivity extends Activity {
return false;
}
- private boolean setOrientationSpinnerSelectionNoCallback(int position) {
- if (mOrientationSpinner.getSelectedItemPosition() != position) {
- mIgnoreNextOrientationChange = true;
- mOrientationSpinner.setSelection(position);
- return true;
- }
- return false;
- }
-
private void updateUiForNewPrinterCapabilities() {
// The printer changed so we want to start with a clean slate
// for the print options and let them be populated from the
@@ -1881,7 +1770,7 @@ public class PrintJobConfigActivity extends Activity {
}
if (!mOrientationSpinnerAdapter.isEmpty()) {
mIgnoreNextOrientationChange = true;
- mOrientationSpinnerAdapter.clear();
+ mOrientationSpinner.setSelection(0);
}
if (mRangeOptionsSpinner.getSelectedItemPosition() != 0) {
mIgnoreNextRangeOptionChange = true;
@@ -2102,18 +1991,13 @@ public class PrintJobConfigActivity extends Activity {
PrinterCapabilitiesInfo capabilities =
new PrinterCapabilitiesInfo.Builder(printerId)
- .addMediaSize(MediaSize.createMediaSize(getPackageManager(),
- MediaSize.ISO_A4), true)
- .addMediaSize(MediaSize.createMediaSize(getPackageManager(),
- MediaSize.NA_LETTER), false)
+ .addMediaSize(MediaSize.ISO_A4, true)
+ .addMediaSize(MediaSize.NA_LETTER, false)
.addResolution(new Resolution("PDF resolution", "PDF resolution",
300, 300), true)
.setColorModes(PrintAttributes.COLOR_MODE_COLOR
| PrintAttributes.COLOR_MODE_MONOCHROME,
PrintAttributes.COLOR_MODE_COLOR)
- .setOrientations(PrintAttributes.ORIENTATION_PORTRAIT
- | PrintAttributes.ORIENTATION_LANDSCAPE,
- PrintAttributes.ORIENTATION_PORTRAIT)
.create();
return new PrinterInfo.Builder(printerId, getString(R.string.save_as_pdf),
diff --git a/packages/PrintSpooler/src/com/android/printspooler/PrintSpoolerService.java b/packages/PrintSpooler/src/com/android/printspooler/PrintSpoolerService.java
index dd2598c..00c9a68 100644
--- a/packages/PrintSpooler/src/com/android/printspooler/PrintSpoolerService.java
+++ b/packages/PrintSpooler/src/com/android/printspooler/PrintSpoolerService.java
@@ -36,7 +36,6 @@ import android.print.PrintAttributes;
import android.print.PrintAttributes.Margins;
import android.print.PrintAttributes.MediaSize;
import android.print.PrintAttributes.Resolution;
-import android.print.PrintAttributes.Tray;
import android.print.PrintDocumentInfo;
import android.print.PrintJobInfo;
import android.print.PrintManager;
@@ -51,8 +50,6 @@ import com.android.internal.os.HandlerCaller;
import com.android.internal.os.SomeArgs;
import com.android.internal.util.FastXmlSerializer;
-import libcore.io.IoUtils;
-
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;
@@ -65,6 +62,8 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
+import libcore.io.IoUtils;
+
/**
* Service for exposing some of the {@link PrintSpooler} functionality to
* another process.
@@ -676,6 +675,8 @@ public final class PrintSpoolerService extends Service {
private static final String ATTR_ID = "id";
private static final String ATTR_LABEL = "label";
+ private static final String ATTR_LABEL_RES_ID = "labelResId";
+ private static final String ATTR_PACKAGE_NAME = "packageName";
private static final String ATTR_STATE = "state";
private static final String ATTR_APP_ID = "appId";
private static final String ATTR_USER_ID = "userId";
@@ -685,13 +686,8 @@ public final class PrintSpoolerService extends Service {
private static final String TAG_MEDIA_SIZE = "mediaSize";
private static final String TAG_RESOLUTION = "resolution";
private static final String TAG_MARGINS = "margins";
- private static final String TAG_INPUT_TRAY = "inputTray";
- private static final String TAG_OUTPUT_TRAY = "outputTray";
- private static final String ATTR_DUPLEX_MODE = "duplexMode";
private static final String ATTR_COLOR_MODE = "colorMode";
- private static final String ATTR_FITTING_MODE = "fittingMode";
- private static final String ATTR_ORIENTATION = "orientation";
private static final String ATTR_LOCAL_ID = "printerName";
private static final String ATTR_SERVICE_NAME = "serviceName";
@@ -806,32 +802,31 @@ public final class PrintSpoolerService extends Service {
if (attributes != null) {
serializer.startTag(null, TAG_ATTRIBUTES);
- final int duplexMode = attributes.getDuplexMode();
- serializer.attribute(null, ATTR_DUPLEX_MODE,
- String.valueOf(duplexMode));
-
final int colorMode = attributes.getColorMode();
serializer.attribute(null, ATTR_COLOR_MODE,
String.valueOf(colorMode));
- final int fittingMode = attributes.getFittingMode();
- serializer.attribute(null, ATTR_FITTING_MODE,
- String.valueOf(fittingMode));
-
- final int orientation = attributes.getOrientation();
- serializer.attribute(null, ATTR_ORIENTATION,
- String.valueOf(orientation));
-
MediaSize mediaSize = attributes.getMediaSize();
if (mediaSize != null) {
serializer.startTag(null, TAG_MEDIA_SIZE);
serializer.attribute(null, ATTR_ID, mediaSize.getId());
- serializer.attribute(null, ATTR_LABEL, mediaSize.getLabel()
- .toString());
serializer.attribute(null, ATTR_WIDTH_MILS, String.valueOf(
mediaSize.getWidthMils()));
serializer.attribute(null, ATTR_HEIGHT_MILS, String.valueOf(
mediaSize.getHeightMils()));
+ // Store only the platform localized versions of the label
+ // since the resource ids for a print service are not stable
+ // across application versions.
+ if ("android".equals(mediaSize.mPackageName)
+ && mediaSize.mLabelResId > 0) {
+ serializer.attribute(null, ATTR_PACKAGE_NAME,
+ mediaSize.mPackageName);
+ serializer.attribute(null, ATTR_LABEL_RES_ID,
+ String.valueOf(mediaSize.mLabelResId));
+ } else {
+ serializer.attribute(null, ATTR_LABEL,
+ mediaSize.getLabel(getPackageManager()));
+ }
serializer.endTag(null, TAG_MEDIA_SIZE);
}
@@ -839,12 +834,23 @@ public final class PrintSpoolerService extends Service {
if (resolution != null) {
serializer.startTag(null, TAG_RESOLUTION);
serializer.attribute(null, ATTR_ID, resolution.getId());
- serializer.attribute(null, ATTR_LABEL, resolution.getLabel()
- .toString());
serializer.attribute(null, ATTR_HORIZONTAL_DPI, String.valueOf(
resolution.getHorizontalDpi()));
serializer.attribute(null, ATTR_VERTICAL_DPI, String.valueOf(
resolution.getVerticalDpi()));
+ // Store only the platform localized versions of the label
+ // since the resource ids for a print service are not stable
+ // across application versions.
+ if ("android".equals(resolution.mPackageName)
+ && resolution.mLabelResId > 0) {
+ serializer.attribute(null, ATTR_PACKAGE_NAME,
+ resolution.mPackageName);
+ serializer.attribute(null, ATTR_LABEL_RES_ID,
+ String.valueOf(resolution.mLabelResId));
+ } else {
+ serializer.attribute(null, ATTR_LABEL,
+ resolution.getLabel(getPackageManager()));
+ }
serializer.endTag(null, TAG_RESOLUTION);
}
@@ -862,24 +868,6 @@ public final class PrintSpoolerService extends Service {
serializer.endTag(null, TAG_MARGINS);
}
- Tray inputTray = attributes.getInputTray();
- if (inputTray != null) {
- serializer.startTag(null, TAG_INPUT_TRAY);
- serializer.attribute(null, ATTR_ID, inputTray.getId());
- serializer.attribute(null, ATTR_LABEL, inputTray.getLabel()
- .toString());
- serializer.endTag(null, TAG_INPUT_TRAY);
- }
-
- Tray outputTray = attributes.getOutputTray();
- if (outputTray != null) {
- serializer.startTag(null, TAG_OUTPUT_TRAY);
- serializer.attribute(null, ATTR_ID, outputTray.getId());
- serializer.attribute(null, ATTR_LABEL, outputTray.getLabel()
- .toString());
- serializer.endTag(null, TAG_OUTPUT_TRAY);
- }
-
serializer.endTag(null, TAG_ATTRIBUTES);
}
@@ -1026,18 +1014,9 @@ public final class PrintSpoolerService extends Service {
PrintAttributes.Builder builder = new PrintAttributes.Builder();
- String duplexMode = parser.getAttributeValue(null, ATTR_DUPLEX_MODE);
- builder.setDuplexMode(Integer.parseInt(duplexMode));
-
String colorMode = parser.getAttributeValue(null, ATTR_COLOR_MODE);
builder.setColorMode(Integer.parseInt(colorMode));
- String fittingMode = parser.getAttributeValue(null, ATTR_FITTING_MODE);
- builder.setFittingMode(Integer.parseInt(fittingMode));
-
- String orientation = parser.getAttributeValue(null, ATTR_ORIENTATION);
- builder.setOrientation(Integer.parseInt(orientation));
-
parser.next();
skipEmptyTextTags(parser);
@@ -1048,7 +1027,12 @@ public final class PrintSpoolerService extends Service {
ATTR_WIDTH_MILS));
final int heightMils = Integer.parseInt(parser.getAttributeValue(null,
ATTR_HEIGHT_MILS));
- MediaSize mediaSize = new MediaSize(id, label, widthMils, heightMils);
+ String packageName = parser.getAttributeValue(null, ATTR_PACKAGE_NAME);
+ final int labelResId = Integer.parseInt(parser.getAttributeValue(null,
+ ATTR_LABEL_RES_ID));
+ label = parser.getAttributeValue(null, ATTR_LABEL);
+ MediaSize mediaSize = new MediaSize(id, label, packageName, labelResId,
+ widthMils, heightMils);
builder.setMediaSize(mediaSize);
parser.next();
skipEmptyTextTags(parser);
@@ -1064,7 +1048,11 @@ public final class PrintSpoolerService extends Service {
ATTR_HORIZONTAL_DPI));
final int verticalDpi = Integer.parseInt(parser.getAttributeValue(null,
ATTR_VERTICAL_DPI));
- Resolution resolution = new Resolution(id, label, horizontalDpi, verticalDpi);
+ String packageName = parser.getAttributeValue(null, ATTR_PACKAGE_NAME);
+ final int labelResId = Integer.parseInt(
+ parser.getAttributeValue(null, ATTR_LABEL_RES_ID));
+ Resolution resolution = new Resolution(id, label, packageName, labelResId,
+ horizontalDpi, verticalDpi);
builder.setResolution(resolution);
parser.next();
skipEmptyTextTags(parser);
@@ -1090,30 +1078,6 @@ public final class PrintSpoolerService extends Service {
parser.next();
}
- skipEmptyTextTags(parser);
- if (accept(parser, XmlPullParser.START_TAG, TAG_INPUT_TRAY)) {
- String id = parser.getAttributeValue(null, ATTR_ID);
- label = parser.getAttributeValue(null, ATTR_LABEL);
- Tray tray = new Tray(id, label);
- builder.setInputTray(tray);
- parser.next();
- skipEmptyTextTags(parser);
- expect(parser, XmlPullParser.END_TAG, TAG_INPUT_TRAY);
- parser.next();
- }
-
- skipEmptyTextTags(parser);
- if (accept(parser, XmlPullParser.START_TAG, TAG_OUTPUT_TRAY)) {
- String id = parser.getAttributeValue(null, ATTR_ID);
- label = parser.getAttributeValue(null, ATTR_LABEL);
- Tray tray = new Tray(id, label);
- builder.setOutputTray(tray);
- parser.next();
- skipEmptyTextTags(parser);
- expect(parser, XmlPullParser.END_TAG, TAG_OUTPUT_TRAY);
- parser.next();
- }
-
printJob.setAttributes(builder.create());
skipEmptyTextTags(parser);