PrintDocumentInfo.java revision 85b1f883056a1d74473fd9ce774948878f389ab6
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; 21 22/** 23 * This class encapsulates information about a printed document. 24 */ 25public final class PrintDocumentInfo implements Parcelable { 26 27 /** 28 * Constant for unknown page count (default). 29 */ 30 public static final int PAGE_COUNT_UNKNOWN = -1; 31 32 /** 33 * Content type: unknown (default). 34 */ 35 public static final int CONTENT_TYPE_UNKNOWN = -1; 36 37 /** 38 * Content type: document. 39 */ 40 public static final int CONTENT_TYPE_DOCUMENT = 0; 41 42 /** 43 * Content type: photo. 44 */ 45 public static final int CONTENT_TYPE_PHOTO = 1; 46 47 private int mPageCount; 48 private int mContentType; 49 50 /** 51 * Creates a new instance. 52 */ 53 private PrintDocumentInfo() { 54 mPageCount = PAGE_COUNT_UNKNOWN; 55 mContentType = CONTENT_TYPE_UNKNOWN; 56 } 57 58 /** 59 * Creates a new instance. 60 * 61 * @param Prototype from which to clone. 62 */ 63 private PrintDocumentInfo(PrintDocumentInfo prototype) { 64 mPageCount = prototype.mPageCount; 65 mContentType = prototype.mContentType; 66 } 67 68 /** 69 * Creates a new instance. 70 * 71 * @param parcel Data from which to initialize. 72 */ 73 private PrintDocumentInfo(Parcel parcel) { 74 mPageCount = parcel.readInt(); 75 mContentType = parcel.readInt(); 76 } 77 78 /** 79 * Gets the total number of pages. 80 * 81 * @return The number of pages. 82 * 83 * @see #PAGE_COUNT_UNKNOWN 84 */ 85 public int getPageCount() { 86 return mPageCount; 87 } 88 89 /** 90 * Gets the content type. 91 * 92 * @return The content type. 93 * 94 * @see #CONTENT_TYPE_UNKNOWN 95 * @see #CONTENT_TYPE_DOCUMENT 96 * @see #CONTENT_TYPE_PHOTO 97 */ 98 public int getContentType() { 99 return mContentType; 100 } 101 102 @Override 103 public int describeContents() { 104 return 0; 105 } 106 107 @Override 108 public void writeToParcel(Parcel parcel, int flags) { 109 parcel.writeInt(mPageCount); 110 parcel.writeInt(mContentType); 111 } 112 113 @Override 114 public int hashCode() { 115 final int prime = 31; 116 int result = 1; 117 result = prime * result + mContentType; 118 result = prime * result + mPageCount; 119 return result; 120 } 121 122 @Override 123 public boolean equals(Object obj) { 124 if (this == obj) { 125 return true; 126 } 127 if (obj == null) { 128 return false; 129 } 130 if (getClass() != obj.getClass()) { 131 return false; 132 } 133 PrintDocumentInfo other = (PrintDocumentInfo) obj; 134 if (mContentType != other.mContentType) { 135 return false; 136 } 137 if (mPageCount != other.mPageCount) { 138 return false; 139 } 140 return true; 141 } 142 143 @Override 144 public String toString() { 145 StringBuilder builder = new StringBuilder(); 146 builder.append("PrintDocumentInfo{"); 147 builder.append("pageCount: ").append(mPageCount); 148 builder.append(", contentType: ").append(mContentType); 149 builder.append("}"); 150 return builder.toString(); 151 } 152 153 /** 154 * Builder for creating an {@link PrintDocumentInfo}. 155 */ 156 public static final class Builder { 157 private final PrintDocumentInfo mPrototype = new PrintDocumentInfo(); 158 159 /** 160 * Sets the total number of pages. 161 * 162 * @param pageCount The number of pages. Must be greater than 163 * or equal to zero or {@link PrintDocumentInfo#PAGE_COUNT_UNKNOWN}. 164 */ 165 public Builder setPageCount(int pageCount) { 166 if (pageCount < 0 && pageCount != PAGE_COUNT_UNKNOWN) { 167 throw new IllegalArgumentException("pageCount" 168 + " must be greater than or euqal to zero or" 169 + " DocumentInfo#PAGE_COUNT_UNKNOWN"); 170 } 171 mPrototype.mPageCount = pageCount; 172 return this; 173 } 174 175 /** 176 * Sets the content type. 177 * 178 * @param type The content type. 179 * 180 * @see #CONTENT_TYPE_UNKNOWN 181 * @see #CONTENT_TYPE_DOCUMENT 182 * @see #CONTENT_TYPE_PHOTO 183 */ 184 public Builder setContentType(int type) { 185 mPrototype.mContentType = type; 186 return this; 187 } 188 189 /** 190 * Creates a new {@link PrintDocumentInfo} instance. 191 * 192 * @return The new instance. 193 */ 194 public PrintDocumentInfo create() { 195 return new PrintDocumentInfo(mPrototype); 196 } 197 } 198 199 public static final Parcelable.Creator<PrintDocumentInfo> CREATOR = 200 new Creator<PrintDocumentInfo>() { 201 @Override 202 public PrintDocumentInfo createFromParcel(Parcel parcel) { 203 return new PrintDocumentInfo(parcel); 204 } 205 206 @Override 207 public PrintDocumentInfo[] newArray(int size) { 208 return new PrintDocumentInfo[size]; 209 } 210 }; 211} 212