From d07c9d5d920c261ef10efc825ee5225e83ad1c5b Mon Sep 17 00:00:00 2001 From: Mike Lockwood Date: Wed, 29 Dec 2010 08:48:46 -0500 Subject: UsbManager: Add methods to convert between USB device names and IDs Change-Id: I199a47805b629cc7b1714191d6af2fd70c2bda6d Signed-off-by: Mike Lockwood --- core/java/android/hardware/UsbManager.java | 11 ++++++ core/jni/Android.mk | 4 +- core/jni/AndroidRuntime.cpp | 3 +- core/jni/android_hardware_UsbManager.cpp | 61 ++++++++++++++++++++++++++++++ 4 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 core/jni/android_hardware_UsbManager.cpp (limited to 'core') diff --git a/core/java/android/hardware/UsbManager.java b/core/java/android/hardware/UsbManager.java index 1003bf9..6022b12 100644 --- a/core/java/android/hardware/UsbManager.java +++ b/core/java/android/hardware/UsbManager.java @@ -105,6 +105,14 @@ public class UsbManager { */ public static final String USB_FUNCTION_DISABLED = "disabled"; + public static final int getDeviceId(String name) { + return native_get_device_id(name); + } + + public static final String getDeviceName(int id) { + return native_get_device_name(id); + } + private static File getFunctionEnableFile(String function) { return new File("/sys/class/usb_composite/" + function + "/enable"); } @@ -130,4 +138,7 @@ public class UsbManager { return false; } } + + private static native int native_get_device_id(String name); + private static native String native_get_device_name(int id); } diff --git a/core/jni/Android.mk b/core/jni/Android.mk index 8eeed3d..e9566ad 100644 --- a/core/jni/Android.mk +++ b/core/jni/Android.mk @@ -123,6 +123,7 @@ LOCAL_SRC_FILES:= \ android_media_ToneGenerator.cpp \ android_hardware_Camera.cpp \ android_hardware_SensorManager.cpp \ + android_hardware_UsbManager.cpp \ android_debug_JNITest.cpp \ android_util_FileObserver.cpp \ android/opengl/poly_clip.cpp.arm \ @@ -200,7 +201,8 @@ LOCAL_SHARED_LIBRARIES := \ libmedia \ libwpa_client \ libjpeg \ - libnfc_ndef + libnfc_ndef \ + libusbhost ifeq ($(USE_OPENGL_RENDERER),true) LOCAL_SHARED_LIBRARIES += libhwui diff --git a/core/jni/AndroidRuntime.cpp b/core/jni/AndroidRuntime.cpp index 2dfebe5..961bc1f1 100644 --- a/core/jni/AndroidRuntime.cpp +++ b/core/jni/AndroidRuntime.cpp @@ -77,8 +77,8 @@ extern int register_android_opengl_jni_GLES11Ext(JNIEnv* env); extern int register_android_opengl_jni_GLES20(JNIEnv* env); extern int register_android_hardware_Camera(JNIEnv *env); - extern int register_android_hardware_SensorManager(JNIEnv *env); +extern int register_android_hardware_UsbManager(JNIEnv *env); extern int register_android_media_AudioRecord(JNIEnv *env); extern int register_android_media_AudioSystem(JNIEnv *env); @@ -1266,6 +1266,7 @@ static const RegJNIRec gRegJNI[] = { REG_JNI(register_com_android_internal_os_ZygoteInit), REG_JNI(register_android_hardware_Camera), REG_JNI(register_android_hardware_SensorManager), + REG_JNI(register_android_hardware_UsbManager), REG_JNI(register_android_media_AudioRecord), REG_JNI(register_android_media_AudioSystem), REG_JNI(register_android_media_AudioTrack), diff --git a/core/jni/android_hardware_UsbManager.cpp b/core/jni/android_hardware_UsbManager.cpp new file mode 100644 index 0000000..8f32abf --- /dev/null +++ b/core/jni/android_hardware_UsbManager.cpp @@ -0,0 +1,61 @@ +/* + * Copyright (C) 2010 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. + */ + +#include "jni.h" +#include "JNIHelp.h" +#include "android_runtime/AndroidRuntime.h" + +#include + +#include + +using namespace android; + +static jint android_hardware_UsbManager_get_device_id(JNIEnv *env, jobject clazz, jstring name) +{ + const char *nameStr = env->GetStringUTFChars(name, NULL); + int id = usb_device_get_unique_id_from_name(nameStr); + env->ReleaseStringUTFChars(name, nameStr); + return id; +} + +static jstring android_hardware_UsbManager_get_device_name(JNIEnv *env, jobject clazz, jint id) +{ + char* name = usb_device_get_name_from_unique_id(id); + jstring result = env->NewStringUTF(name); + free(name); + return result; +} + +static JNINativeMethod method_table[] = { + { "native_get_device_id", "(Ljava/lang/String;)I", + (void*)android_hardware_UsbManager_get_device_id }, + { "native_get_device_name", "(I)Ljava/lang/String;", + (void*)android_hardware_UsbManager_get_device_name }, +}; + +int register_android_hardware_UsbManager(JNIEnv *env) +{ + jclass clazz = env->FindClass("android/hardware/UsbManager"); + if (clazz == NULL) { + LOGE("Can't find android/hardware/UsbManager"); + return -1; + } + + return AndroidRuntime::registerNativeMethods(env, "android/hardware/UsbManager", + method_table, NELEM(method_table)); +} + -- cgit v1.1