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 | */ |
Mike Lockwood | 270e87f | 2009-05-22 10:02:14 -0400 | [diff] [blame] | 16 | #define LOG_TAG "SensorManager" |
| 17 | |
Peng Xu | d5ec5f9 | 2016-04-27 14:49:25 -0700 | [diff] [blame] | 18 | #include "JNIHelp.h" |
| 19 | #include "android_os_MessageQueue.h" |
| 20 | #include "core_jni_helpers.h" |
| 21 | #include "jni.h" |
destradaa | 9ba7c1c | 2015-02-10 15:04:43 -0800 | [diff] [blame] | 22 | |
Aravind Akella | 8844599 | 2015-02-26 17:05:28 -0800 | [diff] [blame] | 23 | #include <ScopedUtfChars.h> |
Jeff Brown | 3b4049e | 2015-04-17 15:22:27 -0700 | [diff] [blame] | 24 | #include <ScopedLocalRef.h> |
Peng Xu | d5ec5f9 | 2016-04-27 14:49:25 -0700 | [diff] [blame] | 25 | #include <android_runtime/AndroidRuntime.h> |
| 26 | #include <gui/Sensor.h> |
| 27 | #include <gui/SensorEventQueue.h> |
| 28 | #include <gui/SensorManager.h> |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 29 | #include <utils/Log.h> |
| 30 | #include <utils/Looper.h> |
Peng Xu | a35b553 | 2016-01-20 00:05:45 -0800 | [diff] [blame] | 31 | #include <utils/Vector.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | |
Peng Xu | d5ec5f9 | 2016-04-27 14:49:25 -0700 | [diff] [blame] | 33 | #include <endian.h> // htobe64 |
| 34 | #include <map> |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 35 | |
Peng Xu | 4a4baef | 2016-03-16 16:56:39 -0700 | [diff] [blame] | 36 | namespace { |
| 37 | |
| 38 | using namespace android; |
| 39 | |
| 40 | struct { |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 41 | jclass clazz; |
| 42 | jmethodID dispatchSensorEvent; |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 43 | jmethodID dispatchFlushCompleteEvent; |
Peng Xu | a295835 | 2016-01-26 18:42:17 -0800 | [diff] [blame] | 44 | jmethodID dispatchAdditionalInfoEvent; |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 45 | } gBaseEventQueueClassInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 46 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | struct SensorOffsets |
| 48 | { |
Peng Xu | a35b553 | 2016-01-20 00:05:45 -0800 | [diff] [blame] | 49 | jclass clazz; |
Peng Xu | b85b1fe | 2016-04-20 21:28:33 -0700 | [diff] [blame] | 50 | //fields |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | jfieldID name; |
| 52 | jfieldID vendor; |
| 53 | jfieldID version; |
| 54 | jfieldID handle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | jfieldID range; |
| 56 | jfieldID resolution; |
| 57 | jfieldID power; |
Mathias Agopian | 050b562 | 2010-07-29 16:51:38 -0700 | [diff] [blame] | 58 | jfieldID minDelay; |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 59 | jfieldID fifoReservedEventCount; |
| 60 | jfieldID fifoMaxEventCount; |
Aravind Akella | cd9a7bb | 2014-04-07 22:55:21 +0000 | [diff] [blame] | 61 | jfieldID stringType; |
| 62 | jfieldID requiredPermission; |
Aravind Akella | 2f8b914 | 2014-05-12 14:39:29 -0700 | [diff] [blame] | 63 | jfieldID maxDelay; |
Aravind Akella | 2790035 | 2014-06-03 19:20:42 -0700 | [diff] [blame] | 64 | jfieldID flags; |
Peng Xu | b85b1fe | 2016-04-20 21:28:33 -0700 | [diff] [blame] | 65 | //methods |
destradaa | 9ba7c1c | 2015-02-10 15:04:43 -0800 | [diff] [blame] | 66 | jmethodID setType; |
Peng Xu | b85b1fe | 2016-04-20 21:28:33 -0700 | [diff] [blame] | 67 | jmethodID setUuid; |
Peng Xu | a35b553 | 2016-01-20 00:05:45 -0800 | [diff] [blame] | 68 | jmethodID init; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | } gSensorOffsets; |
| 70 | |
Peng Xu | a35b553 | 2016-01-20 00:05:45 -0800 | [diff] [blame] | 71 | struct ListOffsets { |
| 72 | jclass clazz; |
| 73 | jmethodID add; |
| 74 | } gListOffsets; |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 75 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | /* |
Peng Xu | 4a4baef | 2016-03-16 16:56:39 -0700 | [diff] [blame] | 77 | * nativeClassInit is not inteneded to be thread-safe. It should be called before other native... |
| 78 | * functions (except nativeCreate). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 79 | */ |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 80 | static void |
| 81 | nativeClassInit (JNIEnv *_env, jclass _this) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | { |
Peng Xu | a35b553 | 2016-01-20 00:05:45 -0800 | [diff] [blame] | 83 | //android.hardware.Sensor |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 84 | SensorOffsets& sensorOffsets = gSensorOffsets; |
Peng Xu | a35b553 | 2016-01-20 00:05:45 -0800 | [diff] [blame] | 85 | jclass sensorClass = (jclass) _env->NewGlobalRef(_env->FindClass("android/hardware/Sensor")); |
| 86 | sensorOffsets.clazz = sensorClass; |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 87 | sensorOffsets.name = _env->GetFieldID(sensorClass, "mName", "Ljava/lang/String;"); |
| 88 | sensorOffsets.vendor = _env->GetFieldID(sensorClass, "mVendor", "Ljava/lang/String;"); |
| 89 | sensorOffsets.version = _env->GetFieldID(sensorClass, "mVersion", "I"); |
| 90 | sensorOffsets.handle = _env->GetFieldID(sensorClass, "mHandle", "I"); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 91 | sensorOffsets.range = _env->GetFieldID(sensorClass, "mMaxRange", "F"); |
| 92 | sensorOffsets.resolution = _env->GetFieldID(sensorClass, "mResolution","F"); |
| 93 | sensorOffsets.power = _env->GetFieldID(sensorClass, "mPower", "F"); |
| 94 | sensorOffsets.minDelay = _env->GetFieldID(sensorClass, "mMinDelay", "I"); |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 95 | sensorOffsets.fifoReservedEventCount = |
| 96 | _env->GetFieldID(sensorClass, "mFifoReservedEventCount", "I"); |
| 97 | sensorOffsets.fifoMaxEventCount = _env->GetFieldID(sensorClass, "mFifoMaxEventCount", "I"); |
Aravind Akella | cd9a7bb | 2014-04-07 22:55:21 +0000 | [diff] [blame] | 98 | sensorOffsets.stringType = _env->GetFieldID(sensorClass, "mStringType", "Ljava/lang/String;"); |
| 99 | sensorOffsets.requiredPermission = _env->GetFieldID(sensorClass, "mRequiredPermission", |
| 100 | "Ljava/lang/String;"); |
Aravind Akella | 2f8b914 | 2014-05-12 14:39:29 -0700 | [diff] [blame] | 101 | sensorOffsets.maxDelay = _env->GetFieldID(sensorClass, "mMaxDelay", "I"); |
Aravind Akella | 2790035 | 2014-06-03 19:20:42 -0700 | [diff] [blame] | 102 | sensorOffsets.flags = _env->GetFieldID(sensorClass, "mFlags", "I"); |
Peng Xu | a35b553 | 2016-01-20 00:05:45 -0800 | [diff] [blame] | 103 | |
destradaa | 9ba7c1c | 2015-02-10 15:04:43 -0800 | [diff] [blame] | 104 | sensorOffsets.setType = _env->GetMethodID(sensorClass, "setType", "(I)Z"); |
Peng Xu | b85b1fe | 2016-04-20 21:28:33 -0700 | [diff] [blame] | 105 | sensorOffsets.setUuid = _env->GetMethodID(sensorClass, "setUuid", "(JJ)V"); |
Peng Xu | a35b553 | 2016-01-20 00:05:45 -0800 | [diff] [blame] | 106 | sensorOffsets.init = _env->GetMethodID(sensorClass, "<init>", "()V"); |
| 107 | |
| 108 | // java.util.List; |
| 109 | ListOffsets& listOffsets = gListOffsets; |
| 110 | jclass listClass = (jclass) _env->NewGlobalRef(_env->FindClass("java/util/List")); |
| 111 | listOffsets.clazz = listClass; |
| 112 | listOffsets.add = _env->GetMethodID(listClass, "add", "(Ljava/lang/Object;)Z"); |
destradaa | 9ba7c1c | 2015-02-10 15:04:43 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | /** |
| 116 | * A key comparator predicate. |
| 117 | * It is used to intern strings associated with Sensor data. |
| 118 | * It defines a 'Strict weak ordering' for the interned strings. |
| 119 | */ |
| 120 | class InternedStringCompare { |
| 121 | public: |
| 122 | bool operator()(const String8* string1, const String8* string2) const { |
| 123 | if (string1 == NULL) { |
| 124 | return string2 != NULL; |
| 125 | } |
Aravind Akella | 8844599 | 2015-02-26 17:05:28 -0800 | [diff] [blame] | 126 | if (string2 == NULL) { |
| 127 | return false; |
| 128 | } |
destradaa | 9ba7c1c | 2015-02-10 15:04:43 -0800 | [diff] [blame] | 129 | return string1->compare(*string2) < 0; |
| 130 | } |
| 131 | }; |
| 132 | |
| 133 | /** |
| 134 | * A localized interning mechanism for Sensor strings. |
| 135 | * We implement our own interning to avoid the overhead of using java.lang.String#intern(). |
| 136 | * It is common that Vendor, StringType, and RequirePermission data is common between many of the |
| 137 | * Sensors, by interning the memory usage to represent Sensors is optimized. |
| 138 | */ |
| 139 | static jstring |
| 140 | getInternedString(JNIEnv *env, const String8* string) { |
| 141 | static std::map<const String8*, jstring, InternedStringCompare> internedStrings; |
| 142 | |
| 143 | jstring internedString; |
| 144 | std::map<const String8*, jstring>::iterator iterator = internedStrings.find(string); |
| 145 | if (iterator != internedStrings.end()) { |
| 146 | internedString = iterator->second; |
| 147 | } else { |
| 148 | jstring localString = env->NewStringUTF(string->string()); |
| 149 | // we are implementing our own interning so expect these strings to be backed by global refs |
| 150 | internedString = (jstring) env->NewGlobalRef(localString); |
| 151 | internedStrings.insert(std::make_pair(string, internedString)); |
| 152 | env->DeleteLocalRef(localString); |
| 153 | } |
destradaa | 9ba7c1c | 2015-02-10 15:04:43 -0800 | [diff] [blame] | 154 | return internedString; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | } |
| 156 | |
Svet Ganov | b9d71a6 | 2015-04-30 10:38:13 -0700 | [diff] [blame] | 157 | static jlong |
| 158 | nativeCreate |
| 159 | (JNIEnv *env, jclass clazz, jstring opPackageName) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 160 | { |
Svet Ganov | b9d71a6 | 2015-04-30 10:38:13 -0700 | [diff] [blame] | 161 | ScopedUtfChars opPackageNameUtf(env, opPackageName); |
Aravind Akella | 2af91d0 | 2015-07-29 18:39:52 -0700 | [diff] [blame] | 162 | return (jlong) &SensorManager::getInstanceForPackage(String16(opPackageNameUtf.c_str())); |
Svet Ganov | b9d71a6 | 2015-04-30 10:38:13 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Peng Xu | a35b553 | 2016-01-20 00:05:45 -0800 | [diff] [blame] | 165 | static jobject |
| 166 | translateNativeSensorToJavaSensor(JNIEnv *env, jobject sensor, const Sensor& nativeSensor) { |
| 167 | const SensorOffsets& sensorOffsets(gSensorOffsets); |
| 168 | |
| 169 | if (sensor == NULL) { |
| 170 | // Sensor sensor = new Sensor(); |
| 171 | sensor = env->NewObject(sensorOffsets.clazz, sensorOffsets.init, ""); |
| 172 | } |
| 173 | |
| 174 | if (sensor != NULL) { |
| 175 | jstring name = env->NewStringUTF(nativeSensor.getName().string()); |
| 176 | jstring vendor = env->NewStringUTF(nativeSensor.getVendor().string()); |
| 177 | jstring requiredPermission = |
| 178 | env->NewStringUTF(nativeSensor.getRequiredPermission().string()); |
| 179 | |
| 180 | env->SetObjectField(sensor, sensorOffsets.name, name); |
| 181 | env->SetObjectField(sensor, sensorOffsets.vendor, vendor); |
| 182 | env->SetIntField(sensor, sensorOffsets.version, nativeSensor.getVersion()); |
| 183 | env->SetIntField(sensor, sensorOffsets.handle, nativeSensor.getHandle()); |
| 184 | env->SetFloatField(sensor, sensorOffsets.range, nativeSensor.getMaxValue()); |
| 185 | env->SetFloatField(sensor, sensorOffsets.resolution, nativeSensor.getResolution()); |
| 186 | env->SetFloatField(sensor, sensorOffsets.power, nativeSensor.getPowerUsage()); |
| 187 | env->SetIntField(sensor, sensorOffsets.minDelay, nativeSensor.getMinDelay()); |
| 188 | env->SetIntField(sensor, sensorOffsets.fifoReservedEventCount, |
| 189 | nativeSensor.getFifoReservedEventCount()); |
| 190 | env->SetIntField(sensor, sensorOffsets.fifoMaxEventCount, |
| 191 | nativeSensor.getFifoMaxEventCount()); |
| 192 | env->SetObjectField(sensor, sensorOffsets.requiredPermission, |
| 193 | requiredPermission); |
| 194 | env->SetIntField(sensor, sensorOffsets.maxDelay, nativeSensor.getMaxDelay()); |
| 195 | env->SetIntField(sensor, sensorOffsets.flags, nativeSensor.getFlags()); |
Peng Xu | b85b1fe | 2016-04-20 21:28:33 -0700 | [diff] [blame] | 196 | |
Peng Xu | a35b553 | 2016-01-20 00:05:45 -0800 | [diff] [blame] | 197 | if (env->CallBooleanMethod(sensor, sensorOffsets.setType, nativeSensor.getType()) |
| 198 | == JNI_FALSE) { |
| 199 | jstring stringType = getInternedString(env, &nativeSensor.getStringType()); |
| 200 | env->SetObjectField(sensor, sensorOffsets.stringType, stringType); |
| 201 | } |
Peng Xu | b85b1fe | 2016-04-20 21:28:33 -0700 | [diff] [blame] | 202 | |
| 203 | // java.util.UUID constructor UUID(long a, long b) assumes the two long inputs a and b |
| 204 | // correspond to first half and second half of the 16-byte stream in big-endian, |
| 205 | // respectively, if the byte stream is serialized according to RFC 4122 (Sec. 4.1.2). |
| 206 | // |
| 207 | // For Java UUID 12345678-90AB-CDEF-1122-334455667788, the byte stream will be |
| 208 | // (uint8_t) {0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF, ....} according RFC 4122. |
| 209 | // |
| 210 | // According to Java UUID constructor document, the long parameter a should be always |
| 211 | // 0x12345678980ABCDEF regardless of host machine endianess. Thus, htobe64 is used to |
| 212 | // convert int64_t read directly, which will be in host-endian, to the big-endian required |
| 213 | // by Java constructor. |
| 214 | const int64_t (&uuid)[2] = nativeSensor.getUuid().i64; |
| 215 | env->CallVoidMethod(sensor, sensorOffsets.setUuid, htobe64(uuid[0]), htobe64(uuid[1])); |
Peng Xu | a35b553 | 2016-01-20 00:05:45 -0800 | [diff] [blame] | 216 | } |
| 217 | return sensor; |
| 218 | } |
| 219 | |
Aravind Akella | 516e40e | 2015-07-01 16:54:24 -0700 | [diff] [blame] | 220 | static jboolean |
| 221 | nativeGetSensorAtIndex(JNIEnv *env, jclass clazz, jlong sensorManager, jobject sensor, jint index) |
Svet Ganov | b9d71a6 | 2015-04-30 10:38:13 -0700 | [diff] [blame] | 222 | { |
| 223 | SensorManager* mgr = reinterpret_cast<SensorManager*>(sensorManager); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 224 | |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 225 | Sensor const* const* sensorList; |
Peng Xu | a08c9f5 | 2016-02-04 22:27:20 -0800 | [diff] [blame] | 226 | ssize_t count = mgr->getSensorList(&sensorList); |
| 227 | if (ssize_t(index) >= count) { |
Aravind Akella | 516e40e | 2015-07-01 16:54:24 -0700 | [diff] [blame] | 228 | return false; |
destradaa | 9ba7c1c | 2015-02-10 15:04:43 -0800 | [diff] [blame] | 229 | } |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 230 | |
Peng Xu | b85b1fe | 2016-04-20 21:28:33 -0700 | [diff] [blame] | 231 | return translateNativeSensorToJavaSensor(env, sensor, *sensorList[index]) != NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | } |
| 233 | |
Peng Xu | a35b553 | 2016-01-20 00:05:45 -0800 | [diff] [blame] | 234 | static void |
| 235 | nativeGetDynamicSensors(JNIEnv *env, jclass clazz, jlong sensorManager, jobject sensorList) { |
| 236 | |
| 237 | SensorManager* mgr = reinterpret_cast<SensorManager*>(sensorManager); |
| 238 | const ListOffsets& listOffsets(gListOffsets); |
| 239 | |
| 240 | Vector<Sensor> nativeList; |
| 241 | |
| 242 | mgr->getDynamicSensorList(nativeList); |
| 243 | |
| 244 | ALOGI("DYNS native SensorManager.getDynamicSensorList return %d sensors", nativeList.size()); |
| 245 | for (size_t i = 0; i < nativeList.size(); ++i) { |
| 246 | jobject sensor = translateNativeSensorToJavaSensor(env, NULL, nativeList[i]); |
| 247 | // add to list |
| 248 | env->CallBooleanMethod(sensorList, listOffsets.add, sensor); |
| 249 | } |
| 250 | } |
| 251 | |
Aravind Akella | d123b51 | 2015-06-29 12:35:51 -0700 | [diff] [blame] | 252 | static jboolean nativeIsDataInjectionEnabled(JNIEnv *_env, jclass _this, jlong sensorManager) { |
Svet Ganov | b9d71a6 | 2015-04-30 10:38:13 -0700 | [diff] [blame] | 253 | SensorManager* mgr = reinterpret_cast<SensorManager*>(sensorManager); |
Aravind Akella | d123b51 | 2015-06-29 12:35:51 -0700 | [diff] [blame] | 254 | return mgr->isDataInjectionEnabled(); |
Aravind Akella | 18ebf732b | 2015-04-20 17:39:51 -0700 | [diff] [blame] | 255 | } |
| 256 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 257 | //---------------------------------------------------------------------------- |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 258 | |
| 259 | class Receiver : public LooperCallback { |
| 260 | sp<SensorEventQueue> mSensorQueue; |
| 261 | sp<MessageQueue> mMessageQueue; |
Jeff Brown | 3b4049e | 2015-04-17 15:22:27 -0700 | [diff] [blame] | 262 | jobject mReceiverWeakGlobal; |
Peng Xu | a295835 | 2016-01-26 18:42:17 -0800 | [diff] [blame] | 263 | jfloatArray mFloatScratch; |
| 264 | jintArray mIntScratch; |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 265 | public: |
| 266 | Receiver(const sp<SensorEventQueue>& sensorQueue, |
| 267 | const sp<MessageQueue>& messageQueue, |
Peng Xu | a295835 | 2016-01-26 18:42:17 -0800 | [diff] [blame] | 268 | jobject receiverWeak) { |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 269 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 270 | mSensorQueue = sensorQueue; |
| 271 | mMessageQueue = messageQueue; |
Jeff Brown | 3b4049e | 2015-04-17 15:22:27 -0700 | [diff] [blame] | 272 | mReceiverWeakGlobal = env->NewGlobalRef(receiverWeak); |
Peng Xu | a295835 | 2016-01-26 18:42:17 -0800 | [diff] [blame] | 273 | |
| 274 | mIntScratch = (jintArray) env->NewGlobalRef(env->NewIntArray(16)); |
| 275 | mFloatScratch = (jfloatArray) env->NewGlobalRef(env->NewFloatArray(16)); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 276 | } |
| 277 | ~Receiver() { |
| 278 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
Jeff Brown | 3b4049e | 2015-04-17 15:22:27 -0700 | [diff] [blame] | 279 | env->DeleteGlobalRef(mReceiverWeakGlobal); |
Peng Xu | a295835 | 2016-01-26 18:42:17 -0800 | [diff] [blame] | 280 | env->DeleteGlobalRef(mFloatScratch); |
| 281 | env->DeleteGlobalRef(mIntScratch); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 282 | } |
| 283 | sp<SensorEventQueue> getSensorEventQueue() const { |
| 284 | return mSensorQueue; |
| 285 | } |
| 286 | |
| 287 | void destroy() { |
| 288 | mMessageQueue->getLooper()->removeFd( mSensorQueue->getFd() ); |
| 289 | } |
| 290 | |
| 291 | private: |
| 292 | virtual void onFirstRef() { |
| 293 | LooperCallback::onFirstRef(); |
| 294 | mMessageQueue->getLooper()->addFd(mSensorQueue->getFd(), 0, |
| 295 | ALOOPER_EVENT_INPUT, this, mSensorQueue.get()); |
| 296 | } |
| 297 | |
| 298 | virtual int handleEvent(int fd, int events, void* data) { |
| 299 | JNIEnv* env = AndroidRuntime::getJNIEnv(); |
| 300 | sp<SensorEventQueue> q = reinterpret_cast<SensorEventQueue *>(data); |
Jeff Brown | 3b4049e | 2015-04-17 15:22:27 -0700 | [diff] [blame] | 301 | ScopedLocalRef<jobject> receiverObj(env, jniGetReferent(env, mReceiverWeakGlobal)); |
| 302 | |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 303 | ssize_t n; |
| 304 | ASensorEvent buffer[16]; |
| 305 | while ((n = q->read(buffer, 16)) > 0) { |
| 306 | for (int i=0 ; i<n ; i++) { |
Aravind Akella | 0288ca6 | 2014-07-28 14:53:44 -0700 | [diff] [blame] | 307 | if (buffer[i].type == SENSOR_TYPE_STEP_COUNTER) { |
Jean-Baptiste Queru | 3c9c091 | 2013-08-05 11:03:28 -0700 | [diff] [blame] | 308 | // step-counter returns a uint64, but the java API only deals with floats |
| 309 | float value = float(buffer[i].u64.step_counter); |
Peng Xu | a295835 | 2016-01-26 18:42:17 -0800 | [diff] [blame] | 310 | env->SetFloatArrayRegion(mFloatScratch, 0, 1, &value); |
Peng Xu | a35b553 | 2016-01-20 00:05:45 -0800 | [diff] [blame] | 311 | } else if (buffer[i].type == SENSOR_TYPE_DYNAMIC_SENSOR_META) { |
| 312 | float value[2]; |
| 313 | value[0] = buffer[i].dynamic_sensor_meta.connected ? 1.f: 0.f; |
| 314 | value[1] = float(buffer[i].dynamic_sensor_meta.handle); |
Peng Xu | a295835 | 2016-01-26 18:42:17 -0800 | [diff] [blame] | 315 | env->SetFloatArrayRegion(mFloatScratch, 0, 2, value); |
| 316 | } else if (buffer[i].type == SENSOR_TYPE_ADDITIONAL_INFO) { |
| 317 | env->SetIntArrayRegion(mIntScratch, 0, 14, |
| 318 | buffer[i].additional_info.data_int32); |
| 319 | env->SetFloatArrayRegion(mFloatScratch, 0, 14, |
| 320 | buffer[i].additional_info.data_float); |
Jean-Baptiste Queru | 3c9c091 | 2013-08-05 11:03:28 -0700 | [diff] [blame] | 321 | } else { |
Peng Xu | a295835 | 2016-01-26 18:42:17 -0800 | [diff] [blame] | 322 | env->SetFloatArrayRegion(mFloatScratch, 0, 16, buffer[i].data); |
Jean-Baptiste Queru | 3c9c091 | 2013-08-05 11:03:28 -0700 | [diff] [blame] | 323 | } |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 324 | |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 325 | if (buffer[i].type == SENSOR_TYPE_META_DATA) { |
| 326 | // This is a flush complete sensor event. Call dispatchFlushCompleteEvent |
| 327 | // method. |
Jeff Brown | 3b4049e | 2015-04-17 15:22:27 -0700 | [diff] [blame] | 328 | if (receiverObj.get()) { |
| 329 | env->CallVoidMethod(receiverObj.get(), |
| 330 | gBaseEventQueueClassInfo.dispatchFlushCompleteEvent, |
| 331 | buffer[i].meta_data.sensor); |
| 332 | } |
Peng Xu | a295835 | 2016-01-26 18:42:17 -0800 | [diff] [blame] | 333 | } else if (buffer[i].type == SENSOR_TYPE_ADDITIONAL_INFO) { |
| 334 | // This is a flush complete sensor event. Call dispatchAdditionalInfoEvent |
| 335 | // method. |
| 336 | if (receiverObj.get()) { |
| 337 | int type = buffer[i].additional_info.type; |
| 338 | int serial = buffer[i].additional_info.serial; |
| 339 | env->CallVoidMethod(receiverObj.get(), |
| 340 | gBaseEventQueueClassInfo.dispatchAdditionalInfoEvent, |
| 341 | buffer[i].sensor, |
| 342 | type, serial, |
| 343 | mFloatScratch, |
| 344 | mIntScratch, |
| 345 | buffer[i].timestamp); |
| 346 | } |
| 347 | }else { |
Etienne Le Grand | af80510 | 2014-05-16 12:21:41 -0700 | [diff] [blame] | 348 | int8_t status; |
| 349 | switch (buffer[i].type) { |
| 350 | case SENSOR_TYPE_ORIENTATION: |
| 351 | case SENSOR_TYPE_MAGNETIC_FIELD: |
| 352 | case SENSOR_TYPE_ACCELEROMETER: |
| 353 | case SENSOR_TYPE_GYROSCOPE: |
Aravind Akella | f38226a | 2015-06-23 10:32:16 -0700 | [diff] [blame] | 354 | case SENSOR_TYPE_GRAVITY: |
| 355 | case SENSOR_TYPE_LINEAR_ACCELERATION: |
Etienne Le Grand | af80510 | 2014-05-16 12:21:41 -0700 | [diff] [blame] | 356 | status = buffer[i].vector.status; |
| 357 | break; |
| 358 | case SENSOR_TYPE_HEART_RATE: |
| 359 | status = buffer[i].heart_rate.status; |
| 360 | break; |
| 361 | default: |
| 362 | status = SENSOR_STATUS_ACCURACY_HIGH; |
| 363 | break; |
| 364 | } |
Jeff Brown | 3b4049e | 2015-04-17 15:22:27 -0700 | [diff] [blame] | 365 | if (receiverObj.get()) { |
| 366 | env->CallVoidMethod(receiverObj.get(), |
| 367 | gBaseEventQueueClassInfo.dispatchSensorEvent, |
| 368 | buffer[i].sensor, |
Peng Xu | a295835 | 2016-01-26 18:42:17 -0800 | [diff] [blame] | 369 | mFloatScratch, |
Jeff Brown | 3b4049e | 2015-04-17 15:22:27 -0700 | [diff] [blame] | 370 | status, |
| 371 | buffer[i].timestamp); |
| 372 | } |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 373 | } |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 374 | if (env->ExceptionCheck()) { |
Aravind Akella | 35187bd | 2014-02-11 18:44:42 -0800 | [diff] [blame] | 375 | mSensorQueue->sendAck(buffer, n); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 376 | ALOGE("Exception dispatching input event."); |
| 377 | return 1; |
| 378 | } |
| 379 | } |
Aravind Akella | 35187bd | 2014-02-11 18:44:42 -0800 | [diff] [blame] | 380 | mSensorQueue->sendAck(buffer, n); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 381 | } |
| 382 | if (n<0 && n != -EAGAIN) { |
| 383 | // FIXME: error receiving events, what to do in this case? |
| 384 | } |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 385 | return 1; |
| 386 | } |
| 387 | }; |
| 388 | |
Svet Ganov | b9d71a6 | 2015-04-30 10:38:13 -0700 | [diff] [blame] | 389 | static jlong nativeInitSensorEventQueue(JNIEnv *env, jclass clazz, jlong sensorManager, |
Peng Xu | a295835 | 2016-01-26 18:42:17 -0800 | [diff] [blame] | 390 | jobject eventQWeak, jobject msgQ, jstring packageName, jint mode) { |
Svet Ganov | b9d71a6 | 2015-04-30 10:38:13 -0700 | [diff] [blame] | 391 | SensorManager* mgr = reinterpret_cast<SensorManager*>(sensorManager); |
Aravind Akella | 8844599 | 2015-02-26 17:05:28 -0800 | [diff] [blame] | 392 | ScopedUtfChars packageUtf(env, packageName); |
| 393 | String8 clientName(packageUtf.c_str()); |
Svet Ganov | b9d71a6 | 2015-04-30 10:38:13 -0700 | [diff] [blame] | 394 | sp<SensorEventQueue> queue(mgr->createEventQueue(clientName, mode)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 395 | |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 396 | sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, msgQ); |
| 397 | if (messageQueue == NULL) { |
| 398 | jniThrowRuntimeException(env, "MessageQueue is not initialized."); |
| 399 | return 0; |
Mathias Agopian | 0b6d77b | 2012-05-07 18:27:06 -0700 | [diff] [blame] | 400 | } |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 401 | |
Peng Xu | a295835 | 2016-01-26 18:42:17 -0800 | [diff] [blame] | 402 | sp<Receiver> receiver = new Receiver(queue, messageQueue, eventQWeak); |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 403 | receiver->incStrong((void*)nativeInitSensorEventQueue); |
Ashok Bhat | 4838e33 | 2014-01-03 14:37:19 +0000 | [diff] [blame] | 404 | return jlong(receiver.get()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 405 | } |
| 406 | |
Ashok Bhat | 4838e33 | 2014-01-03 14:37:19 +0000 | [diff] [blame] | 407 | 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] | 408 | jint maxBatchReportLatency) { |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 409 | sp<Receiver> receiver(reinterpret_cast<Receiver *>(eventQ)); |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 410 | return receiver->getSensorEventQueue()->enableSensor(handle, rate_us, maxBatchReportLatency, |
Aravind Akella | 8844599 | 2015-02-26 17:05:28 -0800 | [diff] [blame] | 411 | 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 412 | } |
| 413 | |
Ashok Bhat | 4838e33 | 2014-01-03 14:37:19 +0000 | [diff] [blame] | 414 | static jint nativeDisableSensor(JNIEnv *env, jclass clazz, jlong eventQ, jint handle) { |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 415 | sp<Receiver> receiver(reinterpret_cast<Receiver *>(eventQ)); |
| 416 | return receiver->getSensorEventQueue()->disableSensor(handle); |
| 417 | } |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 418 | |
Aravind Akella | 18ebf732b | 2015-04-20 17:39:51 -0700 | [diff] [blame] | 419 | static void nativeDestroySensorEventQueue(JNIEnv *env, jclass clazz, jlong eventQ) { |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 420 | sp<Receiver> receiver(reinterpret_cast<Receiver *>(eventQ)); |
| 421 | receiver->destroy(); |
Mathias Agopian | b1d90c8 | 2013-03-06 17:45:42 -0800 | [diff] [blame] | 422 | receiver->decStrong((void*)nativeInitSensorEventQueue); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 423 | } |
Mathias Agopian | 1bf7978 | 2010-07-14 23:41:37 -0700 | [diff] [blame] | 424 | |
Ashok Bhat | 4838e33 | 2014-01-03 14:37:19 +0000 | [diff] [blame] | 425 | static jint nativeFlushSensor(JNIEnv *env, jclass clazz, jlong eventQ) { |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 426 | sp<Receiver> receiver(reinterpret_cast<Receiver *>(eventQ)); |
Aravind Akella | 4bdc37d | 2013-10-01 17:58:35 -0700 | [diff] [blame] | 427 | return receiver->getSensorEventQueue()->flush(); |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 428 | } |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 429 | |
Aravind Akella | 18ebf732b | 2015-04-20 17:39:51 -0700 | [diff] [blame] | 430 | static jint nativeInjectSensorData(JNIEnv *env, jclass clazz, jlong eventQ, jint handle, |
| 431 | jfloatArray values, jint accuracy, jlong timestamp) { |
| 432 | sp<Receiver> receiver(reinterpret_cast<Receiver *>(eventQ)); |
| 433 | // Create a sensor_event from the above data which can be injected into the HAL. |
| 434 | ASensorEvent sensor_event; |
| 435 | memset(&sensor_event, 0, sizeof(sensor_event)); |
| 436 | sensor_event.sensor = handle; |
| 437 | sensor_event.timestamp = timestamp; |
| 438 | env->GetFloatArrayRegion(values, 0, env->GetArrayLength(values), sensor_event.data); |
| 439 | return receiver->getSensorEventQueue()->injectSensorEvent(sensor_event); |
| 440 | } |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 441 | //---------------------------------------------------------------------------- |
| 442 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 443 | static const JNINativeMethod gSystemSensorManagerMethods[] = { |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 444 | {"nativeClassInit", |
| 445 | "()V", |
| 446 | (void*)nativeClassInit }, |
Svet Ganov | b9d71a6 | 2015-04-30 10:38:13 -0700 | [diff] [blame] | 447 | {"nativeCreate", |
| 448 | "(Ljava/lang/String;)J", |
| 449 | (void*)nativeCreate }, |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 450 | |
Aravind Akella | 516e40e | 2015-07-01 16:54:24 -0700 | [diff] [blame] | 451 | {"nativeGetSensorAtIndex", |
| 452 | "(JLandroid/hardware/Sensor;I)Z", |
| 453 | (void*)nativeGetSensorAtIndex }, |
Aravind Akella | 18ebf732b | 2015-04-20 17:39:51 -0700 | [diff] [blame] | 454 | |
Peng Xu | a35b553 | 2016-01-20 00:05:45 -0800 | [diff] [blame] | 455 | {"nativeGetDynamicSensors", |
| 456 | "(JLjava/util/List;)V", |
| 457 | (void*)nativeGetDynamicSensors }, |
| 458 | |
Aravind Akella | d123b51 | 2015-06-29 12:35:51 -0700 | [diff] [blame] | 459 | {"nativeIsDataInjectionEnabled", |
| 460 | "(J)Z", |
| 461 | (void*)nativeIsDataInjectionEnabled}, |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 462 | }; |
| 463 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 464 | static const JNINativeMethod gBaseEventQueueMethods[] = { |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 465 | {"nativeInitBaseEventQueue", |
Peng Xu | a295835 | 2016-01-26 18:42:17 -0800 | [diff] [blame] | 466 | "(JLjava/lang/ref/WeakReference;Landroid/os/MessageQueue;Ljava/lang/String;ILjava/lang/String;)J", |
Svet Ganov | b9d71a6 | 2015-04-30 10:38:13 -0700 | [diff] [blame] | 467 | (void*)nativeInitSensorEventQueue }, |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 468 | |
| 469 | {"nativeEnableSensor", |
Aravind Akella | 8844599 | 2015-02-26 17:05:28 -0800 | [diff] [blame] | 470 | "(JIII)I", |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 471 | (void*)nativeEnableSensor }, |
| 472 | |
| 473 | {"nativeDisableSensor", |
Ashok Bhat | 4838e33 | 2014-01-03 14:37:19 +0000 | [diff] [blame] | 474 | "(JI)I", |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 475 | (void*)nativeDisableSensor }, |
| 476 | |
| 477 | {"nativeDestroySensorEventQueue", |
Ashok Bhat | 4838e33 | 2014-01-03 14:37:19 +0000 | [diff] [blame] | 478 | "(J)V", |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 479 | (void*)nativeDestroySensorEventQueue }, |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 480 | |
| 481 | {"nativeFlushSensor", |
Ashok Bhat | 4838e33 | 2014-01-03 14:37:19 +0000 | [diff] [blame] | 482 | "(J)I", |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 483 | (void*)nativeFlushSensor }, |
Aravind Akella | 18ebf732b | 2015-04-20 17:39:51 -0700 | [diff] [blame] | 484 | |
| 485 | {"nativeInjectSensorData", |
| 486 | "(JI[FIJ)I", |
| 487 | (void*)nativeInjectSensorData }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 488 | }; |
| 489 | |
Peng Xu | 4a4baef | 2016-03-16 16:56:39 -0700 | [diff] [blame] | 490 | } //unnamed namespace |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 491 | |
| 492 | int register_android_hardware_SensorManager(JNIEnv *env) |
| 493 | { |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 494 | RegisterMethodsOrDie(env, "android/hardware/SystemSensorManager", |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 495 | gSystemSensorManagerMethods, NELEM(gSystemSensorManagerMethods)); |
| 496 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 497 | RegisterMethodsOrDie(env, "android/hardware/SystemSensorManager$BaseEventQueue", |
Jaikumar Ganesh | 9a8df4d | 2013-02-12 16:31:32 -0800 | [diff] [blame] | 498 | gBaseEventQueueMethods, NELEM(gBaseEventQueueMethods)); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 499 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 500 | gBaseEventQueueClassInfo.clazz = FindClassOrDie(env, |
| 501 | "android/hardware/SystemSensorManager$BaseEventQueue"); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 502 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 503 | gBaseEventQueueClassInfo.dispatchSensorEvent = GetMethodIDOrDie(env, |
| 504 | gBaseEventQueueClassInfo.clazz, "dispatchSensorEvent", "(I[FIJ)V"); |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 505 | |
Andreas Gampe | 987f79f | 2014-11-18 17:29:46 -0800 | [diff] [blame] | 506 | gBaseEventQueueClassInfo.dispatchFlushCompleteEvent = GetMethodIDOrDie(env, |
| 507 | gBaseEventQueueClassInfo.clazz, "dispatchFlushCompleteEvent", "(I)V"); |
Aravind Akella | b4c76b1 | 2013-06-27 12:04:16 -0700 | [diff] [blame] | 508 | |
Peng Xu | a295835 | 2016-01-26 18:42:17 -0800 | [diff] [blame] | 509 | gBaseEventQueueClassInfo.dispatchAdditionalInfoEvent = GetMethodIDOrDie(env, |
| 510 | gBaseEventQueueClassInfo.clazz, "dispatchAdditionalInfoEvent", "(III[F[I)V"); |
| 511 | |
Mathias Agopian | db772d8 | 2013-01-31 19:31:12 -0800 | [diff] [blame] | 512 | return 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 513 | } |