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