From 935850bc41d42e29b75279a718312dbefc70f405 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Wed, 25 Apr 2012 06:21:30 +0000 Subject: [PATCH] Added v-ios and v-android for mobile devices (#7911) svn changeset:23625/svn branch:6.8 --- .../terminal/gwt/client/BrowserInfo.java | 66 +++++++++++++++++-- .../terminal/gwt/client/VBrowserDetails.java | 51 +++++++++++--- .../TestVBrowserDetailsUserAgentParser.java | 39 ++++++++++- 3 files changed, 141 insertions(+), 15 deletions(-) diff --git a/src/com/vaadin/terminal/gwt/client/BrowserInfo.java b/src/com/vaadin/terminal/gwt/client/BrowserInfo.java index 844b4f2e96..a7edee5f32 100644 --- a/src/com/vaadin/terminal/gwt/client/BrowserInfo.java +++ b/src/com/vaadin/terminal/gwt/client/BrowserInfo.java @@ -31,6 +31,9 @@ public class BrowserInfo { private static final String OS_LINUX = "lin"; private static final String OS_MACOSX = "mac"; + private static final String MOS_ANDROID = "android"; + private static final String MOS_IOS = "ios"; + private static BrowserInfo instance; private static String cssClass = null; @@ -66,11 +69,11 @@ public class BrowserInfo { browserDetails.setIEMode(documentMode); } } - + if (browserDetails.isChrome()) { - touchDevice = detectChromeTouchDevice(); + touchDevice = detectChromeTouchDevice(); } else { - touchDevice = detectTouchDevice(); + touchDevice = detectTouchDevice(); } } @@ -78,7 +81,7 @@ public class BrowserInfo { /*-{ try { document.createEvent("TouchEvent");return true;} catch(e){return false;}; }-*/; - + private native boolean detectChromeTouchDevice() /*-{ return ("ontouchstart" in window); @@ -169,6 +172,11 @@ public class BrowserInfo { if (osClass != null) { cssClass = cssClass + " " + prefix + osClass; } + + String mosClass = getMobileOperatingSystemClass(); + if (mosClass != null) { + cssClass = cssClass + " " + prefix + mosClass; + } } return cssClass; @@ -186,6 +194,16 @@ public class BrowserInfo { return null; } + private String getMobileOperatingSystemClass() { + if (isAndroid()) { + return MOS_ANDROID; + } else if (browserDetails.isIOS()) { + return MOS_IOS; + } + // Unknown MOS + return null; + } + public boolean isIE() { return browserDetails.isIE(); } @@ -409,4 +427,44 @@ public class BrowserInfo { return touchDevice; } + /** + * Checks if the browser is run on iOS + * + * @return true if the browser is run on iOS, false otherwise + */ + public boolean isIOS() { + return browserDetails.isIOS(); + } + + /** + * Checks if the browser is run on Android + * + * @return true if the browser is run on Android, false otherwise + */ + public boolean isAndroid() { + return browserDetails.isAndroid(); + } + + /** + * Checks if the browser is capable of handling scrolling natively or if a + * touch scroll helper is needed for scrolling. + * + * @return true if browser needs a touch scroll helper, false if the browser + * can handle scrolling natively + */ + public boolean requiresTouchScrollDelegate() { + if (!isTouchDevice()) { + return false; + } + + if (isAndroid() && isWebkit() && getWebkitVersion() < 534) { + return true; + } + // if (isIOS() && isWebkit() && getWebkitVersion() < ???) { + // return true; + // } + + return false; + } + } diff --git a/src/com/vaadin/terminal/gwt/client/VBrowserDetails.java b/src/com/vaadin/terminal/gwt/client/VBrowserDetails.java index aaef981bab..8e9d683baf 100644 --- a/src/com/vaadin/terminal/gwt/client/VBrowserDetails.java +++ b/src/com/vaadin/terminal/gwt/client/VBrowserDetails.java @@ -28,9 +28,16 @@ public class VBrowserDetails implements Serializable { private boolean isOpera = false; private boolean isIE = false; - private boolean isWindows = false; - private boolean isMacOSX = false; - private boolean isLinux = false; + private OperatingSystem os = OperatingSystem.UNKNOWN; + private MobileSystem ms = MobileSystem.UNKNOWN; + + public enum OperatingSystem { + UNKNOWN, WINDOWS, MACOSX, LINUX; + } + + public enum MobileSystem { + UNKNOWN, IOS, ANDROID; + } private float browserEngineVersion = -1; private int browserMajorVersion = -1; @@ -115,13 +122,21 @@ public class VBrowserDetails implements Serializable { // Operating system if (userAgent.contains("windows ")) { - isWindows = true; + os = OperatingSystem.WINDOWS; } else if (userAgent.contains("linux")) { - isLinux = true; + os = OperatingSystem.LINUX; + if (userAgent.contains("android")) { + ms = MobileSystem.ANDROID; + + } } else if (userAgent.contains("macintosh") || userAgent.contains("mac osx") || userAgent.contains("mac os x")) { - isMacOSX = true; + os = OperatingSystem.MACOSX; + if (userAgent.contains("ipad") || userAgent.contains("ipod") + || userAgent.contains("iphone")) { + ms = MobileSystem.IOS; + } } } @@ -281,7 +296,7 @@ public class VBrowserDetails implements Serializable { * @return true if run on Windows, false otherwise */ public boolean isWindows() { - return isWindows; + return os == OperatingSystem.WINDOWS; } /** @@ -290,7 +305,7 @@ public class VBrowserDetails implements Serializable { * @return true if run on Mac OSX, false otherwise */ public boolean isMacOSX() { - return isMacOSX; + return os == OperatingSystem.MACOSX; } /** @@ -299,7 +314,25 @@ public class VBrowserDetails implements Serializable { * @return true if run on Linux, false otherwise */ public boolean isLinux() { - return isLinux; + return os == OperatingSystem.LINUX; + } + + /** + * Tests if the browser is run on Android. + * + * @return true if run on Android, false otherwise + */ + public boolean isAndroid() { + return ms == MobileSystem.ANDROID; + } + + /** + * Tests if the browser is run in iOS. + * + * @return true if run in iOS, false otherwise + */ + public boolean isIOS() { + return ms == MobileSystem.IOS; } } diff --git a/tests/client-side/com/vaadin/terminal/gwt/client/TestVBrowserDetailsUserAgentParser.java b/tests/client-side/com/vaadin/terminal/gwt/client/TestVBrowserDetailsUserAgentParser.java index f661b6cf15..98159775bd 100644 --- a/tests/client-side/com/vaadin/terminal/gwt/client/TestVBrowserDetailsUserAgentParser.java +++ b/tests/client-side/com/vaadin/terminal/gwt/client/TestVBrowserDetailsUserAgentParser.java @@ -2,8 +2,6 @@ package com.vaadin.terminal.gwt.client; import junit.framework.TestCase; -import com.vaadin.terminal.gwt.client.VBrowserDetails; - public class TestVBrowserDetailsUserAgentParser extends TestCase { private static final String FIREFOX30_WINDOWS = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6"; @@ -37,6 +35,9 @@ public class TestVBrowserDetailsUserAgentParser extends TestCase { private static final String SAFARI3_WINDOWS = "Mozilla/5.0 (Windows; U; Windows NT 5.1; cs-CZ) AppleWebKit/525.28.3 (KHTML, like Gecko) Version/3.2.3 Safari/525.29"; private static final String SAFARI4_MAC = "Mozilla/5.0 (Macintosh; U; PPC Mac OS X 10_5_8; en-us) AppleWebKit/531.22.7 (KHTML, like Gecko) Version/4.0.5 Safari/531.22.7"; + private static final String IPHONE_IOS_5_1 = "Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9B179 Safari/7534.48.3"; + private static final String IPAD_IOS_4_3_1 = "Mozilla/5.0 (iPad; U; CPU OS 4_3_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8G4 Safari/6533.18.5"; + public void testSafari3() { VBrowserDetails bd = new VBrowserDetails(SAFARI3_WINDOWS); assertWebKit(bd); @@ -57,6 +58,28 @@ public class TestVBrowserDetailsUserAgentParser extends TestCase { assertMacOSX(bd); } + public void testIPhoneIOS5() { + VBrowserDetails bd = new VBrowserDetails(IPHONE_IOS_5_1); + assertWebKit(bd); + assertSafari(bd); + assertBrowserMajorVersion(bd, 5); + assertBrowserMinorVersion(bd, 1); + assertEngineVersion(bd, 534f); + assertMacOSX(bd); + assertIOS(bd); + } + + public void testIPadIOS4() { + VBrowserDetails bd = new VBrowserDetails(IPAD_IOS_4_3_1); + assertWebKit(bd); + assertSafari(bd); + assertBrowserMajorVersion(bd, 5); + assertBrowserMinorVersion(bd, 0); + assertEngineVersion(bd, 533f); + assertMacOSX(bd); + assertIOS(bd); + } + public void testChrome3() { VBrowserDetails bd = new VBrowserDetails(CHROME3_MAC); assertWebKit(bd); @@ -352,18 +375,30 @@ public class TestVBrowserDetailsUserAgentParser extends TestCase { assertFalse(browserDetails.isLinux()); assertFalse(browserDetails.isWindows()); assertTrue(browserDetails.isMacOSX()); + assertFalse(browserDetails.isAndroid()); + } + + private void assertIOS(VBrowserDetails browserDetails) { + assertFalse(browserDetails.isLinux()); + assertFalse(browserDetails.isWindows()); + assertTrue(browserDetails.isMacOSX()); + assertTrue(browserDetails.isIOS()); + assertFalse(browserDetails.isAndroid()); } private void assertWindows(VBrowserDetails browserDetails) { assertFalse(browserDetails.isLinux()); assertTrue(browserDetails.isWindows()); assertFalse(browserDetails.isMacOSX()); + assertFalse(browserDetails.isIOS()); + assertFalse(browserDetails.isAndroid()); } private void assertLinux(VBrowserDetails browserDetails) { assertTrue(browserDetails.isLinux()); assertFalse(browserDetails.isWindows()); assertFalse(browserDetails.isMacOSX()); + assertFalse(browserDetails.isIOS()); } } -- 2.39.5