Skip to content
This repository was archived by the owner on Apr 25, 2024. It is now read-only.

Commit 6d29bbf

Browse files
committed
Add QASystemDispatcherProxy as a proxy between QML and C++ for using QASystemDispatcher
1 parent 6c93d74 commit 6d29bbf

File tree

9 files changed

+119
-17
lines changed

9 files changed

+119
-17
lines changed

examples/quickandroidexample/Components.qml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ Activity {
5757
demo : "switch/SwitchDemo.qml"
5858
}
5959

60+
ListElement {
61+
name : "Notification"
62+
preview : ""
63+
demo : "notification/NotificationDemo.qml"
64+
}
65+
6066
}
6167

6268
ListView {

examples/quickandroidexample/android-sources/src/quickandroid/example/ExampleActivity.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,12 @@ public void run() {
5454
m_builder.setSmallIcon(R.drawable.icon);
5555
}
5656

57-
Log.d("",String.format("notificationManagerNotify(%s)",message));
58-
5957
m_builder.setContentTitle(title);
6058
m_builder.setContentText(message);
6159
m_notificationManager.notify(1, m_builder.build());
6260

6361
// Test function. Remove it later.
64-
SystemDispatcher.dispatch("notificationManagerNotifyFinished");
62+
SystemDispatcher.dispatch("Notifier.notifyFinished");
6563
} catch (Exception e) {
6664
Log.d("",e.getMessage());
6765
}
@@ -93,9 +91,8 @@ public void run() {
9391
}
9492

9593
public void onDispatched(String name , Map data) {
96-
Log.d("","Listener::post");
9794

98-
if (name.equals("notificationManagerNotify")) {
95+
if (name.equals("Notifier.notify")) {
9996
notificationManagerNotify(data);
10097
return;
10198
} else if (name.equals("hapticFeedbackPerform")) {

examples/quickandroidexample/main.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,9 @@ int main(int argc, char *argv[])
4040
view.setSource(QUrl(QStringLiteral("qrc:///main.qml")));
4141
view.show();
4242

43-
4443
/* Testing Code. Not needed for regular project */
4544
Automator* automator = new Automator();
4645
automator->start();
4746

48-
QVariantMap message;
49-
message.clear();
50-
message["title"] = "Quick Android Example";
51-
message["message"] = "Hello! It is Quick Android Hello World";
52-
QASystemDispatcher::instance()->dispatch("notificationManagerNotify",message);
53-
54-
view.rootContext()->setContextProperty("SystemMessenger",QASystemDispatcher::instance());
55-
5647
return app.exec();
5748
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import QtQuick 2.2
2+
import QtQuick.Window 2.1
3+
import QuickAndroid 0.1
4+
import QuickAndroid.style 0.1
5+
6+
Activity {
7+
8+
actionBar: ActionBar {
9+
id : actionBar
10+
title: "Notification"
11+
z: 10
12+
upEnabled: true
13+
onActionButtonClicked: back();
14+
15+
MaterialShadow {
16+
asynchronous: true
17+
anchors.fill: actionBar
18+
depth: 1
19+
z: -1
20+
}
21+
}
22+
23+
24+
Text {
25+
id: label
26+
text : "Press to send notification"
27+
anchors.fill: parent
28+
verticalAlignment: Text.AlignVCenter
29+
horizontalAlignment: Text.AlignHCenter
30+
color : Style.theme.black87
31+
font.pixelSize: Style.theme.largeText.textSize * A.dp
32+
}
33+
34+
MouseArea {
35+
anchors.fill: parent
36+
onClicked: {
37+
SystemDispatcher.dispatch("Notifier.notify",{
38+
title: "Quick Android Example",
39+
message: "Hello!"
40+
});
41+
}
42+
}
43+
44+
}

examples/quickandroidexample/qml.qrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@
1919
<file>textinput/TextInputDemo.qml</file>
2020
<file>switch/SwitchDemo.qml</file>
2121
<file>switch/SwitchPreview.qml</file>
22+
<file>notification/NotificationDemo.qml</file>
2223
</qresource>
2324
</RCC>

priv/qasystemdispatcherproxy.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <QtQml>
2+
#include "qasystemdispatcherproxy.h"
3+
#include "qasystemdispatcher.h"
4+
5+
QASystemDispatcherProxy::QASystemDispatcherProxy(QObject *parent) : QObject(parent)
6+
{
7+
connect(QASystemDispatcher::instance(),SIGNAL(dispatched(QString,QVariantMap)),
8+
this,SIGNAL(dispatched(QString,QVariantMap)));
9+
10+
}
11+
12+
QASystemDispatcherProxy::~QASystemDispatcherProxy()
13+
{
14+
15+
}
16+
17+
void QASystemDispatcherProxy::dispatch(QString name, QVariantMap message)
18+
{
19+
QASystemDispatcher::instance()->dispatch(name,message);
20+
}
21+
22+
static QObject *provider(QQmlEngine *engine, QJSEngine *scriptEngine) {
23+
Q_UNUSED(engine);
24+
Q_UNUSED(scriptEngine);
25+
26+
QASystemDispatcherProxy* object = new QASystemDispatcherProxy();
27+
28+
return object;
29+
}
30+
31+
class QASystemDispatcherProxyRegisterHelper {
32+
33+
public:
34+
QASystemDispatcherProxyRegisterHelper() {
35+
qmlRegisterSingletonType<QASystemDispatcherProxy>("QuickAndroid", 0, 1,
36+
"SystemDispatcher", provider);
37+
}
38+
};
39+
40+
static QASystemDispatcherProxyRegisterHelper registerHelper;

priv/qasystemdispatcherproxy.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#ifndef QASYSTEMDISPATCHERPROXY_H
2+
#define QASYSTEMDISPATCHERPROXY_H
3+
4+
#include <QObject>
5+
#include <QVariantMap>
6+
7+
class QASystemDispatcherProxy : public QObject
8+
{
9+
Q_OBJECT
10+
public:
11+
explicit QASystemDispatcherProxy(QObject *parent = 0);
12+
~QASystemDispatcherProxy();
13+
14+
Q_INVOKABLE void dispatch(QString name, QVariantMap message);
15+
16+
signals:
17+
void dispatched(QString name , QVariantMap data);
18+
19+
};
20+
21+
#endif // QASYSTEMDISPATCHERPROXY_H

qasystemdispatcher.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ static jobject createHashMap(const QVariantMap &data) {
116116
while (iter.hasNext()) {
117117
iter.next();
118118

119-
qDebug() << iter.key() << iter.value();
119+
// qDebug() << iter.key() << iter.value();
120120
QString key = iter.key();
121121
jstring jkey = env->NewStringUTF(key.toLocal8Bit().data());
122122
QVariant v = iter.value();

quickandroid.pri

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ RESOURCES += $$PWD/QuickAndroid/quickandroid.qrc
77
HEADERS += \
88
$$PWD/quickandroid.h \
99
$$PWD/qadrawableprovider.h \
10-
$$PWD/qasystemdispatcher.h
10+
$$PWD/qasystemdispatcher.h \
11+
$$PWD/priv/qasystemdispatcherproxy.h
1112

1213
SOURCES += \
1314
$$PWD/quickandroid.cpp \
1415
$$PWD/qadrawableprovider.cpp \
15-
$$PWD/qasystemdispatcher.cpp
16+
$$PWD/qasystemdispatcher.cpp \
17+
$$PWD/priv/qasystemdispatcherproxy.cpp
1618

1719
android {
1820
QT += androidextras

0 commit comments

Comments
 (0)