blob: a51e28b6061498914d9111cbe8bed3adab8bc0f2 [file] [log] [blame]
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.print;
18
19import android.os.Parcel;
20import android.os.Parcelable;
Svetoslav Ganov798bed62013-08-11 12:29:39 -070021import android.text.TextUtils;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070022
23/**
Svetoslav Ganov798bed62013-08-11 12:29:39 -070024 * This class represents the description of a printer.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070025 */
26public final class PrinterInfo implements Parcelable {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070027
Svetoslav Ganovaec14172013-08-27 00:30:54 -070028 /** Printer status: the printer is idle and ready to print. */
29 public static final int STATUS_IDLE = 1;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070030
Svetoslav Ganovaec14172013-08-27 00:30:54 -070031 /** Printer status: the printer is busy printing. */
32 public static final int STATUS_BUSY = 2;
33
34 /** Printer status: the printer is not available. */
35 public static final int STATUS_UNAVAILABLE = 3;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070036
37 private PrinterId mId;
Svetoslav Ganov798bed62013-08-11 12:29:39 -070038
39 private String mName;
40
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070041 private int mStatus;
42
Svetoslav Ganov798bed62013-08-11 12:29:39 -070043 private String mDescription;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070044
Svetoslav Ganov798bed62013-08-11 12:29:39 -070045 private PrinterCapabilitiesInfo mCapabilities;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070046
Svetoslav Ganov798bed62013-08-11 12:29:39 -070047 private PrinterInfo() {
48 /* do nothing */
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070049 }
50
Svetoslav Ganova0027152013-06-25 14:59:53 -070051 private PrinterInfo(PrinterInfo prototype) {
Svetoslav Ganov55b409a2013-07-31 17:25:13 -070052 copyFrom(prototype);
53 }
Svetoslav Ganova0027152013-06-25 14:59:53 -070054
Svetoslav Ganov55b409a2013-07-31 17:25:13 -070055 /**
56 * @hide
57 */
58 public void copyFrom(PrinterInfo other) {
Svetoslav651dd4e2013-09-12 14:37:47 -070059 if (this == other) {
60 return;
61 }
Svetoslav Ganov55b409a2013-07-31 17:25:13 -070062 mId = other.mId;
Svetoslav Ganov798bed62013-08-11 12:29:39 -070063 mName = other.mName;
Svetoslav Ganov55b409a2013-07-31 17:25:13 -070064 mStatus = other.mStatus;
Svetoslav Ganov798bed62013-08-11 12:29:39 -070065 mDescription = other.mDescription;
66 if (other.mCapabilities != null) {
67 if (mCapabilities != null) {
68 mCapabilities.copyFrom(other.mCapabilities);
Svetoslav Ganov55b409a2013-07-31 17:25:13 -070069 } else {
Svetoslav Ganov798bed62013-08-11 12:29:39 -070070 mCapabilities = new PrinterCapabilitiesInfo(other.mCapabilities);
Svetoslav Ganov55b409a2013-07-31 17:25:13 -070071 }
72 } else {
Svetoslav Ganov798bed62013-08-11 12:29:39 -070073 mCapabilities = null;
Svetoslav Ganova0027152013-06-25 14:59:53 -070074 }
Svetoslav Ganova0027152013-06-25 14:59:53 -070075 }
76
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070077 /**
78 * Get the globally unique printer id.
79 *
80 * @return The printer id.
81 */
82 public PrinterId getId() {
83 return mId;
84 }
85
86 /**
Svetoslav Ganov798bed62013-08-11 12:29:39 -070087 * Get the printer name.
88 *
89 * @return The printer name.
90 */
91 public String getName() {
92 return mName;
93 }
94
95 /**
96 * Gets the printer status.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070097 *
98 * @return The status.
99 */
100 public int getStatus() {
101 return mStatus;
102 }
103
104 /**
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700105 * Gets the printer description.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700106 *
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700107 * @return The description.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700108 */
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700109 public String getDescription() {
110 return mDescription;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700111 }
112
113 /**
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700114 * Gets the printer capabilities.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700115 *
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700116 * @return The capabilities.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700117 */
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700118 public PrinterCapabilitiesInfo getCapabilities() {
119 return mCapabilities;
Svetoslav Ganov55b409a2013-07-31 17:25:13 -0700120 }
121
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700122 private PrinterInfo(Parcel parcel) {
123 mId = parcel.readParcelable(null);
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700124 mName = parcel.readString();
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700125 mStatus = parcel.readInt();
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700126 mDescription = parcel.readString();
127 mCapabilities = parcel.readParcelable(null);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700128 }
129
130 @Override
131 public int describeContents() {
132 return 0;
133 }
134
135 @Override
136 public void writeToParcel(Parcel parcel, int flags) {
137 parcel.writeParcelable(mId, flags);
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700138 parcel.writeString(mName);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700139 parcel.writeInt(mStatus);
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700140 parcel.writeString(mDescription);
141 parcel.writeParcelable(mCapabilities, flags);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700142 }
143
144 @Override
Svetoslav Ganov55b409a2013-07-31 17:25:13 -0700145 public int hashCode() {
146 final int prime = 31;
147 int result = 1;
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700148 result = prime * result + ((mId != null) ? mId.hashCode() : 0);
149 result = prime * result + ((mName != null) ? mName.hashCode() : 0);
Svetoslav Ganov55b409a2013-07-31 17:25:13 -0700150 result = prime * result + mStatus;
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700151 result = prime * result + ((mDescription != null) ? mDescription.hashCode() : 0);
152 result = prime * result + ((mCapabilities != null) ? mCapabilities.hashCode() : 0);
Svetoslav Ganov55b409a2013-07-31 17:25:13 -0700153 return result;
154 }
155
156 @Override
157 public boolean equals(Object obj) {
158 if (this == obj) {
159 return true;
160 }
161 if (obj == null) {
162 return false;
163 }
164 if (getClass() != obj.getClass()) {
165 return false;
166 }
167 PrinterInfo other = (PrinterInfo) obj;
168 if (mId == null) {
169 if (other.mId != null) {
170 return false;
171 }
172 } else if (!mId.equals(other.mId)) {
173 return false;
174 }
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700175 if (!TextUtils.equals(mName, other.mName)) {
176 return false;
177 }
Svetoslav Ganov55b409a2013-07-31 17:25:13 -0700178 if (mStatus != other.mStatus) {
179 return false;
180 }
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700181 if (!TextUtils.equals(mDescription, other.mDescription)) {
182 return false;
183 }
184 if (mCapabilities == null) {
185 if (other.mCapabilities != null) {
Svetoslav Ganov55b409a2013-07-31 17:25:13 -0700186 return false;
187 }
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700188 } else if (!mCapabilities.equals(other.mCapabilities)) {
Svetoslav Ganov55b409a2013-07-31 17:25:13 -0700189 return false;
190 }
191 return true;
192 }
193
194 @Override
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700195 public String toString() {
196 StringBuilder builder = new StringBuilder();
197 builder.append("PrinterInfo{");
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700198 builder.append("id=").append(mId);
199 builder.append(", name=").append(mName);
200 builder.append(", status=").append(mStatus);
201 builder.append(", description=").append(mDescription);
202 builder.append(", capabilities=").append(mCapabilities);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700203 builder.append("\"}");
204 return builder.toString();
205 }
206
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700207 /**
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700208 * Builder for creating of a {@link PrinterInfo}.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700209 */
210 public static final class Builder {
Svetoslav Ganova0027152013-06-25 14:59:53 -0700211 private final PrinterInfo mPrototype;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700212
213 /**
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700214 * Constructor.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700215 *
Svetoslav Ganov55b409a2013-07-31 17:25:13 -0700216 * @param printerId The printer id. Cannot be null.
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700217 * @param name The printer name. Cannot be empty.
218 * @param status The printer status. Must be a valid status.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700219 */
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700220 public Builder(PrinterId printerId, String name, int status) {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700221 if (printerId == null) {
222 throw new IllegalArgumentException("printerId cannot be null.");
223 }
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700224 if (TextUtils.isEmpty(name)) {
225 throw new IllegalArgumentException("name cannot be empty.");
226 }
227 if (!isValidStatus(status)) {
228 throw new IllegalArgumentException("status is invalid.");
229 }
Svetoslav Ganova0027152013-06-25 14:59:53 -0700230 mPrototype = new PrinterInfo();
Svetoslav Ganova0027152013-06-25 14:59:53 -0700231 mPrototype.mId = printerId;
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700232 mPrototype.mName = name;
Svetoslav Ganova0027152013-06-25 14:59:53 -0700233 mPrototype.mStatus = status;
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700234 }
235
236 /**
237 * Constructor.
238 *
Svetoslav269403b2013-08-14 17:31:04 -0700239 * @param other Other info from which to start building.
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700240 */
Svetoslav269403b2013-08-14 17:31:04 -0700241 public Builder(PrinterInfo other) {
242 mPrototype = new PrinterInfo();
243 mPrototype.copyFrom(other);
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700244 }
245
246 /**
Svetoslav Ganovaec14172013-08-27 00:30:54 -0700247 * Sets the printer status.
248 *
249 * @param status The status.
250 * @return This builder.
251 *
252 * @see PrinterInfo#STATUS_IDLE
253 * @see PrinterInfo#STATUS_BUSY
254 * @see PrinterInfo#STATUS_UNAVAILABLE
255 */
256 public Builder setStatus(int status) {
257 mPrototype.mStatus = status;
258 return this;
259 }
260
261 /**
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700262 * Sets the printer name.
263 *
264 * @param name The name.
265 * @return This builder.
266 */
267 public Builder setName(String name) {
268 mPrototype.mName = name;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700269 return this;
270 }
271
272 /**
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700273 * Sets the printer description.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700274 *
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700275 * @param description The description.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700276 * @return This builder.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700277 */
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700278 public Builder setDescription(String description) {
279 mPrototype.mDescription = description;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700280 return this;
281 }
282
283 /**
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700284 * Sets the printer capabilities.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700285 *
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700286 * @param capabilities The capabilities.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700287 * @return This builder.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700288 */
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700289 public Builder setCapabilities(PrinterCapabilitiesInfo capabilities) {
290 mPrototype.mCapabilities = capabilities;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700291 return this;
292 }
293
294 /**
Svetoslav Ganov55b409a2013-07-31 17:25:13 -0700295 * Crates a new {@link PrinterInfo}.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700296 *
297 * @return A new {@link PrinterInfo}.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700298 */
Svetoslav651dd4e2013-09-12 14:37:47 -0700299 public PrinterInfo build() {
Svetoslav Ganova0027152013-06-25 14:59:53 -0700300 return new PrinterInfo(mPrototype);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700301 }
302
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700303 private boolean isValidStatus(int status) {
Svetoslav Ganovaec14172013-08-27 00:30:54 -0700304 return (status == STATUS_IDLE
305 || status == STATUS_IDLE
306 || status == STATUS_UNAVAILABLE);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700307 }
308 }
309
310 public static final Parcelable.Creator<PrinterInfo> CREATOR =
311 new Parcelable.Creator<PrinterInfo>() {
312 @Override
313 public PrinterInfo createFromParcel(Parcel parcel) {
314 return new PrinterInfo(parcel);
315 }
316
317 @Override
318 public PrinterInfo[] newArray(int size) {
319 return new PrinterInfo[size];
320 }
321 };
322}