Commit 9b6aeb95 authored by BogDan Vatra's avatar BogDan Vatra
Browse files

Allow the user to easily register a binder creator


Until now the users were forced to subclass QAndroidService in order to
provide the binder. Now is much easier, the user just pass a lambda in
the QAndroidService constructor e.g.

QAndroidService app(argc, argv, [](const QAndroidIntent &){ return new
MyBinder{};});

Change-Id: I97608f806b311ad3c853a86cde132aea8352349b
Reviewed-by: default avatarEskil Abrahamsen Blomfeldt <eskil.abrahamsen-blomfeldt@qt.io>
parent bb3c2b90
No related merge requests found
Showing with 27 additions and 5 deletions
......@@ -53,8 +53,9 @@ QT_BEGIN_NAMESPACE
class QAndroidServicePrivate : public QObject, public QtAndroidPrivate::OnBindListener
{
public:
QAndroidServicePrivate(QAndroidService *service)
QAndroidServicePrivate(QAndroidService *service, const std::function<QAndroidBinder *(const QAndroidIntent &)> &binder = {})
: m_service(service)
, m_binder(binder)
{
QtAndroidPrivate::setOnBindListener(this);
}
......@@ -73,7 +74,8 @@ public:
// OnBindListener interface
jobject onBind(jobject intent) override
{
auto binder = m_service->onBind(QAndroidIntent(intent));
auto qai = QAndroidIntent(intent);
auto binder = m_binder ? m_binder(qai) : m_service->onBind(qai);
if (binder) {
{
QMutexLocker lock(&m_bindersMutex);
......@@ -92,8 +94,9 @@ private:
m_binders.remove(obj);
}
private:
QAndroidService *m_service;
public:
QAndroidService *m_service = nullptr;
std::function<QAndroidBinder *(const QAndroidIntent &)> m_binder;
QMutex m_bindersMutex;
QSet<QAndroidBinder*> m_binders;
};
......@@ -116,7 +119,19 @@ private:
*/
QAndroidService::QAndroidService(int &argc, char **argv, int flags)
: QCoreApplication (argc, argv, QtAndroidPrivate::acuqireServiceSetup(flags))
, d(new QAndroidServicePrivate(this))
, d(new QAndroidServicePrivate{this})
{
}
/*!
Creates a new Android Service
\a binder is used to create a binder each when is needed
\sa QCoreApplication
*/
QAndroidService::QAndroidService(int &argc, char **argv, const std::function<QAndroidBinder *(const QAndroidIntent &)> &binder, int flags)
: QCoreApplication (argc, argv, QtAndroidPrivate::acuqireServiceSetup(flags))
, d(new QAndroidServicePrivate{this, binder})
{
}
......
......@@ -43,6 +43,7 @@
#include <QtAndroidExtras/qandroidextrasglobal.h>
#include <QCoreApplication>
#include <QSharedPointer>
#include <functional>
QT_BEGIN_NAMESPACE
class QAndroidServicePrivate;
......@@ -54,6 +55,12 @@ public:
QAndroidService(int &argc, char **argv
#ifndef Q_QDOC
, int flags = ApplicationFlags
#endif
);
QAndroidService(int &argc, char **argv,
const std::function<QAndroidBinder*(const QAndroidIntent &intent)> & binder
#ifndef Q_QDOC
, int flags = ApplicationFlags
#endif
);
virtual ~QAndroidService();
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment