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; |
| 22 | |
| 23 | /** |
| 24 | * This class represents the unique id of a printer. |
| 25 | */ |
| 26 | public final class PrinterId implements Parcelable { |
| 27 | |
| 28 | private final ComponentName mServiceComponentName; |
| 29 | |
| 30 | private final String mLocalId; |
| 31 | |
| 32 | /** |
| 33 | * Creates a new instance. |
| 34 | * |
| 35 | * @param serviceComponentName The managing print service. |
| 36 | * @param localId The unique id within the managing service. |
| 37 | * |
| 38 | * @hide |
| 39 | */ |
| 40 | public PrinterId(ComponentName serviceComponentName, String localId) { |
| 41 | mServiceComponentName = serviceComponentName; |
| 42 | mLocalId = localId; |
| 43 | } |
| 44 | |
| 45 | private PrinterId(Parcel parcel) { |
| 46 | mServiceComponentName = parcel.readParcelable(null); |
| 47 | mLocalId = parcel.readString(); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * The id of the print service this printer is managed by. |
| 52 | * |
| 53 | * @return The print service component name. |
| 54 | * |
| 55 | * @hide |
| 56 | */ |
Svetoslav Ganov | a002715 | 2013-06-25 14:59:53 -0700 | [diff] [blame] | 57 | public ComponentName getService() { |
Svetoslav Ganov | 4b9a4d1 | 2013-06-11 15:20:06 -0700 | [diff] [blame] | 58 | return mServiceComponentName; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Gets the local id of this printer in the context |
| 63 | * of the print service that manages it. |
| 64 | * |
| 65 | * @return The local id. |
| 66 | */ |
| 67 | public String getLocalId() { |
| 68 | return mLocalId; |
| 69 | } |
| 70 | |
| 71 | @Override |
| 72 | public int describeContents() { |
| 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void writeToParcel(Parcel parcel, int flags) { |
| 78 | parcel.writeParcelable(mServiceComponentName, flags); |
| 79 | parcel.writeString(mLocalId); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public boolean equals(Object object) { |
| 84 | if (this == object) { |
| 85 | return true; |
| 86 | } |
| 87 | if (object == null) { |
| 88 | return false; |
| 89 | } |
| 90 | if (getClass() != object.getClass()) { |
| 91 | return false; |
| 92 | } |
| 93 | PrinterId other = (PrinterId) object; |
| 94 | if (mServiceComponentName == null) { |
| 95 | if (other.mServiceComponentName != null) { |
| 96 | return false; |
| 97 | } |
| 98 | } else if (!mServiceComponentName.equals(other.mServiceComponentName)) { |
| 99 | return false; |
| 100 | } |
| 101 | if (mLocalId != other.mLocalId) { |
| 102 | return false; |
| 103 | } |
| 104 | return true; |
| 105 | } |
| 106 | |
| 107 | @Override |
| 108 | public int hashCode() { |
| 109 | final int prime = 31; |
| 110 | int hashCode = 1; |
| 111 | hashCode = prime * hashCode + ((mServiceComponentName != null) |
| 112 | ? mServiceComponentName.hashCode() : 1); |
| 113 | hashCode = prime * hashCode + mLocalId.hashCode(); |
| 114 | return hashCode; |
| 115 | } |
| 116 | |
| 117 | @Override |
| 118 | public String toString() { |
| 119 | StringBuilder builder = new StringBuilder(); |
| 120 | builder.append("PrinterId{"); |
| 121 | builder.append(mServiceComponentName.flattenToString()); |
| 122 | builder.append(":"); |
| 123 | builder.append(mLocalId); |
| 124 | builder.append('}'); |
| 125 | return builder.toString(); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * @hide |
| 130 | */ |
| 131 | public String flattenToString() { |
| 132 | return mServiceComponentName.flattenToString() + ":" + mLocalId; |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * @hide |
| 137 | */ |
| 138 | public static PrinterId unflattenFromString(String string) { |
| 139 | String[] split = string.split(":"); |
| 140 | if (split.length != 2) { |
| 141 | throw new IllegalArgumentException("Not well-formed printer id:" + string); |
| 142 | } |
| 143 | ComponentName component = ComponentName.unflattenFromString(split[0]); |
| 144 | String localId = String.valueOf(split[1]); |
| 145 | return new PrinterId(component, localId); |
| 146 | } |
| 147 | |
| 148 | public static final Parcelable.Creator<PrinterId> CREATOR = |
| 149 | new Creator<PrinterId>() { |
| 150 | @Override |
| 151 | public PrinterId createFromParcel(Parcel parcel) { |
| 152 | return new PrinterId(parcel); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public PrinterId[] newArray(int size) { |
| 157 | return new PrinterId[size]; |
| 158 | } |
| 159 | }; |
| 160 | } |