1 /* 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 17 package android.print; 18 19 import android.os.Parcel; 20 import android.os.Parcelable; 21 import android.text.TextUtils; 22 23 /** 24 * This class represents the description of a printer. Instances of 25 * this class are created by print services to report to the system 26 * the printers they manage. The information of this class has two 27 * major components, printer properties such as name, id, status, 28 * description and printer capabilities which describe the various 29 * print modes a printer supports such as media sizes, margins, etc. 30 */ 31 public final class PrinterInfo implements Parcelable { 32 33 /** Printer status: the printer is idle and ready to print. */ 34 public static final int STATUS_IDLE = 1; 35 36 /** Printer status: the printer is busy printing. */ 37 public static final int STATUS_BUSY = 2; 38 39 /** Printer status: the printer is not available. */ 40 public static final int STATUS_UNAVAILABLE = 3; 41 42 private PrinterId mId; 43 44 private String mName; 45 46 private int mStatus; 47 48 private String mDescription; 49 50 private PrinterCapabilitiesInfo mCapabilities; 51 PrinterInfo()52 private PrinterInfo() { 53 /* do nothing */ 54 } 55 PrinterInfo(PrinterInfo prototype)56 private PrinterInfo(PrinterInfo prototype) { 57 copyFrom(prototype); 58 } 59 60 /** 61 * @hide 62 */ copyFrom(PrinterInfo other)63 public void copyFrom(PrinterInfo other) { 64 if (this == other) { 65 return; 66 } 67 mId = other.mId; 68 mName = other.mName; 69 mStatus = other.mStatus; 70 mDescription = other.mDescription; 71 if (other.mCapabilities != null) { 72 if (mCapabilities != null) { 73 mCapabilities.copyFrom(other.mCapabilities); 74 } else { 75 mCapabilities = new PrinterCapabilitiesInfo(other.mCapabilities); 76 } 77 } else { 78 mCapabilities = null; 79 } 80 } 81 82 /** 83 * Get the globally unique printer id. 84 * 85 * @return The printer id. 86 */ getId()87 public PrinterId getId() { 88 return mId; 89 } 90 91 /** 92 * Get the printer name. 93 * 94 * @return The printer name. 95 */ getName()96 public String getName() { 97 return mName; 98 } 99 100 /** 101 * Gets the printer status. 102 * 103 * @return The status. 104 * 105 * @see #STATUS_BUSY 106 * @see #STATUS_IDLE 107 * @see #STATUS_UNAVAILABLE 108 */ getStatus()109 public int getStatus() { 110 return mStatus; 111 } 112 113 /** 114 * Gets the printer description. 115 * 116 * @return The description. 117 */ getDescription()118 public String getDescription() { 119 return mDescription; 120 } 121 122 /** 123 * Gets the printer capabilities. 124 * 125 * @return The capabilities. 126 */ getCapabilities()127 public PrinterCapabilitiesInfo getCapabilities() { 128 return mCapabilities; 129 } 130 PrinterInfo(Parcel parcel)131 private PrinterInfo(Parcel parcel) { 132 mId = parcel.readParcelable(null); 133 mName = parcel.readString(); 134 mStatus = parcel.readInt(); 135 mDescription = parcel.readString(); 136 mCapabilities = parcel.readParcelable(null); 137 } 138 139 @Override describeContents()140 public int describeContents() { 141 return 0; 142 } 143 144 @Override writeToParcel(Parcel parcel, int flags)145 public void writeToParcel(Parcel parcel, int flags) { 146 parcel.writeParcelable(mId, flags); 147 parcel.writeString(mName); 148 parcel.writeInt(mStatus); 149 parcel.writeString(mDescription); 150 parcel.writeParcelable(mCapabilities, flags); 151 } 152 153 @Override hashCode()154 public int hashCode() { 155 final int prime = 31; 156 int result = 1; 157 result = prime * result + ((mId != null) ? mId.hashCode() : 0); 158 result = prime * result + ((mName != null) ? mName.hashCode() : 0); 159 result = prime * result + mStatus; 160 result = prime * result + ((mDescription != null) ? mDescription.hashCode() : 0); 161 result = prime * result + ((mCapabilities != null) ? mCapabilities.hashCode() : 0); 162 return result; 163 } 164 165 @Override equals(Object obj)166 public boolean equals(Object obj) { 167 if (this == obj) { 168 return true; 169 } 170 if (obj == null) { 171 return false; 172 } 173 if (getClass() != obj.getClass()) { 174 return false; 175 } 176 PrinterInfo other = (PrinterInfo) obj; 177 if (mId == null) { 178 if (other.mId != null) { 179 return false; 180 } 181 } else if (!mId.equals(other.mId)) { 182 return false; 183 } 184 if (!TextUtils.equals(mName, other.mName)) { 185 return false; 186 } 187 if (mStatus != other.mStatus) { 188 return false; 189 } 190 if (!TextUtils.equals(mDescription, other.mDescription)) { 191 return false; 192 } 193 if (mCapabilities == null) { 194 if (other.mCapabilities != null) { 195 return false; 196 } 197 } else if (!mCapabilities.equals(other.mCapabilities)) { 198 return false; 199 } 200 return true; 201 } 202 203 @Override toString()204 public String toString() { 205 StringBuilder builder = new StringBuilder(); 206 builder.append("PrinterInfo{"); 207 builder.append("id=").append(mId); 208 builder.append(", name=").append(mName); 209 builder.append(", status=").append(mStatus); 210 builder.append(", description=").append(mDescription); 211 builder.append(", capabilities=").append(mCapabilities); 212 builder.append("\"}"); 213 return builder.toString(); 214 } 215 216 /** 217 * Builder for creating of a {@link PrinterInfo}. 218 */ 219 public static final class Builder { 220 private final PrinterInfo mPrototype; 221 222 /** 223 * Constructor. 224 * 225 * @param printerId The printer id. Cannot be null. 226 * @param name The printer name. Cannot be empty. 227 * @param status The printer status. Must be a valid status. 228 * @throws IllegalArgumentException If the printer id is null, or the 229 * printer name is empty or the status is not a valid one. 230 */ Builder(PrinterId printerId, String name, int status)231 public Builder(PrinterId printerId, String name, int status) { 232 if (printerId == null) { 233 throw new IllegalArgumentException("printerId cannot be null."); 234 } 235 if (TextUtils.isEmpty(name)) { 236 throw new IllegalArgumentException("name cannot be empty."); 237 } 238 if (!isValidStatus(status)) { 239 throw new IllegalArgumentException("status is invalid."); 240 } 241 mPrototype = new PrinterInfo(); 242 mPrototype.mId = printerId; 243 mPrototype.mName = name; 244 mPrototype.mStatus = status; 245 } 246 247 /** 248 * Constructor. 249 * 250 * @param other Other info from which to start building. 251 */ Builder(PrinterInfo other)252 public Builder(PrinterInfo other) { 253 mPrototype = new PrinterInfo(); 254 mPrototype.copyFrom(other); 255 } 256 257 /** 258 * Sets the printer status. 259 * 260 * @param status The status. 261 * @return This builder. 262 * 263 * @see PrinterInfo#STATUS_IDLE 264 * @see PrinterInfo#STATUS_BUSY 265 * @see PrinterInfo#STATUS_UNAVAILABLE 266 */ setStatus(int status)267 public Builder setStatus(int status) { 268 mPrototype.mStatus = status; 269 return this; 270 } 271 272 /** 273 * Sets the <strong>localized</strong> printer name which 274 * is shown to the user 275 * 276 * @param name The name. 277 * @return This builder. 278 */ setName(String name)279 public Builder setName(String name) { 280 mPrototype.mName = name; 281 return this; 282 } 283 284 /** 285 * Sets the <strong>localized</strong> printer description 286 * which is shown to the user 287 * 288 * @param description The description. 289 * @return This builder. 290 */ setDescription(String description)291 public Builder setDescription(String description) { 292 mPrototype.mDescription = description; 293 return this; 294 } 295 296 /** 297 * Sets the printer capabilities. 298 * 299 * @param capabilities The capabilities. 300 * @return This builder. 301 */ setCapabilities(PrinterCapabilitiesInfo capabilities)302 public Builder setCapabilities(PrinterCapabilitiesInfo capabilities) { 303 mPrototype.mCapabilities = capabilities; 304 return this; 305 } 306 307 /** 308 * Creates a new {@link PrinterInfo}. 309 * 310 * @return A new {@link PrinterInfo}. 311 */ build()312 public PrinterInfo build() { 313 return mPrototype; 314 } 315 isValidStatus(int status)316 private boolean isValidStatus(int status) { 317 return (status == STATUS_IDLE 318 || status == STATUS_BUSY 319 || status == STATUS_UNAVAILABLE); 320 } 321 } 322 323 public static final Parcelable.Creator<PrinterInfo> CREATOR = 324 new Parcelable.Creator<PrinterInfo>() { 325 @Override 326 public PrinterInfo createFromParcel(Parcel parcel) { 327 return new PrinterInfo(parcel); 328 } 329 330 @Override 331 public PrinterInfo[] newArray(int size) { 332 return new PrinterInfo[size]; 333 } 334 }; 335 } 336