Subversion Repository Public Repository

litesoft

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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
/*
 * Copyright 2010 Daniel Kurka
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package com.googlecode.mgwt.ui.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.dom.client.BodyElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.LinkElement;
import com.google.gwt.dom.client.MetaElement;
import com.google.gwt.dom.client.NodeList;
import com.google.gwt.dom.client.Style.Display;
import com.google.gwt.dom.client.Style.Float;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.dom.client.StyleInjector;
import com.google.gwt.event.logical.shared.CloseEvent;
import com.google.gwt.event.logical.shared.CloseHandler;
import com.google.gwt.event.logical.shared.ResizeEvent;
import com.google.gwt.event.logical.shared.ResizeHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.event.shared.SimpleEventBus;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.RootPanel;
import com.googlecode.mgwt.dom.client.event.orientation.OrientationChangeEvent;
import com.googlecode.mgwt.dom.client.event.orientation.OrientationChangeEvent.ORIENTATION;
import com.googlecode.mgwt.dom.client.event.orientation.OrientationChangeHandler;
import com.googlecode.mgwt.ui.client.MGWTSettings.ViewPort;
import com.googlecode.mgwt.ui.client.theme.base.UtilCss;
import com.googlecode.mgwt.ui.client.util.AddressBarUtil;

/**
 * The MGWT Object is used to apply settings for an MGWT App. It also provides an instance of
 * {@link OsDetection}, as well a way to determine the device orientation
 *
 *
 *
 * @author Daniel Kurka
 */
public class MGWT {

  private static OsDetection OS_DETECTION;

  private static EventBus manager;

  private static ORIENTATION currentOrientation;
  private static Timer timer;

  private static boolean scrollingDisabled;
  private static JavaScriptObject nativeJsFunction;

  private static AddressBarUtil addressBarUtil;

  /**
   * Add a orientation handler to detect the device orientation
   *
   * @param handler the handler to add
   *          {@link com.googlecode.mgwt.dom.client.event.orientation.OrientationChangeHandler} .
   * @return a {@link com.google.gwt.event.shared.HandlerRegistration} object.
   */
  public static HandlerRegistration addOrientationChangeHandler(OrientationChangeHandler handler) {
    maybeSetupOrientation();
    return getManager().addHandler(OrientationChangeEvent.getType(), handler);
  }

  /**
   * Apply settings for this mgwt app. Every app should call this method with the options its wants
   * for their app
   *
   * The options are documented in @link {@link MGWTSettings}
   *
   * @param settings the settings for this app
   *
   */
  public static void applySettings(MGWTSettings settings) {

    // This is a very nasty workaround because GWT CssResource does not
    // support @media correctly!
    StyleInjector.inject(MGWTStyle.getTheme().getMGWTClientBundle().utilTextResource().getText());

    Element head = getHead();

    if (settings.getIconUrl() != null) {

      LinkElement iconUrlLinkElement = Document.get().createLinkElement();
      if (settings.isAddGlosToIcon()) {
        iconUrlLinkElement.setRel("apple-touch-startup-image");
      } else {
        iconUrlLinkElement.setRel("apple-touch-startup-image-precomposed");
      }

      iconUrlLinkElement.setHref(settings.getIconUrl());
      head.appendChild(iconUrlLinkElement);

    }

    if (settings.getStartUrl() != null) {
      LinkElement startUrlLinkElement = Document.get().createLinkElement();
      startUrlLinkElement.setRel("apple-touch-startup-image");
      startUrlLinkElement.setHref(settings.getStartUrl());
      head.appendChild(startUrlLinkElement);
    }

    ViewPort viewPort = settings.getViewPort();

    if (viewPort != null) {
      MetaElement fixViewPortElement = Document.get().createMetaElement();
      fixViewPortElement.setName("viewport");
      fixViewPortElement.setContent(viewPort.getContent());
      head.appendChild(fixViewPortElement);

    }

    if (settings.isFullscreen()) {
      MetaElement fullScreenMetaTag = Document.get().createMetaElement();
      fullScreenMetaTag.setName("apple-mobile-web-app-capable");
      fullScreenMetaTag.setContent("yes");
      head.appendChild(fullScreenMetaTag);

      if (settings.getStatusBar() != null) {
        MetaElement statusBarMetaTag = Document.get().createMetaElement();
        statusBarMetaTag.setName("apple-mobile-web-app-status-bar-style");
        statusBarMetaTag.setContent(settings.getStatusBar());
        head.appendChild(statusBarMetaTag);

      }

    }

    scrollingDisabled = settings.isPreventScrolling();
    if (settings.isPreventScrolling() && getOsDetection().isIOs()) {
      BodyElement body = Document.get().getBody();
      setupPreventScrolling(body);

    }

    if (settings.isDisablePhoneNumberDetection()) {
      MetaElement fullScreenMetaTag = Document.get().createMetaElement();
      fullScreenMetaTag.setName("format-detection");
      fullScreenMetaTag.setContent("telephone=no");
      head.appendChild(fullScreenMetaTag);
    }

    if (settings.getStatusBarStyle() != null) {
      MetaElement statusBarTag = Document.get().createMetaElement();
      statusBarTag.setName("apple-mobile-web-app-status-bar-style");
      switch (settings.getStatusBarStyle()) {

        case BLACK:
          statusBarTag.setContent("black");
          break;
        case BLACK_TRANSLUCENT:
          statusBarTag.setContent("black-translucent");
          break;
        case DEFAULT:
        default:
          statusBarTag.setContent("default");
          break;
      }

      head.appendChild(statusBarTag);
    }

  }

  /**
   * Detect if a web app is in full screen mode
   *
   * fullscreen currently only exits on ios
   *
   * @return true if the web app is in full screen mode, otherwise false
   */
  public static native boolean isFullScreen()/*-{
		if ($wnd.navigator.standalone) {
			return true;
		}
		return false;
  }-*/;

  /**
   *
   * Considered internal don`t call!
   * <p>
   * fixIOSScrollIssueBlur
   * </p>
   */
  // @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "LI_LAZY_INIT_UPDATE_STATIC", justification = "no multithreading in gwt code")
  public static void fixIOSScrollIssueBlur() {
    if (!scrollingDisabled) {
      return;
    }
    if (timer != null) {
      timer.cancel();

    }

    timer = new Timer() {

      @Override
      public void run() {
        Window.scrollTo(0, 0);

      }

    };

    timer.schedule(100);
  }

  /**
   * Considered internal don`t call!
   * <p>
   * fixIOSScrollIssueFocus
   * </p>
   */
  public static void fixIOSScrollIssueFocus() {
    if (!scrollingDisabled) {
      return;
    }
    if (timer != null) {
      timer.cancel();

    }
    timer = null;
  }

  /**
   * Get the os detection interface
   *
   * @return a {@link com.googlecode.mgwt.ui.client.OsDetection} object.
   */
  public static OsDetection getOsDetection() {
    if (OS_DETECTION == null) {
      OS_DETECTION = GWT.create(OsDetection.class);
    }
    return OS_DETECTION;
  }

  /**
   * Get the current orientation of the device
   *
   * @return the current orientation of the device
   */
  public static ORIENTATION getOrientation() {

    if (!orientationSupport()) {
      int height = Window.getClientHeight();
      int width = Window.getClientWidth();

      if (width > height) {
        return ORIENTATION.LANDSCAPE;
      } else {
        return ORIENTATION.PORTRAIT;
      }

    } else {
      int orientation = getOrientation0();

      ORIENTATION o;
      switch (orientation) {
        case 0:
        case 180:
          o = ORIENTATION.PORTRAIT;
          break;

        case 90:
        case -90:
          o = ORIENTATION.LANDSCAPE;
          break;

        default:
          throw new IllegalStateException("this should not happen!?");
      }

      return o;
    }

  }

  private static void fireOrientationChangedEvent(ORIENTATION orientation) {
    setClasses(orientation);
    getManager().fireEvent(new OrientationChangeEvent(orientation));
  }

  private static Element getHead() {
    NodeList<Element> elementsByTagName = Document.get().getElementsByTagName("head");

    if (elementsByTagName.getLength() != 1) {
      throw new RuntimeException("there is no head element");
    }

    return elementsByTagName.getItem(0);
  }

  private static native int getOrientation0()/*-{
		if (typeof ($wnd.orientation) == 'undefined') {
			return 0;
		}

		return $wnd.orientation;
  }-*/;

  private static void onorientationChange(int orientation) {

    ORIENTATION o;
    switch (orientation) {
      case 0:
      case 180:
        o = ORIENTATION.PORTRAIT;
        break;

      case 90:
      case -90:
        o = ORIENTATION.LANDSCAPE;
        break;

      default:
        o = ORIENTATION.PORTRAIT;
        break;
    }
    currentOrientation = o;
    fireOrientationChangedEvent(o);

  }

  // update styles on body
  private static void setClasses(ORIENTATION o) {
    UtilCss utilCss = MGWTStyle.getTheme().getMGWTClientBundle().getUtilCss();
    switch (o) {

      case PORTRAIT:
        Document.get().getBody().addClassName(utilCss.portrait());
        Document.get().getBody().removeClassName(utilCss.landscape());
        break;
      case LANDSCAPE:
        Document.get().getBody().addClassName(utilCss.landscape());
        Document.get().getBody().removeClassName(utilCss.portrait());
        break;

      default:
        break;
    }
  }

  private native static boolean orientationSupport()/*-{
		var ua = window.navigator.userAgent.toLowerCase();
		if (ua.indexOf('android') != -1) {
			return false;
		}
		if (ua.indexOf('iphone') != -1) {
			return true;
		}
		if (ua.indexOf('ipad') != -1) {
			return true;
		}

		return false;
  }-*/;

  private static boolean orientationInitialized;

  private static void maybeSetupOrientation() {
    if (orientationInitialized)
      return;
    if (!GWT.isClient()) {
      return;
    }

    if (!orientationSupport()) {
      Window.addResizeHandler(new ResizeHandler() {

        @Override
        public void onResize(ResizeEvent event) {
          ORIENTATION orientation = getOrientation();
          if (orientation != currentOrientation) {
            currentOrientation = orientation;
            fireOrientationChangedEvent(orientation);
          }
        }
      });
    } else {
      nativeJsFunction = setupOrientation0();
      Window.addCloseHandler(new CloseHandler<Window>() {

        @Override
        public void onClose(CloseEvent<Window> event) {
          destroyOrientation(nativeJsFunction);

        }
      });
    }

  }

  private static native JavaScriptObject setupOrientation0()/*-{

		var func = $entry(function() {
			@com.googlecode.mgwt.ui.client.MGWT::onorientationChange(I)($wnd.orientation);
		});
		$doc.body.onorientationchange = func;
		$doc.addEventListener("orientationChanged", func);
		return func;
  }-*/;

  private static native void destroyOrientation(JavaScriptObject o)/*-{
		$doc.body.onorientationchange = null;
		$doc.removeEventListener("orientationChanged", o);
  }-*/;

  private static native void setupPreventScrolling(Element el)/*-{
		var func = function(event) {
			event.preventDefault();
			return false;
		};

		el.ontouchmove = func;

  }-*/;

  /**
   * A utility method to hide the soft keyboard
   */
  public static void hideKeyBoard() {
    final Anchor anchor = new Anchor(" ");

    anchor.getElement().getStyle().setWidth(1, Unit.PX);
    anchor.getElement().getStyle().setHeight(1, Unit.PX);
    anchor.getElement().getStyle().setDisplay(Display.INLINE);
    anchor.getElement().getStyle().setFloat(Float.LEFT);

    RootPanel.get().add(anchor);
    anchor.setFocus(true);

    Scheduler.get().scheduleDeferred(new ScheduledCommand() {

      @Override
      public void execute() {
        anchor.removeFromParent();

      }
    });

  }

  public static void showAddressBar(boolean s) {
    if (s) {
      getAddressBarUtil().showAddressBar();
    } else {
      getAddressBarUtil().hideAddressBar();
    }
  }

  private static AddressBarUtil getAddressBarUtil() {
    if (addressBarUtil == null) {
      addressBarUtil = GWT.create(AddressBarUtil.class);
    }
    return addressBarUtil;
  }

  private static EventBus getManager() {
    if (manager == null) {
      manager = new SimpleEventBus();
    }
    return manager;
  }

}

Commits for litesoft/trunk/mobileGWT/mgwt/src/main/java/com/googlecode/mgwt/ui/client/MGWT.java

Diff revisions: vs.
Revision Author Commited Message
887 Diff Diff GeorgeS picture GeorgeS Tue 11 Dec, 2012 02:05:08 +0000
876 Diff Diff GeorgeS picture GeorgeS Mon 10 Dec, 2012 03:38:59 +0000

Updated to current Version

740 Diff Diff GeorgeS picture GeorgeS Tue 26 Jun, 2012 00:47:02 +0000
681 GeorgeS picture GeorgeS Sun 20 May, 2012 23:24:07 +0000