diff options
author | Peter Rustler <peter.rustler@basyskom.com> | 2015-01-09 15:05:30 +0100 |
---|---|---|
committer | Alex Blasche <alexander.blasche@theqtcompany.com> | 2015-02-02 08:01:40 +0000 |
commit | 454fb59a5146b4b27756c7787e80681167819a63 (patch) | |
tree | 46da70e8fe05c040f8501293a95e0cdb8aca9c41 /src/nfc/android | |
parent | dd0f27136aa1d788c40631d9766fa9d1f11da4e6 (diff) |
Adapt to the new "onNewIntent" API
In Anroid the foreground activity get an intent by
implemmenting onNewIntent callback. This callback will
be called from android when a nfc tag is in range.
To support this callback an API was added in qtbase.
This patch implement an listener of this API to get
nfc events.
Change-Id: I9ecadf6db53fe1269cb9e66f0062e9ce1ec5a95e
Reviewed-by: Alex Blasche <alexander.blasche@theqtcompany.com>
Diffstat (limited to 'src/nfc/android')
-rw-r--r-- | src/nfc/android/androidjninfc.cpp | 39 | ||||
-rw-r--r-- | src/nfc/android/androidmainnewintentlistener.cpp | 87 | ||||
-rw-r--r-- | src/nfc/android/androidmainnewintentlistener_p.h | 68 |
3 files changed, 160 insertions, 34 deletions
diff --git a/src/nfc/android/androidjninfc.cpp b/src/nfc/android/androidjninfc.cpp index 6a46c422..424db381 100644 --- a/src/nfc/android/androidjninfc.cpp +++ b/src/nfc/android/androidjninfc.cpp @@ -43,9 +43,9 @@ #include <android/log.h> +#include "androidmainnewintentlistener_p.h" + #include "qglobal.h" -#include "qlist.h" -#include "qreadwritelock.h" #include "qbytearray.h" #include "qdebug.h" @@ -56,8 +56,7 @@ static jmethodID startDiscoveryId; static jmethodID stopDiscoveryId; static jmethodID isAvailableId; -static QList<AndroidNfc::AndroidNfcListenerInterface*> listeners; -QReadWriteLock listenersLock; +static AndroidNfc::MainNfcNewIntentListener mainListener; QT_BEGIN_ANDROIDNFC_NAMESPACE @@ -114,18 +113,12 @@ bool stopDiscovery() bool registerListener(AndroidNfcListenerInterface *listener) { - listenersLock.lockForWrite(); - listeners.push_back(listener); - listenersLock.unlock(); - return true; + return mainListener.registerListener(listener); } bool unregisterListener(AndroidNfcListenerInterface *listener) { - listenersLock.lockForWrite(); - listeners.removeOne(listener); - listenersLock.unlock(); - return true; + return mainListener.unregisterListener(listener); } jobject getTag(JNIEnv *env, jobject intent) @@ -159,23 +152,6 @@ 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 newIntent(JNIEnv *env, jobject obj, jobject intent) -{ - Q_UNUSED(obj); - listenersLock.lockForRead(); - foreach (AndroidNfc::AndroidNfcListenerInterface *listener, listeners) { - // Making new global reference for each listener. - // Listeners must release reference when it is not used anymore. - jobject newIntentRef = env->NewGlobalRef(intent); - listener->newIntent(newIntentRef); - } - listenersLock.unlock(); -} - -static JNINativeMethod methods[] = { - {"newIntent", "(Landroid/content/Intent;)V", (void*)newIntent} -}; - #define FIND_AND_CHECK_CLASS(CLASS_NAME) \ clazz = env->FindClass(CLASS_NAME); \ if (!clazz) { \ @@ -200,11 +176,6 @@ Q_DECL_EXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void * /*reserved*/) FIND_AND_CHECK_CLASS("org/qtproject/qt5/android/nfc/QtNfc"); nfcClass = static_cast<jclass>(env->NewGlobalRef(clazz)); - if (env->RegisterNatives(nfcClass, methods, sizeof(methods) / sizeof(methods[0])) < 0) { - __android_log_print(ANDROID_LOG_FATAL, logTag, "RegisterNatives failed"); - return JNI_FALSE; - } - GET_AND_CHECK_STATIC_METHOD(startDiscoveryId, nfcClass, "start", "()V"); GET_AND_CHECK_STATIC_METHOD(stopDiscoveryId, nfcClass, "stop", "()V"); GET_AND_CHECK_STATIC_METHOD(isAvailableId, nfcClass, "isAvailable", "()Z"); diff --git a/src/nfc/android/androidmainnewintentlistener.cpp b/src/nfc/android/androidmainnewintentlistener.cpp new file mode 100644 index 00000000..9146f70e --- /dev/null +++ b/src/nfc/android/androidmainnewintentlistener.cpp @@ -0,0 +1,87 @@ +/**************************************************************************** +** +** Copyright (C) 2015 BasysKom GmbH +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtNfc 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 "androidmainnewintentlistener_p.h" + +QT_BEGIN_ANDROIDNFC_NAMESPACE + +MainNfcNewIntentListener::MainNfcNewIntentListener() + : listeners(), listenersLock() +{ + QtAndroidPrivate::registerNewIntentListener(this); +} + +MainNfcNewIntentListener::~MainNfcNewIntentListener() +{ + QtAndroidPrivate::unregisterNewIntentListener(this); +} + +bool MainNfcNewIntentListener::handleNewIntent(JNIEnv *env, jobject intent) +{ + AndroidNfc::AttachedJNIEnv aenv; + listenersLock.lockForRead(); + foreach (AndroidNfc::AndroidNfcListenerInterface *listener, listeners) { + // Making new global reference for each listener. + // Listeners must release reference when it is not used anymore. + jobject newIntentRef = env->NewGlobalRef(intent); + listener->newIntent(newIntentRef); + } + listenersLock.unlock(); + return true; +} + +bool MainNfcNewIntentListener::registerListener(AndroidNfcListenerInterface *listener) +{ + listenersLock.lockForWrite(); + listeners.push_back(listener); + listenersLock.unlock(); + return true; +} + +bool MainNfcNewIntentListener::unregisterListener(AndroidNfcListenerInterface *listener) +{ + listenersLock.lockForWrite(); + listeners.removeOne(listener); + listenersLock.unlock(); + return true; +} + +QT_END_ANDROIDNFC_NAMESPACE diff --git a/src/nfc/android/androidmainnewintentlistener_p.h b/src/nfc/android/androidmainnewintentlistener_p.h new file mode 100644 index 00000000..34541214 --- /dev/null +++ b/src/nfc/android/androidmainnewintentlistener_p.h @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** Copyright (C) 2015 BasysKom GmbH +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtNfc 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 ANDROIDMAINNEWINTENTLISTENER_P_H_ +#define ANDROIDMAINNEWINTENTLISTENER_P_H_ + +#include <QtCore/private/qjnihelpers_p.h> + +#include "qlist.h" +#include "qreadwritelock.h" +#include "androidjninfc_p.h" + +QT_BEGIN_ANDROIDNFC_NAMESPACE + +class MainNfcNewIntentListener : public QtAndroidPrivate::NewIntentListener +{ +public: + MainNfcNewIntentListener(); + ~MainNfcNewIntentListener(); + bool handleNewIntent(JNIEnv *env, jobject intent); + bool registerListener(AndroidNfcListenerInterface *listener); + bool unregisterListener(AndroidNfcListenerInterface *listener); +protected: + QList<AndroidNfc::AndroidNfcListenerInterface*> listeners; + QReadWriteLock listenersLock; +}; + +QT_END_ANDROIDNFC_NAMESPACE + +#endif /* ANDROIDMAINNEWINTENTLISTENER_P_H_ */ |