PrintDocumentInfo.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.print.PrintAttributes.Margins;
22import android.print.PrintAttributes.MediaSize;
23import android.text.TextUtils;
24
25/**
26 * This class encapsulates information about a printed document.
27 */
28public final class PrintDocumentInfo implements Parcelable {
29
30    /**
31     * Constant for an unknown media size.
32     */
33    public static final MediaSize MEDIA_SIZE_UNKNOWN = new MediaSize("Unknown", "Unknown", 1, 1);
34
35    /**
36     * Constant for unknown page count..
37     */
38    public static final int PAGE_COUNT_UNKNOWN = -1;
39
40    /**
41     * Content type: unknown.
42     */
43    public static final int CONTENT_TYPE_UNKNOWN = -1;
44
45    /**
46     * Content type: document.
47     */
48    public static final int CONTENT_TYPE_DOCUMENT = 0;
49
50    /**
51     * Content type: photo.
52     */
53    public static final int CONTENT_TYPE_PHOTO = 1;
54
55    private String mName;
56    private int mPageCount;
57    private int mContentType;
58    private int mOrientation;
59    private int mFittingMode;
60    private int mColorMode;
61    private Margins mMargins;
62    private MediaSize mMediaSize;
63
64    /**
65     * Creates a new instance.
66     */
67    private PrintDocumentInfo() {
68        /* do nothing */
69    }
70
71    /**
72     * Creates a new instance.
73     *
74     * @param Prototype from which to clone.
75     */
76    private PrintDocumentInfo(PrintDocumentInfo prototype) {
77        mName = prototype.mName;
78        mPageCount = prototype.mPageCount;
79        mContentType = prototype.mContentType;
80        mOrientation = prototype.mOrientation;
81        mFittingMode = prototype.mFittingMode;
82        mColorMode = prototype.mColorMode;
83        mMargins = prototype.mMargins;
84        mMediaSize = prototype.mMediaSize;
85    }
86
87    /**
88     * Creates a new instance.
89     *
90     * @param parcel Data from which to initialize.
91     */
92    private PrintDocumentInfo(Parcel parcel) {
93        mName = parcel.readString();
94        mPageCount = parcel.readInt();
95        mContentType = parcel.readInt();
96        mOrientation = parcel.readInt();
97        mFittingMode = parcel.readInt();
98        mColorMode = parcel.readInt();
99        mMargins = Margins.createFromParcel(parcel);
100        mMediaSize = MediaSize.createFromParcel(parcel);
101    }
102
103    /**
104     * Gets the document name.
105     *
106     * @return The document name.
107     */
108    public String getName() {
109        return mName;
110    }
111
112    /**
113     * Gets the total number of pages.
114     *
115     * @return The number of pages.
116     *
117     * @see #PAGE_COUNT_UNKNOWN
118     */
119    public int getPageCount() {
120        return mPageCount;
121    }
122
123    /**
124     * Gets the content type.
125     *
126     * @return The content type.
127     *
128     * @see #CONTENT_TYPE_UNKNOWN
129     * @see #CONTENT_TYPE_DOCUMENT
130     * @see #CONTENT_TYPE_PHOTO
131     */
132    public int getContentType() {
133        return mContentType;
134    }
135
136    /**
137     * Gets the document orientation.
138     *
139     * @return The orientation.
140     *
141     * @see PrintAttributes#ORIENTATION_PORTRAIT PrintAttributes.ORIENTATION_PORTRAIT
142     * @see PrintAttributes#ORIENTATION_LANDSCAPE PrintAttributes.ORIENTATION_LANDSCAPE
143     */
144    public int getOrientation() {
145        return mOrientation;
146    }
147
148    /**
149     * Gets the document fitting mode.
150     *
151     * @return The fitting mode.
152     *
153     * @see PrintAttributes#FITTING_MODE_NONE PrintAttributes.FITTING_MODE_NONE
154     * @see PrintAttributes#FITTING_MODE_SCALE_TO_FILL PrintAttributes.FITTING_MODE_SCALE_TO_FILL
155     * @see PrintAttributes#FITTING_MODE_SCALE_TO_FIT PrintAttributes.FITTING_MODE_SCALE_TO_FIT
156     */
157    public int getFittingMode() {
158        return mFittingMode;
159    }
160
161    /**
162     * Gets document color mode.
163     *
164     * @return The color mode.
165     *
166     * @see PrintAttributes#COLOR_MODE_COLOR PrintAttributes.COLOR_MODE_COLOR
167     * @see PrintAttributes#COLOR_MODE_MONOCHROME PrintAttributes.COLOR_MODE_MONOCHROME
168     */
169    public int getColorMode() {
170        return mColorMode;
171    }
172
173    /**
174     * Gets the document margins.
175     *
176     * @return The margins.
177     */
178    public Margins getMargins() {
179        return mMargins;
180    }
181
182    /**
183     * Gets the media size.
184     *
185     * @return The media size.
186     */
187    public MediaSize getMediaSize() {
188        return mMediaSize;
189    }
190
191    @Override
192    public int describeContents() {
193        return 0;
194    }
195
196    @Override
197    public void writeToParcel(Parcel parcel, int flags) {
198        parcel.writeString(mName);
199        parcel.writeInt(mPageCount);
200        parcel.writeInt(mContentType);
201        parcel.writeInt(mOrientation);
202        parcel.writeInt(mFittingMode);
203        parcel.writeInt(mColorMode);
204        mMargins.writeToParcel(parcel);
205        mMediaSize.writeToParcel(parcel);
206    }
207
208    @Override
209    public int hashCode() {
210        final int prime = 31;
211        int result = 1;
212        result = prime * result + ((mName != null) ? mName.hashCode() : 0);
213        result = prime * result + mContentType;
214        result = prime * result + mPageCount;
215        result = prime * result + mOrientation;
216        result = prime * result + mFittingMode;
217        result = prime * result + mColorMode;
218        result = prime * result + (mMargins != null ? mMargins.hashCode() : 0);
219        result = prime * result + (mMediaSize != null ? mMediaSize.hashCode() : 0);
220        return result;
221    }
222
223    @Override
224    public boolean equals(Object obj) {
225        if (this == obj) {
226            return true;
227        }
228        if (obj == null) {
229            return false;
230        }
231        if (getClass() != obj.getClass()) {
232            return false;
233        }
234        PrintDocumentInfo other = (PrintDocumentInfo) obj;
235        if (!TextUtils.equals(mName, other.mName)) {
236            return false;
237        }
238        if (mContentType != other.mContentType) {
239            return false;
240        }
241        if (mPageCount != other.mPageCount) {
242            return false;
243        }
244        if (mOrientation != other.mOrientation) {
245            return false;
246        }
247        if (mFittingMode != other.mFittingMode) {
248            return false;
249        }
250        if (mColorMode != other.mColorMode) {
251            return false;
252        }
253        if (mMargins == null) {
254            if (other.mMargins != null) {
255                return false;
256            }
257        } else if (!mMargins.equals(other.mMargins)) {
258            return false;
259        }
260        if (mMediaSize == null) {
261            if (other.mMediaSize != null) {
262                return false;
263            }
264        } else if (!mMediaSize.equals(other.mMediaSize)) {
265            return false;
266        }
267        return true;
268    }
269
270    @Override
271    public String toString() {
272        StringBuilder builder = new StringBuilder();
273        builder.append("PrintDocumentInfo{");
274        builder.append("name=").append(mName);
275        builder.append(", pageCount=").append(mPageCount);
276        builder.append(", contentType=").append(contentTyepToString(mContentType));
277        builder.append(", orientation=").append(PrintAttributes.orientationToString(mOrientation));
278        builder.append(", fittingMode=").append(PrintAttributes.fittingModeToString(mFittingMode));
279        builder.append(", colorMode=").append(PrintAttributes.colorModeToString(mColorMode));
280        builder.append(", margins=").append(mMargins);
281        builder.append(", mediaSize=").append(mMediaSize);
282        builder.append("}");
283        return builder.toString();
284    }
285
286    private String contentTyepToString(int contentType) {
287        switch (contentType) {
288            case CONTENT_TYPE_DOCUMENT: {
289                return "CONTENT_TYPE_DOCUMENT";
290            }
291            case CONTENT_TYPE_PHOTO: {
292                return "CONTENT_TYPE_PHOTO";
293            }
294            default: {
295                return "CONTENT_TYPE_UNKNOWN";
296            }
297        }
298    }
299
300    /**
301     * Builder for creating an {@link PrintDocumentInfo}.
302     */
303    public static final class Builder {
304        private final PrintDocumentInfo mPrototype;
305
306        /**
307         * Constructor.
308         * <p>
309         * The values of the relevant properties are initialized from the
310         * provided print attributes. For example, the orientation is set
311         * to be the same as the orientation returned by calling {@link
312         * PrintAttributes#getOrientation() PrintAttributes.getOrientation()}.
313         * </p>
314         *
315         * @param name The document name. Cannot be empty.
316         * @param attributes Print attributes. Cannot be null.
317         *
318         * @throws IllegalArgumentException If the name is empty.
319         */
320        public Builder(String name, PrintAttributes attributes) {
321            if (TextUtils.isEmpty(name)) {
322                throw new IllegalArgumentException("name cannot be empty");
323            }
324            if (attributes == null) {
325                throw new IllegalArgumentException("attributes cannot be null");
326            }
327            mPrototype = new PrintDocumentInfo();
328            mPrototype.mName = name;
329            mPrototype.mOrientation = attributes.getOrientation();
330            mPrototype.mFittingMode = attributes.getFittingMode();
331            mPrototype.mColorMode = attributes.getColorMode();
332            mPrototype.mMargins = attributes.getMargins();
333            mPrototype.mMediaSize = attributes.getMediaSize();
334        }
335
336        /**
337         * Constructor.
338         * <p>
339         * The values of the relevant properties are initialized with default
340         * values. Please refer to the documentation of the individual setters
341         * for information about the default values.
342         * </p>
343         *
344         * @param name The document name. Cannot be empty.
345         */
346        public Builder(String name) {
347            if (TextUtils.isEmpty(name)) {
348                throw new IllegalArgumentException("name cannot be empty");
349            }
350            mPrototype = new PrintDocumentInfo();
351            mPrototype.mName = name;
352            mPrototype.mOrientation = PrintAttributes.ORIENTATION_PORTRAIT;
353            mPrototype.mFittingMode = PrintAttributes.FITTING_MODE_NONE;
354            mPrototype.mColorMode = PrintAttributes.COLOR_MODE_COLOR;
355            mPrototype.mMargins = Margins.NO_MARGINS;
356            mPrototype.mMediaSize = MEDIA_SIZE_UNKNOWN;
357        }
358
359        /**
360         * Sets the total number of pages.
361         * <p>
362         * <strong>Default: </strong> {@link #PAGE_COUNT_UNKNOWN}
363         * </p>
364         *
365         * @param pageCount The number of pages. Must be greater than
366         * or equal to zero or {@link PrintDocumentInfo#PAGE_COUNT_UNKNOWN}.
367         */
368        public Builder setPageCount(int pageCount) {
369            if (pageCount < 0 && pageCount != PAGE_COUNT_UNKNOWN) {
370                throw new IllegalArgumentException("pageCount"
371                        + " must be greater than or euqal to zero or"
372                        + " DocumentInfo#PAGE_COUNT_UNKNOWN");
373            }
374            mPrototype.mPageCount = pageCount;
375            return this;
376        }
377
378        /**
379         * Sets the content type.
380         * <p>
381         * <strong>Default: </strong> {@link #CONTENT_TYPE_UNKNOWN}
382         * </p>
383         *
384         * @param type The content type.
385         *
386         * @see #CONTENT_TYPE_UNKNOWN
387         * @see #CONTENT_TYPE_DOCUMENT
388         * @see #CONTENT_TYPE_PHOTO
389         */
390        public Builder setContentType(int type) {
391            mPrototype.mContentType = type;
392            return this;
393        }
394
395        /**
396         * Sets the orientation.
397         * <p>
398         * <strong>Default: </strong> {@link PrintAttributes#ORIENTATION_PORTRAIT
399         * PrintAttributes.ORIENTATION_PORTRAIT}
400         * </p>
401         *
402         * @param orientation The orientation.
403         *
404         * @see PrintAttributes#ORIENTATION_PORTRAIT PrintAttributes.ORIENTATION_PORTRAIT
405         * @see PrintAttributes#ORIENTATION_LANDSCAPE PrintAttributes.ORIENTATION_LANDSCAPE
406         */
407        public Builder setOrientation(int orientation) {
408            PrintAttributes.enforceValidOrientation(orientation);
409            mPrototype.mOrientation = orientation;
410            return this;
411        }
412
413        /**
414         * Sets the content fitting mode.
415         * <p>
416         * <strong>Default: </strong> {@link PrintAttributes#FITTING_MODE_NONE
417         * PrintAttributes.FITTING_MODE_NONE}
418         * </p>
419         *
420         * @param fittingMode The fitting mode.
421         *
422         * @see PrintAttributes#FITTING_MODE_NONE PrintAttributes.FITTING_MODE_NONE
423         * @see PrintAttributes#FITTING_MODE_SCALE_TO_FILL PrintAttributes.FITTING_MODE_SCALE_TO_FILL
424         * @see PrintAttributes#FITTING_MODE_SCALE_TO_FIT PrintAttributes.FITTING_MODE_SCALE_TO_FIT
425         */
426        public Builder setFittingMode(int fittingMode) {
427            PrintAttributes.enforceValidFittingMode(fittingMode);
428            mPrototype.mFittingMode = fittingMode;
429            return this;
430        }
431
432        /**
433         * Sets the content color mode.
434         * <p>
435         * <strong>Default: </strong> {@link PrintAttributes#COLOR_MODE_COLOR
436         * PrintAttributes.COLOR_MODE_COLOR}
437         * </p>
438         *
439         * @param colorMode The color mode.
440         *
441         * @see PrintAttributes#COLOR_MODE_COLOR PrintAttributes.COLOR_MODE_COLOR
442         * @see PrintAttributes#COLOR_MODE_MONOCHROME PrintAttributes.COLOR_MODE_MONOCHROME
443         */
444        public Builder setColorMode(int colorMode) {
445            PrintAttributes.enforceValidColorMode(colorMode);
446            mPrototype.mColorMode = colorMode;
447            return this;
448        }
449
450        /**
451         * Sets the document margins.
452         * <p>
453         * <strong>Default: </strong> {@link PrintAttributes.Margins#NO_MARGINS Margins.NO_MARGINS}
454         * </p>
455         *
456         * @param margins The margins. Cannot be null.
457         */
458        public Builder setMargins(Margins margins) {
459            if (margins == null) {
460                throw new IllegalArgumentException("margins cannot be null");
461            }
462            mPrototype.mMargins = margins;
463            return this;
464        }
465
466        /**
467         * Sets the document media size.
468         * <p>
469         * <strong>Default: </strong>#MEDIA_SIZE_UNKNOWN
470         * </p>
471         *
472         * @param mediaSize The media size. Cannot be null.
473         *
474         * @see #MEDIA_SIZE_UNKNOWN
475         */
476        public Builder setMediaSize(MediaSize mediaSize) {
477            if (mediaSize == null) {
478                throw new IllegalArgumentException("media size cannot be null");
479            }
480            mPrototype.mMediaSize = mediaSize;
481            return this;
482        }
483
484        /**
485         * Creates a new {@link PrintDocumentInfo} instance.
486         *
487         * @return The new instance.
488         */
489        public PrintDocumentInfo create() {
490            return new PrintDocumentInfo(mPrototype);
491        }
492    }
493
494    public static final Parcelable.Creator<PrintDocumentInfo> CREATOR =
495            new Creator<PrintDocumentInfo>() {
496        @Override
497        public PrintDocumentInfo createFromParcel(Parcel parcel) {
498            return new PrintDocumentInfo(parcel);
499        }
500
501        @Override
502        public PrintDocumentInfo[] newArray(int size) {
503            return new PrintDocumentInfo[size];
504        }
505    };
506}
507