UsbAccessory.java revision ac36d7c715a9cd793b2dce6de547594810101c3a
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
17package android.hardware.usb;
18
19import android.os.Bundle;
20import android.os.Parcel;
21import android.os.Parcelable;
22import android.util.Log;
23
24/**
25 * A class representing a USB accessory.
26 */
27public class UsbAccessory implements Parcelable {
28
29    private static final String TAG = "UsbAccessory";
30
31    private final String mManufacturer;
32    private final String mModel;
33    private final String mDescription;
34    private final String mVersion;
35    private final String mUri;
36
37    /**
38     * UsbAccessory should only be instantiated by UsbService implementation
39     * @hide
40     */
41    public UsbAccessory(String manufacturer, String model, String description,
42            String version, String uri) {
43        mManufacturer = manufacturer;
44        mModel = model;
45        mDescription = description;
46        mVersion = version;
47        mUri = uri;
48    }
49
50    /**
51     * UsbAccessory should only be instantiated by UsbService implementation
52     * @hide
53     */
54    public UsbAccessory(String[] strings) {
55        mManufacturer = strings[0];
56        mModel = strings[1];
57        mDescription = strings[2];
58        mVersion = strings[3];
59        mUri = strings[4];
60    }
61
62    /**
63     * Returns the manufacturer of the accessory.
64     *
65     * @return the accessory manufacturer
66     */
67    public String getManufacturer() {
68        return mManufacturer;
69    }
70
71    /**
72     * Returns the model name of the accessory.
73     *
74     * @return the accessory model
75     */
76    public String getModel() {
77        return mModel;
78    }
79
80    /**
81     * Returns a user visible description of the accessory.
82     *
83     * @return the accessory description
84     */
85    public String getDescription() {
86        return mDescription;
87    }
88
89    /**
90     * Returns the version of the accessory.
91     *
92     * @return the accessory version
93     */
94    public String getVersion() {
95        return mVersion;
96    }
97
98    /**
99     * Returns the URI for the accessory.
100     * This is an optional URI that might show information about the accessory
101     * or provide the option to download an application for the accessory
102     *
103     * @return the accessory URI
104     */
105    public String getUri() {
106        return mUri;
107    }
108
109    private static boolean compare(String s1, String s2) {
110        if (s1 == null) return (s2 == null);
111        return s1.equals(s2);
112    }
113
114    @Override
115    public boolean equals(Object obj) {
116        if (obj instanceof UsbAccessory) {
117            UsbAccessory accessory = (UsbAccessory)obj;
118            return (compare(mManufacturer, accessory.getManufacturer()) &&
119                    compare(mModel, accessory.getModel()) &&
120                    compare(mDescription, accessory.getDescription()) &&
121                    compare(mVersion, accessory.getVersion()) &&
122                    compare(mUri, accessory.getUri()));
123        }
124        return false;
125    }
126
127    @Override
128    public int hashCode() {
129        return ((mManufacturer == null ? 0 : mManufacturer.hashCode()) ^
130                (mModel == null ? 0 : mModel.hashCode()) ^
131                (mDescription == null ? 0 : mDescription.hashCode()) ^
132                (mVersion == null ? 0 : mVersion.hashCode()) ^
133                (mUri == null ? 0 : mUri.hashCode()));
134    }
135
136    @Override
137    public String toString() {
138        return "UsbAccessory[mManufacturer=" + mManufacturer +
139                            ", mModel=" + mModel +
140                            ", mDescription=" + mDescription +
141                            ", mVersion=" + mVersion +
142                            ", mUri=" + mUri + "]";
143    }
144
145    public static final Parcelable.Creator<UsbAccessory> CREATOR =
146        new Parcelable.Creator<UsbAccessory>() {
147        public UsbAccessory createFromParcel(Parcel in) {
148            String manufacturer = in.readString();
149            String model = in.readString();
150            String description = in.readString();
151            String version = in.readString();
152            String uri = in.readString();
153            return new UsbAccessory(manufacturer, model, description, version, uri);
154        }
155
156        public UsbAccessory[] newArray(int size) {
157            return new UsbAccessory[size];
158        }
159    };
160
161    public int describeContents() {
162        return 0;
163    }
164
165    public void writeToParcel(Parcel parcel, int flags) {
166        parcel.writeString(mManufacturer);
167        parcel.writeString(mModel);
168        parcel.writeString(mDescription);
169        parcel.writeString(mVersion);
170        parcel.writeString(mUri);
171   }
172}
173