blob: 770204466eafd31ec1d9afe334d3eedae14cc166 [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
19import android.os.Bundle;
20import android.os.Parcel;
21import android.os.Parcelable;
Mike Lockwood9182d3c2011-02-15 09:50:22 -050022import android.util.Log;
23
24/**
Mike Lockwood11dd5ae2011-04-01 14:00:08 -040025 * 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 Fernandez3aef8e1d2011-12-20 10:38:34 -080040 *
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 Lockwood9182d3c2011-02-15 09:50:22 -050046 */
Mike Lockwoodc4308f02011-03-01 08:04:54 -080047public class UsbAccessory implements Parcelable {
Mike Lockwood9182d3c2011-02-15 09:50:22 -050048
49 private static final String TAG = "UsbAccessory";
50
Mike Lockwoodc4308f02011-03-01 08:04:54 -080051 private final String mManufacturer;
52 private final String mModel;
Mike Lockwoodac36d7c2011-03-09 22:03:57 -050053 private final String mDescription;
Mike Lockwoodc4308f02011-03-01 08:04:54 -080054 private final String mVersion;
Mike Lockwoodac36d7c2011-03-09 22:03:57 -050055 private final String mUri;
Mike Lockwood015b1ec2011-03-14 18:24:35 -040056 private final String mSerial;
Mike Lockwood9182d3c2011-02-15 09:50:22 -050057
58 /**
59 * UsbAccessory should only be instantiated by UsbService implementation
60 * @hide
61 */
Mike Lockwoodac36d7c2011-03-09 22:03:57 -050062 public UsbAccessory(String manufacturer, String model, String description,
Mike Lockwood015b1ec2011-03-14 18:24:35 -040063 String version, String uri, String serial) {
Mike Lockwood9182d3c2011-02-15 09:50:22 -050064 mManufacturer = manufacturer;
65 mModel = model;
Mike Lockwoodac36d7c2011-03-09 22:03:57 -050066 mDescription = description;
Mike Lockwood9182d3c2011-02-15 09:50:22 -050067 mVersion = version;
Mike Lockwoodac36d7c2011-03-09 22:03:57 -050068 mUri = uri;
Mike Lockwood015b1ec2011-03-14 18:24:35 -040069 mSerial = serial;
Mike Lockwood9182d3c2011-02-15 09:50:22 -050070 }
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 Lockwoodac36d7c2011-03-09 22:03:57 -050079 mDescription = strings[2];
Mike Lockwood9182d3c2011-02-15 09:50:22 -050080 mVersion = strings[3];
Mike Lockwoodac36d7c2011-03-09 22:03:57 -050081 mUri = strings[4];
Mike Lockwood015b1ec2011-03-14 18:24:35 -040082 mSerial = strings[5];
Mike Lockwood9182d3c2011-02-15 09:50:22 -050083 }
84
85 /**
Mike Lockwood11dd5ae2011-04-01 14:00:08 -040086 * Returns the manufacturer name of the accessory.
Mike Lockwood9182d3c2011-02-15 09:50:22 -050087 *
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 Lockwoodac36d7c2011-03-09 22:03:57 -0500104 * Returns a user visible description of the accessory.
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500105 *
Mike Lockwoodac36d7c2011-03-09 22:03:57 -0500106 * @return the accessory description
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500107 */
Mike Lockwoodac36d7c2011-03-09 22:03:57 -0500108 public String getDescription() {
109 return mDescription;
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500110 }
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 Lockwoodac36d7c2011-03-09 22:03:57 -0500121 /**
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 Lockwood015b1ec2011-03-14 18:24:35 -0400132 /**
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 Lockwood02eb8742011-02-27 09:10:37 -0800143 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 Lockwoodac36d7c2011-03-09 22:03:57 -0500154 compare(mDescription, accessory.getDescription()) &&
155 compare(mVersion, accessory.getVersion()) &&
Mike Lockwood015b1ec2011-03-14 18:24:35 -0400156 compare(mUri, accessory.getUri()) &&
157 compare(mSerial, accessory.getSerial()));
Mike Lockwood02eb8742011-02-27 09:10:37 -0800158 }
159 return false;
160 }
161
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500162 @Override
Mike Lockwoodc6f23e82011-03-09 12:05:20 -0500163 public int hashCode() {
164 return ((mManufacturer == null ? 0 : mManufacturer.hashCode()) ^
165 (mModel == null ? 0 : mModel.hashCode()) ^
Mike Lockwoodac36d7c2011-03-09 22:03:57 -0500166 (mDescription == null ? 0 : mDescription.hashCode()) ^
167 (mVersion == null ? 0 : mVersion.hashCode()) ^
Mike Lockwood015b1ec2011-03-14 18:24:35 -0400168 (mUri == null ? 0 : mUri.hashCode()) ^
169 (mSerial == null ? 0 : mSerial.hashCode()));
Mike Lockwoodc6f23e82011-03-09 12:05:20 -0500170 }
171
172 @Override
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500173 public String toString() {
174 return "UsbAccessory[mManufacturer=" + mManufacturer +
175 ", mModel=" + mModel +
Mike Lockwoodac36d7c2011-03-09 22:03:57 -0500176 ", mDescription=" + mDescription +
177 ", mVersion=" + mVersion +
Mike Lockwood015b1ec2011-03-14 18:24:35 -0400178 ", mUri=" + mUri +
179 ", mSerial=" + mSerial + "]";
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500180 }
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 Lockwoodac36d7c2011-03-09 22:03:57 -0500187 String description = in.readString();
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500188 String version = in.readString();
Mike Lockwoodac36d7c2011-03-09 22:03:57 -0500189 String uri = in.readString();
Mike Lockwood015b1ec2011-03-14 18:24:35 -0400190 String serial = in.readString();
191 return new UsbAccessory(manufacturer, model, description, version, uri, serial);
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500192 }
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 Lockwoodac36d7c2011-03-09 22:03:57 -0500206 parcel.writeString(mDescription);
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500207 parcel.writeString(mVersion);
Mike Lockwoodac36d7c2011-03-09 22:03:57 -0500208 parcel.writeString(mUri);
Mike Lockwood015b1ec2011-03-14 18:24:35 -0400209 parcel.writeString(mSerial);
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500210 }
211}