diff options
author | Mike Lockwood <lockwood@android.com> | 2011-03-01 22:55:51 -0800 |
---|---|---|
committer | Mike Lockwood <lockwood@android.com> | 2011-03-02 15:23:41 -0800 |
commit | 1110748b2df664f9c5066819c1f0616eae3394a7 (patch) | |
tree | da4b6118573d9dcaaeab5d79f2ebd3aabafac124 /libs/usb/src/com/android/future | |
parent | 40bbf9295d5245d3917629ce15f7b37670aef1ac (diff) | |
download | frameworks_base-1110748b2df664f9c5066819c1f0616eae3394a7.zip frameworks_base-1110748b2df664f9c5066819c1f0616eae3394a7.tar.gz frameworks_base-1110748b2df664f9c5066819c1f0616eae3394a7.tar.bz2 |
DO NOT MERGE: USB accessory support library
This provides a mechanism for developing applications to work with
USB accessories in versions of android prior to the introduction
of the android.hardware.UsbManager APIs.
Applications should link against the com.android.future.usb.accessory
library to use this support.
Change-Id: I0b61e20b63eec42c506f0895a0c9a439bdfdf7f5
Signed-off-by: Mike Lockwood <lockwood@android.com>
Diffstat (limited to 'libs/usb/src/com/android/future')
-rw-r--r-- | libs/usb/src/com/android/future/usb/UsbAccessory.java | 96 | ||||
-rw-r--r-- | libs/usb/src/com/android/future/usb/UsbManager.java | 127 |
2 files changed, 223 insertions, 0 deletions
diff --git a/libs/usb/src/com/android/future/usb/UsbAccessory.java b/libs/usb/src/com/android/future/usb/UsbAccessory.java new file mode 100644 index 0000000..cdd2b73 --- /dev/null +++ b/libs/usb/src/com/android/future/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.android.future.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/android/future/usb/UsbManager.java b/libs/usb/src/com/android/future/usb/UsbManager.java new file mode 100644 index 0000000..f74b291 --- /dev/null +++ b/libs/usb/src/com/android/future/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.android.future.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; + } + } +} |