How to distinguish between a Phone and a Tablet from ADF ManagedBean

There are usecases where, only in phones few components need to be replaced or hided. It may still be displayed in tablets.

The ADF supported and apache supported options currently (Feb 2013) is not able to distinguish between Phone and Tablet. Even though it’s able recognize them as ‘touchable’ client.

Reading the Header information does the trick.

IOS

Mozilla/5.0 (iPhone; CPU iPhone OS 6_0_2 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A551 Safari/8536.25

Mozilla/5.0 (iPad; CPU OS 6_0_1 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A523 Safari/8536.25

Android

Tablet – Mozilla/5.0 (Linux; U; Android 3.2; en-us; GT-P7510 Build/HTJ85B) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13

Phone – Mozilla/5.0 (Linux; Android 4.2.1; Galaxy Nexus Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19

Code

HttpServletRequest request =
           (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
    String userAgent = request.getHeader("user-agent");
    System.out.println(userAgent);

Other alternatives are but they recognizes both iPhone and iPad as the same (iPhone):

adfFacesContext.getAgent() or
org.apache.myfaces.trinidad.context.RequestContext.getCurrentInstance().getAgent();

Javascript code for the same. (Javascript also provides the very same information read from the header. )


      function checkPlatform(event)
      {
        alert(navigator.platform + ' ------------- ' + navigator.userAgent);
	AdfCustomEvent.queue(event.getSource(), "handleEnterEvent",
	{mobilePlatform: navigator.platform}, {userAgent: navigator.userAgent}, true);
        event.cancel();

      }

So now you will be able to switch or hide the component and customize the UI from the server level before serving the client request.

 

About Arun

Love finding and sharing new means to improve our daily life. https://www.youtube.com/watch?v=YmDWcixx8Pg&t=15s View all posts by Arun

2 responses to “How to distinguish between a Phone and a Tablet from ADF ManagedBean

Leave a comment