Subversion Repository Public Repository

litesoft

Diff Revisions 905 vs 906 for /trunk/mobileGWT/gwt-phonegap/src/main/java/com/googlecode/gwtphonegap/client/PhoneGapStandardImpl.java

Diff revisions: vs.
  @@ -1,28 +1,24 @@
1 1 /*
2 2 * Copyright 2010 Daniel Kurka
3 3 *
4 - * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 - * use this file except in compliance with the License. You may obtain a copy of
6 - * the License at
4 + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 + * in compliance with the License. You may obtain a copy of the License at
7 6 *
8 7 * http://www.apache.org/licenses/LICENSE-2.0
9 8 *
10 - * Unless required by applicable law or agreed to in writing, software
11 - * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 - * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 - * License for the specific language governing permissions and limitations under
9 + * Unless required by applicable law or agreed to in writing, software distributed under the License
10 + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 + * or implied. See the License for the specific language governing permissions and limitations under
14 12 * the License.
15 13 */
16 14 package com.googlecode.gwtphonegap.client;
17 15
18 - import java.util.HashMap;
19 - import java.util.Map;
20 -
21 16 import com.google.gwt.core.client.GWT;
22 17 import com.google.gwt.user.client.Timer;
23 18 import com.google.web.bindery.event.shared.EventBus;
24 19 import com.google.web.bindery.event.shared.HandlerRegistration;
25 20 import com.google.web.bindery.event.shared.SimpleEventBus;
21 +
26 22 import com.googlecode.gwtphonegap.client.accelerometer.Accelerometer;
27 23 import com.googlecode.gwtphonegap.client.camera.Camera;
28 24 import com.googlecode.gwtphonegap.client.capture.Capture;
  @@ -33,6 +29,8 @@
33 29 import com.googlecode.gwtphonegap.client.event.Event;
34 30 import com.googlecode.gwtphonegap.client.file.File;
35 31 import com.googlecode.gwtphonegap.client.geolocation.Geolocation;
32 + import com.googlecode.gwtphonegap.client.globalization.Globalization;
33 + import com.googlecode.gwtphonegap.client.inappbrowser.InAppBrowser;
36 34 import com.googlecode.gwtphonegap.client.log.PhoneGapLog;
37 35 import com.googlecode.gwtphonegap.client.log.PhoneGapLogStandardImpl;
38 36 import com.googlecode.gwtphonegap.client.media.MediaModule;
  @@ -40,196 +38,202 @@
40 38 import com.googlecode.gwtphonegap.client.plugins.PhoneGapPlugin;
41 39 import com.googlecode.gwtphonegap.client.splashscreen.SplashScreen;
42 40
41 + import java.util.HashMap;
42 + import java.util.Map;
43 +
43 44 public class PhoneGapStandardImpl implements PhoneGap {
44 45
45 - private static final int INIT_TICK = 10;
46 + private static final int INIT_TICK = 10;
46 47
47 - private Device device;
48 + private Device device;
48 49
49 - private Accelerometer accelerometer;
50 - private Camera camera;
51 - private Geolocation geolocation;
52 - private Notification notification;
53 - private Contacts contacts;
54 - private File file;
55 - private Connection connection;
56 - private Event event;
57 - private MediaModule mediaModule;
58 - private Compass compass;
59 - private Capture capture;
60 - private SplashScreen splashScreen;
61 -
62 - private Map<String, PhoneGapPlugin> plugins = new HashMap<String, PhoneGapPlugin>();
63 -
64 - private boolean hasHandlers = false;
65 - private EventBus handlerManager = new SimpleEventBus();
66 -
67 - private PhoneGapLogStandardImpl phoneGapLog;
68 -
69 - private boolean deviceReady;
70 -
71 - public PhoneGapStandardImpl() {
72 - // log configures it self
73 - getLog();
74 -
75 - setupReadyHook();
76 - }
77 -
78 - public PhoneGapLog getLog() {
79 - if (phoneGapLog == null) {
80 - phoneGapLog = new PhoneGapLogStandardImpl();
81 - }
82 - return phoneGapLog;
83 - }
50 + private Accelerometer accelerometer;
51 + private Camera camera;
52 + private Geolocation geolocation;
53 + private Notification notification;
54 + private Contacts contacts;
55 + private File file;
56 + private Connection connection;
57 + private Event event;
58 + private MediaModule mediaModule;
59 + private Compass compass;
60 + private Capture capture;
61 + private SplashScreen splashScreen;
62 + private InAppBrowser inAppBrowser;
84 63
85 - @Override
86 - public boolean isPhoneGapInitialized() {
87 - return deviceReady;
88 - }
89 -
90 - @Override
91 - public void initializePhoneGap() {
92 - initializePhoneGap(10000);
93 - }
94 -
95 - @Override
96 - public void initializePhoneGap(final int timeoutInMs) {
97 -
98 - final long end = System.currentTimeMillis() + timeoutInMs;
99 - if (isPhoneGapInitialized()) {
100 -
101 - firePhoneGapAvailable();
102 -
103 - } else {
104 - Timer timer = new Timer() {
105 -
106 - @Override
107 - public void run() {
108 - if (isPhoneGapInitialized()) {
109 - firePhoneGapAvailable();
110 - return;
111 - }
112 -
113 - if (System.currentTimeMillis() - end > 0) {
114 - handlerManager.fireEvent(new PhoneGapTimeoutEvent());
115 - } else {
116 - schedule(INIT_TICK);
117 - }
64 + private Map<String, PhoneGapPlugin> plugins = new HashMap<String, PhoneGapPlugin>();
118 65
119 - }
120 - };
66 + private boolean hasHandlers = false;
67 + private EventBus handlerManager = new SimpleEventBus();
121 68
122 - timer.schedule(INIT_TICK);
123 - }
124 - }
69 + private PhoneGapLogStandardImpl phoneGapLog;
70 +
71 + private boolean deviceReady;
72 +
73 + private Globalization globalization;
74 +
75 + public PhoneGapStandardImpl() {
76 + // log configures it self
77 + getLog();
78 +
79 + setupReadyHook();
80 + }
81 +
82 + public PhoneGapLog getLog() {
83 + if (phoneGapLog == null) {
84 + phoneGapLog = new PhoneGapLogStandardImpl();
85 + }
86 + return phoneGapLog;
87 + }
125 88
126 - @Override
127 - public HandlerRegistration addHandler(PhoneGapAvailableHandler handler) {
89 + @Override
90 + public boolean isPhoneGapInitialized() {
91 + return deviceReady;
92 + }
93 +
94 + @Override
95 + public void initializePhoneGap() {
96 + initializePhoneGap(10000);
97 + }
98 +
99 + @Override
100 + public void initializePhoneGap(final int timeoutInMs) {
101 +
102 + final long end = System.currentTimeMillis() + timeoutInMs;
103 + if (isPhoneGapInitialized()) {
104 +
105 + firePhoneGapAvailable();
106 +
107 + } else {
108 + Timer timer = new Timer() {
109 +
110 + @Override
111 + public void run() {
112 + if (isPhoneGapInitialized()) {
113 + firePhoneGapAvailable();
114 + return;
115 + }
116 +
117 + if (System.currentTimeMillis() - end > 0) {
118 + handlerManager.fireEvent(new PhoneGapTimeoutEvent());
119 + } else {
120 + schedule(INIT_TICK);
121 + }
122 +
123 + }
124 + };
125 +
126 + timer.schedule(INIT_TICK);
127 + }
128 + }
129 +
130 + @Override
131 + public HandlerRegistration addHandler(PhoneGapAvailableHandler handler) {
128 132 hasHandlers = true;
129 - return handlerManager.addHandler(PhoneGapAvailableEvent.TYPE, handler);
130 - }
133 + return handlerManager.addHandler(PhoneGapAvailableEvent.TYPE, handler);
134 + }
131 135
132 - @Override
133 - public HandlerRegistration addHandler(PhoneGapTimeoutHandler handler) {
136 + @Override
137 + public HandlerRegistration addHandler(PhoneGapTimeoutHandler handler) {
134 138 hasHandlers = true;
135 - return handlerManager.addHandler(PhoneGapTimeoutEvent.TYPE, handler);
136 - }
139 + return handlerManager.addHandler(PhoneGapTimeoutEvent.TYPE, handler);
140 + }
137 141
138 - @Override
139 - public Device getDevice() {
140 - if (device == null) {
141 - device = constructDevice();
142 - }
143 - return device;
144 - }
142 + @Override
143 + public Device getDevice() {
144 + if (device == null) {
145 + device = constructDevice();
146 + }
147 + return device;
148 + }
145 149
146 - @Override
147 - public Accelerometer getAccelerometer() {
148 - if (accelerometer == null) {
149 - accelerometer = constructAccelerometer();
150 - }
151 - return accelerometer;
152 - }
150 + @Override
151 + public Accelerometer getAccelerometer() {
152 + if (accelerometer == null) {
153 + accelerometer = constructAccelerometer();
154 + }
155 + return accelerometer;
156 + }
153 157
154 - @Override
155 - public Camera getCamera() {
156 - if (camera == null) {
157 - camera = constructCamera();
158 - }
159 - return camera;
160 - }
158 + @Override
159 + public Camera getCamera() {
160 + if (camera == null) {
161 + camera = constructCamera();
162 + }
163 + return camera;
164 + }
161 165
162 - @Override
163 - public Geolocation getGeolocation() {
164 - if (geolocation == null) {
165 - geolocation = constructGeolocation();
166 - }
167 - return geolocation;
168 - }
166 + @Override
167 + public Geolocation getGeolocation() {
168 + if (geolocation == null) {
169 + geolocation = constructGeolocation();
170 + }
171 + return geolocation;
172 + }
169 173
170 - @Override
171 - public Notification getNotification() {
172 - if (notification == null) {
173 - notification = constructNotification();
174 - }
175 - return notification;
176 - }
174 + @Override
175 + public Notification getNotification() {
176 + if (notification == null) {
177 + notification = constructNotification();
178 + }
179 + return notification;
180 + }
177 181
178 - private void firePhoneGapAvailable() {
179 - phoneGapLog.setClientId(getDevice().getUuid());
182 + private void firePhoneGapAvailable() {
183 + phoneGapLog.setClientId(getDevice().getUuid());
180 184
181 - handlerManager.fireEvent(new PhoneGapAvailableEvent());
182 - }
185 + handlerManager.fireEvent(new PhoneGapAvailableEvent());
186 + }
183 187
184 - @Override
185 - public Contacts getContacts() {
186 - if (contacts == null) {
187 - contacts = constructContacts();
188 - }
189 - return contacts;
190 - }
188 + @Override
189 + public Contacts getContacts() {
190 + if (contacts == null) {
191 + contacts = constructContacts();
192 + }
193 + return contacts;
194 + }
191 195
192 - @Override
193 - public PhoneGapPlugin getPluginById(String id) {
194 - return plugins.get(id);
195 - }
196 -
197 - @Override
198 - public void loadPlugin(String id, PhoneGapPlugin instance) {
199 - if (plugins.containsKey(id)) {
200 - throw new IllegalStateException("id is already in use");
201 - }
196 + @Override
197 + public PhoneGapPlugin getPluginById(String id) {
198 + return plugins.get(id);
199 + }
202 200
203 - plugins.put(id, instance);
204 - }
201 + @Override
202 + public void loadPlugin(String id, PhoneGapPlugin instance) {
203 + if (plugins.containsKey(id)) {
204 + throw new IllegalStateException("id is already in use");
205 + }
205 206
206 - @Override
207 - public File getFile() {
208 - if (file == null) {
209 - file = constructFile();
210 - }
211 - return file;
212 - }
207 + plugins.put(id, instance);
208 + }
213 209
214 - @Override
215 - public Connection getConnection() {
216 - if (connection == null) {
217 - connection = constructConnection();
218 - }
219 - return connection;
220 - }
210 + @Override
211 + public File getFile() {
212 + if (file == null) {
213 + file = constructFile();
214 + }
215 + return file;
216 + }
221 217
222 - @Override
223 - public Event getEvent() {
224 - hasHandlers = true;
225 - if (event == null) {
226 - event = constructEvent();
227 - }
228 - return event;
229 - }
218 + @Override
219 + public Connection getConnection() {
220 + if (connection == null) {
221 + connection = constructConnection();
222 + }
223 + return connection;
224 + }
225 +
226 + @Override
227 + public Event getEvent() {
228 + hasHandlers = true;
229 + if (event == null) {
230 + event = constructEvent();
231 + }
232 + return event;
233 + }
230 234
231 - @Override
232 - public native boolean exitApp() /*-{
235 + @Override
236 + public native boolean exitApp() /*-{
233 237 if ($wnd.navigator.app != null) {
234 238 if ($wnd.navigator.app.exitApp != null) {
235 239 $wnd.navigator.app.exitApp();
  @@ -238,119 +242,143 @@
238 242 }
239 243 return false;
240 244
241 - }-*/;
245 + }-*/;
242 246
243 - @Override
244 - public MediaModule getMedia() {
245 - if (mediaModule == null) {
246 - mediaModule = constructMediaModule();
247 - }
248 - return mediaModule;
249 - }
247 + @Override
248 + public MediaModule getMedia() {
249 + if (mediaModule == null) {
250 + mediaModule = constructMediaModule();
251 + }
252 + return mediaModule;
253 + }
250 254
251 - @Override
252 - public Compass getCompass() {
253 - if (compass == null) {
254 - compass = constructCompass();
255 - }
256 - return compass;
257 - }
255 + @Override
256 + public Compass getCompass() {
257 + if (compass == null) {
258 + compass = constructCompass();
259 + }
260 + return compass;
261 + }
262 +
263 + @Override
264 + public Capture getCapture() {
265 + if (capture == null) {
266 + capture = constructCapture();
267 + }
268 + return capture;
269 + }
270 +
271 + @Override
272 + public SplashScreen getSplashScreen() {
273 + if (splashScreen == null) {
274 + splashScreen = constructSplashscreen();
275 + }
276 + return splashScreen;
277 + }
278 +
279 + @Override
280 + public boolean isPhoneGapDevice() {
281 + return true;
282 + }
283 +
284 + protected Device constructDevice() {
285 + return GWT.create(Device.class);
286 + }
287 +
288 + protected Accelerometer constructAccelerometer() {
289 + return GWT.create(Accelerometer.class);
290 + }
291 +
292 + protected Camera constructCamera() {
293 + return GWT.create(Camera.class);
294 + }
295 +
296 + protected Geolocation constructGeolocation() {
297 + return GWT.create(Geolocation.class);
298 + }
299 +
300 + protected Notification constructNotification() {
301 + return GWT.create(Notification.class);
302 + }
303 +
304 + protected Capture constructCapture() {
305 + return GWT.create(Capture.class);
306 + }
258 307
259 - @Override
260 - public Capture getCapture() {
261 - if (capture == null) {
262 - capture = constructCapture();
263 - }
264 - return capture;
265 - }
266 -
267 - @Override
268 - public SplashScreen getSplashScreen() {
269 - if (splashScreen == null) {
270 - splashScreen = constructSplashscreen();
271 - }
272 - return splashScreen;
273 - }
274 -
275 - @Override
276 - public boolean isPhoneGapDevice() {
277 - return true;
278 - }
279 -
280 - protected Device constructDevice() {
281 - return GWT.create(Device.class);
282 - }
283 -
284 - protected Accelerometer constructAccelerometer() {
285 - return GWT.create(Accelerometer.class);
286 - }
287 -
288 - protected Camera constructCamera() {
289 - return GWT.create(Camera.class);
290 - }
291 -
292 - protected Geolocation constructGeolocation() {
293 - return GWT.create(Geolocation.class);
294 - }
295 -
296 - protected Notification constructNotification() {
297 - return GWT.create(Notification.class);
298 - }
299 -
300 - protected Capture constructCapture() {
301 - return GWT.create(Capture.class);
302 - }
303 -
304 308 protected SplashScreen constructSplashscreen() {
305 309 return GWT.create(SplashScreen.class);
306 - }
310 + }
311 +
312 + protected Compass constructCompass() {
313 + return GWT.create(Compass.class);
314 + }
315 +
316 + protected MediaModule constructMediaModule() {
317 + return GWT.create(MediaModule.class);
318 + }
319 +
320 + protected Event constructEvent() {
321 + Event event = GWT.create(Event.class);
322 + event.setEventBus(handlerManager);
323 + return event;
324 + }
325 +
326 + protected Connection constructConnection() {
327 + return GWT.create(Connection.class);
328 + }
307 329
308 - protected Compass constructCompass() {
309 - return GWT.create(Compass.class);
310 - }
311 -
312 - protected MediaModule constructMediaModule() {
313 - return GWT.create(MediaModule.class);
314 - }
315 -
316 - protected Event constructEvent() {
317 - Event event = GWT.create(Event.class);
318 - event.setEventBus(handlerManager);
319 - return event;
320 - }
321 -
322 - protected Connection constructConnection() {
323 - return GWT.create(Connection.class);
324 - }
325 -
326 - protected File constructFile() {
327 - return GWT.create(File.class);
328 - }
329 -
330 - protected Contacts constructContacts() {
331 - return GWT.create(Contacts.class);
332 - }
333 -
334 - protected void nativeDeviceReady() {
335 - deviceReady = true;
336 - }
330 + protected File constructFile() {
331 + return GWT.create(File.class);
332 + }
333 +
334 + protected Contacts constructContacts() {
335 + return GWT.create(Contacts.class);
336 + }
337 +
338 + protected void nativeDeviceReady() {
339 + deviceReady = true;
340 + }
337 341
338 - private native void setupReadyHook() /*-{
342 + private native void setupReadyHook() /*-{
339 343 var that = this;
340 344 var f = function() {
341 345 that.@com.googlecode.gwtphonegap.client.PhoneGapStandardImpl::nativeDeviceReady()();
342 346 };
343 347 $doc.addEventListener("deviceready", $entry(f), false);
344 348
345 - }-*/;
349 + }-*/;
346 350
347 351 @Override
348 352 public void setEventBus(EventBus eventBus) {
349 353 assert !hasHandlers : "The handlerManager can not be replaced because it has got handlers";
350 354 handlerManager = eventBus;
351 - // We force the event construction so as the app can add events directly
355 + // We force the event construction so as the app can add events directly
352 356 // to the eventBus instead of through the phoneGap.getEvent() method;
353 357 event = constructEvent();
354 358 }
355 359
360 + @Override
361 + public InAppBrowser getInAppBrowser() {
362 + if (inAppBrowser == null) {
363 + inAppBrowser = constructInAppBrowser();
364 + }
365 + return inAppBrowser;
366 + }
367 +
368 + protected InAppBrowser constructInAppBrowser() {
369 + return GWT.create(InAppBrowser.class);
370 + }
371 +
372 + @Override
373 + public Globalization getGlobalization() {
374 + if (globalization == null) {
375 + globalization = constructGlobalization();
376 + }
377 + return globalization;
378 + }
379 +
380 + protected Globalization constructGlobalization() {
381 + return GWT.create(Globalization.class);
382 + }
383 +
356 384 }