From 4558d134e0b612852d7360da2512f47be53bb305 Mon Sep 17 00:00:00 2001 From: Markus Bullmann Date: Tue, 12 Nov 2019 16:23:29 +0100 Subject: [PATCH] Added vibration --- RTT.pro | 6 ++- _android/AndroidManifest.xml | 73 ++++++++++++++++++------------------ main.cpp | 6 +++ main.qml | 4 ++ vibration.cpp | 53 ++++++++++++++++++++++++++ vibration.h | 29 ++++++++++++++ 6 files changed, 133 insertions(+), 38 deletions(-) create mode 100644 vibration.cpp create mode 100644 vibration.h diff --git a/RTT.pro b/RTT.pro index 0ed2d08..13c2d9b 100644 --- a/RTT.pro +++ b/RTT.pro @@ -20,7 +20,8 @@ DEFINES += QT_DEPRECATED_WARNINGS SOURCES += \ histogramchart.cpp \ main.cpp \ - Manager.cpp + Manager.cpp \ + vibration.cpp RESOURCES += qml.qrc @@ -50,7 +51,8 @@ else: unix:!android: target.path = /opt/$${TARGET}/bin HEADERS += \ Manager.h \ histogramchart.h \ - uwb.h + uwb.h \ + vibration.h DISTFILES += \ _android/src/UWB.java \ diff --git a/_android/AndroidManifest.xml b/_android/AndroidManifest.xml index 64f977d..7b823fa 100644 --- a/_android/AndroidManifest.xml +++ b/_android/AndroidManifest.xml @@ -1,49 +1,50 @@ - + - - - - - - - - - - + + + + + + + + + + - + - + - + - - - - + + + + - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + - + + diff --git a/main.cpp b/main.cpp index ffeb905..c8c57cc 100644 --- a/main.cpp +++ b/main.cpp @@ -5,6 +5,7 @@ #include #include "histogramchart.h" +#include "vibration.h" #include "Manager.h" extern Manager mgmt; @@ -18,10 +19,15 @@ int main(int argc, char *argv[]) { qmlRegisterType("Test", 1, 0, "HistogramChart"); + QQmlApplicationEngine engine; engine.rootContext()->setContextProperty("mgmt", &mgmt); + Vibration vibration; + engine.rootContext()->setContextProperty("Vibration", &vibration); + + engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); engine.rootObjects().first()->dumpObjectTree(); diff --git a/main.qml b/main.qml index f4b73e1..1874f02 100644 --- a/main.qml +++ b/main.qml @@ -39,6 +39,10 @@ Window { cbNUC.enabled = true; cbLogToDisk.enabled = true; cbMeasTime.enabled = true; + + if (cbMeasTime.currentIndex != 0) { + Vibration.vibrate(1000); + } } } } diff --git a/vibration.cpp b/vibration.cpp new file mode 100644 index 0000000..e1e6942 --- /dev/null +++ b/vibration.cpp @@ -0,0 +1,53 @@ +#include "vibration.h" + +#include + + +Vibration::Vibration(QObject *parent) : QObject(parent) +{ +#ifdef ANDROID + activity = QAndroidJniObject::callStaticObjectMethod("org/qtproject/qt5/android/QtNative", "activity", "()Landroid/app/Activity;"); + + QAndroidJniObject vibroString = QAndroidJniObject::fromString("vibrator"); + QAndroidJniObject appctx = activity.callObjectMethod("getApplicationContext","()Landroid/content/Context;"); + vibratorService = appctx.callObjectMethod("getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;", vibroString.object()); +#endif +} + + +void Vibration::vibrate(int milliseconds) +{ +#ifdef ANDROID + if (vibratorService.isValid()) { + jlong ms = milliseconds; + jboolean hasvibro = vibratorService.callMethod("hasVibrator", "()Z"); + + if (hasvibro) { + vibratorService.callMethod("vibrate", "(J)V", ms); + } + } else { + qDebug() << "No vibrator service available"; + } + + jint notifcationType = QAndroidJniObject::getStaticField("android/media/RingtoneManager", "TYPE_NOTIFICATION"); + QAndroidJniObject notification = QAndroidJniObject::callStaticObjectMethod("android/media/RingtoneManager", "getDefaultUri", "(I)Landroid/net/Uri;", notifcationType); + if ( notification.isValid() ) + { + // Note that package and class names needs to be separated with '/' and not '.' + QAndroidJniObject ring = QAndroidJniObject::callStaticObjectMethod("android/media/RingtoneManager", + "getRingtone", + "(Landroid/content/Context;Landroid/net/Uri;)Landroid/media/Ringtone;", + activity.object(), + notification.object()); + if ( ring.isValid() ) + { + ring.callMethod("play", "()V"); + } + } +#else + Q_UNUSED(milliseconds); +#endif +} + + + diff --git a/vibration.h b/vibration.h new file mode 100644 index 0000000..06cc621 --- /dev/null +++ b/vibration.h @@ -0,0 +1,29 @@ +#ifndef VIBRATION_H +#define VIBRATION_H + +#include + +#ifdef ANDROID +#include +#include +#endif + +class Vibration : public QObject +{ + Q_OBJECT +public: + explicit Vibration(QObject *parent = nullptr); + +signals: + +public slots: + void vibrate(int milliseconds); + +private: +#ifdef ANDROID + QAndroidJniObject activity; + QAndroidJniObject vibratorService; +#endif +}; + +#endif // VIBRATION_H