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 | |
Mike Lockwood | 270e87f | 2009-05-22 10:02:14 -0400 | [diff] [blame] | 19 | #include "utils/Log.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 21 | #include <gui/Sensor.h> |
| 22 | #include <gui/SensorManager.h> |
| 23 | #include <gui/SensorEventQueue.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | |
| 25 | #include "jni.h" |
| 26 | #include "JNIHelp.h" |
| 27 | |
| 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | struct SensorOffsets |
| 32 | { |
| 33 | jfieldID name; |
| 34 | jfieldID vendor; |
| 35 | jfieldID version; |
| 36 | jfieldID handle; |
| 37 | jfieldID type; |
| 38 | jfieldID range; |
| 39 | jfieldID resolution; |
| 40 | jfieldID power; |
Mathias Agopian | 050b562 | 2010-07-29 16:51:38 -0700 | [diff] [blame] | 41 | jfieldID minDelay; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | } gSensorOffsets; |
| 43 | |
| 44 | /* |
| 45 | * The method below are not thread-safe and not intended to be |
| 46 | */ |
| 47 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | |
| 49 | static jint |
| 50 | sensors_module_init(JNIEnv *env, jclass clazz) |
| 51 | { |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 52 | SensorManager::getInstance(); |
| 53 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static jint |
| 57 | sensors_module_get_next_sensor(JNIEnv *env, jobject clazz, jobject sensor, jint next) |
| 58 | { |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 59 | SensorManager& mgr(SensorManager::getInstance()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 60 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 61 | Sensor const* const* sensorList; |
| 62 | size_t count = mgr.getSensorList(&sensorList); |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 63 | if (size_t(next) >= count) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | return -1; |
| 65 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 66 | Sensor const* const list = sensorList[next]; |
| 67 | const SensorOffsets& sensorOffsets(gSensorOffsets); |
| 68 | jstring name = env->NewStringUTF(list->getName().string()); |
| 69 | jstring vendor = env->NewStringUTF(list->getVendor().string()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | env->SetObjectField(sensor, sensorOffsets.name, name); |
| 71 | env->SetObjectField(sensor, sensorOffsets.vendor, vendor); |
Mathias Agopian | 277d1b2 | 2012-05-04 15:51:29 -0700 | [diff] [blame] | 72 | env->SetIntField(sensor, sensorOffsets.version, list->getVersion()); |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 73 | env->SetIntField(sensor, sensorOffsets.handle, list->getHandle()); |
| 74 | env->SetIntField(sensor, sensorOffsets.type, list->getType()); |
| 75 | env->SetFloatField(sensor, sensorOffsets.range, list->getMaxValue()); |
| 76 | env->SetFloatField(sensor, sensorOffsets.resolution, list->getResolution()); |
| 77 | env->SetFloatField(sensor, sensorOffsets.power, list->getPowerUsage()); |
Mathias Agopian | 050b562 | 2010-07-29 16:51:38 -0700 | [diff] [blame] | 78 | env->SetIntField(sensor, sensorOffsets.minDelay, list->getMinDelay()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | |
| 80 | next++; |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 81 | return size_t(next) < count ? next : 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | //---------------------------------------------------------------------------- |
| 85 | static jint |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 86 | sensors_create_queue(JNIEnv *env, jclass clazz) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | { |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 88 | SensorManager& mgr(SensorManager::getInstance()); |
| 89 | sp<SensorEventQueue> queue(mgr.createEventQueue()); |
| 90 | queue->incStrong(clazz); |
| 91 | return reinterpret_cast<int>(queue.get()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 94 | static void |
| 95 | sensors_destroy_queue(JNIEnv *env, jclass clazz, jint nativeQueue) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | { |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 97 | sp<SensorEventQueue> queue(reinterpret_cast<SensorEventQueue *>(nativeQueue)); |
| 98 | if (queue != 0) { |
| 99 | queue->decStrong(clazz); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | } |
| 102 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 103 | static jboolean |
| 104 | sensors_enable_sensor(JNIEnv *env, jclass clazz, |
Mathias Agopian | 23e8de2 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 105 | jint nativeQueue, jstring name, jint sensor, jint delay) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 106 | { |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 107 | sp<SensorEventQueue> queue(reinterpret_cast<SensorEventQueue *>(nativeQueue)); |
| 108 | if (queue == 0) return JNI_FALSE; |
| 109 | status_t res; |
Mathias Agopian | 23e8de2 | 2010-07-21 15:59:50 -0700 | [diff] [blame] | 110 | if (delay >= 0) { |
| 111 | res = queue->enableSensor(sensor, delay); |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 112 | } else { |
| 113 | res = queue->disableSensor(sensor); |
Mike Lockwood | 270e87f | 2009-05-22 10:02:14 -0400 | [diff] [blame] | 114 | } |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 115 | return res == NO_ERROR ? true : false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static jint |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 119 | sensors_data_poll(JNIEnv *env, jclass clazz, jint nativeQueue, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | jfloatArray values, jintArray status, jlongArray timestamp) |
| 121 | { |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 122 | sp<SensorEventQueue> queue(reinterpret_cast<SensorEventQueue *>(nativeQueue)); |
| 123 | if (queue == 0) return -1; |
| 124 | |
| 125 | status_t res; |
| 126 | ASensorEvent event; |
| 127 | |
| 128 | res = queue->read(&event, 1); |
Mathias Agopian | bb9ba8b | 2011-11-29 11:42:32 -0800 | [diff] [blame] | 129 | if (res == 0) { |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 130 | res = queue->waitForEvent(); |
| 131 | if (res != NO_ERROR) |
| 132 | return -1; |
Mathias Agopian | 0b6d77b | 2012-05-07 18:27:06 -0700 | [diff] [blame] | 133 | // here we're guaranteed to have an event |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 134 | res = queue->read(&event, 1); |
Mathias Agopian | 0b6d77b | 2012-05-07 18:27:06 -0700 | [diff] [blame] | 135 | ALOGE_IF(res==0, "sensors_data_poll: nothing to read after waitForEvent()"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 136 | } |
Mathias Agopian | 0b6d77b | 2012-05-07 18:27:06 -0700 | [diff] [blame] | 137 | if (res <= 0) { |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 138 | return -1; |
Mathias Agopian | 0b6d77b | 2012-05-07 18:27:06 -0700 | [diff] [blame] | 139 | } |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 140 | |
| 141 | jint accuracy = event.vector.status; |
| 142 | env->SetFloatArrayRegion(values, 0, 3, event.vector.v); |
| 143 | env->SetIntArrayRegion(status, 0, 1, &accuracy); |
| 144 | env->SetLongArrayRegion(timestamp, 0, 1, &event.timestamp); |
| 145 | |
| 146 | return event.sensor; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | static void |
| 150 | nativeClassInit (JNIEnv *_env, jclass _this) |
| 151 | { |
| 152 | jclass sensorClass = _env->FindClass("android/hardware/Sensor"); |
| 153 | SensorOffsets& sensorOffsets = gSensorOffsets; |
| 154 | sensorOffsets.name = _env->GetFieldID(sensorClass, "mName", "Ljava/lang/String;"); |
| 155 | sensorOffsets.vendor = _env->GetFieldID(sensorClass, "mVendor", "Ljava/lang/String;"); |
| 156 | sensorOffsets.version = _env->GetFieldID(sensorClass, "mVersion", "I"); |
| 157 | sensorOffsets.handle = _env->GetFieldID(sensorClass, "mHandle", "I"); |
| 158 | sensorOffsets.type = _env->GetFieldID(sensorClass, "mType", "I"); |
| 159 | sensorOffsets.range = _env->GetFieldID(sensorClass, "mMaxRange", "F"); |
| 160 | sensorOffsets.resolution = _env->GetFieldID(sensorClass, "mResolution","F"); |
| 161 | sensorOffsets.power = _env->GetFieldID(sensorClass, "mPower", "F"); |
Mathias Agopian | 050b562 | 2010-07-29 16:51:38 -0700 | [diff] [blame] | 162 | sensorOffsets.minDelay = _env->GetFieldID(sensorClass, "mMinDelay", "I"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | static JNINativeMethod gMethods[] = { |
| 166 | {"nativeClassInit", "()V", (void*)nativeClassInit }, |
| 167 | {"sensors_module_init","()I", (void*)sensors_module_init }, |
| 168 | {"sensors_module_get_next_sensor","(Landroid/hardware/Sensor;I)I", |
| 169 | (void*)sensors_module_get_next_sensor }, |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 170 | |
| 171 | {"sensors_create_queue", "()I", (void*)sensors_create_queue }, |
| 172 | {"sensors_destroy_queue", "(I)V", (void*)sensors_destroy_queue }, |
| 173 | {"sensors_enable_sensor", "(ILjava/lang/String;II)Z", |
| 174 | (void*)sensors_enable_sensor }, |
| 175 | |
| 176 | {"sensors_data_poll", "(I[F[I[J)I", (void*)sensors_data_poll }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 177 | }; |
| 178 | |
| 179 | }; // namespace android |
| 180 | |
| 181 | using namespace android; |
| 182 | |
| 183 | int register_android_hardware_SensorManager(JNIEnv *env) |
| 184 | { |
Jeff Brown | 25157e4 | 2012-04-16 12:13:05 -0700 | [diff] [blame] | 185 | return jniRegisterNativeMethods(env, "android/hardware/SystemSensorManager", |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | gMethods, NELEM(gMethods)); |
| 187 | } |