blob: 42570c69650f27af51792887b0b2811c8073d083 [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;
Mathew Inwood06bf4b72018-07-31 16:36:56 +010020import android.annotation.UnsupportedAppUsage;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070021import android.content.ComponentName;
22import android.os.Parcel;
23import android.os.Parcelable;
Philip P. Moltmanncdf2b402016-01-14 14:45:04 -080024
25import com.android.internal.util.Preconditions;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070026
27/**
28 * This class represents the unique id of a printer.
29 */
30public final class PrinterId implements Parcelable {
31
Philip P. Moltmanncdf2b402016-01-14 14:45:04 -080032 private final @NonNull ComponentName mServiceName;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070033
Philip P. Moltmanncdf2b402016-01-14 14:45:04 -080034 private final @NonNull String mLocalId;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070035
36 /**
37 * Creates a new instance.
38 *
Svetoslav Ganov8c433762013-08-02 14:22:19 -070039 * @param serviceName The managing print service.
Svetoslav Ganov798bed62013-08-11 12:29:39 -070040 * @param localId The locally unique id within the managing service.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070041 *
42 * @hide
43 */
Philip P. Moltmanncdf2b402016-01-14 14:45:04 -080044 public PrinterId(@NonNull ComponentName serviceName, @NonNull String localId) {
Svetoslav Ganov8c433762013-08-02 14:22:19 -070045 mServiceName = serviceName;
Svetoslav Ganov798bed62013-08-11 12:29:39 -070046 mLocalId = localId;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070047 }
48
Philip P. Moltmanncdf2b402016-01-14 14:45:04 -080049 private PrinterId(@NonNull Parcel parcel) {
50 mServiceName = Preconditions.checkNotNull((ComponentName) parcel.readParcelable(null));
51 mLocalId = Preconditions.checkNotNull(parcel.readString());
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070052 }
53
54 /**
55 * The id of the print service this printer is managed by.
56 *
57 * @return The print service component name.
58 *
59 * @hide
60 */
Mathew Inwood06bf4b72018-07-31 16:36:56 +010061 @UnsupportedAppUsage
Philip P. Moltmanncdf2b402016-01-14 14:45:04 -080062 public @NonNull ComponentName getServiceName() {
Svetoslav Ganov8c433762013-08-02 14:22:19 -070063 return mServiceName;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070064 }
65
66 /**
Svetoslav Ganov798bed62013-08-11 12:29:39 -070067 * Gets the id of this printer which is unique in the context
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070068 * of the print service that manages it.
69 *
Svetoslav Ganov8c433762013-08-02 14:22:19 -070070 * @return The printer name.
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070071 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -080072 public @NonNull String getLocalId() {
Svetoslav Ganov798bed62013-08-11 12:29:39 -070073 return mLocalId;
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070074 }
75
76 @Override
77 public int describeContents() {
78 return 0;
79 }
80
81 @Override
82 public void writeToParcel(Parcel parcel, int flags) {
Svetoslav Ganov8c433762013-08-02 14:22:19 -070083 parcel.writeParcelable(mServiceName, flags);
Svetoslav Ganov798bed62013-08-11 12:29:39 -070084 parcel.writeString(mLocalId);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -070085 }
86
87 @Override
88 public boolean equals(Object object) {
89 if (this == object) {
90 return true;
91 }
92 if (object == null) {
93 return false;
94 }
95 if (getClass() != object.getClass()) {
96 return false;
97 }
98 PrinterId other = (PrinterId) object;
Philip P. Moltmanncdf2b402016-01-14 14:45:04 -080099 if (!mServiceName.equals(other.mServiceName)) {
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700100 return false;
101 }
Philip P. Moltmanncdf2b402016-01-14 14:45:04 -0800102 if (!mLocalId.equals(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;
Philip P. Moltmanncdf2b402016-01-14 14:45:04 -0800112 hashCode = prime * hashCode + mServiceName.hashCode();
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700113 hashCode = prime * hashCode + mLocalId.hashCode();
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700114 return hashCode;
115 }
116
117 @Override
118 public String toString() {
119 StringBuilder builder = new StringBuilder();
120 builder.append("PrinterId{");
Svetoslav Ganov798bed62013-08-11 12:29:39 -0700121 builder.append("serviceName=").append(mServiceName.flattenToString());
122 builder.append(", localId=").append(mLocalId);
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700123 builder.append('}');
124 return builder.toString();
125 }
126
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700127 public static final @android.annotation.NonNull Parcelable.Creator<PrinterId> CREATOR =
Svetoslav Ganov4b9a4d12013-06-11 15:20:06 -0700128 new Creator<PrinterId>() {
129 @Override
130 public PrinterId createFromParcel(Parcel parcel) {
131 return new PrinterId(parcel);
132 }
133
134 @Override
135 public PrinterId[] newArray(int size) {
136 return new PrinterId[size];
137 }
138 };
139}