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