PrinterInfo.java revision 651dd4e6ee6510caf9f15c51094a11121af17ec2
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 represents the description of a printer.
25 */
26public final class PrinterInfo implements Parcelable {
27
28    /** Printer status: the printer is idle and ready to print. */
29    public static final int STATUS_IDLE = 1;
30
31    /** Printer status: the printer is busy printing. */
32    public static final int STATUS_BUSY = 2;
33
34    /** Printer status: the printer is not available. */
35    public static final int STATUS_UNAVAILABLE = 3;
36
37    private PrinterId mId;
38
39    private String mName;
40
41    private int mStatus;
42
43    private String mDescription;
44
45    private PrinterCapabilitiesInfo mCapabilities;
46
47    private PrinterInfo() {
48        /* do nothing */
49    }
50
51    private PrinterInfo(PrinterInfo prototype) {
52        copyFrom(prototype);
53    }
54
55    /**
56     * @hide
57     */
58    public void copyFrom(PrinterInfo other) {
59        if (this == other) {
60            return;
61        }
62        mId = other.mId;
63        mName = other.mName;
64        mStatus = other.mStatus;
65        mDescription = other.mDescription;
66        if (other.mCapabilities != null) {
67            if (mCapabilities != null) {
68                mCapabilities.copyFrom(other.mCapabilities);
69            } else {
70                mCapabilities = new PrinterCapabilitiesInfo(other.mCapabilities);
71            }
72        } else {
73            mCapabilities = null;
74        }
75    }
76
77    /**
78     * Get the globally unique printer id.
79     *
80     * @return The printer id.
81     */
82    public PrinterId getId() {
83        return mId;
84    }
85
86    /**
87     * Get the printer name.
88     *
89     * @return The printer name.
90     */
91    public String getName() {
92        return mName;
93    }
94
95    /**
96     * Gets the printer status.
97     *
98     * @return The status.
99     */
100    public int getStatus() {
101        return mStatus;
102    }
103
104    /**
105     * Gets the  printer description.
106     *
107     * @return The description.
108     */
109    public String getDescription() {
110        return mDescription;
111    }
112
113    /**
114     * Gets the printer capabilities.
115     *
116     * @return The capabilities.
117     */
118    public PrinterCapabilitiesInfo getCapabilities() {
119        return mCapabilities;
120    }
121
122    private PrinterInfo(Parcel parcel) {
123        mId = parcel.readParcelable(null);
124        mName = parcel.readString();
125        mStatus = parcel.readInt();
126        mDescription = parcel.readString();
127        mCapabilities = parcel.readParcelable(null);
128    }
129
130    @Override
131    public int describeContents() {
132        return 0;
133    }
134
135    @Override
136    public void writeToParcel(Parcel parcel, int flags) {
137        parcel.writeParcelable(mId, flags);
138        parcel.writeString(mName);
139        parcel.writeInt(mStatus);
140        parcel.writeString(mDescription);
141        parcel.writeParcelable(mCapabilities, flags);
142    }
143
144    @Override
145    public int hashCode() {
146        final int prime = 31;
147        int result = 1;
148        result = prime * result + ((mId != null) ? mId.hashCode() : 0);
149        result = prime * result + ((mName != null) ? mName.hashCode() : 0);
150        result = prime * result + mStatus;
151        result = prime * result + ((mDescription != null) ? mDescription.hashCode() : 0);
152        result = prime * result + ((mCapabilities != null) ? mCapabilities.hashCode() : 0);
153        return result;
154    }
155
156    @Override
157    public boolean equals(Object obj) {
158        if (this == obj) {
159            return true;
160        }
161        if (obj == null) {
162            return false;
163        }
164        if (getClass() != obj.getClass()) {
165            return false;
166        }
167        PrinterInfo other = (PrinterInfo) obj;
168        if (mId == null) {
169            if (other.mId != null) {
170                return false;
171            }
172        } else if (!mId.equals(other.mId)) {
173            return false;
174        }
175        if (!TextUtils.equals(mName, other.mName)) {
176            return false;
177        }
178        if (mStatus != other.mStatus) {
179            return false;
180        }
181        if (!TextUtils.equals(mDescription, other.mDescription)) {
182            return false;
183        }
184        if (mCapabilities == null) {
185            if (other.mCapabilities != null) {
186                return false;
187            }
188        } else if (!mCapabilities.equals(other.mCapabilities)) {
189            return false;
190        }
191        return true;
192    }
193
194    @Override
195    public String toString() {
196        StringBuilder builder = new StringBuilder();
197        builder.append("PrinterInfo{");
198        builder.append("id=").append(mId);
199        builder.append(", name=").append(mName);
200        builder.append(", status=").append(mStatus);
201        builder.append(", description=").append(mDescription);
202        builder.append(", capabilities=").append(mCapabilities);
203        builder.append("\"}");
204        return builder.toString();
205    }
206
207    /**
208     * Builder for creating of a {@link PrinterInfo}.
209     */
210    public static final class Builder {
211        private final PrinterInfo mPrototype;
212
213        /**
214         * Constructor.
215         *
216         * @param printerId The printer id. Cannot be null.
217         * @param name The printer name. Cannot be empty.
218         * @param status The printer status. Must be a valid status.
219         */
220        public Builder(PrinterId printerId, String name, int status) {
221            if (printerId == null) {
222                throw new IllegalArgumentException("printerId cannot be null.");
223            }
224            if (TextUtils.isEmpty(name)) {
225                throw new IllegalArgumentException("name cannot be empty.");
226            }
227            if (!isValidStatus(status)) {
228                throw new IllegalArgumentException("status is invalid.");
229            }
230            mPrototype = new PrinterInfo();
231            mPrototype.mId = printerId;
232            mPrototype.mName = name;
233            mPrototype.mStatus = status;
234        }
235
236        /**
237         * Constructor.
238         *
239         * @param other Other info from which to start building.
240         */
241        public Builder(PrinterInfo other) {
242            mPrototype = new PrinterInfo();
243            mPrototype.copyFrom(other);
244        }
245
246        /**
247         * Sets the printer status.
248         *
249         * @param status The status.
250         * @return This builder.
251         *
252         * @see PrinterInfo#STATUS_IDLE
253         * @see PrinterInfo#STATUS_BUSY
254         * @see PrinterInfo#STATUS_UNAVAILABLE
255         */
256        public Builder setStatus(int status) {
257            mPrototype.mStatus = status;
258            return this;
259        }
260
261        /**
262         * Sets the printer name.
263         *
264         * @param name The name.
265         * @return This builder.
266         */
267        public Builder setName(String name) {
268            mPrototype.mName = name;
269            return this;
270        }
271
272        /**
273         * Sets the printer description.
274         *
275         * @param description The description.
276         * @return This builder.
277         */
278        public Builder setDescription(String description) {
279            mPrototype.mDescription = description;
280            return this;
281        }
282
283        /**
284         * Sets the printer capabilities.
285         *
286         * @param capabilities The capabilities.
287         * @return This builder.
288         */
289        public Builder setCapabilities(PrinterCapabilitiesInfo capabilities) {
290            mPrototype.mCapabilities = capabilities;
291            return this;
292        }
293
294        /**
295         * Crates a new {@link PrinterInfo}.
296         *
297         * @return A new {@link PrinterInfo}.
298         */
299        public PrinterInfo build() {
300            return new PrinterInfo(mPrototype);
301        }
302
303        private boolean isValidStatus(int status) {
304            return (status == STATUS_IDLE
305                    || status == STATUS_IDLE
306                    || status == STATUS_UNAVAILABLE);
307        }
308    }
309
310    public static final Parcelable.Creator<PrinterInfo> CREATOR =
311            new Parcelable.Creator<PrinterInfo>() {
312        @Override
313        public PrinterInfo createFromParcel(Parcel parcel) {
314            return new PrinterInfo(parcel);
315        }
316
317        @Override
318        public PrinterInfo[] newArray(int size) {
319            return new PrinterInfo[size];
320        }
321    };
322}
323