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