blob: a3f3b2bfee11ba37f65eadfcdbd2ca7ea129923d [file] [log] [blame]
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -07001/*
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;
Svetoslav Ganov55b409a2013-07-31 17:25:13 -070022import android.text.TextUtils;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070023
24/**
25 * This class represents the unique id of a printer.
26 */
27public final class PrinterId implements Parcelable {
28
Svetoslav Ganov8c433762013-08-02 14:22:19 -070029 private final ComponentName mServiceName;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070030
Svetoslav Ganov798bed62013-08-11 12:29:39 -070031 private final String mLocalId;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070032
33 /**
34 * Creates a new instance.
35 *
Svetoslav Ganov8c433762013-08-02 14:22:19 -070036 * @param serviceName The managing print service.
Svetoslav Ganov798bed62013-08-11 12:29:39 -070037 * @param localId The locally unique id within the managing service.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070038 *
39 * @hide
40 */
Svetoslav Ganov798bed62013-08-11 12:29:39 -070041 public PrinterId(ComponentName serviceName, String localId) {
Svetoslav Ganov8c433762013-08-02 14:22:19 -070042 mServiceName = serviceName;
Svetoslav Ganov798bed62013-08-11 12:29:39 -070043 mLocalId = localId;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070044 }
45
46 private PrinterId(Parcel parcel) {
Svetoslav Ganov8c433762013-08-02 14:22:19 -070047 mServiceName = parcel.readParcelable(null);
Svetoslav Ganov798bed62013-08-11 12:29:39 -070048 mLocalId = parcel.readString();
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070049 }
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 */
Svetoslav Ganov8c433762013-08-02 14:22:19 -070058 public ComponentName getServiceName() {
59 return mServiceName;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070060 }
61
62 /**
Svetoslav Ganov798bed62013-08-11 12:29:39 -070063 * Gets the id of this printer which is unique in the context
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070064 * of the print service that manages it.
65 *
Svetoslav Ganov8c433762013-08-02 14:22:19 -070066 * @return The printer name.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070067 */
Svetoslav Ganov798bed62013-08-11 12:29:39 -070068 public String getLocalId() {
69 return mLocalId;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070070 }
71
72 @Override
73 public int describeContents() {
74 return 0;
75 }
76
77 @Override
78 public void writeToParcel(Parcel parcel, int flags) {
Svetoslav Ganov8c433762013-08-02 14:22:19 -070079 parcel.writeParcelable(mServiceName, flags);
Svetoslav Ganov798bed62013-08-11 12:29:39 -070080 parcel.writeString(mLocalId);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070081 }
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;
Svetoslav Ganov8c433762013-08-02 14:22:19 -070095 if (mServiceName == null) {
96 if (other.mServiceName != null) {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070097 return false;
98 }
Svetoslav Ganov8c433762013-08-02 14:22:19 -070099 } else if (!mServiceName.equals(other.mServiceName)) {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700100 return false;
101 }
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700102 if (!TextUtils.equals(mLocalId, other.mLocalId)) {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700103 return false;
104 }
105 return true;
106 }
107
108 @Override
109 public int hashCode() {
110 final int prime = 31;
111 int hashCode = 1;
Svetoslav Ganov8c433762013-08-02 14:22:19 -0700112 hashCode = prime * hashCode + ((mServiceName != null)
113 ? mServiceName.hashCode() : 1);
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700114 hashCode = prime * hashCode + mLocalId.hashCode();
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700115 return hashCode;
116 }
117
118 @Override
119 public String toString() {
120 StringBuilder builder = new StringBuilder();
121 builder.append("PrinterId{");
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700122 builder.append("serviceName=").append(mServiceName.flattenToString());
123 builder.append(", localId=").append(mLocalId);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700124 builder.append('}');
125 return builder.toString();
126 }
127
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700128 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}