Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect tablet in Vaadin 7

Tags:

vaadin

I am looking for a way to easily detect whether my user is on a tablet or on a fullblown pc. Any idea how to do that?

getSession().getBrowser().isTouchDevice() won't work with more and more pc's having a touchscreen. And getBrowser() is deprecated in Vaadin 7 anyhow.

I am not using vaadin-touchkit (should i?)

Regards, Rob.

like image 325
bluevoid Avatar asked Apr 23 '13 21:04

bluevoid


2 Answers

Here is a one way for Vaadin 7:

in UI.init() you get the parameter VaadinRequest, which you can cast (after type check) to VaadinServletRequest and use the getHttpServletRequest to get the underlying HTTP request. This you can then use to check the "User-Agent". Something like:

if (request instanceof VaadinServletRequest) {
    HttpServletRequest httpRequest = ((VaadinServletRequest)request).getHttpServletRequest();
    String userAgent = httpRequest.getHeader("User-Agent").toLowerCase();

    // TODO: Check user agent for all tablet matching keywords
    if (userAgent.contains("ipad")) { 
        //...
    }

}
like image 153
eeq Avatar answered Sep 22 '22 00:09

eeq


You can also get the User-Agent HTTP header with

Page.getCurrent().getWebBrowser().getBrowserApplication()

It seems like there is no attribute sent in the headers to differenciate between tablet or pc but you can find out whether it's a touch screen by using.

if(Page.getCurrent().getWebBrowser().isTouchDevice()){ //..}
  • getWebBrowser is not deprectated up to version 7.4.7
like image 40
PbxMan Avatar answered Sep 19 '22 00:09

PbxMan