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. |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 40 | */ |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 41 | public class UsbAccessory implements Parcelable { |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 42 | |
| 43 | private static final String TAG = "UsbAccessory"; |
| 44 | |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 45 | private final String mManufacturer; |
| 46 | private final String mModel; |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 47 | private final String mDescription; |
Mike Lockwood | c4308f0 | 2011-03-01 08:04:54 -0800 | [diff] [blame] | 48 | private final String mVersion; |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 49 | private final String mUri; |
Mike Lockwood | 015b1ec | 2011-03-14 18:24:35 -0400 | [diff] [blame] | 50 | private final String mSerial; |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 51 | |
| 52 | /** |
| 53 | * UsbAccessory should only be instantiated by UsbService implementation |
| 54 | * @hide |
| 55 | */ |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 56 | public UsbAccessory(String manufacturer, String model, String description, |
Mike Lockwood | 015b1ec | 2011-03-14 18:24:35 -0400 | [diff] [blame] | 57 | String version, String uri, String serial) { |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 58 | mManufacturer = manufacturer; |
| 59 | mModel = model; |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 60 | mDescription = description; |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 61 | mVersion = version; |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 62 | mUri = uri; |
Mike Lockwood | 015b1ec | 2011-03-14 18:24:35 -0400 | [diff] [blame] | 63 | mSerial = serial; |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 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]; |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 73 | mDescription = strings[2]; |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 74 | mVersion = strings[3]; |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 75 | mUri = strings[4]; |
Mike Lockwood | 015b1ec | 2011-03-14 18:24:35 -0400 | [diff] [blame] | 76 | mSerial = strings[5]; |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
Mike Lockwood | 11dd5ae | 2011-04-01 14:00:08 -0400 | [diff] [blame] | 80 | * Returns the manufacturer name of the accessory. |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 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 | /** |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 98 | * Returns a user visible description of the accessory. |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 99 | * |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 100 | * @return the accessory description |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 101 | */ |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 102 | public String getDescription() { |
| 103 | return mDescription; |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 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 | |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 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 | |
Mike Lockwood | 015b1ec | 2011-03-14 18:24:35 -0400 | [diff] [blame] | 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 | |
Mike Lockwood | 02eb874 | 2011-02-27 09:10:37 -0800 | [diff] [blame] | 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()) && |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 148 | compare(mDescription, accessory.getDescription()) && |
| 149 | compare(mVersion, accessory.getVersion()) && |
Mike Lockwood | 015b1ec | 2011-03-14 18:24:35 -0400 | [diff] [blame] | 150 | compare(mUri, accessory.getUri()) && |
| 151 | compare(mSerial, accessory.getSerial())); |
Mike Lockwood | 02eb874 | 2011-02-27 09:10:37 -0800 | [diff] [blame] | 152 | } |
| 153 | return false; |
| 154 | } |
| 155 | |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 156 | @Override |
Mike Lockwood | c6f23e8 | 2011-03-09 12:05:20 -0500 | [diff] [blame] | 157 | public int hashCode() { |
| 158 | return ((mManufacturer == null ? 0 : mManufacturer.hashCode()) ^ |
| 159 | (mModel == null ? 0 : mModel.hashCode()) ^ |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 160 | (mDescription == null ? 0 : mDescription.hashCode()) ^ |
| 161 | (mVersion == null ? 0 : mVersion.hashCode()) ^ |
Mike Lockwood | 015b1ec | 2011-03-14 18:24:35 -0400 | [diff] [blame] | 162 | (mUri == null ? 0 : mUri.hashCode()) ^ |
| 163 | (mSerial == null ? 0 : mSerial.hashCode())); |
Mike Lockwood | c6f23e8 | 2011-03-09 12:05:20 -0500 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | @Override |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 167 | public String toString() { |
| 168 | return "UsbAccessory[mManufacturer=" + mManufacturer + |
| 169 | ", mModel=" + mModel + |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 170 | ", mDescription=" + mDescription + |
| 171 | ", mVersion=" + mVersion + |
Mike Lockwood | 015b1ec | 2011-03-14 18:24:35 -0400 | [diff] [blame] | 172 | ", mUri=" + mUri + |
| 173 | ", mSerial=" + mSerial + "]"; |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 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(); |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 181 | String description = in.readString(); |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 182 | String version = in.readString(); |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 183 | String uri = in.readString(); |
Mike Lockwood | 015b1ec | 2011-03-14 18:24:35 -0400 | [diff] [blame] | 184 | String serial = in.readString(); |
| 185 | return new UsbAccessory(manufacturer, model, description, version, uri, serial); |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 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); |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 200 | parcel.writeString(mDescription); |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 201 | parcel.writeString(mVersion); |
Mike Lockwood | ac36d7c | 2011-03-09 22:03:57 -0500 | [diff] [blame] | 202 | parcel.writeString(mUri); |
Mike Lockwood | 015b1ec | 2011-03-14 18:24:35 -0400 | [diff] [blame] | 203 | parcel.writeString(mSerial); |
Mike Lockwood | 9182d3c | 2011-02-15 09:50:22 -0500 | [diff] [blame] | 204 | } |
| 205 | } |