diff options
author | BogDan Vatra <bogdan@kde.org> | 2019-04-02 14:00:37 +0300 |
---|---|---|
committer | BogDan Vatra <bogdan@kdab.com> | 2019-04-11 06:52:45 +0000 |
commit | 2c9fc2c1a197b84ba9ef3dc4e586a77a18c3dcc5 (patch) | |
tree | 38ac45eb44b585e47542c7b6df20088388836283 /src/plugins | |
parent | f528104474dd74e34ea6a01da096e7c31730c01a (diff) | |
download | qtsensors-2c9fc2c1a197b84ba9ef3dc4e586a77a18c3dcc5.tar.gz |
Say hello to Qt Android Sensors based on NDK API
Until Qt 5.9, we had to use JNI way because the NDK sensors API was
introduced in API-16. Starting with Qt 5.9 we can safely use the NDK API
as API_16 is the minimum API supported by Qt.
Using the NDK API instead of JNI boost the performance a lot! The CPU
usage dropped 2 to 4 times, from over 60% using the JNI to less than
20%.
[ChangeLog][Android] Rewrite Qt Android Sensors using NDK
API.
Change-Id: If3e3f1e56973e2a189662912b2a6ebfbd71dee14
Reviewed-by: Lorn Potter <lorn.potter@gmail.com>
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/sensors/android/android.pro | 43 | ||||
-rw-r--r-- | src/plugins/sensors/android/androidaccelerometer.cpp (renamed from src/plugins/sensors/android/src/androidaccelerometer.cpp) | 80 | ||||
-rw-r--r-- | src/plugins/sensors/android/androidaccelerometer.h (renamed from src/plugins/sensors/android/src/androidaccelerometer.h) | 32 | ||||
-rw-r--r-- | src/plugins/sensors/android/androidcompass.cpp | 167 | ||||
-rw-r--r-- | src/plugins/sensors/android/androidcompass.h (renamed from src/plugins/sensors/android/src/androidcompass.h) | 34 | ||||
-rw-r--r-- | src/plugins/sensors/android/androidgyroscope.cpp (renamed from src/plugins/sensors/android/src/androidgyroscope.cpp) | 32 | ||||
-rw-r--r-- | src/plugins/sensors/android/androidgyroscope.h (renamed from src/plugins/sensors/android/src/androidgyroscope.h) | 16 | ||||
-rw-r--r-- | src/plugins/sensors/android/androidlight.cpp (renamed from src/plugins/sensors/android/src/androidlight.cpp) | 23 | ||||
-rw-r--r-- | src/plugins/sensors/android/androidlight.h (renamed from src/plugins/sensors/android/src/androidlight.h) | 16 | ||||
-rw-r--r-- | src/plugins/sensors/android/androidmagnetometer.cpp (renamed from src/plugins/sensors/android/src/androidmagnetometer.cpp) | 48 | ||||
-rw-r--r-- | src/plugins/sensors/android/androidmagnetometer.h (renamed from src/plugins/sensors/android/src/androidmagnetometer.h) | 16 | ||||
-rw-r--r-- | src/plugins/sensors/android/androidpressure.cpp (renamed from src/plugins/sensors/android/src/androidpressure.cpp) | 23 | ||||
-rw-r--r-- | src/plugins/sensors/android/androidpressure.h (renamed from src/plugins/sensors/android/src/androidpressure.h) | 17 | ||||
-rw-r--r-- | src/plugins/sensors/android/androidproximity.cpp (renamed from src/plugins/sensors/android/src/androidproximity.cpp) | 23 | ||||
-rw-r--r-- | src/plugins/sensors/android/androidproximity.h (renamed from src/plugins/sensors/android/src/androidproximity.h) | 13 | ||||
-rw-r--r-- | src/plugins/sensors/android/androidrotation.cpp | 65 | ||||
-rw-r--r-- | src/plugins/sensors/android/androidrotation.h (renamed from src/plugins/sensors/android/src/androidrotation.h) | 17 | ||||
-rw-r--r-- | src/plugins/sensors/android/androidtemperature.cpp (renamed from src/plugins/sensors/android/src/androidrotation.cpp) | 27 | ||||
-rw-r--r-- | src/plugins/sensors/android/androidtemperature.h (renamed from src/plugins/sensors/android/src/androidtemperature.h) | 18 | ||||
-rw-r--r-- | src/plugins/sensors/android/jar/AndroidManifest.xml | 6 | ||||
-rw-r--r-- | src/plugins/sensors/android/jar/jar.pro | 15 | ||||
-rw-r--r-- | src/plugins/sensors/android/jar/src/org/qtproject/qt5/android/sensors/QtSensors.java | 192 | ||||
-rw-r--r-- | src/plugins/sensors/android/main.cpp (renamed from src/plugins/sensors/android/src/main.cpp) | 115 | ||||
-rw-r--r-- | src/plugins/sensors/android/plugin.json (renamed from src/plugins/sensors/android/src/plugin.json) | 0 | ||||
-rw-r--r-- | src/plugins/sensors/android/sensoreventqueue.h | 166 | ||||
-rw-r--r-- | src/plugins/sensors/android/sensormanager.cpp | 120 | ||||
-rw-r--r-- | src/plugins/sensors/android/sensormanager.h | 77 | ||||
-rw-r--r-- | src/plugins/sensors/android/src/androidcommonsensor.h | 86 | ||||
-rw-r--r-- | src/plugins/sensors/android/src/androidcompass.cpp | 177 | ||||
-rw-r--r-- | src/plugins/sensors/android/src/androidjnisensors.cpp | 277 | ||||
-rw-r--r-- | src/plugins/sensors/android/src/androidjnisensors.h | 87 | ||||
-rw-r--r-- | src/plugins/sensors/android/src/androidtemperature.cpp | 64 | ||||
-rw-r--r-- | src/plugins/sensors/android/src/src.pro | 39 |
33 files changed, 906 insertions, 1225 deletions
diff --git a/src/plugins/sensors/android/android.pro b/src/plugins/sensors/android/android.pro index 0dc6a3f..15e26ee 100644 --- a/src/plugins/sensors/android/android.pro +++ b/src/plugins/sensors/android/android.pro @@ -1,2 +1,41 @@ -TEMPLATE = subdirs -SUBDIRS += jar src +TARGET = qtsensors_android + +QT = sensors core-private + +# STATICPLUGIN needed because there's a Q_IMPORT_PLUGIN in main.cpp +# Yes, the plugin imports itself statically +DEFINES += QT_STATICPLUGIN + +HEADERS = \ + androidaccelerometer.h \ + androidcompass.h \ + androidgyroscope.h \ + androidmagnetometer.h \ + androidpressure.h \ + androidproximity.h \ + androidrotation.h \ + androidtemperature.h \ + androidlight.h \ + sensoreventqueue.h \ + sensormanager.h + +SOURCES = \ + main.cpp \ + androidaccelerometer.cpp \ + androidcompass.cpp \ + androidgyroscope.cpp \ + androidmagnetometer.cpp \ + androidpressure.cpp \ + androidproximity.cpp \ + androidrotation.cpp \ + androidtemperature.cpp \ + androidlight.cpp \ + sensormanager.cpp + +OTHER_FILES = plugin.json + +LIBS += -landroid + +PLUGIN_TYPE = sensors +PLUGIN_CLASS_NAME = QCounterGesturePlugin +load(qt_plugin) diff --git a/src/plugins/sensors/android/src/androidaccelerometer.cpp b/src/plugins/sensors/android/androidaccelerometer.cpp index 2e9990b..c9f5141 100644 --- a/src/plugins/sensors/android/src/androidaccelerometer.cpp +++ b/src/plugins/sensors/android/androidaccelerometer.cpp @@ -1,7 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 BogDan Vatra <bogdan@kde.org> -** Copyright (C) 2016 BlackBerry Limited. All rights reserved. +** Copyright (C) 2019 BogDan Vatra <bogdan@kde.org> ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtSensors module of the Qt Toolkit. @@ -39,62 +38,63 @@ ****************************************************************************/ #include "androidaccelerometer.h" +#include <QDebug> -AndroidAccelerometer::AndroidAccelerometer(AndroidSensors::AndroidSensorType type, QSensor *sensor) - : AndroidCommonSensor<QAccelerometerReading>(type, sensor) +AndroidAccelerometer::AndroidAccelerometer(int accelerationModes, QSensor *sensor, QObject *parent) + : SensorEventQueue<QAccelerometerReading>(ASENSOR_TYPE_ACCELEROMETER, sensor, parent) + , m_accelerationModes(accelerationModes) { - QAccelerometer * const accelerometer = qobject_cast<QAccelerometer *>(sensor); + auto accelerometer = qobject_cast<QAccelerometer *>(sensor); if (accelerometer) { - connect(accelerometer, SIGNAL(accelerationModeChanged(AccelerationMode)), - this, SLOT(applyAccelerationMode())); + connect(accelerometer, &QAccelerometer::accelerationModeChanged, + this, &AndroidAccelerometer::applyAccelerationMode); + applyAccelerationMode(accelerometer->accelerationMode()); } } -void AndroidAccelerometer::onSensorChanged(jlong timestamp, const jfloat *values, uint size) +bool AndroidAccelerometer::isFeatureSupported(QSensor::Feature feature) const { - if (size < 3) - return; - m_reader.setTimestamp(timestamp/1000); - // check https://developer.android.com/reference/android/hardware/SensorEvent.html#values - m_reader.setX(values[0]); - m_reader.setY(values[1]); - m_reader.setZ(values[2]); - newReadingAvailable(); -} - -void AndroidAccelerometer::onAccuracyChanged(jint accuracy) -{ - Q_UNUSED(accuracy) + return (feature == QSensor::AccelerationMode) ? m_accelerationModes == AllModes : SensorEventQueue<QAccelerometerReading>::isFeatureSupported(feature); } -void AndroidAccelerometer::applyAccelerationMode() +void AndroidAccelerometer::dataReceived(const ASensorEvent &event) { - const QAccelerometer * const accelerometer = qobject_cast<QAccelerometer *>(sensor()); - if (accelerometer) { - stop(); //Stop previous sensor and start new one - m_type = modeToSensor(accelerometer->accelerationMode()); - start(); + // check https://developer.android.com/reference/android/hardware/SensorEvent.html#sensor.type_accelerometer: + // check https://developer.android.com/reference/android/hardware/SensorEvent.html#sensor.type_linear_acceleration: + // check https://developer.android.com/reference/android/hardware/SensorEvent.html#sensor.type_gravity: + const auto &acc = event.acceleration; + auto x = qreal(acc.x); + auto y = qreal(acc.y); + auto z = qreal(acc.z); + if (sensor()->skipDuplicates() && qFuzzyCompare(m_reader.x(), x) && + qFuzzyCompare(m_reader.y(), y) && + qFuzzyCompare(m_reader.z(), z)) { + return; } + m_reader.setTimestamp(uint64_t(event.timestamp / 1000)); + m_reader.setX(x); + m_reader.setY(y); + m_reader.setZ(z); + newReadingAvailable(); } -AndroidSensors::AndroidSensorType AndroidAccelerometer::modeToSensor(QAccelerometer::AccelerationMode mode) -{ - AndroidSensors::AndroidSensorType type; - switch (mode) { +void AndroidAccelerometer::applyAccelerationMode(QAccelerometer::AccelerationMode accelerationMode) +{ + switch (accelerationMode) { case QAccelerometer::Gravity: - type = AndroidSensors::TYPE_GRAVITY; + if (!(m_accelerationModes & Gravity)) + qWarning() << "Gravity sensor missing"; + setSensorType(ASENSOR_TYPE_GRAVITY); break; case QAccelerometer::User: - type = AndroidSensors::TYPE_LINEAR_ACCELERATION; + if (!(m_accelerationModes & LinearAcceleration)) + qWarning() << "Linear acceleration sensor missing"; + setSensorType(ASENSOR_TYPE_LINEAR_ACCELERATION); break; case QAccelerometer::Combined: - default: - type = AndroidSensors::TYPE_ACCELEROMETER; + if (!(m_accelerationModes & Accelerometer)) + qWarning() << "Accelerometer sensor missing"; + setSensorType(ASENSOR_TYPE_ACCELEROMETER); break; } - - if (type != AndroidSensors::TYPE_ACCELEROMETER && !AndroidSensors::availableSensors().contains(type)) - type = AndroidSensors::TYPE_ACCELEROMETER; - - return type; } diff --git a/src/plugins/sensors/android/src/androidaccelerometer.h b/src/plugins/sensors/android/androidaccelerometer.h index 9b8bf0b..0436245 100644 --- a/src/plugins/sensors/android/src/androidaccelerometer.h +++ b/src/plugins/sensors/android/androidaccelerometer.h @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 BogDan Vatra <bogdan@kde.org> +** Copyright (C) 2019 BogDan Vatra <bogdan@kde.org> ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtSensors module of the Qt Toolkit. @@ -39,23 +39,35 @@ #ifndef ANDROIDACCELEROMETER_H #define ANDROIDACCELEROMETER_H + #include <qaccelerometer.h> -#include "androidcommonsensor.h" +#include "sensoreventqueue.h" -class AndroidAccelerometer : public AndroidCommonSensor<QAccelerometerReading> +class AndroidAccelerometer : public SensorEventQueue<QAccelerometerReading> { Q_OBJECT - public: - AndroidAccelerometer(AndroidSensors::AndroidSensorType type, QSensor *sensor); - static AndroidSensors::AndroidSensorType modeToSensor(QAccelerometer::AccelerationMode mode); + enum AccelerationModes { + Accelerometer = 1, + Gravity = 2, + LinearAcceleration = 4, + AllModes = (Accelerometer | Gravity | LinearAcceleration) + }; +public: + AndroidAccelerometer(int accelerationModes, QSensor *sensor, QObject *parent = nullptr); + // QSensorBackend interface + bool isFeatureSupported(QSensor::Feature feature) const override; + +protected: + // SensorEventQueue interface + void dataReceived(const ASensorEvent &event) override; + private: - void onAccuracyChanged(jint accuracy) override; - void onSensorChanged(jlong timestamp, const jfloat *values, uint size) override; + void applyAccelerationMode(QAccelerometer::AccelerationMode accelerationMode); -private Q_SLOTS: - void applyAccelerationMode(); +private: + int m_accelerationModes; }; diff --git a/src/plugins/sensors/android/androidcompass.cpp b/src/plugins/sensors/android/androidcompass.cpp new file mode 100644 index 0000000..653c5e9 --- /dev/null +++ b/src/plugins/sensors/android/androidcompass.cpp @@ -0,0 +1,167 @@ +/**************************************************************************** +** +** Copyright (C) 2019 BogDan Vatra <bogdan@kde.org> +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +/* + * Copyright (C) 2008 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +#include "androidcompass.h" + +#include <qmath.h> + +#include "sensormanager.h" + +AndroidCompass::AndroidCompass(QSensor *sensor, QObject *parent) + : ThreadSafeSensorBackend(sensor, parent) +{ + setDescription("Compass"); + setReading<QCompassReading>(&m_reading); + memset(&m_accelerometerEvent, 0, sizeof(ASensorVector)); + memset(&m_magneticEvent, 0, sizeof(ASensorVector)); + m_sensorEventQueue = ASensorManager_createEventQueue(m_sensorManager->manager(), m_sensorManager->looper(), -1, &looperCallback, this); + m_accelerometer = ASensorManager_getDefaultSensor(m_sensorManager->manager(), ASENSOR_TYPE_ACCELEROMETER); + m_magnetometer = ASensorManager_getDefaultSensor(m_sensorManager->manager(), ASENSOR_TYPE_MAGNETIC_FIELD); +} + +AndroidCompass::~AndroidCompass() +{ + stop(); + ASensorManager_destroyEventQueue(m_sensorManager->manager(), m_sensorEventQueue); +} + +void AndroidCompass::start() +{ + ASensorEventQueue_enableSensor(m_sensorEventQueue, m_accelerometer); + if (sensor()->dataRate() > 0) + ASensorEventQueue_setEventRate(m_sensorEventQueue, m_accelerometer, std::max(ASensor_getMinDelay(m_accelerometer), sensor()->dataRate())); + + ASensorEventQueue_enableSensor(m_sensorEventQueue, m_magnetometer); + if (sensor()->dataRate() > 0) + ASensorEventQueue_setEventRate(m_sensorEventQueue, m_magnetometer, std::max(ASensor_getMinDelay(m_magnetometer), sensor()->dataRate())); +} + +void AndroidCompass::stop() +{ + ASensorEventQueue_disableSensor(m_sensorEventQueue, m_accelerometer); + ASensorEventQueue_disableSensor(m_sensorEventQueue, m_magnetometer); +} + +void AndroidCompass::readAllEvents() +{ + { + ASensorEvent sensorEvent; + QMutexLocker lock(&m_sensorsMutex); + while (ASensorEventQueue_getEvents(m_sensorEventQueue, &sensorEvent, 1)) { + switch (sensorEvent.type) { + case ASENSOR_TYPE_ACCELEROMETER: + m_accelerometerEvent = sensorEvent.acceleration; + m_accelerometerEvent.status = m_accelerometerEvent.status == ASENSOR_STATUS_NO_CONTACT ? 0 : m_accelerometerEvent.status; + break; + case ASENSOR_TYPE_MAGNETIC_FIELD: + m_magneticEvent = sensorEvent.magnetic; + m_magneticEvent.status = m_magneticEvent.status == ASENSOR_STATUS_NO_CONTACT ? 0 : m_magneticEvent.status; + break; + } + } + } + + QCoreApplication::postEvent(this, new FunctionEvent{[=]() { + // merged getRotationMatrix https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/hardware/SensorManager.java#1182 + // and getOrientation https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/hardware/SensorManager.java#1477 + QMutexLocker lock(&m_sensorsMutex); + auto Ax = qreal(m_accelerometerEvent.x); + auto Ay = qreal(m_accelerometerEvent.y); + auto Az = qreal(m_accelerometerEvent.z); + + const qreal normsqA = (Ax * Ax + Ay * Ay + Az * Az); + const auto g = qreal(ASENSOR_STANDARD_GRAVITY); + const qreal freeFallGravitySquared = 0.01 * g * g; + if (normsqA < freeFallGravitySquared) + return; + + auto Ex = qreal(m_magneticEvent.x); + auto Ey = qreal(m_magneticEvent.y); + auto Ez = qreal(m_magneticEvent.z); + qreal Hx = Ey * Az - Ez * Ay; + qreal Hy = Ez * Ax - Ex * Az; + qreal Hz = Ex * Ay - Ey * Ax; + const qreal normH = std::sqrt(Hx * Hx + Hy * Hy + Hz * Hz); + + if (normH < 0.1) + return; + const qreal invH = 1.0 / normH; + Hx *= invH; + Hy *= invH; + Hz *= invH; + const qreal invA = 1.0 / std::sqrt(Ax * Ax + Ay * Ay + Az * Az); + Ax *= invA; + Ay *= invA; + Az *= invA; + const qreal My = Az * Hx - Ax * Hz; + qreal azimuth = std::atan2(Hy, My); + qreal accuracyValue = (m_accelerometerEvent.status + m_magneticEvent.status) / 6.0; + if (sensor()->skipDuplicates() && qFuzzyCompare(azimuth, m_reading.azimuth()) && + qFuzzyCompare(accuracyValue, m_reading.calibrationLevel())) { + return; + } + m_reading.setAzimuth(qRadiansToDegrees(azimuth)); + m_reading.setCalibrationLevel(accuracyValue); + newReadingAvailable(); + }}); +} + +int AndroidCompass::looperCallback(int, int, void *data) +{ + auto self = reinterpret_cast<AndroidCompass*>(data); + self->readAllEvents(); + return 1; // 1 means keep receiving events +} diff --git a/src/plugins/sensors/android/src/androidcompass.h b/src/plugins/sensors/android/androidcompass.h index 16a7c41..1d20de7 100644 --- a/src/plugins/sensors/android/src/androidcompass.h +++ b/src/plugins/sensors/android/androidcompass.h @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2019 BogDan Vatra <bogdan@kde.org> ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtSensors module of the Qt Toolkit. @@ -39,36 +39,34 @@ #ifndef ANDROIDCOMPASS_H #define ANDROIDCOMPASS_H -#include <qcompass.h> -#include "androidcommonsensor.h" +#include <QMutex> +#include <qcompass.h> -class AndroidAccelerometerListener; -class AndroidMagnetometerListener; +#include "sensoreventqueue.h" -class AndroidCompass : public QSensorBackend +class AndroidCompass : public ThreadSafeSensorBackend { Q_OBJECT public: - static char const * const id; - - AndroidCompass(QSensor *sensor); - ~AndroidCompass(); + AndroidCompass(QSensor *sensor, QObject *parent = nullptr); + ~AndroidCompass() override; void start() override; void stop() override; - private: - AndroidAccelerometerListener *m_accelerometerListener; - AndroidMagnetometerListener *m_magnetometerListener; + void readAllEvents(); + static int looperCallback(int /*fd*/, int /*events*/, void* data); +private: QCompassReading m_reading; - bool m_isStarted; - -public Q_SLOTS: - void testStuff(); - + const ASensor *m_accelerometer = nullptr; + const ASensor *m_magnetometer = nullptr; + ASensorEventQueue* m_sensorEventQueue = nullptr; + ASensorVector m_accelerometerEvent; + ASensorVector m_magneticEvent; + QMutex m_sensorsMutex; }; #endif // ANDROIDCOMPASS_H diff --git a/src/plugins/sensors/android/src/androidgyroscope.cpp b/src/plugins/sensors/android/androidgyroscope.cpp index c963761..2a38b5a 100644 --- a/src/plugins/sensors/android/src/androidgyroscope.cpp +++ b/src/plugins/sensors/android/androidgyroscope.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 BogDan Vatra <bogdan@kde.org> +** Copyright (C) 2019 BogDan Vatra <bogdan@kde.org> ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtSensors module of the Qt Toolkit. @@ -40,23 +40,25 @@ #include "androidgyroscope.h" #include <QtCore/qmath.h> -AndroidGyroscope::AndroidGyroscope(AndroidSensors::AndroidSensorType type, QSensor *sensor) - : AndroidCommonSensor<QGyroscopeReading>(type, sensor) +AndroidGyroscope::AndroidGyroscope(int type, QSensor *sensor, QObject *parent) + : SensorEventQueue<QGyroscopeReading>(type, sensor, parent) {} -void AndroidGyroscope::onSensorChanged(jlong timestamp, const jfloat *values, uint size) +void AndroidGyroscope::dataReceived(const ASensorEvent &event) { - if (size < 3) + // check https://developer.android.com/reference/android/hardware/SensorEvent.html#sensor.type_gyroscope: + const auto &vec = event.vector; + qreal x = qRadiansToDegrees(qreal(vec.x)); + qreal y = qRadiansToDegrees(qreal(vec.y)); + qreal z = qRadiansToDegrees(qreal(vec.z)); + if (sensor()->skipDuplicates() && qFuzzyCompare(m_reader.x(), x) && + qFuzzyCompare(m_reader.y(), y) && + qFuzzyCompare(m_reader.z(), z)) { return; - m_reader.setTimestamp(timestamp/1000); - // check https://developer.android.com/reference/android/hardware/SensorEvent.html#values - m_reader.setX(qRadiansToDegrees(values[0])); - m_reader.setY(qRadiansToDegrees(values[1])); - m_reader.setZ(qRadiansToDegrees(values[2])); + } + m_reader.setTimestamp(uint64_t(event.timestamp / 1000)); + m_reader.setX(x); + m_reader.setY(y); + m_reader.setZ(z); newReadingAvailable(); } - -void AndroidGyroscope::onAccuracyChanged(jint accuracy) -{ - Q_UNUSED(accuracy) -} diff --git a/src/plugins/sensors/android/src/androidgyroscope.h b/src/plugins/sensors/android/androidgyroscope.h index 9dd5629..7d1ecec 100644 --- a/src/plugins/sensors/android/src/androidgyroscope.h +++ b/src/plugins/sensors/android/androidgyroscope.h @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 BogDan Vatra <bogdan@kde.org> +** Copyright (C) 2019 BogDan Vatra <bogdan@kde.org> ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtSensors module of the Qt Toolkit. @@ -39,17 +39,19 @@ #ifndef ANDROIDGYROSCOPE_H #define ANDROIDGYROSCOPE_H + #include <qgyroscope.h> -#include "androidcommonsensor.h" +#include "sensoreventqueue.h" -class AndroidGyroscope : public AndroidCommonSensor<QGyroscopeReading> +class AndroidGyroscope : public SensorEventQueue<QGyroscopeReading> { public: - AndroidGyroscope(AndroidSensors::AndroidSensorType type, QSensor *sensor); -private: - void onAccuracyChanged(jint accuracy) override; - void onSensorChanged(jlong timestamp, const jfloat *values, uint size) override; + AndroidGyroscope(int type, QSensor *sensor, QObject *parent = nullptr); + +protected: + // SensorEventQueue interface + void dataReceived(const ASensorEvent &event) override; }; #endif // ANDROIDGYROSCOPE_H diff --git a/src/plugins/sensors/android/src/androidlight.cpp b/src/plugins/sensors/android/androidlight.cpp index 1059670..724ed97 100644 --- a/src/plugins/sensors/android/src/androidlight.cpp +++ b/src/plugins/sensors/android/androidlight.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 BogDan Vatra <bogdan@kde.org> +** Copyright (C) 2019 BogDan Vatra <bogdan@kde.org> ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtSensors module of the Qt Toolkit. @@ -39,22 +39,17 @@ #include "androidlight.h" -AndroidLight::AndroidLight(AndroidSensors::AndroidSensorType type, QSensor *sensor) - : AndroidCommonSensor<QLightReading>(type, sensor) +AndroidLight::AndroidLight(int type, QSensor *sensor, QObject *parent) + : SensorEventQueue<QLightReading>(type, sensor, parent) {} - -void AndroidLight::onAccuracyChanged(jint accuracy) +void AndroidLight::dataReceived(const ASensorEvent &event) { - Q_UNUSED(accuracy) -} - -void AndroidLight::onSensorChanged(jlong timestamp, const jfloat *values, uint size) -{ - if (size < 1) + // check https://developer.android.com/reference/android/hardware/SensorEvent.html#sensor.type_light: + if (sensor()->skipDuplicates() && qFuzzyCompare(m_reader.lux(), qreal(event.light))) return; - m_reader.setTimestamp(timestamp/1000); - // check https://developer.android.com/reference/android/hardware/SensorEvent.html#values - m_reader.setLux(values[0]); + + m_reader.setTimestamp(uint64_t(event.timestamp / 1000)); + m_reader.setLux(qreal(event.light)); newReadingAvailable(); } diff --git a/src/plugins/sensors/android/src/androidlight.h b/src/plugins/sensors/android/androidlight.h index 3c94c13..74c963f 100644 --- a/src/plugins/sensors/android/src/androidlight.h +++ b/src/plugins/sensors/android/androidlight.h @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 BogDan Vatra <bogdan@kde.org> +** Copyright (C) 2019 BogDan Vatra <bogdan@kde.org> ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtSensors module of the Qt Toolkit. @@ -39,17 +39,19 @@ #ifndef ANDROIDLIGHT_H #define ANDROIDLIGHT_H + #include <qlightsensor.h> -#include "androidcommonsensor.h" +#include "sensoreventqueue.h" -class AndroidLight : public AndroidCommonSensor<QLightReading> +class AndroidLight : public SensorEventQueue<QLightReading> { public: - AndroidLight(AndroidSensors::AndroidSensorType type, QSensor *sensor); -private: - void onAccuracyChanged(jint accuracy) override; - void onSensorChanged(jlong timestamp, const jfloat *values, uint size) override; + AndroidLight(int type, QSensor *sensor, QObject *parent = nullptr); + +protected: + // SensorEventQueue interface + void dataReceived(const ASensorEvent &event) override; }; #endif // ANDROIDLIGHT_H diff --git a/src/plugins/sensors/android/src/androidmagnetometer.cpp b/src/plugins/sensors/android/androidmagnetometer.cpp index fcde4cf..ec663c0 100644 --- a/src/plugins/sensors/android/src/androidmagnetometer.cpp +++ b/src/plugins/sensors/android/androidmagnetometer.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 BogDan Vatra <bogdan@kde.org> +** Copyright (C) 2019 BogDan Vatra <bogdan@kde.org> ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtSensors module of the Qt Toolkit. @@ -39,37 +39,29 @@ #include "androidmagnetometer.h" -enum AndroidSensorStatus -{ - SENSOR_STATUS_UNRELIABLE = 0, - SENSOR_STATUS_ACCURACY_LOW = 1, - SENSOR_STATUS_ACCURACY_MEDIUM = 2, - SENSOR_STATUS_ACCURACY_HIGH = 3, -}; - -AndroidMagnetometer::AndroidMagnetometer(AndroidSensors::AndroidSensorType type, QSensor *sensor) - :AndroidCommonSensor<QMagnetometerReading>(type, sensor) +AndroidMagnetometer::AndroidMagnetometer(int type, QSensor *sensor, QObject *parent) + : SensorEventQueue<QMagnetometerReading>(type, sensor, parent) {} -void AndroidMagnetometer::onAccuracyChanged(jint accuracy) +void AndroidMagnetometer::dataReceived(const ASensorEvent &event) { - // Expected range is [0, 3] - if (accuracy < SENSOR_STATUS_UNRELIABLE || accuracy > SENSOR_STATUS_ACCURACY_HIGH) { - qWarning("Unable to get sensor accuracy. Unexpected value: %d", accuracy); + const auto &mag = event.magnetic; + qreal accuracy = mag.status == ASENSOR_STATUS_NO_CONTACT ? 0 : mag.status / 3.0; + // check https://developer.android.com/reference/android/hardware/SensorEvent.html#sensor.type_magnetic_field: + // Android uses micro-Tesla, Qt uses Tesla + qreal x = qreal(mag.x) / 1e6; + qreal y = qreal(mag.y) / 1e6; + qreal z = qreal(mag.z) / 1e6; + if (sensor()->skipDuplicates() && qFuzzyCompare(accuracy, m_reader.calibrationLevel()) && + qFuzzyCompare(x, m_reader.x()) && + qFuzzyCompare(y, m_reader.y()) && + qFuzzyCompare(z, m_reader.z())) { return; } - - m_reader.setCalibrationLevel(accuracy / qreal(3.0)); -} - -void AndroidMagnetometer::onSensorChanged(jlong timestamp, const jfloat *values, uint size) -{ - if (size<3) - return; - m_reader.setTimestamp(timestamp/1000); - // check https://developer.android.com/reference/android/hardware/SensorEvent.html#values - m_reader.setX(values[0]/1e6); - m_reader.setY(values[1]/1e6); - m_reader.setZ(values[2]/1e6); + m_reader.setCalibrationLevel(accuracy); + m_reader.setTimestamp(uint64_t(event.timestamp / 1000)); + m_reader.setX(x); + m_reader.setY(y); + m_reader.setZ(z); newReadingAvailable(); } diff --git a/src/plugins/sensors/android/src/androidmagnetometer.h b/src/plugins/sensors/android/androidmagnetometer.h index 1b6a435..fb1f0ee 100644 --- a/src/plugins/sensors/android/src/androidmagnetometer.h +++ b/src/plugins/sensors/android/androidmagnetometer.h @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 BogDan Vatra <bogdan@kde.org> +** Copyright (C) 2019 BogDan Vatra <bogdan@kde.org> ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtSensors module of the Qt Toolkit. @@ -39,17 +39,19 @@ #ifndef ANDROIDMAGNETOMETER_H #define ANDROIDMAGNETOMETER_H + #include <qmagnetometer.h> -#include "androidcommonsensor.h" +#include "sensoreventqueue.h" -class AndroidMagnetometer : public AndroidCommonSensor<QMagnetometerReading> +class AndroidMagnetometer : public SensorEventQueue<QMagnetometerReading> { public: - AndroidMagnetometer(AndroidSensors::AndroidSensorType type, QSensor *sensor); -private: - void onAccuracyChanged(jint accuracy) override; - void onSensorChanged(jlong timestamp, const jfloat *values, uint size) override; + AndroidMagnetometer(int type, QSensor *sensor, QObject *parent = nullptr); + +protected: + // SensorEventQueue interface + void dataReceived(const ASensorEvent &event) override; }; #endif // ANDROIDMAGNETOMETER_H diff --git a/src/plugins/sensors/android/src/androidpressure.cpp b/src/plugins/sensors/android/androidpressure.cpp index 0c9a026..46acffd 100644 --- a/src/plugins/sensors/android/src/androidpressure.cpp +++ b/src/plugins/sensors/android/androidpressure.cpp @@ -1,7 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 BogDan Vatra <bogdan@kde.org> -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2019 BogDan Vatra <bogdan@kde.org> ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtSensors module of the Qt Toolkit. @@ -40,22 +39,18 @@ #include "androidpressure.h" -AndroidPressure::AndroidPressure(AndroidSensors::AndroidSensorType type, QSensor *sensor) - : AndroidCommonSensor<QPressureReading>(type, sensor) +AndroidPressure::AndroidPressure(int type, QSensor *sensor, QObject *parent) + : SensorEventQueue<QPressureReading>(type, sensor, parent) {} -void AndroidPressure::onAccuracyChanged(jint accuracy) +void AndroidPressure::dataReceived(const ASensorEvent &event) { - Q_UNUSED(accuracy) -} - -void AndroidPressure::onSensorChanged(jlong timestamp, const jfloat *values, uint size) -{ - if (size < 1) + // check https://developer.android.com/reference/android/hardware/SensorEvent.html#sensor.type_pressure: + auto pressurePa = qreal(event.pressure) * 100; + if (sensor()->skipDuplicates() && qFuzzyCompare(pressurePa, m_reader.pressure())) return; - m_reader.setTimestamp(timestamp/1000); - // check https://developer.android.com/reference/android/hardware/SensorEvent.html#values - m_reader.setPressure(values[0]*100); //Android uses hPa, we use Pa + m_reader.setTimestamp(uint64_t(event.timestamp / 1000)); + m_reader.setPressure(pressurePa); //Android uses hPa, we use Pa newReadingAvailable(); } diff --git a/src/plugins/sensors/android/src/androidpressure.h b/src/plugins/sensors/android/androidpressure.h index dbb59c1..ec682e5 100644 --- a/src/plugins/sensors/android/src/androidpressure.h +++ b/src/plugins/sensors/android/androidpressure.h @@ -1,7 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 BogDan Vatra <bogdan@kde.org> -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2019 BogDan Vatra <bogdan@kde.org> ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtSensors module of the Qt Toolkit. @@ -40,17 +39,19 @@ #ifndef ANDROIDPRESSURE_H #define ANDROIDPRESSURE_H + #include <qpressuresensor.h> -#include "androidcommonsensor.h" +#include "sensoreventqueue.h" -class AndroidPressure : public AndroidCommonSensor<QPressureReading> +class AndroidPressure : public SensorEventQueue<QPressureReading> { public: - AndroidPressure(AndroidSensors::AndroidSensorType type, QSensor *sensor); -private: - void onAccuracyChanged(jint accuracy) override; - void onSensorChanged(jlong timestamp, const jfloat *values, uint size) override; + AndroidPressure(int type, QSensor *sensor, QObject *parent = nullptr); + +protected: + // SensorEventQueue interface + void dataReceived(const ASensorEvent &event) override; }; #endif // ANDROIDPRESSURE_H diff --git a/src/plugins/sensors/android/src/androidproximity.cpp b/src/plugins/sensors/android/androidproximity.cpp index 27c5891..000399b 100644 --- a/src/plugins/sensors/android/src/androidproximity.cpp +++ b/src/plugins/sensors/android/androidproximity.cpp @@ -39,12 +39,11 @@ ****************************************************************************/ #include "androidproximity.h" -#include "androidjnisensors.h" -AndroidProximity::AndroidProximity(AndroidSensors::AndroidSensorType type, QSensor *sensor) - : AndroidCommonSensor<QProximityReading>(type, sensor) +AndroidProximity::AndroidProximity(int type, QSensor *sensor, QObject *parent) + : SensorEventQueue<QProximityReading>(type, sensor, parent) { - m_maximumRange = AndroidSensors::sensorMaximumRange(type); + m_maximumRange = m_sensorManager->getMaximumRange(m_sensor); // if we can't get the range, we arbitrarily define anything closer than 10 cm as "close" if (m_maximumRange <= 0) @@ -52,19 +51,13 @@ AndroidProximity::AndroidProximity(AndroidSensors::AndroidSensorType type, QSens } -void AndroidProximity::onAccuracyChanged(jint accuracy) +void AndroidProximity::dataReceived(const ASensorEvent &event) { - Q_UNUSED(accuracy) -} - -void AndroidProximity::onSensorChanged(jlong timestamp, const jfloat *values, uint size) -{ - if (size < 1) + // https://developer.android.com/reference/android/hardware/SensorEvent.html#sensor.type_proximity: + bool close = qreal(event.distance) < m_maximumRange; + if (sensor()->skipDuplicates() && close == m_reader.close()) return; - m_reader.setTimestamp(timestamp/1000); - - qreal reading = values[0]; - bool close = (reading < m_maximumRange); + m_reader.setTimestamp(uint64_t(event.timestamp / 1000)); m_reader.setClose(close); newReadingAvailable(); } diff --git a/src/plugins/sensors/android/src/androidproximity.h b/src/plugins/sensors/android/androidproximity.h index 5a3110c..22169d6 100644 --- a/src/plugins/sensors/android/src/androidproximity.h +++ b/src/plugins/sensors/android/androidproximity.h @@ -42,15 +42,18 @@ #define ANDROIDPROXIMITY_H #include <qproximitysensor.h> -#include "androidcommonsensor.h" +#include "sensoreventqueue.h" -class AndroidProximity : public AndroidCommonSensor<QProximityReading> +class AndroidProximity : public SensorEventQueue<QProximityReading> { public: - AndroidProximity(AndroidSensors::AndroidSensorType type, QSensor *sensor); + AndroidProximity(int type, QSensor *sensor, QObject *parent = nullptr); + +protected: + // SensorEventQueue interface + void dataReceived(const ASensorEvent &event) override; + private: - void onAccuracyChanged(jint accuracy) override; - void onSensorChanged(jlong timestamp, const jfloat *values, uint size) override; qreal m_maximumRange; }; diff --git a/src/plugins/sensors/android/androidrotation.cpp b/src/plugins/sensors/android/androidrotation.cpp new file mode 100644 index 0000000..c1d141a --- /dev/null +++ b/src/plugins/sensors/android/androidrotation.cpp @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2019 BogDan Vatra <bogdan@kde.org> +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "androidrotation.h" + +#include <QtCore/qmath.h> + +AndroidRotation::AndroidRotation(int type, QSensor *sensor, QObject *parent) + : SensorEventQueue<QRotationReading>(type, sensor, parent) +{} + + +void AndroidRotation::dataReceived(const ASensorEvent &event) +{ + // ### Check me, at first look it seems wrong, + // here https://developer.android.com/reference/android/hardware/SensorEvent.html#sensor.type_rotation_vector: + // are the Android values + qreal rz = -qRadiansToDegrees(qreal(event.data[0])); // event.data[0] corresponds to x + qreal rx = -qRadiansToDegrees(qreal(event.data[1])); // event.data[1] corresponds to y + qreal ry = qRadiansToDegrees(qreal(event.data[2])); // event.data[2] corresponds to z + if (sensor()->skipDuplicates() && qFuzzyCompare(m_reader.x(), rx) && + qFuzzyCompare(m_reader.y(), ry) && + qFuzzyCompare(m_reader.z(), rz)) { + return; + } + m_reader.setTimestamp(uint64_t(event.timestamp / 1000)); + m_reader.setFromEuler(rx, ry, rz); + newReadingAvailable(); +} diff --git a/src/plugins/sensors/android/src/androidrotation.h b/src/plugins/sensors/android/androidrotation.h index 0fe90a6..9761b24 100644 --- a/src/plugins/sensors/android/src/androidrotation.h +++ b/src/plugins/sensors/android/androidrotation.h @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 BogDan Vatra <bogdan@kde.org> +** Copyright (C) 2019 BogDan Vatra <bogdan@kde.org> ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtSensors module of the Qt Toolkit. @@ -39,16 +39,19 @@ #ifndef ANDROIDROTATION_H #define ANDROIDROTATION_H + #include <qrotationsensor.h> -#include "androidcommonsensor.h" -class AndroidRotation : public AndroidCommonSensor<QRotationReading> +#include "sensoreventqueue.h" + +class AndroidRotation : public SensorEventQueue<QRotationReading> { public: - AndroidRotation(AndroidSensors::AndroidSensorType type, QSensor *sensor); -private: - void onAccuracyChanged(jint accuracy) override; - void onSensorChanged(jlong timestamp, const jfloat *values, uint size) override; + AndroidRotation(int type, QSensor *sensor, QObject *parent = nullptr); + +protected: + // SensorEventQueue interface + void dataReceived(const ASensorEvent &event) override; }; #endif // ANDROIDROTATION_H diff --git a/src/plugins/sensors/android/src/androidrotation.cpp b/src/plugins/sensors/android/androidtemperature.cpp index f7d0225..5f85750 100644 --- a/src/plugins/sensors/android/src/androidrotation.cpp +++ b/src/plugins/sensors/android/androidtemperature.cpp @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 BogDan Vatra <bogdan@kde.org> +** Copyright (C) 2019 BogDan Vatra <bogdan@kde.org> ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtSensors module of the Qt Toolkit. @@ -37,27 +37,18 @@ ** ****************************************************************************/ -#include "androidrotation.h" -#include <QtCore/qmath.h> +#include "androidtemperature.h" -AndroidRotation::AndroidRotation(AndroidSensors::AndroidSensorType type, QSensor *sensor) - : AndroidCommonSensor<QRotationReading>(type, sensor) +AndroidTemperature::AndroidTemperature(int type, QSensor *sensor, QObject *parent) + : SensorEventQueue<QAmbientTemperatureReading>(type, sensor, parent) {} -void AndroidRotation::onAccuracyChanged(jint accuracy) +void AndroidTemperature::dataReceived(const ASensorEvent &event) { - Q_UNUSED(accuracy) -} - -void AndroidRotation::onSensorChanged(jlong timestamp, const jfloat *values, uint size) -{ - if (size < 3) + if (sensor()->skipDuplicates() && qFuzzyCompare(m_reader.temperature(), qreal(event.temperature))) return; - m_reader.setTimestamp(timestamp/1000); - - float rz = -qRadiansToDegrees(values[0]); - float rx = -qRadiansToDegrees(values[1]); - float ry = qRadiansToDegrees(values[2]); - m_reader.setFromEuler(rx, ry, rz); + m_reader.setTimestamp(uint64_t(event.timestamp / 1000)); + // https://developer.android.com/reference/android/hardware/SensorEvent.html#sensor.type_ambient_temperature: + m_reader.setTemperature(qreal(event.temperature)); // in degree Celsius newReadingAvailable(); } diff --git a/src/plugins/sensors/android/src/androidtemperature.h b/src/plugins/sensors/android/androidtemperature.h index 1640033..ba62605 100644 --- a/src/plugins/sensors/android/src/androidtemperature.h +++ b/src/plugins/sensors/android/androidtemperature.h @@ -1,7 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 BogDan Vatra <bogdan@kde.org> -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2019 BogDan Vatra <bogdan@kde.org> ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtSensors module of the Qt Toolkit. @@ -37,20 +36,21 @@ ** $QT_END_LICENSE$ ** ****************************************************************************/ - #ifndef ANDROIDTEMPERATURE_H #define ANDROIDTEMPERATURE_H + #include <qambienttemperaturesensor.h> -#include "androidcommonsensor.h" +#include "sensoreventqueue.h" -class AndroidTemperature : public AndroidCommonSensor<QAmbientTemperatureReading> +class AndroidTemperature : public SensorEventQueue<QAmbientTemperatureReading> { public: - AndroidTemperature(AndroidSensors::AndroidSensorType type, QSensor *sensor); -private: - void onAccuracyChanged(jint accuracy) override; - void onSensorChanged(jlong timestamp, const jfloat *values, uint size) override; + AndroidTemperature(int type, QSensor *sensor, QObject *parent = nullptr); + +protected: + // SensorEventQueue interface + void dataReceived(const ASensorEvent &event) override; }; #endif // ANDROIDTEMPERATURE_H diff --git a/src/plugins/sensors/android/jar/AndroidManifest.xml b/src/plugins/sensors/android/jar/AndroidManifest.xml deleted file mode 100644 index 5d15cab..0000000 --- a/src/plugins/sensors/android/jar/AndroidManifest.xml +++ /dev/null @@ -1,6 +0,0 @@ -<manifest xmlns:android="http://schemas.android.com/apk/res/android" - package="org.qtproject.qt5.android.sensors" - android:versionCode="1" - android:versionName="1.0" > - <supports-screens android:largeScreens="true" android:normalScreens="true" android:anyDensity="true" android:smallScreens="true"/> -</manifest> diff --git a/src/plugins/sensors/android/jar/jar.pro b/src/plugins/sensors/android/jar/jar.pro deleted file mode 100644 index 1d5d4ae..0000000 --- a/src/plugins/sensors/android/jar/jar.pro +++ /dev/null @@ -1,15 +0,0 @@ -TARGET = QtSensors - -load(qt_build_paths) - -CONFIG += java -DESTDIR = $$MODULE_BASE_OUTDIR/jar - -JAVACLASSPATH += $$PWD/src - -JAVASOURCES += \ - $$PWD/src/org/qtproject/qt5/android/sensors/QtSensors.java - -# install -target.path = $$[QT_INSTALL_PREFIX]/jar -INSTALLS += target diff --git a/src/plugins/sensors/android/jar/src/org/qtproject/qt5/android/sensors/QtSensors.java b/src/plugins/sensors/android/jar/src/org/qtproject/qt5/android/sensors/QtSensors.java deleted file mode 100644 index 3067d40..0000000 --- a/src/plugins/sensors/android/jar/src/org/qtproject/qt5/android/sensors/QtSensors.java +++ /dev/null @@ -1,192 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 BogDan Vatra <bogdan@kde.org> -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtSensor module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -package org.qtproject.qt5.android.sensors; - -import java.util.HashSet; -import java.util.List; - -import android.content.Context; -import android.hardware.Sensor; -import android.hardware.SensorEvent; -import android.hardware.SensorEventListener; -import android.hardware.SensorManager; -import android.os.Build; -import android.util.SparseArray; - -import android.util.Log; - -public class QtSensors implements SensorEventListener -{ - static final QtSensors m_sensorsListener = new QtSensors(); - static SensorManager m_sensorManager = null; - static SparseArray<Sensor> m_registeredSensors = new SparseArray<Sensor>(); - static Object m_syncObject = new Object(); - static public void setContext(Context context) - { - try { - m_sensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE); - } catch(Exception e) { - e.printStackTrace(); - } - } - - private static String getSensorDescription(int sensorType) - { - try { - Sensor s = m_sensorManager.getDefaultSensor(sensorType); - if (s == null) { - return null; - } - return s.getName() + " " + s.getVendor() + " v" + s.getVersion(); - } catch(Exception e) { - e.printStackTrace(); - } - return null; - } - - private static int[] getSensorList() - { - try { - List<Sensor> list = m_sensorManager.getSensorList(Sensor.TYPE_ALL); - HashSet<Integer> filteredList = new HashSet<Integer>(); - for (Sensor s : list) - filteredList.add(s.getType()); - int retList[] = new int[filteredList.size()]; - int pos = 0; - for (int type : filteredList) - retList[pos++] = type; - return retList; - } catch(Exception e) { - e.printStackTrace(); - } - return null; - } - - private static float getSensorMaximumRange(int sensorType) - { - try { - Sensor s = m_sensorManager.getDefaultSensor(sensorType); - return s.getMaximumRange(); - } catch(Exception e) { - e.printStackTrace(); - } - return 0; - } - - private static boolean registerSensor(int sensorType, int rate) - { - synchronized (m_syncObject) { - try { - Sensor s = m_sensorManager.getDefaultSensor(sensorType); - m_sensorManager.registerListener(m_sensorsListener, s, rate); - m_registeredSensors.put(sensorType, s); - } catch(Exception e) { - e.printStackTrace(); - return false; - } - } - return true; - } - - private static boolean unregisterSensor(int sensorType) - { - synchronized (m_syncObject) { - try { - Sensor s = m_registeredSensors.get(sensorType); - if (s != null) { - m_sensorManager.unregisterListener(m_sensorsListener, m_registeredSensors.get(sensorType)); - m_registeredSensors.remove(sensorType); - } - } catch (Exception e) { - e.printStackTrace(); - return false; - } - } - return true; - } - - private static float[] convertQuaternionToEuler(float[] rotationVector) - { - float matrix[] = new float[9]; - SensorManager.getRotationMatrixFromVector (matrix, rotationVector); - float angles[] = new float[3]; - SensorManager.getOrientation (matrix, angles); - return angles; - } - - private static float[] mRotation = new float[9]; - private static float[] mOrientation = new float[3]; - private static float[] mAcc = new float[3]; - private static float[] mMag = new float[3]; - - private static float getCompassAzimuth(float a0, float a1, float a2, float m0, float m1, float m2) - { - mAcc[0] = a0; - mAcc[1] = a1; - mAcc[2] = a2; - mMag[0] = m0; - mMag[1] = m1; - mMag[2] = m2; - - SensorManager.getRotationMatrix(mRotation, null, mAcc, mMag); - SensorManager.getOrientation(mRotation, mOrientation); - return mOrientation[0]; - } - - public static native void accuracyChanged(int sensorType, int accuracy); - public static native void sensorChanged(int sensorType, long timestamp, float[] values); - - @Override - public void onAccuracyChanged(Sensor sensor, int accuracy) - { - accuracyChanged(sensor.getType(), accuracy); - } - - @Override - public void onSensorChanged(SensorEvent sensorEvent) - { - if (sensorEvent.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) { - //#### hacky, but much easier than exposing the convert function and converting the arrays back and forth... - sensorChanged(sensorEvent.sensor.getType(), sensorEvent.timestamp, convertQuaternionToEuler(sensorEvent.values)); - } else { - sensorChanged(sensorEvent.sensor.getType(), sensorEvent.timestamp, sensorEvent.values); - } - } -} diff --git a/src/plugins/sensors/android/src/main.cpp b/src/plugins/sensors/android/main.cpp index fc80a27..85853cd 100644 --- a/src/plugins/sensors/android/src/main.cpp +++ b/src/plugins/sensors/android/main.cpp @@ -53,7 +53,12 @@ #include "androidrotation.h" #include "androidtemperature.h" -using namespace AndroidSensors; +#include "sensormanager.h" +#include <android/sensor.h> + +namespace { + const char AndroidCompassId[] = "android.synthetic.compass"; +} class AndroidSensorPlugin : public QObject, public QSensorPluginInterface, public QSensorBackendFactory { @@ -65,101 +70,95 @@ public: { bool accelerometer = false; bool magnetometer = false; - foreach (AndroidSensorType sensor, availableSensors()) { + ASensorList availableSensors; + int count = ASensorManager_getSensorList(SensorManager::instance()->manager(), &availableSensors); + for (int i = 0; i < count; i++) { + int sensor = ASensor_getType(availableSensors[i]); switch (sensor) { - case TYPE_ACCELEROMETER: + case ASENSOR_TYPE_ACCELEROMETER: + m_accelerationModes |= AndroidAccelerometer::Accelerometer; QSensorManager::registerBackend(QAccelerometer::type, QByteArray::number(sensor), this); accelerometer = true; break; - case TYPE_AMBIENT_TEMPERATURE: - case TYPE_TEMPERATURE: + case ASENSOR_TYPE_GRAVITY: + m_accelerationModes |= AndroidAccelerometer::Gravity; + break; + case ASENSOR_TYPE_LINEAR_ACCELERATION: + m_accelerationModes |= AndroidAccelerometer::LinearAcceleration; + break; + case ASENSOR_TYPE_AMBIENT_TEMPERATURE: QSensorManager::registerBackend(QAmbientTemperatureSensor::type, QByteArray::number(sensor), this); break; - case TYPE_GRAVITY: - break; // add the gravity sensor backend - case TYPE_GYROSCOPE: + case ASENSOR_TYPE_GYROSCOPE: QSensorManager::registerBackend(QGyroscope::type, QByteArray::number(sensor), this); break; - case TYPE_LIGHT: + case ASENSOR_TYPE_LIGHT: QSensorManager::registerBackend(QLightSensor::type, QByteArray::number(sensor), this); - break; // add the light sensor backend - case TYPE_LINEAR_ACCELERATION: - break; // add the linear acceleration sensor backend - case TYPE_MAGNETIC_FIELD: + break; + case ASENSOR_TYPE_MAGNETIC_FIELD: QSensorManager::registerBackend(QMagnetometer::type, QByteArray::number(sensor), this); magnetometer = true; break; - case TYPE_ORIENTATION: - break; // add the orientation sensor backend - case TYPE_PRESSURE: + case ASENSOR_TYPE_PRESSURE: QSensorManager::registerBackend(QPressureSensor::type, QByteArray::number(sensor), this); break; - case TYPE_PROXIMITY: + case ASENSOR_TYPE_PROXIMITY: QSensorManager::registerBackend(QProximitySensor::type, QByteArray::number(sensor), this); break; - case TYPE_RELATIVE_HUMIDITY: - break; // add the relative humidity sensor backend - case TYPE_ROTATION_VECTOR: + case ASENSOR_TYPE_ROTATION_VECTOR: QSensorManager::registerBackend(QRotationSensor::type, QByteArray::number(sensor), this); break; - case TYPE_GAME_ROTATION_VECTOR: - case TYPE_GYROSCOPE_UNCALIBRATED: - case TYPE_MAGNETIC_FIELD_UNCALIBRATED: - case TYPE_SIGNIFICANT_MOTION: - break; // add backends for API level 18 sensors + case ASENSOR_TYPE_RELATIVE_HUMIDITY: + case ASENSOR_TYPE_MAGNETIC_FIELD_UNCALIBRATED: + case ASENSOR_TYPE_GAME_ROTATION_VECTOR: + case ASENSOR_TYPE_GYROSCOPE_UNCALIBRATED: + case ASENSOR_TYPE_SIGNIFICANT_MOTION: + case ASENSOR_TYPE_STEP_DETECTOR: + case ASENSOR_TYPE_STEP_COUNTER: + case ASENSOR_TYPE_GEOMAGNETIC_ROTATION_VECTOR: + case ASENSOR_TYPE_HEART_RATE: + case ASENSOR_TYPE_POSE_6DOF: + case ASENSOR_TYPE_STATIONARY_DETECT: + case ASENSOR_TYPE_MOTION_DETECT: + case ASENSOR_TYPE_HEART_BEAT: + case ASENSOR_TYPE_LOW_LATENCY_OFFBODY_DETECT: + case ASENSOR_TYPE_ACCELEROMETER_UNCALIBRATED: + break; // ### TODO add backends for missing Android sensors } } if (accelerometer && magnetometer) - QSensorManager::registerBackend(QCompass::type, AndroidCompass::id, this); + QSensorManager::registerBackend(QCompass::type, AndroidCompassId, this); } QSensorBackend *createBackend(QSensor *sensor) override { - if (sensor->identifier() == AndroidCompass::id) + if (sensor->identifier() == AndroidCompassId) return new AndroidCompass(sensor); - AndroidSensorType type = static_cast<AndroidSensorType>(sensor->identifier().toInt()); + int type = sensor->identifier().toInt(); switch (type) { - case TYPE_ACCELEROMETER: { - QAccelerometer * const accelerometer = qobject_cast<QAccelerometer *>(sensor); - AndroidSensors::AndroidSensorType type - = accelerometer ? AndroidAccelerometer::modeToSensor(accelerometer->accelerationMode()) - : AndroidSensors::TYPE_ACCELEROMETER; - return new AndroidAccelerometer(type, sensor); - } - case TYPE_AMBIENT_TEMPERATURE: - case TYPE_TEMPERATURE: + case ASENSOR_TYPE_ACCELEROMETER: + return new AndroidAccelerometer(m_accelerationModes, sensor); + case ASENSOR_TYPE_AMBIENT_TEMPERATURE: return new AndroidTemperature(type, sensor); - case TYPE_GRAVITY: - break; // add the gravity sensor backend - case TYPE_GYROSCOPE: + case ASENSOR_TYPE_GYROSCOPE: return new AndroidGyroscope(type, sensor); - case TYPE_LIGHT: + case ASENSOR_TYPE_LIGHT: return new AndroidLight(type, sensor); - case TYPE_LINEAR_ACCELERATION: - break; // add the linear acceleration sensor backend - case TYPE_MAGNETIC_FIELD: + case ASENSOR_TYPE_MAGNETIC_FIELD: return new AndroidMagnetometer(type, sensor); - case TYPE_ORIENTATION: - break; // add the orientation sensor backend - case TYPE_PRESSURE: + case ASENSOR_TYPE_PRESSURE: return new AndroidPressure(type, sensor); - case TYPE_PROXIMITY: + case ASENSOR_TYPE_PROXIMITY: return new AndroidProximity(type, sensor); - case TYPE_RELATIVE_HUMIDITY: - break; // add the relative humidity sensor backend - case TYPE_ROTATION_VECTOR: + case ASENSOR_TYPE_ROTATION_VECTOR: return new AndroidRotation(type, sensor); - - case TYPE_GAME_ROTATION_VECTOR: - case TYPE_GYROSCOPE_UNCALIBRATED: - case TYPE_MAGNETIC_FIELD_UNCALIBRATED: - case TYPE_SIGNIFICANT_MOTION: - break; // add backends for API level 18 sensors } - return 0; + return nullptr; } +private: + int m_accelerationModes = 0; }; Q_IMPORT_PLUGIN (AndroidSensorPlugin) // automatically register the plugin diff --git a/src/plugins/sensors/android/src/plugin.json b/src/plugins/sensors/android/plugin.json index ef0cbb6..ef0cbb6 100644 --- a/src/plugins/sensors/android/src/plugin.json +++ b/src/plugins/sensors/android/plugin.json diff --git a/src/plugins/sensors/android/sensoreventqueue.h b/src/plugins/sensors/android/sensoreventqueue.h new file mode 100644 index 0000000..77bf05c --- /dev/null +++ b/src/plugins/sensors/android/sensoreventqueue.h @@ -0,0 +1,166 @@ +/**************************************************************************** +** +** Copyright (C) 2019 BogDan Vatra <bogdan@kde.org> +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef SENSOREVENTQUEUE_H +#define SENSOREVENTQUEUE_H + +#include "sensormanager.h" + +#include <functional> + +#include <QCoreApplication> +#include <QEvent> +#include <QSensorBackend> + +class FunctionEvent : public QEvent +{ +public: + using Function = std::function<void()>; + enum { + FunctionEventType = User + 1 + }; + explicit FunctionEvent(const Function &function) + : QEvent(Type(FunctionEventType)) + , m_function(function) + {} + + void callFunction() const + { + m_function(); + } +private: + Function m_function; +}; + +class ThreadSafeSensorBackend : public QSensorBackend +{ +public: + ThreadSafeSensorBackend(QSensor *sensor, QObject *parent = nullptr) + : QSensorBackend(sensor, parent) + , m_sensorManager(SensorManager::instance()) + {} + + // QObject interface + bool event(QEvent *event) override + { + if (event->type() == FunctionEvent::FunctionEventType) { + static_cast<FunctionEvent*>(event)->callFunction(); + event->accept(); + return true; + } + return QSensorBackend::event(event); + } +protected: + QSharedPointer<SensorManager> m_sensorManager; +}; + + + +template <typename T> +class SensorEventQueue : public ThreadSafeSensorBackend +{ +public: + explicit SensorEventQueue(int androidSensorType, QSensor *sensor, QObject *parent = nullptr) + : ThreadSafeSensorBackend(sensor, parent) + { + setReading<T>(&m_reader); + m_sensorEventQueue = ASensorManager_createEventQueue(m_sensorManager->manager(), m_sensorManager->looper(), -1, &looperCallback, this); + setSensorType(androidSensorType); + } + + ~SensorEventQueue() override + { + stop(); + ASensorManager_destroyEventQueue(m_sensorManager->manager(), m_sensorEventQueue); + } + + void setSensorType(int type) + { + bool started = m_started; + if (started) + stop(); + m_sensor = ASensorManager_getDefaultSensor(m_sensorManager->manager(), type); + setDescription(m_sensorManager->description(m_sensor)); + if (started) + start(); + } + + // QSensorBackend interface + void start() override + { + ASensorEventQueue_enableSensor(m_sensorEventQueue, m_sensor); + if (sensor()->dataRate() > 0) + ASensorEventQueue_setEventRate(m_sensorEventQueue, m_sensor, std::max(ASensor_getMinDelay(m_sensor), sensor()->dataRate())); + m_started = true; + } + void stop() override + { + ASensorEventQueue_disableSensor(m_sensorEventQueue, m_sensor); + m_started = false; + } + bool isFeatureSupported(QSensor::Feature feature) const override + { + switch (feature) { + case QSensor::SkipDuplicates: + return true; + default: + return false; + } + } + +protected: + virtual void dataReceived(const ASensorEvent &event) = 0; + static int looperCallback(int /*fd*/, int /*events*/, void* data) + { + ASensorEvent sensorEvent; + auto self = reinterpret_cast<SensorEventQueue*>(data); + while (ASensorEventQueue_getEvents(self->m_sensorEventQueue, &sensorEvent, 1)) + QCoreApplication::postEvent(self, new FunctionEvent{[=]{self->dataReceived(sensorEvent);}}); + + return 1; // 1 means keep receiving events + } + +protected: + T m_reader; + const ASensor *m_sensor = nullptr; + ASensorEventQueue* m_sensorEventQueue = nullptr; + bool m_started = false; +}; + +#endif // SENSOREVENTQUEUE_H diff --git a/src/plugins/sensors/android/sensormanager.cpp b/src/plugins/sensors/android/sensormanager.cpp new file mode 100644 index 0000000..590f478 --- /dev/null +++ b/src/plugins/sensors/android/sensormanager.cpp @@ -0,0 +1,120 @@ +/**************************************************************************** +** +** Copyright (C) 2019 BogDan Vatra <bogdan@kde.org> +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include "sensormanager.h" +#include <dlfcn.h> + +SensorManager::SensorManager() +{ + auto sensorService = QJNIObjectPrivate::getStaticObjectField("android.content.Context", "SENSOR_SERVICE", "Ljava/lang/String;"); + m_sensorManager = QJNIObjectPrivate{QtAndroidPrivate::context()}.callObjectMethod("getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;", sensorService.object()); + setObjectName("QtSensorsLooperThread"); + start(); + m_waitForStart.acquire(); +} + +SensorManager::~SensorManager() +{ + m_quit.store(1); + wait(); +} + +QJNIObjectPrivate SensorManager::javaSensor(const ASensor *sensor) const +{ + return m_sensorManager.callObjectMethod("getDefaultSensor", "(I)Landroid/hardware/Sensor;", ASensor_getType(sensor)); +} + +QSharedPointer<SensorManager> &SensorManager::instance() +{ + static QSharedPointer<SensorManager> looper{new SensorManager}; + return looper; +} + +ALooper *SensorManager::looper() const +{ + return m_looper; +} + +static inline ASensorManager* androidManager() +{ +#if __ANDROID_API__ >= 26 + retrun ASensorManager_getInstanceForPackage(QJNIObjectPrivate{QtAndroidPrivate::context()} + .callObjectMethod("getPackageName", "()Ljava/lang/String;") + .toString().toUtf8().constData()); +#else + if (QtAndroidPrivate::androidSdkVersion() >= 26) { + using GetInstanceForPackage = ASensorManager *(*)(const char *); + auto handler = dlopen("libandroid.so", RTLD_NOW); + auto function = GetInstanceForPackage(dlsym(handler, "ASensorManager_getInstanceForPackage")); + if (function) { + auto res = function(QJNIObjectPrivate{QtAndroidPrivate::context()} + .callObjectMethod("getPackageName", "()Ljava/lang/String;") + .toString().toUtf8().constData()); + dlclose(handler); + return res; + } + dlclose(handler); + } + return ASensorManager_getInstance(); +#endif +} +ASensorManager *SensorManager::manager() const +{ + static auto sensorManger = androidManager(); + return sensorManger; +} + +QString SensorManager::description(const ASensor *sensor) const +{ + return QString::fromUtf8(ASensor_getName(sensor)) + " " + ASensor_getVendor(sensor) + " v" + QString::number(javaSensor(sensor).callMethod<jint>("getVersion")); +} + +double SensorManager::getMaximumRange(const ASensor *sensor) const +{ + return qreal(javaSensor(sensor).callMethod<jfloat>("getMaximumRange")); +} + +void SensorManager::run() +{ + m_looper = ALooper_prepare(0); + m_waitForStart.release(); + do { + if (ALooper_pollAll(5 /*ms*/, nullptr, nullptr, nullptr) == ALOOPER_POLL_TIMEOUT) + QThread::yieldCurrentThread(); + } while (!m_quit.load()); +} diff --git a/src/plugins/sensors/android/sensormanager.h b/src/plugins/sensors/android/sensormanager.h new file mode 100644 index 0000000..dc7ffe1 --- /dev/null +++ b/src/plugins/sensors/android/sensormanager.h @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** Copyright (C) 2019 BogDan Vatra <bogdan@kde.org> +** Contact: https://www.qt.io/licensing/ +** +** This file is part of the QtSensors module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see https://www.qt.io/terms-conditions. For further +** information use the contact form at https://www.qt.io/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 3 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL3 included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 3 requirements +** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 2.0 or (at your option) the GNU General +** Public license version 3 or any later version approved by the KDE Free +** Qt Foundation. The licenses are as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 +** included in the packaging of this file. Please review the following +** information to ensure the GNU General Public License requirements will +** be met: https://www.gnu.org/licenses/gpl-2.0.html and +** https://www.gnu.org/licenses/gpl-3.0.html. +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef SENSORMANAGER_H +#define SENSORMANAGER_H + +#include <QThread> +#include <QSemaphore> +#include <QMutex> +#include <QWaitCondition> + +#include <private/qjni_p.h> +#include <private/qjnihelpers_p.h> + +#include <android/sensor.h> + +class SensorManager : public QThread +{ +public: + ~SensorManager() override; + static QSharedPointer<SensorManager> &instance(); + ALooper *looper() const; + ASensorManager *manager() const; + + QJNIObjectPrivate javaSensor(const ASensor *sensor) const; + QString description(const ASensor *sensor) const; + double getMaximumRange(const ASensor *sensor) const; + +private: + SensorManager(); + // QThread interface + void run() override; + +private: + QAtomicInt m_quit{0}; + ALooper *m_looper = nullptr; + QSemaphore m_waitForStart; + QJNIObjectPrivate m_sensorManager; +}; + +#endif // SENSORMANAGER_H diff --git a/src/plugins/sensors/android/src/androidcommonsensor.h b/src/plugins/sensors/android/src/androidcommonsensor.h deleted file mode 100644 index bfd36db..0000000 --- a/src/plugins/sensors/android/src/androidcommonsensor.h +++ /dev/null @@ -1,86 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 BogDan Vatra <bogdan@kde.org> -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtSensors module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef ANDROIDCOMMONSENSOR_H -#define ANDROIDCOMMONSENSOR_H - -#include <qsensorbackend.h> -#include <qsensor.h> -#include "androidjnisensors.h" - -template <typename ReaderType> -class AndroidCommonSensor : public QSensorBackend, protected AndroidSensors::AndroidSensorsListenerInterface -{ -public: - AndroidCommonSensor(AndroidSensors::AndroidSensorType type, QSensor *sensor) : QSensorBackend(sensor) - { - setDescription(AndroidSensors::sensorDescription(type)); - setReading<ReaderType>(&m_reader); - m_type = type; - m_isStarted = false; - } - - virtual ~AndroidCommonSensor() - { - if (m_isStarted) - stop(); - } - void start() override - { - if (AndroidSensors::registerListener(m_type, this, sensor()->dataRate())) - m_isStarted = true; - } - - void stop() override - { - if (m_isStarted) { - m_isStarted = false; - AndroidSensors::unregisterListener(m_type, this); - } - } - -protected: - ReaderType m_reader; - AndroidSensors::AndroidSensorType m_type; - -private: - bool m_isStarted; -}; - -#endif // ANDROIDCOMMONSENSOR_H diff --git a/src/plugins/sensors/android/src/androidcompass.cpp b/src/plugins/sensors/android/src/androidcompass.cpp deleted file mode 100644 index 0911d90..0000000 --- a/src/plugins/sensors/android/src/androidcompass.cpp +++ /dev/null @@ -1,177 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtSensors module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "androidcompass.h" - -#include <QDebug> -#include <qmath.h> -#include "androidjnisensors.h" - - -class AndroidAccelerometerListener : public AndroidSensors::AndroidSensorsListenerInterface -{ -public: - - AndroidAccelerometerListener(AndroidCompass *parent) - : accuracy(0), m_compass(parent) - { - memset(reading, 0, sizeof(reading)); - } - - void start(int dataRate) - { - AndroidSensors::registerListener(AndroidSensors::TYPE_ACCELEROMETER, this, dataRate); - } - - void stop() - { - AndroidSensors::unregisterListener(AndroidSensors::TYPE_ACCELEROMETER, this); - } - - void onAccuracyChanged(jint value) override - { - accuracy = value; - } - - void onSensorChanged(jlong /*timestamp*/, const jfloat *values, uint size) override - { - if (size < 3) - return; - reading[0] = values[0]; - reading[1] = values[1]; - reading[2] = values[2]; - m_compass->testStuff(); - } - - jfloat reading[3]; - jint accuracy; - -private: - AndroidCompass *m_compass; -}; - -class AndroidMagnetometerListener : public AndroidSensors::AndroidSensorsListenerInterface -{ -public: - AndroidMagnetometerListener(AndroidCompass *parent) - : accuracy(0), m_compass(parent) - { - memset(reading, 0, sizeof(reading)); - } - - void start(int dataRate) - { - AndroidSensors::registerListener(AndroidSensors::TYPE_MAGNETIC_FIELD, this, dataRate); - } - - void stop() - { - AndroidSensors::unregisterListener(AndroidSensors::TYPE_MAGNETIC_FIELD, this); - } - - void onAccuracyChanged(jint value) override - { - accuracy = value; - } - - void onSensorChanged(jlong /*timestamp*/, const jfloat *values, uint size) override - { - if (size < 3) - return; - reading[0] = values[0]; - reading[1] = values[1]; - reading[2] = values[2]; - m_compass->testStuff(); - } - - jfloat reading[3]; - jint accuracy; -private: - AndroidCompass *m_compass; -}; - -char const * const AndroidCompass::id("android.synthetic.compass"); - -AndroidCompass::AndroidCompass(QSensor *sensor) - : QSensorBackend(sensor), m_accelerometerListener(0), m_magnetometerListener(0), m_isStarted(false) -{ - setReading<QCompassReading>(&m_reading); - m_isStarted = false; -} - -AndroidCompass::~AndroidCompass() -{ - if (m_isStarted) - stop(); - delete m_accelerometerListener; - delete m_magnetometerListener; -} - -void AndroidCompass::start() -{ - if (!m_accelerometerListener) - m_accelerometerListener = new AndroidAccelerometerListener(this); - if (!m_magnetometerListener) - m_magnetometerListener = new AndroidMagnetometerListener(this); - m_accelerometerListener->start(sensor()->dataRate()); - m_magnetometerListener->start(sensor()->dataRate()); - - m_isStarted = true; -} - -void AndroidCompass::stop() -{ - if (m_isStarted) { - m_isStarted = false; - m_accelerometerListener->stop(); - m_magnetometerListener->stop(); - } -} - -void AndroidCompass::testStuff() -{ - if (!m_accelerometerListener || !m_magnetometerListener) - return; - qreal azimuth = AndroidSensors::getCompassAzimuth(m_accelerometerListener->reading, m_magnetometerListener->reading); - - m_reading.setAzimuth(qRadiansToDegrees(azimuth)); - const qreal accuracyValue = (m_accelerometerListener->accuracy + m_magnetometerListener->accuracy) / 6; - m_reading.setCalibrationLevel(accuracyValue); - newReadingAvailable(); -} diff --git a/src/plugins/sensors/android/src/androidjnisensors.cpp b/src/plugins/sensors/android/src/androidjnisensors.cpp deleted file mode 100644 index 5e0e414..0000000 --- a/src/plugins/sensors/android/src/androidjnisensors.cpp +++ /dev/null @@ -1,277 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 BogDan Vatra <bogdan@kde.org> -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtSensors module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include <qglobal.h> -#include <android/log.h> -#include <QtCore/QReadWriteLock> -#include <QtCore/QHash> - -#include "androidjnisensors.h" - -static JavaVM *javaVM = 0; -jclass sensorsClass; - -static jmethodID getSensorListMethodId; -static jmethodID registerSensorMethodId; -static jmethodID unregisterSensorMethodId; -static jmethodID getSensorDescriptionMethodId; -static jmethodID getSensorMaximumRangeMethodId; -static jmethodID getCompassAzimuthId; - -static QHash<int, QList<AndroidSensors::AndroidSensorsListenerInterface *> > listenersHash; -QReadWriteLock listenersLocker; -enum { - SENSOR_DELAY_FASTEST = 0, - SENSOR_DELAY_GAME = 1, - SENSOR_DELAY_NORMAL = 3, - SENSOR_DELAY_UI =2 -}; -namespace AndroidSensors -{ - struct AttachedJNIEnv - { - AttachedJNIEnv() - { - attached = false; - if (javaVM->GetEnv((void**)&jniEnv, JNI_VERSION_1_6) < 0) { - if (javaVM->AttachCurrentThread(&jniEnv, NULL) < 0) { - __android_log_print(ANDROID_LOG_ERROR, "Qt", "AttachCurrentThread failed"); - jniEnv = 0; - return; - } - attached = true; - } - } - - ~AttachedJNIEnv() - { - if (attached) - javaVM->DetachCurrentThread(); - } - bool attached; - JNIEnv *jniEnv; - }; - - QVector<AndroidSensorType> availableSensors() - { - QVector<AndroidSensorType> ret; - AttachedJNIEnv aenv; - if (!aenv.jniEnv) - return ret; - jintArray jsensors = static_cast<jintArray>(aenv.jniEnv->CallStaticObjectMethod(sensorsClass, - getSensorListMethodId)); - jint *sensors = aenv.jniEnv->GetIntArrayElements(jsensors, 0); - const uint sz = aenv.jniEnv->GetArrayLength(jsensors); - for (uint i = 0; i < sz; i++) - ret.push_back(AndroidSensorType(sensors[i])); - aenv.jniEnv->ReleaseIntArrayElements(jsensors, sensors, 0); - return ret; - } - - QString sensorDescription(AndroidSensorType sensor) - { - AttachedJNIEnv aenv; - if (!aenv.jniEnv) - return QString(); - jstring jstr = static_cast<jstring>(aenv.jniEnv->CallStaticObjectMethod(sensorsClass, - getSensorDescriptionMethodId, - jint(sensor))); - if (!jstr) - return QString(); - const jchar *pstr = aenv.jniEnv->GetStringChars(jstr, 0); - QString ret(reinterpret_cast<const QChar *>(pstr), aenv.jniEnv->GetStringLength(jstr)); - aenv.jniEnv->ReleaseStringChars(jstr, pstr); - aenv.jniEnv->DeleteLocalRef(jstr); - return ret; - } - - qreal sensorMaximumRange(AndroidSensorType sensor) - { - AttachedJNIEnv aenv; - if (!aenv.jniEnv) - return 0; - jfloat range = aenv.jniEnv->CallStaticFloatMethod(sensorsClass, getSensorMaximumRangeMethodId, jint(sensor)); - return range; - } - - bool registerListener(AndroidSensorType sensor, AndroidSensorsListenerInterface *listener, int dataRate) - { - listenersLocker.lockForWrite(); - bool startService = listenersHash[sensor].empty(); - listenersHash[sensor].push_back(listener); - listenersLocker.unlock(); - if (startService) { - AttachedJNIEnv aenv; - if (!aenv.jniEnv) - return false; - int rate = dataRate > 0 ? 1000000/dataRate : SENSOR_DELAY_GAME; - return aenv.jniEnv->CallStaticBooleanMethod(sensorsClass, - registerSensorMethodId, - jint(sensor), - jint(rate)); - } - return true; - } - - bool unregisterListener(AndroidSensorType sensor, AndroidSensorsListenerInterface *listener) - { - listenersLocker.lockForWrite(); - listenersHash[sensor].removeOne(listener); - bool stopService = listenersHash[sensor].empty(); - if (stopService) - listenersHash.remove(sensor); - listenersLocker.unlock(); - if (stopService) { - AttachedJNIEnv aenv; - if (!aenv.jniEnv) - return false; - return aenv.jniEnv->CallStaticBooleanMethod(sensorsClass, unregisterSensorMethodId, jint(sensor)); - } - return true; - } - - qreal getCompassAzimuth(jfloat *accelerometerReading, jfloat *magnetometerReading) - { - AttachedJNIEnv aenv; - if (!aenv.jniEnv) - return 0.0; - return aenv.jniEnv->CallStaticFloatMethod(sensorsClass, getCompassAzimuthId, - accelerometerReading[0], - accelerometerReading[1], - accelerometerReading[2], - magnetometerReading[0], - magnetometerReading[1], - magnetometerReading[2]); - } -} - -static const char logTag[] = "Qt"; -static const char classErrorMsg[] = "Can't find class \"%s\""; -static const char methodErrorMsg[] = "Can't find method \"%s%s\""; - -static void accuracyChanged(JNIEnv * /*env*/, jobject /*thiz*/, jint sensor, jint accuracy) -{ - listenersLocker.lockForRead(); - foreach (AndroidSensors::AndroidSensorsListenerInterface *listener, listenersHash[sensor]) - listener->onAccuracyChanged(accuracy); - listenersLocker.unlock(); -} - -static void sensorChanged(JNIEnv *env, jobject /*thiz*/, jint sensor, jlong timeStamp, jfloatArray array) -{ - uint size = env->GetArrayLength(array); - jfloat *values = env->GetFloatArrayElements(array, 0); - listenersLocker.lockForRead(); - foreach (AndroidSensors::AndroidSensorsListenerInterface *listener, listenersHash[sensor]) - listener->onSensorChanged(timeStamp, values, size); - listenersLocker.unlock(); - env->ReleaseFloatArrayElements(array, values, JNI_ABORT); // don't copy back the elements - -} - -static JNINativeMethod methods[] = { - {"accuracyChanged", "(II)V", (void *)accuracyChanged}, - {"sensorChanged", "(IJ[F)V", (void *)sensorChanged} -}; - -#define FIND_AND_CHECK_CLASS(CLASS_NAME) \ -clazz = env->FindClass(CLASS_NAME); \ -if (!clazz) { \ - __android_log_print(ANDROID_LOG_FATAL, logTag, classErrorMsg, CLASS_NAME); \ - return JNI_FALSE; \ -} - -#define GET_AND_CHECK_STATIC_METHOD(VAR, CLASS, METHOD_NAME, METHOD_SIGNATURE) \ -VAR = env->GetStaticMethodID(CLASS, METHOD_NAME, METHOD_SIGNATURE); \ -if (!VAR) { \ - __android_log_print(ANDROID_LOG_FATAL, logTag, methodErrorMsg, METHOD_NAME, METHOD_SIGNATURE); \ - return JNI_FALSE; \ -} - -static bool registerNatives(JNIEnv *env) -{ - jclass clazz; - FIND_AND_CHECK_CLASS("org/qtproject/qt5/android/sensors/QtSensors"); - sensorsClass = static_cast<jclass>(env->NewGlobalRef(clazz)); - - if (env->RegisterNatives(sensorsClass, methods, sizeof(methods) / sizeof(methods[0])) < 0) { - __android_log_print(ANDROID_LOG_FATAL, logTag, "RegisterNatives failed"); - return JNI_FALSE; - } - - GET_AND_CHECK_STATIC_METHOD(getSensorListMethodId, sensorsClass, "getSensorList", "()[I"); - GET_AND_CHECK_STATIC_METHOD(registerSensorMethodId, sensorsClass, "registerSensor", "(II)Z"); - GET_AND_CHECK_STATIC_METHOD(unregisterSensorMethodId, sensorsClass, "unregisterSensor", "(I)Z"); - GET_AND_CHECK_STATIC_METHOD(getSensorDescriptionMethodId, sensorsClass, "getSensorDescription", "(I)Ljava/lang/String;"); - GET_AND_CHECK_STATIC_METHOD(getSensorMaximumRangeMethodId, sensorsClass, "getSensorMaximumRange", "(I)F"); - GET_AND_CHECK_STATIC_METHOD(getCompassAzimuthId, sensorsClass, "getCompassAzimuth", "(FFFFFF)F"); - - return true; -} - -Q_DECL_EXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void * /*reserved*/) -{ - static bool initialized = false; - if (initialized) - return JNI_VERSION_1_6; - initialized = true; - - typedef union { - JNIEnv *nativeEnvironment; - void *venv; - } UnionJNIEnvToVoid; - - __android_log_print(ANDROID_LOG_INFO, logTag, "Sensors start"); - UnionJNIEnvToVoid uenv; - uenv.venv = NULL; - javaVM = 0; - - if (vm->GetEnv(&uenv.venv, JNI_VERSION_1_4) != JNI_OK) { - __android_log_print(ANDROID_LOG_FATAL, logTag, "GetEnv failed"); - return -1; - } - JNIEnv *env = uenv.nativeEnvironment; - if (!registerNatives(env)) { - __android_log_print(ANDROID_LOG_FATAL, logTag, "registerNatives failed"); - return -1; - } - - javaVM = vm; - return JNI_VERSION_1_4; -} diff --git a/src/plugins/sensors/android/src/androidjnisensors.h b/src/plugins/sensors/android/src/androidjnisensors.h deleted file mode 100644 index 8aee4fc..0000000 --- a/src/plugins/sensors/android/src/androidjnisensors.h +++ /dev/null @@ -1,87 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 BogDan Vatra <bogdan@kde.org> -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtSensors module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#ifndef ANDROIDJNISENSORS_H -#define ANDROIDJNISENSORS_H - -#include <jni.h> - -#include <QtCore/QVector> -#include <QtCore/QString> - -namespace AndroidSensors -{ - // must be in sync with https://developer.android.com/reference/android/hardware/Sensor.html#TYPE_ACCELEROMETER - enum AndroidSensorType - { - TYPE_ACCELEROMETER = 1, - TYPE_AMBIENT_TEMPERATURE = 13, //Added in API level 14 - TYPE_GAME_ROTATION_VECTOR = 15, //Added in API level 18 - TYPE_GRAVITY = 9, - TYPE_GYROSCOPE = 4, - TYPE_GYROSCOPE_UNCALIBRATED = 16, //Added in API level 18 - TYPE_LIGHT = 5, - TYPE_LINEAR_ACCELERATION = 10, - TYPE_MAGNETIC_FIELD = 2, - TYPE_MAGNETIC_FIELD_UNCALIBRATED = 14, //Added in API level 18 - TYPE_ORIENTATION = 3, //This constant was deprecated in API level 8. use SensorManager.getOrientation() instead. - TYPE_PRESSURE = 6, - TYPE_PROXIMITY = 8, - TYPE_RELATIVE_HUMIDITY = 12, //Added in API level 14 - TYPE_ROTATION_VECTOR = 11, - TYPE_SIGNIFICANT_MOTION = 17, //Added in API level 18 - TYPE_TEMPERATURE = 7 //This constant was deprecated in API level 14. use Sensor.TYPE_AMBIENT_TEMPERATURE instead. - }; - - struct AndroidSensorsListenerInterface - { - virtual ~AndroidSensorsListenerInterface() {} - virtual void onAccuracyChanged(jint accuracy) = 0; - virtual void onSensorChanged(jlong timestamp, const jfloat *values, uint size) = 0; - }; - - QVector<AndroidSensorType> availableSensors(); - QString sensorDescription(AndroidSensorType sensor); - qreal sensorMaximumRange(AndroidSensorType sensor); - bool registerListener(AndroidSensorType sensor, AndroidSensorsListenerInterface *listener, int dataRate = 0); - bool unregisterListener(AndroidSensorType sensor, AndroidSensorsListenerInterface *listener); - qreal getCompassAzimuth(jfloat *accelerometerReading, jfloat *magnetometerReading); -} - -#endif // ANDROIDJNISENSORS_H diff --git a/src/plugins/sensors/android/src/androidtemperature.cpp b/src/plugins/sensors/android/src/androidtemperature.cpp deleted file mode 100644 index 3a9e1f5..0000000 --- a/src/plugins/sensors/android/src/androidtemperature.cpp +++ /dev/null @@ -1,64 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 BogDan Vatra <bogdan@kde.org> -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the QtSensors module of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:LGPL$ -** Commercial License Usage -** Licensees holding valid commercial Qt licenses may use this file in -** accordance with the commercial license agreement provided with the -** Software or, alternatively, in accordance with the terms contained in -** a written agreement between you and The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU Lesser General Public License Usage -** Alternatively, this file may be used under the terms of the GNU Lesser -** General Public License version 3 as published by the Free Software -** Foundation and appearing in the file LICENSE.LGPL3 included in the -** packaging of this file. Please review the following information to -** ensure the GNU Lesser General Public License version 3 requirements -** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 2.0 or (at your option) the GNU General -** Public license version 3 or any later version approved by the KDE Free -** Qt Foundation. The licenses are as published by the Free Software -** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-2.0.html and -** https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ - -#include "androidtemperature.h" - -AndroidTemperature::AndroidTemperature(AndroidSensors::AndroidSensorType type, QSensor *sensor) - : AndroidCommonSensor<QAmbientTemperatureReading>(type, sensor) -{} - - -void AndroidTemperature::onAccuracyChanged(jint accuracy) -{ - Q_UNUSED(accuracy) -} - -void AndroidTemperature::onSensorChanged(jlong timestamp, const jfloat *values, uint size) -{ - if (size < 1) - return; - m_reader.setTimestamp(timestamp/1000); - - // TODO: I was unable to test this since the devices I was testing this with did not have - // a temperature sensor. Verify that this works and check that the units are correct. - - m_reader.setTemperature(values[0]); - newReadingAvailable(); -} diff --git a/src/plugins/sensors/android/src/src.pro b/src/plugins/sensors/android/src/src.pro deleted file mode 100644 index 8366944..0000000 --- a/src/plugins/sensors/android/src/src.pro +++ /dev/null @@ -1,39 +0,0 @@ -TARGET = qtsensors_android - -QT = sensors core - -# STATICPLUGIN needed because there's a Q_IMPORT_PLUGIN in main.cpp -# Yes, the plugin imports itself statically -DEFINES += QT_STATICPLUGIN - -HEADERS = \ - androidjnisensors.h \ - androidaccelerometer.h \ - androidcompass.h \ - androidcommonsensor.h \ - androidgyroscope.h \ - androidmagnetometer.h \ - androidpressure.h \ - androidproximity.h \ - androidrotation.h \ - androidtemperature.h \ - androidlight.h - -SOURCES = \ - main.cpp \ - androidjnisensors.cpp \ - androidaccelerometer.cpp \ - androidcompass.cpp \ - androidgyroscope.cpp \ - androidmagnetometer.cpp \ - androidpressure.cpp \ - androidproximity.cpp \ - androidrotation.cpp \ - androidtemperature.cpp \ - androidlight.cpp - -OTHER_FILES = plugin.json - -PLUGIN_TYPE = sensors -PLUGIN_CLASS_NAME = QCounterGesturePlugin -load(qt_plugin) |