Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 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 | |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 19 | import android.annotation.NonNull; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 20 | import android.content.ComponentName; |
| 21 | import android.os.Parcel; |
| 22 | import android.os.Parcelable; |
Svetoslav Ganov | 55b409a | 2013-07-31 17:25:13 -0700 | [diff] [blame] | 23 | import android.text.TextUtils; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 24 | |
| 25 | /** |
| 26 | * This class represents the unique id of a printer. |
| 27 | */ |
| 28 | public final class PrinterId implements Parcelable { |
| 29 | |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 30 | private final ComponentName mServiceName; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 31 | |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 32 | private final String mLocalId; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Creates a new instance. |
| 36 | * |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 37 | * @param serviceName The managing print service. |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 38 | * @param localId The locally unique id within the managing service. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 39 | * |
| 40 | * @hide |
| 41 | */ |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 42 | public PrinterId(ComponentName serviceName, String localId) { |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 43 | mServiceName = serviceName; |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 44 | mLocalId = localId; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | private PrinterId(Parcel parcel) { |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 48 | mServiceName = parcel.readParcelable(null); |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 49 | mLocalId = parcel.readString(); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 50 | } |
| 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 Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 59 | public ComponentName getServiceName() { |
| 60 | return mServiceName; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 64 | * Gets the id of this printer which is unique in the context |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 65 | * of the print service that manages it. |
| 66 | * |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 67 | * @return The printer name. |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 68 | */ |
Philip P. Moltmann | c43639c | 2015-12-18 13:58:40 -0800 | [diff] [blame] | 69 | public @NonNull String getLocalId() { |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 70 | return mLocalId; |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
| 74 | public int describeContents() { |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | @Override |
| 79 | public void writeToParcel(Parcel parcel, int flags) { |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 80 | parcel.writeParcelable(mServiceName, flags); |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 81 | parcel.writeString(mLocalId); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 82 | } |
| 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 Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 96 | if (mServiceName == null) { |
| 97 | if (other.mServiceName != null) { |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 98 | return false; |
| 99 | } |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 100 | } else if (!mServiceName.equals(other.mServiceName)) { |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 101 | return false; |
| 102 | } |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 103 | if (!TextUtils.equals(mLocalId, other.mLocalId)) { |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 104 | return false; |
| 105 | } |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public int hashCode() { |
| 111 | final int prime = 31; |
| 112 | int hashCode = 1; |
Svetoslav Ganov | 8c43376 | 2013-08-02 14:22:19 -0700 | [diff] [blame] | 113 | hashCode = prime * hashCode + ((mServiceName != null) |
| 114 | ? mServiceName.hashCode() : 1); |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 115 | hashCode = prime * hashCode + mLocalId.hashCode(); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 116 | return hashCode; |
| 117 | } |
| 118 | |
| 119 | @Override |
| 120 | public String toString() { |
| 121 | StringBuilder builder = new StringBuilder(); |
| 122 | builder.append("PrinterId{"); |
Svetoslav Ganov | 798bed6 | 2013-08-11 12:29:39 -0700 | [diff] [blame] | 123 | builder.append("serviceName=").append(mServiceName.flattenToString()); |
| 124 | builder.append(", localId=").append(mLocalId); |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 125 | builder.append('}'); |
| 126 | return builder.toString(); |
| 127 | } |
| 128 | |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 129 | 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 | } |