Created
October 15, 2013 19:05
-
-
Save filipenevola/6996961 to your computer and use it in GitHub Desktop.
Detecção Server Side Tablet/Mobile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//dep maven | |
// <!-- identificar browser do usuário --> | |
// <dependency> | |
// <groupId>bitwalker</groupId> | |
// <artifactId>UserAgentUtils</artifactId> | |
// <version>1.9</version> | |
// </dependency> | |
private static String getUserAgentString() { | |
return ((HttpServletRequest) getContext().getExternalContext() | |
.getRequest()).getHeader("User-Agent"); | |
} | |
private static UserAgent getUserAgent() { | |
String userAgentString = getUserAgentString(); | |
if(Strings.isNullOrEmpty(userAgentString)) { | |
return null; | |
} | |
return UserAgent.parseUserAgentString(userAgentString); | |
} | |
public static boolean isBrowserChromeDesktop() { | |
String userAgentString = JsfUtil.getUserAgentString(); | |
if (JsfUtil.isRequestedFromMobileOrTablet() || Strings.isNullOrEmpty(userAgentString)) { | |
return false; | |
} | |
return Browser.CHROME.isInUserAgentString(userAgentString); | |
} | |
public static boolean isRequestedFromMobileOrTablet() { | |
UserAgent ua = getUserAgent(); | |
if (ua == null || ua.getOperatingSystem() == null | |
|| ua.getOperatingSystem().getDeviceType() == null) { | |
return false; | |
} | |
return ua.getOperatingSystem().getDeviceType() == DeviceType.MOBILE | |
|| ua.getOperatingSystem().getDeviceType() == DeviceType.TABLET; | |
} | |
private static FacesContext getContext() { | |
return FacesContext.getCurrentInstance(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment