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