From 96d4de7b36188cf8fe87b1d3f840bf9371744956 Mon Sep 17 00:00:00 2001 From: Paul Olav Tvete Date: Mon, 4 Mar 2013 14:09:10 +0100 Subject: Sensor implementation for Android Based on the Necessitas project by Bogdan Vatra. Contributors to the Qt5 project: BogDan Vatra Eskil Abrahamsen Blomfeldt Paul Olav Tvete The full history of the Qt5 port can be found in refs/old-heads/android, SHA-1 caa4103a80ef90db5eb9836f6b6028b7ce36c73a Change-Id: I57e772ee5079c0ea74f685b65ae2864d73e7c750 Reviewed-by: Eskil Abrahamsen Blomfeldt --- src/plugins/sensors/android/android.pro | 2 + .../sensors/android/jar/AndroidManifest.xml | 6 + src/plugins/sensors/android/jar/jar.pro | 10 + .../qtproject/qt5/android/sensors/QtSensors.java | 161 ++++++++++++++ .../sensors/android/src/androidaccelerometer.cpp | 63 ++++++ .../sensors/android/src/androidaccelerometer.h | 59 +++++ .../sensors/android/src/androidcommonsensor.h | 88 ++++++++ .../sensors/android/src/androidgyroscope.cpp | 64 ++++++ src/plugins/sensors/android/src/androidgyroscope.h | 57 +++++ .../sensors/android/src/androidjnisensors.cpp | 245 +++++++++++++++++++++ .../sensors/android/src/androidjnisensors.h | 83 +++++++ src/plugins/sensors/android/src/androidlight.cpp | 62 ++++++ src/plugins/sensors/android/src/androidlight.h | 57 +++++ .../sensors/android/src/androidmagnetometer.cpp | 63 ++++++ .../sensors/android/src/androidmagnetometer.h | 57 +++++ .../sensors/android/src/androidrotation.cpp | 65 ++++++ src/plugins/sensors/android/src/androidrotation.h | 56 +++++ src/plugins/sensors/android/src/main.cpp | 135 ++++++++++++ src/plugins/sensors/android/src/plugin.json | 1 + src/plugins/sensors/android/src/src.pro | 29 +++ src/plugins/sensors/generic/generic.pro | 12 +- src/plugins/sensors/generic/main.cpp | 34 ++- src/plugins/sensors/sensors.pro | 4 + src/sensors/doc/src/compatmap.qdoc | 24 ++ src/sensors/sensors.pro | 5 + 25 files changed, 1434 insertions(+), 8 deletions(-) create mode 100644 src/plugins/sensors/android/android.pro create mode 100644 src/plugins/sensors/android/jar/AndroidManifest.xml create mode 100644 src/plugins/sensors/android/jar/jar.pro create mode 100644 src/plugins/sensors/android/jar/src/org/qtproject/qt5/android/sensors/QtSensors.java create mode 100644 src/plugins/sensors/android/src/androidaccelerometer.cpp create mode 100644 src/plugins/sensors/android/src/androidaccelerometer.h create mode 100644 src/plugins/sensors/android/src/androidcommonsensor.h create mode 100644 src/plugins/sensors/android/src/androidgyroscope.cpp create mode 100644 src/plugins/sensors/android/src/androidgyroscope.h create mode 100644 src/plugins/sensors/android/src/androidjnisensors.cpp create mode 100644 src/plugins/sensors/android/src/androidjnisensors.h create mode 100644 src/plugins/sensors/android/src/androidlight.cpp create mode 100644 src/plugins/sensors/android/src/androidlight.h create mode 100644 src/plugins/sensors/android/src/androidmagnetometer.cpp create mode 100644 src/plugins/sensors/android/src/androidmagnetometer.h create mode 100644 src/plugins/sensors/android/src/androidrotation.cpp create mode 100644 src/plugins/sensors/android/src/androidrotation.h create mode 100644 src/plugins/sensors/android/src/main.cpp create mode 100644 src/plugins/sensors/android/src/plugin.json create mode 100644 src/plugins/sensors/android/src/src.pro (limited to 'src') diff --git a/src/plugins/sensors/android/android.pro b/src/plugins/sensors/android/android.pro new file mode 100644 index 00000000..0dc6a3fc --- /dev/null +++ b/src/plugins/sensors/android/android.pro @@ -0,0 +1,2 @@ +TEMPLATE = subdirs +SUBDIRS += jar src diff --git a/src/plugins/sensors/android/jar/AndroidManifest.xml b/src/plugins/sensors/android/jar/AndroidManifest.xml new file mode 100644 index 00000000..5d15cab2 --- /dev/null +++ b/src/plugins/sensors/android/jar/AndroidManifest.xml @@ -0,0 +1,6 @@ + + + diff --git a/src/plugins/sensors/android/jar/jar.pro b/src/plugins/sensors/android/jar/jar.pro new file mode 100644 index 00000000..a9350743 --- /dev/null +++ b/src/plugins/sensors/android/jar/jar.pro @@ -0,0 +1,10 @@ +load(qt_build_paths) + +CONFIG += java +TARGET = QtSensors +DESTDIR = $$MODULE_BASE_OUTDIR/jar + +JAVACLASSPATH += $$PWD/src + +JAVASOURCES += \ + $$PWD/src/org/qtproject/qt5/android/sensors/QtSensors.java 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 new file mode 100644 index 00000000..c7956d2d --- /dev/null +++ b/src/plugins/sensors/android/jar/src/org/qtproject/qt5/android/sensors/QtSensors.java @@ -0,0 +1,161 @@ +/**************************************************************************** +** +** Copyright (C) 2013 BogDan Vatra +** Contact: http://www.qt-project.org/legal +** +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +package org.qtproject.qt5.android.sensors; + +import java.util.HashSet; +import java.util.List; + +import android.app.Activity; +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 setActivity(Activity activity, Object acitvityDelegate) + { + try { + m_sensorManager = (SensorManager)activity.getSystemService(Activity.SENSOR_SERVICE); + } catch(Exception e) { + e.printStackTrace(); + } + } + + private static String getSensorDescription(int sensorType) + { + try { + Sensor s = m_sensorManager.getDefaultSensor(sensorType); + 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 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; + } + + public static native void accuracyChanged(int sensorType, int accuracy); + public static native void sensorChanged(int sensorType, long timestamp, float[] values); + + @Override + public void onAccuracyChanged(Sensor sensor, int accuracy) + { + accuracyChanged(sensor.getType(), accuracy); + } + + @Override + public void onSensorChanged(SensorEvent sensorEvent) + { + if (sensorEvent.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) { + //#### hacky, but much easier than exposing the convert function and converting the arrays back and forth... + sensorChanged(sensorEvent.sensor.getType(), sensorEvent.timestamp, convertQuaternionToEuler(sensorEvent.values)); + } else { + sensorChanged(sensorEvent.sensor.getType(), sensorEvent.timestamp, sensorEvent.values); + } + } +} diff --git a/src/plugins/sensors/android/src/androidaccelerometer.cpp b/src/plugins/sensors/android/src/androidaccelerometer.cpp new file mode 100644 index 00000000..b64536ef --- /dev/null +++ b/src/plugins/sensors/android/src/androidaccelerometer.cpp @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2013 BogDan Vatra +** Contact: http://www.qt-project.org/legal +** +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "androidaccelerometer.h" + +AndroidAccelerometer::AndroidAccelerometer(AndroidSensors::AndroidSensorType type, QSensor *sensor) + : AndroidCommonSensor(type, sensor) +{} + +void AndroidAccelerometer::onSensorChanged(jlong timestamp, const jfloat *values, uint size) +{ + if (size < 3) + return; + m_reader.setTimestamp(timestamp); + // 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) +} diff --git a/src/plugins/sensors/android/src/androidaccelerometer.h b/src/plugins/sensors/android/src/androidaccelerometer.h new file mode 100644 index 00000000..6c95fdb3 --- /dev/null +++ b/src/plugins/sensors/android/src/androidaccelerometer.h @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** Copyright (C) 2013 BogDan Vatra +** Contact: http://www.qt-project.org/legal +** +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef ANDROIDACCELEROMETER_H +#define ANDROIDACCELEROMETER_H +#include + +#include "androidcommonsensor.h" + +class AndroidAccelerometer : public AndroidCommonSensor +{ +public: + AndroidAccelerometer(AndroidSensors::AndroidSensorType type, QSensor *sensor); + +private: + virtual void onAccuracyChanged(jint accuracy); + virtual void onSensorChanged(jlong timestamp, const jfloat *values, uint size); + +}; + +#endif // ANDROIDACCELEROMETER_H diff --git a/src/plugins/sensors/android/src/androidcommonsensor.h b/src/plugins/sensors/android/src/androidcommonsensor.h new file mode 100644 index 00000000..40f118ed --- /dev/null +++ b/src/plugins/sensors/android/src/androidcommonsensor.h @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** Copyright (C) 2013 BogDan Vatra +** Contact: http://www.qt-project.org/legal +** +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.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(); + } + virtual void start() + { + if (AndroidSensors::registerListener(m_type, this, sensor()->dataRate())) + m_isStarted = true; + } + + virtual void stop() + { + if (m_isStarted) { + m_isStarted = false; + AndroidSensors::unregisterListener(m_type, this); + } + } + +protected: + ReaderType m_reader; + +private: + AndroidSensors::AndroidSensorType m_type; + bool m_isStarted; +}; + +#endif // ANDROIDCOMMONSENSOR_H diff --git a/src/plugins/sensors/android/src/androidgyroscope.cpp b/src/plugins/sensors/android/src/androidgyroscope.cpp new file mode 100644 index 00000000..69a05c18 --- /dev/null +++ b/src/plugins/sensors/android/src/androidgyroscope.cpp @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2013 BogDan Vatra +** Contact: http://www.qt-project.org/legal +** +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.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); + // check https://developer.android.com/reference/android/hardware/SensorEvent.html#values + m_reader.setX(values[0]*180/M_PI); + m_reader.setY(values[1]*180/M_PI); + m_reader.setZ(values[2]*180/M_PI); + 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 new file mode 100644 index 00000000..3b633b7c --- /dev/null +++ b/src/plugins/sensors/android/src/androidgyroscope.h @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2013 BogDan Vatra +** Contact: http://www.qt-project.org/legal +** +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.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: + virtual void onAccuracyChanged(jint accuracy); + virtual void onSensorChanged(jlong timestamp, const jfloat *values, uint size); +}; + +#endif // ANDROIDGYROSCOPE_H diff --git a/src/plugins/sensors/android/src/androidjnisensors.cpp b/src/plugins/sensors/android/src/androidjnisensors.cpp new file mode 100644 index 00000000..6365433c --- /dev/null +++ b/src/plugins/sensors/android/src/androidjnisensors.cpp @@ -0,0 +1,245 @@ +/**************************************************************************** +** +** Copyright (C) 2013 BogDan Vatra +** Contact: http://www.qt-project.org/legal +** +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.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 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))); + 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; + } + + 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; + } +} + +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;"); + + return true; +} + +Q_DECL_EXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void * /*reserved*/) +{ + 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 new file mode 100644 index 00000000..53ca58d0 --- /dev/null +++ b/src/plugins/sensors/android/src/androidjnisensors.h @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** Copyright (C) 2013 BogDan Vatra +** Contact: http://www.qt-project.org/legal +** +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.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, + TYPE_GRAVITY = 9, + TYPE_GYROSCOPE = 4, + TYPE_LIGHT = 5, + TYPE_LINEAR_ACCELERATION = 10, + TYPE_MAGNETIC_FIELD = 2, + 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, + TYPE_ROTATION_VECTOR = 11, + 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); + bool registerListener(AndroidSensorType sensor, AndroidSensorsListenerInterface *listener, int dataRate = 0); + bool unregisterListener(AndroidSensorType sensor, AndroidSensorsListenerInterface *listener); +} + +#endif // ANDROIDJNISENSORS_H diff --git a/src/plugins/sensors/android/src/androidlight.cpp b/src/plugins/sensors/android/src/androidlight.cpp new file mode 100644 index 00000000..42295e54 --- /dev/null +++ b/src/plugins/sensors/android/src/androidlight.cpp @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2013 BogDan Vatra +** Contact: http://www.qt-project.org/legal +** +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.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); + // 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 new file mode 100644 index 00000000..fdea3edb --- /dev/null +++ b/src/plugins/sensors/android/src/androidlight.h @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2013 BogDan Vatra +** Contact: http://www.qt-project.org/legal +** +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.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: + virtual void onAccuracyChanged(jint accuracy); + virtual void onSensorChanged(jlong timestamp, const jfloat *values, uint size); +}; + +#endif // ANDROIDLIGHT_H diff --git a/src/plugins/sensors/android/src/androidmagnetometer.cpp b/src/plugins/sensors/android/src/androidmagnetometer.cpp new file mode 100644 index 00000000..9175f584 --- /dev/null +++ b/src/plugins/sensors/android/src/androidmagnetometer.cpp @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2013 BogDan Vatra +** Contact: http://www.qt-project.org/legal +** +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "androidmagnetometer.h" + +AndroidMagnetometer::AndroidMagnetometer(AndroidSensors::AndroidSensorType type, QSensor *sensor) + :AndroidCommonSensor(type, sensor) +{} + +void AndroidMagnetometer::onAccuracyChanged(jint accuracy) +{ + m_reader.setCalibrationLevel(accuracy); +} + +void AndroidMagnetometer::onSensorChanged(jlong timestamp, const jfloat *values, uint size) +{ + if (size<3) + return; + m_reader.setTimestamp(timestamp); + // 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 new file mode 100644 index 00000000..57d9f36d --- /dev/null +++ b/src/plugins/sensors/android/src/androidmagnetometer.h @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2013 BogDan Vatra +** Contact: http://www.qt-project.org/legal +** +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.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: + virtual void onAccuracyChanged(jint accuracy); + virtual void onSensorChanged(jlong timestamp, const jfloat *values, uint size); +}; + +#endif // ANDROIDMAGNETOMETER_H diff --git a/src/plugins/sensors/android/src/androidrotation.cpp b/src/plugins/sensors/android/src/androidrotation.cpp new file mode 100644 index 00000000..602575fe --- /dev/null +++ b/src/plugins/sensors/android/src/androidrotation.cpp @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2013 BogDan Vatra +** Contact: http://www.qt-project.org/legal +** +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.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); + + float rz = -values[0]*180/M_PI; + float rx = -values[1]*180/M_PI; + float ry = values[2]*180/M_PI; + 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 new file mode 100644 index 00000000..cd2c4d65 --- /dev/null +++ b/src/plugins/sensors/android/src/androidrotation.h @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** Copyright (C) 2013 BogDan Vatra +** Contact: http://www.qt-project.org/legal +** +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.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: + virtual void onAccuracyChanged(jint accuracy); + virtual void onSensorChanged(jlong timestamp, const jfloat *values, uint size); +}; + +#endif // ANDROIDROTATION_H diff --git a/src/plugins/sensors/android/src/main.cpp b/src/plugins/sensors/android/src/main.cpp new file mode 100644 index 00000000..0cb4de49 --- /dev/null +++ b/src/plugins/sensors/android/src/main.cpp @@ -0,0 +1,135 @@ +/**************************************************************************** +** +** Copyright (C) 2013 BogDan Vatra +** Contact: http://www.qt-project.org/legal +** +** 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 Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include +#include +#include +#include +#include "androidaccelerometer.h" +#include "androidgyroscope.h" +#include "androidlight.h" +#include "androidmagnetometer.h" +#include "androidrotation.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() + { + foreach (AndroidSensorType sensor, availableSensors()) { + switch (sensor) { + case TYPE_ACCELEROMETER: + QSensorManager::registerBackend(QAccelerometer::type, QByteArray::number(sensor), this); + break; + case TYPE_AMBIENT_TEMPERATURE: + case TYPE_TEMPERATURE: + break; // add the temperature sensor backend + 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); + break; + case TYPE_ORIENTATION: + break; // add the orientation sensor backend + case TYPE_PRESSURE: + break; // add the pressure sensor backend + case TYPE_PROXIMITY: + break; // add the proximity sensor backend + case TYPE_RELATIVE_HUMIDITY: + break; // add the relative humidity sensor backend + case TYPE_ROTATION_VECTOR: + QSensorManager::registerBackend(QRotationSensor::type, QByteArray::number(sensor), this); + break; + } + } + } + + QSensorBackend *createBackend(QSensor *sensor) + { + AndroidSensorType type = static_cast(sensor->identifier().toInt()); + switch (type) { + case TYPE_ACCELEROMETER: + return new AndroidAccelerometer(type, sensor); + case TYPE_AMBIENT_TEMPERATURE: + case TYPE_TEMPERATURE: + break; // add the temperature sensor backend + 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: + break; // add the pressure sensor backend + case TYPE_PROXIMITY: + break; // add the proximity sensor backend + case TYPE_RELATIVE_HUMIDITY: + break; // add the relative humidity sensor backend + case TYPE_ROTATION_VECTOR: + return new AndroidRotation(type, sensor); + } + 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 new file mode 100644 index 00000000..8a55b3ae --- /dev/null +++ b/src/plugins/sensors/android/src/plugin.json @@ -0,0 +1 @@ +{ "Keys": [ "notused" ] } diff --git a/src/plugins/sensors/android/src/src.pro b/src/plugins/sensors/android/src/src.pro new file mode 100644 index 00000000..715a0111 --- /dev/null +++ b/src/plugins/sensors/android/src/src.pro @@ -0,0 +1,29 @@ +TARGET = qtsensors_android +QT = sensors core + +PLUGIN_TYPE = sensors +load(qt_plugin) + +# 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 \ + androidcommonsensor.h \ + androidgyroscope.h \ + androidmagnetometer.h \ + androidrotation.h \ + androidlight.h + +SOURCES = \ + main.cpp \ + androidjnisensors.cpp \ + androidaccelerometer.cpp \ + androidgyroscope.cpp \ + androidmagnetometer.cpp \ + androidrotation.cpp \ + androidlight.cpp + +OTHER_FILES = plugin.json diff --git a/src/plugins/sensors/generic/generic.pro b/src/plugins/sensors/generic/generic.pro index 5bfeff64..fcc1bfcd 100644 --- a/src/plugins/sensors/generic/generic.pro +++ b/src/plugins/sensors/generic/generic.pro @@ -8,15 +8,23 @@ HEADERS += generictiltsensor.h SOURCES += main.cpp\ generictiltsensor.cpp +DEFINES += QTSENSORS_GENERICTILTSENSOR !blackberry { HEADERS += genericorientationsensor.h\ - genericrotationsensor.h\ genericalssensor.h SOURCES += genericorientationsensor.cpp\ - genericrotationsensor.cpp\ genericalssensor.cpp + DEFINES += QTSENSORS_GENERICORIENTATIONSENSOR QTSENSORS_GENERICALSSENSOR +} + +!blackberry:!android { + HEADERS += genericrotationsensor.h + + SOURCES += genericrotationsensor.cpp + + DEFINES += QTSENSORS_GENERICROTATIONSENSOR } OTHER_FILES = plugin.json diff --git a/src/plugins/sensors/generic/main.cpp b/src/plugins/sensors/generic/main.cpp index 4b348b19..aa9f58c2 100644 --- a/src/plugins/sensors/generic/main.cpp +++ b/src/plugins/sensors/generic/main.cpp @@ -39,12 +39,18 @@ ** ****************************************************************************/ -#ifndef Q_OS_BLACKBERRY +#ifdef QTSENSORS_GENERICORIENTATIONSENSOR #include "genericorientationsensor.h" +#endif +#ifdef QTSENSORS_GENERICROTATIONSENSOR #include "genericrotationsensor.h" +#endif +#ifdef QTSENSORS_GENERICALSSENSOR #include "genericalssensor.h" #endif +#ifdef QTSENSORS_GENERICTILTSENSOR #include "generictiltsensor.h" +#endif #include #include #include @@ -66,44 +72,60 @@ public: { if (!QSensor::defaultSensorForType(QAccelerometer::type).isEmpty()) { // There is an accelerometer available. Register the backends -#ifndef Q_OS_BLACKBERRY +#ifdef QTSENSORS_GENERICORIENTATIONSENSOR if (!QSensorManager::isBackendRegistered(QOrientationSensor::type, genericorientationsensor::id)) QSensorManager::registerBackend(QOrientationSensor::type, genericorientationsensor::id, this); +#endif +#ifdef QTSENSORS_GENERICROTATIONSENSOR if (!QSensorManager::isBackendRegistered(QRotationSensor::type, genericrotationsensor::id)) QSensorManager::registerBackend(QRotationSensor::type, genericrotationsensor::id, this); +#endif +#ifdef QTSENSORS_GENERICALSSENSOR if (!QSensorManager::isBackendRegistered(QAmbientLightSensor::type, genericalssensor::id)) QSensorManager::registerBackend(QAmbientLightSensor::type, genericalssensor::id, this); #endif +#ifdef QTSENSORS_GENERICTILTSENSOR if (!QSensorManager::isBackendRegistered(QTiltSensor::type, GenericTiltSensor::id)) QSensorManager::registerBackend(QTiltSensor::type, GenericTiltSensor::id, this); +#endif } else { -#ifndef Q_OS_BLACKBERRY +#ifdef QTSENSORS_GENERICORIENTATIONSENSOR if (QSensorManager::isBackendRegistered(QOrientationSensor::type, genericorientationsensor::id)) QSensorManager::unregisterBackend(QOrientationSensor::type, genericorientationsensor::id); +#endif +#ifdef QTSENSORS_GENERICROTATIONSENSOR if (QSensorManager::isBackendRegistered(QRotationSensor::type, genericrotationsensor::id)) QSensorManager::unregisterBackend(QRotationSensor::type, genericrotationsensor::id); +#endif +#ifdef QTSENSORS_GENERICALSSENSOR if (QSensorManager::isBackendRegistered(QAmbientLightSensor::type, genericalssensor::id)) QSensorManager::unregisterBackend(QAmbientLightSensor::type, genericalssensor::id); #endif +#ifdef QTSENSORS_GENERICTILTSENSOR if (QSensorManager::isBackendRegistered(QTiltSensor::type, GenericTiltSensor::id)) QSensorManager::unregisterBackend(QTiltSensor::type, GenericTiltSensor::id); +#endif } } QSensorBackend *createBackend(QSensor *sensor) { -#ifndef Q_OS_BLACKBERRY +#ifdef QTSENSORS_GENERICORIENTATIONSENSOR if (sensor->identifier() == genericorientationsensor::id) return new genericorientationsensor(sensor); - +#endif +#ifdef QTSENSORS_GENERICROTATIONSENSOR if (sensor->identifier() == genericrotationsensor::id) return new genericrotationsensor(sensor); - +#endif +#ifdef QTSENSORS_GENERICALSSENSOR if (sensor->identifier() == genericalssensor::id) return new genericalssensor(sensor); #endif +#ifdef QTSENSORS_GENERICTILTSENSOR if (sensor->identifier() == GenericTiltSensor::id) return new GenericTiltSensor(sensor); +#endif return 0; } diff --git a/src/plugins/sensors/sensors.pro b/src/plugins/sensors/sensors.pro index a8b59b0d..3822dafc 100644 --- a/src/plugins/sensors/sensors.pro +++ b/src/plugins/sensors/sensors.pro @@ -4,9 +4,13 @@ TEMPLATE = subdirs # of these. This reduces compile time and plugin loading time. blackberry { isEmpty(SENSORS_PLUGINS): SENSORS_PLUGINS = blackberry generic +} else: android { + SENSORS_PLUGINS = android generic } + isEmpty(SENSORS_PLUGINS)|contains(SENSORS_PLUGINS, dummy):SUBDIRS += dummy isEmpty(SENSORS_PLUGINS)|contains(SENSORS_PLUGINS, generic):SUBDIRS += generic isEmpty(SENSORS_PLUGINS)|contains(SENSORS_PLUGINS, simulator):simulator:SUBDIRS += simulator isEmpty(SENSORS_PLUGINS)|contains(SENSORS_PLUGINS, blackberry):blackberry:SUBDIRS += blackberry isEmpty(SENSORS_PLUGINS)|contains(SENSORS_PLUGINS, linux):linux:SUBDIRS += linux +isEmpty(SENSORS_PLUGINS)|contains(SENSORS_PLUGINS, android):android:SUBDIRS += android diff --git a/src/sensors/doc/src/compatmap.qdoc b/src/sensors/doc/src/compatmap.qdoc index abb71a15..8fe24adc 100644 --- a/src/sensors/doc/src/compatmap.qdoc +++ b/src/sensors/doc/src/compatmap.qdoc @@ -54,6 +54,7 @@ Feature Blackberry + Android Linux Generic @@ -61,6 +62,7 @@ Accelerometer + @@ -68,28 +70,33 @@ + Ambient Light Sensor + Ambient Temperature Sensor + Compass + Gyroscope + @@ -98,22 +105,26 @@ + IR Proximity Sensor + Light Sensor + Magnetometer + @@ -121,6 +132,7 @@ Orientation Sensor + @@ -128,16 +140,19 @@ + Proximity Sensor + Rotation Sensor + @@ -146,11 +161,20 @@ + + + + Tilt Sensor + + + + Sensor Gestures + 1) diff --git a/src/sensors/sensors.pro b/src/sensors/sensors.pro index b70c17c6..fe10a5e7 100644 --- a/src/sensors/sensors.pro +++ b/src/sensors/sensors.pro @@ -12,6 +12,11 @@ simulator { QMAKE_DOCS = $$PWD/doc/qtsensors.qdocconf +ANDROID_JAR_DEPENDENCIES = \ + jar/QtSensors.jar:org.qtproject.qt5.android.sensors.QtSensors +ANDROID_LIB_DEPENDENCIES = \ + plugins/sensors/libqtsensors_android.so + load(qt_module) PUBLIC_HEADERS += \ -- cgit v1.2.3