diff options
Diffstat (limited to 'core')
-rw-r--r-- | core/java/android/hardware/ISensorService.aidl | 29 | ||||
-rw-r--r-- | core/java/android/hardware/SensorManager.java | 203 | ||||
-rw-r--r-- | core/jni/Android.mk | 1 | ||||
-rw-r--r-- | core/jni/android_hardware_SensorManager.cpp | 152 |
4 files changed, 140 insertions, 245 deletions
diff --git a/core/java/android/hardware/ISensorService.aidl b/core/java/android/hardware/ISensorService.aidl deleted file mode 100644 index 67180bd..0000000 --- a/core/java/android/hardware/ISensorService.aidl +++ /dev/null @@ -1,29 +0,0 @@ -/* //device/java/android/android/hardware/ISensorService.aidl -** -** Copyright 2008, 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; - -/** - * {@hide} - */ -interface ISensorService -{ - Bundle getDataChannel(); - boolean enableSensor(IBinder listener, String name, int sensor, int enable); -} diff --git a/core/java/android/hardware/SensorManager.java b/core/java/android/hardware/SensorManager.java index 492f8cc..e6750e6 100644 --- a/core/java/android/hardware/SensorManager.java +++ b/core/java/android/hardware/SensorManager.java @@ -16,12 +16,7 @@ package android.hardware; -import android.content.Context; -import android.os.Binder; -import android.os.Bundle; import android.os.Looper; -import android.os.Parcelable; -import android.os.ParcelFileDescriptor; import android.os.Process; import android.os.RemoteException; import android.os.Handler; @@ -33,8 +28,6 @@ import android.view.IRotationWatcher; import android.view.IWindowManager; import android.view.Surface; -import java.io.FileDescriptor; -import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; @@ -339,7 +332,6 @@ public class SensorManager /*-----------------------------------------------------------------------*/ - private ISensorService mSensorService; Looper mMainLooper; @SuppressWarnings("deprecation") private HashMap<SensorListener, LegacyListener> mLegacyListenersMap = @@ -356,6 +348,7 @@ public class SensorManager /* The thread and the sensor list are global to the process * but the actual thread is spawned on demand */ private static SensorThread sSensorThread; + private static int sQueue; // Used within this module from outside SensorManager, don't make private static SparseArray<Sensor> sHandleToSensor = new SparseArray<Sensor>(); @@ -370,80 +363,41 @@ public class SensorManager boolean mSensorsReady; SensorThread() { - // this gets to the sensor module. We can have only one per process. - sensors_data_init(); } @Override protected void finalize() { - sensors_data_uninit(); } // must be called with sListeners lock - boolean startLocked(ISensorService service) { + boolean startLocked() { try { if (mThread == null) { - Bundle dataChannel = service.getDataChannel(); - if (dataChannel != null) { - mSensorsReady = false; - SensorThreadRunnable runnable = new SensorThreadRunnable(dataChannel); - Thread thread = new Thread(runnable, SensorThread.class.getName()); - thread.start(); - synchronized (runnable) { - while (mSensorsReady == false) { - runnable.wait(); - } + mSensorsReady = false; + SensorThreadRunnable runnable = new SensorThreadRunnable(); + Thread thread = new Thread(runnable, SensorThread.class.getName()); + thread.start(); + synchronized (runnable) { + while (mSensorsReady == false) { + runnable.wait(); } - mThread = thread; } + mThread = thread; } - } catch (RemoteException e) { - Log.e(TAG, "RemoteException in startLocked: ", e); } catch (InterruptedException e) { } return mThread == null ? false : true; } private class SensorThreadRunnable implements Runnable { - private Bundle mDataChannel; - SensorThreadRunnable(Bundle dataChannel) { - mDataChannel = dataChannel; + SensorThreadRunnable() { } private boolean open() { // NOTE: this cannot synchronize on sListeners, since // it's held in the main thread at least until we // return from here. - - // this thread is guaranteed to be unique - Parcelable[] pfds = mDataChannel.getParcelableArray("fds"); - FileDescriptor[] fds; - if (pfds != null) { - int length = pfds.length; - fds = new FileDescriptor[length]; - for (int i = 0; i < length; i++) { - ParcelFileDescriptor pfd = (ParcelFileDescriptor)pfds[i]; - fds[i] = pfd.getFileDescriptor(); - } - } else { - fds = null; - } - int[] ints = mDataChannel.getIntArray("ints"); - sensors_data_open(fds, ints); - if (pfds != null) { - try { - // close our copies of the file descriptors, - // since we are just passing these to the JNI code and not using them here. - for (int i = pfds.length - 1; i >= 0; i--) { - ParcelFileDescriptor pfd = (ParcelFileDescriptor)pfds[i]; - pfd.close(); - } - } catch (IOException e) { - // *shrug* - Log.e(TAG, "IOException: ", e); - } - } - mDataChannel = null; + sQueue = sensors_create_queue(); return true; } @@ -466,7 +420,7 @@ public class SensorManager while (true) { // wait for an event - final int sensor = sensors_data_poll(values, status, timestamp); + final int sensor = sensors_data_poll(sQueue, values, status, timestamp); int accuracy = status[0]; synchronized (sListeners) { @@ -478,7 +432,8 @@ public class SensorManager } // we have no more listeners or polling failed, terminate the thread - sensors_data_close(); + sensors_destroy_queue(sQueue); + sQueue = 0; mThread = null; break; } @@ -506,7 +461,7 @@ public class SensorManager /*-----------------------------------------------------------------------*/ - private class ListenerDelegate extends Binder { + private class ListenerDelegate { final SensorEventListener mSensorEventListener; private final ArrayList<Sensor> mSensorList = new ArrayList<Sensor>(); private final Handler mHandler; @@ -602,8 +557,6 @@ public class SensorManager * {@hide} */ public SensorManager(Looper mainLooper) { - mSensorService = ISensorService.Stub.asInterface( - ServiceManager.getService(Context.SENSOR_SERVICE)); mMainLooper = mainLooper; @@ -1051,42 +1004,37 @@ public class SensorManager return false; } - try { - synchronized (sListeners) { - ListenerDelegate l = null; - for (ListenerDelegate i : sListeners) { - if (i.getListener() == listener) { - l = i; - break; - } + synchronized (sListeners) { + ListenerDelegate l = null; + for (ListenerDelegate i : sListeners) { + if (i.getListener() == listener) { + l = i; + break; } + } - String name = sensor.getName(); - int handle = sensor.getHandle(); - if (l == null) { - result = false; - l = new ListenerDelegate(listener, sensor, handler); - sListeners.add(l); - if (!sListeners.isEmpty()) { - result = sSensorThread.startLocked(mSensorService); - if (result) { - result = mSensorService.enableSensor(l, name, handle, delay); - if (!result) { - // there was an error, remove the listeners - sListeners.remove(l); - } - } - } - } else { - result = mSensorService.enableSensor(l, name, handle, delay); + String name = sensor.getName(); + int handle = sensor.getHandle(); + if (l == null) { + result = false; + l = new ListenerDelegate(listener, sensor, handler); + sListeners.add(l); + if (!sListeners.isEmpty()) { + result = sSensorThread.startLocked(); if (result) { - l.addSensor(sensor); + result = sensors_enable_sensor(sQueue, name, handle, delay); + if (!result) { + // there was an error, remove the listeners + sListeners.remove(l); + } } } + } else { + result = sensors_enable_sensor(sQueue, name, handle, delay); + if (result) { + l.addSensor(sensor); + } } - } catch (RemoteException e) { - Log.e(TAG, "RemoteException in registerListener: ", e); - result = false; } return result; } @@ -1095,27 +1043,23 @@ public class SensorManager if (listener == null || sensor == null) { return; } - try { - synchronized (sListeners) { - final int size = sListeners.size(); - for (int i=0 ; i<size ; i++) { - ListenerDelegate l = sListeners.get(i); - if (l.getListener() == listener) { - // disable these sensors - String name = sensor.getName(); - int handle = sensor.getHandle(); - mSensorService.enableSensor(l, name, handle, SENSOR_DISABLE); - // if we have no more sensors enabled on this listener, - // take it off the list. - if (l.removeSensor(sensor) == 0) { - sListeners.remove(i); - } - break; + synchronized (sListeners) { + final int size = sListeners.size(); + for (int i=0 ; i<size ; i++) { + ListenerDelegate l = sListeners.get(i); + if (l.getListener() == listener) { + // disable these sensors + String name = sensor.getName(); + int handle = sensor.getHandle(); + sensors_enable_sensor(sQueue, name, handle, SENSOR_DISABLE); + // if we have no more sensors enabled on this listener, + // take it off the list. + if (l.removeSensor(sensor) == 0) { + sListeners.remove(i); } + break; } } - } catch (RemoteException e) { - Log.e(TAG, "RemoteException in unregisterListener: ", e); } } @@ -1123,25 +1067,21 @@ public class SensorManager if (listener == null) { return; } - try { - synchronized (sListeners) { - final int size = sListeners.size(); - for (int i=0 ; i<size ; i++) { - ListenerDelegate l = sListeners.get(i); - if (l.getListener() == listener) { - // disable all sensors for this listener - for (Sensor sensor : l.getSensors()) { - String name = sensor.getName(); - int handle = sensor.getHandle(); - mSensorService.enableSensor(l, name, handle, SENSOR_DISABLE); - } - sListeners.remove(i); - break; + synchronized (sListeners) { + final int size = sListeners.size(); + for (int i=0 ; i<size ; i++) { + ListenerDelegate l = sListeners.get(i); + if (l.getListener() == listener) { + // disable all sensors for this listener + for (Sensor sensor : l.getSensors()) { + String name = sensor.getName(); + int handle = sensor.getHandle(); + sensors_enable_sensor(sQueue, name, handle, SENSOR_DISABLE); } + sListeners.remove(i); + break; } } - } catch (RemoteException e) { - Log.e(TAG, "RemoteException in unregisterListener: ", e); } } @@ -1794,9 +1734,8 @@ public class SensorManager private static native int sensors_module_get_next_sensor(Sensor sensor, int next); // Used within this module from outside SensorManager, don't make private - static native int sensors_data_init(); - static native int sensors_data_uninit(); - static native int sensors_data_open(FileDescriptor[] fds, int[] ints); - static native int sensors_data_close(); - static native int sensors_data_poll(float[] values, int[] status, long[] timestamp); + static native int sensors_create_queue(); + static native void sensors_destroy_queue(int queue); + static native boolean sensors_enable_sensor(int queue, String name, int sensor, int enable); + static native int sensors_data_poll(int queue, float[] values, int[] status, long[] timestamp); } diff --git a/core/jni/Android.mk b/core/jni/Android.mk index 89fea41..5c37c7c 100644 --- a/core/jni/Android.mk +++ b/core/jni/Android.mk @@ -168,6 +168,7 @@ LOCAL_SHARED_LIBRARIES := \ libbinder \ libnetutils \ libui \ + libgui \ libsurfaceflinger_client \ libcamera_client \ libskiagl \ diff --git a/core/jni/android_hardware_SensorManager.cpp b/core/jni/android_hardware_SensorManager.cpp index 9a90b72..232c8af 100644 --- a/core/jni/android_hardware_SensorManager.cpp +++ b/core/jni/android_hardware_SensorManager.cpp @@ -18,8 +18,9 @@ #include "utils/Log.h" -#include <hardware/sensors.h> -#include <cutils/native_handle.h> +#include <gui/Sensor.h> +#include <gui/SensorManager.h> +#include <gui/SensorEventQueue.h> #include "jni.h" #include "JNIHelp.h" @@ -43,44 +44,36 @@ struct SensorOffsets * The method below are not thread-safe and not intended to be */ -static sensors_module_t* sSensorModule = 0; -static sensors_data_device_t* sSensorDevice = 0; static jint sensors_module_init(JNIEnv *env, jclass clazz) { - int err = 0; - sensors_module_t const* module; - err = hw_get_module(SENSORS_HARDWARE_MODULE_ID, (const hw_module_t **)&module); - if (err == 0) - sSensorModule = (sensors_module_t*)module; - return err; + SensorManager::getInstance(); + return 0; } static jint sensors_module_get_next_sensor(JNIEnv *env, jobject clazz, jobject sensor, jint next) { - if (sSensorModule == NULL) - return 0; + SensorManager& mgr(SensorManager::getInstance()); - SensorOffsets& sensorOffsets = gSensorOffsets; - const struct sensor_t* list; - int count = sSensorModule->get_sensors_list(sSensorModule, &list); + Sensor const* const* sensorList; + size_t count = mgr.getSensorList(&sensorList); if (next >= count) return -1; - list += next; - - jstring name = env->NewStringUTF(list->name); - jstring vendor = env->NewStringUTF(list->vendor); + Sensor const* const list = sensorList[next]; + const SensorOffsets& sensorOffsets(gSensorOffsets); + jstring name = env->NewStringUTF(list->getName().string()); + jstring vendor = env->NewStringUTF(list->getVendor().string()); env->SetObjectField(sensor, sensorOffsets.name, name); env->SetObjectField(sensor, sensorOffsets.vendor, vendor); - env->SetIntField(sensor, sensorOffsets.version, list->version); - env->SetIntField(sensor, sensorOffsets.handle, list->handle); - env->SetIntField(sensor, sensorOffsets.type, list->type); - env->SetFloatField(sensor, sensorOffsets.range, list->maxRange); - env->SetFloatField(sensor, sensorOffsets.resolution, list->resolution); - env->SetFloatField(sensor, sensorOffsets.power, list->power); + env->SetIntField(sensor, sensorOffsets.version, 1); + env->SetIntField(sensor, sensorOffsets.handle, list->getHandle()); + env->SetIntField(sensor, sensorOffsets.type, list->getType()); + env->SetFloatField(sensor, sensorOffsets.range, list->getMaxValue()); + env->SetFloatField(sensor, sensorOffsets.resolution, list->getResolution()); + env->SetFloatField(sensor, sensorOffsets.power, list->getPowerUsage()); next++; return next<count ? next : 0; @@ -88,75 +81,64 @@ sensors_module_get_next_sensor(JNIEnv *env, jobject clazz, jobject sensor, jint //---------------------------------------------------------------------------- static jint -sensors_data_init(JNIEnv *env, jclass clazz) +sensors_create_queue(JNIEnv *env, jclass clazz) { - if (sSensorModule == NULL) - return -1; - int err = sensors_data_open(&sSensorModule->common, &sSensorDevice); - return err; + SensorManager& mgr(SensorManager::getInstance()); + sp<SensorEventQueue> queue(mgr.createEventQueue()); + queue->incStrong(clazz); + return reinterpret_cast<int>(queue.get()); } -static jint -sensors_data_uninit(JNIEnv *env, jclass clazz) +static void +sensors_destroy_queue(JNIEnv *env, jclass clazz, jint nativeQueue) { - int err = 0; - if (sSensorDevice) { - err = sensors_data_close(sSensorDevice); - if (err == 0) - sSensorDevice = 0; + sp<SensorEventQueue> queue(reinterpret_cast<SensorEventQueue *>(nativeQueue)); + if (queue != 0) { + queue->decStrong(clazz); } - return err; } -static jint -sensors_data_open(JNIEnv *env, jclass clazz, jobjectArray fdArray, jintArray intArray) +static jboolean +sensors_enable_sensor(JNIEnv *env, jclass clazz, + jint nativeQueue, jstring name, jint sensor, jint enable) { - jclass FileDescriptor = env->FindClass("java/io/FileDescriptor"); - jfieldID fieldOffset = env->GetFieldID(FileDescriptor, "descriptor", "I"); - int numFds = (fdArray ? env->GetArrayLength(fdArray) : 0); - int numInts = (intArray ? env->GetArrayLength(intArray) : 0); - native_handle_t* handle = native_handle_create(numFds, numInts); - int offset = 0; - - for (int i = 0; i < numFds; i++) { - jobject fdo = env->GetObjectArrayElement(fdArray, i); - if (fdo) { - handle->data[offset++] = env->GetIntField(fdo, fieldOffset); - } else { - handle->data[offset++] = -1; - } - } - if (numInts > 0) { - jint* ints = env->GetIntArrayElements(intArray, 0); - for (int i = 0; i < numInts; i++) { - handle->data[offset++] = ints[i]; - } - env->ReleaseIntArrayElements(intArray, ints, 0); + sp<SensorEventQueue> queue(reinterpret_cast<SensorEventQueue *>(nativeQueue)); + if (queue == 0) return JNI_FALSE; + status_t res; + if (enable) { + res = queue->enableSensor(sensor); + } else { + res = queue->disableSensor(sensor); } - - // doesn't take ownership of the native handle - return sSensorDevice->data_open(sSensorDevice, handle); + return res == NO_ERROR ? true : false; } static jint -sensors_data_close(JNIEnv *env, jclass clazz) -{ - return sSensorDevice->data_close(sSensorDevice); -} - -static jint -sensors_data_poll(JNIEnv *env, jclass clazz, +sensors_data_poll(JNIEnv *env, jclass clazz, jint nativeQueue, jfloatArray values, jintArray status, jlongArray timestamp) { - sensors_data_t data; - int res = sSensorDevice->poll(sSensorDevice, &data); - if (res >= 0) { - jint accuracy = data.vector.status; - env->SetFloatArrayRegion(values, 0, 3, data.vector.v); - env->SetIntArrayRegion(status, 0, 1, &accuracy); - env->SetLongArrayRegion(timestamp, 0, 1, &data.time); + sp<SensorEventQueue> queue(reinterpret_cast<SensorEventQueue *>(nativeQueue)); + if (queue == 0) return -1; + + status_t res; + ASensorEvent event; + + res = queue->read(&event, 1); + if (res == -EAGAIN) { + res = queue->waitForEvent(); + if (res != NO_ERROR) + return -1; + res = queue->read(&event, 1); } - return res; + if (res < 0) + return -1; + + jint accuracy = event.vector.status; + env->SetFloatArrayRegion(values, 0, 3, event.vector.v); + env->SetIntArrayRegion(status, 0, 1, &accuracy); + env->SetLongArrayRegion(timestamp, 0, 1, &event.timestamp); + + return event.sensor; } static void @@ -179,11 +161,13 @@ static JNINativeMethod gMethods[] = { {"sensors_module_init","()I", (void*)sensors_module_init }, {"sensors_module_get_next_sensor","(Landroid/hardware/Sensor;I)I", (void*)sensors_module_get_next_sensor }, - {"sensors_data_init", "()I", (void*)sensors_data_init }, - {"sensors_data_uninit", "()I", (void*)sensors_data_uninit }, - {"sensors_data_open", "([Ljava/io/FileDescriptor;[I)I", (void*)sensors_data_open }, - {"sensors_data_close", "()I", (void*)sensors_data_close }, - {"sensors_data_poll", "([F[I[J)I", (void*)sensors_data_poll }, + + {"sensors_create_queue", "()I", (void*)sensors_create_queue }, + {"sensors_destroy_queue", "(I)V", (void*)sensors_destroy_queue }, + {"sensors_enable_sensor", "(ILjava/lang/String;II)Z", + (void*)sensors_enable_sensor }, + + {"sensors_data_poll", "(I[F[I[J)I", (void*)sensors_data_poll }, }; }; // namespace android |