mobile_template 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (211) hide show
  1. data/.gitignore +17 -0
  2. data/Gemfile +4 -0
  3. data/LICENSE +22 -0
  4. data/README.md +28 -0
  5. data/Rakefile +3 -0
  6. data/bin/mobile_template +71 -0
  7. data/lib/mobile_template/version.rb +4 -0
  8. data/lib/mobile_template.rb +5 -0
  9. data/mobile_template.gemspec +21 -0
  10. data/templates/assets/Gemfile +4 -0
  11. data/templates/assets/config.rb +96 -0
  12. data/templates/assets/config.ru +4 -0
  13. data/templates/assets/source/images/vendor/ajax-loader.gif +0 -0
  14. data/templates/assets/source/images/vendor/ajax-loader.png +0 -0
  15. data/templates/assets/source/images/vendor/icons-18-black.png +0 -0
  16. data/templates/assets/source/images/vendor/icons-18-white.png +0 -0
  17. data/templates/assets/source/images/vendor/icons-36-black.png +0 -0
  18. data/templates/assets/source/images/vendor/icons-36-white.png +0 -0
  19. data/templates/assets/source/index.html.erb +2 -0
  20. data/templates/assets/source/javascripts/app/index.js.coffee +2 -0
  21. data/templates/assets/source/javascripts/application.js.coffee +5 -0
  22. data/templates/assets/source/javascripts/vendor/cordova.js +4841 -0
  23. data/templates/assets/source/javascripts/vendor/jquery.js +9267 -0
  24. data/templates/assets/source/javascripts/vendor/jquery.mobile.js +7410 -0
  25. data/templates/assets/source/layout.erb +24 -0
  26. data/templates/assets/source/stylesheets/application.css.scss +2 -0
  27. data/templates/assets/source/stylesheets/vendor/jquery.mobile.css.scss +1872 -0
  28. data/templates/cordova_android/.gitignore +18 -0
  29. data/templates/cordova_android/LICENSE +202 -0
  30. data/templates/cordova_android/NOTICE +5 -0
  31. data/templates/cordova_android/README.md +95 -0
  32. data/templates/cordova_android/VERSION +1 -0
  33. data/templates/cordova_android/bin/BOOM +4 -0
  34. data/templates/cordova_android/bin/autotest +2 -0
  35. data/templates/cordova_android/bin/bench +29 -0
  36. data/templates/cordova_android/bin/create +46 -0
  37. data/templates/cordova_android/bin/create.bat +1 -0
  38. data/templates/cordova_android/bin/create.js +88 -0
  39. data/templates/cordova_android/bin/create.xml +79 -0
  40. data/templates/cordova_android/bin/node_modules/.bin/cake +7 -0
  41. data/templates/cordova_android/bin/node_modules/.bin/coffee +7 -0
  42. data/templates/cordova_android/bin/node_modules/.bin/nodeunit +120 -0
  43. data/templates/cordova_android/bin/node_modules/coffee-script/.npmignore +11 -0
  44. data/templates/cordova_android/bin/node_modules/coffee-script/LICENSE +22 -0
  45. data/templates/cordova_android/bin/node_modules/coffee-script/README +48 -0
  46. data/templates/cordova_android/bin/node_modules/coffee-script/Rakefile +78 -0
  47. data/templates/cordova_android/bin/node_modules/coffee-script/bin/cake +7 -0
  48. data/templates/cordova_android/bin/node_modules/coffee-script/bin/coffee +7 -0
  49. data/templates/cordova_android/bin/node_modules/coffee-script/extras/jsl.conf +44 -0
  50. data/templates/cordova_android/bin/node_modules/coffee-script/lib/browser.js +75 -0
  51. data/templates/cordova_android/bin/node_modules/coffee-script/lib/cake.js +76 -0
  52. data/templates/cordova_android/bin/node_modules/coffee-script/lib/coffee-script.js +135 -0
  53. data/templates/cordova_android/bin/node_modules/coffee-script/lib/command.js +301 -0
  54. data/templates/cordova_android/bin/node_modules/coffee-script/lib/grammar.js +591 -0
  55. data/templates/cordova_android/bin/node_modules/coffee-script/lib/helpers.js +66 -0
  56. data/templates/cordova_android/bin/node_modules/coffee-script/lib/index.js +8 -0
  57. data/templates/cordova_android/bin/node_modules/coffee-script/lib/lexer.js +656 -0
  58. data/templates/cordova_android/bin/node_modules/coffee-script/lib/nodes.js +2289 -0
  59. data/templates/cordova_android/bin/node_modules/coffee-script/lib/optparse.js +111 -0
  60. data/templates/cordova_android/bin/node_modules/coffee-script/lib/parser.js +676 -0
  61. data/templates/cordova_android/bin/node_modules/coffee-script/lib/repl.js +123 -0
  62. data/templates/cordova_android/bin/node_modules/coffee-script/lib/rewriter.js +363 -0
  63. data/templates/cordova_android/bin/node_modules/coffee-script/lib/scope.js +120 -0
  64. data/templates/cordova_android/bin/node_modules/coffee-script/package.json +27 -0
  65. data/templates/cordova_android/bin/node_modules/nodeunit/.gitignore +5 -0
  66. data/templates/cordova_android/bin/node_modules/nodeunit/.npmignore +3 -0
  67. data/templates/cordova_android/bin/node_modules/nodeunit/CONTRIBUTORS.md +60 -0
  68. data/templates/cordova_android/bin/node_modules/nodeunit/LICENSE +19 -0
  69. data/templates/cordova_android/bin/node_modules/nodeunit/Makefile +126 -0
  70. data/templates/cordova_android/bin/node_modules/nodeunit/README.md +432 -0
  71. data/templates/cordova_android/bin/node_modules/nodeunit/bin/nodeunit +120 -0
  72. data/templates/cordova_android/bin/node_modules/nodeunit/bin/nodeunit.json +10 -0
  73. data/templates/cordova_android/bin/node_modules/nodeunit/deps/async.js +623 -0
  74. data/templates/cordova_android/bin/node_modules/nodeunit/deps/console.log.js +55 -0
  75. data/templates/cordova_android/bin/node_modules/nodeunit/deps/ejs.js +125 -0
  76. data/templates/cordova_android/bin/node_modules/nodeunit/deps/json2.js +483 -0
  77. data/templates/cordova_android/bin/node_modules/nodeunit/examples/browser/nodeunit.js +1757 -0
  78. data/templates/cordova_android/bin/node_modules/nodeunit/examples/browser/suite1.js +12 -0
  79. data/templates/cordova_android/bin/node_modules/nodeunit/examples/browser/suite2.js +13 -0
  80. data/templates/cordova_android/bin/node_modules/nodeunit/examples/browser/test.html +16 -0
  81. data/templates/cordova_android/bin/node_modules/nodeunit/img/example_fail.png +0 -0
  82. data/templates/cordova_android/bin/node_modules/nodeunit/img/example_pass.png +0 -0
  83. data/templates/cordova_android/bin/node_modules/nodeunit/index.js +3 -0
  84. data/templates/cordova_android/bin/node_modules/nodeunit/lib/assert.js +316 -0
  85. data/templates/cordova_android/bin/node_modules/nodeunit/lib/core.js +260 -0
  86. data/templates/cordova_android/bin/node_modules/nodeunit/lib/nodeunit.js +82 -0
  87. data/templates/cordova_android/bin/node_modules/nodeunit/lib/reporters/browser.js +119 -0
  88. data/templates/cordova_android/bin/node_modules/nodeunit/lib/reporters/default.js +123 -0
  89. data/templates/cordova_android/bin/node_modules/nodeunit/lib/reporters/html.js +107 -0
  90. data/templates/cordova_android/bin/node_modules/nodeunit/lib/reporters/index.js +9 -0
  91. data/templates/cordova_android/bin/node_modules/nodeunit/lib/reporters/junit.js +183 -0
  92. data/templates/cordova_android/bin/node_modules/nodeunit/lib/reporters/minimal.js +112 -0
  93. data/templates/cordova_android/bin/node_modules/nodeunit/lib/reporters/skip_passed.js +105 -0
  94. data/templates/cordova_android/bin/node_modules/nodeunit/lib/track.js +48 -0
  95. data/templates/cordova_android/bin/node_modules/nodeunit/lib/types.js +187 -0
  96. data/templates/cordova_android/bin/node_modules/nodeunit/lib/utils.js +209 -0
  97. data/templates/cordova_android/bin/node_modules/nodeunit/man1/nodeunit.1 +95 -0
  98. data/templates/cordova_android/bin/node_modules/nodeunit/nodelint.cfg +4 -0
  99. data/templates/cordova_android/bin/node_modules/nodeunit/package.json +56 -0
  100. data/templates/cordova_android/bin/node_modules/nodeunit/share/junit.xml.ejs +19 -0
  101. data/templates/cordova_android/bin/node_modules/nodeunit/share/license.js +11 -0
  102. data/templates/cordova_android/bin/node_modules/nodeunit/share/nodeunit.css +70 -0
  103. data/templates/cordova_android/bin/templates/project/.cordova/android/readme.md +1 -0
  104. data/templates/cordova_android/bin/templates/project/.cordova/readme.md +3 -0
  105. data/templates/cordova_android/bin/templates/project/cordova/create +36 -0
  106. data/templates/cordova_android/bin/templates/project/cordova/debug +9 -0
  107. data/templates/cordova_android/bin/templates/project/cordova/emulate +12 -0
  108. data/templates/cordova_android/bin/templates/project/cordova/log +3 -0
  109. data/templates/cordova_android/bin/templates/project/cordova/templates/Activity.java +16 -0
  110. data/templates/cordova_android/bin/templates/project/cordova/templates/project/AndroidManifest.xml +50 -0
  111. data/templates/cordova_android/bin/templates/project/cordova/templates/project/assets/www/index.html +42 -0
  112. data/templates/cordova_android/bin/templates/project/cordova/templates/project/assets/www/main.js +146 -0
  113. data/templates/cordova_android/bin/templates/project/cordova/templates/project/assets/www/master.css +96 -0
  114. data/templates/cordova_android/bin/templates/project/cordova/templates/project/res/drawable/icon.png +0 -0
  115. data/templates/cordova_android/bin/templates/project/cordova/templates/project/res/xml/cordova.xml +5 -0
  116. data/templates/cordova_android/bin/templates/project/cordova/templates/project/res/xml/plugins.xml +19 -0
  117. data/templates/cordova_android/bin/test +26 -0
  118. data/templates/cordova_android/bin/tests/autotest.coffee +4 -0
  119. data/templates/cordova_android/bin/tests/create.coffee +21 -0
  120. data/templates/cordova_android/bin/tests/debug.coffee +0 -0
  121. data/templates/cordova_android/bin/tests/test.coffee +0 -0
  122. data/templates/cordova_android/framework/.classpath +8 -0
  123. data/templates/cordova_android/framework/.project +33 -0
  124. data/templates/cordova_android/framework/AndroidManifest.xml +68 -0
  125. data/templates/cordova_android/framework/ant.properties +34 -0
  126. data/templates/cordova_android/framework/assets/js/accelerometer.js +137 -0
  127. data/templates/cordova_android/framework/assets/js/app.js +89 -0
  128. data/templates/cordova_android/framework/assets/js/battery.js +134 -0
  129. data/templates/cordova_android/framework/assets/js/camera.js +168 -0
  130. data/templates/cordova_android/framework/assets/js/capture.js +203 -0
  131. data/templates/cordova_android/framework/assets/js/compass.js +168 -0
  132. data/templates/cordova_android/framework/assets/js/contact.js +310 -0
  133. data/templates/cordova_android/framework/assets/js/cordova.android.js +4841 -0
  134. data/templates/cordova_android/framework/assets/js/cordova.js.base +924 -0
  135. data/templates/cordova_android/framework/assets/js/crypto.js +54 -0
  136. data/templates/cordova_android/framework/assets/js/device.js +83 -0
  137. data/templates/cordova_android/framework/assets/js/file.js +1082 -0
  138. data/templates/cordova_android/framework/assets/js/filetransfer.js +125 -0
  139. data/templates/cordova_android/framework/assets/js/geolocation.js +209 -0
  140. data/templates/cordova_android/framework/assets/js/header.txt +19 -0
  141. data/templates/cordova_android/framework/assets/js/media.js +233 -0
  142. data/templates/cordova_android/framework/assets/js/network.js +100 -0
  143. data/templates/cordova_android/framework/assets/js/notification.js +133 -0
  144. data/templates/cordova_android/framework/assets/js/position.js +100 -0
  145. data/templates/cordova_android/framework/assets/js/storage.js +439 -0
  146. data/templates/cordova_android/framework/assets/www/index.html +27 -0
  147. data/templates/cordova_android/framework/build.xml +216 -0
  148. data/templates/cordova_android/framework/libs/commons-codec-1.3.jar +0 -0
  149. data/templates/cordova_android/framework/proguard-project.txt +20 -0
  150. data/templates/cordova_android/framework/project.properties +14 -0
  151. data/templates/cordova_android/framework/res/drawable/icon.png +0 -0
  152. data/templates/cordova_android/framework/res/drawable/splash.png +0 -0
  153. data/templates/cordova_android/framework/res/layout/main.xml +29 -0
  154. data/templates/cordova_android/framework/res/values/strings.xml +23 -0
  155. data/templates/cordova_android/framework/res/xml/cordova.xml +37 -0
  156. data/templates/cordova_android/framework/res/xml/plugins.xml +37 -0
  157. data/templates/cordova_android/framework/src/com/phonegap/api/IPlugin.java +27 -0
  158. data/templates/cordova_android/framework/src/com/phonegap/api/LOG.java +28 -0
  159. data/templates/cordova_android/framework/src/com/phonegap/api/PhonegapActivity.java +28 -0
  160. data/templates/cordova_android/framework/src/com/phonegap/api/Plugin.java +27 -0
  161. data/templates/cordova_android/framework/src/com/phonegap/api/PluginManager.java +35 -0
  162. data/templates/cordova_android/framework/src/com/phonegap/api/PluginResult.java +53 -0
  163. data/templates/cordova_android/framework/src/org/apache/cordova/AccelListener.java +311 -0
  164. data/templates/cordova_android/framework/src/org/apache/cordova/App.java +198 -0
  165. data/templates/cordova_android/framework/src/org/apache/cordova/AudioHandler.java +364 -0
  166. data/templates/cordova_android/framework/src/org/apache/cordova/AudioPlayer.java +450 -0
  167. data/templates/cordova_android/framework/src/org/apache/cordova/AuthenticationToken.java +69 -0
  168. data/templates/cordova_android/framework/src/org/apache/cordova/BatteryListener.java +156 -0
  169. data/templates/cordova_android/framework/src/org/apache/cordova/CallbackServer.java +431 -0
  170. data/templates/cordova_android/framework/src/org/apache/cordova/CameraLauncher.java +500 -0
  171. data/templates/cordova_android/framework/src/org/apache/cordova/Capture.java +400 -0
  172. data/templates/cordova_android/framework/src/org/apache/cordova/CompassListener.java +308 -0
  173. data/templates/cordova_android/framework/src/org/apache/cordova/ContactAccessor.java +198 -0
  174. data/templates/cordova_android/framework/src/org/apache/cordova/ContactAccessorSdk5.java +1934 -0
  175. data/templates/cordova_android/framework/src/org/apache/cordova/ContactManager.java +113 -0
  176. data/templates/cordova_android/framework/src/org/apache/cordova/CordovaChromeClient.java +314 -0
  177. data/templates/cordova_android/framework/src/org/apache/cordova/CordovaWebViewClient.java +306 -0
  178. data/templates/cordova_android/framework/src/org/apache/cordova/Device.java +219 -0
  179. data/templates/cordova_android/framework/src/org/apache/cordova/DirectoryManager.java +161 -0
  180. data/templates/cordova_android/framework/src/org/apache/cordova/DroidGap.java +1417 -0
  181. data/templates/cordova_android/framework/src/org/apache/cordova/ExifHelper.java +165 -0
  182. data/templates/cordova_android/framework/src/org/apache/cordova/FileTransfer.java +458 -0
  183. data/templates/cordova_android/framework/src/org/apache/cordova/FileUploadResult.java +63 -0
  184. data/templates/cordova_android/framework/src/org/apache/cordova/FileUtils.java +1048 -0
  185. data/templates/cordova_android/framework/src/org/apache/cordova/GeoBroker.java +165 -0
  186. data/templates/cordova_android/framework/src/org/apache/cordova/GeoListener.java +133 -0
  187. data/templates/cordova_android/framework/src/org/apache/cordova/GpsListener.java +163 -0
  188. data/templates/cordova_android/framework/src/org/apache/cordova/HttpHandler.java +80 -0
  189. data/templates/cordova_android/framework/src/org/apache/cordova/LinearLayoutSoftKeyboardDetect.java +104 -0
  190. data/templates/cordova_android/framework/src/org/apache/cordova/NetworkListener.java +153 -0
  191. data/templates/cordova_android/framework/src/org/apache/cordova/NetworkManager.java +248 -0
  192. data/templates/cordova_android/framework/src/org/apache/cordova/Notification.java +366 -0
  193. data/templates/cordova_android/framework/src/org/apache/cordova/PreferenceNode.java +34 -0
  194. data/templates/cordova_android/framework/src/org/apache/cordova/PreferenceSet.java +62 -0
  195. data/templates/cordova_android/framework/src/org/apache/cordova/StandAlone.java +35 -0
  196. data/templates/cordova_android/framework/src/org/apache/cordova/Storage.java +239 -0
  197. data/templates/cordova_android/framework/src/org/apache/cordova/TempListener.java +112 -0
  198. data/templates/cordova_android/framework/src/org/apache/cordova/api/CordovaInterface.java +145 -0
  199. data/templates/cordova_android/framework/src/org/apache/cordova/api/IPlugin.java +116 -0
  200. data/templates/cordova_android/framework/src/org/apache/cordova/api/LOG.java +234 -0
  201. data/templates/cordova_android/framework/src/org/apache/cordova/api/Plugin.java +210 -0
  202. data/templates/cordova_android/framework/src/org/apache/cordova/api/PluginManager.java +359 -0
  203. data/templates/cordova_android/framework/src/org/apache/cordova/api/PluginResult.java +119 -0
  204. data/templates/cordova_android/framework/src/org/apache/cordova/file/EncodingException.java +28 -0
  205. data/templates/cordova_android/framework/src/org/apache/cordova/file/FileExistsException.java +28 -0
  206. data/templates/cordova_android/framework/src/org/apache/cordova/file/InvalidModificationException.java +29 -0
  207. data/templates/cordova_android/framework/src/org/apache/cordova/file/NoModificationAllowedException.java +28 -0
  208. data/templates/cordova_android/framework/src/org/apache/cordova/file/TypeMismatchException.java +29 -0
  209. data/templates/cordova_android/framework/test/org/apache/cordova/PreferenceNodeTest.java +53 -0
  210. data/templates/cordova_android/framework/test/org/apache/cordova/PreferenceSetTest.java +73 -0
  211. metadata +279 -0
@@ -0,0 +1,431 @@
1
+ /*
2
+ Licensed to the Apache Software Foundation (ASF) under one
3
+ or more contributor license agreements. See the NOTICE file
4
+ distributed with this work for additional information
5
+ regarding copyright ownership. The ASF licenses this file
6
+ to you under the Apache License, Version 2.0 (the
7
+ "License"); you may not use this file except in compliance
8
+ with the License. You may obtain a copy of the License at
9
+
10
+ http://www.apache.org/licenses/LICENSE-2.0
11
+
12
+ Unless required by applicable law or agreed to in writing,
13
+ software distributed under the License is distributed on an
14
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15
+ KIND, either express or implied. See the License for the
16
+ specific language governing permissions and limitations
17
+ under the License.
18
+ */
19
+ package org.apache.cordova;
20
+
21
+ import java.io.BufferedReader;
22
+ import java.io.DataOutputStream;
23
+ import java.io.IOException;
24
+ import java.io.InputStreamReader;
25
+ import java.io.UnsupportedEncodingException;
26
+ import java.net.ServerSocket;
27
+ import java.net.Socket;
28
+ import java.net.URLEncoder;
29
+ import java.util.LinkedList;
30
+
31
+ import android.util.Log;
32
+
33
+ /**
34
+ * This class provides a way for Java to run JavaScript in the web page that has loaded Cordova.
35
+ * The CallbackServer class implements an XHR server and a polling server with a list of JavaScript
36
+ * statements that are to be executed on the web page.
37
+ *
38
+ * The process flow for XHR is:
39
+ * 1. JavaScript makes an async XHR call.
40
+ * 2. The server holds the connection open until data is available.
41
+ * 3. The server writes the data to the client and closes the connection.
42
+ * 4. The server immediately starts listening for the next XHR call.
43
+ * 5. The client receives this XHR response, processes it.
44
+ * 6. The client sends a new async XHR request.
45
+ *
46
+ * The CallbackServer class requires the following permission in Android manifest file
47
+ * <uses-permission android:name="android.permission.INTERNET" />
48
+ *
49
+ * If the device has a proxy set, then XHR cannot be used, so polling must be used instead.
50
+ * This can be determined by the client by calling CallbackServer.usePolling().
51
+ *
52
+ * The process flow for polling is:
53
+ * 1. The client calls CallbackServer.getJavascript() to retrieve next statement.
54
+ * 2. If statement available, then client processes it.
55
+ * 3. The client repeats #1 in loop.
56
+ */
57
+ public class CallbackServer implements Runnable {
58
+
59
+ private static final String LOG_TAG = "CallbackServer";
60
+
61
+ /**
62
+ * The list of JavaScript statements to be sent to JavaScript.
63
+ */
64
+ private LinkedList<String> javascript;
65
+
66
+ /**
67
+ * The port to listen on.
68
+ */
69
+ private int port;
70
+
71
+ /**
72
+ * The server thread.
73
+ */
74
+ private Thread serverThread;
75
+
76
+ /**
77
+ * Indicates the server is running.
78
+ */
79
+ private boolean active;
80
+
81
+ /**
82
+ * Indicates that the JavaScript statements list is empty
83
+ */
84
+ private boolean empty;
85
+
86
+ /**
87
+ * Indicates that polling should be used instead of XHR.
88
+ */
89
+ private boolean usePolling = true;
90
+
91
+ /**
92
+ * Security token to prevent other apps from accessing this callback server via XHR
93
+ */
94
+ private String token;
95
+
96
+ /**
97
+ * Constructor.
98
+ */
99
+ public CallbackServer() {
100
+ //System.out.println("CallbackServer()");
101
+ this.active = false;
102
+ this.empty = true;
103
+ this.port = 0;
104
+ this.javascript = new LinkedList<String>();
105
+ }
106
+
107
+ /**
108
+ * Init callback server and start XHR if running local app.
109
+ *
110
+ * If Cordova app is loaded from file://, then we can use XHR
111
+ * otherwise we have to use polling due to cross-domain security restrictions.
112
+ *
113
+ * @param url The URL of the Cordova app being loaded
114
+ */
115
+ public void init(String url) {
116
+ //System.out.println("CallbackServer.start("+url+")");
117
+ this.active = false;
118
+ this.empty = true;
119
+ this.port = 0;
120
+ this.javascript = new LinkedList<String>();
121
+
122
+ // Determine if XHR or polling is to be used
123
+ if ((url != null) && !url.startsWith("file://")) {
124
+ this.usePolling = true;
125
+ this.stopServer();
126
+ }
127
+ else if (android.net.Proxy.getDefaultHost() != null) {
128
+ this.usePolling = true;
129
+ this.stopServer();
130
+ }
131
+ else {
132
+ this.usePolling = false;
133
+ this.startServer();
134
+ }
135
+ }
136
+
137
+ /**
138
+ * Re-init when loading a new HTML page into webview.
139
+ *
140
+ * @param url The URL of the Cordova app being loaded
141
+ */
142
+ public void reinit(String url) {
143
+ this.stopServer();
144
+ this.init(url);
145
+ }
146
+
147
+ /**
148
+ * Return if polling is being used instead of XHR.
149
+ *
150
+ * @return
151
+ */
152
+ public boolean usePolling() {
153
+ return this.usePolling;
154
+ }
155
+
156
+ /**
157
+ * Get the port that this server is running on.
158
+ *
159
+ * @return
160
+ */
161
+ public int getPort() {
162
+ return this.port;
163
+ }
164
+
165
+ /**
166
+ * Get the security token that this server requires when calling getJavascript().
167
+ *
168
+ * @return
169
+ */
170
+ public String getToken() {
171
+ return this.token;
172
+ }
173
+
174
+ /**
175
+ * Start the server on a new thread.
176
+ */
177
+ public void startServer() {
178
+ //System.out.println("CallbackServer.startServer()");
179
+ this.active = false;
180
+
181
+ // Start server on new thread
182
+ this.serverThread = new Thread(this);
183
+ this.serverThread.start();
184
+ }
185
+
186
+ /**
187
+ * Restart the server on a new thread.
188
+ */
189
+ public void restartServer() {
190
+
191
+ // Stop server
192
+ this.stopServer();
193
+
194
+ // Start server again
195
+ this.startServer();
196
+ }
197
+
198
+ /**
199
+ * Start running the server.
200
+ * This is called automatically when the server thread is started.
201
+ */
202
+ public void run() {
203
+
204
+ // Start server
205
+ try {
206
+ this.active = true;
207
+ String request;
208
+ ServerSocket waitSocket = new ServerSocket(0);
209
+ this.port = waitSocket.getLocalPort();
210
+ //System.out.println("CallbackServer -- using port " +this.port);
211
+ this.token = java.util.UUID.randomUUID().toString();
212
+ //System.out.println("CallbackServer -- using token "+this.token);
213
+
214
+ while (this.active) {
215
+ //System.out.println("CallbackServer: Waiting for data on socket");
216
+ Socket connection = waitSocket.accept();
217
+ BufferedReader xhrReader = new BufferedReader(new InputStreamReader(connection.getInputStream()),40);
218
+ DataOutputStream output = new DataOutputStream(connection.getOutputStream());
219
+ request = xhrReader.readLine();
220
+ String response = "";
221
+ //System.out.println("CallbackServerRequest="+request);
222
+ if (this.active && (request != null)) {
223
+ if (request.contains("GET")) {
224
+
225
+ // Get requested file
226
+ String[] requestParts = request.split(" ");
227
+
228
+ // Must have security token
229
+ if ((requestParts.length == 3) && (requestParts[1].substring(1).equals(this.token))) {
230
+ //System.out.println("CallbackServer -- Processing GET request");
231
+
232
+ // Wait until there is some data to send, or send empty data every 10 sec
233
+ // to prevent XHR timeout on the client
234
+ synchronized (this) {
235
+ while (this.empty) {
236
+ try {
237
+ this.wait(10000); // prevent timeout from happening
238
+ //System.out.println("CallbackServer>>> break <<<");
239
+ break;
240
+ }
241
+ catch (Exception e) { }
242
+ }
243
+ }
244
+
245
+ // If server is still running
246
+ if (this.active) {
247
+
248
+ // If no data, then send 404 back to client before it times out
249
+ if (this.empty) {
250
+ //System.out.println("CallbackServer -- sending data 0");
251
+ response = "HTTP/1.1 404 NO DATA\r\n\r\n "; // need to send content otherwise some Android devices fail, so send space
252
+ }
253
+ else {
254
+ //System.out.println("CallbackServer -- sending item");
255
+ response = "HTTP/1.1 200 OK\r\n\r\n";
256
+ String js = this.getJavascript();
257
+ if (js != null) {
258
+ response += encode(js, "UTF-8");
259
+ }
260
+ }
261
+ }
262
+ else {
263
+ response = "HTTP/1.1 503 Service Unavailable\r\n\r\n ";
264
+ }
265
+ }
266
+ else {
267
+ response = "HTTP/1.1 403 Forbidden\r\n\r\n ";
268
+ }
269
+ }
270
+ else {
271
+ response = "HTTP/1.1 400 Bad Request\r\n\r\n ";
272
+ }
273
+ //System.out.println("CallbackServer: response="+response);
274
+ //System.out.println("CallbackServer: closing output");
275
+ output.writeBytes(response);
276
+ output.flush();
277
+ }
278
+ output.close();
279
+ xhrReader.close();
280
+ }
281
+ } catch (IOException e) {
282
+ e.printStackTrace();
283
+ }
284
+ this.active = false;
285
+ //System.out.println("CallbackServer.startServer() - EXIT");
286
+ }
287
+
288
+ /**
289
+ * Stop server.
290
+ * This stops the thread that the server is running on.
291
+ */
292
+ public void stopServer() {
293
+ //System.out.println("CallbackServer.stopServer()");
294
+ if (this.active) {
295
+ this.active = false;
296
+
297
+ // Break out of server wait
298
+ synchronized (this) {
299
+ this.notify();
300
+ }
301
+ }
302
+ }
303
+
304
+ /**
305
+ * Destroy
306
+ */
307
+ public void destroy() {
308
+ this.stopServer();
309
+ }
310
+
311
+ /**
312
+ * Get the number of JavaScript statements.
313
+ *
314
+ * @return int
315
+ */
316
+ public int getSize() {
317
+ synchronized(this) {
318
+ int size = this.javascript.size();
319
+ return size;
320
+ }
321
+ }
322
+
323
+ /**
324
+ * Get the next JavaScript statement and remove from list.
325
+ *
326
+ * @return String
327
+ */
328
+ public String getJavascript() {
329
+ synchronized(this) {
330
+ if (this.javascript.size() == 0) {
331
+ return null;
332
+ }
333
+ String statement = this.javascript.remove(0);
334
+ if (this.javascript.size() == 0) {
335
+ this.empty = true;
336
+ }
337
+ return statement;
338
+ }
339
+ }
340
+
341
+ /**
342
+ * Add a JavaScript statement to the list.
343
+ *
344
+ * @param statement
345
+ */
346
+ public void sendJavascript(String statement) {
347
+ synchronized (this) {
348
+ this.javascript.add(statement);
349
+ this.empty = false;
350
+ this.notify();
351
+ }
352
+ }
353
+
354
+ /* The Following code has been modified from original implementation of URLEncoder */
355
+
356
+ /* start */
357
+
358
+ /*
359
+ * Licensed to the Apache Software Foundation (ASF) under one or more
360
+ * contributor license agreements. See the NOTICE file distributed with
361
+ * this work for additional information regarding copyright ownership.
362
+ * The ASF licenses this file to You under the Apache License, Version 2.0
363
+ * (the "License"); you may not use this file except in compliance with
364
+ * the License. You may obtain a copy of the License at
365
+ *
366
+ * http://www.apache.org/licenses/LICENSE-2.0
367
+ *
368
+ * Unless required by applicable law or agreed to in writing, software
369
+ * distributed under the License is distributed on an "AS IS" BASIS,
370
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
371
+ * See the License for the specific language governing permissions and
372
+ * limitations under the License.
373
+ */
374
+ static final String digits = "0123456789ABCDEF";
375
+
376
+ /**
377
+ * This will encode the return value to JavaScript. We revert the encoding for
378
+ * common characters that don't require encoding to reduce the size of the string
379
+ * being passed to JavaScript.
380
+ *
381
+ * @param s to be encoded
382
+ * @param enc encoding type
383
+ * @return encoded string
384
+ */
385
+ public static String encode(String s, String enc) throws UnsupportedEncodingException {
386
+ if (s == null || enc == null) {
387
+ throw new NullPointerException();
388
+ }
389
+ // check for UnsupportedEncodingException
390
+ "".getBytes(enc);
391
+
392
+ // Guess a bit bigger for encoded form
393
+ StringBuilder buf = new StringBuilder(s.length() + 16);
394
+ int start = -1;
395
+ for (int i = 0; i < s.length(); i++) {
396
+ char ch = s.charAt(i);
397
+ if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')
398
+ || (ch >= '0' && ch <= '9')
399
+ || " .-*_'(),<>=?@[]{}:~\"\\/;!".indexOf(ch) > -1) {
400
+ if (start >= 0) {
401
+ convert(s.substring(start, i), buf, enc);
402
+ start = -1;
403
+ }
404
+ if (ch != ' ') {
405
+ buf.append(ch);
406
+ } else {
407
+ buf.append(' ');
408
+ }
409
+ } else {
410
+ if (start < 0) {
411
+ start = i;
412
+ }
413
+ }
414
+ }
415
+ if (start >= 0) {
416
+ convert(s.substring(start, s.length()), buf, enc);
417
+ }
418
+ return buf.toString();
419
+ }
420
+
421
+ private static void convert(String s, StringBuilder buf, String enc) throws UnsupportedEncodingException {
422
+ byte[] bytes = s.getBytes(enc);
423
+ for (int j = 0; j < bytes.length; j++) {
424
+ buf.append('%');
425
+ buf.append(digits.charAt((bytes[j] & 0xf0) >> 4));
426
+ buf.append(digits.charAt(bytes[j] & 0xf));
427
+ }
428
+ }
429
+
430
+ /* end */
431
+ }