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.content.ComponentName;
20import android.os.Parcel;
21import android.os.Parcelable;
22import android.text.TextUtils;
23
24/**
25 * This class represents the unique id of a printer.
26 */
27public final class PrinterId implements Parcelable {
28
29    private final ComponentName mServiceName;
30
31    private final String mLocalId;
32
33    /**
34     * Creates a new instance.
35     *
36     * @param serviceName The managing print service.
37     * @param localId The locally unique id within the managing service.
38     *
39     * @hide
40     */
41    public PrinterId(ComponentName serviceName, String localId) {
42        mServiceName = serviceName;
43        mLocalId = localId;
44    }
45
46    private PrinterId(Parcel parcel) {
47        mServiceName = parcel.readParcelable(null);
48        mLocalId = parcel.readString();
49    }
50
51    /**
52     * The id of the print service this printer is managed by.
53     *
54     * @return The print service component name.
55     *
56     * @hide
57     */
58    public ComponentName getServiceName() {
59        return mServiceName;
60    }
61
62    /**
63     * Gets the id of this printer which is unique in the context
64     * of the print service that manages it.
65     *
66     * @return The printer name.
67     */
68    public String getLocalId() {
69        return mLocalId;
70    }
71
72    @Override
73    public int describeContents() {
74        return 0;
75    }
76
77    @Override
78    public void writeToParcel(Parcel parcel, int flags) {
79        parcel.writeParcelable(mServiceName, flags);
80        parcel.writeString(mLocalId);
81    }
82
83    @Override
84    public boolean equals(Object object) {
85        if (this == object) {
86            return true;
87        }
88        if (object == null) {
89            return false;
90        }
91        if (getClass() != object.getClass()) {
92            return false;
93        }
94        PrinterId other = (PrinterId) object;
95        if (mServiceName == null) {
96            if (other.mServiceName != null) {
97                return false;
98            }
99        } else if (!mServiceName.equals(other.mServiceName)) {
100            return false;
101        }
102        if (!TextUtils.equals(mLocalId, other.mLocalId)) {
103            return false;
104        }
105        return true;
106    }
107
108    @Override
109    public int hashCode() {
110        final int prime = 31;
111        int hashCode = 1;
112        hashCode = prime * hashCode + ((mServiceName != null)
113                ? mServiceName.hashCode() : 1);
114        hashCode = prime * hashCode + mLocalId.hashCode();
115        return hashCode;
116    }
117
118    @Override
119    public String toString() {
120        StringBuilder builder = new StringBuilder();
121        builder.append("PrinterId{");
122        builder.append("serviceName=").append(mServiceName.flattenToString());
123        builder.append(", localId=").append(mLocalId);
124        builder.append('}');
125        return builder.toString();
126    }
127
128    public static final Parcelable.Creator<PrinterId> CREATOR =
129            new Creator<PrinterId>() {
130        @Override
131        public PrinterId createFromParcel(Parcel parcel) {
132            return new PrinterId(parcel);
133        }
134
135        @Override
136        public PrinterId[] newArray(int size) {
137            return new PrinterId[size];
138        }
139    };
140}
141