blob: bec6f290dbdfc54e79f18e0fa06741e0884540e1 [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
Philip P. Moltmannc43639c2015-12-18 13:58:40 -080019import android.annotation.IntDef;
20import android.annotation.IntRange;
21import android.annotation.NonNull;
Svetoslav Ganova0027152013-06-25 14:59:53 -070022import android.os.Parcel;
23import android.os.Parcelable;
Svetoslav Ganov798bed62013-08-11 12:29:39 -070024import android.text.TextUtils;
Philip P. Moltmann4723f362016-03-30 13:48:38 -070025import com.android.internal.util.Preconditions;
Svetoslav Ganova0027152013-06-25 14:59:53 -070026
Philip P. Moltmannc43639c2015-12-18 13:58:40 -080027import java.lang.annotation.Retention;
28import java.lang.annotation.RetentionPolicy;
29
Svetoslav Ganova0027152013-06-25 14:59:53 -070030/**
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -070031 * This class encapsulates information about a document for printing
32 * purposes. This meta-data is used by the platform and print services,
33 * components that interact with printers. For example, this class
34 * contains the number of pages contained in the document it describes and
35 * this number of pages is shown to the user allowing him/her to select
36 * the range to print. Also a print service may optimize the printing
37 * process based on the content type, such as document or photo.
38 * <p>
39 * Instances of this class are created by the printing application and
40 * passed to the {@link PrintDocumentAdapter.LayoutResultCallback#onLayoutFinished(
41 * PrintDocumentInfo, boolean) PrintDocumentAdapter.LayoutResultCallback.onLayoutFinished(
42 * PrintDocumentInfo, boolean)} callback after successfully laying out the
43 * content which is performed in {@link PrintDocumentAdapter#onLayout(PrintAttributes,
44 * PrintAttributes, android.os.CancellationSignal, PrintDocumentAdapter.LayoutResultCallback,
45 * android.os.Bundle) PrintDocumentAdapter.onLayout(PrintAttributes,
46 * PrintAttributes, android.os.CancellationSignal,
47 * PrintDocumentAdapter.LayoutResultCallback, android.os.Bundle)}.
48 * </p>
49 * <p>
50 * An example usage looks like this:
51 * <pre>
52 *
53 * . . .
54 *
55 * public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes,
56 * CancellationSignal cancellationSignal, LayoutResultCallback callback,
57 * Bundle metadata) {
58 *
59 * // Assume the app defined a LayoutResult class which contains
60 * // the layout result data and that the content is a document.
61 * LayoutResult result = doSomeLayoutWork();
62 *
63 * PrintDocumentInfo info = new PrintDocumentInfo
64 * .Builder("printed_file.pdf")
65 * .setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
66 * .setPageCount(result.getPageCount())
67 * .build();
68 *
69 * callback.onLayoutFinished(info, result.getContentChanged());
70 * }
71 *
72 * . . .
73 *
74 * </pre>
75 * </p>
Svetoslav Ganova0027152013-06-25 14:59:53 -070076 */
77public final class PrintDocumentInfo implements Parcelable {
78
79 /**
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -070080 * Constant for unknown page count.
Svetoslav Ganova0027152013-06-25 14:59:53 -070081 */
82 public static final int PAGE_COUNT_UNKNOWN = -1;
83
Philip P. Moltmannc43639c2015-12-18 13:58:40 -080084 /** @hide */
85 @Retention(RetentionPolicy.SOURCE)
86 @IntDef({
87 CONTENT_TYPE_UNKNOWN, CONTENT_TYPE_DOCUMENT, CONTENT_TYPE_PHOTO
88 })
89 public @interface ContentType {
90 }
Svetoslav Ganova0027152013-06-25 14:59:53 -070091 /**
Svetoslav Ganovaec14172013-08-27 00:30:54 -070092 * Content type: unknown.
Svetoslav Ganova0027152013-06-25 14:59:53 -070093 */
94 public static final int CONTENT_TYPE_UNKNOWN = -1;
95
96 /**
97 * Content type: document.
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -070098 * <p>
99 * A print service may use normal paper to print the content instead
100 * of dedicated photo paper. Also it may use a lower quality printing
101 * process as the content is not as sensitive to print quality variation
102 * as a photo is.
103 * </p>
Svetoslav Ganova0027152013-06-25 14:59:53 -0700104 */
105 public static final int CONTENT_TYPE_DOCUMENT = 0;
106
107 /**
108 * Content type: photo.
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700109 * <p>
110 * A print service may use dedicated photo paper to print the content
111 * instead of normal paper. Also it may use a higher quality printing
112 * process as the content is more sensitive to print quality variation
113 * than a document.
114 * </p>
Svetoslav Ganova0027152013-06-25 14:59:53 -0700115 */
116 public static final int CONTENT_TYPE_PHOTO = 1;
117
Philip P. Moltmann4723f362016-03-30 13:48:38 -0700118 private @NonNull String mName;
119 private @IntRange(from = -1) int mPageCount;
Svetoslav Ganova0027152013-06-25 14:59:53 -0700120 private int mContentType;
Svetoslav Ganovd26d4892013-08-28 14:37:54 -0700121 private long mDataSize;
Svetoslav Ganova0027152013-06-25 14:59:53 -0700122
123 /**
124 * Creates a new instance.
125 */
126 private PrintDocumentInfo() {
Svetoslav Ganovaec14172013-08-27 00:30:54 -0700127 /* do nothing */
Svetoslav Ganova0027152013-06-25 14:59:53 -0700128 }
129
130 /**
131 * Creates a new instance.
132 *
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800133 * @param prototype from which to clone.
Svetoslav Ganova0027152013-06-25 14:59:53 -0700134 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800135 private PrintDocumentInfo(@NonNull PrintDocumentInfo prototype) {
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700136 mName = prototype.mName;
Svetoslav Ganova0027152013-06-25 14:59:53 -0700137 mPageCount = prototype.mPageCount;
138 mContentType = prototype.mContentType;
Svetoslav Ganovd26d4892013-08-28 14:37:54 -0700139 mDataSize = prototype.mDataSize;
Svetoslav Ganova0027152013-06-25 14:59:53 -0700140 }
141
142 /**
143 * Creates a new instance.
144 *
145 * @param parcel Data from which to initialize.
146 */
147 private PrintDocumentInfo(Parcel parcel) {
Philip P. Moltmann4723f362016-03-30 13:48:38 -0700148 mName = Preconditions.checkStringNotEmpty(parcel.readString());
Svetoslav Ganova0027152013-06-25 14:59:53 -0700149 mPageCount = parcel.readInt();
Philip P. Moltmann4723f362016-03-30 13:48:38 -0700150 Preconditions.checkArgument(mPageCount == PAGE_COUNT_UNKNOWN || mPageCount > 0);
Svetoslav Ganova0027152013-06-25 14:59:53 -0700151 mContentType = parcel.readInt();
Philip P. Moltmann4723f362016-03-30 13:48:38 -0700152 mDataSize = Preconditions.checkArgumentNonnegative(parcel.readLong());
Svetoslav Ganova0027152013-06-25 14:59:53 -0700153 }
154
155 /**
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700156 * Gets the document name. This name may be shown to
157 * the user.
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700158 *
159 * @return The document name.
160 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800161 public @NonNull String getName() {
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700162 return mName;
163 }
164
165 /**
Svetoslav Ganova0027152013-06-25 14:59:53 -0700166 * Gets the total number of pages.
167 *
168 * @return The number of pages.
169 *
170 * @see #PAGE_COUNT_UNKNOWN
171 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800172 public @IntRange(from = -1) int getPageCount() {
Svetoslav Ganova0027152013-06-25 14:59:53 -0700173 return mPageCount;
174 }
175
176 /**
177 * Gets the content type.
178 *
179 * @return The content type.
180 *
181 * @see #CONTENT_TYPE_UNKNOWN
182 * @see #CONTENT_TYPE_DOCUMENT
183 * @see #CONTENT_TYPE_PHOTO
184 */
Philip P. Moltmann4723f362016-03-30 13:48:38 -0700185 public int getContentType() {
Svetoslav Ganova0027152013-06-25 14:59:53 -0700186 return mContentType;
187 }
188
Svetoslav Ganovaec14172013-08-27 00:30:54 -0700189 /**
Svetoslav Ganovd26d4892013-08-28 14:37:54 -0700190 * Gets the document data size in bytes.
191 *
192 * @return The data size.
193 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800194 public @IntRange(from = 0) long getDataSize() {
Svetoslav Ganovd26d4892013-08-28 14:37:54 -0700195 return mDataSize;
196 }
197
198 /**
199 * Sets the document data size in bytes.
200 *
201 * @param dataSize The data size.
202 *
203 * @hide
204 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800205 public void setDataSize(@IntRange(from = 0) long dataSize) {
Svetoslav Ganovd26d4892013-08-28 14:37:54 -0700206 mDataSize = dataSize;
207 }
208
Svetoslav Ganova0027152013-06-25 14:59:53 -0700209 @Override
210 public int describeContents() {
211 return 0;
212 }
213
214 @Override
215 public void writeToParcel(Parcel parcel, int flags) {
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700216 parcel.writeString(mName);
Svetoslav Ganova0027152013-06-25 14:59:53 -0700217 parcel.writeInt(mPageCount);
218 parcel.writeInt(mContentType);
Svetoslav Ganovd26d4892013-08-28 14:37:54 -0700219 parcel.writeLong(mDataSize);
Svetoslav Ganova0027152013-06-25 14:59:53 -0700220 }
221
Svetoslav Ganov88d19912013-07-22 12:32:03 -0700222 @Override
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700223 public int hashCode() {
224 final int prime = 31;
225 int result = 1;
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700226 result = prime * result + ((mName != null) ? mName.hashCode() : 0);
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700227 result = prime * result + mContentType;
228 result = prime * result + mPageCount;
Svetoslav Ganovd26d4892013-08-28 14:37:54 -0700229 result = prime * result + (int) mDataSize;
Andreas Gampe869d26f2015-03-15 13:59:50 -0700230 result = prime * result + (int) (mDataSize >> 32);
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700231 return result;
232 }
233
234 @Override
235 public boolean equals(Object obj) {
236 if (this == obj) {
237 return true;
238 }
239 if (obj == null) {
240 return false;
241 }
242 if (getClass() != obj.getClass()) {
243 return false;
244 }
245 PrintDocumentInfo other = (PrintDocumentInfo) obj;
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700246 if (!TextUtils.equals(mName, other.mName)) {
247 return false;
248 }
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700249 if (mContentType != other.mContentType) {
250 return false;
251 }
252 if (mPageCount != other.mPageCount) {
253 return false;
254 }
Svetoslav Ganovd26d4892013-08-28 14:37:54 -0700255 if (mDataSize != other.mDataSize) {
256 return false;
257 }
Svetoslav Ganov85b1f882013-07-24 17:00:06 -0700258 return true;
259 }
260
261 @Override
Svetoslav Ganov88d19912013-07-22 12:32:03 -0700262 public String toString() {
263 StringBuilder builder = new StringBuilder();
264 builder.append("PrintDocumentInfo{");
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700265 builder.append("name=").append(mName);
266 builder.append(", pageCount=").append(mPageCount);
Philip P. Moltmann4723f362016-03-30 13:48:38 -0700267 builder.append(", contentType=").append(contentTypeToString(mContentType));
Svetoslav Ganov7d7888d2013-10-12 13:18:12 -0700268 builder.append(", dataSize=").append(mDataSize);
Svetoslav Ganov88d19912013-07-22 12:32:03 -0700269 builder.append("}");
270 return builder.toString();
271 }
272
Philip P. Moltmann4723f362016-03-30 13:48:38 -0700273 private String contentTypeToString(int contentType) {
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700274 switch (contentType) {
275 case CONTENT_TYPE_DOCUMENT: {
276 return "CONTENT_TYPE_DOCUMENT";
277 }
278 case CONTENT_TYPE_PHOTO: {
279 return "CONTENT_TYPE_PHOTO";
280 }
281 default: {
282 return "CONTENT_TYPE_UNKNOWN";
283 }
284 }
285 }
286
Svetoslav Ganova0027152013-06-25 14:59:53 -0700287 /**
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700288 * Builder for creating a {@link PrintDocumentInfo}.
Svetoslav Ganova0027152013-06-25 14:59:53 -0700289 */
290 public static final class Builder {
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700291 private final PrintDocumentInfo mPrototype;
292
293 /**
294 * Constructor.
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700295 *
Svetoslav Ganovaec14172013-08-27 00:30:54 -0700296 * <p>
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700297 * The values of the relevant properties are initialized with defaults.
298 * Please refer to the documentation of the individual setters for
299 * information about the default values.
Svetoslav Ganovaec14172013-08-27 00:30:54 -0700300 * </p>
301 *
Svetoslav Ganov4d4c66d2013-10-24 18:04:39 -0700302 * @param name The document name which may be shown to the user and
303 * is the file name if the content it describes is saved as a PDF.
304 * Cannot be empty.
Svetoslav Ganovaec14172013-08-27 00:30:54 -0700305 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800306 public Builder(@NonNull String name) {
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700307 if (TextUtils.isEmpty(name)) {
308 throw new IllegalArgumentException("name cannot be empty");
309 }
310 mPrototype = new PrintDocumentInfo();
311 mPrototype.mName = name;
312 }
Svetoslav Ganova0027152013-06-25 14:59:53 -0700313
314 /**
315 * Sets the total number of pages.
Svetoslav Ganovaec14172013-08-27 00:30:54 -0700316 * <p>
317 * <strong>Default: </strong> {@link #PAGE_COUNT_UNKNOWN}
318 * </p>
Svetoslav Ganova0027152013-06-25 14:59:53 -0700319 *
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800320 * @param pageCount The number of pages. Must be greater than or equal to zero or
321 * {@link PrintDocumentInfo#PAGE_COUNT_UNKNOWN}.
322 * @return This builder.
Svetoslav Ganova0027152013-06-25 14:59:53 -0700323 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800324 public @NonNull Builder setPageCount(@IntRange(from = -1) int pageCount) {
Svetoslav Ganova0027152013-06-25 14:59:53 -0700325 if (pageCount < 0 && pageCount != PAGE_COUNT_UNKNOWN) {
326 throw new IllegalArgumentException("pageCount"
Svetoslav0c126a42014-07-10 17:36:27 -0700327 + " must be greater than or equal to zero or"
Svetoslav Ganova0027152013-06-25 14:59:53 -0700328 + " DocumentInfo#PAGE_COUNT_UNKNOWN");
329 }
330 mPrototype.mPageCount = pageCount;
331 return this;
332 }
333
334 /**
335 * Sets the content type.
Svetoslav Ganovaec14172013-08-27 00:30:54 -0700336 * <p>
337 * <strong>Default: </strong> {@link #CONTENT_TYPE_UNKNOWN}
338 * </p>
Svetoslav Ganova0027152013-06-25 14:59:53 -0700339 *
340 * @param type The content type.
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800341 * @return This builder.
Svetoslav Ganova0027152013-06-25 14:59:53 -0700342 * @see #CONTENT_TYPE_UNKNOWN
343 * @see #CONTENT_TYPE_DOCUMENT
344 * @see #CONTENT_TYPE_PHOTO
345 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800346 public @NonNull Builder setContentType(@ContentType int type) {
Svetoslav Ganova0027152013-06-25 14:59:53 -0700347 mPrototype.mContentType = type;
348 return this;
349 }
350
351 /**
Svetoslav Ganova0027152013-06-25 14:59:53 -0700352 * Creates a new {@link PrintDocumentInfo} instance.
353 *
354 * @return The new instance.
355 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800356 public @NonNull PrintDocumentInfo build() {
Svetoslav0c126a42014-07-10 17:36:27 -0700357 // Zero pages is the same as unknown as in this case
358 // we will have to ask for all pages and look a the
359 // wiritten PDF file for the page count.
360 if (mPrototype.mPageCount == 0) {
361 mPrototype.mPageCount = PAGE_COUNT_UNKNOWN;
362 }
Svetoslav Ganova0027152013-06-25 14:59:53 -0700363 return new PrintDocumentInfo(mPrototype);
364 }
365 }
366
367 public static final Parcelable.Creator<PrintDocumentInfo> CREATOR =
368 new Creator<PrintDocumentInfo>() {
369 @Override
370 public PrintDocumentInfo createFromParcel(Parcel parcel) {
371 return new PrintDocumentInfo(parcel);
372 }
373
374 @Override
375 public PrintDocumentInfo[] newArray(int size) {
376 return new PrintDocumentInfo[size];
377 }
378 };
379}