blob: 83efe8020d984b7cc759b1c57018e17b3250829a [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
Philip P. Moltmannc43639c2015-12-18 13:58:40 -080019import android.annotation.NonNull;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070020import android.content.ComponentName;
21import android.os.Parcel;
22import android.os.Parcelable;
Svetoslav Ganov55b409a2013-07-31 17:25:13 -070023import android.text.TextUtils;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070024
25/**
26 * This class represents the unique id of a printer.
27 */
28public final class PrinterId implements Parcelable {
29
Svetoslav Ganov8c433762013-08-02 14:22:19 -070030 private final ComponentName mServiceName;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070031
Svetoslav Ganov798bed62013-08-11 12:29:39 -070032 private final String mLocalId;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070033
34 /**
35 * Creates a new instance.
36 *
Svetoslav Ganov8c433762013-08-02 14:22:19 -070037 * @param serviceName The managing print service.
Svetoslav Ganov798bed62013-08-11 12:29:39 -070038 * @param localId The locally unique id within the managing service.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070039 *
40 * @hide
41 */
Svetoslav Ganov798bed62013-08-11 12:29:39 -070042 public PrinterId(ComponentName serviceName, String localId) {
Svetoslav Ganov8c433762013-08-02 14:22:19 -070043 mServiceName = serviceName;
Svetoslav Ganov798bed62013-08-11 12:29:39 -070044 mLocalId = localId;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070045 }
46
47 private PrinterId(Parcel parcel) {
Svetoslav Ganov8c433762013-08-02 14:22:19 -070048 mServiceName = parcel.readParcelable(null);
Svetoslav Ganov798bed62013-08-11 12:29:39 -070049 mLocalId = parcel.readString();
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070050 }
51
52 /**
53 * The id of the print service this printer is managed by.
54 *
55 * @return The print service component name.
56 *
57 * @hide
58 */
Svetoslav Ganov8c433762013-08-02 14:22:19 -070059 public ComponentName getServiceName() {
60 return mServiceName;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070061 }
62
63 /**
Svetoslav Ganov798bed62013-08-11 12:29:39 -070064 * Gets the id of this printer which is unique in the context
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070065 * of the print service that manages it.
66 *
Svetoslav Ganov8c433762013-08-02 14:22:19 -070067 * @return The printer name.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070068 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -080069 public @NonNull String getLocalId() {
Svetoslav Ganov798bed62013-08-11 12:29:39 -070070 return mLocalId;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070071 }
72
73 @Override
74 public int describeContents() {
75 return 0;
76 }
77
78 @Override
79 public void writeToParcel(Parcel parcel, int flags) {
Svetoslav Ganov8c433762013-08-02 14:22:19 -070080 parcel.writeParcelable(mServiceName, flags);
Svetoslav Ganov798bed62013-08-11 12:29:39 -070081 parcel.writeString(mLocalId);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070082 }
83
84 @Override
85 public boolean equals(Object object) {
86 if (this == object) {
87 return true;
88 }
89 if (object == null) {
90 return false;
91 }
92 if (getClass() != object.getClass()) {
93 return false;
94 }
95 PrinterId other = (PrinterId) object;
Svetoslav Ganov8c433762013-08-02 14:22:19 -070096 if (mServiceName == null) {
97 if (other.mServiceName != null) {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070098 return false;
99 }
Svetoslav Ganov8c433762013-08-02 14:22:19 -0700100 } else if (!mServiceName.equals(other.mServiceName)) {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700101 return false;
102 }
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700103 if (!TextUtils.equals(mLocalId, other.mLocalId)) {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700104 return false;
105 }
106 return true;
107 }
108
109 @Override
110 public int hashCode() {
111 final int prime = 31;
112 int hashCode = 1;
Svetoslav Ganov8c433762013-08-02 14:22:19 -0700113 hashCode = prime * hashCode + ((mServiceName != null)
114 ? mServiceName.hashCode() : 1);
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700115 hashCode = prime * hashCode + mLocalId.hashCode();
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700116 return hashCode;
117 }
118
119 @Override
120 public String toString() {
121 StringBuilder builder = new StringBuilder();
122 builder.append("PrinterId{");
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700123 builder.append("serviceName=").append(mServiceName.flattenToString());
124 builder.append(", localId=").append(mLocalId);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700125 builder.append('}');
126 return builder.toString();
127 }
128
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700129 public static final Parcelable.Creator<PrinterId> CREATOR =
130 new Creator<PrinterId>() {
131 @Override
132 public PrinterId createFromParcel(Parcel parcel) {
133 return new PrinterId(parcel);
134 }
135
136 @Override
137 public PrinterId[] newArray(int size) {
138 return new PrinterId[size];
139 }
140 };
141}