UsbAccessory.java revision 015b1ecaec27b7cf5f1a78099d9ae34a0c3169f2
1/* 2 * Copyright (C) 2011 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.hardware.usb; 18 19import android.os.Bundle; 20import android.os.Parcel; 21import android.os.Parcelable; 22import android.util.Log; 23 24/** 25 * A class representing a USB accessory. 26 */ 27public class UsbAccessory implements Parcelable { 28 29 private static final String TAG = "UsbAccessory"; 30 31 private final String mManufacturer; 32 private final String mModel; 33 private final String mDescription; 34 private final String mVersion; 35 private final String mUri; 36 private final String mSerial; 37 38 /** 39 * UsbAccessory should only be instantiated by UsbService implementation 40 * @hide 41 */ 42 public UsbAccessory(String manufacturer, String model, String description, 43 String version, String uri, String serial) { 44 mManufacturer = manufacturer; 45 mModel = model; 46 mDescription = description; 47 mVersion = version; 48 mUri = uri; 49 mSerial = serial; 50 } 51 52 /** 53 * UsbAccessory should only be instantiated by UsbService implementation 54 * @hide 55 */ 56 public UsbAccessory(String[] strings) { 57 mManufacturer = strings[0]; 58 mModel = strings[1]; 59 mDescription = strings[2]; 60 mVersion = strings[3]; 61 mUri = strings[4]; 62 mSerial = strings[5]; 63 } 64 65 /** 66 * Returns the manufacturer of the accessory. 67 * 68 * @return the accessory manufacturer 69 */ 70 public String getManufacturer() { 71 return mManufacturer; 72 } 73 74 /** 75 * Returns the model name of the accessory. 76 * 77 * @return the accessory model 78 */ 79 public String getModel() { 80 return mModel; 81 } 82 83 /** 84 * Returns a user visible description of the accessory. 85 * 86 * @return the accessory description 87 */ 88 public String getDescription() { 89 return mDescription; 90 } 91 92 /** 93 * Returns the version of the accessory. 94 * 95 * @return the accessory version 96 */ 97 public String getVersion() { 98 return mVersion; 99 } 100 101 /** 102 * Returns the URI for the accessory. 103 * This is an optional URI that might show information about the accessory 104 * or provide the option to download an application for the accessory 105 * 106 * @return the accessory URI 107 */ 108 public String getUri() { 109 return mUri; 110 } 111 112 /** 113 * Returns the unique serial number for the accessory. 114 * This is an optional serial number that can be used to differentiate 115 * between individual accessories of the same model and manufacturer 116 * 117 * @return the unique serial number 118 */ 119 public String getSerial() { 120 return mSerial; 121 } 122 123 private static boolean compare(String s1, String s2) { 124 if (s1 == null) return (s2 == null); 125 return s1.equals(s2); 126 } 127 128 @Override 129 public boolean equals(Object obj) { 130 if (obj instanceof UsbAccessory) { 131 UsbAccessory accessory = (UsbAccessory)obj; 132 return (compare(mManufacturer, accessory.getManufacturer()) && 133 compare(mModel, accessory.getModel()) && 134 compare(mDescription, accessory.getDescription()) && 135 compare(mVersion, accessory.getVersion()) && 136 compare(mUri, accessory.getUri()) && 137 compare(mSerial, accessory.getSerial())); 138 } 139 return false; 140 } 141 142 @Override 143 public int hashCode() { 144 return ((mManufacturer == null ? 0 : mManufacturer.hashCode()) ^ 145 (mModel == null ? 0 : mModel.hashCode()) ^ 146 (mDescription == null ? 0 : mDescription.hashCode()) ^ 147 (mVersion == null ? 0 : mVersion.hashCode()) ^ 148 (mUri == null ? 0 : mUri.hashCode()) ^ 149 (mSerial == null ? 0 : mSerial.hashCode())); 150 } 151 152 @Override 153 public String toString() { 154 return "UsbAccessory[mManufacturer=" + mManufacturer + 155 ", mModel=" + mModel + 156 ", mDescription=" + mDescription + 157 ", mVersion=" + mVersion + 158 ", mUri=" + mUri + 159 ", mSerial=" + mSerial + "]"; 160 } 161 162 public static final Parcelable.Creator<UsbAccessory> CREATOR = 163 new Parcelable.Creator<UsbAccessory>() { 164 public UsbAccessory createFromParcel(Parcel in) { 165 String manufacturer = in.readString(); 166 String model = in.readString(); 167 String description = in.readString(); 168 String version = in.readString(); 169 String uri = in.readString(); 170 String serial = in.readString(); 171 return new UsbAccessory(manufacturer, model, description, version, uri, serial); 172 } 173 174 public UsbAccessory[] newArray(int size) { 175 return new UsbAccessory[size]; 176 } 177 }; 178 179 public int describeContents() { 180 return 0; 181 } 182 183 public void writeToParcel(Parcel parcel, int flags) { 184 parcel.writeString(mManufacturer); 185 parcel.writeString(mModel); 186 parcel.writeString(mDescription); 187 parcel.writeString(mVersion); 188 parcel.writeString(mUri); 189 parcel.writeString(mSerial); 190 } 191} 192