Automatically detecting connections from a phone and switching to small format
authorJaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 29 Nov 2009 09:12:55 +0100
changeset 156e3d6e326eac1
parent 152 07e3bcb65c1d
child 159 0d90e4cb9175
Automatically detecting connections from a phone and switching to small format
freemarkerdor/pom.xml
freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java
     1.1 --- a/freemarkerdor/pom.xml	Thu Nov 19 09:35:29 2009 +0100
     1.2 +++ b/freemarkerdor/pom.xml	Sun Nov 29 09:12:55 2009 +0100
     1.3 @@ -10,7 +10,7 @@
     1.4    <groupId>org.apidesign</groupId>
     1.5    <artifactId>freemarkerdor</artifactId>
     1.6    <name>freemarkerdor</name>
     1.7 -  <version>1.41</version>
     1.8 +  <version>1.42</version>
     1.9    <url>http://maven.apache.org</url>
    1.10    <dependencies>
    1.11      <dependency>
    1.12 @@ -123,3 +123,4 @@
    1.13  
    1.14  
    1.15  
    1.16 +
     2.1 --- a/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Thu Nov 19 09:35:29 2009 +0100
     2.2 +++ b/freemarkerdor/src/main/java/cz/xelfi/quoridor/freemarkerdor/UI.java	Sun Nov 29 09:12:55 2009 +0100
     2.3 @@ -40,6 +40,7 @@
     2.4  import java.io.InputStream;
     2.5  import java.net.URI;
     2.6  import java.util.HashMap;
     2.7 +import java.util.List;
     2.8  import java.util.Locale;
     2.9  import java.util.Map;
    2.10  import java.util.MissingResourceException;
    2.11 @@ -205,6 +206,10 @@
    2.12          if (format.length() == 0) {
    2.13              if (cFormat != null) {
    2.14                  format = cFormat.getValue();
    2.15 +            } else {
    2.16 +                if (isMobile(headers)) {
    2.17 +                    format = "small";
    2.18 +                }
    2.19              }
    2.20          } else {
    2.21              if (cFormat == null || !format.equals(cFormat.getValue())) {
    2.22 @@ -438,4 +443,22 @@
    2.23          return new Viewable(page, map);
    2.24      }
    2.25  
    2.26 +
    2.27 +    private static boolean isMobile(HttpHeaders headers) {
    2.28 +        final String[] keywords = {
    2.29 +            "Profile/MIDP",
    2.30 +        };
    2.31 +        List<String> agent = headers.getRequestHeader(HttpHeaders.USER_AGENT);
    2.32 +        if (agent != null) {
    2.33 +            for (String a : agent) {
    2.34 +                for (String k : keywords) {
    2.35 +                    if (a.contains(k)) {
    2.36 +                        return true;
    2.37 +                    }
    2.38 +                }
    2.39 +            }
    2.40 +        }
    2.41 +        return false;
    2.42 +    }
    2.43 +
    2.44  }