Skip to content

Instantly share code, notes, and snippets.

@filipenevola
Created October 15, 2013 19:05
Show Gist options
  • Save filipenevola/6996961 to your computer and use it in GitHub Desktop.
Save filipenevola/6996961 to your computer and use it in GitHub Desktop.
Detecção Server Side Tablet/Mobile
//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