开发者

GWT - How to compile mobile permutations

开发者 https://www.devze.com 2023-03-27 09:43 出处:网络
I am aw开发者_如何学Care of how to use deferred binding to compile a GWT app for different user agents, but this does not seem to offer a way to distingiush between desktop + mobile browsers.

I am aw开发者_如何学Care of how to use deferred binding to compile a GWT app for different user agents, but this does not seem to offer a way to distingiush between desktop + mobile browsers.

Other than by making a new app based on gwt-mobile-webkit, how would you convert an existing GWT app to have a restyled mobile interface?


If you use the MVP pattern described here, you can switch the views' implementations based on the user agent.

You can have a ClientFactoryImpl and ClientFactoryMobileImpl. Then you use GWT.create(ClientFactory.class) to create the implementation defined into the .gwt.xml file.

Here is an example of the .gwt.xml file

<replace-with class="com.bell.cts.e911.ers.web.client.ClientFactoryImpl">
  <when-type-is class="com.bell.cts.e911.ers.web.client.ClientFactory" />
  <when-property-is name="user.agent" value="ie6" />
</replace-with>

<replace-with class="com.bell.cts.e911.ers.web.client.ClientFactoryMobileImpl">
  <when-type-is class="com.bell.cts.e911.ers.web.client.ClientFactory" />
  <when-property-is name="user.agent" value="mobilesafari" />
</replace-with>

You can always set up user.agents using the technique described here : http://code.google.com/p/google-web-toolkit/wiki/ConditionalProperties

http://jectbd.com/?p=1282


You can see this sample application from GWT: http://code.google.com/p/google-web-toolkit/source/browse/trunk/samples/mobilewebapp/src/com/google/gwt/sample/mobilewebapp/?r=10041 It detects the form factor in the 'FormFactor.gwt.xml' module which might look like this:

<?xml version="1.0" encoding="UTF-8"?>

<!-- Defines the formfactor property and its provider function. -->
<module>

  <!-- Determine if we are in a mobile browser. -->
  <define-property name="formfactor" values="desktop,tablet,mobile"/>

  <property-provider name="formfactor">
  <![CDATA[
      // Look for the formfactor as a url argument.
      var args = location.search;
      var start = args.indexOf("formfactor");
      if (start >= 0) {
        var value = args.substring(start);
        var begin = value.indexOf("=") + 1;
        var end = value.indexOf("&");
        if (end == -1) {
          end = value.length;
        }
        return value.substring(begin, end);
      }

      // Detect form factor from user agent.
      var ua = navigator.userAgent.toLowerCase();
      if (ua.indexOf("iphone") != -1 || ua.indexOf("ipod") != -1) {
        // iphone and ipod.
        return "mobile";
      } else if (ua.indexOf("ipad") != -1) {
        // ipad.
        return "tablet";
      } else if (ua.indexOf("android") != -1 || ua.indexOf("mobile") != -1) {
        /*
         * Android - determine the form factor of android devices based on the diagonal screen
         * size. Anything under six inches is a phone, anything over six inches is a tablet.
         */
        var dpi = 160;
        var width = $wnd.screen.width / dpi;
        var height = $wnd.screen.height / dpi;
        var size = Math.sqrt(width*width + height*height);
        return (size < 6) ? "mobile" : "tablet";
      }

      // Everything else is a desktop.
      return "desktop";
  ]]>
  </property-provider>

</module>
0

精彩评论

暂无评论...
验证码 换一张
取 消