blob: 44e6410f64d5551fc003a781208e6c0c7f5f22de [file] [log] [blame]
Svetoslav Ganova0027152013-06-25 14:59:53 -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 Ganova0027152013-06-25 14:59:53 -070022
23/**
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -070024 * This class encapsulates information about a document for printing
25 * purposes. This meta-data is used by the platform and print services,
26 * components that interact with printers. For example, this class
27 * contains the number of pages contained in the document it describes and
28 * this number of pages is shown to the user allowing him/her to select
29 * the range to print. Also a print service may optimize the printing
30 * process based on the content type, such as document or photo.
31 * <p>
32 * Instances of this class are created by the printing application and
33 * passed to the {@link PrintDocumentAdapter.LayoutResultCallback#onLayoutFinished(
34 * PrintDocumentInfo, boolean) PrintDocumentAdapter.LayoutResultCallback.onLayoutFinished(
35 * PrintDocumentInfo, boolean)} callback after successfully laying out the
36 * content which is performed in {@link PrintDocumentAdapter#onLayout(PrintAttributes,
37 * PrintAttributes, android.os.CancellationSignal, PrintDocumentAdapter.LayoutResultCallback,
38 * android.os.Bundle) PrintDocumentAdapter.onLayout(PrintAttributes,
39 * PrintAttributes, android.os.CancellationSignal,
40 * PrintDocumentAdapter.LayoutResultCallback, android.os.Bundle)}.
41 * </p>
42 * <p>
43 * An example usage looks like this:
44 * <pre>
45 *
46 * . . .
47 *
48 * public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes,
49 * CancellationSignal cancellationSignal, LayoutResultCallback callback,
50 * Bundle metadata) {
51 *
52 * // Assume the app defined a LayoutResult class which contains
53 * // the layout result data and that the content is a document.
54 * LayoutResult result = doSomeLayoutWork();
55 *
56 * PrintDocumentInfo info = new PrintDocumentInfo
57 * .Builder("printed_file.pdf")
58 * .setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
59 * .setPageCount(result.getPageCount())
60 * .build();
61 *
62 * callback.onLayoutFinished(info, result.getContentChanged());
63 * }
64 *
65 * . . .
66 *
67 * </pre>
68 * </p>
Svetoslav Ganova0027152013-06-25 14:59:53 -070069 */
70public final class PrintDocumentInfo implements Parcelable {
71
72 /**
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -070073 * Constant for unknown page count.
Svetoslav Ganova0027152013-06-25 14:59:53 -070074 */
75 public static final int PAGE_COUNT_UNKNOWN = -1;
76
77 /**
Svetoslav Ganovaec14172013-08-27 00:30:54 -070078 * Content type: unknown.
Svetoslav Ganova0027152013-06-25 14:59:53 -070079 */
80 public static final int CONTENT_TYPE_UNKNOWN = -1;
81
82 /**
83 * Content type: document.
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -070084 * <p>
85 * A print service may use normal paper to print the content instead
86 * of dedicated photo paper. Also it may use a lower quality printing
87 * process as the content is not as sensitive to print quality variation
88 * as a photo is.
89 * </p>
Svetoslav Ganova0027152013-06-25 14:59:53 -070090 */
91 public static final int CONTENT_TYPE_DOCUMENT = 0;
92
93 /**
94 * Content type: photo.
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -070095 * <p>
96 * A print service may use dedicated photo paper to print the content
97 * instead of normal paper. Also it may use a higher quality printing
98 * process as the content is more sensitive to print quality variation
99 * than a document.
100 * </p>
Svetoslav Ganova0027152013-06-25 14:59:53 -0700101 */
102 public static final int CONTENT_TYPE_PHOTO = 1;
103
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700104 private String mName;
Svetoslav Ganova0027152013-06-25 14:59:53 -0700105 private int mPageCount;
106 private int mContentType;
Svetoslav Ganovd26d4892013-08-28 14:37:54 -0700107 private long mDataSize;
Svetoslav Ganova0027152013-06-25 14:59:53 -0700108
109 /**
110 * Creates a new instance.
111 */
112 private PrintDocumentInfo() {
Svetoslav Ganovaec14172013-08-27 00:30:54 -0700113 /* do nothing */
Svetoslav Ganova0027152013-06-25 14:59:53 -0700114 }
115
116 /**
117 * Creates a new instance.
118 *
119 * @param Prototype from which to clone.
120 */
121 private PrintDocumentInfo(PrintDocumentInfo prototype) {
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700122 mName = prototype.mName;
Svetoslav Ganova0027152013-06-25 14:59:53 -0700123 mPageCount = prototype.mPageCount;
124 mContentType = prototype.mContentType;
Svetoslav Ganovd26d4892013-08-28 14:37:54 -0700125 mDataSize = prototype.mDataSize;
Svetoslav Ganova0027152013-06-25 14:59:53 -0700126 }
127
128 /**
129 * Creates a new instance.
130 *
131 * @param parcel Data from which to initialize.
132 */
133 private PrintDocumentInfo(Parcel parcel) {
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700134 mName = parcel.readString();
Svetoslav Ganova0027152013-06-25 14:59:53 -0700135 mPageCount = parcel.readInt();
136 mContentType = parcel.readInt();
Svetoslav Ganovd26d4892013-08-28 14:37:54 -0700137 mDataSize = parcel.readLong();
Svetoslav Ganova0027152013-06-25 14:59:53 -0700138 }
139
140 /**
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700141 * Gets the document name. This name may be shown to
142 * the user.
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700143 *
144 * @return The document name.
145 */
146 public String getName() {
147 return mName;
148 }
149
150 /**
Svetoslav Ganova0027152013-06-25 14:59:53 -0700151 * Gets the total number of pages.
152 *
153 * @return The number of pages.
154 *
155 * @see #PAGE_COUNT_UNKNOWN
156 */
157 public int getPageCount() {
158 return mPageCount;
159 }
160
161 /**
162 * Gets the content type.
163 *
164 * @return The content type.
165 *
166 * @see #CONTENT_TYPE_UNKNOWN
167 * @see #CONTENT_TYPE_DOCUMENT
168 * @see #CONTENT_TYPE_PHOTO
169 */
170 public int getContentType() {
171 return mContentType;
172 }
173
Svetoslav Ganovaec14172013-08-27 00:30:54 -0700174 /**
Svetoslav Ganovd26d4892013-08-28 14:37:54 -0700175 * Gets the document data size in bytes.
176 *
177 * @return The data size.
178 */
179 public long getDataSize() {
180 return mDataSize;
181 }
182
183 /**
184 * Sets the document data size in bytes.
185 *
186 * @param dataSize The data size.
187 *
188 * @hide
189 */
190 public void setDataSize(long dataSize) {
191 mDataSize = dataSize;
192 }
193
Svetoslav Ganova0027152013-06-25 14:59:53 -0700194 @Override
195 public int describeContents() {
196 return 0;
197 }
198
199 @Override
200 public void writeToParcel(Parcel parcel, int flags) {
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700201 parcel.writeString(mName);
Svetoslav Ganova0027152013-06-25 14:59:53 -0700202 parcel.writeInt(mPageCount);
203 parcel.writeInt(mContentType);
Svetoslav Ganovd26d4892013-08-28 14:37:54 -0700204 parcel.writeLong(mDataSize);
Svetoslav Ganova0027152013-06-25 14:59:53 -0700205 }
206
Svetoslav Ganov88d19912013-07-22 12:32:03 -0700207 @Override
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700208 public int hashCode() {
209 final int prime = 31;
210 int result = 1;
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700211 result = prime * result + ((mName != null) ? mName.hashCode() : 0);
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700212 result = prime * result + mContentType;
213 result = prime * result + mPageCount;
Svetoslav Ganovd26d4892013-08-28 14:37:54 -0700214 result = prime * result + (int) mDataSize;
Andreas Gampe869d26f2015-03-15 13:59:50 -0700215 result = prime * result + (int) (mDataSize >> 32);
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700216 return result;
217 }
218
219 @Override
220 public boolean equals(Object obj) {
221 if (this == obj) {
222 return true;
223 }
224 if (obj == null) {
225 return false;
226 }
227 if (getClass() != obj.getClass()) {
228 return false;
229 }
230 PrintDocumentInfo other = (PrintDocumentInfo) obj;
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700231 if (!TextUtils.equals(mName, other.mName)) {
232 return false;
233 }
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700234 if (mContentType != other.mContentType) {
235 return false;
236 }
237 if (mPageCount != other.mPageCount) {
238 return false;
239 }
Svetoslav Ganovd26d4892013-08-28 14:37:54 -0700240 if (mDataSize != other.mDataSize) {
241 return false;
242 }
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700243 return true;
244 }
245
246 @Override
Svetoslav Ganov88d19912013-07-22 12:32:03 -0700247 public String toString() {
248 StringBuilder builder = new StringBuilder();
249 builder.append("PrintDocumentInfo{");
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700250 builder.append("name=").append(mName);
251 builder.append(", pageCount=").append(mPageCount);
252 builder.append(", contentType=").append(contentTyepToString(mContentType));
Svetoslav Ganov7d7888d2013-10-12 13:18:12 -0700253 builder.append(", dataSize=").append(mDataSize);
Svetoslav Ganov88d19912013-07-22 12:32:03 -0700254 builder.append("}");
255 return builder.toString();
256 }
257
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700258 private String contentTyepToString(int contentType) {
259 switch (contentType) {
260 case CONTENT_TYPE_DOCUMENT: {
261 return "CONTENT_TYPE_DOCUMENT";
262 }
263 case CONTENT_TYPE_PHOTO: {
264 return "CONTENT_TYPE_PHOTO";
265 }
266 default: {
267 return "CONTENT_TYPE_UNKNOWN";
268 }
269 }
270 }
271
Svetoslav Ganova0027152013-06-25 14:59:53 -0700272 /**
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700273 * Builder for creating a {@link PrintDocumentInfo}.
Svetoslav Ganova0027152013-06-25 14:59:53 -0700274 */
275 public static final class Builder {
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700276 private final PrintDocumentInfo mPrototype;
277
278 /**
279 * Constructor.
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700280 *
Svetoslav Ganovaec14172013-08-27 00:30:54 -0700281 * <p>
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700282 * The values of the relevant properties are initialized with defaults.
283 * Please refer to the documentation of the individual setters for
284 * information about the default values.
Svetoslav Ganovaec14172013-08-27 00:30:54 -0700285 * </p>
286 *
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700287 * @param name The document name which may be shown to the user and
288 * is the file name if the content it describes is saved as a PDF.
289 * Cannot be empty.
Svetoslav Ganovaec14172013-08-27 00:30:54 -0700290 */
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700291 public Builder(String name) {
292 if (TextUtils.isEmpty(name)) {
293 throw new IllegalArgumentException("name cannot be empty");
294 }
295 mPrototype = new PrintDocumentInfo();
296 mPrototype.mName = name;
297 }
Svetoslav Ganova0027152013-06-25 14:59:53 -0700298
299 /**
300 * Sets the total number of pages.
Svetoslav Ganovaec14172013-08-27 00:30:54 -0700301 * <p>
302 * <strong>Default: </strong> {@link #PAGE_COUNT_UNKNOWN}
303 * </p>
Svetoslav Ganova0027152013-06-25 14:59:53 -0700304 *
305 * @param pageCount The number of pages. Must be greater than
306 * or equal to zero or {@link PrintDocumentInfo#PAGE_COUNT_UNKNOWN}.
307 */
308 public Builder setPageCount(int pageCount) {
309 if (pageCount < 0 && pageCount != PAGE_COUNT_UNKNOWN) {
310 throw new IllegalArgumentException("pageCount"
Svetoslav0c126a42014-07-10 17:36:27 -0700311 + " must be greater than or equal to zero or"
Svetoslav Ganova0027152013-06-25 14:59:53 -0700312 + " DocumentInfo#PAGE_COUNT_UNKNOWN");
313 }
314 mPrototype.mPageCount = pageCount;
315 return this;
316 }
317
318 /**
319 * Sets the content type.
Svetoslav Ganovaec14172013-08-27 00:30:54 -0700320 * <p>
321 * <strong>Default: </strong> {@link #CONTENT_TYPE_UNKNOWN}
322 * </p>
Svetoslav Ganova0027152013-06-25 14:59:53 -0700323 *
324 * @param type The content type.
325 *
326 * @see #CONTENT_TYPE_UNKNOWN
327 * @see #CONTENT_TYPE_DOCUMENT
328 * @see #CONTENT_TYPE_PHOTO
329 */
330 public Builder setContentType(int type) {
331 mPrototype.mContentType = type;
332 return this;
333 }
334
335 /**
Svetoslav Ganova0027152013-06-25 14:59:53 -0700336 * Creates a new {@link PrintDocumentInfo} instance.
337 *
338 * @return The new instance.
339 */
Svetoslav651dd4e2013-09-12 14:37:47 -0700340 public PrintDocumentInfo build() {
Svetoslav0c126a42014-07-10 17:36:27 -0700341 // Zero pages is the same as unknown as in this case
342 // we will have to ask for all pages and look a the
343 // wiritten PDF file for the page count.
344 if (mPrototype.mPageCount == 0) {
345 mPrototype.mPageCount = PAGE_COUNT_UNKNOWN;
346 }
Svetoslav Ganova0027152013-06-25 14:59:53 -0700347 return new PrintDocumentInfo(mPrototype);
348 }
349 }
350
351 public static final Parcelable.Creator<PrintDocumentInfo> CREATOR =
352 new Creator<PrintDocumentInfo>() {
353 @Override
354 public PrintDocumentInfo createFromParcel(Parcel parcel) {
355 return new PrintDocumentInfo(parcel);
356 }
357
358 @Override
359 public PrintDocumentInfo[] newArray(int size) {
360 return new PrintDocumentInfo[size];
361 }
362 };
363}