-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
181 lines (161 loc) · 4.26 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* @file main.cpp
*
* Sample application that illustrates how to call into C++
* from JavaScript.
*/
#include <Wormhole/WebAppMoblet.h>
#include <conprint.h>
#include "MessageProtocol.h"
#include "MessageStream.h"
#include "MessageStreamJSON.h"
#include "NativeUIMessageHandler.h"
#include "ResourceMessageHandler.h"
// Namespaces we want to access.
using namespace MAUtil; // Class Moblet
using namespace NativeUI; // WebView widget.
using namespace Wormhole; // Class WebAppMoblet
/**
* The application class.
*/
class MyMoblet : public WebAppMoblet
{
public:
MyMoblet()
{
// Create message handler for NativeUI.
mNativeUIMessageHandler = new NativeUIMessageHandler(getWebView());
// Create message handler for Resources.
mResourceMessageHandler = new ResourceMessageHandler(getWebView());
// Enable message sending from JavaScript to C++.
enableWebViewMessages();
//The webview that processes our Javascript code it hidden
// users can create other webviews from html
getWebView()->disableZoom();
getWebView()->setVisible(false);
// Remove this line to enable the user to
// zoom the web page. To disable zoom is one
// way of making web pages display in a
// reasonable degault size on devices with
// different screen sizes.
getWebView()->enableZoom();
// The page in the "LocalFiles" folder to
// show when the application starts.
showPage("index.html");
//Send the Device Screen size to JavaScript
MAExtent scrSize = maGetScrSize();
int width = EXTENT_X(scrSize);
int height = EXTENT_Y(scrSize);
char buf[512];
sprintf(
buf,
"{mosyncScreenWidth=%d, mosyncScreenHeight = %d;}",
width,
height);
lprintfln(buf);
callJS(buf);
}
/**
* This method is called when a key is pressed. It closes
* the application when the back key (on Android) is pressed.
* Forwards the event to JavaScript by Calling the same function
* Implement the function to handle events in JavaScript
*/
void keyPressEvent(int keyCode, int nativeCode)
{
char buffer[256];
// forward the event to application
sprintf(buffer,
"keyPressEvent(%d, %d)",
keyCode,
nativeCode);
callJS(buffer);
}
/**
* This method handles messages sent from the WebView.
* @param webView The WebView that sent the message.
* @param urlData Data object that holds message content.
* Note that the data object will be valid only during
* the life-time of the call of this method, then it
* will be deallocated.
*/
void handleWebViewMessage(WebView* webView, MAHandle data)
{
Wormhole::MessageProtocol protocol(data);
if (protocol.isMessageStream())
{
handleMessageStream(webView, data);
}
else if (protocol.isMessageArrayJSON())
{
handleMessageStreamJSON(webView, data);
}
else
{
lprintfln("undefined message protocol");
}
}
void handleMessageStream(WebView* webView, MAHandle data)
{
Wormhole::MessageStream stream(webView, data);
const char* p;
while (p = stream.getNext())
{
if (0 == strcmp(p, "NativeUI"))
{
lprintfln("received a NativeUI Message .......");
//Forward NativeUI messages to the respective message handler
mNativeUIMessageHandler->handleMessage(stream);
}
else if (0 == strcmp(p, "Resource"))
{
//Forward Resource messages to the respective message handler
mResourceMessageHandler->handleMessage(stream);
}
else if (0 == strcmp(p, "close"))
{
close();
}
}
}
void handleMessageStreamJSON(WebView* webView, MAHandle data)
{
Wormhole::MessageStreamJSON message(webView, data);
while (message.next())
{
handleJSONMessage(message);
}
}
void handleJSONMessage(Wormhole::MessageStreamJSON& message)
{
if (message.is("JSONMessage"))
{
// No action.
}
else if (message.is("JSONMessageEnd"))
{
callJS("JSONMessageEnd()");
}
else if (message.is("JSONRoundtripMessage"))
{
callJS("JSONRoundtripCallback()");
}
else
{
lprintfln("@@@ C++ Unknown message");
}
}
private:
NativeUIMessageHandler* mNativeUIMessageHandler;
ResourceMessageHandler* mResourceMessageHandler;
};
/**
* Main function that is called when the program starts.
* Here an instance of the MyMoblet class is created and
* the program enters the main event loop.
*/
extern "C" int MAMain()
{
Moblet::run(new MyMoblet());
return 0;
}