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 | |
destradaa | 9ba7c1c | 2015-02-10 15:04:43 -0800 | [diff] [blame] | 19 | #include <map> |
| 20 | |
Aravind Akella | 8844599 | 2015-02-26 17:05:28 -0800 | [diff] [blame] | 21 | #include <ScopedUtfChars.h> |
Jeff Brown | 3b4049e | 2015-04-17 15:22:27 -0700 | [diff] [blame] | 22 | #include <ScopedLocalRef.h> |
Aravind Akella | 8844599 | 2015-02-26 17:05:28 -0800 | [diff] [blame] | 23 | |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 24 | #include <utils/Log.h> |
| 25 | #include <utils/Looper.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 27 | #include <gui/Sensor.h> |
| 28 | #include <gui/SensorManager.h> |
| 29 | #include <gui/SensorEventQueue.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | |
| 31 | #include "jni.h" |
| 32 | #include "JNIHelp.h" |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 33 | #include "android_os_MessageQueue.h" |
| 34 | #include <android_runtime/AndroidRuntime.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 36 | #include "core_jni_helpers.h" |
| 37 | |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 38 | static struct { |
| 39 | jclass clazz; |
| 40 | jmethodID dispatchSensorEvent; |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 41 | jmethodID dispatchFlushCompleteEvent; |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 42 | } gBaseEventQueueClassInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | |
| 44 | namespace android { |
| 45 | |
| 46 | struct SensorOffsets |
| 47 | { |
| 48 | jfieldID name; |
| 49 | jfieldID vendor; |
| 50 | jfieldID version; |
| 51 | jfieldID handle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | jfieldID range; |
| 53 | jfieldID resolution; |
| 54 | jfieldID power; |
Mathias Agopian | 050b562 | 2010-07-29 16:51:38 -0700 | [diff] [blame] | 55 | jfieldID minDelay; |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 56 | jfieldID fifoReservedEventCount; |
| 57 | jfieldID fifoMaxEventCount; |
Aravind Akella | cd9a7bb | 2014-04-07 22:55:21 +0000 | [diff] [blame] | 58 | jfieldID stringType; |
| 59 | jfieldID requiredPermission; |
Aravind Akella | 2f8b914 | 2014-05-12 14:39:29 -0700 | [diff] [blame] | 60 | jfieldID maxDelay; |
Aravind Akella | 2790035 | 2014-06-03 19:20:42 -0700 | [diff] [blame] | 61 | jfieldID flags; |
destradaa | 9ba7c1c | 2015-02-10 15:04:43 -0800 | [diff] [blame] | 62 | jmethodID setType; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | } gSensorOffsets; |
| 64 | |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 65 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | /* |
| 67 | * The method below are not thread-safe and not intended to be |
| 68 | */ |
| 69 | |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 70 | static void |
| 71 | nativeClassInit (JNIEnv *_env, jclass _this) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | { |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 73 | jclass sensorClass = _env->FindClass("android/hardware/Sensor"); |
| 74 | SensorOffsets& sensorOffsets = gSensorOffsets; |
| 75 | sensorOffsets.name = _env->GetFieldID(sensorClass, "mName", "Ljava/lang/String;"); |
| 76 | sensorOffsets.vendor = _env->GetFieldID(sensorClass, "mVendor", "Ljava/lang/String;"); |
| 77 | sensorOffsets.version = _env->GetFieldID(sensorClass, "mVersion", "I"); |
| 78 | sensorOffsets.handle = _env->GetFieldID(sensorClass, "mHandle", "I"); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 79 | sensorOffsets.range = _env->GetFieldID(sensorClass, "mMaxRange", "F"); |
| 80 | sensorOffsets.resolution = _env->GetFieldID(sensorClass, "mResolution","F"); |
| 81 | sensorOffsets.power = _env->GetFieldID(sensorClass, "mPower", "F"); |
| 82 | sensorOffsets.minDelay = _env->GetFieldID(sensorClass, "mMinDelay", "I"); |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 83 | sensorOffsets.fifoReservedEventCount = |
| 84 | _env->GetFieldID(sensorClass, "mFifoReservedEventCount", "I"); |
| 85 | sensorOffsets.fifoMaxEventCount = _env->GetFieldID(sensorClass, "mFifoMaxEventCount", "I"); |
Aravind Akella | cd9a7bb | 2014-04-07 22:55:21 +0000 | [diff] [blame] | 86 | sensorOffsets.stringType = _env->GetFieldID(sensorClass, "mStringType", "Ljava/lang/String;"); |
| 87 | sensorOffsets.requiredPermission = _env->GetFieldID(sensorClass, "mRequiredPermission", |
| 88 | "Ljava/lang/String;"); |
Aravind Akella | 2f8b914 | 2014-05-12 14:39:29 -0700 | [diff] [blame] | 89 | sensorOffsets.maxDelay = _env->GetFieldID(sensorClass, "mMaxDelay", "I"); |
Aravind Akella | 2790035 | 2014-06-03 19:20:42 -0700 | [diff] [blame] | 90 | sensorOffsets.flags = _env->GetFieldID(sensorClass, "mFlags", "I"); |
destradaa | 9ba7c1c | 2015-02-10 15:04:43 -0800 | [diff] [blame] | 91 | sensorOffsets.setType = _env->GetMethodID(sensorClass, "setType", "(I)Z"); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * A key comparator predicate. |
| 96 | * It is used to intern strings associated with Sensor data. |
| 97 | * It defines a 'Strict weak ordering' for the interned strings. |
| 98 | */ |
| 99 | class InternedStringCompare { |
| 100 | public: |
| 101 | bool operator()(const String8* string1, const String8* string2) const { |
| 102 | if (string1 == NULL) { |
| 103 | return string2 != NULL; |
| 104 | } |
Aravind Akella | 8844599 | 2015-02-26 17:05:28 -0800 | [diff] [blame] | 105 | if (string2 == NULL) { |
| 106 | return false; |
| 107 | } |
destradaa | 9ba7c1c | 2015-02-10 15:04:43 -0800 | [diff] [blame] | 108 | return string1->compare(*string2) < 0; |
| 109 | } |
| 110 | }; |
| 111 | |
| 112 | /** |
| 113 | * A localized interning mechanism for Sensor strings. |
| 114 | * We implement our own interning to avoid the overhead of using java.lang.String#intern(). |
| 115 | * It is common that Vendor, StringType, and RequirePermission data is common between many of the |
| 116 | * Sensors, by interning the memory usage to represent Sensors is optimized. |
| 117 | */ |
| 118 | static jstring |
| 119 | getInternedString(JNIEnv *env, const String8* string) { |
| 120 | static std::map<const String8*, jstring, InternedStringCompare> internedStrings; |
| 121 | |
| 122 | jstring internedString; |
| 123 | std::map<const String8*, jstring>::iterator iterator = internedStrings.find(string); |
| 124 | if (iterator != internedStrings.end()) { |
| 125 | internedString = iterator->second; |
| 126 | } else { |
| 127 | jstring localString = env->NewStringUTF(string->string()); |
| 128 | // we are implementing our own interning so expect these strings to be backed by global refs |
| 129 | internedString = (jstring) env->NewGlobalRef(localString); |
| 130 | internedStrings.insert(std::make_pair(string, internedString)); |
| 131 | env->DeleteLocalRef(localString); |
| 132 | } |
| 133 | |
| 134 | return internedString; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | } |
| 136 | |
Svet Ganov | b9d71a6 | 2015-04-30 10:38:13 -0700 | [diff] [blame] | 137 | static jlong |
| 138 | nativeCreate |
| 139 | (JNIEnv *env, jclass clazz, jstring opPackageName) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 140 | { |
Svet Ganov | b9d71a6 | 2015-04-30 10:38:13 -0700 | [diff] [blame] | 141 | ScopedUtfChars opPackageNameUtf(env, opPackageName); |
| 142 | return (jlong) new SensorManager(String16(opPackageNameUtf.c_str())); |
| 143 | } |
| 144 | |
| 145 | static jint |
| 146 | nativeGetNextSensor(JNIEnv *env, jclass clazz, jlong sensorManager, jobject sensor, jint next) |
| 147 | { |
| 148 | SensorManager* mgr = reinterpret_cast<SensorManager*>(sensorManager); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 150 | Sensor const* const* sensorList; |
Svet Ganov | b9d71a6 | 2015-04-30 10:38:13 -0700 | [diff] [blame] | 151 | size_t count = mgr->getSensorList(&sensorList); |
destradaa | 9ba7c1c | 2015-02-10 15:04:43 -0800 | [diff] [blame] | 152 | if (size_t(next) >= count) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | return -1; |
destradaa | 9ba7c1c | 2015-02-10 15:04:43 -0800 | [diff] [blame] | 154 | } |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 155 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 156 | Sensor const* const list = sensorList[next]; |
| 157 | const SensorOffsets& sensorOffsets(gSensorOffsets); |
destradaa | 9ba7c1c | 2015-02-10 15:04:43 -0800 | [diff] [blame] | 158 | jstring name = getInternedString(env, &list->getName()); |
| 159 | jstring vendor = getInternedString(env, &list->getVendor()); |
| 160 | jstring requiredPermission = getInternedString(env, &list->getRequiredPermission()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | env->SetObjectField(sensor, sensorOffsets.name, name); |
| 162 | env->SetObjectField(sensor, sensorOffsets.vendor, vendor); |
Mathias Agopian | 277d1b2 | 2012-05-04 15:51:29 -0700 | [diff] [blame] | 163 | env->SetIntField(sensor, sensorOffsets.version, list->getVersion()); |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 164 | env->SetIntField(sensor, sensorOffsets.handle, list->getHandle()); |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 165 | env->SetFloatField(sensor, sensorOffsets.range, list->getMaxValue()); |
| 166 | env->SetFloatField(sensor, sensorOffsets.resolution, list->getResolution()); |
| 167 | env->SetFloatField(sensor, sensorOffsets.power, list->getPowerUsage()); |
Mathias Agopian | 050b562 | 2010-07-29 16:51:38 -0700 | [diff] [blame] | 168 | env->SetIntField(sensor, sensorOffsets.minDelay, list->getMinDelay()); |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 169 | env->SetIntField(sensor, sensorOffsets.fifoReservedEventCount, |
| 170 | list->getFifoReservedEventCount()); |
Aravind Akella | cd9a7bb | 2014-04-07 22:55:21 +0000 | [diff] [blame] | 171 | env->SetIntField(sensor, sensorOffsets.fifoMaxEventCount, |
| 172 | list->getFifoMaxEventCount()); |
Aravind Akella | cd9a7bb | 2014-04-07 22:55:21 +0000 | [diff] [blame] | 173 | env->SetObjectField(sensor, sensorOffsets.requiredPermission, |
| 174 | requiredPermission); |
Aravind Akella | 2f8b914 | 2014-05-12 14:39:29 -0700 | [diff] [blame] | 175 | env->SetIntField(sensor, sensorOffsets.maxDelay, list->getMaxDelay()); |
Aravind Akella | 2790035 | 2014-06-03 19:20:42 -0700 | [diff] [blame] | 176 | env->SetIntField(sensor, sensorOffsets.flags, list->getFlags()); |
destradaa | 9ba7c1c | 2015-02-10 15:04:43 -0800 | [diff] [blame] | 177 | if (env->CallBooleanMethod(sensor, sensorOffsets.setType, list->getType()) == JNI_FALSE) { |
| 178 | jstring stringType = getInternedString(env, &list->getStringType()); |
| 179 | env->SetObjectField(sensor, sensorOffsets.stringType, stringType); |
| 180 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 181 | next++; |
Jeff Brown | 4fe6c3e | 2010-09-13 23:17:30 -0700 | [diff] [blame] | 182 | return size_t(next) < count ? next : 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Svet Ganov | b9d71a6 | 2015-04-30 10:38:13 -0700 | [diff] [blame] | 185 | static int nativeEnableDataInjection(JNIEnv *_env, jclass _this, jlong sensorManager, |
| 186 | jboolean enable) { |
| 187 | SensorManager* mgr = reinterpret_cast<SensorManager*>(sensorManager); |
| 188 | return mgr->enableDataInjection(enable); |
Aravind Akella | 18ebf732b | 2015-04-20 17:39:51 -0700 | [diff] [blame] | 189 | } |
| 190 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | //---------------------------------------------------------------------------- |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 192 | |
| 193 | class Receiver : public LooperCallback { |
| 194 | sp<SensorEventQueue> mSensorQueue; |
| 195 | sp<MessageQueue> mMessageQueue; |
Jeff Brown | 3b4049e | 2015-04-17 15:22:27 -0700 | [diff] [blame] | 196 | jobject mReceiverWeakGlobal; |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 197 | jfloatArray mScratch; |
| 198 | public: |
| 199 | Receiver(const sp<SensorEventQueue>& sensorQueue, |
| 200 | const sp<MessageQueue>& messageQueue, |
Jeff Brown | 3b4049e | 2015-04-17 15:22:27 -0700 | [diff] [blame] | 201 | jobject receiverWeak, jfloatArray scratch) { |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 202 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 203 | mSensorQueue = sensorQueue; |
| 204 | mMessageQueue = messageQueue; |
Jeff Brown | 3b4049e | 2015-04-17 15:22:27 -0700 | [diff] [blame] | 205 | mReceiverWeakGlobal = env->NewGlobalRef(receiverWeak); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 206 | mScratch = (jfloatArray)env->NewGlobalRef(scratch); |
| 207 | } |
| 208 | ~Receiver() { |
| 209 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
Jeff Brown | 3b4049e | 2015-04-17 15:22:27 -0700 | [diff] [blame] | 210 | env->DeleteGlobalRef(mReceiverWeakGlobal); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 211 | env->DeleteGlobalRef(mScratch); |
| 212 | } |
| 213 | sp<SensorEventQueue> getSensorEventQueue() const { |
| 214 | return mSensorQueue; |
| 215 | } |
| 216 | |
| 217 | void destroy() { |
| 218 | mMessageQueue->getLooper()->removeFd( mSensorQueue->getFd() ); |
| 219 | } |
| 220 | |
| 221 | private: |
| 222 | virtual void onFirstRef() { |
| 223 | LooperCallback::onFirstRef(); |
| 224 | mMessageQueue->getLooper()->addFd(mSensorQueue->getFd(), 0, |
| 225 | ALOOPER_EVENT_INPUT, this, mSensorQueue.get()); |
| 226 | } |
| 227 | |
| 228 | virtual int handleEvent(int fd, int events, void* data) { |
| 229 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 230 | sp<SensorEventQueue> q = reinterpret_cast<SensorEventQueue *>(data); |
Jeff Brown | 3b4049e | 2015-04-17 15:22:27 -0700 | [diff] [blame] | 231 | ScopedLocalRef<jobject> receiverObj(env, jniGetReferent(env, mReceiverWeakGlobal)); |
| 232 | |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 233 | ssize_t n; |
| 234 | ASensorEvent buffer[16]; |
| 235 | while ((n = q->read(buffer, 16)) > 0) { |
| 236 | for (int i=0 ; i<n ; i++) { |
Aravind Akella | 0288ca6 | 2014-07-28 14:53:44 -0700 | [diff] [blame] | 237 | if (buffer[i].type == SENSOR_TYPE_STEP_COUNTER) { |
Jean-Baptiste Queru | 3c9c091 | 2013-08-05 11:03:28 -0700 | [diff] [blame] | 238 | // step-counter returns a uint64, but the java API only deals with floats |
| 239 | float value = float(buffer[i].u64.step_counter); |
| 240 | env->SetFloatArrayRegion(mScratch, 0, 1, &value); |
| 241 | } else { |
| 242 | env->SetFloatArrayRegion(mScratch, 0, 16, buffer[i].data); |
| 243 | } |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 244 | |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 245 | if (buffer[i].type == SENSOR_TYPE_META_DATA) { |
| 246 | // This is a flush complete sensor event. Call dispatchFlushCompleteEvent |
| 247 | // method. |
Jeff Brown | 3b4049e | 2015-04-17 15:22:27 -0700 | [diff] [blame] | 248 | if (receiverObj.get()) { |
| 249 | env->CallVoidMethod(receiverObj.get(), |
| 250 | gBaseEventQueueClassInfo.dispatchFlushCompleteEvent, |
| 251 | buffer[i].meta_data.sensor); |
| 252 | } |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 253 | } else { |
Etienne Le Grand | af80510 | 2014-05-16 12:21:41 -0700 | [diff] [blame] | 254 | int8_t status; |
| 255 | switch (buffer[i].type) { |
| 256 | case SENSOR_TYPE_ORIENTATION: |
| 257 | case SENSOR_TYPE_MAGNETIC_FIELD: |
| 258 | case SENSOR_TYPE_ACCELEROMETER: |
| 259 | case SENSOR_TYPE_GYROSCOPE: |
Aravind Akella | f38226a | 2015-06-23 10:32:16 -0700 | [diff] [blame^] | 260 | case SENSOR_TYPE_GRAVITY: |
| 261 | case SENSOR_TYPE_LINEAR_ACCELERATION: |
Etienne Le Grand | af80510 | 2014-05-16 12:21:41 -0700 | [diff] [blame] | 262 | status = buffer[i].vector.status; |
| 263 | break; |
| 264 | case SENSOR_TYPE_HEART_RATE: |
| 265 | status = buffer[i].heart_rate.status; |
| 266 | break; |
| 267 | default: |
| 268 | status = SENSOR_STATUS_ACCURACY_HIGH; |
| 269 | break; |
| 270 | } |
Jeff Brown | 3b4049e | 2015-04-17 15:22:27 -0700 | [diff] [blame] | 271 | if (receiverObj.get()) { |
| 272 | env->CallVoidMethod(receiverObj.get(), |
| 273 | gBaseEventQueueClassInfo.dispatchSensorEvent, |
| 274 | buffer[i].sensor, |
| 275 | mScratch, |
| 276 | status, |
| 277 | buffer[i].timestamp); |
| 278 | } |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 279 | } |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 280 | if (env->ExceptionCheck()) { |
Aravind Akella | 35187bd | 2014-02-11 18:44:42 -0800 | [diff] [blame] | 281 | mSensorQueue->sendAck(buffer, n); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 282 | ALOGE("Exception dispatching input event."); |
| 283 | return 1; |
| 284 | } |
| 285 | } |
Aravind Akella | 35187bd | 2014-02-11 18:44:42 -0800 | [diff] [blame] | 286 | mSensorQueue->sendAck(buffer, n); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 287 | } |
| 288 | if (n<0 && n != -EAGAIN) { |
| 289 | // FIXME: error receiving events, what to do in this case? |
| 290 | } |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 291 | return 1; |
| 292 | } |
| 293 | }; |
| 294 | |
Svet Ganov | b9d71a6 | 2015-04-30 10:38:13 -0700 | [diff] [blame] | 295 | static jlong nativeInitSensorEventQueue(JNIEnv *env, jclass clazz, jlong sensorManager, |
| 296 | jobject eventQWeak, jobject msgQ, jfloatArray scratch, jstring packageName, jint mode) { |
| 297 | SensorManager* mgr = reinterpret_cast<SensorManager*>(sensorManager); |
Aravind Akella | 8844599 | 2015-02-26 17:05:28 -0800 | [diff] [blame] | 298 | ScopedUtfChars packageUtf(env, packageName); |
| 299 | String8 clientName(packageUtf.c_str()); |
Svet Ganov | b9d71a6 | 2015-04-30 10:38:13 -0700 | [diff] [blame] | 300 | sp<SensorEventQueue> queue(mgr->createEventQueue(clientName, mode)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 302 | sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, msgQ); |
| 303 | if (messageQueue == NULL) { |
| 304 | jniThrowRuntimeException(env, "MessageQueue is not initialized."); |
| 305 | return 0; |
Mathias Agopian | 0b6d77b | 2012-05-07 18:27:06 -0700 | [diff] [blame] | 306 | } |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 307 | |
Jeff Brown | 3b4049e | 2015-04-17 15:22:27 -0700 | [diff] [blame] | 308 | sp<Receiver> receiver = new Receiver(queue, messageQueue, eventQWeak, scratch); |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 309 | receiver->incStrong((void*)nativeInitSensorEventQueue); |
Ashok Bhat | 4838e33 | 2014-01-03 14:37:19 +0000 | [diff] [blame] | 310 | return jlong(receiver.get()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | } |
| 312 | |
Ashok Bhat | 4838e33 | 2014-01-03 14:37:19 +0000 | [diff] [blame] | 313 | static jint nativeEnableSensor(JNIEnv *env, jclass clazz, jlong eventQ, jint handle, jint rate_us, |
Aravind Akella | 8844599 | 2015-02-26 17:05:28 -0800 | [diff] [blame] | 314 | jint maxBatchReportLatency) { |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 315 | sp<Receiver> receiver(reinterpret_cast<Receiver *>(eventQ)); |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 316 | return receiver->getSensorEventQueue()->enableSensor(handle, rate_us, maxBatchReportLatency, |
Aravind Akella | 8844599 | 2015-02-26 17:05:28 -0800 | [diff] [blame] | 317 | 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | } |
| 319 | |
Ashok Bhat | 4838e33 | 2014-01-03 14:37:19 +0000 | [diff] [blame] | 320 | static jint nativeDisableSensor(JNIEnv *env, jclass clazz, jlong eventQ, jint handle) { |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 321 | sp<Receiver> receiver(reinterpret_cast<Receiver *>(eventQ)); |
| 322 | return receiver->getSensorEventQueue()->disableSensor(handle); |
| 323 | } |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 324 | |
Aravind Akella | 18ebf732b | 2015-04-20 17:39:51 -0700 | [diff] [blame] | 325 | static void nativeDestroySensorEventQueue(JNIEnv *env, jclass clazz, jlong eventQ) { |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 326 | sp<Receiver> receiver(reinterpret_cast<Receiver *>(eventQ)); |
| 327 | receiver->destroy(); |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 328 | receiver->decStrong((void*)nativeInitSensorEventQueue); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 329 | } |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 330 | |
Ashok Bhat | 4838e33 | 2014-01-03 14:37:19 +0000 | [diff] [blame] | 331 | static jint nativeFlushSensor(JNIEnv *env, jclass clazz, jlong eventQ) { |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 332 | sp<Receiver> receiver(reinterpret_cast<Receiver *>(eventQ)); |
Aravind Akella | 4bdc37d | 2013-10-01 17:58:35 -0700 | [diff] [blame] | 333 | return receiver->getSensorEventQueue()->flush(); |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 334 | } |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 335 | |
Aravind Akella | 18ebf732b | 2015-04-20 17:39:51 -0700 | [diff] [blame] | 336 | static jint nativeInjectSensorData(JNIEnv *env, jclass clazz, jlong eventQ, jint handle, |
| 337 | jfloatArray values, jint accuracy, jlong timestamp) { |
| 338 | sp<Receiver> receiver(reinterpret_cast<Receiver *>(eventQ)); |
| 339 | // Create a sensor_event from the above data which can be injected into the HAL. |
| 340 | ASensorEvent sensor_event; |
| 341 | memset(&sensor_event, 0, sizeof(sensor_event)); |
| 342 | sensor_event.sensor = handle; |
| 343 | sensor_event.timestamp = timestamp; |
| 344 | env->GetFloatArrayRegion(values, 0, env->GetArrayLength(values), sensor_event.data); |
| 345 | return receiver->getSensorEventQueue()->injectSensorEvent(sensor_event); |
| 346 | } |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 347 | //---------------------------------------------------------------------------- |
| 348 | |
| 349 | static JNINativeMethod gSystemSensorManagerMethods[] = { |
| 350 | {"nativeClassInit", |
| 351 | "()V", |
| 352 | (void*)nativeClassInit }, |
Svet Ganov | b9d71a6 | 2015-04-30 10:38:13 -0700 | [diff] [blame] | 353 | {"nativeCreate", |
| 354 | "(Ljava/lang/String;)J", |
| 355 | (void*)nativeCreate }, |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 356 | |
| 357 | {"nativeGetNextSensor", |
Svet Ganov | b9d71a6 | 2015-04-30 10:38:13 -0700 | [diff] [blame] | 358 | "(JLandroid/hardware/Sensor;I)I", |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 359 | (void*)nativeGetNextSensor }, |
Aravind Akella | 18ebf732b | 2015-04-20 17:39:51 -0700 | [diff] [blame] | 360 | |
| 361 | {"nativeEnableDataInjection", |
Svet Ganov | b9d71a6 | 2015-04-30 10:38:13 -0700 | [diff] [blame] | 362 | "(JZ)I", |
Aravind Akella | 18ebf732b | 2015-04-20 17:39:51 -0700 | [diff] [blame] | 363 | (void*)nativeEnableDataInjection }, |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 364 | }; |
| 365 | |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 366 | static JNINativeMethod gBaseEventQueueMethods[] = { |
| 367 | {"nativeInitBaseEventQueue", |
Svet Ganov | b9d71a6 | 2015-04-30 10:38:13 -0700 | [diff] [blame] | 368 | "(JLjava/lang/ref/WeakReference;Landroid/os/MessageQueue;[FLjava/lang/String;ILjava/lang/String;)J", |
| 369 | (void*)nativeInitSensorEventQueue }, |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 370 | |
| 371 | {"nativeEnableSensor", |
Aravind Akella | 8844599 | 2015-02-26 17:05:28 -0800 | [diff] [blame] | 372 | "(JIII)I", |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 373 | (void*)nativeEnableSensor }, |
| 374 | |
| 375 | {"nativeDisableSensor", |
Ashok Bhat | 4838e33 | 2014-01-03 14:37:19 +0000 | [diff] [blame] | 376 | "(JI)I", |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 377 | (void*)nativeDisableSensor }, |
| 378 | |
| 379 | {"nativeDestroySensorEventQueue", |
Ashok Bhat | 4838e33 | 2014-01-03 14:37:19 +0000 | [diff] [blame] | 380 | "(J)V", |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 381 | (void*)nativeDestroySensorEventQueue }, |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 382 | |
| 383 | {"nativeFlushSensor", |
Ashok Bhat | 4838e33 | 2014-01-03 14:37:19 +0000 | [diff] [blame] | 384 | "(J)I", |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 385 | (void*)nativeFlushSensor }, |
Aravind Akella | 18ebf732b | 2015-04-20 17:39:51 -0700 | [diff] [blame] | 386 | |
| 387 | {"nativeInjectSensorData", |
| 388 | "(JI[FIJ)I", |
| 389 | (void*)nativeInjectSensorData }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | }; |
| 391 | |
| 392 | }; // namespace android |
| 393 | |
| 394 | using namespace android; |
| 395 | |
| 396 | int register_android_hardware_SensorManager(JNIEnv *env) |
| 397 | { |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 398 | RegisterMethodsOrDie(env, "android/hardware/SystemSensorManager", |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 399 | gSystemSensorManagerMethods, NELEM(gSystemSensorManagerMethods)); |
| 400 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 401 | RegisterMethodsOrDie(env, "android/hardware/SystemSensorManager$BaseEventQueue", |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 402 | gBaseEventQueueMethods, NELEM(gBaseEventQueueMethods)); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 403 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 404 | gBaseEventQueueClassInfo.clazz = FindClassOrDie(env, |
| 405 | "android/hardware/SystemSensorManager$BaseEventQueue"); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 406 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 407 | gBaseEventQueueClassInfo.dispatchSensorEvent = GetMethodIDOrDie(env, |
| 408 | gBaseEventQueueClassInfo.clazz, "dispatchSensorEvent", "(I[FIJ)V"); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 409 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 410 | gBaseEventQueueClassInfo.dispatchFlushCompleteEvent = GetMethodIDOrDie(env, |
| 411 | gBaseEventQueueClassInfo.clazz, "dispatchFlushCompleteEvent", "(I)V"); |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 412 | |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 413 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 414 | } |