From 2c9fc2c1a197b84ba9ef3dc4e586a77a18c3dcc5 Mon Sep 17 00:00:00 2001 From: BogDan Vatra Date: Tue, 2 Apr 2019 14:00:37 +0300 Subject: 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 --- src/plugins/sensors/android/android.pro | 43 +++- .../sensors/android/androidaccelerometer.cpp | 100 ++++++++ src/plugins/sensors/android/androidaccelerometer.h | 74 ++++++ src/plugins/sensors/android/androidcompass.cpp | 167 +++++++++++++ src/plugins/sensors/android/androidcompass.h | 72 ++++++ src/plugins/sensors/android/androidgyroscope.cpp | 64 +++++ src/plugins/sensors/android/androidgyroscope.h | 57 +++++ src/plugins/sensors/android/androidlight.cpp | 55 ++++ src/plugins/sensors/android/androidlight.h | 57 +++++ .../sensors/android/androidmagnetometer.cpp | 67 +++++ src/plugins/sensors/android/androidmagnetometer.h | 57 +++++ src/plugins/sensors/android/androidpressure.cpp | 56 +++++ src/plugins/sensors/android/androidpressure.h | 57 +++++ src/plugins/sensors/android/androidproximity.cpp | 63 +++++ src/plugins/sensors/android/androidproximity.h | 60 +++++ src/plugins/sensors/android/androidrotation.cpp | 65 +++++ src/plugins/sensors/android/androidrotation.h | 57 +++++ src/plugins/sensors/android/androidtemperature.cpp | 54 ++++ src/plugins/sensors/android/androidtemperature.h | 56 +++++ .../sensors/android/jar/AndroidManifest.xml | 6 - src/plugins/sensors/android/jar/jar.pro | 15 -- .../qtproject/qt5/android/sensors/QtSensors.java | 192 -------------- src/plugins/sensors/android/main.cpp | 167 +++++++++++++ src/plugins/sensors/android/plugin.json | 1 + src/plugins/sensors/android/sensoreventqueue.h | 166 ++++++++++++ src/plugins/sensors/android/sensormanager.cpp | 120 +++++++++ src/plugins/sensors/android/sensormanager.h | 77 ++++++ .../sensors/android/src/androidaccelerometer.cpp | 100 -------- .../sensors/android/src/androidaccelerometer.h | 62 ----- .../sensors/android/src/androidcommonsensor.h | 86 ------- src/plugins/sensors/android/src/androidcompass.cpp | 177 ------------- src/plugins/sensors/android/src/androidcompass.h | 74 ------ .../sensors/android/src/androidgyroscope.cpp | 62 ----- src/plugins/sensors/android/src/androidgyroscope.h | 55 ---- .../sensors/android/src/androidjnisensors.cpp | 277 --------------------- .../sensors/android/src/androidjnisensors.h | 87 ------- src/plugins/sensors/android/src/androidlight.cpp | 60 ----- src/plugins/sensors/android/src/androidlight.h | 55 ---- .../sensors/android/src/androidmagnetometer.cpp | 75 ------ .../sensors/android/src/androidmagnetometer.h | 55 ---- .../sensors/android/src/androidpressure.cpp | 61 ----- src/plugins/sensors/android/src/androidpressure.h | 56 ----- .../sensors/android/src/androidproximity.cpp | 70 ------ src/plugins/sensors/android/src/androidproximity.h | 57 ----- .../sensors/android/src/androidrotation.cpp | 63 ----- src/plugins/sensors/android/src/androidrotation.h | 54 ---- .../sensors/android/src/androidtemperature.cpp | 64 ----- .../sensors/android/src/androidtemperature.h | 56 ----- src/plugins/sensors/android/src/main.cpp | 168 ------------- src/plugins/sensors/android/src/plugin.json | 1 - src/plugins/sensors/android/src/src.pro | 39 --- 51 files changed, 1810 insertions(+), 2129 deletions(-) create mode 100644 src/plugins/sensors/android/androidaccelerometer.cpp create mode 100644 src/plugins/sensors/android/androidaccelerometer.h create mode 100644 src/plugins/sensors/android/androidcompass.cpp create mode 100644 src/plugins/sensors/android/androidcompass.h create mode 100644 src/plugins/sensors/android/androidgyroscope.cpp create mode 100644 src/plugins/sensors/android/androidgyroscope.h create mode 100644 src/plugins/sensors/android/androidlight.cpp create mode 100644 src/plugins/sensors/android/androidlight.h create mode 100644 src/plugins/sensors/android/androidmagnetometer.cpp create mode 100644 src/plugins/sensors/android/androidmagnetometer.h create mode 100644 src/plugins/sensors/android/androidpressure.cpp create mode 100644 src/plugins/sensors/android/androidpressure.h create mode 100644 src/plugins/sensors/android/androidproximity.cpp create mode 100644 src/plugins/sensors/android/androidproximity.h create mode 100644 src/plugins/sensors/android/androidrotation.cpp create mode 100644 src/plugins/sensors/android/androidrotation.h create mode 100644 src/plugins/sensors/android/androidtemperature.cpp create mode 100644 src/plugins/sensors/android/androidtemperature.h delete mode 100644 src/plugins/sensors/android/jar/AndroidManifest.xml delete mode 100644 src/plugins/sensors/android/jar/jar.pro delete mode 100644 src/plugins/sensors/android/jar/src/org/qtproject/qt5/android/sensors/QtSensors.java create mode 100644 src/plugins/sensors/android/main.cpp create mode 100644 src/plugins/sensors/android/plugin.json create mode 100644 src/plugins/sensors/android/sensoreventqueue.h create mode 100644 src/plugins/sensors/android/sensormanager.cpp create mode 100644 src/plugins/sensors/android/sensormanager.h delete mode 100644 src/plugins/sensors/android/src/androidaccelerometer.cpp delete mode 100644 src/plugins/sensors/android/src/androidaccelerometer.h delete mode 100644 src/plugins/sensors/android/src/androidcommonsensor.h delete mode 100644 src/plugins/sensors/android/src/androidcompass.cpp delete mode 100644 src/plugins/sensors/android/src/androidcompass.h delete mode 100644 src/plugins/sensors/android/src/androidgyroscope.cpp delete mode 100644 src/plugins/sensors/android/src/androidgyroscope.h delete mode 100644 src/plugins/sensors/android/src/androidjnisensors.cpp delete mode 100644 src/plugins/sensors/android/src/androidjnisensors.h delete mode 100644 src/plugins/sensors/android/src/androidlight.cpp delete mode 100644 src/plugins/sensors/android/src/androidlight.h delete mode 100644 src/plugins/sensors/android/src/androidmagnetometer.cpp delete mode 100644 src/plugins/sensors/android/src/androidmagnetometer.h delete mode 100644 src/plugins/sensors/android/src/androidpressure.cpp delete mode 100644 src/plugins/sensors/android/src/androidpressure.h delete mode 100644 src/plugins/sensors/android/src/androidproximity.cpp delete mode 100644 src/plugins/sensors/android/src/androidproximity.h delete mode 100644 src/plugins/sensors/android/src/androidrotation.cpp delete mode 100644 src/plugins/sensors/android/src/androidrotation.h delete mode 100644 src/plugins/sensors/android/src/androidtemperature.cpp delete mode 100644 src/plugins/sensors/android/src/androidtemperature.h delete mode 100644 src/plugins/sensors/android/src/main.cpp delete mode 100644 src/plugins/sensors/android/src/plugin.json delete mode 100644 src/plugins/sensors/android/src/src.pro (limited to 'src/plugins') diff --git a/src/plugins/sensors/android/android.pro b/src/plugins/sensors/android/android.pro index 0dc6a3fc..15e26ee9 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/androidaccelerometer.cpp b/src/plugins/sensors/android/androidaccelerometer.cpp new file mode 100644 index 00000000..c9f5141e --- /dev/null +++ b/src/plugins/sensors/android/androidaccelerometer.cpp @@ -0,0 +1,100 @@ +/**************************************************************************** +** +** Copyright (C) 2019 BogDan Vatra +** 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 "androidaccelerometer.h" +#include + +AndroidAccelerometer::AndroidAccelerometer(int accelerationModes, QSensor *sensor, QObject *parent) + : SensorEventQueue(ASENSOR_TYPE_ACCELEROMETER, sensor, parent) + , m_accelerationModes(accelerationModes) +{ + auto accelerometer = qobject_cast(sensor); + if (accelerometer) { + connect(accelerometer, &QAccelerometer::accelerationModeChanged, + this, &AndroidAccelerometer::applyAccelerationMode); + applyAccelerationMode(accelerometer->accelerationMode()); + } +} + +bool AndroidAccelerometer::isFeatureSupported(QSensor::Feature feature) const +{ + return (feature == QSensor::AccelerationMode) ? m_accelerationModes == AllModes : SensorEventQueue::isFeatureSupported(feature); +} + +void AndroidAccelerometer::dataReceived(const ASensorEvent &event) +{ + // 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(); +} + +void AndroidAccelerometer::applyAccelerationMode(QAccelerometer::AccelerationMode accelerationMode) +{ + switch (accelerationMode) { + case QAccelerometer::Gravity: + if (!(m_accelerationModes & Gravity)) + qWarning() << "Gravity sensor missing"; + setSensorType(ASENSOR_TYPE_GRAVITY); + break; + case QAccelerometer::User: + if (!(m_accelerationModes & LinearAcceleration)) + qWarning() << "Linear acceleration sensor missing"; + setSensorType(ASENSOR_TYPE_LINEAR_ACCELERATION); + break; + case QAccelerometer::Combined: + if (!(m_accelerationModes & Accelerometer)) + qWarning() << "Accelerometer sensor missing"; + setSensorType(ASENSOR_TYPE_ACCELEROMETER); + break; + } +} diff --git a/src/plugins/sensors/android/androidaccelerometer.h b/src/plugins/sensors/android/androidaccelerometer.h new file mode 100644 index 00000000..04362451 --- /dev/null +++ b/src/plugins/sensors/android/androidaccelerometer.h @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** Copyright (C) 2019 BogDan Vatra +** 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 ANDROIDACCELEROMETER_H +#define ANDROIDACCELEROMETER_H + +#include + +#include "sensoreventqueue.h" + +class AndroidAccelerometer : public SensorEventQueue +{ + Q_OBJECT +public: + 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 applyAccelerationMode(QAccelerometer::AccelerationMode accelerationMode); + +private: + int m_accelerationModes; + +}; + +#endif // ANDROIDACCELEROMETER_H diff --git a/src/plugins/sensors/android/androidcompass.cpp b/src/plugins/sensors/android/androidcompass.cpp new file mode 100644 index 00000000..653c5e99 --- /dev/null +++ b/src/plugins/sensors/android/androidcompass.cpp @@ -0,0 +1,167 @@ +/**************************************************************************** +** +** Copyright (C) 2019 BogDan Vatra +** 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 + +#include "sensormanager.h" + +AndroidCompass::AndroidCompass(QSensor *sensor, QObject *parent) + : ThreadSafeSensorBackend(sensor, parent) +{ + setDescription("Compass"); + setReading(&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(data); + self->readAllEvents(); + return 1; // 1 means keep receiving events +} diff --git a/src/plugins/sensors/android/androidcompass.h b/src/plugins/sensors/android/androidcompass.h new file mode 100644 index 00000000..1d20de72 --- /dev/null +++ b/src/plugins/sensors/android/androidcompass.h @@ -0,0 +1,72 @@ +/**************************************************************************** +** +** Copyright (C) 2019 BogDan Vatra +** 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 ANDROIDCOMPASS_H +#define ANDROIDCOMPASS_H + +#include +#include + +#include "sensoreventqueue.h" + +class AndroidCompass : public ThreadSafeSensorBackend +{ + Q_OBJECT + +public: + AndroidCompass(QSensor *sensor, QObject *parent = nullptr); + ~AndroidCompass() override; + + void start() override; + void stop() override; +private: + void readAllEvents(); + static int looperCallback(int /*fd*/, int /*events*/, void* data); + +private: + QCompassReading m_reading; + 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/androidgyroscope.cpp b/src/plugins/sensors/android/androidgyroscope.cpp new file mode 100644 index 00000000..2a38b5a0 --- /dev/null +++ b/src/plugins/sensors/android/androidgyroscope.cpp @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2019 BogDan Vatra +** 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 "androidgyroscope.h" +#include + +AndroidGyroscope::AndroidGyroscope(int type, QSensor *sensor, QObject *parent) + : SensorEventQueue(type, sensor, parent) +{} + +void AndroidGyroscope::dataReceived(const ASensorEvent &event) +{ + // 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(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/androidgyroscope.h b/src/plugins/sensors/android/androidgyroscope.h new file mode 100644 index 00000000..7d1ecec0 --- /dev/null +++ b/src/plugins/sensors/android/androidgyroscope.h @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2019 BogDan Vatra +** 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 ANDROIDGYROSCOPE_H +#define ANDROIDGYROSCOPE_H + +#include + +#include "sensoreventqueue.h" + +class AndroidGyroscope : public SensorEventQueue +{ +public: + 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/androidlight.cpp b/src/plugins/sensors/android/androidlight.cpp new file mode 100644 index 00000000..724ed970 --- /dev/null +++ b/src/plugins/sensors/android/androidlight.cpp @@ -0,0 +1,55 @@ +/**************************************************************************** +** +** Copyright (C) 2019 BogDan Vatra +** 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 "androidlight.h" + +AndroidLight::AndroidLight(int type, QSensor *sensor, QObject *parent) + : SensorEventQueue(type, sensor, parent) +{} + +void AndroidLight::dataReceived(const ASensorEvent &event) +{ + // 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(uint64_t(event.timestamp / 1000)); + m_reader.setLux(qreal(event.light)); + newReadingAvailable(); +} diff --git a/src/plugins/sensors/android/androidlight.h b/src/plugins/sensors/android/androidlight.h new file mode 100644 index 00000000..74c963fb --- /dev/null +++ b/src/plugins/sensors/android/androidlight.h @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2019 BogDan Vatra +** 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 ANDROIDLIGHT_H +#define ANDROIDLIGHT_H + +#include + +#include "sensoreventqueue.h" + +class AndroidLight : public SensorEventQueue +{ +public: + 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/androidmagnetometer.cpp b/src/plugins/sensors/android/androidmagnetometer.cpp new file mode 100644 index 00000000..ec663c0b --- /dev/null +++ b/src/plugins/sensors/android/androidmagnetometer.cpp @@ -0,0 +1,67 @@ +/**************************************************************************** +** +** Copyright (C) 2019 BogDan Vatra +** 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 "androidmagnetometer.h" + +AndroidMagnetometer::AndroidMagnetometer(int type, QSensor *sensor, QObject *parent) + : SensorEventQueue(type, sensor, parent) +{} + +void AndroidMagnetometer::dataReceived(const ASensorEvent &event) +{ + 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); + 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/androidmagnetometer.h b/src/plugins/sensors/android/androidmagnetometer.h new file mode 100644 index 00000000..fb1f0ee7 --- /dev/null +++ b/src/plugins/sensors/android/androidmagnetometer.h @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2019 BogDan Vatra +** 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 ANDROIDMAGNETOMETER_H +#define ANDROIDMAGNETOMETER_H + +#include + +#include "sensoreventqueue.h" + +class AndroidMagnetometer : public SensorEventQueue +{ +public: + 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/androidpressure.cpp b/src/plugins/sensors/android/androidpressure.cpp new file mode 100644 index 00000000..46acffd6 --- /dev/null +++ b/src/plugins/sensors/android/androidpressure.cpp @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** Copyright (C) 2019 BogDan Vatra +** 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 "androidpressure.h" + +AndroidPressure::AndroidPressure(int type, QSensor *sensor, QObject *parent) + : SensorEventQueue(type, sensor, parent) +{} + + +void AndroidPressure::dataReceived(const ASensorEvent &event) +{ + // 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(uint64_t(event.timestamp / 1000)); + m_reader.setPressure(pressurePa); //Android uses hPa, we use Pa + newReadingAvailable(); +} diff --git a/src/plugins/sensors/android/androidpressure.h b/src/plugins/sensors/android/androidpressure.h new file mode 100644 index 00000000..ec682e59 --- /dev/null +++ b/src/plugins/sensors/android/androidpressure.h @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2019 BogDan Vatra +** 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 ANDROIDPRESSURE_H +#define ANDROIDPRESSURE_H + +#include + +#include "sensoreventqueue.h" + +class AndroidPressure : public SensorEventQueue +{ +public: + 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/androidproximity.cpp b/src/plugins/sensors/android/androidproximity.cpp new file mode 100644 index 00000000..000399b0 --- /dev/null +++ b/src/plugins/sensors/android/androidproximity.cpp @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2016 BogDan Vatra +** 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 "androidproximity.h" + +AndroidProximity::AndroidProximity(int type, QSensor *sensor, QObject *parent) + : SensorEventQueue(type, sensor, parent) +{ + 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) + m_maximumRange = 10.0; +} + + +void AndroidProximity::dataReceived(const ASensorEvent &event) +{ + // 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(uint64_t(event.timestamp / 1000)); + m_reader.setClose(close); + newReadingAvailable(); +} diff --git a/src/plugins/sensors/android/androidproximity.h b/src/plugins/sensors/android/androidproximity.h new file mode 100644 index 00000000..22169d6f --- /dev/null +++ b/src/plugins/sensors/android/androidproximity.h @@ -0,0 +1,60 @@ +/**************************************************************************** +** +** Copyright (C) 2016 BogDan Vatra +** 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$ +** +****************************************************************************/ + +#ifndef ANDROIDPROXIMITY_H +#define ANDROIDPROXIMITY_H +#include + +#include "sensoreventqueue.h" + +class AndroidProximity : public SensorEventQueue +{ +public: + AndroidProximity(int type, QSensor *sensor, QObject *parent = nullptr); + +protected: + // SensorEventQueue interface + void dataReceived(const ASensorEvent &event) override; + +private: + qreal m_maximumRange; +}; + +#endif // ANDROIDPROXIMITY_H diff --git a/src/plugins/sensors/android/androidrotation.cpp b/src/plugins/sensors/android/androidrotation.cpp new file mode 100644 index 00000000..c1d141a0 --- /dev/null +++ b/src/plugins/sensors/android/androidrotation.cpp @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2019 BogDan Vatra +** 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 + +AndroidRotation::AndroidRotation(int type, QSensor *sensor, QObject *parent) + : SensorEventQueue(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/androidrotation.h b/src/plugins/sensors/android/androidrotation.h new file mode 100644 index 00000000..9761b249 --- /dev/null +++ b/src/plugins/sensors/android/androidrotation.h @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2019 BogDan Vatra +** 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 ANDROIDROTATION_H +#define ANDROIDROTATION_H + +#include + +#include "sensoreventqueue.h" + +class AndroidRotation : public SensorEventQueue +{ +public: + 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/androidtemperature.cpp b/src/plugins/sensors/android/androidtemperature.cpp new file mode 100644 index 00000000..5f85750a --- /dev/null +++ b/src/plugins/sensors/android/androidtemperature.cpp @@ -0,0 +1,54 @@ +/**************************************************************************** +** +** Copyright (C) 2019 BogDan Vatra +** 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(int type, QSensor *sensor, QObject *parent) + : SensorEventQueue(type, sensor, parent) +{} + +void AndroidTemperature::dataReceived(const ASensorEvent &event) +{ + if (sensor()->skipDuplicates() && qFuzzyCompare(m_reader.temperature(), qreal(event.temperature))) + return; + 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/androidtemperature.h b/src/plugins/sensors/android/androidtemperature.h new file mode 100644 index 00000000..ba626051 --- /dev/null +++ b/src/plugins/sensors/android/androidtemperature.h @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** Copyright (C) 2019 BogDan Vatra +** 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 ANDROIDTEMPERATURE_H +#define ANDROIDTEMPERATURE_H + +#include + +#include "sensoreventqueue.h" + +class AndroidTemperature : public SensorEventQueue +{ +public: + 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 5d15cab2..00000000 --- a/src/plugins/sensors/android/jar/AndroidManifest.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - diff --git a/src/plugins/sensors/android/jar/jar.pro b/src/plugins/sensors/android/jar/jar.pro deleted file mode 100644 index 1d5d4ae4..00000000 --- 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 3067d401..00000000 --- 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 -** 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 m_registeredSensors = new SparseArray(); - 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 list = m_sensorManager.getSensorList(Sensor.TYPE_ALL); - HashSet filteredList = new HashSet(); - 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/main.cpp b/src/plugins/sensors/android/main.cpp new file mode 100644 index 00000000..85853cd2 --- /dev/null +++ b/src/plugins/sensors/android/main.cpp @@ -0,0 +1,167 @@ +/**************************************************************************** +** +** Copyright (C) 2016 BogDan Vatra +** 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 +#include +#include +#include +#include +#include +#include "androidaccelerometer.h" +#include "androidcompass.h" +#include "androidgyroscope.h" +#include "androidlight.h" +#include "androidmagnetometer.h" +#include "androidpressure.h" +#include "androidproximity.h" +#include "androidrotation.h" +#include "androidtemperature.h" + +#include "sensormanager.h" +#include + +namespace { + const char AndroidCompassId[] = "android.synthetic.compass"; +} + +class AndroidSensorPlugin : public QObject, public QSensorPluginInterface, public QSensorBackendFactory +{ + Q_OBJECT + Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QSensorPluginInterface/1.0" FILE "plugin.json") + Q_INTERFACES(QSensorPluginInterface) +public: + void registerSensors() override + { + bool accelerometer = false; + bool magnetometer = false; + 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 ASENSOR_TYPE_ACCELEROMETER: + m_accelerationModes |= AndroidAccelerometer::Accelerometer; + QSensorManager::registerBackend(QAccelerometer::type, QByteArray::number(sensor), this); + accelerometer = true; + break; + 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 ASENSOR_TYPE_GYROSCOPE: + QSensorManager::registerBackend(QGyroscope::type, QByteArray::number(sensor), this); + break; + case ASENSOR_TYPE_LIGHT: + QSensorManager::registerBackend(QLightSensor::type, QByteArray::number(sensor), this); + break; + case ASENSOR_TYPE_MAGNETIC_FIELD: + QSensorManager::registerBackend(QMagnetometer::type, QByteArray::number(sensor), this); + magnetometer = true; + break; + case ASENSOR_TYPE_PRESSURE: + QSensorManager::registerBackend(QPressureSensor::type, QByteArray::number(sensor), this); + break; + case ASENSOR_TYPE_PROXIMITY: + QSensorManager::registerBackend(QProximitySensor::type, QByteArray::number(sensor), this); + break; + case ASENSOR_TYPE_ROTATION_VECTOR: + QSensorManager::registerBackend(QRotationSensor::type, QByteArray::number(sensor), this); + break; + + 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, AndroidCompassId, this); + } + + QSensorBackend *createBackend(QSensor *sensor) override + { + if (sensor->identifier() == AndroidCompassId) + return new AndroidCompass(sensor); + + int type = sensor->identifier().toInt(); + switch (type) { + case ASENSOR_TYPE_ACCELEROMETER: + return new AndroidAccelerometer(m_accelerationModes, sensor); + case ASENSOR_TYPE_AMBIENT_TEMPERATURE: + return new AndroidTemperature(type, sensor); + case ASENSOR_TYPE_GYROSCOPE: + return new AndroidGyroscope(type, sensor); + case ASENSOR_TYPE_LIGHT: + return new AndroidLight(type, sensor); + case ASENSOR_TYPE_MAGNETIC_FIELD: + return new AndroidMagnetometer(type, sensor); + case ASENSOR_TYPE_PRESSURE: + return new AndroidPressure(type, sensor); + case ASENSOR_TYPE_PROXIMITY: + return new AndroidProximity(type, sensor); + case ASENSOR_TYPE_ROTATION_VECTOR: + return new AndroidRotation(type, sensor); + } + return nullptr; + } +private: + int m_accelerationModes = 0; +}; + +Q_IMPORT_PLUGIN (AndroidSensorPlugin) // automatically register the plugin + +#include "main.moc" + diff --git a/src/plugins/sensors/android/plugin.json b/src/plugins/sensors/android/plugin.json new file mode 100644 index 00000000..ef0cbb62 --- /dev/null +++ b/src/plugins/sensors/android/plugin.json @@ -0,0 +1 @@ +{ "Keys": [ "android" ] } diff --git a/src/plugins/sensors/android/sensoreventqueue.h b/src/plugins/sensors/android/sensoreventqueue.h new file mode 100644 index 00000000..77bf05c6 --- /dev/null +++ b/src/plugins/sensors/android/sensoreventqueue.h @@ -0,0 +1,166 @@ +/**************************************************************************** +** +** Copyright (C) 2019 BogDan Vatra +** 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 + +#include +#include +#include + +class FunctionEvent : public QEvent +{ +public: + using Function = std::function; + 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(event)->callFunction(); + event->accept(); + return true; + } + return QSensorBackend::event(event); + } +protected: + QSharedPointer m_sensorManager; +}; + + + +template +class SensorEventQueue : public ThreadSafeSensorBackend +{ +public: + explicit SensorEventQueue(int androidSensorType, QSensor *sensor, QObject *parent = nullptr) + : ThreadSafeSensorBackend(sensor, parent) + { + setReading(&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(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 00000000..590f478c --- /dev/null +++ b/src/plugins/sensors/android/sensormanager.cpp @@ -0,0 +1,120 @@ +/**************************************************************************** +** +** Copyright (C) 2019 BogDan Vatra +** 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 + +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::instance() +{ + static QSharedPointer 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("getVersion")); +} + +double SensorManager::getMaximumRange(const ASensor *sensor) const +{ + return qreal(javaSensor(sensor).callMethod("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 00000000..dc7ffe1f --- /dev/null +++ b/src/plugins/sensors/android/sensormanager.h @@ -0,0 +1,77 @@ +/**************************************************************************** +** +** Copyright (C) 2019 BogDan Vatra +** 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 +#include +#include +#include + +#include +#include + +#include + +class SensorManager : public QThread +{ +public: + ~SensorManager() override; + static QSharedPointer &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/androidaccelerometer.cpp b/src/plugins/sensors/android/src/androidaccelerometer.cpp deleted file mode 100644 index 2e9990b7..00000000 --- a/src/plugins/sensors/android/src/androidaccelerometer.cpp +++ /dev/null @@ -1,100 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 BogDan Vatra -** Copyright (C) 2016 BlackBerry Limited. All rights reserved. -** 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 "androidaccelerometer.h" - -AndroidAccelerometer::AndroidAccelerometer(AndroidSensors::AndroidSensorType type, QSensor *sensor) - : AndroidCommonSensor(type, sensor) -{ - QAccelerometer * const accelerometer = qobject_cast(sensor); - if (accelerometer) { - connect(accelerometer, SIGNAL(accelerationModeChanged(AccelerationMode)), - this, SLOT(applyAccelerationMode())); - } -} - -void AndroidAccelerometer::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]); - m_reader.setY(values[1]); - m_reader.setZ(values[2]); - newReadingAvailable(); -} - -void AndroidAccelerometer::onAccuracyChanged(jint accuracy) -{ - Q_UNUSED(accuracy) -} - -void AndroidAccelerometer::applyAccelerationMode() -{ - const QAccelerometer * const accelerometer = qobject_cast(sensor()); - if (accelerometer) { - stop(); //Stop previous sensor and start new one - m_type = modeToSensor(accelerometer->accelerationMode()); - start(); - } -} -AndroidSensors::AndroidSensorType AndroidAccelerometer::modeToSensor(QAccelerometer::AccelerationMode mode) -{ - AndroidSensors::AndroidSensorType type; - - switch (mode) { - case QAccelerometer::Gravity: - type = AndroidSensors::TYPE_GRAVITY; - break; - case QAccelerometer::User: - type = AndroidSensors::TYPE_LINEAR_ACCELERATION; - break; - case QAccelerometer::Combined: - default: - type = AndroidSensors::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/src/androidaccelerometer.h deleted file mode 100644 index 9b8bf0b4..00000000 --- a/src/plugins/sensors/android/src/androidaccelerometer.h +++ /dev/null @@ -1,62 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 BogDan Vatra -** 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 ANDROIDACCELEROMETER_H -#define ANDROIDACCELEROMETER_H -#include - -#include "androidcommonsensor.h" - -class AndroidAccelerometer : public AndroidCommonSensor -{ - Q_OBJECT - -public: - AndroidAccelerometer(AndroidSensors::AndroidSensorType type, QSensor *sensor); - static AndroidSensors::AndroidSensorType modeToSensor(QAccelerometer::AccelerationMode mode); -private: - void onAccuracyChanged(jint accuracy) override; - void onSensorChanged(jlong timestamp, const jfloat *values, uint size) override; - -private Q_SLOTS: - void applyAccelerationMode(); - -}; - -#endif // ANDROIDACCELEROMETER_H diff --git a/src/plugins/sensors/android/src/androidcommonsensor.h b/src/plugins/sensors/android/src/androidcommonsensor.h deleted file mode 100644 index bfd36db0..00000000 --- a/src/plugins/sensors/android/src/androidcommonsensor.h +++ /dev/null @@ -1,86 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 BogDan Vatra -** 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 -#include -#include "androidjnisensors.h" - -template -class AndroidCommonSensor : public QSensorBackend, protected AndroidSensors::AndroidSensorsListenerInterface -{ -public: - AndroidCommonSensor(AndroidSensors::AndroidSensorType type, QSensor *sensor) : QSensorBackend(sensor) - { - setDescription(AndroidSensors::sensorDescription(type)); - setReading(&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 0911d901..00000000 --- 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 -#include -#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(&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/androidcompass.h b/src/plugins/sensors/android/src/androidcompass.h deleted file mode 100644 index 16a7c41b..00000000 --- a/src/plugins/sensors/android/src/androidcompass.h +++ /dev/null @@ -1,74 +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$ -** -****************************************************************************/ - -#ifndef ANDROIDCOMPASS_H -#define ANDROIDCOMPASS_H -#include - -#include "androidcommonsensor.h" - -class AndroidAccelerometerListener; -class AndroidMagnetometerListener; - -class AndroidCompass : public QSensorBackend -{ - Q_OBJECT - -public: - static char const * const id; - - AndroidCompass(QSensor *sensor); - ~AndroidCompass(); - - void start() override; - void stop() override; - -private: - AndroidAccelerometerListener *m_accelerometerListener; - AndroidMagnetometerListener *m_magnetometerListener; - - QCompassReading m_reading; - bool m_isStarted; - -public Q_SLOTS: - void testStuff(); - -}; - -#endif // ANDROIDCOMPASS_H diff --git a/src/plugins/sensors/android/src/androidgyroscope.cpp b/src/plugins/sensors/android/src/androidgyroscope.cpp deleted file mode 100644 index c963761b..00000000 --- a/src/plugins/sensors/android/src/androidgyroscope.cpp +++ /dev/null @@ -1,62 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 BogDan Vatra -** 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 "androidgyroscope.h" -#include - -AndroidGyroscope::AndroidGyroscope(AndroidSensors::AndroidSensorType type, QSensor *sensor) - : AndroidCommonSensor(type, sensor) -{} - -void AndroidGyroscope::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(qRadiansToDegrees(values[0])); - m_reader.setY(qRadiansToDegrees(values[1])); - m_reader.setZ(qRadiansToDegrees(values[2])); - newReadingAvailable(); -} - -void AndroidGyroscope::onAccuracyChanged(jint accuracy) -{ - Q_UNUSED(accuracy) -} diff --git a/src/plugins/sensors/android/src/androidgyroscope.h b/src/plugins/sensors/android/src/androidgyroscope.h deleted file mode 100644 index 9dd5629d..00000000 --- a/src/plugins/sensors/android/src/androidgyroscope.h +++ /dev/null @@ -1,55 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 BogDan Vatra -** 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 ANDROIDGYROSCOPE_H -#define ANDROIDGYROSCOPE_H -#include - -#include "androidcommonsensor.h" - -class AndroidGyroscope : public AndroidCommonSensor -{ -public: - AndroidGyroscope(AndroidSensors::AndroidSensorType type, QSensor *sensor); -private: - void onAccuracyChanged(jint accuracy) override; - void onSensorChanged(jlong timestamp, const jfloat *values, uint size) override; -}; - -#endif // ANDROIDGYROSCOPE_H diff --git a/src/plugins/sensors/android/src/androidjnisensors.cpp b/src/plugins/sensors/android/src/androidjnisensors.cpp deleted file mode 100644 index 5e0e4146..00000000 --- a/src/plugins/sensors/android/src/androidjnisensors.cpp +++ /dev/null @@ -1,277 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 BogDan Vatra -** 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 -#include -#include -#include - -#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 > 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 availableSensors() - { - QVector ret; - AttachedJNIEnv aenv; - if (!aenv.jniEnv) - return ret; - jintArray jsensors = static_cast(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(aenv.jniEnv->CallStaticObjectMethod(sensorsClass, - getSensorDescriptionMethodId, - jint(sensor))); - if (!jstr) - return QString(); - const jchar *pstr = aenv.jniEnv->GetStringChars(jstr, 0); - QString ret(reinterpret_cast(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(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 8aee4fcf..00000000 --- a/src/plugins/sensors/android/src/androidjnisensors.h +++ /dev/null @@ -1,87 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 BogDan Vatra -** 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 - -#include -#include - -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 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/androidlight.cpp b/src/plugins/sensors/android/src/androidlight.cpp deleted file mode 100644 index 1059670b..00000000 --- a/src/plugins/sensors/android/src/androidlight.cpp +++ /dev/null @@ -1,60 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 BogDan Vatra -** 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 "androidlight.h" - -AndroidLight::AndroidLight(AndroidSensors::AndroidSensorType type, QSensor *sensor) - : AndroidCommonSensor(type, sensor) -{} - - -void AndroidLight::onAccuracyChanged(jint accuracy) -{ - Q_UNUSED(accuracy) -} - -void AndroidLight::onSensorChanged(jlong timestamp, const jfloat *values, uint size) -{ - if (size < 1) - return; - m_reader.setTimestamp(timestamp/1000); - // check https://developer.android.com/reference/android/hardware/SensorEvent.html#values - m_reader.setLux(values[0]); - newReadingAvailable(); -} diff --git a/src/plugins/sensors/android/src/androidlight.h b/src/plugins/sensors/android/src/androidlight.h deleted file mode 100644 index 3c94c138..00000000 --- a/src/plugins/sensors/android/src/androidlight.h +++ /dev/null @@ -1,55 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 BogDan Vatra -** 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 ANDROIDLIGHT_H -#define ANDROIDLIGHT_H -#include - -#include "androidcommonsensor.h" - -class AndroidLight : public AndroidCommonSensor -{ -public: - AndroidLight(AndroidSensors::AndroidSensorType type, QSensor *sensor); -private: - void onAccuracyChanged(jint accuracy) override; - void onSensorChanged(jlong timestamp, const jfloat *values, uint size) override; -}; - -#endif // ANDROIDLIGHT_H diff --git a/src/plugins/sensors/android/src/androidmagnetometer.cpp b/src/plugins/sensors/android/src/androidmagnetometer.cpp deleted file mode 100644 index fcde4cf5..00000000 --- a/src/plugins/sensors/android/src/androidmagnetometer.cpp +++ /dev/null @@ -1,75 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 BogDan Vatra -** 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 "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(type, sensor) -{} - -void AndroidMagnetometer::onAccuracyChanged(jint accuracy) -{ - // 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); - 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); - newReadingAvailable(); -} diff --git a/src/plugins/sensors/android/src/androidmagnetometer.h b/src/plugins/sensors/android/src/androidmagnetometer.h deleted file mode 100644 index 1b6a4359..00000000 --- a/src/plugins/sensors/android/src/androidmagnetometer.h +++ /dev/null @@ -1,55 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 BogDan Vatra -** 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 ANDROIDMAGNETOMETER_H -#define ANDROIDMAGNETOMETER_H -#include - -#include "androidcommonsensor.h" - -class AndroidMagnetometer : public AndroidCommonSensor -{ -public: - AndroidMagnetometer(AndroidSensors::AndroidSensorType type, QSensor *sensor); -private: - void onAccuracyChanged(jint accuracy) override; - void onSensorChanged(jlong timestamp, const jfloat *values, uint size) override; -}; - -#endif // ANDROIDMAGNETOMETER_H diff --git a/src/plugins/sensors/android/src/androidpressure.cpp b/src/plugins/sensors/android/src/androidpressure.cpp deleted file mode 100644 index 0c9a026d..00000000 --- a/src/plugins/sensors/android/src/androidpressure.cpp +++ /dev/null @@ -1,61 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 BogDan Vatra -** 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 "androidpressure.h" - -AndroidPressure::AndroidPressure(AndroidSensors::AndroidSensorType type, QSensor *sensor) - : AndroidCommonSensor(type, sensor) -{} - - -void AndroidPressure::onAccuracyChanged(jint accuracy) -{ - Q_UNUSED(accuracy) -} - -void AndroidPressure::onSensorChanged(jlong timestamp, const jfloat *values, uint size) -{ - if (size < 1) - 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 - newReadingAvailable(); -} diff --git a/src/plugins/sensors/android/src/androidpressure.h b/src/plugins/sensors/android/src/androidpressure.h deleted file mode 100644 index dbb59c1b..00000000 --- a/src/plugins/sensors/android/src/androidpressure.h +++ /dev/null @@ -1,56 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 BogDan Vatra -** 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$ -** -****************************************************************************/ - -#ifndef ANDROIDPRESSURE_H -#define ANDROIDPRESSURE_H -#include - -#include "androidcommonsensor.h" - -class AndroidPressure : public AndroidCommonSensor -{ -public: - AndroidPressure(AndroidSensors::AndroidSensorType type, QSensor *sensor); -private: - void onAccuracyChanged(jint accuracy) override; - void onSensorChanged(jlong timestamp, const jfloat *values, uint size) override; -}; - -#endif // ANDROIDPRESSURE_H diff --git a/src/plugins/sensors/android/src/androidproximity.cpp b/src/plugins/sensors/android/src/androidproximity.cpp deleted file mode 100644 index 27c5891c..00000000 --- a/src/plugins/sensors/android/src/androidproximity.cpp +++ /dev/null @@ -1,70 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 BogDan Vatra -** 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 "androidproximity.h" -#include "androidjnisensors.h" - -AndroidProximity::AndroidProximity(AndroidSensors::AndroidSensorType type, QSensor *sensor) - : AndroidCommonSensor(type, sensor) -{ - m_maximumRange = AndroidSensors::sensorMaximumRange(type); - - // if we can't get the range, we arbitrarily define anything closer than 10 cm as "close" - if (m_maximumRange <= 0) - m_maximumRange = 10.0; -} - - -void AndroidProximity::onAccuracyChanged(jint accuracy) -{ - Q_UNUSED(accuracy) -} - -void AndroidProximity::onSensorChanged(jlong timestamp, const jfloat *values, uint size) -{ - if (size < 1) - return; - m_reader.setTimestamp(timestamp/1000); - - qreal reading = values[0]; - bool close = (reading < m_maximumRange); - m_reader.setClose(close); - newReadingAvailable(); -} diff --git a/src/plugins/sensors/android/src/androidproximity.h b/src/plugins/sensors/android/src/androidproximity.h deleted file mode 100644 index 5a3110c2..00000000 --- a/src/plugins/sensors/android/src/androidproximity.h +++ /dev/null @@ -1,57 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 BogDan Vatra -** 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$ -** -****************************************************************************/ - -#ifndef ANDROIDPROXIMITY_H -#define ANDROIDPROXIMITY_H -#include - -#include "androidcommonsensor.h" - -class AndroidProximity : public AndroidCommonSensor -{ -public: - AndroidProximity(AndroidSensors::AndroidSensorType type, QSensor *sensor); -private: - void onAccuracyChanged(jint accuracy) override; - void onSensorChanged(jlong timestamp, const jfloat *values, uint size) override; - qreal m_maximumRange; -}; - -#endif // ANDROIDPROXIMITY_H diff --git a/src/plugins/sensors/android/src/androidrotation.cpp b/src/plugins/sensors/android/src/androidrotation.cpp deleted file mode 100644 index f7d02257..00000000 --- a/src/plugins/sensors/android/src/androidrotation.cpp +++ /dev/null @@ -1,63 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 BogDan Vatra -** 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 - -AndroidRotation::AndroidRotation(AndroidSensors::AndroidSensorType type, QSensor *sensor) - : AndroidCommonSensor(type, sensor) -{} - -void AndroidRotation::onAccuracyChanged(jint accuracy) -{ - Q_UNUSED(accuracy) -} - -void AndroidRotation::onSensorChanged(jlong timestamp, const jfloat *values, uint size) -{ - if (size < 3) - 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); - newReadingAvailable(); -} diff --git a/src/plugins/sensors/android/src/androidrotation.h b/src/plugins/sensors/android/src/androidrotation.h deleted file mode 100644 index 0fe90a61..00000000 --- a/src/plugins/sensors/android/src/androidrotation.h +++ /dev/null @@ -1,54 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 BogDan Vatra -** 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 ANDROIDROTATION_H -#define ANDROIDROTATION_H -#include - -#include "androidcommonsensor.h" -class AndroidRotation : public AndroidCommonSensor -{ -public: - AndroidRotation(AndroidSensors::AndroidSensorType type, QSensor *sensor); -private: - void onAccuracyChanged(jint accuracy) override; - void onSensorChanged(jlong timestamp, const jfloat *values, uint size) override; -}; - -#endif // ANDROIDROTATION_H diff --git a/src/plugins/sensors/android/src/androidtemperature.cpp b/src/plugins/sensors/android/src/androidtemperature.cpp deleted file mode 100644 index 3a9e1f51..00000000 --- a/src/plugins/sensors/android/src/androidtemperature.cpp +++ /dev/null @@ -1,64 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 BogDan Vatra -** 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(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/androidtemperature.h b/src/plugins/sensors/android/src/androidtemperature.h deleted file mode 100644 index 1640033b..00000000 --- a/src/plugins/sensors/android/src/androidtemperature.h +++ /dev/null @@ -1,56 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 BogDan Vatra -** 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$ -** -****************************************************************************/ - -#ifndef ANDROIDTEMPERATURE_H -#define ANDROIDTEMPERATURE_H -#include - -#include "androidcommonsensor.h" - -class AndroidTemperature : public AndroidCommonSensor -{ -public: - AndroidTemperature(AndroidSensors::AndroidSensorType type, QSensor *sensor); -private: - void onAccuracyChanged(jint accuracy) override; - void onSensorChanged(jlong timestamp, const jfloat *values, uint size) override; -}; - -#endif // ANDROIDTEMPERATURE_H diff --git a/src/plugins/sensors/android/src/main.cpp b/src/plugins/sensors/android/src/main.cpp deleted file mode 100644 index fc80a273..00000000 --- a/src/plugins/sensors/android/src/main.cpp +++ /dev/null @@ -1,168 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2016 BogDan Vatra -** 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 -#include -#include -#include -#include -#include -#include "androidaccelerometer.h" -#include "androidcompass.h" -#include "androidgyroscope.h" -#include "androidlight.h" -#include "androidmagnetometer.h" -#include "androidpressure.h" -#include "androidproximity.h" -#include "androidrotation.h" -#include "androidtemperature.h" - -using namespace AndroidSensors; - -class AndroidSensorPlugin : public QObject, public QSensorPluginInterface, public QSensorBackendFactory -{ - Q_OBJECT - Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QSensorPluginInterface/1.0" FILE "plugin.json") - Q_INTERFACES(QSensorPluginInterface) -public: - void registerSensors() override - { - bool accelerometer = false; - bool magnetometer = false; - foreach (AndroidSensorType sensor, availableSensors()) { - switch (sensor) { - case TYPE_ACCELEROMETER: - QSensorManager::registerBackend(QAccelerometer::type, QByteArray::number(sensor), this); - accelerometer = true; - break; - case TYPE_AMBIENT_TEMPERATURE: - case TYPE_TEMPERATURE: - QSensorManager::registerBackend(QAmbientTemperatureSensor::type, QByteArray::number(sensor), this); - break; - case TYPE_GRAVITY: - break; // add the gravity sensor backend - case TYPE_GYROSCOPE: - QSensorManager::registerBackend(QGyroscope::type, QByteArray::number(sensor), this); - break; - case 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: - QSensorManager::registerBackend(QMagnetometer::type, QByteArray::number(sensor), this); - magnetometer = true; - break; - case TYPE_ORIENTATION: - break; // add the orientation sensor backend - case TYPE_PRESSURE: - QSensorManager::registerBackend(QPressureSensor::type, QByteArray::number(sensor), this); - break; - case 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: - 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 - } - } - if (accelerometer && magnetometer) - QSensorManager::registerBackend(QCompass::type, AndroidCompass::id, this); - } - - QSensorBackend *createBackend(QSensor *sensor) override - { - if (sensor->identifier() == AndroidCompass::id) - return new AndroidCompass(sensor); - - AndroidSensorType type = static_cast(sensor->identifier().toInt()); - switch (type) { - case TYPE_ACCELEROMETER: { - QAccelerometer * const accelerometer = qobject_cast(sensor); - AndroidSensors::AndroidSensorType type - = accelerometer ? AndroidAccelerometer::modeToSensor(accelerometer->accelerationMode()) - : AndroidSensors::TYPE_ACCELEROMETER; - return new AndroidAccelerometer(type, sensor); - } - case TYPE_AMBIENT_TEMPERATURE: - case TYPE_TEMPERATURE: - return new AndroidTemperature(type, sensor); - case TYPE_GRAVITY: - break; // add the gravity sensor backend - case TYPE_GYROSCOPE: - return new AndroidGyroscope(type, sensor); - case TYPE_LIGHT: - return new AndroidLight(type, sensor); - case TYPE_LINEAR_ACCELERATION: - break; // add the linear acceleration sensor backend - case TYPE_MAGNETIC_FIELD: - return new AndroidMagnetometer(type, sensor); - case TYPE_ORIENTATION: - break; // add the orientation sensor backend - case TYPE_PRESSURE: - return new AndroidPressure(type, sensor); - case TYPE_PROXIMITY: - return new AndroidProximity(type, sensor); - case TYPE_RELATIVE_HUMIDITY: - break; // add the relative humidity sensor backend - case 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; - } -}; - -Q_IMPORT_PLUGIN (AndroidSensorPlugin) // automatically register the plugin - -#include "main.moc" - diff --git a/src/plugins/sensors/android/src/plugin.json b/src/plugins/sensors/android/src/plugin.json deleted file mode 100644 index ef0cbb62..00000000 --- a/src/plugins/sensors/android/src/plugin.json +++ /dev/null @@ -1 +0,0 @@ -{ "Keys": [ "android" ] } diff --git a/src/plugins/sensors/android/src/src.pro b/src/plugins/sensors/android/src/src.pro deleted file mode 100644 index 8366944c..00000000 --- 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) -- cgit v1.2.3