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