check Browser Info - Android Network

Android examples for Network:User Agent

Description

check Browser Info

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String userAgent = "java2s.com";
        System.out.println(checkBrowserInfo(userAgent));
    }/*ww w .  ja  va2 s  .c o m*/

    private static final String WB_ANDROID = "Android";
    private static final String WB_IPHONE = "iPhone OS";
    private static final String WB_OPERA = "Opera";
    private static final String WB_FIREFOX = "Firefox";
    private static final String WB_CHROMEFRAME = "chromeframe";
    private static final String WB_CHROME = "Chrome";
    private static final String WB_SAFARI = "Safari";
    private static final String WB_SAFARI_VER = "Version";
    private static final String WB_MSIE = "MSIE";

    public static String checkBrowserInfo(String userAgent) {
        int idx;
        StringBuilder builder = new StringBuilder();

        if ((idx = userAgent.indexOf(WB_ANDROID)) > -1) {
            builder.append(WB_ANDROID + "/");
            idx += WB_ANDROID.length() + 1;
            builder.append(getBrowserVersion(userAgent, idx));
            return builder.toString();
        }

        // iphone
        if ((idx = userAgent.indexOf(WB_IPHONE)) > -1) {
            builder.append(WB_IPHONE + "/");
            idx += WB_IPHONE.length() + 1;
            builder.append(getBrowserVersion(userAgent, idx));
            return builder.toString();
        }

        // opera
        if ((idx = userAgent.indexOf(WB_OPERA)) > -1) {
            builder.append(WB_OPERA + "/");
            idx += WB_OPERA.length() + 1;
            builder.append(getBrowserVersion(userAgent, idx));
            return builder.toString();
        }

        // firefox
        if ((idx = userAgent.indexOf(WB_FIREFOX)) > -1) {
            builder.append(WB_FIREFOX + "/");
            idx += WB_FIREFOX.length() + 1;
            builder.append(getBrowserVersion(userAgent, idx));
            return builder.toString();
        }

        // chromeframe
        if ((idx = userAgent.indexOf(WB_CHROMEFRAME)) > -1) {
            builder.append(WB_CHROMEFRAME + "/");
            idx += WB_CHROMEFRAME.length() + 1;
            builder.append(getBrowserVersion(userAgent, idx));
            return builder.toString();
        }

        // chrome
        if ((idx = userAgent.indexOf(WB_CHROME)) > -1) {
            builder.append(WB_CHROME + "/");
            idx += WB_CHROME.length() + 1;
            builder.append(getBrowserVersion(userAgent, idx));
            return builder.toString();
        }

        // safari
        if (userAgent.indexOf(WB_SAFARI) > -1
                && (idx = userAgent.indexOf(WB_SAFARI_VER)) > -1) {
            builder.append(WB_SAFARI + "/");
            idx += WB_SAFARI_VER.length() + 1;
            builder.append(getBrowserVersion(userAgent, idx));
            return builder.toString();
        }

        // msie
        if ((idx = userAgent.indexOf(WB_MSIE)) > -1) {
            builder.append(WB_MSIE + "/");
            idx += WB_MSIE.length() + 1;
            builder.append(getBrowserVersion(userAgent, idx));
            return builder.toString();
        }


        return "ETC";
    }

    private static String getBrowserVersion(String userAgent, int fromIdx) {
        StringBuilder version = new StringBuilder();
        int dotCnt = 0;
        for (int i = fromIdx; i < userAgent.length(); i++) {
            char c = userAgent.charAt(i);
            if (c == '_')
                c = '.';

            if (c != '.' && (c < '0' || c > '9'))
                break;

            if (c == '.')
                dotCnt++;

            if (dotCnt > 1)
                break;

            version.append(c);
        }

        return version.toString();
    }
}

Related Tutorials