blob: a76e4ad2416ed166b016219664685c25b7ba31d5 [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
Philip P. Moltmannf2d83ed2016-10-17 12:14:41 -070019import android.annotation.NonNull;
20import android.annotation.Nullable;
Philip P. Moltmanna2c6eea2018-08-24 09:50:40 -070021import android.app.ActivityThread;
Mike Lockwood9182d3c2011-02-15 09:50:22 -050022import android.os.Parcel;
23import android.os.Parcelable;
Philip P. Moltmanna2c6eea2018-08-24 09:50:40 -070024import android.os.RemoteException;
25
Philip P. Moltmannf2d83ed2016-10-17 12:14:41 -070026import com.android.internal.util.Preconditions;
Mike Lockwood9182d3c2011-02-15 09:50:22 -050027
28/**
Mike Lockwood11dd5ae2011-04-01 14:00:08 -040029 * A class representing a USB accessory, which is an external hardware component
30 * that communicates with an android application over USB.
31 * The accessory is the USB host and android the device side of the USB connection.
32 *
33 * <p>When the accessory connects, it reports its manufacturer and model names,
34 * the version of the accessory, and a user visible description of the accessory to the device.
35 * The manufacturer, model and version strings are used by the USB Manager to choose
36 * an appropriate application for the accessory.
37 * The accessory may optionally provide a unique serial number
38 * and a URL to for the accessory's website to the device as well.
39 *
40 * <p>An instance of this class is sent to the application via the
41 * {@link UsbManager#ACTION_USB_ACCESSORY_ATTACHED} Intent.
42 * The application can then call {@link UsbManager#openAccessory} to open a file descriptor
43 * for reading and writing data to and from the accessory.
Joe Fernandez3aef8e1d2011-12-20 10:38:34 -080044 *
45 * <div class="special reference">
46 * <h3>Developer Guides</h3>
47 * <p>For more information about communicating with USB hardware, read the
48 * <a href="{@docRoot}guide/topics/usb/index.html">USB</a> developer guide.</p>
49 * </div>
Mike Lockwood9182d3c2011-02-15 09:50:22 -050050 */
Mike Lockwoodc4308f02011-03-01 08:04:54 -080051public class UsbAccessory implements Parcelable {
Mike Lockwood9182d3c2011-02-15 09:50:22 -050052
53 private static final String TAG = "UsbAccessory";
54
Philip P. Moltmannf2d83ed2016-10-17 12:14:41 -070055 private final @NonNull String mManufacturer;
56 private final @NonNull String mModel;
57 private final @Nullable String mDescription;
58 private final @Nullable String mVersion;
59 private final @Nullable String mUri;
Philip P. Moltmanna2c6eea2018-08-24 09:50:40 -070060 private final @NonNull IUsbSerialReader mSerialNumberReader;
Mike Lockwood9182d3c2011-02-15 09:50:22 -050061
Mike Lockwood166b05e2012-04-24 10:45:18 -070062 /** @hide */
63 public static final int MANUFACTURER_STRING = 0;
64 /** @hide */
65 public static final int MODEL_STRING = 1;
66 /** @hide */
67 public static final int DESCRIPTION_STRING = 2;
68 /** @hide */
69 public static final int VERSION_STRING = 3;
70 /** @hide */
71 public static final int URI_STRING = 4;
72 /** @hide */
73 public static final int SERIAL_STRING = 5;
74
Mike Lockwood9182d3c2011-02-15 09:50:22 -050075 /**
76 * UsbAccessory should only be instantiated by UsbService implementation
77 * @hide
78 */
Philip P. Moltmannf2d83ed2016-10-17 12:14:41 -070079 public UsbAccessory(@NonNull String manufacturer, @NonNull String model,
80 @Nullable String description, @Nullable String version, @Nullable String uri,
Philip P. Moltmanna2c6eea2018-08-24 09:50:40 -070081 @NonNull IUsbSerialReader serialNumberReader) {
Philip P. Moltmannf2d83ed2016-10-17 12:14:41 -070082 mManufacturer = Preconditions.checkNotNull(manufacturer);
83 mModel = Preconditions.checkNotNull(model);
Mike Lockwoodac36d7c2011-03-09 22:03:57 -050084 mDescription = description;
Mike Lockwood9182d3c2011-02-15 09:50:22 -050085 mVersion = version;
Mike Lockwoodac36d7c2011-03-09 22:03:57 -050086 mUri = uri;
Philip P. Moltmanna2c6eea2018-08-24 09:50:40 -070087 mSerialNumberReader = serialNumberReader;
88
89 // Make sure the binder belongs to the system
90 if (ActivityThread.isSystem()) {
91 Preconditions.checkArgument(mSerialNumberReader instanceof IUsbSerialReader.Stub);
92 }
Mike Lockwood9182d3c2011-02-15 09:50:22 -050093 }
94
95 /**
Philip P. Moltmanna2c6eea2018-08-24 09:50:40 -070096 * DO NOT USE. Only for backwards compatibility with
97 * {@link com.android.future.usb.UsbAccessory}.
98 *
Mike Lockwood9182d3c2011-02-15 09:50:22 -050099 * @hide
Philip P. Moltmanna2c6eea2018-08-24 09:50:40 -0700100 * @deprecated use {@link UsbAccessory#UsbAccessory(String, String, String, String, String,
101 * IUsbSerialReader) instead}
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500102 */
Philip P. Moltmanna2c6eea2018-08-24 09:50:40 -0700103 @Deprecated
104 public UsbAccessory(@NonNull String manufacturer, @NonNull String model,
105 @Nullable String description, @Nullable String version, @Nullable String uri,
106 @Nullable String serialNumber) {
107 this(manufacturer, model, description, version, uri, new IUsbSerialReader.Stub() {
108 @Override
109 public String getSerial(String packageName) {
110 return serialNumber;
111 }
112 });
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500113 }
114
115 /**
Mike Lockwood11dd5ae2011-04-01 14:00:08 -0400116 * Returns the manufacturer name of the accessory.
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500117 *
118 * @return the accessory manufacturer
119 */
Philip P. Moltmannf2d83ed2016-10-17 12:14:41 -0700120 public @NonNull String getManufacturer() {
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500121 return mManufacturer;
122 }
123
124 /**
125 * Returns the model name of the accessory.
126 *
127 * @return the accessory model
128 */
Philip P. Moltmannf2d83ed2016-10-17 12:14:41 -0700129 public @NonNull String getModel() {
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500130 return mModel;
131 }
132
133 /**
Mike Lockwoodac36d7c2011-03-09 22:03:57 -0500134 * Returns a user visible description of the accessory.
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500135 *
Philip P. Moltmannf2d83ed2016-10-17 12:14:41 -0700136 * @return the accessory description, or {@code null} if not set
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500137 */
Philip P. Moltmannf2d83ed2016-10-17 12:14:41 -0700138 public @Nullable String getDescription() {
Mike Lockwoodac36d7c2011-03-09 22:03:57 -0500139 return mDescription;
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500140 }
141
142 /**
143 * Returns the version of the accessory.
144 *
Philip P. Moltmannf2d83ed2016-10-17 12:14:41 -0700145 * @return the accessory version, or {@code null} if not set
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500146 */
Philip P. Moltmannf2d83ed2016-10-17 12:14:41 -0700147 public @Nullable String getVersion() {
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500148 return mVersion;
149 }
150
Mike Lockwoodac36d7c2011-03-09 22:03:57 -0500151 /**
152 * Returns the URI for the accessory.
153 * This is an optional URI that might show information about the accessory
154 * or provide the option to download an application for the accessory
155 *
Philip P. Moltmannf2d83ed2016-10-17 12:14:41 -0700156 * @return the accessory URI, or {@code null} if not set
Mike Lockwoodac36d7c2011-03-09 22:03:57 -0500157 */
Philip P. Moltmannf2d83ed2016-10-17 12:14:41 -0700158 public @Nullable String getUri() {
Mike Lockwoodac36d7c2011-03-09 22:03:57 -0500159 return mUri;
160 }
161
Mike Lockwood015b1ec2011-03-14 18:24:35 -0400162 /**
163 * Returns the unique serial number for the accessory.
164 * This is an optional serial number that can be used to differentiate
165 * between individual accessories of the same model and manufacturer
166 *
Philip P. Moltmannf2d83ed2016-10-17 12:14:41 -0700167 * @return the unique serial number, or {@code null} if not set
Philip P. Moltmanna2c6eea2018-08-24 09:50:40 -0700168 *
169 * @throws SecurityException if the app targets SDK >= {@value android.os.Build.VERSION_CODES#Q}
170 * and the app does not have permission to read from the accessory.
Mike Lockwood015b1ec2011-03-14 18:24:35 -0400171 */
Philip P. Moltmannf2d83ed2016-10-17 12:14:41 -0700172 public @Nullable String getSerial() {
Philip P. Moltmanna2c6eea2018-08-24 09:50:40 -0700173 try {
174 return mSerialNumberReader.getSerial(ActivityThread.currentPackageName());
175 } catch (RemoteException e) {
176 e.rethrowFromSystemServer();
177 return null;
178 }
Mike Lockwood015b1ec2011-03-14 18:24:35 -0400179 }
180
Mike Lockwood02eb8742011-02-27 09:10:37 -0800181 private static boolean compare(String s1, String s2) {
182 if (s1 == null) return (s2 == null);
183 return s1.equals(s2);
184 }
185
186 @Override
187 public boolean equals(Object obj) {
188 if (obj instanceof UsbAccessory) {
189 UsbAccessory accessory = (UsbAccessory)obj;
190 return (compare(mManufacturer, accessory.getManufacturer()) &&
191 compare(mModel, accessory.getModel()) &&
Mike Lockwoodac36d7c2011-03-09 22:03:57 -0500192 compare(mDescription, accessory.getDescription()) &&
193 compare(mVersion, accessory.getVersion()) &&
Mike Lockwood015b1ec2011-03-14 18:24:35 -0400194 compare(mUri, accessory.getUri()) &&
Philip P. Moltmanna2c6eea2018-08-24 09:50:40 -0700195 compare(getSerial(), accessory.getSerial()));
Mike Lockwood02eb8742011-02-27 09:10:37 -0800196 }
197 return false;
198 }
199
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500200 @Override
Mike Lockwoodc6f23e82011-03-09 12:05:20 -0500201 public int hashCode() {
Philip P. Moltmannf2d83ed2016-10-17 12:14:41 -0700202 return mManufacturer.hashCode() ^ mModel.hashCode() ^
Mike Lockwoodac36d7c2011-03-09 22:03:57 -0500203 (mDescription == null ? 0 : mDescription.hashCode()) ^
204 (mVersion == null ? 0 : mVersion.hashCode()) ^
Philip P. Moltmanna2c6eea2018-08-24 09:50:40 -0700205 (mUri == null ? 0 : mUri.hashCode());
Mike Lockwoodc6f23e82011-03-09 12:05:20 -0500206 }
207
208 @Override
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500209 public String toString() {
210 return "UsbAccessory[mManufacturer=" + mManufacturer +
211 ", mModel=" + mModel +
Mike Lockwoodac36d7c2011-03-09 22:03:57 -0500212 ", mDescription=" + mDescription +
213 ", mVersion=" + mVersion +
Mike Lockwood015b1ec2011-03-14 18:24:35 -0400214 ", mUri=" + mUri +
Philip P. Moltmanna2c6eea2018-08-24 09:50:40 -0700215 ", mSerialNumberReader=" + mSerialNumberReader + "]";
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500216 }
217
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700218 public static final @android.annotation.NonNull Parcelable.Creator<UsbAccessory> CREATOR =
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500219 new Parcelable.Creator<UsbAccessory>() {
220 public UsbAccessory createFromParcel(Parcel in) {
221 String manufacturer = in.readString();
222 String model = in.readString();
Mike Lockwoodac36d7c2011-03-09 22:03:57 -0500223 String description = in.readString();
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500224 String version = in.readString();
Mike Lockwoodac36d7c2011-03-09 22:03:57 -0500225 String uri = in.readString();
Philip P. Moltmanna2c6eea2018-08-24 09:50:40 -0700226 IUsbSerialReader serialNumberReader = IUsbSerialReader.Stub.asInterface(
227 in.readStrongBinder());
228
229 return new UsbAccessory(manufacturer, model, description, version, uri,
230 serialNumberReader);
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500231 }
232
233 public UsbAccessory[] newArray(int size) {
234 return new UsbAccessory[size];
235 }
236 };
237
238 public int describeContents() {
239 return 0;
240 }
241
242 public void writeToParcel(Parcel parcel, int flags) {
243 parcel.writeString(mManufacturer);
244 parcel.writeString(mModel);
Mike Lockwoodac36d7c2011-03-09 22:03:57 -0500245 parcel.writeString(mDescription);
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500246 parcel.writeString(mVersion);
Mike Lockwoodac36d7c2011-03-09 22:03:57 -0500247 parcel.writeString(mUri);
Philip P. Moltmanna2c6eea2018-08-24 09:50:40 -0700248 parcel.writeStrongBinder(mSerialNumberReader.asBinder());
Mike Lockwood9182d3c2011-02-15 09:50:22 -0500249 }
250}