PrintDocumentInfo.java revision 773f54de3de9bce7b6f915aa47ed686b161d77aa
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..
30     */
31    public static final int PAGE_COUNT_UNKNOWN = -1;
32
33    /**
34     * Content type: unknown.
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    private long mDataSize;
52
53    /**
54     * Creates a new instance.
55     */
56    private PrintDocumentInfo() {
57        /* do nothing */
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        mDataSize = prototype.mDataSize;
70    }
71
72    /**
73     * Creates a new instance.
74     *
75     * @param parcel Data from which to initialize.
76     */
77    private PrintDocumentInfo(Parcel parcel) {
78        mName = parcel.readString();
79        mPageCount = parcel.readInt();
80        mContentType = parcel.readInt();
81        mDataSize = parcel.readLong();
82    }
83
84    /**
85     * Gets the document name.
86     *
87     * @return The document name.
88     */
89    public String getName() {
90        return mName;
91    }
92
93    /**
94     * Gets the total number of pages.
95     *
96     * @return The number of pages.
97     *
98     * @see #PAGE_COUNT_UNKNOWN
99     */
100    public int getPageCount() {
101        return mPageCount;
102    }
103
104    /**
105     * Gets the content type.
106     *
107     * @return The content type.
108     *
109     * @see #CONTENT_TYPE_UNKNOWN
110     * @see #CONTENT_TYPE_DOCUMENT
111     * @see #CONTENT_TYPE_PHOTO
112     */
113    public int getContentType() {
114        return mContentType;
115    }
116
117    /**
118     * Gets the document data size in bytes.
119     *
120     * @return The data size.
121     */
122    public long getDataSize() {
123        return mDataSize;
124    }
125
126    /**
127     * Sets the document data size in bytes.
128     *
129     * @param dataSize The data size.
130     *
131     * @hide
132     */
133    public void setDataSize(long dataSize) {
134        mDataSize = dataSize;
135    }
136
137    @Override
138    public int describeContents() {
139        return 0;
140    }
141
142    @Override
143    public void writeToParcel(Parcel parcel, int flags) {
144        parcel.writeString(mName);
145        parcel.writeInt(mPageCount);
146        parcel.writeInt(mContentType);
147        parcel.writeLong(mDataSize);
148    }
149
150    @Override
151    public int hashCode() {
152        final int prime = 31;
153        int result = 1;
154        result = prime * result + ((mName != null) ? mName.hashCode() : 0);
155        result = prime * result + mContentType;
156        result = prime * result + mPageCount;
157        result = prime * result + (int) mDataSize;
158        result = prime * result + (int) mDataSize >> 32;
159        return result;
160    }
161
162    @Override
163    public boolean equals(Object obj) {
164        if (this == obj) {
165            return true;
166        }
167        if (obj == null) {
168            return false;
169        }
170        if (getClass() != obj.getClass()) {
171            return false;
172        }
173        PrintDocumentInfo other = (PrintDocumentInfo) obj;
174        if (!TextUtils.equals(mName, other.mName)) {
175            return false;
176        }
177        if (mContentType != other.mContentType) {
178            return false;
179        }
180        if (mPageCount != other.mPageCount) {
181            return false;
182        }
183        if (mDataSize != other.mDataSize) {
184            return false;
185        }
186        return true;
187    }
188
189    @Override
190    public String toString() {
191        StringBuilder builder = new StringBuilder();
192        builder.append("PrintDocumentInfo{");
193        builder.append("name=").append(mName);
194        builder.append(", pageCount=").append(mPageCount);
195        builder.append(", contentType=").append(contentTyepToString(mContentType));
196        builder.append(", size=").append(mDataSize);
197        builder.append("}");
198        return builder.toString();
199    }
200
201    private String contentTyepToString(int contentType) {
202        switch (contentType) {
203            case CONTENT_TYPE_DOCUMENT: {
204                return "CONTENT_TYPE_DOCUMENT";
205            }
206            case CONTENT_TYPE_PHOTO: {
207                return "CONTENT_TYPE_PHOTO";
208            }
209            default: {
210                return "CONTENT_TYPE_UNKNOWN";
211            }
212        }
213    }
214
215    /**
216     * Builder for creating an {@link PrintDocumentInfo}.
217     */
218    public static final class Builder {
219        private final PrintDocumentInfo mPrototype;
220
221        /**
222         * Constructor.
223         * <p>
224         * The values of the relevant properties are initialized with default
225         * values. Please refer to the documentation of the individual setters
226         * for information about the default values.
227         * </p>
228         *
229         * @param name The document name. Cannot be empty.
230         */
231        public Builder(String name) {
232            if (TextUtils.isEmpty(name)) {
233                throw new IllegalArgumentException("name cannot be empty");
234            }
235            mPrototype = new PrintDocumentInfo();
236            mPrototype.mName = name;
237        }
238
239        /**
240         * Sets the total number of pages.
241         * <p>
242         * <strong>Default: </strong> {@link #PAGE_COUNT_UNKNOWN}
243         * </p>
244         *
245         * @param pageCount The number of pages. Must be greater than
246         * or equal to zero or {@link PrintDocumentInfo#PAGE_COUNT_UNKNOWN}.
247         */
248        public Builder setPageCount(int pageCount) {
249            if (pageCount < 0 && pageCount != PAGE_COUNT_UNKNOWN) {
250                throw new IllegalArgumentException("pageCount"
251                        + " must be greater than or euqal to zero or"
252                        + " DocumentInfo#PAGE_COUNT_UNKNOWN");
253            }
254            mPrototype.mPageCount = pageCount;
255            return this;
256        }
257
258        /**
259         * Sets the content type.
260         * <p>
261         * <strong>Default: </strong> {@link #CONTENT_TYPE_UNKNOWN}
262         * </p>
263         *
264         * @param type The content type.
265         *
266         * @see #CONTENT_TYPE_UNKNOWN
267         * @see #CONTENT_TYPE_DOCUMENT
268         * @see #CONTENT_TYPE_PHOTO
269         */
270        public Builder setContentType(int type) {
271            mPrototype.mContentType = type;
272            return this;
273        }
274
275        /**
276         * Creates a new {@link PrintDocumentInfo} instance.
277         *
278         * @return The new instance.
279         */
280        public PrintDocumentInfo create() {
281            return new PrintDocumentInfo(mPrototype);
282        }
283    }
284
285    public static final Parcelable.Creator<PrintDocumentInfo> CREATOR =
286            new Creator<PrintDocumentInfo>() {
287        @Override
288        public PrintDocumentInfo createFromParcel(Parcel parcel) {
289            return new PrintDocumentInfo(parcel);
290        }
291
292        @Override
293        public PrintDocumentInfo[] newArray(int size) {
294            return new PrintDocumentInfo[size];
295        }
296    };
297}
298