| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.print; |
| 18 |
|
| 19 |
import android.annotation.IntDef; |
| 20 |
import android.annotation.IntRange; |
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.annotation.Nullable; |
| 23 |
import android.os.Parcel; |
| 24 |
import android.os.Parcelable; |
| 25 |
import android.text.TextUtils; |
| 26 |
|
| 27 |
import com.android.internal.util.Preconditions; |
| 28 |
|
| 29 |
import java.lang.annotation.Retention; |
| 30 |
import java.lang.annotation.RetentionPolicy; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public final class PrintDocumentInfo implements Parcelable { |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public static final int PAGE_COUNT_UNKNOWN = -1; |
| 85 |
|
| 86 |
|
| 87 |
@Retention(RetentionPolicy.SOURCE) |
| 88 |
@IntDef(prefix = { "CONTENT_TYPE_" }, value = { |
| 89 |
CONTENT_TYPE_UNKNOWN, |
| 90 |
CONTENT_TYPE_DOCUMENT, |
| 91 |
CONTENT_TYPE_PHOTO |
| 92 |
}) |
| 93 |
public @interface ContentType { |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public static final int CONTENT_TYPE_UNKNOWN = -1; |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public static final int CONTENT_TYPE_DOCUMENT = 0; |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public static final int CONTENT_TYPE_PHOTO = 1; |
| 122 |
|
| 123 |
private @NonNull String mName; |
| 124 |
private @IntRange(from = -1) int mPageCount; |
| 125 |
private int mContentType; |
| 126 |
private long mDataSize; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
private PrintDocumentInfo() { |
| 132 |
|
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
private PrintDocumentInfo(@NonNull PrintDocumentInfo prototype) { |
| 141 |
mName = prototype.mName; |
| 142 |
mPageCount = prototype.mPageCount; |
| 143 |
mContentType = prototype.mContentType; |
| 144 |
mDataSize = prototype.mDataSize; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
private PrintDocumentInfo(Parcel parcel) { |
| 153 |
mName = Preconditions.checkStringNotEmpty(parcel.readString()); |
| 154 |
mPageCount = parcel.readInt(); |
| 155 |
Preconditions.checkArgument(mPageCount == PAGE_COUNT_UNKNOWN || mPageCount > 0); |
| 156 |
mContentType = parcel.readInt(); |
| 157 |
mDataSize = Preconditions.checkArgumentNonnegative(parcel.readLong()); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
public @NonNull String getName() { |
| 167 |
return mName; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
public @IntRange(from = -1) int getPageCount() { |
| 178 |
return mPageCount; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
public int getContentType() { |
| 191 |
return mContentType; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
public @IntRange(from = 0) long getDataSize() { |
| 200 |
return mDataSize; |
| 201 |
} |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
public void setDataSize(@IntRange(from = 0) long dataSize) { |
| 211 |
mDataSize = dataSize; |
| 212 |
} |
| 213 |
|
| 214 |
@Override |
| 215 |
public int describeContents() { |
| 216 |
return 0; |
| 217 |
} |
| 218 |
|
| 219 |
@Override |
| 220 |
public void writeToParcel(Parcel parcel, int flags) { |
| 221 |
parcel.writeString(mName); |
| 222 |
parcel.writeInt(mPageCount); |
| 223 |
parcel.writeInt(mContentType); |
| 224 |
parcel.writeLong(mDataSize); |
| 225 |
} |
| 226 |
|
| 227 |
@Override |
| 228 |
public int hashCode() { |
| 229 |
final int prime = 31; |
| 230 |
int result = 1; |
| 231 |
result = prime * result + ((mName != null) ? mName.hashCode() : 0); |
| 232 |
result = prime * result + mContentType; |
| 233 |
result = prime * result + mPageCount; |
| 234 |
result = prime * result + (int) mDataSize; |
| 235 |
result = prime * result + (int) (mDataSize >> 32); |
| 236 |
return result; |
| 237 |
} |
| 238 |
|
| 239 |
@Override |
| 240 |
public boolean equals(@Nullable Object obj) { |
| 241 |
if (this == obj) { |
| 242 |
return true; |
| 243 |
} |
| 244 |
if (obj == null) { |
| 245 |
return false; |
| 246 |
} |
| 247 |
if (getClass() != obj.getClass()) { |
| 248 |
return false; |
| 249 |
} |
| 250 |
PrintDocumentInfo other = (PrintDocumentInfo) obj; |
| 251 |
if (!TextUtils.equals(mName, other.mName)) { |
| 252 |
return false; |
| 253 |
} |
| 254 |
if (mContentType != other.mContentType) { |
| 255 |
return false; |
| 256 |
} |
| 257 |
if (mPageCount != other.mPageCount) { |
| 258 |
return false; |
| 259 |
} |
| 260 |
if (mDataSize != other.mDataSize) { |
| 261 |
return false; |
| 262 |
} |
| 263 |
return true; |
| 264 |
} |
| 265 |
|
| 266 |
@Override |
| 267 |
public String toString() { |
| 268 |
StringBuilder builder = new StringBuilder(); |
| 269 |
builder.append("PrintDocumentInfo{"); |
| 270 |
builder.append("name=").append(mName); |
| 271 |
builder.append(", pageCount=").append(mPageCount); |
| 272 |
builder.append(", contentType=").append(contentTypeToString(mContentType)); |
| 273 |
builder.append(", dataSize=").append(mDataSize); |
| 274 |
builder.append("}"); |
| 275 |
return builder.toString(); |
| 276 |
} |
| 277 |
|
| 278 |
private String contentTypeToString(int contentType) { |
| 279 |
switch (contentType) { |
| 280 |
case CONTENT_TYPE_DOCUMENT: { |
| 281 |
return "CONTENT_TYPE_DOCUMENT"; |
| 282 |
} |
| 283 |
case CONTENT_TYPE_PHOTO: { |
| 284 |
return "CONTENT_TYPE_PHOTO"; |
| 285 |
} |
| 286 |
default: { |
| 287 |
return "CONTENT_TYPE_UNKNOWN"; |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
public static final class Builder { |
| 296 |
private final PrintDocumentInfo mPrototype; |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
public Builder(@NonNull String name) { |
| 312 |
if (TextUtils.isEmpty(name)) { |
| 313 |
throw new IllegalArgumentException("name cannot be empty"); |
| 314 |
} |
| 315 |
mPrototype = new PrintDocumentInfo(); |
| 316 |
mPrototype.mName = name; |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
public @NonNull Builder setPageCount(@IntRange(from = -1) int pageCount) { |
| 330 |
if (pageCount < 0 && pageCount != PAGE_COUNT_UNKNOWN) { |
| 331 |
throw new IllegalArgumentException("pageCount" |
| 332 |
+ " must be greater than or equal to zero or" |
| 333 |
+ " DocumentInfo#PAGE_COUNT_UNKNOWN"); |
| 334 |
} |
| 335 |
mPrototype.mPageCount = pageCount; |
| 336 |
return this; |
| 337 |
} |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
public @NonNull Builder setContentType(@ContentType int type) { |
| 352 |
mPrototype.mContentType = type; |
| 353 |
return this; |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
public @NonNull PrintDocumentInfo build() { |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
if (mPrototype.mPageCount == 0) { |
| 366 |
mPrototype.mPageCount = PAGE_COUNT_UNKNOWN; |
| 367 |
} |
| 368 |
return new PrintDocumentInfo(mPrototype); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
public static final @android.annotation.NonNull Parcelable.Creator<PrintDocumentInfo> CREATOR = |
| 373 |
new Creator<PrintDocumentInfo>() { |
| 374 |
@Override |
| 375 |
public PrintDocumentInfo createFromParcel(Parcel parcel) { |
| 376 |
return new PrintDocumentInfo(parcel); |
| 377 |
} |
| 378 |
|
| 379 |
@Override |
| 380 |
public PrintDocumentInfo[] newArray(int size) { |
| 381 |
return new PrintDocumentInfo[size]; |
| 382 |
} |
| 383 |
}; |
| 384 |
} |