Merge "Add support for USB accessory serial numbers" into honeycomb-mr1
This commit is contained in:
committed by
Android (Google) Code Review
commit
221830b93d
@@ -94481,6 +94481,17 @@
|
||||
visibility="public"
|
||||
>
|
||||
</method>
|
||||
<method name="getSerial"
|
||||
return="java.lang.String"
|
||||
abstract="false"
|
||||
native="false"
|
||||
synchronized="false"
|
||||
static="false"
|
||||
final="false"
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
</method>
|
||||
<method name="getUri"
|
||||
return="java.lang.String"
|
||||
abstract="false"
|
||||
@@ -267064,7 +267075,7 @@
|
||||
deprecated="not deprecated"
|
||||
visibility="public"
|
||||
>
|
||||
<parameter name="arg0" type="T">
|
||||
<parameter name="t" type="T">
|
||||
</parameter>
|
||||
</method>
|
||||
</interface>
|
||||
|
||||
@@ -33,18 +33,20 @@ public class UsbAccessory implements Parcelable {
|
||||
private final String mDescription;
|
||||
private final String mVersion;
|
||||
private final String mUri;
|
||||
private final String mSerial;
|
||||
|
||||
/**
|
||||
* UsbAccessory should only be instantiated by UsbService implementation
|
||||
* @hide
|
||||
*/
|
||||
public UsbAccessory(String manufacturer, String model, String description,
|
||||
String version, String uri) {
|
||||
String version, String uri, String serial) {
|
||||
mManufacturer = manufacturer;
|
||||
mModel = model;
|
||||
mDescription = description;
|
||||
mVersion = version;
|
||||
mUri = uri;
|
||||
mSerial = serial;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,6 +59,7 @@ public class UsbAccessory implements Parcelable {
|
||||
mDescription = strings[2];
|
||||
mVersion = strings[3];
|
||||
mUri = strings[4];
|
||||
mSerial = strings[5];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -106,6 +109,17 @@ public class UsbAccessory implements Parcelable {
|
||||
return mUri;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the unique serial number for the accessory.
|
||||
* This is an optional serial number that can be used to differentiate
|
||||
* between individual accessories of the same model and manufacturer
|
||||
*
|
||||
* @return the unique serial number
|
||||
*/
|
||||
public String getSerial() {
|
||||
return mSerial;
|
||||
}
|
||||
|
||||
private static boolean compare(String s1, String s2) {
|
||||
if (s1 == null) return (s2 == null);
|
||||
return s1.equals(s2);
|
||||
@@ -119,7 +133,8 @@ public class UsbAccessory implements Parcelable {
|
||||
compare(mModel, accessory.getModel()) &&
|
||||
compare(mDescription, accessory.getDescription()) &&
|
||||
compare(mVersion, accessory.getVersion()) &&
|
||||
compare(mUri, accessory.getUri()));
|
||||
compare(mUri, accessory.getUri()) &&
|
||||
compare(mSerial, accessory.getSerial()));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -130,7 +145,8 @@ public class UsbAccessory implements Parcelable {
|
||||
(mModel == null ? 0 : mModel.hashCode()) ^
|
||||
(mDescription == null ? 0 : mDescription.hashCode()) ^
|
||||
(mVersion == null ? 0 : mVersion.hashCode()) ^
|
||||
(mUri == null ? 0 : mUri.hashCode()));
|
||||
(mUri == null ? 0 : mUri.hashCode()) ^
|
||||
(mSerial == null ? 0 : mSerial.hashCode()));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -139,7 +155,8 @@ public class UsbAccessory implements Parcelable {
|
||||
", mModel=" + mModel +
|
||||
", mDescription=" + mDescription +
|
||||
", mVersion=" + mVersion +
|
||||
", mUri=" + mUri + "]";
|
||||
", mUri=" + mUri +
|
||||
", mSerial=" + mSerial + "]";
|
||||
}
|
||||
|
||||
public static final Parcelable.Creator<UsbAccessory> CREATOR =
|
||||
@@ -150,7 +167,8 @@ public class UsbAccessory implements Parcelable {
|
||||
String description = in.readString();
|
||||
String version = in.readString();
|
||||
String uri = in.readString();
|
||||
return new UsbAccessory(manufacturer, model, description, version, uri);
|
||||
String serial = in.readString();
|
||||
return new UsbAccessory(manufacturer, model, description, version, uri, serial);
|
||||
}
|
||||
|
||||
public UsbAccessory[] newArray(int size) {
|
||||
@@ -168,5 +186,6 @@ public class UsbAccessory implements Parcelable {
|
||||
parcel.writeString(mDescription);
|
||||
parcel.writeString(mVersion);
|
||||
parcel.writeString(mUri);
|
||||
parcel.writeString(mSerial);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,13 +19,14 @@ package com.android.future.usb;
|
||||
/**
|
||||
* A class representing a USB accessory.
|
||||
*/
|
||||
public final class UsbAccessory {
|
||||
public class UsbAccessory {
|
||||
|
||||
private final String mManufacturer;
|
||||
private final String mModel;
|
||||
private final String mDescription;
|
||||
private final String mVersion;
|
||||
private final String mUri;
|
||||
private final String mSerial;
|
||||
|
||||
/* package */ UsbAccessory(android.hardware.usb.UsbAccessory accessory) {
|
||||
mManufacturer = accessory.getManufacturer();
|
||||
@@ -33,6 +34,7 @@ public final class UsbAccessory {
|
||||
mDescription = accessory.getDescription();
|
||||
mVersion = accessory.getVersion();
|
||||
mUri = accessory.getUri();
|
||||
mSerial = accessory.getSerial();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -82,6 +84,17 @@ public final class UsbAccessory {
|
||||
return mUri;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the unique serial number for the accessory.
|
||||
* This is an optional serial number that can be used to differentiate
|
||||
* between individual accessories of the same model and manufacturer
|
||||
*
|
||||
* @return the unique serial number
|
||||
*/
|
||||
public String getSerial() {
|
||||
return mSerial;
|
||||
}
|
||||
|
||||
private static boolean compare(String s1, String s2) {
|
||||
if (s1 == null) return (s2 == null);
|
||||
return s1.equals(s2);
|
||||
@@ -95,7 +108,8 @@ public final class UsbAccessory {
|
||||
compare(mModel, accessory.getModel()) &&
|
||||
compare(mDescription, accessory.getDescription()) &&
|
||||
compare(mVersion, accessory.getVersion()) &&
|
||||
compare(mUri, accessory.getUri()));
|
||||
compare(mUri, accessory.getUri()) &&
|
||||
compare(mSerial, accessory.getSerial()));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@@ -106,7 +120,8 @@ public final class UsbAccessory {
|
||||
(mModel == null ? 0 : mModel.hashCode()) ^
|
||||
(mDescription == null ? 0 : mDescription.hashCode()) ^
|
||||
(mVersion == null ? 0 : mVersion.hashCode()) ^
|
||||
(mUri == null ? 0 : mUri.hashCode()));
|
||||
(mUri == null ? 0 : mUri.hashCode()) ^
|
||||
(mSerial == null ? 0 : mSerial.hashCode()));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -115,6 +130,7 @@ public final class UsbAccessory {
|
||||
", mModel=" + mModel +
|
||||
", mDescription=" + mDescription +
|
||||
", mVersion=" + mVersion +
|
||||
", mUri=" + mUri + "]";
|
||||
", mUri=" + mUri +
|
||||
", mSerial=" + mSerial + "]";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,7 +130,8 @@ public class UsbManager {
|
||||
try {
|
||||
return mService.openAccessory(new android.hardware.usb.UsbAccessory(
|
||||
accessory.getManufacturer(),accessory.getModel(),
|
||||
accessory.getDescription(), accessory.getVersion(), accessory.getUri()));
|
||||
accessory.getDescription(), accessory.getVersion(),
|
||||
accessory.getUri(), accessory.getSerial()));
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, "RemoteException in openAccessory" , e);
|
||||
return null;
|
||||
@@ -150,7 +151,8 @@ public class UsbManager {
|
||||
try {
|
||||
return mService.hasAccessoryPermission(new android.hardware.usb.UsbAccessory(
|
||||
accessory.getManufacturer(),accessory.getModel(),
|
||||
accessory.getDescription(), accessory.getVersion(), accessory.getUri()));
|
||||
accessory.getDescription(), accessory.getVersion(),
|
||||
accessory.getUri(), accessory.getSerial()));
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, "RemoteException in hasPermission", e);
|
||||
return false;
|
||||
@@ -174,7 +176,8 @@ public class UsbManager {
|
||||
try {
|
||||
mService.requestAccessoryPermission(new android.hardware.usb.UsbAccessory(
|
||||
accessory.getManufacturer(),accessory.getModel(),
|
||||
accessory.getDescription(), accessory.getVersion(), accessory.getUri()),
|
||||
accessory.getDescription(), accessory.getVersion(),
|
||||
accessory.getUri(), accessory.getSerial()),
|
||||
mContext.getPackageName(), pi);
|
||||
} catch (RemoteException e) {
|
||||
Log.e(TAG, "RemoteException in requestPermission", e);
|
||||
|
||||
@@ -21,7 +21,7 @@ LOCAL_MODULE_TAGS := tests
|
||||
|
||||
LOCAL_SRC_FILES := $(call all-subdir-java-files)
|
||||
|
||||
LOCAL_PACKAGE_NAME := AccessoryChatGB
|
||||
LOCAL_PACKAGE_NAME := AccessoryChat
|
||||
|
||||
LOCAL_JAVA_LIBRARIES := com.android.future.usb.accessory
|
||||
|
||||
|
||||
@@ -20,7 +20,7 @@
|
||||
<application>
|
||||
<uses-library android:name="com.android.future.usb.accessory" />
|
||||
|
||||
<activity android:name="AccessoryChat" android:label="Accessory Chat GB">
|
||||
<activity android:name="AccessoryChat" android:label="Accessory Chat">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
<category android:name="android.intent.category.DEFAULT" />
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <pthread.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <usbhost/usbhost.h>
|
||||
#include <linux/usb/f_accessory.h>
|
||||
@@ -65,9 +66,20 @@ static void* write_thread(void* arg) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void milli_sleep(int millis) {
|
||||
struct timespec tm;
|
||||
|
||||
tm.tv_sec = 0;
|
||||
tm.tv_nsec = millis * 1000000;
|
||||
nanosleep(&tm, NULL);
|
||||
}
|
||||
|
||||
static void send_string(struct usb_device *device, int index, const char* string) {
|
||||
int ret = usb_device_control_transfer(device, USB_DIR_OUT | USB_TYPE_VENDOR,
|
||||
ACCESSORY_SEND_STRING, 0, index, (void *)string, strlen(string) + 1, 0);
|
||||
|
||||
// some devices can't handle back-to-back requests, so delay a bit
|
||||
milli_sleep(10);
|
||||
}
|
||||
|
||||
static int usb_device_added(const char *devname, void* client_data) {
|
||||
@@ -146,6 +158,7 @@ static int usb_device_added(const char *devname, void* client_data) {
|
||||
send_string(device, ACCESSORY_STRING_DESCRIPTION, "Sample Program");
|
||||
send_string(device, ACCESSORY_STRING_VERSION, "1.0");
|
||||
send_string(device, ACCESSORY_STRING_URI, "http://www.android.com");
|
||||
send_string(device, ACCESSORY_STRING_SERIAL, "1234567890");
|
||||
|
||||
ret = usb_device_control_transfer(device, USB_DIR_OUT | USB_TYPE_VENDOR,
|
||||
ACCESSORY_START, 0, 0, 0, 0, 0);
|
||||
|
||||
@@ -135,7 +135,8 @@ public class AccessoryChat extends Activity implements Runnable, TextView.OnEdit
|
||||
}
|
||||
|
||||
private void openAccessory(UsbAccessory accessory) {
|
||||
mFileDescriptor = mUsbManager.openAccessory(accessory);
|
||||
Log.d(TAG, "openAccessory: " + accessory);
|
||||
mFileDescriptor = mUsbManager.openAccessory(accessory);
|
||||
if (mFileDescriptor != null) {
|
||||
FileDescriptor fd = mFileDescriptor.getFileDescriptor();
|
||||
mInputStream = new FileInputStream(fd);
|
||||
|
||||
@@ -193,13 +193,14 @@ static jobjectArray android_server_UsbService_getAccessoryStrings(JNIEnv *env, j
|
||||
return NULL;
|
||||
}
|
||||
jclass stringClass = env->FindClass("java/lang/String");
|
||||
jobjectArray strArray = env->NewObjectArray(5, stringClass, NULL);
|
||||
jobjectArray strArray = env->NewObjectArray(6, stringClass, NULL);
|
||||
if (!strArray) goto out;
|
||||
set_accessory_string(env, fd, ACCESSORY_GET_STRING_MANUFACTURER, strArray, 0);
|
||||
set_accessory_string(env, fd, ACCESSORY_GET_STRING_MODEL, strArray, 1);
|
||||
set_accessory_string(env, fd, ACCESSORY_GET_STRING_DESCRIPTION, strArray, 2);
|
||||
set_accessory_string(env, fd, ACCESSORY_GET_STRING_VERSION, strArray, 3);
|
||||
set_accessory_string(env, fd, ACCESSORY_GET_STRING_URI, strArray, 4);
|
||||
set_accessory_string(env, fd, ACCESSORY_GET_STRING_SERIAL, strArray, 5);
|
||||
|
||||
out:
|
||||
close(fd);
|
||||
|
||||
Reference in New Issue
Block a user