The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2008, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
Mike Lockwood | 270e87f | 2009-05-22 10:02:14 -0400 | [diff] [blame] | 17 | #define LOG_TAG "SensorManager" |
| 18 | |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 19 | #include <utils/Log.h> |
| 20 | #include <utils/Looper.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 22 | #include <gui/Sensor.h> |
| 23 | #include <gui/SensorManager.h> |
| 24 | #include <gui/SensorEventQueue.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | |
| 26 | #include "jni.h" |
| 27 | #include "JNIHelp.h" |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 28 | #include "android_os_MessageQueue.h" |
| 29 | #include <android_runtime/AndroidRuntime.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 31 | static struct { |
| 32 | jclass clazz; |
| 33 | jmethodID dispatchSensorEvent; |
Jaikumar Ganesh | 1aab1db | 2013-03-13 15:00:21 -0700 | [diff] [blame] | 34 | } gSensorEventQueueClassInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | |
| 36 | namespace android { |
| 37 | |
| 38 | struct SensorOffsets |
| 39 | { |
| 40 | jfieldID name; |
| 41 | jfieldID vendor; |
| 42 | jfieldID version; |
| 43 | jfieldID handle; |
| 44 | jfieldID type; |
| 45 | jfieldID range; |
| 46 | jfieldID resolution; |
| 47 | jfieldID power; |
Mathias Agopian | 050b562 | 2010-07-29 16:51:38 -0700 | [diff] [blame] | 48 | jfieldID minDelay; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | } gSensorOffsets; |
| 50 | |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 51 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | /* |
| 53 | * The method below are not thread-safe and not intended to be |
| 54 | */ |
| 55 | |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 56 | static void |
| 57 | nativeClassInit (JNIEnv *_env, jclass _this) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 58 | { |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 59 | jclass sensorClass = _env->FindClass("android/hardware/Sensor"); |
| 60 | SensorOffsets& sensorOffsets = gSensorOffsets; |
| 61 | sensorOffsets.name = _env->GetFieldID(sensorClass, "mName", "Ljava/lang/String;"); |
| 62 | sensorOffsets.vendor = _env->GetFieldID(sensorClass, "mVendor", "Ljava/lang/String;"); |
| 63 | sensorOffsets.version = _env->GetFieldID(sensorClass, "mVersion", "I"); |
| 64 | sensorOffsets.handle = _env->GetFieldID(sensorClass, "mHandle", "I"); |
| 65 | sensorOffsets.type = _env->GetFieldID(sensorClass, "mType", "I"); |
| 66 | sensorOffsets.range = _env->GetFieldID(sensorClass, "mMaxRange", "F"); |
| 67 | sensorOffsets.resolution = _env->GetFieldID(sensorClass, "mResolution","F"); |
| 68 | sensorOffsets.power = _env->GetFieldID(sensorClass, "mPower", "F"); |
| 69 | sensorOffsets.minDelay = _env->GetFieldID(sensorClass, "mMinDelay", "I"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static jint |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 73 | nativeGetNextSensor(JNIEnv *env, jclass clazz, jobject sensor, jint next) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | { |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 75 | SensorManager& mgr(SensorManager::getInstance()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 77 | Sensor const* const* sensorList; |
| 78 | size_t count = mgr.getSensorList(&sensorList); |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 79 | if (size_t(next) >= count) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | return -1; |
| 81 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 82 | Sensor const* const list = sensorList[next]; |
| 83 | const SensorOffsets& sensorOffsets(gSensorOffsets); |
| 84 | jstring name = env->NewStringUTF(list->getName().string()); |
| 85 | jstring vendor = env->NewStringUTF(list->getVendor().string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | env->SetObjectField(sensor, sensorOffsets.name, name); |
| 87 | env->SetObjectField(sensor, sensorOffsets.vendor, vendor); |
Mathias Agopian | 277d1b2 | 2012-05-04 15:51:29 -0700 | [diff] [blame] | 88 | env->SetIntField(sensor, sensorOffsets.version, list->getVersion()); |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 89 | env->SetIntField(sensor, sensorOffsets.handle, list->getHandle()); |
| 90 | env->SetIntField(sensor, sensorOffsets.type, list->getType()); |
| 91 | env->SetFloatField(sensor, sensorOffsets.range, list->getMaxValue()); |
| 92 | env->SetFloatField(sensor, sensorOffsets.resolution, list->getResolution()); |
| 93 | env->SetFloatField(sensor, sensorOffsets.power, list->getPowerUsage()); |
Mathias Agopian | 050b562 | 2010-07-29 16:51:38 -0700 | [diff] [blame] | 94 | env->SetIntField(sensor, sensorOffsets.minDelay, list->getMinDelay()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | |
| 96 | next++; |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 97 | return size_t(next) < count ? next : 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | //---------------------------------------------------------------------------- |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 101 | |
| 102 | class Receiver : public LooperCallback { |
| 103 | sp<SensorEventQueue> mSensorQueue; |
| 104 | sp<MessageQueue> mMessageQueue; |
| 105 | jobject mReceiverObject; |
| 106 | jfloatArray mScratch; |
| 107 | public: |
| 108 | Receiver(const sp<SensorEventQueue>& sensorQueue, |
| 109 | const sp<MessageQueue>& messageQueue, |
| 110 | jobject receiverObject, jfloatArray scratch) { |
| 111 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 112 | mSensorQueue = sensorQueue; |
| 113 | mMessageQueue = messageQueue; |
| 114 | mReceiverObject = env->NewGlobalRef(receiverObject); |
| 115 | mScratch = (jfloatArray)env->NewGlobalRef(scratch); |
| 116 | } |
| 117 | ~Receiver() { |
| 118 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 119 | env->DeleteGlobalRef(mReceiverObject); |
| 120 | env->DeleteGlobalRef(mScratch); |
| 121 | } |
| 122 | sp<SensorEventQueue> getSensorEventQueue() const { |
| 123 | return mSensorQueue; |
| 124 | } |
| 125 | |
| 126 | void destroy() { |
| 127 | mMessageQueue->getLooper()->removeFd( mSensorQueue->getFd() ); |
| 128 | } |
| 129 | |
| 130 | private: |
| 131 | virtual void onFirstRef() { |
| 132 | LooperCallback::onFirstRef(); |
| 133 | mMessageQueue->getLooper()->addFd(mSensorQueue->getFd(), 0, |
| 134 | ALOOPER_EVENT_INPUT, this, mSensorQueue.get()); |
| 135 | } |
| 136 | |
| 137 | virtual int handleEvent(int fd, int events, void* data) { |
| 138 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 139 | sp<SensorEventQueue> q = reinterpret_cast<SensorEventQueue *>(data); |
| 140 | ssize_t n; |
| 141 | ASensorEvent buffer[16]; |
| 142 | while ((n = q->read(buffer, 16)) > 0) { |
| 143 | for (int i=0 ; i<n ; i++) { |
| 144 | |
| 145 | env->SetFloatArrayRegion(mScratch, 0, 16, buffer[i].data); |
| 146 | |
| 147 | env->CallVoidMethod(mReceiverObject, |
Jaikumar Ganesh | 1aab1db | 2013-03-13 15:00:21 -0700 | [diff] [blame] | 148 | gSensorEventQueueClassInfo.dispatchSensorEvent, |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 149 | buffer[i].sensor, |
| 150 | mScratch, |
| 151 | buffer[i].vector.status, |
| 152 | buffer[i].timestamp); |
| 153 | |
| 154 | if (env->ExceptionCheck()) { |
| 155 | ALOGE("Exception dispatching input event."); |
| 156 | return 1; |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | if (n<0 && n != -EAGAIN) { |
| 161 | // FIXME: error receiving events, what to do in this case? |
| 162 | } |
| 163 | |
| 164 | return 1; |
| 165 | } |
| 166 | }; |
| 167 | |
| 168 | static jint nativeInitSensorEventQueue(JNIEnv *env, jclass clazz, jobject eventQ, jobject msgQ, jfloatArray scratch) { |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 169 | SensorManager& mgr(SensorManager::getInstance()); |
| 170 | sp<SensorEventQueue> queue(mgr.createEventQueue()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 172 | sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, msgQ); |
| 173 | if (messageQueue == NULL) { |
| 174 | jniThrowRuntimeException(env, "MessageQueue is not initialized."); |
| 175 | return 0; |
Mathias Agopian | 0b6d77b | 2012-05-07 18:27:06 -0700 | [diff] [blame] | 176 | } |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 177 | |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 178 | sp<Receiver> receiver = new Receiver(queue, messageQueue, eventQ, scratch); |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 179 | receiver->incStrong((void*)nativeInitSensorEventQueue); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 180 | return jint(receiver.get()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | } |
| 182 | |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 183 | static jint nativeEnableSensor(JNIEnv *env, jclass clazz, jint eventQ, jint handle, jint us) { |
| 184 | sp<Receiver> receiver(reinterpret_cast<Receiver *>(eventQ)); |
| 185 | return receiver->getSensorEventQueue()->enableSensor(handle, us); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | } |
| 187 | |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 188 | static jint nativeDisableSensor(JNIEnv *env, jclass clazz, jint eventQ, jint handle) { |
| 189 | sp<Receiver> receiver(reinterpret_cast<Receiver *>(eventQ)); |
| 190 | return receiver->getSensorEventQueue()->disableSensor(handle); |
| 191 | } |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 192 | |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 193 | static void nativeDestroySensorEventQueue(JNIEnv *env, jclass clazz, jint eventQ, jint handle) { |
| 194 | sp<Receiver> receiver(reinterpret_cast<Receiver *>(eventQ)); |
| 195 | receiver->destroy(); |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 196 | receiver->decStrong((void*)nativeInitSensorEventQueue); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 197 | } |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 198 | |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 199 | |
| 200 | //---------------------------------------------------------------------------- |
| 201 | |
| 202 | static JNINativeMethod gSystemSensorManagerMethods[] = { |
| 203 | {"nativeClassInit", |
| 204 | "()V", |
| 205 | (void*)nativeClassInit }, |
| 206 | |
| 207 | {"nativeGetNextSensor", |
| 208 | "(Landroid/hardware/Sensor;I)I", |
| 209 | (void*)nativeGetNextSensor }, |
| 210 | }; |
| 211 | |
Jaikumar Ganesh | 1aab1db | 2013-03-13 15:00:21 -0700 | [diff] [blame] | 212 | static JNINativeMethod gSensorEventQueueMethods[] = { |
| 213 | {"nativeInitSensorEventQueue", |
| 214 | "(Landroid/hardware/SystemSensorManager$SensorEventQueue;Landroid/os/MessageQueue;[F)I", |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 215 | (void*)nativeInitSensorEventQueue }, |
| 216 | |
| 217 | {"nativeEnableSensor", |
| 218 | "(III)I", |
| 219 | (void*)nativeEnableSensor }, |
| 220 | |
| 221 | {"nativeDisableSensor", |
| 222 | "(II)I", |
| 223 | (void*)nativeDisableSensor }, |
| 224 | |
| 225 | {"nativeDestroySensorEventQueue", |
| 226 | "(I)V", |
| 227 | (void*)nativeDestroySensorEventQueue }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | }; |
| 229 | |
| 230 | }; // namespace android |
| 231 | |
| 232 | using namespace android; |
| 233 | |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 234 | #define FIND_CLASS(var, className) \ |
| 235 | var = env->FindClass(className); \ |
| 236 | LOG_FATAL_IF(! var, "Unable to find class " className); \ |
| 237 | var = jclass(env->NewGlobalRef(var)); |
| 238 | |
| 239 | #define GET_METHOD_ID(var, clazz, methodName, methodDescriptor) \ |
| 240 | var = env->GetMethodID(clazz, methodName, methodDescriptor); \ |
| 241 | LOG_FATAL_IF(! var, "Unable to find method " methodName); |
| 242 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 243 | int register_android_hardware_SensorManager(JNIEnv *env) |
| 244 | { |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 245 | jniRegisterNativeMethods(env, "android/hardware/SystemSensorManager", |
| 246 | gSystemSensorManagerMethods, NELEM(gSystemSensorManagerMethods)); |
| 247 | |
Jaikumar Ganesh | 1aab1db | 2013-03-13 15:00:21 -0700 | [diff] [blame] | 248 | jniRegisterNativeMethods(env, "android/hardware/SystemSensorManager$SensorEventQueue", |
| 249 | gSensorEventQueueMethods, NELEM(gSensorEventQueueMethods)); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 250 | |
Jaikumar Ganesh | 1aab1db | 2013-03-13 15:00:21 -0700 | [diff] [blame] | 251 | FIND_CLASS(gSensorEventQueueClassInfo.clazz, "android/hardware/SystemSensorManager$SensorEventQueue"); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 252 | |
Jaikumar Ganesh | 1aab1db | 2013-03-13 15:00:21 -0700 | [diff] [blame] | 253 | GET_METHOD_ID(gSensorEventQueueClassInfo.dispatchSensorEvent, |
| 254 | gSensorEventQueueClassInfo.clazz, |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 255 | "dispatchSensorEvent", "(I[FIJ)V"); |
| 256 | |
| 257 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | } |