Home | History | Annotate | Download | only in usb
      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 
     17 package android.hardware.usb;
     18 
     19 import android.os.Bundle;
     20 import android.os.Parcel;
     21 import android.os.Parcelable;
     22 import android.util.Log;
     23 
     24 /**
     25  * 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.
     40  *
     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>
     46  */
     47 public class UsbAccessory implements Parcelable {
     48 
     49     private static final String TAG = "UsbAccessory";
     50 
     51     private final String mManufacturer;
     52     private final String mModel;
     53     private final String mDescription;
     54     private final String mVersion;
     55     private final String mUri;
     56     private final String mSerial;
     57 
     58     /** @hide */
     59     public static final int MANUFACTURER_STRING = 0;
     60     /** @hide */
     61     public static final int MODEL_STRING = 1;
     62     /** @hide */
     63     public static final int DESCRIPTION_STRING = 2;
     64     /** @hide */
     65     public static final int VERSION_STRING = 3;
     66     /** @hide */
     67     public static final int URI_STRING = 4;
     68     /** @hide */
     69     public static final int SERIAL_STRING = 5;
     70 
     71     /**
     72      * UsbAccessory should only be instantiated by UsbService implementation
     73      * @hide
     74      */
     75     public UsbAccessory(String manufacturer, String model, String description,
     76             String version, String uri, String serial) {
     77         mManufacturer = manufacturer;
     78         mModel = model;
     79         mDescription = description;
     80         mVersion = version;
     81         mUri = uri;
     82         mSerial = serial;
     83     }
     84 
     85     /**
     86      * UsbAccessory should only be instantiated by UsbService implementation
     87      * @hide
     88      */
     89     public UsbAccessory(String[] strings) {
     90         mManufacturer = strings[MANUFACTURER_STRING];
     91         mModel = strings[MODEL_STRING];
     92         mDescription = strings[DESCRIPTION_STRING];
     93         mVersion = strings[VERSION_STRING];
     94         mUri = strings[URI_STRING];
     95         mSerial = strings[SERIAL_STRING];
     96     }
     97 
     98     /**
     99      * Returns the manufacturer name of the accessory.
    100      *
    101      * @return the accessory manufacturer
    102      */
    103     public String getManufacturer() {
    104         return mManufacturer;
    105     }
    106 
    107     /**
    108      * Returns the model name of the accessory.
    109      *
    110      * @return the accessory model
    111      */
    112     public String getModel() {
    113         return mModel;
    114     }
    115 
    116     /**
    117      * Returns a user visible description of the accessory.
    118      *
    119      * @return the accessory description
    120      */
    121     public String getDescription() {
    122         return mDescription;
    123     }
    124 
    125     /**
    126      * Returns the version of the accessory.
    127      *
    128      * @return the accessory version
    129      */
    130     public String getVersion() {
    131         return mVersion;
    132     }
    133 
    134     /**
    135      * Returns the URI for the accessory.
    136      * This is an optional URI that might show information about the accessory
    137      * or provide the option to download an application for the accessory
    138      *
    139      * @return the accessory URI
    140      */
    141     public String getUri() {
    142         return mUri;
    143     }
    144 
    145     /**
    146      * Returns the unique serial number for the accessory.
    147      * This is an optional serial number that can be used to differentiate
    148      * between individual accessories of the same model and manufacturer
    149      *
    150      * @return the unique serial number
    151      */
    152     public String getSerial() {
    153         return mSerial;
    154     }
    155 
    156     private static boolean compare(String s1, String s2) {
    157         if (s1 == null) return (s2 == null);
    158         return s1.equals(s2);
    159     }
    160 
    161     @Override
    162     public boolean equals(Object obj) {
    163         if (obj instanceof UsbAccessory) {
    164             UsbAccessory accessory = (UsbAccessory)obj;
    165             return (compare(mManufacturer, accessory.getManufacturer()) &&
    166                     compare(mModel, accessory.getModel()) &&
    167                     compare(mDescription, accessory.getDescription()) &&
    168                     compare(mVersion, accessory.getVersion()) &&
    169                     compare(mUri, accessory.getUri()) &&
    170                     compare(mSerial, accessory.getSerial()));
    171         }
    172         return false;
    173     }
    174 
    175     @Override
    176     public int hashCode() {
    177         return ((mManufacturer == null ? 0 : mManufacturer.hashCode()) ^
    178                 (mModel == null ? 0 : mModel.hashCode()) ^
    179                 (mDescription == null ? 0 : mDescription.hashCode()) ^
    180                 (mVersion == null ? 0 : mVersion.hashCode()) ^
    181                 (mUri == null ? 0 : mUri.hashCode()) ^
    182                 (mSerial == null ? 0 : mSerial.hashCode()));
    183     }
    184 
    185     @Override
    186     public String toString() {
    187         return "UsbAccessory[mManufacturer=" + mManufacturer +
    188                             ", mModel=" + mModel +
    189                             ", mDescription=" + mDescription +
    190                             ", mVersion=" + mVersion +
    191                             ", mUri=" + mUri +
    192                             ", mSerial=" + mSerial + "]";
    193     }
    194 
    195     public static final Parcelable.Creator<UsbAccessory> CREATOR =
    196         new Parcelable.Creator<UsbAccessory>() {
    197         public UsbAccessory createFromParcel(Parcel in) {
    198             String manufacturer = in.readString();
    199             String model = in.readString();
    200             String description = in.readString();
    201             String version = in.readString();
    202             String uri = in.readString();
    203             String serial = in.readString();
    204             return new UsbAccessory(manufacturer, model, description, version, uri, serial);
    205         }
    206 
    207         public UsbAccessory[] newArray(int size) {
    208             return new UsbAccessory[size];
    209         }
    210     };
    211 
    212     public int describeContents() {
    213         return 0;
    214     }
    215 
    216     public void writeToParcel(Parcel parcel, int flags) {
    217         parcel.writeString(mManufacturer);
    218         parcel.writeString(mModel);
    219         parcel.writeString(mDescription);
    220         parcel.writeString(mVersion);
    221         parcel.writeString(mUri);
    222         parcel.writeString(mSerial);
    223    }
    224 }
    225