Java Code Examples for android.webkit.WebSettings#getDefaultUserAgent()

The following examples show how to use android.webkit.WebSettings#getDefaultUserAgent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpUtils.java    From YiZhi with Apache License 2.0 6 votes vote down vote up
/**
 * 获取UserAgent
 *
 * @return UserAgent
 */
@NonNull
public static String getUserAgent() {
    String userAgent = "";
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        try {
            userAgent = WebSettings.getDefaultUserAgent(AppUtils.getContext());
        } catch (Exception e) {
            userAgent = System.getProperty("http.agent");
        }
    } else {
        userAgent = System.getProperty("http.agent");
    }
    StringBuffer sb = new StringBuffer();
    for (int i = 0, length = userAgent.length(); i < length; i++) {
        char c = userAgent.charAt(i);
        if (c <= '\u001f' || c >= '\u007f') {
            sb.append(String.format("\\u%04x", (int) c));
        } else {
            sb.append(c);
        }
    }
    return sb.toString();
}
 
Example 2
Source File: WebViewActivity.java    From Aria2App with GNU General Public License v3.0 5 votes vote down vote up
private void toggleDesktopMode(boolean enabled) {
    WebSettings settings = web.getSettings();

    settings.setLoadWithOverviewMode(enabled);
    settings.setUseWideViewPort(enabled);

    settings.setSupportZoom(enabled);
    settings.setBuiltInZoomControls(enabled);
    settings.setDisplayZoomControls(!enabled);

    String userAgent = enabled ? "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.4) Gecko/20100101 Firefox/4.0" : WebSettings.getDefaultUserAgent(this);
    userAgent += " Aria2App/" + BuildConfig.VERSION_NAME + "-" + BuildConfig.FLAVOR;
    settings.setUserAgentString(userAgent);
}
 
Example 3
Source File: App.java    From ForPDA with GNU General Public License v3.0 5 votes vote down vote up
public boolean isWebViewFound(Context context) {
    if (webViewFound == null) {
        try {
            WebSettings.getDefaultUserAgent(context);
            webViewFound = true;
        } catch (Exception e) {
            webViewFound = false;
        }
    }
    return webViewFound;
}
 
Example 4
Source File: Api.java    From materialup with Apache License 2.0 5 votes vote down vote up
public static String getUserAgent(Context context) {
    if (sUserAgent != null) {
        return sUserAgent;
    }
    String agent = context == null ? "" : WebSettings.getDefaultUserAgent(context);
    sUserAgent = agent + " ; MaterialUp-App/" + BuildConfig.VERSION_CODE;
    return sUserAgent;
}
 
Example 5
Source File: EasyIdMod.java    From easydeviceinfo with Apache License 2.0 5 votes vote down vote up
/**
 * Gets ua.
 *
 * @return the ua
 */
public final String getUA() {
  final String systemUa = System.getProperty("http.agent");
  String result;
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
    result = WebSettings.getDefaultUserAgent(context) + "__" + systemUa;
  } else {
    result = new WebView(context).getSettings().getUserAgentString() + "__" + systemUa;
  }
  return CheckValidityUtil.checkValidData(result);
}
 
Example 6
Source File: WebViewPageActivity.java    From phphub-android with Apache License 2.0 5 votes vote down vote up
private String getUserAgent() {
    if (Build.VERSION.SDK_INT < 19) {
        WebView webView = new WebView(this);
        WebSettings settings = webView.getSettings();
        return settings.getUserAgentString();
    }

    // api >= 19
    return WebSettings.getDefaultUserAgent(this);
}
 
Example 7
Source File: Helper.java    From Hentoid with Apache License 2.0 5 votes vote down vote up
public static int getChromeVersion(Context context) {
    String chromeString = "Chrome/";
    String defaultUserAgent = WebSettings.getDefaultUserAgent(context);
    if (defaultUserAgent.contains(chromeString)) {
        int chromeIndex = defaultUserAgent.indexOf(chromeString);
        int dotIndex = defaultUserAgent.indexOf('.', chromeIndex);
        String version = defaultUserAgent.substring(chromeIndex + chromeString.length(), dotIndex);
        return Integer.parseInt(version);
    } else return -1;
}
 
Example 8
Source File: RNDeviceModule.java    From react-native-device-info with MIT License 5 votes vote down vote up
@ReactMethod(isBlockingSynchronousMethod = true)
public String getUserAgentSync() {
  try {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
      return WebSettings.getDefaultUserAgent(getReactApplicationContext());
    } else {
      return System.getProperty("http.agent");
    }
  } catch (RuntimeException e) {
    return System.getProperty("http.agent");
  }
}
 
Example 9
Source File: NestedWebview.java    From SimplicityBrowser with MIT License 4 votes vote down vote up
public static void getUserAgent(Context context) {
    WebSettings.getDefaultUserAgent(context);
}