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