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