summaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorMike Lockwood <lockwood@android.com>2011-02-16 12:42:35 -0800
committerAndroid (Google) Code Review <android-gerrit@google.com>2011-02-16 12:42:35 -0800
commit984dd8a206e782027c2b0c866aa845e6d236f218 (patch)
treebc19dd5e86fe963321b06b72b09479a6a6725907 /core
parentca20af0c2990a969f1a793821d2ee239b0310d45 (diff)
parent9182d3c4eb1f9065cb33df5a3594969dd0d42acc (diff)
downloadframeworks_base-984dd8a206e782027c2b0c866aa845e6d236f218.zip
frameworks_base-984dd8a206e782027c2b0c866aa845e6d236f218.tar.gz
frameworks_base-984dd8a206e782027c2b0c866aa845e6d236f218.tar.bz2
Merge "UsbManager: New APIs for USB accessories"
Diffstat (limited to 'core')
-rw-r--r--core/java/android/hardware/IUsbManager.aidl3
-rw-r--r--core/java/android/hardware/UsbAccessory.aidl19
-rw-r--r--core/java/android/hardware/UsbAccessory.java131
-rw-r--r--core/java/android/hardware/UsbManager.java149
-rw-r--r--core/res/AndroidManifest.xml4
5 files changed, 297 insertions, 9 deletions
diff --git a/core/java/android/hardware/IUsbManager.aidl b/core/java/android/hardware/IUsbManager.aidl
index b50b6b9..6c99ab3 100644
--- a/core/java/android/hardware/IUsbManager.aidl
+++ b/core/java/android/hardware/IUsbManager.aidl
@@ -16,6 +16,7 @@
package android.hardware;
+import android.hardware.UsbAccessory;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
@@ -25,4 +26,6 @@ interface IUsbManager
/* Returns a list of all currently attached USB devices */
void getDeviceList(out Bundle devices);
ParcelFileDescriptor openDevice(String deviceName);
+ UsbAccessory getCurrentAccessory();
+ ParcelFileDescriptor openAccessory();
}
diff --git a/core/java/android/hardware/UsbAccessory.aidl b/core/java/android/hardware/UsbAccessory.aidl
new file mode 100644
index 0000000..97a777b
--- /dev/null
+++ b/core/java/android/hardware/UsbAccessory.aidl
@@ -0,0 +1,19 @@
+/*
+ * 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 android.hardware;
+
+parcelable UsbAccessory;
diff --git a/core/java/android/hardware/UsbAccessory.java b/core/java/android/hardware/UsbAccessory.java
new file mode 100644
index 0000000..71672fa
--- /dev/null
+++ b/core/java/android/hardware/UsbAccessory.java
@@ -0,0 +1,131 @@
+/*
+ * 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 android.hardware;
+
+import android.os.Bundle;
+import android.os.Parcel;
+import android.os.Parcelable;
+import android.os.ParcelFileDescriptor;
+import android.util.Log;
+
+/**
+ * A class representing a USB accessory.
+ */
+public final class UsbAccessory implements Parcelable {
+
+ private static final String TAG = "UsbAccessory";
+
+ private String mManufacturer;
+ private String mModel;
+ private String mType;
+ private String mVersion;
+
+ private UsbAccessory() {
+ }
+
+ /**
+ * UsbAccessory should only be instantiated by UsbService implementation
+ * @hide
+ */
+ public UsbAccessory(String manufacturer, String model, String type, String version) {
+ mManufacturer = manufacturer;
+ mModel = model;
+ mType = type;
+ mVersion = version;
+ }
+
+ /**
+ * UsbAccessory should only be instantiated by UsbService implementation
+ * @hide
+ */
+ public UsbAccessory(String[] strings) {
+ mManufacturer = strings[0];
+ mModel = strings[1];
+ mType = strings[2];
+ mVersion = strings[3];
+ }
+
+ /**
+ * 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;
+ }
+
+ @Override
+ public String toString() {
+ return "UsbAccessory[mManufacturer=" + mManufacturer +
+ ", mModel=" + mModel +
+ ", mType=" + mType +
+ ", mVersion=" + mVersion + "]";
+ }
+
+ public static final Parcelable.Creator<UsbAccessory> CREATOR =
+ new Parcelable.Creator<UsbAccessory>() {
+ public UsbAccessory createFromParcel(Parcel in) {
+ String manufacturer = in.readString();
+ String model = in.readString();
+ String type = in.readString();
+ String version = in.readString();
+ return new UsbAccessory(manufacturer, model, type, version);
+ }
+
+ public UsbAccessory[] newArray(int size) {
+ return new UsbAccessory[size];
+ }
+ };
+
+ public int describeContents() {
+ return 0;
+ }
+
+ public void writeToParcel(Parcel parcel, int flags) {
+ parcel.writeString(mManufacturer);
+ parcel.writeString(mModel);
+ parcel.writeString(mType);
+ parcel.writeString(mVersion);
+ }
+}
diff --git a/core/java/android/hardware/UsbManager.java b/core/java/android/hardware/UsbManager.java
index 8fad210..0f616ff 100644
--- a/core/java/android/hardware/UsbManager.java
+++ b/core/java/android/hardware/UsbManager.java
@@ -24,6 +24,7 @@ import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
+import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
@@ -44,11 +45,14 @@ public class UsbManager {
* Broadcast Action: A sticky broadcast for USB state change events when in device mode.
*
* This is a sticky broadcast for clients that includes USB connected/disconnected state,
- * the USB configuration that is currently set and a bundle containing name/value pairs
- * with the names of the functions and a value of either {@link #USB_FUNCTION_ENABLED}
- * or {@link #USB_FUNCTION_DISABLED}.
- * Possible USB function names include {@link #USB_FUNCTION_MASS_STORAGE},
- * {@link #USB_FUNCTION_ADB}, {@link #USB_FUNCTION_RNDIS} and {@link #USB_FUNCTION_MTP}.
+ * <ul>
+ * <li> {@link #USB_CONNECTED} boolean indicating whether USB is connected or disconnected.
+ * <li> {@link #USB_CONFIGURATION} a Bundle containing name/value pairs where the name
+ * is the name of a USB function and the value is either {@link #USB_FUNCTION_ENABLED}
+ * or {@link #USB_FUNCTION_DISABLED}. The possible function names include
+ * {@link #USB_FUNCTION_MASS_STORAGE}, {@link #USB_FUNCTION_ADB}, {@link #USB_FUNCTION_RNDIS},
+ * {@link #USB_FUNCTION_MTP} and {@link #USB_FUNCTION_ACCESSORY}.
+ * </ul>
*/
public static final String ACTION_USB_STATE =
"android.hardware.action.USB_STATE";
@@ -57,6 +61,16 @@ public class UsbManager {
* Broadcast Action: A broadcast for USB device attached event.
*
* This intent is sent when a USB device is attached to the USB bus when in host mode.
+ * <ul>
+ * <li> {@link #EXTRA_DEVICE_NAME} containing the device's name (String)
+ * <li> {@link #EXTRA_VENDOR_ID} containing the device's vendor ID (Integer)
+ * <li> {@link #EXTRA_PRODUCT_ID} containing the device's product ID (Integer)
+ * <li> {@link #EXTRA_DEVICE_CLASS} } containing the device class (Integer)
+ * <li> {@link #EXTRA_DEVICE_SUBCLASS} containing the device subclass (Integer)
+ * <li> {@link #EXTRA_DEVICE_PROTOCOL} containing the device protocol (Integer)
+ * <li> {@link #EXTRA_DEVICE} containing the {@link android.hardware.UsbDevice}
+ * for the attached device
+ * </ul>
*/
public static final String ACTION_USB_DEVICE_ATTACHED =
"android.hardware.action.USB_DEVICE_ATTACHED";
@@ -65,10 +79,41 @@ public class UsbManager {
* Broadcast Action: A broadcast for USB device detached event.
*
* This intent is sent when a USB device is detached from the USB bus when in host mode.
+ * <ul>
+ * <li> {@link #EXTRA_DEVICE_NAME} containing the device's name (String)
+ * </ul>
*/
public static final String ACTION_USB_DEVICE_DETACHED =
"android.hardware.action.USB_DEVICE_DETACHED";
+ /**
+ * Broadcast Action: A broadcast for USB accessory attached event.
+ *
+ * This intent is sent when a USB accessory is attached.
+ * <ul>
+ * <li> {@link #EXTRA_ACCESSORY_MANUFACTURER} containing the accessory's manufacturer (String)
+ * <li> {@link #EXTRA_ACCESSORY_PRODUCT} containing the accessory's product name (String)
+ * <li> {@link #EXTRA_ACCESSORY_TYPE} containing the accessory's type (String)
+ * <li> {@link #EXTRA_ACCESSORY_VERSION} containing the accessory's version (String)
+ * <li> {@link #EXTRA_ACCESSORY} containing the {@link android.hardware.UsbAccessory}
+ * for the attached accessory
+ * </ul>
+ */
+ public static final String ACTION_USB_ACCESSORY_ATTACHED =
+ "android.hardware.action.USB_ACCESSORY_ATTACHED";
+
+ /**
+ * Broadcast Action: A broadcast for USB accessory detached event.
+ *
+ * This intent is sent when a USB accessory is detached.
+ * <ul>
+ * <li> {@link #EXTRA_ACCESSORY} containing the {@link android.hardware.UsbAccessory}
+ * for the attached accessory that was detached
+ * </ul>
+ */
+ public static final String ACTION_USB_ACCESSORY_DETACHED =
+ "android.hardware.action.USB_ACCESSORY_DETACHED";
+
/**
* Boolean extra indicating whether USB is connected or disconnected.
* Used in extras for the {@link #ACTION_USB_STATE} broadcast.
@@ -106,14 +151,22 @@ public class UsbManager {
public static final String USB_FUNCTION_MTP = "mtp";
/**
- * Value indicating that a USB function is enabled.
+ * Name of the Accessory USB function.
* Used in extras for the {@link #ACTION_USB_STATE} broadcast
*/
+ public static final String USB_FUNCTION_ACCESSORY = "accessory";
+
+ /**
+ * Value indicating that a USB function is enabled.
+ * Used in {@link #USB_CONFIGURATION} extras bundle for the
+ * {@link #ACTION_USB_STATE} broadcast
+ */
public static final String USB_FUNCTION_ENABLED = "enabled";
/**
* Value indicating that a USB function is disabled.
- * Used in extras for the {@link #ACTION_USB_STATE} broadcast
+ * Used in {@link #USB_CONFIGURATION} extras bundle for the
+ * {@link #ACTION_USB_STATE} broadcast
*/
public static final String USB_FUNCTION_DISABLED = "disabled";
@@ -158,8 +211,39 @@ public class UsbManager {
* Name of extra for {@link #ACTION_USB_DEVICE_ATTACHED} broadcast
* containing the UsbDevice object for the device.
*/
+
public static final String EXTRA_DEVICE = "device";
+ /**
+ * Name of extra for {@link #ACTION_USB_ACCESSORY_ATTACHED} broadcast
+ * containing the UsbAccessory object for the accessory.
+ */
+ public static final String EXTRA_ACCESSORY = "accessory";
+
+ /**
+ * Name of extra for {@link #ACTION_USB_ACCESSORY_ATTACHED} broadcast
+ * containing the accessory's manufacturer name.
+ */
+ public static final String EXTRA_ACCESSORY_MANUFACTURER = "accessory-manufacturer";
+
+ /**
+ * Name of extra for {@link #ACTION_USB_ACCESSORY_ATTACHED} broadcast
+ * containing the accessory's product name.
+ */
+ public static final String EXTRA_ACCESSORY_PRODUCT = "accessory-product";
+
+ /**
+ * Name of extra for {@link #ACTION_USB_ACCESSORY_ATTACHED} broadcast
+ * containing the accessory's type.
+ */
+ public static final String EXTRA_ACCESSORY_TYPE = "accessory-type";
+
+ /**
+ * Name of extra for {@link #ACTION_USB_ACCESSORY_ATTACHED} broadcast
+ * containing the accessory's version.
+ */
+ public static final String EXTRA_ACCESSORY_VERSION = "accessory-version";
+
private IUsbManager mService;
/**
@@ -214,6 +298,41 @@ public class UsbManager {
}
}
+ /**
+ * 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 {
+ UsbAccessory accessory = mService.getCurrentAccessory();
+ if (accessory == null) {
+ return null;
+ } else {
+ return new UsbAccessory[] { accessory };
+ }
+ } catch (RemoteException e) {
+ Log.e(TAG, "RemoteException in openAccessory" , 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();
+ } catch (RemoteException e) {
+ Log.e(TAG, "RemoteException in openAccessory" , e);
+ return null;
+ }
+ }
+
private static File getFunctionEnableFile(String function) {
return new File("/sys/class/usb_composite/" + function + "/enable");
}
@@ -245,4 +364,20 @@ public class UsbManager {
return false;
}
}
+
+ /**
+ * Enables or disables a USB function.
+ *
+ * @hide
+ */
+ public static boolean setFunctionEnabled(String function, boolean enable) {
+ try {
+ FileOutputStream stream = new FileOutputStream(getFunctionEnableFile(function));
+ stream.write(enable ? '1' : '0');
+ stream.close();
+ return true;
+ } catch (IOException e) {
+ return false;
+ }
+ }
}
diff --git a/core/res/AndroidManifest.xml b/core/res/AndroidManifest.xml
index 8e8383a..1df6fe5 100644
--- a/core/res/AndroidManifest.xml
+++ b/core/res/AndroidManifest.xml
@@ -82,9 +82,9 @@
<protected-broadcast android:name="android.bluetooth.device.action.PAIRING_REQUEST" />
<protected-broadcast android:name="android.bluetooth.device.action.PAIRING_CANCEL" />
- <protected-broadcast android:name="android.hardware.action.USB_CONNECTED" />
- <protected-broadcast android:name="android.hardware.action.USB_DISCONNECTED" />
<protected-broadcast android:name="android.hardware.action.USB_STATE" />
+ <protected-broadcast android:name="android.hardware.action.USB_ACCESSORY_ATTACHED" />
+ <protected-broadcast android:name="android.hardware.action.USB_ACCESSORY_ATTACHED" />
<protected-broadcast android:name="android.hardware.action.USB_DEVICE_ATTACHED" />
<protected-broadcast android:name="android.hardware.action.USB_DEVICE_DETACHED" />