blob: 2f9178c0f62c5b296a741c637b7637dc4ccc6f2a [file] [log] [blame]
Mike Lockwood9182d3c2011-02-15 09:50:22 -05001/*
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 Lockwoodc4308f02011-03-01 08:04:54 -080017package android.hardware.usb;
Mike Lockwood9182d3c2011-02-15 09:50:22 -050018
Mike Lockwood9182d3c2011-02-15 09:50:22 -050019import android.os.Parcel;
20import android.os.Parcelable;
Mike Lockwood9182d3c2011-02-15 09:50:22 -050021
22/**
Mike Lockwood11dd5ae2011-04-01 14:00:08 -040023 * 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 Fernandez3aef8e1d2011-12-20 10:38:34 -080038 *
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 Lockwood9182d3c2011-02-15 09:50:22 -050044 */
Mike Lockwoodc4308f02011-03-01 08:04:54 -080045public class UsbAccessory implements Parcelable {
Mike Lockwood9182d3c2011-02-15 09:50:22 -050046
47 private static final String TAG = "UsbAccessory";
48
Mike Lockwoodc4308f02011-03-01 08:04:54 -080049 private final String mManufacturer;
50 private final String mModel;
Mike Lockwoodac36d7c2011-03-09 22:03:57 -050051 private final String mDescription;
Mike Lockwoodc4308f02011-03-01 08:04:54 -080052 private final String mVersion;
Mike Lockwoodac36d7c2011-03-09 22:03:57 -050053 private final String mUri;
Mike Lockwood015b1ec2011-03-14 18:24:35 -040054 private final String mSerial;
Mike Lockwood9182d3c2011-02-15 09:50:22 -050055
Mike Lockwood166b05e2012-04-24 10:45:18 -070056 /** @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 Lockwood9182d3c2011-02-15 09:50:22 -050069 /**
70 * UsbAccessory should only be instantiated by UsbService implementation
71 * @hide
72 */
Mike Lockwoodac36d7c2011-03-09 22:03:57 -050073 public UsbAccessory(String manufacturer, String model, String description,
Mike Lockwood015b1ec2011-03-14 18:24:35 -040074 String version, String uri, String serial) {
Mike Lockwood9182d3c2011-02-15 09:50:22 -050075 mManufacturer = manufacturer;
76 mModel = model;
Mike Lockwoodac36d7c2011-03-09 22:03:57 -050077 mDescription = description;
Mike Lockwood9182d3c2011-02-15 09:50:22 -050078 mVersion = version;
Mike Lockwoodac36d7c2011-03-09 22:03:57 -050079 mUri = uri;
Mike Lockwood015b1ec2011-03-14 18:24:35 -040080 mSerial = serial;
Mike Lockwood9182d3c2011-02-15 09:50:22 -050081 }
82
83 /**
84 * UsbAccessory should only be instantiated by UsbService implementation
85 * @hide
86 */
87 public UsbAccessory(String[] strings) {
Mike Lockwood166b05e2012-04-24 10:45:18 -070088 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 Lockwood9182d3c2011-02-15 09:50:22 -050094 }
95
96 /**
Mike Lockwood11dd5ae2011-04-01 14:00:08 -040097 * Returns the manufacturer name of the accessory.
Mike Lockwood9182d3c2011-02-15 09:50:22 -050098 *
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 Lockwoodac36d7c2011-03-09 22:03:57 -0500115 * Returns a user visible description of the accessory.
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500116 *
Mike Lockwoodac36d7c2011-03-09 22:03:57 -0500117 * @return the accessory description
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500118 */
Mike Lockwoodac36d7c2011-03-09 22:03:57 -0500119 public String getDescription() {
120 return mDescription;
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500121 }
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 Lockwoodac36d7c2011-03-09 22:03:57 -0500132 /**
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 Lockwood015b1ec2011-03-14 18:24:35 -0400143 /**
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 Lockwood02eb8742011-02-27 09:10:37 -0800154 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 Lockwoodac36d7c2011-03-09 22:03:57 -0500165 compare(mDescription, accessory.getDescription()) &&
166 compare(mVersion, accessory.getVersion()) &&
Mike Lockwood015b1ec2011-03-14 18:24:35 -0400167 compare(mUri, accessory.getUri()) &&
168 compare(mSerial, accessory.getSerial()));
Mike Lockwood02eb8742011-02-27 09:10:37 -0800169 }
170 return false;
171 }
172
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500173 @Override
Mike Lockwoodc6f23e82011-03-09 12:05:20 -0500174 public int hashCode() {
175 return ((mManufacturer == null ? 0 : mManufacturer.hashCode()) ^
176 (mModel == null ? 0 : mModel.hashCode()) ^
Mike Lockwoodac36d7c2011-03-09 22:03:57 -0500177 (mDescription == null ? 0 : mDescription.hashCode()) ^
178 (mVersion == null ? 0 : mVersion.hashCode()) ^
Mike Lockwood015b1ec2011-03-14 18:24:35 -0400179 (mUri == null ? 0 : mUri.hashCode()) ^
180 (mSerial == null ? 0 : mSerial.hashCode()));
Mike Lockwoodc6f23e82011-03-09 12:05:20 -0500181 }
182
183 @Override
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500184 public String toString() {
185 return "UsbAccessory[mManufacturer=" + mManufacturer +
186 ", mModel=" + mModel +
Mike Lockwoodac36d7c2011-03-09 22:03:57 -0500187 ", mDescription=" + mDescription +
188 ", mVersion=" + mVersion +
Mike Lockwood015b1ec2011-03-14 18:24:35 -0400189 ", mUri=" + mUri +
190 ", mSerial=" + mSerial + "]";
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500191 }
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 Lockwoodac36d7c2011-03-09 22:03:57 -0500198 String description = in.readString();
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500199 String version = in.readString();
Mike Lockwoodac36d7c2011-03-09 22:03:57 -0500200 String uri = in.readString();
Mike Lockwood015b1ec2011-03-14 18:24:35 -0400201 String serial = in.readString();
202 return new UsbAccessory(manufacturer, model, description, version, uri, serial);
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500203 }
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 Lockwoodac36d7c2011-03-09 22:03:57 -0500217 parcel.writeString(mDescription);
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500218 parcel.writeString(mVersion);
Mike Lockwoodac36d7c2011-03-09 22:03:57 -0500219 parcel.writeString(mUri);
Mike Lockwood015b1ec2011-03-14 18:24:35 -0400220 parcel.writeString(mSerial);
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500221 }
222}