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