blob: c8ea825d72fa5328dae3b5a962b32d59b9f988ae [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.
Mike Lockwood9182d3c2011-02-15 09:50:22 -050040 */
Mike Lockwoodc4308f02011-03-01 08:04:54 -080041public class UsbAccessory implements Parcelable {
Mike Lockwood9182d3c2011-02-15 09:50:22 -050042
43 private static final String TAG = "UsbAccessory";
44
Mike Lockwoodc4308f02011-03-01 08:04:54 -080045 private final String mManufacturer;
46 private final String mModel;
Mike Lockwoodac36d7c2011-03-09 22:03:57 -050047 private final String mDescription;
Mike Lockwoodc4308f02011-03-01 08:04:54 -080048 private final String mVersion;
Mike Lockwoodac36d7c2011-03-09 22:03:57 -050049 private final String mUri;
Mike Lockwood015b1ec2011-03-14 18:24:35 -040050 private final String mSerial;
Mike Lockwood9182d3c2011-02-15 09:50:22 -050051
52 /**
53 * UsbAccessory should only be instantiated by UsbService implementation
54 * @hide
55 */
Mike Lockwoodac36d7c2011-03-09 22:03:57 -050056 public UsbAccessory(String manufacturer, String model, String description,
Mike Lockwood015b1ec2011-03-14 18:24:35 -040057 String version, String uri, String serial) {
Mike Lockwood9182d3c2011-02-15 09:50:22 -050058 mManufacturer = manufacturer;
59 mModel = model;
Mike Lockwoodac36d7c2011-03-09 22:03:57 -050060 mDescription = description;
Mike Lockwood9182d3c2011-02-15 09:50:22 -050061 mVersion = version;
Mike Lockwoodac36d7c2011-03-09 22:03:57 -050062 mUri = uri;
Mike Lockwood015b1ec2011-03-14 18:24:35 -040063 mSerial = serial;
Mike Lockwood9182d3c2011-02-15 09:50:22 -050064 }
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 Lockwoodac36d7c2011-03-09 22:03:57 -050073 mDescription = strings[2];
Mike Lockwood9182d3c2011-02-15 09:50:22 -050074 mVersion = strings[3];
Mike Lockwoodac36d7c2011-03-09 22:03:57 -050075 mUri = strings[4];
Mike Lockwood015b1ec2011-03-14 18:24:35 -040076 mSerial = strings[5];
Mike Lockwood9182d3c2011-02-15 09:50:22 -050077 }
78
79 /**
Mike Lockwood11dd5ae2011-04-01 14:00:08 -040080 * Returns the manufacturer name of the accessory.
Mike Lockwood9182d3c2011-02-15 09:50:22 -050081 *
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 Lockwoodac36d7c2011-03-09 22:03:57 -050098 * Returns a user visible description of the accessory.
Mike Lockwood9182d3c2011-02-15 09:50:22 -050099 *
Mike Lockwoodac36d7c2011-03-09 22:03:57 -0500100 * @return the accessory description
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500101 */
Mike Lockwoodac36d7c2011-03-09 22:03:57 -0500102 public String getDescription() {
103 return mDescription;
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500104 }
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 Lockwoodac36d7c2011-03-09 22:03:57 -0500115 /**
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 Lockwood015b1ec2011-03-14 18:24:35 -0400126 /**
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 Lockwood02eb8742011-02-27 09:10:37 -0800137 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 Lockwoodac36d7c2011-03-09 22:03:57 -0500148 compare(mDescription, accessory.getDescription()) &&
149 compare(mVersion, accessory.getVersion()) &&
Mike Lockwood015b1ec2011-03-14 18:24:35 -0400150 compare(mUri, accessory.getUri()) &&
151 compare(mSerial, accessory.getSerial()));
Mike Lockwood02eb8742011-02-27 09:10:37 -0800152 }
153 return false;
154 }
155
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500156 @Override
Mike Lockwoodc6f23e82011-03-09 12:05:20 -0500157 public int hashCode() {
158 return ((mManufacturer == null ? 0 : mManufacturer.hashCode()) ^
159 (mModel == null ? 0 : mModel.hashCode()) ^
Mike Lockwoodac36d7c2011-03-09 22:03:57 -0500160 (mDescription == null ? 0 : mDescription.hashCode()) ^
161 (mVersion == null ? 0 : mVersion.hashCode()) ^
Mike Lockwood015b1ec2011-03-14 18:24:35 -0400162 (mUri == null ? 0 : mUri.hashCode()) ^
163 (mSerial == null ? 0 : mSerial.hashCode()));
Mike Lockwoodc6f23e82011-03-09 12:05:20 -0500164 }
165
166 @Override
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500167 public String toString() {
168 return "UsbAccessory[mManufacturer=" + mManufacturer +
169 ", mModel=" + mModel +
Mike Lockwoodac36d7c2011-03-09 22:03:57 -0500170 ", mDescription=" + mDescription +
171 ", mVersion=" + mVersion +
Mike Lockwood015b1ec2011-03-14 18:24:35 -0400172 ", mUri=" + mUri +
173 ", mSerial=" + mSerial + "]";
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500174 }
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 Lockwoodac36d7c2011-03-09 22:03:57 -0500181 String description = in.readString();
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500182 String version = in.readString();
Mike Lockwoodac36d7c2011-03-09 22:03:57 -0500183 String uri = in.readString();
Mike Lockwood015b1ec2011-03-14 18:24:35 -0400184 String serial = in.readString();
185 return new UsbAccessory(manufacturer, model, description, version, uri, serial);
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500186 }
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 Lockwoodac36d7c2011-03-09 22:03:57 -0500200 parcel.writeString(mDescription);
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500201 parcel.writeString(mVersion);
Mike Lockwoodac36d7c2011-03-09 22:03:57 -0500202 parcel.writeString(mUri);
Mike Lockwood015b1ec2011-03-14 18:24:35 -0400203 parcel.writeString(mSerial);
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500204 }
205}