diff options
author | Mike Lockwood <lockwood@android.com> | 2011-03-01 10:23:48 -0800 |
---|---|---|
committer | Mike Lockwood <lockwood@android.com> | 2011-03-01 23:09:12 -0800 |
commit | 40bbf9295d5245d3917629ce15f7b37670aef1ac (patch) | |
tree | 40bb301c9e739a9026a6351c3d79c80ea43ca01b /libs | |
parent | db52ab69f22e24615eaa2e8f9845e157426d3dd6 (diff) | |
download | frameworks_base-40bbf9295d5245d3917629ce15f7b37670aef1ac.zip frameworks_base-40bbf9295d5245d3917629ce15f7b37670aef1ac.tar.gz frameworks_base-40bbf9295d5245d3917629ce15f7b37670aef1ac.tar.bz2 |
DO NOT MERGE: Backport USB accessory support to gingerbread
Signed-off-by: Mike Lockwood <lockwood@android.com>
Diffstat (limited to 'libs')
-rw-r--r-- | libs/usb/src/com/google/android/usb/UsbAccessory.java | 96 | ||||
-rw-r--r-- | libs/usb/src/com/google/android/usb/UsbManager.java | 127 |
2 files changed, 223 insertions, 0 deletions
diff --git a/libs/usb/src/com/google/android/usb/UsbAccessory.java b/libs/usb/src/com/google/android/usb/UsbAccessory.java new file mode 100644 index 0000000..931f42e --- /dev/null +++ b/libs/usb/src/com/google/android/usb/UsbAccessory.java @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.android.usb; + +/** + * A class representing a USB accessory. + */ +public final class UsbAccessory { + + private final String mManufacturer; + private final String mModel; + private final String mType; + private final String mVersion; + + /* package */ UsbAccessory(android.hardware.usb.UsbAccessory accessory) { + mManufacturer = accessory.getManufacturer(); + mModel = accessory.getModel(); + mType = accessory.getType(); + mVersion = accessory.getVersion(); + } + + /** + * Returns the manufacturer of the accessory. + * + * @return the accessory manufacturer + */ + public String getManufacturer() { + return mManufacturer; + } + + /** + * Returns the model name of the accessory. + * + * @return the accessory model + */ + public String getModel() { + return mModel; + } + + /** + * Returns the type of the accessory. + * + * @return the accessory type + */ + public String getType() { + return mType; + } + + /** + * Returns the version of the accessory. + * + * @return the accessory version + */ + public String getVersion() { + return mVersion; + } + + private static boolean compare(String s1, String s2) { + if (s1 == null) return (s2 == null); + return s1.equals(s2); + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof UsbAccessory) { + UsbAccessory accessory = (UsbAccessory)obj; + return (compare(mManufacturer, accessory.getManufacturer()) && + compare(mModel, accessory.getModel()) && + compare(mType, accessory.getType()) && + compare(mVersion, accessory.getVersion())); + } + return false; + } + + @Override + public String toString() { + return "UsbAccessory[mManufacturer=" + mManufacturer + + ", mModel=" + mModel + + ", mType=" + mType + + ", mVersion=" + mVersion + "]"; + } +} diff --git a/libs/usb/src/com/google/android/usb/UsbManager.java b/libs/usb/src/com/google/android/usb/UsbManager.java new file mode 100644 index 0000000..d7afb95 --- /dev/null +++ b/libs/usb/src/com/google/android/usb/UsbManager.java @@ -0,0 +1,127 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package com.google.android.usb; + +import android.content.Context; +import android.content.Intent; +import android.hardware.usb.IUsbManager; +import android.os.IBinder; +import android.os.ParcelFileDescriptor; +import android.os.RemoteException; +import android.os.ServiceManager; +import android.util.Log; + +/** + * This class allows you to access the state of USB, both in host and device mode. + * + * <p>You can obtain an instance of this class by calling {@link #getInstance} + * + */ +public class UsbManager { + private static final String TAG = "UsbManager"; + + /** + * Broadcast Action: A broadcast for USB accessory attached event. + * + * This intent is sent when a USB accessory is attached. + * Call {@link #getAccessory(android.content.Intent)} to retrieve the + * {@link com.google.android.usb.UsbAccessory} for the attached accessory. + */ + public static final String ACTION_USB_ACCESSORY_ATTACHED = + "android.hardware.usb.action.USB_ACCESSORY_ATTACHED"; + + /** + * Broadcast Action: A broadcast for USB accessory detached event. + * + * This intent is sent when a USB accessory is detached. + * Call {@link #getAccessory(android.content.Intent)} to retrieve the + * {@link com.google.android.usb.UsbAccessory} for the attached accessory that was detached. + */ + public static final String ACTION_USB_ACCESSORY_DETACHED = + "android.hardware.usb.action.USB_ACCESSORY_DETACHED"; + + private final IUsbManager mService; + + private UsbManager(IUsbManager service) { + mService = service; + } + + /** + * Returns a new instance of this class. + * + * @return UsbManager instance. + */ + public static UsbManager getInstance() { + IBinder b = ServiceManager.getService(Context.USB_SERVICE); + return new UsbManager(IUsbManager.Stub.asInterface(b)); + } + + /** + * Returns the {@link com.google.android.usb.UsbAccessory} for + * a {@link #ACTION_USB_ACCESSORY_ATTACHED} or {@link #ACTION_USB_ACCESSORY_ATTACHED} + * broadcast Intent + * + * @return UsbAccessory for the broadcast. + */ + public static UsbAccessory getAccessory(Intent intent) { + android.hardware.usb.UsbAccessory accessory = + intent.getParcelableExtra(android.hardware.usb.UsbManager.EXTRA_ACCESSORY); + if (accessory == null) { + return null; + } else { + return new UsbAccessory(accessory); + } + } + + /** + * Returns a list of currently attached USB accessories. + * (in the current implementation there can be at most one) + * + * @return list of USB accessories, or null if none are attached. + */ + public UsbAccessory[] getAccessoryList() { + try { + android.hardware.usb.UsbAccessory accessory = mService.getCurrentAccessory(); + if (accessory == null) { + return null; + } else { + return new UsbAccessory[] { new UsbAccessory(accessory) }; + } + } catch (RemoteException e) { + Log.e(TAG, "RemoteException in getAccessoryList" , e); + return null; + } + } + + /** + * Opens a file descriptor for reading and writing data to the USB accessory. + * + * @param accessory the USB accessory to open + * @return file descriptor, or null if the accessor could not be opened. + */ + public ParcelFileDescriptor openAccessory(UsbAccessory accessory) { + try { + return mService.openAccessory(new android.hardware.usb.UsbAccessory( + accessory.getManufacturer(),accessory.getModel(), + accessory.getType(), accessory.getVersion())); + } catch (RemoteException e) { + Log.e(TAG, "RemoteException in openAccessory" , e); + return null; + } + } +} |