diff options
25 files changed, 1434 insertions, 8 deletions
diff --git a/src/plugins/sensors/android/android.pro b/src/plugins/sensors/android/android.pro new file mode 100644 index 0000000..0dc6a3f --- /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 0000000..5d15cab --- /dev/null +++ b/src/plugins/sensors/android/jar/AndroidManifest.xml @@ -0,0 +1,6 @@ +<manifest xmlns:android="http://schemas.android.com/apk/res/android" + package="org.qtproject.qt5.android.sensors" + android:versionCode="1" + android:versionName="1.0" > + <supports-screens android:largeScreens="true" android:normalScreens="true" android:anyDensity="true" android:smallScreens="true"/> +</manifest> diff --git a/src/plugins/sensors/android/jar/jar.pro b/src/plugins/sensors/android/jar/jar.pro new file mode 100644 index 0000000..a935074 --- /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 0000000..c7956d2 --- /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 <bogdan@kde.org> +** 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<Sensor> m_registeredSensors = new SparseArray<Sensor>(); + 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<Sensor> list = m_sensorManager.getSensorList(Sensor.TYPE_ALL); + HashSet<Integer> filteredList = new HashSet<Integer>(); + for (Sensor s : list) + filteredList.add(s.getType()); + int retList[] = new int[filteredList.size()]; + int pos = 0; + for (int type : filteredList) + retList[pos++] = type; + return retList; + } catch(Exception e) { + e.printStackTrace(); + } + return null; + } + + private static 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 0000000..b64536e --- /dev/null +++ b/src/plugins/sensors/android/src/androidaccelerometer.cpp @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2013 BogDan Vatra <bogdan@kde.org> +** 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<QAccelerometerReading>(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 0000000..6c95fdb --- /dev/null +++ b/src/plugins/sensors/android/src/androidaccelerometer.h @@ -0,0 +1,59 @@ +/**************************************************************************** +** +** Copyright (C) 2013 BogDan Vatra <bogdan@kde.org> +** 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 <qaccelerometer.h> + +#include "androidcommonsensor.h" + +class AndroidAccelerometer : public AndroidCommonSensor<QAccelerometerReading> +{ +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 0000000..40f118e --- /dev/null +++ b/src/plugins/sensors/android/src/androidcommonsensor.h @@ -0,0 +1,88 @@ +/**************************************************************************** +** +** Copyright (C) 2013 BogDan Vatra <bogdan@kde.org> +** 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 <qsensorbackend.h> +#include <qsensor.h> +#include "androidjnisensors.h" + +template <typename ReaderType> +class AndroidCommonSensor : public QSensorBackend, protected AndroidSensors::AndroidSensorsListenerInterface +{ +public: + AndroidCommonSensor(AndroidSensors::AndroidSensorType type, QSensor *sensor) : QSensorBackend(sensor) + { + setDescription(AndroidSensors::sensorDescription(type)); + setReading<ReaderType>(&m_reader); + m_type = type; + m_isStarted = false; + } + + virtual ~AndroidCommonSensor() + { + if (m_isStarted) + stop(); + } + 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 0000000..69a05c1 --- /dev/null +++ b/src/plugins/sensors/android/src/androidgyroscope.cpp @@ -0,0 +1,64 @@ +/**************************************************************************** +** +** Copyright (C) 2013 BogDan Vatra <bogdan@kde.org> +** 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 <math.h> + +AndroidGyroscope::AndroidGyroscope(AndroidSensors::AndroidSensorType type, QSensor *sensor) + : AndroidCommonSensor<QGyroscopeReading>(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 0000000..3b633b7 --- /dev/null +++ b/src/plugins/sensors/android/src/androidgyroscope.h @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2013 BogDan Vatra <bogdan@kde.org> +** 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 <qgyroscope.h> + +#include "androidcommonsensor.h" + +class AndroidGyroscope : public AndroidCommonSensor<QGyroscopeReading> +{ +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 0000000..6365433 --- /dev/null +++ b/src/plugins/sensors/android/src/androidjnisensors.cpp @@ -0,0 +1,245 @@ +/**************************************************************************** +** +** Copyright (C) 2013 BogDan Vatra <bogdan@kde.org> +** 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 <qglobal.h> +#include <android/log.h> +#include <QtCore/QReadWriteLock> +#include <QtCore/QHash> + +#include "androidjnisensors.h" + +static JavaVM *javaVM = 0; +jclass sensorsClass; + +static jmethodID getSensorListMethodId; +static jmethodID registerSensorMethodId; +static jmethodID unregisterSensorMethodId; +static jmethodID getSensorDescriptionMethodId; + +static QHash<int, QList<AndroidSensors::AndroidSensorsListenerInterface *> > listenersHash; +QReadWriteLock listenersLocker; +enum { + SENSOR_DELAY_FASTEST = 0, + SENSOR_DELAY_GAME = 1, + SENSOR_DELAY_NORMAL = 3, + SENSOR_DELAY_UI =2 +}; +namespace AndroidSensors +{ + struct AttachedJNIEnv + { + AttachedJNIEnv() + { + attached = false; + if (javaVM->GetEnv((void**)&jniEnv, JNI_VERSION_1_6) < 0) { + if (javaVM->AttachCurrentThread(&jniEnv, NULL) < 0) { + __android_log_print(ANDROID_LOG_ERROR, "Qt", "AttachCurrentThread failed"); + jniEnv = 0; + return; + } + attached = true; + } + } + + ~AttachedJNIEnv() + { + if (attached) + javaVM->DetachCurrentThread(); + } + bool attached; + JNIEnv *jniEnv; + }; + + QVector<AndroidSensorType> availableSensors() + { + QVector<AndroidSensorType> ret; + AttachedJNIEnv aenv; + if (!aenv.jniEnv) + return ret; + jintArray jsensors = static_cast<jintArray>(aenv.jniEnv->CallStaticObjectMethod(sensorsClass, + getSensorListMethodId)); + jint *sensors = aenv.jniEnv->GetIntArrayElements(jsensors, 0); + const uint sz = aenv.jniEnv->GetArrayLength(jsensors); + for (uint i = 0; i < sz; i++) + ret.push_back(AndroidSensorType(sensors[i])); + aenv.jniEnv->ReleaseIntArrayElements(jsensors, sensors, 0); + return ret; + } + + QString sensorDescription(AndroidSensorType sensor) + { + AttachedJNIEnv aenv; + if (!aenv.jniEnv) + return QString(); + jstring jstr = static_cast<jstring>(aenv.jniEnv->CallStaticObjectMethod(sensorsClass, + getSensorDescriptionMethodId, + jint(sensor))); + const jchar *pstr = aenv.jniEnv->GetStringChars(jstr, 0); + QString ret(reinterpret_cast<const QChar *>(pstr), aenv.jniEnv->GetStringLength(jstr)); + aenv.jniEnv->ReleaseStringChars(jstr, pstr); + aenv.jniEnv->DeleteLocalRef(jstr); + return ret; + } + + 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<jclass>(env->NewGlobalRef(clazz)); + + if (env->RegisterNatives(sensorsClass, methods, sizeof(methods) / sizeof(methods[0])) < 0) { + __android_log_print(ANDROID_LOG_FATAL, logTag, "RegisterNatives failed"); + return JNI_FALSE; + } + + GET_AND_CHECK_STATIC_METHOD(getSensorListMethodId, sensorsClass, "getSensorList", "()[I"); + GET_AND_CHECK_STATIC_METHOD(registerSensorMethodId, sensorsClass, "registerSensor", "(II)Z"); + GET_AND_CHECK_STATIC_METHOD(unregisterSensorMethodId, sensorsClass, "unregisterSensor", "(I)Z"); + GET_AND_CHECK_STATIC_METHOD(getSensorDescriptionMethodId, sensorsClass, "getSensorDescription", "(I)Ljava/lang/String;"); + + 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 0000000..53ca58d --- /dev/null +++ b/src/plugins/sensors/android/src/androidjnisensors.h @@ -0,0 +1,83 @@ +/**************************************************************************** +** +** Copyright (C) 2013 BogDan Vatra <bogdan@kde.org> +** 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 <jni.h> + +#include <QtCore/QVector> +#include <QtCore/QString> + +namespace AndroidSensors +{ + // must be in sync with https://developer.android.com/reference/android/hardware/Sensor.html#TYPE_ACCELEROMETER + enum AndroidSensorType + { + TYPE_ACCELEROMETER = 1, + TYPE_AMBIENT_TEMPERATURE = 13, + 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<AndroidSensorType> 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 0000000..42295e5 --- /dev/null +++ b/src/plugins/sensors/android/src/androidlight.cpp @@ -0,0 +1,62 @@ +/**************************************************************************** +** +** Copyright (C) 2013 BogDan Vatra <bogdan@kde.org> +** 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<QLightReading>(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 0000000..fdea3ed --- /dev/null +++ b/src/plugins/sensors/android/src/androidlight.h @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2013 BogDan Vatra <bogdan@kde.org> +** 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 <qlightsensor.h> + +#include "androidcommonsensor.h" + +class AndroidLight : public AndroidCommonSensor<QLightReading> +{ +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 0000000..9175f58 --- /dev/null +++ b/src/plugins/sensors/android/src/androidmagnetometer.cpp @@ -0,0 +1,63 @@ +/**************************************************************************** +** +** Copyright (C) 2013 BogDan Vatra <bogdan@kde.org> +** 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<QMagnetometerReading>(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 0000000..57d9f36 --- /dev/null +++ b/src/plugins/sensors/android/src/androidmagnetometer.h @@ -0,0 +1,57 @@ +/**************************************************************************** +** +** Copyright (C) 2013 BogDan Vatra <bogdan@kde.org> +** 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 <qmagnetometer.h> + +#include "androidcommonsensor.h" + +class AndroidMagnetometer : public AndroidCommonSensor<QMagnetometerReading> +{ +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 0000000..602575f --- /dev/null +++ b/src/plugins/sensors/android/src/androidrotation.cpp @@ -0,0 +1,65 @@ +/**************************************************************************** +** +** Copyright (C) 2013 BogDan Vatra <bogdan@kde.org> +** 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 <math.h> + +AndroidRotation::AndroidRotation(AndroidSensors::AndroidSensorType type, QSensor *sensor) + : AndroidCommonSensor<QRotationReading>(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 0000000..cd2c4d6 --- /dev/null +++ b/src/plugins/sensors/android/src/androidrotation.h @@ -0,0 +1,56 @@ +/**************************************************************************** +** +** Copyright (C) 2013 BogDan Vatra <bogdan@kde.org> +** 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 <qrotationsensor.h> + +#include "androidcommonsensor.h" +class AndroidRotation : public AndroidCommonSensor<QRotationReading> +{ +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 0000000..0cb4de4 --- /dev/null +++ b/src/plugins/sensors/android/src/main.cpp @@ -0,0 +1,135 @@ +/**************************************************************************** +** +** Copyright (C) 2013 BogDan Vatra <bogdan@kde.org> +** 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 <qplugin.h> +#include <qsensorplugin.h> +#include <qsensorbackend.h> +#include <qsensormanager.h> +#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<AndroidSensorType>(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 0000000..8a55b3a --- /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 0000000..715a011 --- /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 5bfeff6..fcc1bfc 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 4b348b1..aa9f58c 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 <qsensorplugin.h> #include <qsensorbackend.h> #include <qsensormanager.h> @@ -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 a8b59b0..3822daf 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 abb71a1..8fe24ad 100644 --- a/src/sensors/doc/src/compatmap.qdoc +++ b/src/sensors/doc/src/compatmap.qdoc @@ -54,6 +54,7 @@ <tr> <td><b>Feature</b></td> <td><b>Blackberry</b></td> + <td><b>Android</b></td> <td><b>Linux</b></td> <td><b>Generic</b></td> </tr> @@ -61,6 +62,7 @@ <td nowrap="nowrap">Accelerometer</td> <td bgcolor="green"></td> <td bgcolor="green"></td> + <td bgcolor="green"></td> <td bgcolor="gray"></td> </tr> <tr> @@ -68,28 +70,33 @@ <td bgcolor="green"></td> <td bgcolor="gray"></td> <td bgcolor="gray"></td> + <td bgcolor="gray"></td> </tr> <tr> <td nowrap="nowrap">Ambient Light Sensor</td> <td bgcolor="green"></td> <td bgcolor="gray"></td> <td bgcolor="gray"></td> + <td bgcolor="gray"></td> </tr> <tr> <td nowrap="nowrap">Ambient Temperature Sensor</td> <td bgcolor="gray"></td> <td bgcolor="gray"></td> <td bgcolor="gray"></td> + <td bgcolor="gray"></td> </tr> <tr> <td nowrap="nowrap">Compass</td> <td bgcolor="green"></td> <td bgcolor="gray"></td> <td bgcolor="gray"></td> + <td bgcolor="gray"></td> </tr> <tr> <td nowrap="nowrap">Gyroscope</td> <td bgcolor="green"></td> + <td bgcolor="green"></td> <td bgcolor="gray"></td> <td bgcolor="gray"></td> </tr> @@ -98,22 +105,26 @@ <td bgcolor="green"></td> <td bgcolor="gray"></td> <td bgcolor="gray"></td> + <td bgcolor="gray"></td> </tr> <tr> <td nowrap="nowrap">IR Proximity Sensor</td> <td bgcolor="green"></td> <td bgcolor="gray"></td> <td bgcolor="gray"></td> + <td bgcolor="gray"></td> </tr> <tr> <td nowrap="nowrap">Light Sensor</td> <td bgcolor="green"></td> + <td bgcolor="green"></td> <td bgcolor="gray"></td> <td bgcolor="gray"></td> </tr> <tr> <td nowrap="nowrap">Magnetometer</td> <td bgcolor="green"></td> + <td bgcolor="green"></td> <td bgcolor="gray"></td> <td bgcolor="gray"></td> </tr> @@ -121,6 +132,7 @@ <td nowrap="nowrap">Orientation Sensor</td> <td bgcolor="green"></td> <td bgcolor="gray"></td> + <td bgcolor="gray"></td> <td bgcolor="green"></td> </tr> <tr> @@ -128,16 +140,19 @@ <td bgcolor="green"></td> <td bgcolor="gray"></td> <td bgcolor="gray"></td> + <td bgcolor="gray"></td> </tr> <tr> <td nowrap="nowrap">Proximity Sensor</td> <td bgcolor="green"></td> <td bgcolor="gray"></td> <td bgcolor="gray"></td> + <td bgcolor="gray"></td> </tr> <tr> <td nowrap="nowrap">Rotation Sensor</td> <td bgcolor="green"></td> + <td bgcolor="green"></td> <td bgcolor="gray"></td> <td bgcolor="green"></td> </tr> @@ -146,11 +161,20 @@ <td bgcolor="gray"></td> <td bgcolor="gray"></td> <td bgcolor="gray"></td> + <td bgcolor="gray"></td> + </tr> + <tr> + <td nowrap="nowrap">Tilt Sensor</td> + <td bgcolor="gray"></td> + <td bgcolor="gray"></td> + <td bgcolor="gray"></td> + <td bgcolor="green"></td> </tr> <tr> <td nowrap="nowrap">Sensor Gestures</td> <td bgcolor="gray"></td> <td bgcolor="gray"></td> + <td bgcolor="gray"></td> <td bgcolor="green">1)</td> </tr> </table> diff --git a/src/sensors/sensors.pro b/src/sensors/sensors.pro index b70c17c..fe10a5e 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 += \ |