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