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 | |
| 19 | import android.os.Bundle; |
| 20 | import android.os.Parcel; |
| 21 | import android.os.Parcelable; |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 22 | import android.util.Log; |
| 23 | |
| 24 | /** |
Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 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. |
Joe Fernandez | 3aef8e1d | 2011-12-20 10:38:34 -0800 | [diff] [blame] | 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> |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 46 | */ |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 47 | public class UsbAccessory implements Parcelable { |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 48 | |
| 49 | private static final String TAG = "UsbAccessory"; |
| 50 | |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 51 | private final String mManufacturer; |
| 52 | private final String mModel; |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 53 | private final String mDescription; |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 54 | private final String mVersion; |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 55 | private final String mUri; |
Mike Lockwood | 015b1ec | 2011-03-14 18:24:35 -0400 | [diff] [blame] | 56 | private final String mSerial; |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * UsbAccessory should only be instantiated by UsbService implementation |
| 60 | * @hide |
| 61 | */ |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 62 | public UsbAccessory(String manufacturer, String model, String description, |
Mike Lockwood | 015b1ec | 2011-03-14 18:24:35 -0400 | [diff] [blame] | 63 | String version, String uri, String serial) { |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 64 | mManufacturer = manufacturer; |
| 65 | mModel = model; |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 66 | mDescription = description; |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 67 | mVersion = version; |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 68 | mUri = uri; |
Mike Lockwood | 015b1ec | 2011-03-14 18:24:35 -0400 | [diff] [blame] | 69 | mSerial = serial; |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 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]; |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 79 | mDescription = strings[2]; |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 80 | mVersion = strings[3]; |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 81 | mUri = strings[4]; |
Mike Lockwood | 015b1ec | 2011-03-14 18:24:35 -0400 | [diff] [blame] | 82 | mSerial = strings[5]; |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | /** |
Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 86 | * Returns the manufacturer name of the accessory. |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 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 | /** |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 104 | * Returns a user visible description of the accessory. |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 105 | * |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 106 | * @return the accessory description |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 107 | */ |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 108 | public String getDescription() { |
| 109 | return mDescription; |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 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 | |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 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 | |
Mike Lockwood | 015b1ec | 2011-03-14 18:24:35 -0400 | [diff] [blame] | 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 | |
Mike Lockwood | 02eb874 | 2011-02-27 09:10:37 -0800 | [diff] [blame] | 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()) && |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 154 | compare(mDescription, accessory.getDescription()) && |
| 155 | compare(mVersion, accessory.getVersion()) && |
Mike Lockwood | 015b1ec | 2011-03-14 18:24:35 -0400 | [diff] [blame] | 156 | compare(mUri, accessory.getUri()) && |
| 157 | compare(mSerial, accessory.getSerial())); |
Mike Lockwood | 02eb874 | 2011-02-27 09:10:37 -0800 | [diff] [blame] | 158 | } |
| 159 | return false; |
| 160 | } |
| 161 | |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 162 | @Override |
Mike Lockwood | c6f23e8 | 2011-03-09 12:05:20 -0500 | [diff] [blame] | 163 | public int hashCode() { |
| 164 | return ((mManufacturer == null ? 0 : mManufacturer.hashCode()) ^ |
| 165 | (mModel == null ? 0 : mModel.hashCode()) ^ |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 166 | (mDescription == null ? 0 : mDescription.hashCode()) ^ |
| 167 | (mVersion == null ? 0 : mVersion.hashCode()) ^ |
Mike Lockwood | 015b1ec | 2011-03-14 18:24:35 -0400 | [diff] [blame] | 168 | (mUri == null ? 0 : mUri.hashCode()) ^ |
| 169 | (mSerial == null ? 0 : mSerial.hashCode())); |
Mike Lockwood | c6f23e8 | 2011-03-09 12:05:20 -0500 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | @Override |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 173 | public String toString() { |
| 174 | return "UsbAccessory[mManufacturer=" + mManufacturer + |
| 175 | ", mModel=" + mModel + |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 176 | ", mDescription=" + mDescription + |
| 177 | ", mVersion=" + mVersion + |
Mike Lockwood | 015b1ec | 2011-03-14 18:24:35 -0400 | [diff] [blame] | 178 | ", mUri=" + mUri + |
| 179 | ", mSerial=" + mSerial + "]"; |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 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(); |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 187 | String description = in.readString(); |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 188 | String version = in.readString(); |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 189 | String uri = in.readString(); |
Mike Lockwood | 015b1ec | 2011-03-14 18:24:35 -0400 | [diff] [blame] | 190 | String serial = in.readString(); |
| 191 | return new UsbAccessory(manufacturer, model, description, version, uri, serial); |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 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); |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 206 | parcel.writeString(mDescription); |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 207 | parcel.writeString(mVersion); |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 208 | parcel.writeString(mUri); |
Mike Lockwood | 015b1ec | 2011-03-14 18:24:35 -0400 | [diff] [blame] | 209 | parcel.writeString(mSerial); |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 210 | } |
| 211 | } |