summaryrefslogtreecommitdiffstats
path: root/core/java/android/print
diff options
context:
space:
mode:
Diffstat (limited to 'core/java/android/print')
-rw-r--r--core/java/android/print/IPrintSpoolerClient.aidl6
-rw-r--r--core/java/android/print/IPrinterDiscoverySessionController.aidl (renamed from core/java/android/print/IPrinterDiscoveryObserver.aidl)11
-rw-r--r--core/java/android/print/IPrinterDiscoverySessionObserver.aidl33
-rw-r--r--core/java/android/print/PrintAttributes.java38
-rw-r--r--core/java/android/print/PrintDocumentInfo.java54
-rw-r--r--core/java/android/print/PrintFileDocumentAdapter.java (renamed from core/java/android/print/FileDocumentAdapter.java)30
-rw-r--r--core/java/android/print/PrintJobInfo.java28
-rw-r--r--core/java/android/print/PrintManager.java7
-rw-r--r--core/java/android/print/PrinterCapabilitiesInfo.aidl19
-rw-r--r--core/java/android/print/PrinterCapabilitiesInfo.java972
-rw-r--r--core/java/android/print/PrinterId.java27
-rw-r--r--core/java/android/print/PrinterInfo.java810
12 files changed, 1252 insertions, 783 deletions
diff --git a/core/java/android/print/IPrintSpoolerClient.aidl b/core/java/android/print/IPrintSpoolerClient.aidl
index 46857e4..8db2169 100644
--- a/core/java/android/print/IPrintSpoolerClient.aidl
+++ b/core/java/android/print/IPrintSpoolerClient.aidl
@@ -17,7 +17,7 @@
package android.print;
import android.content.ComponentName;
-import android.print.IPrinterDiscoveryObserver;
+import android.print.IPrinterDiscoverySessionObserver;
import android.print.PrinterId;
import android.print.PrintJobInfo;
@@ -28,10 +28,8 @@ import android.print.PrintJobInfo;
* @hide
*/
oneway interface IPrintSpoolerClient {
+ void createPrinterDiscoverySession(IPrinterDiscoverySessionObserver observer);
void onPrintJobQueued(in PrintJobInfo printJob);
- void onStartPrinterDiscovery(IPrinterDiscoveryObserver observer);
- void onStopPrinterDiscovery();
- void onRequestUpdatePrinters(in List<PrinterId> printerIds);
void onAllPrintJobsForServiceHandled(in ComponentName printService);
void onAllPrintJobsHandled();
}
diff --git a/core/java/android/print/IPrinterDiscoveryObserver.aidl b/core/java/android/print/IPrinterDiscoverySessionController.aidl
index deabbcb..13116ef 100644
--- a/core/java/android/print/IPrinterDiscoveryObserver.aidl
+++ b/core/java/android/print/IPrinterDiscoverySessionController.aidl
@@ -17,15 +17,14 @@
package android.print;
import android.print.PrinterId;
-import android.print.PrinterInfo;
/**
- * Interface for observing printer discovery.
+* Interface for the controlling part of a printer discovery session.
*
* @hide
*/
-oneway interface IPrinterDiscoveryObserver {
- void onPrintersAdded(in List<PrinterInfo> printers);
- void onPrintersRemoved(in List<PrinterId> printers);
- void onPrintersUpdated(in List<PrinterInfo> printers);
+oneway interface IPrinterDiscoverySessionController {
+ void open(in List<PrinterId> priorityList);
+ void requestPrinterUpdate(in PrinterId printerId);
+ void close();
}
diff --git a/core/java/android/print/IPrinterDiscoverySessionObserver.aidl b/core/java/android/print/IPrinterDiscoverySessionObserver.aidl
new file mode 100644
index 0000000..a78924c
--- /dev/null
+++ b/core/java/android/print/IPrinterDiscoverySessionObserver.aidl
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.print;
+
+import android.print.IPrinterDiscoverySessionController;
+import android.print.PrinterId;
+import android.print.PrinterInfo;
+
+/**
+ * Interface for the observing part of a printer discovery session.
+ *
+ * @hide
+ */
+oneway interface IPrinterDiscoverySessionObserver {
+ void setController(IPrinterDiscoverySessionController controller);
+ void onPrintersAdded(in List<PrinterInfo> printers);
+ void onPrintersRemoved(in List<PrinterId> printerIds);
+ void onPrintersUpdated(in List<PrinterInfo> printerIds);
+}
diff --git a/core/java/android/print/PrintAttributes.java b/core/java/android/print/PrintAttributes.java
index 911e380..a902c72 100644
--- a/core/java/android/print/PrintAttributes.java
+++ b/core/java/android/print/PrintAttributes.java
@@ -931,7 +931,7 @@ public final class PrintAttributes implements Parcelable {
}
private final String mId;
- private final CharSequence mLabel;
+ private final String mLabel;
private final int mWidthMils;
private final int mHeightMils;
@@ -948,7 +948,7 @@ public final class PrintAttributes implements Parcelable {
* @throws IllegalArgumentException If the widthMils is less than or equal to zero.
* @throws IllegalArgumentException If the heightMils is less than or equal to zero.
*/
- public MediaSize(String id, CharSequence label, int widthMils, int heightMils) {
+ public MediaSize(String id, String label, int widthMils, int heightMils) {
if (TextUtils.isEmpty(id)) {
throw new IllegalArgumentException("id cannot be empty.");
}
@@ -983,7 +983,7 @@ public final class PrintAttributes implements Parcelable {
*
* @return The human readable label.
*/
- public CharSequence getLabel() {
+ public String getLabel() {
return mLabel;
}
@@ -1007,7 +1007,7 @@ public final class PrintAttributes implements Parcelable {
void writeToParcel(Parcel parcel) {
parcel.writeString(mId);
- parcel.writeCharSequence(mLabel);
+ parcel.writeString(mLabel);
parcel.writeInt(mWidthMils);
parcel.writeInt(mHeightMils);
}
@@ -1015,7 +1015,7 @@ public final class PrintAttributes implements Parcelable {
static MediaSize createFromParcel(Parcel parcel) {
return new MediaSize(
parcel.readString(),
- parcel.readCharSequence(),
+ parcel.readString(),
parcel.readInt(),
parcel.readInt());
}
@@ -1076,7 +1076,7 @@ public final class PrintAttributes implements Parcelable {
*/
public static final class Resolution {
private final String mId;
- private final CharSequence mLabel;
+ private final String mLabel;
private final int mHorizontalDpi;
private final int mVerticalDpi;
@@ -1093,7 +1093,7 @@ public final class PrintAttributes implements Parcelable {
* @throws IllegalArgumentException If the horizontalDpi is less than or equal to zero.
* @throws IllegalArgumentException If the verticalDpi is less than or equal to zero.
*/
- public Resolution(String id, CharSequence label, int horizontalDpi, int verticalDpi) {
+ public Resolution(String id, String label, int horizontalDpi, int verticalDpi) {
if (TextUtils.isEmpty(id)) {
throw new IllegalArgumentException("id cannot be empty.");
}
@@ -1128,7 +1128,7 @@ public final class PrintAttributes implements Parcelable {
*
* @return The human readable label.
*/
- public CharSequence getLabel() {
+ public String getLabel() {
return mLabel;
}
@@ -1152,7 +1152,7 @@ public final class PrintAttributes implements Parcelable {
void writeToParcel(Parcel parcel) {
parcel.writeString(mId);
- parcel.writeCharSequence(mLabel);
+ parcel.writeString(mLabel);
parcel.writeInt(mHorizontalDpi);
parcel.writeInt(mVerticalDpi);
}
@@ -1160,7 +1160,7 @@ public final class PrintAttributes implements Parcelable {
static Resolution createFromParcel(Parcel parcel) {
return new Resolution(
parcel.readString(),
- parcel.readCharSequence(),
+ parcel.readString(),
parcel.readInt(),
parcel.readInt());
}
@@ -1364,7 +1364,7 @@ public final class PrintAttributes implements Parcelable {
*/
public static final class Tray {
private final String mId;
- private final CharSequence mLabel;
+ private final String mLabel;
/**
* Creates a new instance.
@@ -1375,7 +1375,7 @@ public final class PrintAttributes implements Parcelable {
* @throws IllegalArgumentException If the id is empty.
* @throws IllegalArgumentException If the label is empty.
*/
- public Tray(String id, CharSequence label) {
+ public Tray(String id, String label) {
if (TextUtils.isEmpty(id)) {
throw new IllegalArgumentException("id cannot be empty.");
}
@@ -1400,19 +1400,19 @@ public final class PrintAttributes implements Parcelable {
*
* @return The human readable label.
*/
- public CharSequence getLabel() {
+ public String getLabel() {
return mLabel;
}
void writeToParcel(Parcel parcel) {
parcel.writeString(mId);
- parcel.writeCharSequence(mLabel);
+ parcel.writeString(mLabel);
}
static Tray createFromParcel(Parcel parcel) {
return new Tray(
parcel.readString(),
- parcel.readCharSequence());
+ parcel.readString());
}
@Override
@@ -1457,7 +1457,7 @@ public final class PrintAttributes implements Parcelable {
}
}
- private static String duplexModeToString(int duplexMode) {
+ static String duplexModeToString(int duplexMode) {
switch (duplexMode) {
case DUPLEX_MODE_NONE: {
return "DUPLEX_MODE_NONE";
@@ -1473,7 +1473,7 @@ public final class PrintAttributes implements Parcelable {
}
}
- private static String colorModeToString(int colorMode) {
+ static String colorModeToString(int colorMode) {
switch (colorMode) {
case COLOR_MODE_MONOCHROME: {
return "COLOR_MODE_MONOCHROME";
@@ -1486,7 +1486,7 @@ public final class PrintAttributes implements Parcelable {
}
}
- private static String orientationToString(int orientation) {
+ static String orientationToString(int orientation) {
switch (orientation) {
case ORIENTATION_PORTRAIT: {
return "ORIENTATION_PORTRAIT";
@@ -1499,7 +1499,7 @@ public final class PrintAttributes implements Parcelable {
}
}
- private static String fittingModeToString(int fittingMode) {
+ static String fittingModeToString(int fittingMode) {
switch (fittingMode) {
case FITTING_MODE_NONE: {
return "FITTING_MODE_NONE";
diff --git a/core/java/android/print/PrintDocumentInfo.java b/core/java/android/print/PrintDocumentInfo.java
index 29e8e7c..653ad4b 100644
--- a/core/java/android/print/PrintDocumentInfo.java
+++ b/core/java/android/print/PrintDocumentInfo.java
@@ -18,6 +18,7 @@ package android.print;
import android.os.Parcel;
import android.os.Parcelable;
+import android.text.TextUtils;
/**
* This class encapsulates information about a printed document.
@@ -44,6 +45,7 @@ public final class PrintDocumentInfo implements Parcelable {
*/
public static final int CONTENT_TYPE_PHOTO = 1;
+ private String mName;
private int mPageCount;
private int mContentType;
@@ -61,6 +63,7 @@ public final class PrintDocumentInfo implements Parcelable {
* @param Prototype from which to clone.
*/
private PrintDocumentInfo(PrintDocumentInfo prototype) {
+ mName = prototype.mName;
mPageCount = prototype.mPageCount;
mContentType = prototype.mContentType;
}
@@ -71,11 +74,21 @@ public final class PrintDocumentInfo implements Parcelable {
* @param parcel Data from which to initialize.
*/
private PrintDocumentInfo(Parcel parcel) {
+ mName = parcel.readString();
mPageCount = parcel.readInt();
mContentType = parcel.readInt();
}
/**
+ * Gets the document name.
+ *
+ * @return The document name.
+ */
+ public String getName() {
+ return mName;
+ }
+
+ /**
* Gets the total number of pages.
*
* @return The number of pages.
@@ -106,6 +119,7 @@ public final class PrintDocumentInfo implements Parcelable {
@Override
public void writeToParcel(Parcel parcel, int flags) {
+ parcel.writeString(mName);
parcel.writeInt(mPageCount);
parcel.writeInt(mContentType);
}
@@ -114,6 +128,7 @@ public final class PrintDocumentInfo implements Parcelable {
public int hashCode() {
final int prime = 31;
int result = 1;
+ result = prime * result + ((mName != null) ? mName.hashCode() : 0);
result = prime * result + mContentType;
result = prime * result + mPageCount;
return result;
@@ -131,6 +146,9 @@ public final class PrintDocumentInfo implements Parcelable {
return false;
}
PrintDocumentInfo other = (PrintDocumentInfo) obj;
+ if (!TextUtils.equals(mName, other.mName)) {
+ return false;
+ }
if (mContentType != other.mContentType) {
return false;
}
@@ -144,17 +162,47 @@ public final class PrintDocumentInfo implements Parcelable {
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("PrintDocumentInfo{");
- builder.append("pageCount: ").append(mPageCount);
- builder.append(", contentType: ").append(mContentType);
+ builder.append("name=").append(mName);
+ builder.append(", pageCount=").append(mPageCount);
+ builder.append(", contentType=").append(contentTyepToString(mContentType));
builder.append("}");
return builder.toString();
}
+ private String contentTyepToString(int contentType) {
+ switch (contentType) {
+ case CONTENT_TYPE_DOCUMENT: {
+ return "CONTENT_TYPE_DOCUMENT";
+ }
+ case CONTENT_TYPE_PHOTO: {
+ return "CONTENT_TYPE_PHOTO";
+ }
+ default: {
+ return "CONTENT_TYPE_UNKNOWN";
+ }
+ }
+ }
+
/**
* Builder for creating an {@link PrintDocumentInfo}.
*/
public static final class Builder {
- private final PrintDocumentInfo mPrototype = new PrintDocumentInfo();
+ private final PrintDocumentInfo mPrototype;
+
+ /**
+ * Constructor.
+ *
+ * @param name The document name. Cannot be empty.
+ *
+ * @throws IllegalArgumentException If the name is empty.
+ */
+ public Builder(String name) {
+ if (TextUtils.isEmpty(name)) {
+ throw new IllegalArgumentException("name cannot be empty");
+ }
+ mPrototype = new PrintDocumentInfo();
+ mPrototype.mName = name;
+ }
/**
* Sets the total number of pages.
diff --git a/core/java/android/print/FileDocumentAdapter.java b/core/java/android/print/PrintFileDocumentAdapter.java
index d642a61..4503eda 100644
--- a/core/java/android/print/FileDocumentAdapter.java
+++ b/core/java/android/print/PrintFileDocumentAdapter.java
@@ -36,34 +36,48 @@ import java.io.InputStream;
import java.io.OutputStream;
/**
- * Adapter for printing files.
+ * Adapter for printing files. This class could be useful if you
+ * want to print a file and intercept when the system is ready
+ * spooling the data, so you can deleted the file if it is a
+ * temporary one.
*/
-final class FileDocumentAdapter extends PrintDocumentAdapter {
+public final class PrintFileDocumentAdapter extends PrintDocumentAdapter {
- private static final String LOG_TAG = "FileDocumentAdapter";
+ private static final String LOG_TAG = "PrintedFileDocumentAdapter";
private final Context mContext;
private final File mFile;
+ private final PrintDocumentInfo mDocumentInfo;
+
private WriteFileAsyncTask mWriteFileAsyncTask;
- public FileDocumentAdapter(Context context, File file) {
+ /**
+ * Constructor.
+ *
+ * @param context Context for accessing resources.
+ * @param file The file to print.
+ * @param documentInfo The information about the printed file.
+ */
+ public PrintFileDocumentAdapter(Context context, File file,
+ PrintDocumentInfo documentInfo) {
if (file == null) {
throw new IllegalArgumentException("File cannot be null!");
}
+ if (documentInfo == null) {
+ throw new IllegalArgumentException("documentInfo cannot be null!");
+ }
mContext = context;
mFile = file;
+ mDocumentInfo = documentInfo;
}
@Override
public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes,
CancellationSignal cancellationSignal, LayoutResultCallback callback,
Bundle metadata) {
- // TODO: When we have a PDF rendering library we should query the page count.
- PrintDocumentInfo info = new PrintDocumentInfo.Builder()
- .setPageCount(PrintDocumentInfo.PAGE_COUNT_UNKNOWN).create();
- callback.onLayoutFinished(info, false);
+ callback.onLayoutFinished(mDocumentInfo, false);
}
@Override
diff --git a/core/java/android/print/PrintJobInfo.java b/core/java/android/print/PrintJobInfo.java
index 096dcd5..2fb4751 100644
--- a/core/java/android/print/PrintJobInfo.java
+++ b/core/java/android/print/PrintJobInfo.java
@@ -109,6 +109,9 @@ public final class PrintJobInfo implements Parcelable {
/** The unique id of the printer. */
private PrinterId mPrinterId;
+ /** The name of the printer - internally used */
+ private String mPrinterName;
+
/** The status of the print job. */
private int mState;
@@ -146,6 +149,7 @@ public final class PrintJobInfo implements Parcelable {
mId = other.mId;
mLabel = other.mLabel;
mPrinterId = other.mPrinterId;
+ mPrinterName = other.mPrinterName;
mState = other.mState;
mAppId = other.mAppId;
mUserId = other.mUserId;
@@ -161,6 +165,7 @@ public final class PrintJobInfo implements Parcelable {
mId = parcel.readInt();
mLabel = parcel.readCharSequence();
mPrinterId = parcel.readParcelable(null);
+ mPrinterName = parcel.readString();
mState = parcel.readInt();
mAppId = parcel.readInt();
mUserId = parcel.readInt();
@@ -245,6 +250,28 @@ public final class PrintJobInfo implements Parcelable {
}
/**
+ * Gets the name of the target printer.
+ *
+ * @return The printer name.
+ *
+ * @hide
+ */
+ public String getPrinterName() {
+ return mPrinterName;
+ }
+
+ /**
+ * Sets the name of the target printer.
+ *
+ * @param printerName The printer name.
+ *
+ * @hide
+ */
+ public void setPrinterName(String printerName) {
+ mPrinterName = printerName;
+ }
+
+ /**
* Gets the current job state.
*
* @return The job state.
@@ -445,6 +472,7 @@ public final class PrintJobInfo implements Parcelable {
parcel.writeInt(mId);
parcel.writeCharSequence(mLabel);
parcel.writeParcelable(mPrinterId, flags);
+ parcel.writeString(mPrinterName);
parcel.writeInt(mState);
parcel.writeInt(mAppId);
parcel.writeInt(mUserId);
diff --git a/core/java/android/print/PrintManager.java b/core/java/android/print/PrintManager.java
index c067661..636b9d4 100644
--- a/core/java/android/print/PrintManager.java
+++ b/core/java/android/print/PrintManager.java
@@ -161,13 +161,16 @@ public final class PrintManager {
*
* @param printJobName A name for the new print job.
* @param pdfFile The PDF file to print.
+ * @param documentInfo Information about the printed document.
* @param attributes The default print job attributes.
* @return The created print job.
*
* @see PrintJob
*/
- public PrintJob print(String printJobName, File pdfFile, PrintAttributes attributes) {
- FileDocumentAdapter documentAdapter = new FileDocumentAdapter(mContext, pdfFile);
+ public PrintJob print(String printJobName, File pdfFile, PrintDocumentInfo documentInfo,
+ PrintAttributes attributes) {
+ PrintFileDocumentAdapter documentAdapter = new PrintFileDocumentAdapter(
+ mContext, pdfFile, documentInfo);
return print(printJobName, documentAdapter, attributes);
}
diff --git a/core/java/android/print/PrinterCapabilitiesInfo.aidl b/core/java/android/print/PrinterCapabilitiesInfo.aidl
new file mode 100644
index 0000000..0f5fb6b
--- /dev/null
+++ b/core/java/android/print/PrinterCapabilitiesInfo.aidl
@@ -0,0 +1,19 @@
+/**
+ * Copyright (c) 2013, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.print;
+
+parcelable PrinterCapabilitiesInfo;
diff --git a/core/java/android/print/PrinterCapabilitiesInfo.java b/core/java/android/print/PrinterCapabilitiesInfo.java
new file mode 100644
index 0000000..70b418c
--- /dev/null
+++ b/core/java/android/print/PrinterCapabilitiesInfo.java
@@ -0,0 +1,972 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.print;
+
+import android.os.Parcel;
+import android.os.Parcelable;
+import android.print.PrintAttributes.Margins;
+import android.print.PrintAttributes.MediaSize;
+import android.print.PrintAttributes.Resolution;
+import android.print.PrintAttributes.Tray;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * This class represents the capabilities of a printer.
+ */
+public final class PrinterCapabilitiesInfo implements Parcelable {
+ /**
+ * Undefined default value.
+ *
+ * @hide
+ */
+ public static final int DEFAULT_UNDEFINED = -1;
+
+ private static final int PROPERTY_MEDIA_SIZE = 0;
+ private static final int PROPERTY_RESOLUTION = 1;
+ private static final int PROPERTY_INPUT_TRAY = 2;
+ private static final int PROPERTY_OUTPUT_TRAY = 3;
+ private static final int PROPERTY_DUPLEX_MODE = 4;
+ private static final int PROPERTY_COLOR_MODE = 5;
+ private static final int PROPERTY_FITTING_MODE = 6;
+ private static final int PROPERTY_ORIENTATION = 7;
+ private static final int PROPERTY_COUNT = 8;
+
+ private static final Margins DEFAULT_MARGINS = new Margins(0, 0, 0, 0);
+
+ private Margins mMinMargins = DEFAULT_MARGINS;
+ private List<MediaSize> mMediaSizes;
+ private List<Resolution> mResolutions;
+ private List<Tray> mInputTrays;
+ private List<Tray> mOutputTrays;
+
+ private int mDuplexModes;
+ private int mColorModes;
+ private int mFittingModes;
+ private int mOrientations;
+
+ private final int[] mDefaults = new int[PROPERTY_COUNT];
+ private Margins mDefaultMargins = DEFAULT_MARGINS;
+
+ /**
+ * @hide
+ */
+ public PrinterCapabilitiesInfo() {
+ Arrays.fill(mDefaults, DEFAULT_UNDEFINED);
+ }
+
+ /**
+ * @hide
+ */
+ public PrinterCapabilitiesInfo(PrinterCapabilitiesInfo prototype) {
+ copyFrom(prototype);
+ }
+
+ /**
+ * @hide
+ */
+ public void copyFrom(PrinterCapabilitiesInfo other) {
+ mMinMargins = other.mMinMargins;
+
+ if (other.mMediaSizes != null) {
+ if (mMediaSizes != null) {
+ mMediaSizes.clear();
+ mMediaSizes.addAll(other.mMediaSizes);
+ } else {
+ mMediaSizes = new ArrayList<MediaSize>(other.mMediaSizes);
+ }
+ } else {
+ mMediaSizes = null;
+ }
+
+ if (other.mResolutions != null) {
+ if (mResolutions != null) {
+ mResolutions.clear();
+ mResolutions.addAll(other.mResolutions);
+ } else {
+ mResolutions = new ArrayList<Resolution>(other.mResolutions);
+ }
+ } else {
+ mResolutions = null;
+ }
+
+ if (other.mInputTrays != null) {
+ if (mInputTrays != null) {
+ mInputTrays.clear();
+ mInputTrays.addAll(other.mInputTrays);
+ } else {
+ mInputTrays = new ArrayList<Tray>(other.mInputTrays);
+ }
+ } else {
+ mInputTrays = null;
+ }
+
+ if (other.mOutputTrays != null) {
+ if (mOutputTrays != null) {
+ mOutputTrays.clear();
+ mOutputTrays.addAll(other.mOutputTrays);
+ } else {
+ mOutputTrays = new ArrayList<Tray>(other.mOutputTrays);
+ }
+ } else {
+ mOutputTrays = null;
+ }
+
+ mDuplexModes = other.mDuplexModes;
+ mColorModes = other.mColorModes;
+ mFittingModes = other.mFittingModes;
+ mOrientations = other.mOrientations;
+
+ final int defaultCount = other.mDefaults.length;
+ for (int i = 0; i < defaultCount; i++) {
+ mDefaults[i] = other.mDefaults[i];
+ }
+
+ mDefaultMargins = other.mDefaultMargins;
+ }
+
+ /**
+ * Gets the supported media sizes.
+ *
+ * @return The media sizes.
+ */
+ public List<MediaSize> getMediaSizes() {
+ return mMediaSizes;
+ }
+
+ /**
+ * Gets the supported resolutions.
+ *
+ * @return The resolutions.
+ */
+ public List<Resolution> getResolutions() {
+ return mResolutions;
+ }
+
+ /**
+ * Gets the minimal supported margins.
+ *
+ * @return The minimal margins.
+ */
+ public Margins getMinMargins() {
+ return mMinMargins;
+ }
+
+ /**
+ * Gets the available input trays.
+ *
+ * @return The input trays.
+ */
+ public List<Tray> getInputTrays() {
+ return mInputTrays;
+ }
+
+ /**
+ * Gets the available output trays.
+ *
+ * @return The output trays.
+ */
+ public List<Tray> getOutputTrays() {
+ return mOutputTrays;
+ }
+
+ /**
+ * Gets the supported duplex modes.
+ *
+ * @return The duplex modes.
+ *
+ * @see PrintAttributes#DUPLEX_MODE_NONE
+ * @see PrintAttributes#DUPLEX_MODE_LONG_EDGE
+ * @see PrintAttributes#DUPLEX_MODE_SHORT_EDGE
+ */
+ public int getDuplexModes() {
+ return mDuplexModes;
+ }
+
+ /**
+ * Gets the supported color modes.
+ *
+ * @return The color modes.
+ *
+ * @see PrintAttributes#COLOR_MODE_COLOR
+ * @see PrintAttributes#COLOR_MODE_MONOCHROME
+ */
+ public int getColorModes() {
+ return mColorModes;
+ }
+
+ /**
+ * Gets the supported fitting modes.
+ *
+ * @return The fitting modes.
+ *
+ * @see PrintAttributes#FITTING_MODE_NONE
+ * @see PrintAttributes#FITTING_MODE_FIT_TO_PAGE
+ */
+ public int getFittingModes() {
+ return mFittingModes;
+ }
+
+ /**
+ * Gets the supported orientations.
+ *
+ * @return The orientations.
+ *
+ * @see PrintAttributes#ORIENTATION_PORTRAIT
+ * @see PrintAttributes#ORIENTATION_LANDSCAPE
+ */
+ public int getOrientations() {
+ return mOrientations;
+ }
+
+ /**
+ * Gets the default print attributes.
+ *
+ * @param outAttributes The attributes to populated.
+ */
+ public void getDefaults(PrintAttributes outAttributes) {
+ outAttributes.clear();
+
+ outAttributes.setMargins(mDefaultMargins);
+
+ final int mediaSizeIndex = mDefaults[PROPERTY_MEDIA_SIZE];
+ if (mediaSizeIndex >= 0) {
+ outAttributes.setMediaSize(mMediaSizes.get(mediaSizeIndex));
+ }
+
+ final int resolutionIndex = mDefaults[PROPERTY_RESOLUTION];
+ if (resolutionIndex >= 0) {
+ outAttributes.setResolution(mResolutions.get(resolutionIndex));
+ }
+
+ final int inputTrayIndex = mDefaults[PROPERTY_INPUT_TRAY];
+ if (inputTrayIndex >= 0) {
+ outAttributes.setInputTray(mInputTrays.get(inputTrayIndex));
+ }
+
+ final int outputTrayIndex = mDefaults[PROPERTY_OUTPUT_TRAY];
+ if (outputTrayIndex >= 0) {
+ outAttributes.setOutputTray(mOutputTrays.get(outputTrayIndex));
+ }
+
+ final int duplexMode = mDefaults[PROPERTY_DUPLEX_MODE];
+ if (duplexMode > 0) {
+ outAttributes.setDuplexMode(duplexMode);
+ }
+
+ final int colorMode = mDefaults[PROPERTY_COLOR_MODE];
+ if (colorMode > 0) {
+ outAttributes.setColorMode(colorMode);
+ }
+
+ final int fittingMode = mDefaults[PROPERTY_FITTING_MODE];
+ if (fittingMode > 0) {
+ outAttributes.setFittingMode(fittingMode);
+ }
+
+ final int orientation = mDefaults[PROPERTY_ORIENTATION];
+ if (orientation > 0) {
+ outAttributes.setOrientation(orientation);
+ }
+ }
+
+ private PrinterCapabilitiesInfo(Parcel parcel) {
+ mMinMargins = readMargins(parcel);
+ readMediaSizes(parcel);
+ readResolutions(parcel);
+ mInputTrays = readInputTrays(parcel);
+ mOutputTrays = readOutputTrays(parcel);
+
+ mColorModes = parcel.readInt();
+ mDuplexModes = parcel.readInt();
+ mFittingModes = parcel.readInt();
+ mOrientations = parcel.readInt();
+
+ readDefaults(parcel);
+ mDefaultMargins = readMargins(parcel);
+ }
+
+ @Override
+ public int describeContents() {
+ return 0;
+ }
+
+ @Override
+ public void writeToParcel(Parcel parcel, int flags) {
+ writeMargins(mMinMargins, parcel);
+ writeMediaSizes(parcel);
+ writeResolutions(parcel);
+ writeInputTrays(parcel);
+ writeOutputTrays(parcel);
+
+ parcel.writeInt(mColorModes);
+ parcel.writeInt(mDuplexModes);
+ parcel.writeInt(mFittingModes);
+ parcel.writeInt(mOrientations);
+
+ writeDefaults(parcel);
+ writeMargins(mDefaultMargins, parcel);
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((mMinMargins == null) ? 0 : mMinMargins.hashCode());
+ result = prime * result + ((mMediaSizes == null) ? 0 : mMediaSizes.hashCode());
+ result = prime * result + ((mResolutions == null) ? 0 : mResolutions.hashCode());
+ result = prime * result + ((mInputTrays == null) ? 0 : mInputTrays.hashCode());
+ result = prime * result + ((mOutputTrays == null) ? 0 : mOutputTrays.hashCode());
+ result = prime * result + mColorModes;
+ result = prime * result + mDuplexModes;
+ result = prime * result + mFittingModes;
+ result = prime * result + mOrientations;
+ result = prime * result + Arrays.hashCode(mDefaults);
+ result = prime * result + ((mDefaultMargins == null) ? 0 : mDefaultMargins.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (getClass() != obj.getClass()) {
+ return false;
+ }
+ PrinterCapabilitiesInfo other = (PrinterCapabilitiesInfo) obj;
+ if (mMinMargins == null) {
+ if (other.mMinMargins != null) {
+ return false;
+ }
+ } else if (!mMinMargins.equals(other.mMinMargins)) {
+ return false;
+ }
+ if (mMediaSizes == null) {
+ if (other.mMediaSizes != null) {
+ return false;
+ }
+ } else if (!mMediaSizes.equals(other.mMediaSizes)) {
+ return false;
+ }
+ if (mResolutions == null) {
+ if (other.mResolutions != null) {
+ return false;
+ }
+ } else if (!mResolutions.equals(other.mResolutions)) {
+ return false;
+ }
+ if (mInputTrays == null) {
+ if (other.mInputTrays != null) {
+ return false;
+ }
+ } else if (!mInputTrays.equals(other.mInputTrays)) {
+ return false;
+ }
+ if (mOutputTrays == null) {
+ if (other.mOutputTrays != null) {
+ return false;
+ }
+ } else if (!mOutputTrays.equals(other.mOutputTrays)) {
+ return false;
+ }
+ if (mDuplexModes != other.mDuplexModes) {
+ return false;
+ }
+ if (mColorModes != other.mColorModes) {
+ return false;
+ }
+ if (mFittingModes != other.mFittingModes) {
+ return false;
+ }
+ if (mOrientations != other.mOrientations) {
+ return false;
+ }
+ if (!Arrays.equals(mDefaults, other.mDefaults)) {
+ return false;
+ }
+ if (mDefaultMargins == null) {
+ if (other.mDefaultMargins != null) {
+ return false;
+ }
+ } else if (!mDefaultMargins.equals(other.mDefaultMargins)) {
+ return false;
+ }
+ return true;
+ }
+
+ @Override
+ public String toString() {
+ StringBuilder builder = new StringBuilder();
+ builder.append("PrinterInfo{");
+ builder.append("minMargins=").append(mMinMargins);
+ builder.append(", mediaSizes=").append(mMediaSizes);
+ builder.append(", resolutions=").append(mResolutions);
+ builder.append(", inputTrays=").append(mInputTrays);
+ builder.append(", outputTrays=").append(mOutputTrays);
+ builder.append(", duplexModes=").append(duplexModesToString());
+ builder.append(", colorModes=").append(colorModesToString());
+ builder.append(", fittingModes=").append(fittingModesToString());
+ builder.append(", orientations=").append(orientationsToString());
+ builder.append("\"}");
+ return builder.toString();
+ }
+
+ private String duplexModesToString() {
+ StringBuilder builder = new StringBuilder();
+ builder.append('[');
+ int duplexModes = mDuplexModes;
+ while (duplexModes != 0) {
+ final int duplexMode = 1 << Integer.numberOfTrailingZeros(duplexModes);
+ duplexModes &= ~duplexMode;
+ if (builder.length() > 0) {
+ builder.append(", ");
+ }
+ builder.append(PrintAttributes.duplexModeToString(duplexMode));
+ }
+ builder.append(']');
+ return builder.toString();
+ }
+
+ private String colorModesToString() {
+ StringBuilder builder = new StringBuilder();
+ builder.append('[');
+ int colorModes = mColorModes;
+ while (colorModes != 0) {
+ final int colorMode = 1 << Integer.numberOfTrailingZeros(colorModes);
+ colorModes &= ~colorMode;
+ if (builder.length() > 0) {
+ builder.append(", ");
+ }
+ builder.append(PrintAttributes.colorModeToString(colorMode));
+ }
+ builder.append(']');
+ return builder.toString();
+ }
+
+ private String fittingModesToString() {
+ StringBuilder builder = new StringBuilder();
+ builder.append('[');
+ int fittingModes = mFittingModes;
+ while (fittingModes != 0) {
+ final int fittingMode = 1 << Integer.numberOfTrailingZeros(fittingModes);
+ fittingModes &= ~fittingMode;
+ if (builder.length() > 0) {
+ builder.append(", ");
+ }
+ builder.append(PrintAttributes.fittingModeToString(fittingMode));
+ }
+ builder.append(']');
+ return builder.toString();
+ }
+
+ private String orientationsToString() {
+ StringBuilder builder = new StringBuilder();
+ builder.append('[');
+ int orientations = mOrientations;
+ while (orientations != 0) {
+ final int orientation = 1 << Integer.numberOfTrailingZeros(orientations);
+ orientations &= ~orientation;
+ if (builder.length() > 0) {
+ builder.append(", ");
+ }
+ builder.append(PrintAttributes.orientationToString(orientation));
+ }
+ builder.append(']');
+ return builder.toString();
+ }
+
+ private void writeMediaSizes(Parcel parcel) {
+ if (mMediaSizes == null) {
+ parcel.writeInt(0);
+ return;
+ }
+ final int mediaSizeCount = mMediaSizes.size();
+ parcel.writeInt(mediaSizeCount);
+ for (int i = 0; i < mediaSizeCount; i++) {
+ mMediaSizes.get(i).writeToParcel(parcel);
+ }
+ }
+
+ private void readMediaSizes(Parcel parcel) {
+ final int mediaSizeCount = parcel.readInt();
+ if (mediaSizeCount > 0 && mMediaSizes == null) {
+ mMediaSizes = new ArrayList<MediaSize>();
+ }
+ for (int i = 0; i < mediaSizeCount; i++) {
+ mMediaSizes.add(MediaSize.createFromParcel(parcel));
+ }
+ }
+
+ private void writeResolutions(Parcel parcel) {
+ if (mResolutions == null) {
+ parcel.writeInt(0);
+ return;
+ }
+ final int resolutionCount = mResolutions.size();
+ parcel.writeInt(resolutionCount);
+ for (int i = 0; i < resolutionCount; i++) {
+ mResolutions.get(i).writeToParcel(parcel);
+ }
+ }
+
+ private void readResolutions(Parcel parcel) {
+ final int resolutionCount = parcel.readInt();
+ if (resolutionCount > 0 && mResolutions == null) {
+ mResolutions = new ArrayList<Resolution>();
+ }
+ for (int i = 0; i < resolutionCount; i++) {
+ mResolutions.add(Resolution.createFromParcel(parcel));
+ }
+ }
+
+ private void writeMargins(Margins margins, Parcel parcel) {
+ if (margins == null) {
+ parcel.writeInt(0);
+ } else {
+ parcel.writeInt(1);
+ margins.writeToParcel(parcel);
+ }
+ }
+
+ private Margins readMargins(Parcel parcel) {
+ return (parcel.readInt() == 1) ? Margins.createFromParcel(parcel) : null;
+ }
+
+ private void writeInputTrays(Parcel parcel) {
+ if (mInputTrays == null) {
+ parcel.writeInt(0);
+ return;
+ }
+ final int inputTrayCount = mInputTrays.size();
+ parcel.writeInt(inputTrayCount);
+ for (int i = 0; i < inputTrayCount; i++) {
+ mInputTrays.get(i).writeToParcel(parcel);
+ }
+ }
+
+ private List<Tray> readInputTrays(Parcel parcel) {
+ final int inputTrayCount = parcel.readInt();
+ if (inputTrayCount <= 0) {
+ return null;
+ }
+ List<Tray> inputTrays = new ArrayList<Tray>(inputTrayCount);
+ for (int i = 0; i < inputTrayCount; i++) {
+ inputTrays.add(Tray.createFromParcel(parcel));
+ }
+ return inputTrays;
+ }
+
+ private void writeOutputTrays(Parcel parcel) {
+ if (mOutputTrays == null) {
+ parcel.writeInt(0);
+ return;
+ }
+ final int outputTrayCount = mOutputTrays.size();
+ parcel.writeInt(outputTrayCount);
+ for (int i = 0; i < outputTrayCount; i++) {
+ mOutputTrays.get(i).writeToParcel(parcel);
+ }
+ }
+
+ private List<Tray> readOutputTrays(Parcel parcel) {
+ final int outputTrayCount = parcel.readInt();
+ if (outputTrayCount <= 0) {
+ return null;
+ }
+ List<Tray> outputTrays = new ArrayList<Tray>(outputTrayCount);
+ for (int i = 0; i < outputTrayCount; i++) {
+ outputTrays.add(Tray.createFromParcel(parcel));
+ }
+ return outputTrays;
+ }
+
+ private void readDefaults(Parcel parcel) {
+ final int defaultCount = parcel.readInt();
+ for (int i = 0; i < defaultCount; i++) {
+ mDefaults[i] = parcel.readInt();
+ }
+ }
+
+ private void writeDefaults(Parcel parcel) {
+ final int defaultCount = mDefaults.length;
+ parcel.writeInt(defaultCount);
+ for (int i = 0; i < defaultCount; i++) {
+ parcel.writeInt(mDefaults[i]);
+ }
+ }
+
+ /**
+ * Builder for creating of a {@link PrinterInfo}. This class is responsible
+ * to enforce that all required attributes have at least one default value.
+ * In other words, this class creates only well-formed {@link PrinterInfo}s.
+ * <p>
+ * Look at the individual methods for a reference whether a property is
+ * required or if it is optional.
+ * </p>
+ */
+ public static final class Builder {
+ private final PrinterCapabilitiesInfo mPrototype;
+
+ /**
+ * Creates a new instance.
+ *
+ * @param printerId The printer id. Cannot be null.
+ *
+ * @throws IllegalArgumentException If the printer id is null.
+ */
+ public Builder(PrinterId printerId) {
+ if (printerId == null) {
+ throw new IllegalArgumentException("printerId cannot be null.");
+ }
+ mPrototype = new PrinterCapabilitiesInfo();
+ }
+
+ /**
+ * Adds a supported media size.
+ * <p>
+ * <strong>Required:</strong> Yes
+ * </p>
+ *
+ * @param mediaSize A media size.
+ * @param isDefault Whether this is the default.
+ * @return This builder.
+ * @throws IllegalArgumentException If set as default and there
+ * is already a default.
+ *
+ * @see PrintAttributes.MediaSize
+ */
+ public Builder addMediaSize(MediaSize mediaSize, boolean isDefault) {
+ if (mPrototype.mMediaSizes == null) {
+ mPrototype.mMediaSizes = new ArrayList<MediaSize>();
+ }
+ final int insertionIndex = mPrototype.mMediaSizes.size();
+ mPrototype.mMediaSizes.add(mediaSize);
+ if (isDefault) {
+ throwIfDefaultAlreadySpecified(PROPERTY_MEDIA_SIZE);
+ mPrototype.mDefaults[PROPERTY_MEDIA_SIZE] = insertionIndex;
+ }
+ return this;
+ }
+
+ /**
+ * Adds a supported resolution.
+ * <p>
+ * <strong>Required:</strong> Yes
+ * </p>
+ *
+ * @param resolution A resolution.
+ * @param isDefault Whether this is the default.
+ * @return This builder.
+ *
+ * @throws IllegalArgumentException If set as default and there
+ * is already a default.
+ *
+ * @see PrintAttributes.Resolution
+ */
+ public Builder addResolution(Resolution resolution, boolean isDefault) {
+ if (mPrototype.mResolutions == null) {
+ mPrototype.mResolutions = new ArrayList<Resolution>();
+ }
+ final int insertionIndex = mPrototype.mResolutions.size();
+ mPrototype.mResolutions.add(resolution);
+ if (isDefault) {
+ throwIfDefaultAlreadySpecified(PROPERTY_RESOLUTION);
+ mPrototype.mDefaults[PROPERTY_RESOLUTION] = insertionIndex;
+ }
+ return this;
+ }
+
+ /**
+ * Sets the minimal margins.
+ * <p>
+ * <strong>Required:</strong> No
+ * </p>
+ *
+ * @param margins The margins.
+ * @param defaultMargins The default margins.
+ * @return This builder.
+ *
+ * @see PrintAttributes.Margins
+ */
+ public Builder setMinMargins(Margins margins, Margins defaultMargins) {
+ if (margins.getLeftMils() > defaultMargins.getLeftMils()
+ || margins.getTopMils() > defaultMargins.getTopMils()
+ || margins.getRightMils() < defaultMargins.getRightMils()
+ || margins.getBottomMils() < defaultMargins.getBottomMils()) {
+ throw new IllegalArgumentException("Default margins"
+ + " cannot be outside of the min margins.");
+ }
+ mPrototype.mMinMargins = margins;
+ mPrototype.mDefaultMargins = defaultMargins;
+ return this;
+ }
+
+ /**
+ * Adds an input tray.
+ * <p>
+ * <strong>Required:</strong> No
+ * </p>
+ *
+ * @param inputTray A tray.
+ * @param isDefault Whether this is the default.
+ * @return This builder.
+ *
+ * @throws IllegalArgumentException If set as default and there
+ * is already a default.
+ *
+ * @see PrintAttributes.Tray
+ */
+ public Builder addInputTray(Tray inputTray, boolean isDefault) {
+ if (mPrototype.mInputTrays == null) {
+ mPrototype.mInputTrays = new ArrayList<Tray>();
+ }
+ final int insertionIndex = mPrototype.mInputTrays.size();
+ mPrototype.mInputTrays.add(inputTray);
+ if (isDefault) {
+ throwIfDefaultAlreadySpecified(PROPERTY_INPUT_TRAY);
+ mPrototype.mDefaults[PROPERTY_INPUT_TRAY] = insertionIndex;
+ }
+ return this;
+ }
+
+ /**
+ * Adds an output tray.
+ * <p>
+ * <strong>Required:</strong> No
+ * </p>
+ *
+ * @param outputTray A tray.
+ * @param isDefault Whether this is the default.
+ * @return This builder.
+ *
+ * @throws IllegalArgumentException If set as default and there
+ * is already a default.
+ *
+ * @see PrintAttributes.Tray
+ */
+ public Builder addOutputTray(Tray outputTray, boolean isDefault) {
+ if (mPrototype.mOutputTrays == null) {
+ mPrototype.mOutputTrays = new ArrayList<Tray>();
+ }
+ final int insertionIndex = mPrototype.mOutputTrays.size();
+ mPrototype.mOutputTrays.add(outputTray);
+ if (isDefault) {
+ throwIfDefaultAlreadySpecified(PROPERTY_OUTPUT_TRAY);
+ mPrototype.mDefaults[PROPERTY_OUTPUT_TRAY] = insertionIndex;
+ }
+ return this;
+ }
+
+ /**
+ * Sets the color modes.
+ * <p>
+ * <strong>Required:</strong> Yes
+ * </p>
+ *
+ * @param colorModes The color mode bit mask.
+ * @param defaultColorMode The default color mode.
+ * @return This builder.
+ *
+ * @throws IllegalArgumentException If color modes contains an invalid
+ * mode bit or if the default color mode is invalid.
+ *
+ * @see PrintAttributes#COLOR_MODE_COLOR
+ * @see PrintAttributes#COLOR_MODE_MONOCHROME
+ */
+ public Builder setColorModes(int colorModes, int defaultColorMode) {
+ int currentModes = colorModes;
+ while (currentModes > 0) {
+ final int currentMode = (1 << Integer.numberOfTrailingZeros(currentModes));
+ currentModes &= ~currentMode;
+ PrintAttributes.enforceValidColorMode(currentMode);
+ }
+ if ((colorModes & defaultColorMode) == 0) {
+ throw new IllegalArgumentException("Default color mode not in color modes.");
+ }
+ PrintAttributes.enforceValidColorMode(colorModes);
+ mPrototype.mColorModes = colorModes;
+ mPrototype.mDefaults[PROPERTY_COLOR_MODE] = defaultColorMode;
+ return this;
+ }
+
+ /**
+ * Set the duplex modes.
+ * <p>
+ * <strong>Required:</strong> No
+ * </p>
+ *
+ * @param duplexModes The duplex mode bit mask.
+ * @param defaultDuplexMode The default duplex mode.
+ * @return This builder.
+ *
+ * @throws IllegalArgumentException If duplex modes contains an invalid
+ * mode bit or if the default duplex mode is invalid.
+ *
+ * @see PrintAttributes#DUPLEX_MODE_NONE
+ * @see PrintAttributes#DUPLEX_MODE_LONG_EDGE
+ * @see PrintAttributes#DUPLEX_MODE_SHORT_EDGE
+ */
+ public Builder setDuplexModes(int duplexModes, int defaultDuplexMode) {
+ int currentModes = duplexModes;
+ while (currentModes > 0) {
+ final int currentMode = (1 << Integer.numberOfTrailingZeros(currentModes));
+ currentModes &= ~currentMode;
+ PrintAttributes.enforceValidDuplexMode(currentMode);
+ }
+ if ((duplexModes & defaultDuplexMode) == 0) {
+ throw new IllegalArgumentException("Default duplex mode not in duplex modes.");
+ }
+ PrintAttributes.enforceValidDuplexMode(defaultDuplexMode);
+ mPrototype.mDuplexModes = duplexModes;
+ mPrototype.mDefaults[PROPERTY_DUPLEX_MODE] = defaultDuplexMode;
+ return this;
+ }
+
+ /**
+ * Sets the fitting modes.
+ * <p>
+ * <strong>Required:</strong> No
+ * </p>
+ *
+ * @param fittingModes The fitting mode bit mask.
+ * @param defaultFittingMode The default fitting mode.
+ * @return This builder.
+ *
+ * @throws IllegalArgumentException If fitting modes contains an invalid
+ * mode bit or if the default fitting mode is invalid.
+ *
+ * @see PrintAttributes#FITTING_MODE_NONE
+ * @see PrintAttributes#FITTING_MODE_FIT_TO_PAGE
+ */
+ public Builder setFittingModes(int fittingModes, int defaultFittingMode) {
+ int currentModes = fittingModes;
+ while (currentModes > 0) {
+ final int currentMode = (1 << Integer.numberOfTrailingZeros(currentModes));
+ currentModes &= ~currentMode;
+ PrintAttributes.enfoceValidFittingMode(currentMode);
+ }
+ if ((fittingModes & defaultFittingMode) == 0) {
+ throw new IllegalArgumentException("Default fitting mode not in fiting modes.");
+ }
+ PrintAttributes.enfoceValidFittingMode(defaultFittingMode);
+ mPrototype.mFittingModes = fittingModes;
+ mPrototype.mDefaults[PROPERTY_FITTING_MODE] = defaultFittingMode;
+ return this;
+ }
+
+ /**
+ * Sets the orientations.
+ * <p>
+ * <strong>Required:</strong> Yes
+ * </p>
+ *
+ * @param orientations The orientation bit mask.
+ * @param defaultOrientation The default orientation.
+ * @return This builder.
+ *
+ * @throws IllegalArgumentException If orientations contains an invalid
+ * mode bit or if the default orientation is invalid.
+ *
+ * @see PrintAttributes#ORIENTATION_PORTRAIT
+ * @see PrintAttributes#ORIENTATION_LANDSCAPE
+ */
+ public Builder setOrientations(int orientations, int defaultOrientation) {
+ int currentOrientaions = orientations;
+ while (currentOrientaions > 0) {
+ final int currentOrnt = (1 << Integer.numberOfTrailingZeros(currentOrientaions));
+ currentOrientaions &= ~currentOrnt;
+ PrintAttributes.enforceValidOrientation(currentOrnt);
+ }
+ if ((orientations & defaultOrientation) == 0) {
+ throw new IllegalArgumentException("Default orientation not in orientations.");
+ }
+ PrintAttributes.enforceValidOrientation(defaultOrientation);
+ mPrototype.mOrientations = orientations;
+ mPrototype.mDefaults[PROPERTY_ORIENTATION] = defaultOrientation;
+ return this;
+ }
+
+ /**
+ * Crates a new {@link PrinterCapabilitiesInfo} enforcing that all
+ * required properties have need specified. See individual methods
+ * in this class for reference about required attributes.
+ *
+ * @return A new {@link PrinterCapabilitiesInfo}.
+ *
+ * @throws IllegalStateException If a required attribute was not specified.
+ */
+ public PrinterCapabilitiesInfo create() {
+ if (mPrototype.mMediaSizes == null || mPrototype.mMediaSizes.isEmpty()) {
+ throw new IllegalStateException("No media size specified.");
+ }
+ if (mPrototype.mDefaults[PROPERTY_MEDIA_SIZE] == DEFAULT_UNDEFINED) {
+ throw new IllegalStateException("No default media size specified.");
+ }
+ if (mPrototype.mResolutions == null || mPrototype.mResolutions.isEmpty()) {
+ throw new IllegalStateException("No resolution specified.");
+ }
+ if (mPrototype.mDefaults[PROPERTY_RESOLUTION] == DEFAULT_UNDEFINED) {
+ throw new IllegalStateException("No default resolution specified.");
+ }
+ if (mPrototype.mColorModes == 0) {
+ throw new IllegalStateException("No color mode specified.");
+ }
+ if (mPrototype.mDefaults[PROPERTY_COLOR_MODE] == DEFAULT_UNDEFINED) {
+ throw new IllegalStateException("No default color mode specified.");
+ }
+ if (mPrototype.mOrientations == 0) {
+ throw new IllegalStateException("No oprientation specified.");
+ }
+ if (mPrototype.mDefaults[PROPERTY_ORIENTATION] == DEFAULT_UNDEFINED) {
+ throw new IllegalStateException("No default orientation specified.");
+ }
+ if (mPrototype.mMinMargins == null) {
+ mPrototype.mMinMargins = new Margins(0, 0, 0, 0);
+ }
+ if (mPrototype.mDefaultMargins == null) {
+ mPrototype.mDefaultMargins = mPrototype.mMinMargins;
+ }
+ return new PrinterCapabilitiesInfo(mPrototype);
+ }
+
+ private void throwIfDefaultAlreadySpecified(int propertyIndex) {
+ if (mPrototype.mDefaults[propertyIndex] != DEFAULT_UNDEFINED) {
+ throw new IllegalArgumentException("Default already specified.");
+ }
+ }
+ }
+
+ public static final Parcelable.Creator<PrinterCapabilitiesInfo> CREATOR =
+ new Parcelable.Creator<PrinterCapabilitiesInfo>() {
+ @Override
+ public PrinterCapabilitiesInfo createFromParcel(Parcel parcel) {
+ return new PrinterCapabilitiesInfo(parcel);
+ }
+
+ @Override
+ public PrinterCapabilitiesInfo[] newArray(int size) {
+ return new PrinterCapabilitiesInfo[size];
+ }
+ };
+}
+
diff --git a/core/java/android/print/PrinterId.java b/core/java/android/print/PrinterId.java
index 8462736..a3f3b2b 100644
--- a/core/java/android/print/PrinterId.java
+++ b/core/java/android/print/PrinterId.java
@@ -28,24 +28,24 @@ public final class PrinterId implements Parcelable {
private final ComponentName mServiceName;
- private final String mPrinterName;
+ private final String mLocalId;
/**
* Creates a new instance.
*
* @param serviceName The managing print service.
- * @param printerName The unique name within the managing service.
+ * @param localId The locally unique id within the managing service.
*
* @hide
*/
- public PrinterId(ComponentName serviceName, String printerName) {
+ public PrinterId(ComponentName serviceName, String localId) {
mServiceName = serviceName;
- mPrinterName = printerName;
+ mLocalId = localId;
}
private PrinterId(Parcel parcel) {
mServiceName = parcel.readParcelable(null);
- mPrinterName = parcel.readString();
+ mLocalId = parcel.readString();
}
/**
@@ -60,13 +60,13 @@ public final class PrinterId implements Parcelable {
}
/**
- * Gets the name of this printer which is unique in the context
+ * Gets the id of this printer which is unique in the context
* of the print service that manages it.
*
* @return The printer name.
*/
- public String getPrinterName() {
- return mPrinterName;
+ public String getLocalId() {
+ return mLocalId;
}
@Override
@@ -77,7 +77,7 @@ public final class PrinterId implements Parcelable {
@Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeParcelable(mServiceName, flags);
- parcel.writeString(mPrinterName);
+ parcel.writeString(mLocalId);
}
@Override
@@ -99,7 +99,7 @@ public final class PrinterId implements Parcelable {
} else if (!mServiceName.equals(other.mServiceName)) {
return false;
}
- if (!TextUtils.equals(mPrinterName, other.mPrinterName)) {
+ if (!TextUtils.equals(mLocalId, other.mLocalId)) {
return false;
}
return true;
@@ -111,7 +111,7 @@ public final class PrinterId implements Parcelable {
int hashCode = 1;
hashCode = prime * hashCode + ((mServiceName != null)
? mServiceName.hashCode() : 1);
- hashCode = prime * hashCode + mPrinterName.hashCode();
+ hashCode = prime * hashCode + mLocalId.hashCode();
return hashCode;
}
@@ -119,9 +119,8 @@ public final class PrinterId implements Parcelable {
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("PrinterId{");
- builder.append(mServiceName.flattenToString());
- builder.append(":");
- builder.append(mPrinterName);
+ builder.append("serviceName=").append(mServiceName.flattenToString());
+ builder.append(", localId=").append(mLocalId);
builder.append('}');
return builder.toString();
}
diff --git a/core/java/android/print/PrinterInfo.java b/core/java/android/print/PrinterInfo.java
index 10cecca..ac782a8 100644
--- a/core/java/android/print/PrinterInfo.java
+++ b/core/java/android/print/PrinterInfo.java
@@ -18,67 +18,30 @@ package android.print;
import android.os.Parcel;
import android.os.Parcelable;
-import android.print.PrintAttributes.Margins;
-import android.print.PrintAttributes.MediaSize;
-import android.print.PrintAttributes.Resolution;
-import android.print.PrintAttributes.Tray;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
+import android.text.TextUtils;
/**
- * This class represents the description of a printer. A description
- * contains the printer id, human readable name, status, and available
- * options for various printer capabilities, such as media size, etc.
+ * This class represents the description of a printer.
*/
public final class PrinterInfo implements Parcelable {
- /**
- * Undefined default value.
- *
- * @hide
- */
- public static final int DEFAULT_UNDEFINED = -1;
-
- private static final int PROPERTY_MEDIA_SIZE = 0;
- private static final int PROPERTY_RESOLUTION = 1;
- private static final int PROPERTY_INPUT_TRAY = 2;
- private static final int PROPERTY_OUTPUT_TRAY = 3;
- private static final int PROPERTY_DUPLEX_MODE = 4;
- private static final int PROPERTY_COLOR_MODE = 5;
- private static final int PROPERTY_FITTING_MODE = 6;
- private static final int PROPERTY_ORIENTATION = 7;
- private static final int PROPERTY_COUNT = 8;
/** Printer status: the printer is ready to print. */
public static final int STATUS_READY = 1;
- private static final Margins DEFAULT_MARGINS = new Margins(0, 0, 0, 0);
-
// TODO: Add printer status constants.
private PrinterId mId;
- private int mStatus;
- private Margins mMinMargins = DEFAULT_MARGINS;
- private List<MediaSize> mMediaSizes;
- private List<Resolution> mResolutions;
- private List<Tray> mInputTrays;
- private List<Tray> mOutputTrays;
+ private String mName;
- private int mDuplexModes;
- private int mColorModes;
- private int mFittingModes;
- private int mOrientations;
+ private int mStatus;
- private final int[] mDefaults = new int[PROPERTY_COUNT];
- private Margins mDefaultMargins = DEFAULT_MARGINS;
+ private String mDescription;
- /**
- * @hide
- */
- public PrinterInfo() {
- Arrays.fill(mDefaults, DEFAULT_UNDEFINED);
+ private PrinterCapabilitiesInfo mCapabilities;
+
+ private PrinterInfo() {
+ /* do nothing */
}
private PrinterInfo(PrinterInfo prototype) {
@@ -90,63 +53,18 @@ public final class PrinterInfo implements Parcelable {
*/
public void copyFrom(PrinterInfo other) {
mId = other.mId;
+ mName = other.mName;
mStatus = other.mStatus;
-
- mMinMargins = other.mMinMargins;
- if (other.mMediaSizes != null) {
- if (mMediaSizes != null) {
- mMediaSizes.clear();
- mMediaSizes.addAll(other.mMediaSizes);
+ mDescription = other.mDescription;
+ if (other.mCapabilities != null) {
+ if (mCapabilities != null) {
+ mCapabilities.copyFrom(other.mCapabilities);
} else {
- mMediaSizes = new ArrayList<MediaSize>(other.mMediaSizes);
+ mCapabilities = new PrinterCapabilitiesInfo(other.mCapabilities);
}
} else {
- mMediaSizes = null;
+ mCapabilities = null;
}
-
- if (other.mResolutions != null) {
- if (mResolutions != null) {
- mResolutions.clear();
- mResolutions.addAll(other.mResolutions);
- } else {
- mResolutions = new ArrayList<Resolution>(other.mResolutions);
- }
- } else {
- mResolutions = null;
- }
-
- if (other.mInputTrays != null) {
- if (mInputTrays != null) {
- mInputTrays.clear();
- mInputTrays.addAll(other.mInputTrays);
- } else {
- mInputTrays = new ArrayList<Tray>(other.mInputTrays);
- }
- } else {
- mInputTrays = null;
- }
-
- if (other.mOutputTrays != null) {
- if (mOutputTrays != null) {
- mOutputTrays.clear();
- mOutputTrays.addAll(other.mOutputTrays);
- } else {
- mOutputTrays = new ArrayList<Tray>(other.mOutputTrays);
- }
- } else {
- mOutputTrays = null;
- }
-
- mDuplexModes = other.mDuplexModes;
- mColorModes = other.mColorModes;
- mFittingModes = other.mFittingModes;
- mOrientations = other.mOrientations;
-
- final int defaultCount = other.mDefaults.length;
- for (int i = 0; i < defaultCount; i++) {
- mDefaults[i] = other.mDefaults[i];
- }
- mDefaultMargins = other.mDefaultMargins;
}
/**
@@ -159,193 +77,47 @@ public final class PrinterInfo implements Parcelable {
}
/**
- * Gets the status of the printer.
- *
- * @return The status.
- */
- public int getStatus() {
- return mStatus;
- }
-
- /**
- * Gets the supported media sizes.
- *
- * @return The supported media sizes.
- */
- public List<MediaSize> getMediaSizes() {
- return mMediaSizes;
- }
-
- /**
- * Gets the supported resolutions.
- *
- * @return The supported resolutions.
- */
- public List<Resolution> getResolutions() {
- return mResolutions;
- }
-
- /**
- * Gets the minimal supported margins.
- *
- * @return The minimal margins.
- */
- public Margins getMinMargins() {
- return mMinMargins;
- }
-
- /**
- * Gets the available input trays.
- *
- * @return The input trays.
- */
- public List<Tray> getInputTrays() {
- return mInputTrays;
- }
-
- /**
- * Gets the available output trays.
- *
- * @return The output trays.
- */
- public List<Tray> getOutputTrays() {
- return mOutputTrays;
- }
-
- /**
- * Gets the supported duplex modes.
- *
- * @return The duplex modes.
+ * Get the printer name.
*
- * @see PrintAttributes#DUPLEX_MODE_NONE
- * @see PrintAttributes#DUPLEX_MODE_LONG_EDGE
- * @see PrintAttributes#DUPLEX_MODE_SHORT_EDGE
+ * @return The printer name.
*/
- public int getDuplexModes() {
- return mDuplexModes;
+ public String getName() {
+ return mName;
}
/**
- * Gets the supported color modes.
+ * Gets the printer status.
*
- * @return The color modes.
- *
- * @see PrintAttributes#COLOR_MODE_COLOR
- * @see PrintAttributes#COLOR_MODE_MONOCHROME
- */
- public int getColorModes() {
- return mColorModes;
- }
-
- /**
- * Gets the supported fitting modes.
- *
- * @return The fitting modes.
- *
- * @see PrintAttributes#FITTING_MODE_NONE
- * @see PrintAttributes#FITTING_MODE_FIT_TO_PAGE
- */
- public int getFittingModes() {
- return mFittingModes;
- }
-
- /**
- * Gets the supported orientations.
- *
- * @return The orientations.
- *
- * @see PrintAttributes#ORIENTATION_PORTRAIT
- * @see PrintAttributes#ORIENTATION_LANDSCAPE
+ * @return The status.
*/
- public int getOrientations() {
- return mOrientations;
+ public int getStatus() {
+ return mStatus;
}
/**
- * Gets the default print attributes.
+ * Gets the printer description.
*
- * @param outAttributes The attributes to populated.
+ * @return The description.
*/
- public void getDefaults(PrintAttributes outAttributes) {
- outAttributes.clear();
-
- outAttributes.setMargins(mDefaultMargins);
-
- final int mediaSizeIndex = mDefaults[PROPERTY_MEDIA_SIZE];
- if (mediaSizeIndex >= 0) {
- outAttributes.setMediaSize(mMediaSizes.get(mediaSizeIndex));
- }
-
- final int resolutionIndex = mDefaults[PROPERTY_RESOLUTION];
- if (resolutionIndex >= 0) {
- outAttributes.setResolution(mResolutions.get(resolutionIndex));
- }
-
- final int inputTrayIndex = mDefaults[PROPERTY_INPUT_TRAY];
- if (inputTrayIndex >= 0) {
- outAttributes.setInputTray(mInputTrays.get(inputTrayIndex));
- }
-
- final int outputTrayIndex = mDefaults[PROPERTY_OUTPUT_TRAY];
- if (outputTrayIndex >= 0) {
- outAttributes.setOutputTray(mOutputTrays.get(outputTrayIndex));
- }
-
- final int duplexMode = mDefaults[PROPERTY_DUPLEX_MODE];
- if (duplexMode > 0) {
- outAttributes.setDuplexMode(duplexMode);
- }
-
- final int colorMode = mDefaults[PROPERTY_COLOR_MODE];
- if (colorMode > 0) {
- outAttributes.setColorMode(mColorModes & colorMode);
- }
-
- final int fittingMode = mDefaults[PROPERTY_FITTING_MODE];
- if (fittingMode > 0) {
- outAttributes.setFittingMode(fittingMode);
- }
-
- final int orientation = mDefaults[PROPERTY_ORIENTATION];
- if (orientation > 0) {
- outAttributes.setOrientation(orientation);
- }
+ public String getDescription() {
+ return mDescription;
}
/**
- * Gets whether this printer info is fully-populated, i.e. whether
- * all required attributes are specified. See the {@link Builder}
- * documentation for which attributes are required.
+ * Gets the printer capabilities.
*
- * @return Whether this info has all required attributes.
+ * @return The capabilities.
*/
- public boolean hasAllRequiredAttributes() {
- return (mMediaSizes != null && !mMediaSizes.isEmpty()
- && mResolutions != null && !mResolutions.isEmpty()
- && mColorModes != 0 || mOrientations != 0
- && mDefaults[PROPERTY_MEDIA_SIZE] != DEFAULT_UNDEFINED
- && mDefaults[PROPERTY_RESOLUTION] != DEFAULT_UNDEFINED
- && mDefaults[PROPERTY_COLOR_MODE] != DEFAULT_UNDEFINED
- && mDefaults[PROPERTY_ORIENTATION] != DEFAULT_UNDEFINED);
+ public PrinterCapabilitiesInfo getCapabilities() {
+ return mCapabilities;
}
private PrinterInfo(Parcel parcel) {
mId = parcel.readParcelable(null);
+ mName = parcel.readString();
mStatus = parcel.readInt();
-
- mMinMargins = readMargins(parcel);
- readMediaSizes(parcel);
- readResolutions(parcel);
- mInputTrays = readInputTrays(parcel);
- mOutputTrays = readOutputTrays(parcel);
-
- mColorModes = parcel.readInt();
- mDuplexModes = parcel.readInt();
- mFittingModes = parcel.readInt();
- mOrientations = parcel.readInt();
-
- readDefaults(parcel);
- mDefaultMargins = readMargins(parcel);
+ mDescription = parcel.readString();
+ mCapabilities = parcel.readParcelable(null);
}
@Override
@@ -356,40 +128,21 @@ public final class PrinterInfo implements Parcelable {
@Override
public void writeToParcel(Parcel parcel, int flags) {
parcel.writeParcelable(mId, flags);
+ parcel.writeString(mName);
parcel.writeInt(mStatus);
-
- writeMargins(mMinMargins, parcel);
- writeMediaSizes(parcel);
- writeResolutions(parcel);
- writeInputTrays(parcel);
- writeOutputTrays(parcel);
-
- parcel.writeInt(mColorModes);
- parcel.writeInt(mDuplexModes);
- parcel.writeInt(mFittingModes);
- parcel.writeInt(mOrientations);
-
- writeDefaults(parcel);
- writeMargins(mDefaultMargins, parcel);
+ parcel.writeString(mDescription);
+ parcel.writeParcelable(mCapabilities, flags);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
- result = prime * result + ((mId == null) ? 0 : mId.hashCode());
+ result = prime * result + ((mId != null) ? mId.hashCode() : 0);
+ result = prime * result + ((mName != null) ? mName.hashCode() : 0);
result = prime * result + mStatus;
- result = prime * result + ((mMinMargins == null) ? 0 : mMinMargins.hashCode());
- result = prime * result + ((mMediaSizes == null) ? 0 : mMediaSizes.hashCode());
- result = prime * result + ((mResolutions == null) ? 0 : mResolutions.hashCode());
- result = prime * result + ((mInputTrays == null) ? 0 : mInputTrays.hashCode());
- result = prime * result + ((mOutputTrays == null) ? 0 : mOutputTrays.hashCode());
- result = prime * result + mDuplexModes;
- result = prime * result + mColorModes;
- result = prime * result + mFittingModes;
- result = prime * result + mOrientations;
- result = prime * result + Arrays.hashCode(mDefaults);
- result = prime * result + ((mDefaultMargins == null) ? 0 : mDefaultMargins.hashCode());
+ result = prime * result + ((mDescription != null) ? mDescription.hashCode() : 0);
+ result = prime * result + ((mCapabilities != null) ? mCapabilities.hashCode() : 0);
return result;
}
@@ -412,64 +165,20 @@ public final class PrinterInfo implements Parcelable {
} else if (!mId.equals(other.mId)) {
return false;
}
- if (mStatus != other.mStatus) {
- return false;
- }
- if (mMinMargins == null) {
- if (other.mMinMargins != null) {
- return false;
- }
- } else if (!mMinMargins.equals(other.mMinMargins)) {
- return false;
- }
- if (mMediaSizes == null) {
- if (other.mMediaSizes != null) {
- return false;
- }
- } else if (!mMediaSizes.equals(other.mMediaSizes)) {
- return false;
- }
- if (mResolutions == null) {
- if (other.mResolutions != null) {
- return false;
- }
- } else if (!mResolutions.equals(other.mResolutions)) {
- return false;
- }
- if (mInputTrays == null) {
- if (other.mInputTrays != null) {
- return false;
- }
- } else if (!mInputTrays.equals(other.mInputTrays)) {
- return false;
- }
- if (mOutputTrays == null) {
- if (other.mOutputTrays != null) {
- return false;
- }
- } else if (!mOutputTrays.equals(other.mOutputTrays)) {
- return false;
- }
- if (mDuplexModes != other.mDuplexModes) {
- return false;
- }
- if (mColorModes != other.mColorModes) {
+ if (!TextUtils.equals(mName, other.mName)) {
return false;
}
- if (mFittingModes != other.mFittingModes) {
- return false;
- }
- if (mOrientations != other.mOrientations) {
+ if (mStatus != other.mStatus) {
return false;
}
- if (!Arrays.equals(mDefaults, other.mDefaults)) {
+ if (!TextUtils.equals(mDescription, other.mDescription)) {
return false;
}
- if (mDefaultMargins == null) {
- if (other.mDefaultMargins != null) {
+ if (mCapabilities == null) {
+ if (other.mCapabilities != null) {
return false;
}
- } else if (!mDefaultMargins.equals(other.mDefaultMargins)) {
+ } else if (!mCapabilities.equals(other.mCapabilities)) {
return false;
}
return true;
@@ -479,434 +188,83 @@ public final class PrinterInfo implements Parcelable {
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("PrinterInfo{");
- builder.append(mId).append(", \"");
+ builder.append("id=").append(mId);
+ builder.append(", name=").append(mName);
+ builder.append(", status=").append(mStatus);
+ builder.append(", description=").append(mDescription);
+ builder.append(", capabilities=").append(mCapabilities);
builder.append("\"}");
return builder.toString();
}
- private void writeMediaSizes(Parcel parcel) {
- if (mMediaSizes == null) {
- parcel.writeInt(0);
- return;
- }
- final int mediaSizeCount = mMediaSizes.size();
- parcel.writeInt(mediaSizeCount);
- for (int i = 0; i < mediaSizeCount; i++) {
- mMediaSizes.get(i).writeToParcel(parcel);
- }
- }
-
- private void readMediaSizes(Parcel parcel) {
- final int mediaSizeCount = parcel.readInt();
- if (mediaSizeCount > 0 && mMediaSizes == null) {
- mMediaSizes = new ArrayList<MediaSize>();
- }
- for (int i = 0; i < mediaSizeCount; i++) {
- mMediaSizes.add(MediaSize.createFromParcel(parcel));
- }
- }
-
- private void writeResolutions(Parcel parcel) {
- if (mResolutions == null) {
- parcel.writeInt(0);
- return;
- }
- final int resolutionCount = mResolutions.size();
- parcel.writeInt(resolutionCount);
- for (int i = 0; i < resolutionCount; i++) {
- mResolutions.get(i).writeToParcel(parcel);
- }
- }
-
- private void readResolutions(Parcel parcel) {
- final int resolutionCount = parcel.readInt();
- if (resolutionCount > 0 && mResolutions == null) {
- mResolutions = new ArrayList<Resolution>();
- }
- for (int i = 0; i < resolutionCount; i++) {
- mResolutions.add(Resolution.createFromParcel(parcel));
- }
- }
-
- private void writeMargins(Margins margins, Parcel parcel) {
- if (margins == null) {
- parcel.writeInt(0);
- } else {
- parcel.writeInt(1);
- margins.writeToParcel(parcel);
- }
- }
-
- private Margins readMargins(Parcel parcel) {
- return (parcel.readInt() == 1) ? Margins.createFromParcel(parcel) : null;
- }
-
- private void writeInputTrays(Parcel parcel) {
- if (mInputTrays == null) {
- parcel.writeInt(0);
- return;
- }
- final int inputTrayCount = mInputTrays.size();
- parcel.writeInt(inputTrayCount);
- for (int i = 0; i < inputTrayCount; i++) {
- mInputTrays.get(i).writeToParcel(parcel);
- }
- }
-
- private List<Tray> readInputTrays(Parcel parcel) {
- final int inputTrayCount = parcel.readInt();
- if (inputTrayCount <= 0) {
- return null;
- }
- List<Tray> inputTrays = new ArrayList<Tray>(inputTrayCount);
- for (int i = 0; i < inputTrayCount; i++) {
- inputTrays.add(Tray.createFromParcel(parcel));
- }
- return inputTrays;
- }
-
- private void writeOutputTrays(Parcel parcel) {
- if (mOutputTrays == null) {
- parcel.writeInt(0);
- return;
- }
- final int outputTrayCount = mOutputTrays.size();
- parcel.writeInt(outputTrayCount);
- for (int i = 0; i < outputTrayCount; i++) {
- mOutputTrays.get(i).writeToParcel(parcel);
- }
- }
-
- private List<Tray> readOutputTrays(Parcel parcel) {
- final int outputTrayCount = parcel.readInt();
- if (outputTrayCount <= 0) {
- return null;
- }
- List<Tray> outputTrays = new ArrayList<Tray>(outputTrayCount);
- for (int i = 0; i < outputTrayCount; i++) {
- outputTrays.add(Tray.createFromParcel(parcel));
- }
- return outputTrays;
- }
-
- private void readDefaults(Parcel parcel) {
- final int defaultCount = parcel.readInt();
- for (int i = 0; i < defaultCount; i++) {
- mDefaults[i] = parcel.readInt();
- }
- }
-
- private void writeDefaults(Parcel parcel) {
- final int defaultCount = mDefaults.length;
- parcel.writeInt(defaultCount);
- for (int i = 0; i < defaultCount; i++) {
- parcel.writeInt(mDefaults[i]);
- }
- }
-
/**
- * Builder for creating of a {@link PrinterInfo}. This class is responsible
- * to enforce that all required attributes have at least one default value.
- * In other words, this class creates only well-formed {@link PrinterInfo}s.
- * <p>
- * Look at the individual methods for a reference whether a property is
- * required or if it is optional.
- * </p>
+ * Builder for creating of a {@link PrinterInfo}.
*/
public static final class Builder {
private final PrinterInfo mPrototype;
/**
- * Creates a new instance.
+ * Constructor.
*
* @param printerId The printer id. Cannot be null.
- *
- * @throws IllegalArgumentException If the printer id is null.
+ * @param name The printer name. Cannot be empty.
+ * @param status The printer status. Must be a valid status.
*/
- public Builder(PrinterId printerId) {
+ public Builder(PrinterId printerId, String name, int status) {
if (printerId == null) {
throw new IllegalArgumentException("printerId cannot be null.");
}
+ if (TextUtils.isEmpty(name)) {
+ throw new IllegalArgumentException("name cannot be empty.");
+ }
+ if (!isValidStatus(status)) {
+ throw new IllegalArgumentException("status is invalid.");
+ }
mPrototype = new PrinterInfo();
mPrototype.mId = printerId;
- }
-
- /**
- * Sets the printer status.
- * <p>
- * <strong>Required:</strong> Yes
- * </p>
- *
- * @param status The status.
- * @return This builder.
- */
- public Builder setStatus(int status) {
+ mPrototype.mName = name;
mPrototype.mStatus = status;
- return this;
- }
-
- /**
- * Adds a supported media size.
- * <p>
- * <strong>Required:</strong> Yes
- * </p>
- *
- * @param mediaSize A media size.
- * @param isDefault Whether this is the default.
- * @return This builder.
- * @throws IllegalArgumentException If set as default and there
- * is already a default.
- *
- * @see PrintAttributes.MediaSize
- */
- public Builder addMediaSize(MediaSize mediaSize, boolean isDefault) {
- if (mPrototype.mMediaSizes == null) {
- mPrototype.mMediaSizes = new ArrayList<MediaSize>();
- }
- final int insertionIndex = mPrototype.mMediaSizes.size();
- mPrototype.mMediaSizes.add(mediaSize);
- if (isDefault) {
- throwIfDefaultAlreadySpecified(PROPERTY_MEDIA_SIZE);
- mPrototype.mDefaults[PROPERTY_MEDIA_SIZE] = insertionIndex;
- }
- return this;
- }
-
- /**
- * Adds a supported resolution.
- * <p>
- * <strong>Required:</strong> Yes
- * </p>
- *
- * @param resolution A resolution.
- * @param isDefault Whether this is the default.
- * @return This builder.
- *
- * @throws IllegalArgumentException If set as default and there
- * is already a default.
- *
- * @see PrintAttributes.Resolution
- */
- public Builder addResolution(Resolution resolution, boolean isDefault) {
- if (mPrototype.mResolutions == null) {
- mPrototype.mResolutions = new ArrayList<Resolution>();
- }
- final int insertionIndex = mPrototype.mResolutions.size();
- mPrototype.mResolutions.add(resolution);
- if (isDefault) {
- throwIfDefaultAlreadySpecified(PROPERTY_RESOLUTION);
- mPrototype.mDefaults[PROPERTY_RESOLUTION] = insertionIndex;
- }
- return this;
- }
-
- /**
- * Sets the minimal margins.
- * <p>
- * <strong>Required:</strong> No
- * </p>
- *
- * @param margins The margins.
- * @param defaultMargins The default margins.
- * @return This builder.
- *
- * @see PrintAttributes.Margins
- */
- public Builder setMinMargins(Margins margins, Margins defaultMargins) {
- if (margins.getLeftMils() > defaultMargins.getLeftMils()
- || margins.getTopMils() > defaultMargins.getTopMils()
- || margins.getRightMils() < defaultMargins.getRightMils()
- || margins.getBottomMils() < defaultMargins.getBottomMils()) {
- throw new IllegalArgumentException("Default margins"
- + " cannot be outside of the min margins.");
- }
- mPrototype.mMinMargins = margins;
- mPrototype.mDefaultMargins = defaultMargins;
- return this;
- }
-
- /**
- * Adds an input tray.
- * <p>
- * <strong>Required:</strong> No
- * </p>
- *
- * @param inputTray A tray.
- * @param isDefault Whether this is the default.
- * @return This builder.
- *
- * @throws IllegalArgumentException If set as default and there
- * is already a default.
- *
- * @see PrintAttributes.Tray
- */
- public Builder addInputTray(Tray inputTray, boolean isDefault) {
- if (mPrototype.mInputTrays == null) {
- mPrototype.mInputTrays = new ArrayList<Tray>();
- }
- final int insertionIndex = mPrototype.mInputTrays.size();
- mPrototype.mInputTrays.add(inputTray);
- if (isDefault) {
- throwIfDefaultAlreadySpecified(PROPERTY_INPUT_TRAY);
- mPrototype.mDefaults[PROPERTY_INPUT_TRAY] = insertionIndex;
- }
- return this;
- }
-
- /**
- * Adds an output tray.
- * <p>
- * <strong>Required:</strong> No
- * </p>
- *
- * @param outputTray A tray.
- * @param isDefault Whether this is the default.
- * @return This builder.
- *
- * @throws IllegalArgumentException If set as default and there
- * is already a default.
- *
- * @see PrintAttributes.Tray
- */
- public Builder addOutputTray(Tray outputTray, boolean isDefault) {
- if (mPrototype.mOutputTrays == null) {
- mPrototype.mOutputTrays = new ArrayList<Tray>();
- }
- final int insertionIndex = mPrototype.mOutputTrays.size();
- mPrototype.mOutputTrays.add(outputTray);
- if (isDefault) {
- throwIfDefaultAlreadySpecified(PROPERTY_OUTPUT_TRAY);
- mPrototype.mDefaults[PROPERTY_OUTPUT_TRAY] = insertionIndex;
- }
- return this;
}
/**
- * Sets the color modes.
- * <p>
- * <strong>Required:</strong> Yes
- * </p>
- *
- * @param colorModes The color mode bit mask.
- * @param defaultColorMode The default color mode.
- * @return This builder.
- *
- * @throws IllegalArgumentException If color modes contains an invalid
- * mode bit or if the default color mode is invalid.
+ * Constructor.
*
- * @see PrintAttributes#COLOR_MODE_COLOR
- * @see PrintAttributes#COLOR_MODE_MONOCHROME
+ * @param prototype Prototype from which to start building.
*/
- public Builder setColorModes(int colorModes, int defaultColorMode) {
- int currentModes = colorModes;
- while (currentModes > 0) {
- final int currentMode = (1 << Integer.numberOfTrailingZeros(currentModes));
- currentModes &= ~currentMode;
- PrintAttributes.enforceValidColorMode(currentMode);
- }
- if ((colorModes & defaultColorMode) == 0) {
- throw new IllegalArgumentException("Default color mode not in color modes.");
- }
- PrintAttributes.enforceValidColorMode(colorModes);
- mPrototype.mColorModes = colorModes;
- mPrototype.mDefaults[PROPERTY_COLOR_MODE] = defaultColorMode;
- return this;
+ public Builder(PrinterInfo prototype) {
+ mPrototype = prototype;
}
/**
- * Set the duplex modes.
- * <p>
- * <strong>Required:</strong> No
- * </p>
+ * Sets the printer name.
*
- * @param duplexModes The duplex mode bit mask.
- * @param defaultDuplexMode The default duplex mode.
+ * @param name The name.
* @return This builder.
- *
- * @throws IllegalArgumentException If duplex modes contains an invalid
- * mode bit or if the default duplex mode is invalid.
- *
- * @see PrintAttributes#DUPLEX_MODE_NONE
- * @see PrintAttributes#DUPLEX_MODE_LONG_EDGE
- * @see PrintAttributes#DUPLEX_MODE_SHORT_EDGE
*/
- public Builder setDuplexModes(int duplexModes, int defaultDuplexMode) {
- int currentModes = duplexModes;
- while (currentModes > 0) {
- final int currentMode = (1 << Integer.numberOfTrailingZeros(currentModes));
- currentModes &= ~currentMode;
- PrintAttributes.enforceValidDuplexMode(currentMode);
- }
- if ((duplexModes & defaultDuplexMode) == 0) {
- throw new IllegalArgumentException("Default duplex mode not in duplex modes.");
- }
- PrintAttributes.enforceValidDuplexMode(defaultDuplexMode);
- mPrototype.mDuplexModes = duplexModes;
- mPrototype.mDefaults[PROPERTY_DUPLEX_MODE] = defaultDuplexMode;
+ public Builder setName(String name) {
+ mPrototype.mName = name;
return this;
}
/**
- * Sets the fitting modes.
- * <p>
- * <strong>Required:</strong> No
- * </p>
+ * Sets the printer description.
*
- * @param fittingModes The fitting mode bit mask.
- * @param defaultFittingMode The default fitting mode.
+ * @param description The description.
* @return This builder.
- *
- * @throws IllegalArgumentException If fitting modes contains an invalid
- * mode bit or if the default fitting mode is invalid.
- *
- * @see PrintAttributes#FITTING_MODE_NONE
- * @see PrintAttributes#FITTING_MODE_FIT_TO_PAGE
*/
- public Builder setFittingModes(int fittingModes, int defaultFittingMode) {
- int currentModes = fittingModes;
- while (currentModes > 0) {
- final int currentMode = (1 << Integer.numberOfTrailingZeros(currentModes));
- currentModes &= ~currentMode;
- PrintAttributes.enfoceValidFittingMode(currentMode);
- }
- if ((fittingModes & defaultFittingMode) == 0) {
- throw new IllegalArgumentException("Default fitting mode not in fiting modes.");
- }
- PrintAttributes.enfoceValidFittingMode(defaultFittingMode);
- mPrototype.mFittingModes = fittingModes;
- mPrototype.mDefaults[PROPERTY_FITTING_MODE] = defaultFittingMode;
+ public Builder setDescription(String description) {
+ mPrototype.mDescription = description;
return this;
}
/**
- * Sets the orientations.
- * <p>
- * <strong>Required:</strong> Yes
- * </p>
+ * Sets the printer capabilities.
*
- * @param orientations The orientation bit mask.
- * @param defaultOrientation The default orientation.
+ * @param capabilities The capabilities.
* @return This builder.
- *
- * @throws IllegalArgumentException If orientations contains an invalid
- * mode bit or if the default orientation is invalid.
- *
- * @see PrintAttributes#ORIENTATION_PORTRAIT
- * @see PrintAttributes#ORIENTATION_LANDSCAPE
*/
- public Builder setOrientations(int orientations, int defaultOrientation) {
- int currentOrientaions = orientations;
- while (currentOrientaions > 0) {
- final int currentOrnt = (1 << Integer.numberOfTrailingZeros(currentOrientaions));
- currentOrientaions &= ~currentOrnt;
- PrintAttributes.enforceValidOrientation(currentOrnt);
- }
- if ((orientations & defaultOrientation) == 0) {
- throw new IllegalArgumentException("Default orientation not in orientations.");
- }
- PrintAttributes.enforceValidOrientation(defaultOrientation);
- mPrototype.mOrientations = orientations;
- mPrototype.mDefaults[PROPERTY_ORIENTATION] = defaultOrientation;
+ public Builder setCapabilities(PrinterCapabilitiesInfo capabilities) {
+ mPrototype.mCapabilities = capabilities;
return this;
}
@@ -919,10 +277,8 @@ public final class PrinterInfo implements Parcelable {
return new PrinterInfo(mPrototype);
}
- private void throwIfDefaultAlreadySpecified(int propertyIndex) {
- if (mPrototype.mDefaults[propertyIndex] != DEFAULT_UNDEFINED) {
- throw new IllegalArgumentException("Default already specified.");
- }
+ private boolean isValidStatus(int status) {
+ return (status == PrinterInfo.STATUS_READY);
}
}