Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 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 | |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 17 | package android.hardware.usb; |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 18 | |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 19 | import android.os.Parcel; |
| 20 | import android.os.Parcelable; |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 21 | |
| 22 | /** |
Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 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. |
Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 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> |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 44 | */ |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 45 | public class UsbAccessory implements Parcelable { |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 46 | |
| 47 | private static final String TAG = "UsbAccessory"; |
| 48 | |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 49 | private final String mManufacturer; |
| 50 | private final String mModel; |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 51 | private final String mDescription; |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 52 | private final String mVersion; |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 53 | private final String mUri; |
Mike Lockwood | 015b1ec | 2011-03-14 18:24:35 -0400 | [diff] [blame] | 54 | private final String mSerial; |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 55 | |
Mike Lockwood | 166b05e | 2012-04-24 10:45:18 -0700 | [diff] [blame] | 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 | |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 69 | /** |
| 70 | * UsbAccessory should only be instantiated by UsbService implementation |
| 71 | * @hide |
| 72 | */ |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 73 | public UsbAccessory(String manufacturer, String model, String description, |
Mike Lockwood | 015b1ec | 2011-03-14 18:24:35 -0400 | [diff] [blame] | 74 | String version, String uri, String serial) { |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 75 | mManufacturer = manufacturer; |
| 76 | mModel = model; |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 77 | mDescription = description; |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 78 | mVersion = version; |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 79 | mUri = uri; |
Mike Lockwood | 015b1ec | 2011-03-14 18:24:35 -0400 | [diff] [blame] | 80 | mSerial = serial; |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** |
| 84 | * UsbAccessory should only be instantiated by UsbService implementation |
| 85 | * @hide |
| 86 | */ |
| 87 | public UsbAccessory(String[] strings) { |
Mike Lockwood | 166b05e | 2012-04-24 10:45:18 -0700 | [diff] [blame] | 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]; |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | /** |
Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 97 | * Returns the manufacturer name of the accessory. |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 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 | /** |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 115 | * Returns a user visible description of the accessory. |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 116 | * |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 117 | * @return the accessory description |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 118 | */ |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 119 | public String getDescription() { |
| 120 | return mDescription; |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 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 | |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 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 | |
Mike Lockwood | 015b1ec | 2011-03-14 18:24:35 -0400 | [diff] [blame] | 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 | |
Mike Lockwood | 02eb874 | 2011-02-27 09:10:37 -0800 | [diff] [blame] | 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()) && |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 165 | compare(mDescription, accessory.getDescription()) && |
| 166 | compare(mVersion, accessory.getVersion()) && |
Mike Lockwood | 015b1ec | 2011-03-14 18:24:35 -0400 | [diff] [blame] | 167 | compare(mUri, accessory.getUri()) && |
| 168 | compare(mSerial, accessory.getSerial())); |
Mike Lockwood | 02eb874 | 2011-02-27 09:10:37 -0800 | [diff] [blame] | 169 | } |
| 170 | return false; |
| 171 | } |
| 172 | |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 173 | @Override |
Mike Lockwood | c6f23e8 | 2011-03-09 12:05:20 -0500 | [diff] [blame] | 174 | public int hashCode() { |
| 175 | return ((mManufacturer == null ? 0 : mManufacturer.hashCode()) ^ |
| 176 | (mModel == null ? 0 : mModel.hashCode()) ^ |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 177 | (mDescription == null ? 0 : mDescription.hashCode()) ^ |
| 178 | (mVersion == null ? 0 : mVersion.hashCode()) ^ |
Mike Lockwood | 015b1ec | 2011-03-14 18:24:35 -0400 | [diff] [blame] | 179 | (mUri == null ? 0 : mUri.hashCode()) ^ |
| 180 | (mSerial == null ? 0 : mSerial.hashCode())); |
Mike Lockwood | c6f23e8 | 2011-03-09 12:05:20 -0500 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | @Override |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 184 | public String toString() { |
| 185 | return "UsbAccessory[mManufacturer=" + mManufacturer + |
| 186 | ", mModel=" + mModel + |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 187 | ", mDescription=" + mDescription + |
| 188 | ", mVersion=" + mVersion + |
Mike Lockwood | 015b1ec | 2011-03-14 18:24:35 -0400 | [diff] [blame] | 189 | ", mUri=" + mUri + |
| 190 | ", mSerial=" + mSerial + "]"; |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 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(); |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 198 | String description = in.readString(); |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 199 | String version = in.readString(); |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 200 | String uri = in.readString(); |
Mike Lockwood | 015b1ec | 2011-03-14 18:24:35 -0400 | [diff] [blame] | 201 | String serial = in.readString(); |
| 202 | return new UsbAccessory(manufacturer, model, description, version, uri, serial); |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 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); |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 217 | parcel.writeString(mDescription); |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 218 | parcel.writeString(mVersion); |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 219 | parcel.writeString(mUri); |
Mike Lockwood | 015b1ec | 2011-03-14 18:24:35 -0400 | [diff] [blame] | 220 | parcel.writeString(mSerial); |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 221 | } |
| 222 | } |