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 17 package android.hardware.usb; 18 19 import android.os.Parcel; 20 import android.os.Parcelable; 21 22 /** 23 * A class representing a USB accessory, which is an external hardware component 24 * that communicates with an android application over USB. 25 * The accessory is the USB host and android the device side of the USB connection. 26 * 27 * <p>When the accessory connects, it reports its manufacturer and model names, 28 * the version of the accessory, and a user visible description of the accessory to the device. 29 * The manufacturer, model and version strings are used by the USB Manager to choose 30 * an appropriate application for the accessory. 31 * The accessory may optionally provide a unique serial number 32 * and a URL to for the accessory's website to the device as well. 33 * 34 * <p>An instance of this class is sent to the application via the 35 * {@link UsbManager#ACTION_USB_ACCESSORY_ATTACHED} Intent. 36 * The application can then call {@link UsbManager#openAccessory} to open a file descriptor 37 * for reading and writing data to and from the accessory. 38 * 39 * <div class="special reference"> 40 * <h3>Developer Guides</h3> 41 * <p>For more information about communicating with USB hardware, read the 42 * <a href="{@docRoot}guide/topics/usb/index.html">USB</a> developer guide.</p> 43 * </div> 44 */ 45 public class UsbAccessory implements Parcelable { 46 47 private static final String TAG = "UsbAccessory"; 48 49 private final String mManufacturer; 50 private final String mModel; 51 private final String mDescription; 52 private final String mVersion; 53 private final String mUri; 54 private final String mSerial; 55 56 /** @hide */ 57 public static final int MANUFACTURER_STRING = 0; 58 /** @hide */ 59 public static final int MODEL_STRING = 1; 60 /** @hide */ 61 public static final int DESCRIPTION_STRING = 2; 62 /** @hide */ 63 public static final int VERSION_STRING = 3; 64 /** @hide */ 65 public static final int URI_STRING = 4; 66 /** @hide */ 67 public static final int SERIAL_STRING = 5; 68 69 /** 70 * UsbAccessory should only be instantiated by UsbService implementation 71 * @hide 72 */ 73 public UsbAccessory(String manufacturer, String model, String description, 74 String version, String uri, String serial) { 75 mManufacturer = manufacturer; 76 mModel = model; 77 mDescription = description; 78 mVersion = version; 79 mUri = uri; 80 mSerial = serial; 81 } 82 83 /** 84 * UsbAccessory should only be instantiated by UsbService implementation 85 * @hide 86 */ 87 public UsbAccessory(String[] strings) { 88 mManufacturer = strings[MANUFACTURER_STRING]; 89 mModel = strings[MODEL_STRING]; 90 mDescription = strings[DESCRIPTION_STRING]; 91 mVersion = strings[VERSION_STRING]; 92 mUri = strings[URI_STRING]; 93 mSerial = strings[SERIAL_STRING]; 94 } 95 96 /** 97 * Returns the manufacturer name of the accessory. 98 * 99 * @return the accessory manufacturer 100 */ 101 public String getManufacturer() { 102 return mManufacturer; 103 } 104 105 /** 106 * Returns the model name of the accessory. 107 * 108 * @return the accessory model 109 */ 110 public String getModel() { 111 return mModel; 112 } 113 114 /** 115 * Returns a user visible description of the accessory. 116 * 117 * @return the accessory description 118 */ 119 public String getDescription() { 120 return mDescription; 121 } 122 123 /** 124 * Returns the version of the accessory. 125 * 126 * @return the accessory version 127 */ 128 public String getVersion() { 129 return mVersion; 130 } 131 132 /** 133 * Returns the URI for the accessory. 134 * This is an optional URI that might show information about the accessory 135 * or provide the option to download an application for the accessory 136 * 137 * @return the accessory URI 138 */ 139 public String getUri() { 140 return mUri; 141 } 142 143 /** 144 * Returns the unique serial number for the accessory. 145 * This is an optional serial number that can be used to differentiate 146 * between individual accessories of the same model and manufacturer 147 * 148 * @return the unique serial number 149 */ 150 public String getSerial() { 151 return mSerial; 152 } 153 154 private static boolean compare(String s1, String s2) { 155 if (s1 == null) return (s2 == null); 156 return s1.equals(s2); 157 } 158 159 @Override 160 public boolean equals(Object obj) { 161 if (obj instanceof UsbAccessory) { 162 UsbAccessory accessory = (UsbAccessory)obj; 163 return (compare(mManufacturer, accessory.getManufacturer()) && 164 compare(mModel, accessory.getModel()) && 165 compare(mDescription, accessory.getDescription()) && 166 compare(mVersion, accessory.getVersion()) && 167 compare(mUri, accessory.getUri()) && 168 compare(mSerial, accessory.getSerial())); 169 } 170 return false; 171 } 172 173 @Override 174 public int hashCode() { 175 return ((mManufacturer == null ? 0 : mManufacturer.hashCode()) ^ 176 (mModel == null ? 0 : mModel.hashCode()) ^ 177 (mDescription == null ? 0 : mDescription.hashCode()) ^ 178 (mVersion == null ? 0 : mVersion.hashCode()) ^ 179 (mUri == null ? 0 : mUri.hashCode()) ^ 180 (mSerial == null ? 0 : mSerial.hashCode())); 181 } 182 183 @Override 184 public String toString() { 185 return "UsbAccessory[mManufacturer=" + mManufacturer + 186 ", mModel=" + mModel + 187 ", mDescription=" + mDescription + 188 ", mVersion=" + mVersion + 189 ", mUri=" + mUri + 190 ", mSerial=" + mSerial + "]"; 191 } 192 193 public static final Parcelable.Creator<UsbAccessory> CREATOR = 194 new Parcelable.Creator<UsbAccessory>() { 195 public UsbAccessory createFromParcel(Parcel in) { 196 String manufacturer = in.readString(); 197 String model = in.readString(); 198 String description = in.readString(); 199 String version = in.readString(); 200 String uri = in.readString(); 201 String serial = in.readString(); 202 return new UsbAccessory(manufacturer, model, description, version, uri, serial); 203 } 204 205 public UsbAccessory[] newArray(int size) { 206 return new UsbAccessory[size]; 207 } 208 }; 209 210 public int describeContents() { 211 return 0; 212 } 213 214 public void writeToParcel(Parcel parcel, int flags) { 215 parcel.writeString(mManufacturer); 216 parcel.writeString(mModel); 217 parcel.writeString(mDescription); 218 parcel.writeString(mVersion); 219 parcel.writeString(mUri); 220 parcel.writeString(mSerial); 221 } 222 } 223