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

The following examples show how to use android.webkit.WebSettings#getUserAgentString() . 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: WebViewActivity.java    From Tok-Android with GNU General Public License v3.0 6 votes vote down vote up
private void initSetting() {
    WebSettings settings = mWebView.getSettings();
    settings.setAppCacheEnabled(false);
    settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        WebView.setWebContentsDebuggingEnabled(true);
    }
    String userAgent = settings.getUserAgentString();
    settings.setUserAgentString(
        userAgent + " TokAndroid/" + String.valueOf(BuildConfig.VERSION_NAME));
    settings.setJavaScriptEnabled(true);
    settings.setAllowFileAccessFromFileURLs(false);
    settings.setAllowUniversalAccessFromFileURLs(false);
    settings.setDomStorageEnabled(true);
    settings.setAllowFileAccess(true);
    settings.setAppCacheEnabled(true);
    settings.setCacheMode(WebSettings.LOAD_DEFAULT);
    settings.setDatabaseEnabled(true);
    settings.setSupportZoom(true);
    settings.setBuiltInZoomControls(true);
    settings.setDisplayZoomControls(false);
    settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
    settings.setUseWideViewPort(true);
    settings.setLoadWithOverviewMode(true);
}
 
Example 2
Source File: CustomWebView.java    From Lucid-Browser with Apache License 2.0 6 votes vote down vote up
public void setDesktopMode(final boolean enabled) {
	final WebSettings webSettings = getSettings();

	if (origionalUserAgent==null) {
		origionalUserAgent = webSettings.getUserAgentString();
		Log.d("LB", "Your user agent is:"+origionalUserAgent);
	}

	String newUserAgent = origionalUserAgent;
	if (enabled) {
		try {
			String ua = webSettings.getUserAgentString();
			String androidOSString = webSettings.getUserAgentString().substring(ua.indexOf("("), ua.indexOf(")") + 1);
			newUserAgent = origionalUserAgent.replace(androidOSString,"(X11; Linux x86_64)");
		}catch (Exception e){
			e.printStackTrace();
		}
	}
	else {
		newUserAgent = origionalUserAgent;
	}

	webSettings.setUserAgentString(newUserAgent);
	webSettings.setUseWideViewPort(enabled);
	webSettings.setLoadWithOverviewMode(enabled);
}
 
Example 3
Source File: WebViewInitializer.java    From FastWaiMai with MIT License 5 votes vote down vote up
/**
 * 初始化传入的webView
 */
@SuppressLint("SetJavaScriptEnabled")
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public WebView initialWebView(WebView webView){
	webView.setHorizontalScrollBarEnabled(false);
	//不能纵向滚动
	webView.setVerticalScrollBarEnabled(false);
	//允许截图
	webView.setDrawingCacheEnabled(true);
	//屏蔽长按事件
	webView.setOnLongClickListener(new View.OnLongClickListener() {
		@Override
		public boolean onLongClick(View v) {
			return true;
		}
	});
	//初始化WebSettings
	final WebSettings settings = webView.getSettings();
	settings.setJavaScriptEnabled(true);
	final String ua = settings.getUserAgentString();
	settings.setUserAgentString(ua + "Latte");
	//隐藏缩放控件
	settings.setBuiltInZoomControls(false);
	settings.setDisplayZoomControls(false);
	//禁止缩放
	settings.setSupportZoom(false);
	//文件权限
	settings.setAllowFileAccess(true);
	settings.setAllowFileAccessFromFileURLs(true);
	settings.setAllowUniversalAccessFromFileURLs(true);
	settings.setAllowContentAccess(true);
	//缓存相关
	settings.setAppCacheEnabled(true);
	settings.setDomStorageEnabled(true);
	settings.setDatabaseEnabled(true);
	settings.setCacheMode(WebSettings.LOAD_DEFAULT);

	return webView;
}
 
Example 4
Source File: NinjaWebView.java    From Ninja with Apache License 2.0 5 votes vote down vote up
private synchronized void initWebSettings() {
    WebSettings webSettings = getSettings();
    userAgentOriginal = webSettings.getUserAgentString();

    webSettings.setAllowContentAccess(true);
    webSettings.setAllowFileAccess(true);
    webSettings.setAllowFileAccessFromFileURLs(true);
    webSettings.setAllowUniversalAccessFromFileURLs(true);

    webSettings.setAppCacheEnabled(true);
    webSettings.setAppCachePath(context.getCacheDir().toString());
    webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);
    webSettings.setDatabaseEnabled(true);
    webSettings.setDomStorageEnabled(true);
    webSettings.setGeolocationDatabasePath(context.getFilesDir().toString());

    webSettings.setSupportZoom(true);
    webSettings.setBuiltInZoomControls(true);
    webSettings.setDisplayZoomControls(false);

    webSettings.setDefaultTextEncodingName(BrowserUnit.URL_ENCODING);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        webSettings.setLoadsImagesAutomatically(true);
    } else {
        webSettings.setLoadsImagesAutomatically(false);
    }
}
 
Example 5
Source File: HuPuWebView.java    From TLint with Apache License 2.0 5 votes vote down vote up
private void init() {
    ((MyApplication) getContext().getApplicationContext()).getApplicationComponent().inject(this);
    WebSettings settings = getSettings();
    settings.setBuiltInZoomControls(false);
    settings.setSupportZoom(false);
    settings.setJavaScriptEnabled(true);
    settings.setAllowFileAccess(true);
    settings.setSupportMultipleWindows(false);
    settings.setJavaScriptCanOpenWindowsAutomatically(true);
    settings.setDomStorageEnabled(true);
    settings.setCacheMode(1);
    settings.setUseWideViewPort(true);
    if (Build.VERSION.SDK_INT > 6) {
        settings.setAppCacheEnabled(true);
        settings.setLoadWithOverviewMode(true);
    }
    settings.setCacheMode(WebSettings.LOAD_DEFAULT);
    String path = getContext().getFilesDir().getPath();
    settings.setGeolocationEnabled(true);
    settings.setGeolocationDatabasePath(path);
    settings.setDomStorageEnabled(true);
    this.basicUA = settings.getUserAgentString() + " kanqiu/7.05.6303/7059";
    setBackgroundColor(0);
    initWebViewClient();
    try {
        if (mUserStorage.isLogin()) {
            String token = mUserStorage.getToken();
            CookieManager cookieManager = CookieManager.getInstance();
            cookieManager.setCookie("http://bbs.mobileapi.hupu.com",
                    "u=" + URLEncoder.encode(mUserStorage.getCookie(), "utf-8"));
            cookieManager.setCookie("http://bbs.mobileapi.hupu.com",
                    "_gamesu=" + URLEncoder.encode(token, "utf-8"));
            cookieManager.setCookie("http://bbs.mobileapi.hupu.com", "_inKanqiuApp=1");
            cookieManager.setCookie("http://bbs.mobileapi.hupu.com", "_kanqiu=1");
            CookieSyncManager.getInstance().sync();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
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: HuPuWebView.java    From SprintNBA with Apache License 2.0 5 votes vote down vote up
private void init() {
    WebSettings settings = getSettings();
    settings.setBuiltInZoomControls(false);
    settings.setSupportZoom(false);
    settings.setJavaScriptEnabled(true);
    settings.setAllowFileAccess(true);
    settings.setSupportMultipleWindows(false);
    settings.setJavaScriptCanOpenWindowsAutomatically(true);
    settings.setDomStorageEnabled(true);
    settings.setCacheMode(WebSettings.LOAD_DEFAULT);
    settings.setUseWideViewPort(true);
    if (Build.VERSION.SDK_INT > 6) {
        settings.setAppCacheEnabled(true);
        settings.setLoadWithOverviewMode(true);
    }
    String path = getContext().getFilesDir().getPath();
    settings.setGeolocationEnabled(true);
    settings.setGeolocationDatabasePath(path);
    settings.setDomStorageEnabled(true);
    this.basicUA = settings.getUserAgentString() + " kanqiu/7.05.6303/7059";
    setBackgroundColor(0);
    initWebViewClient();
    setWebChromeClient(new HuPuChromeClient());
    try {
        if (SettingPrefUtils.isLogin()) {
            CookieManager cookieManager = CookieManager.getInstance();
            cookieManager.setCookie("http://bbs.mobileapi.hupu.com", "u=" + SettingPrefUtils.getCookies());
            cookieManager.setCookie("http://bbs.mobileapi.hupu.com", "_gamesu=" + URLEncoder.encode(SettingPrefUtils.getToken(), "utf-8"));
            cookieManager.setCookie("http://bbs.mobileapi.hupu.com", "_inKanqiuApp=1");
            cookieManager.setCookie("http://bbs.mobileapi.hupu.com", "_kanqiu=1");
            CookieSyncManager.getInstance().sync();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 8
Source File: ClassicWebViewProvider.java    From focus-android with Mozilla Public License 2.0 5 votes vote down vote up
private static String toggleDesktopUA(final WebSettings settings, final boolean requestDesktop) {
    final String existingUAString = settings.getUserAgentString();
    if (requestDesktop) {
        return existingUAString.replace("Mobile", "eliboM").replace("Android", "diordnA");
    } else {
        return existingUAString.replace("eliboM", "Mobile").replace("diordnA", "Android");
    }
}
 
Example 9
Source File: ClassicWebViewProvider.java    From focus-android with Mozilla Public License 2.0 5 votes vote down vote up
private String buildUserAgentString(final Context context, final WebSettings settings, final String appName) {
    final StringBuilder uaBuilder = new StringBuilder();

    uaBuilder.append("Mozilla/5.0");

    // WebView by default includes "; wv" as part of the platform string, but we're a full browser
    // so we shouldn't include that.
    // Most webview based browsers (and chrome), include the device name AND build ID, e.g.
    // "Pixel XL Build/NOF26V", that seems unnecessary (and not great from a privacy perspective),
    // so we skip that too.
    uaBuilder.append(" (Linux; Android ").append(Build.VERSION.RELEASE).append(") ");

    final String existingWebViewUA = settings.getUserAgentString();

    final String appVersion;
    try {
        appVersion = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
    } catch (PackageManager.NameNotFoundException e) {
        // This should be impossible - we should always be able to get information about ourselves:
        throw new IllegalStateException("Unable find package details for Focus", e);
    }

    final String focusToken = appName + "/" + appVersion;
    uaBuilder.append(getUABrowserString(existingWebViewUA, focusToken));

    return uaBuilder.toString();
}
 
Example 10
Source File: BaseWebView.java    From dcs-sdk-java with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
private void init(Context context) {
    this.setVerticalScrollBarEnabled(false);
    this.setHorizontalScrollBarEnabled(false);
    if (Build.VERSION.SDK_INT < 19) {
        removeJavascriptInterface("searchBoxJavaBridge_");
    }

    WebSettings localWebSettings = this.getSettings();
    try {
        // 禁用file协议,http://www.tuicool.com/articles/Q36ZfuF, 防止Android WebView File域攻击
        localWebSettings.setAllowFileAccess(false);
        localWebSettings.setSupportZoom(false);
        localWebSettings.setBuiltInZoomControls(false);
        localWebSettings.setUseWideViewPort(true);
        localWebSettings.setDomStorageEnabled(true);
        localWebSettings.setLoadWithOverviewMode(true);
        localWebSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        localWebSettings.setPluginState(PluginState.ON);
        // 启用数据库
        localWebSettings.setDatabaseEnabled(true);
        // 设置定位的数据库路径
        String dir = context.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
        localWebSettings.setGeolocationDatabasePath(dir);
        localWebSettings.setGeolocationEnabled(true);
        localWebSettings.setJavaScriptEnabled(true);
        localWebSettings.setSavePassword(false);
        String agent = localWebSettings.getUserAgentString();

        localWebSettings.setUserAgentString(agent);
        // setCookie(context, ".baidu.com", bdussCookie);

    } catch (Exception e1) {
        e1.printStackTrace();
    }
    this.setWebViewClient(new BridgeWebViewClient());
}
 
Example 11
Source File: InnerFastClient.java    From FastWebView with MIT License 5 votes vote down vote up
InnerFastClient(FastWebView owner) {
    mOwner = owner;
    WebSettings settings = owner.getSettings();
    mWebViewCacheMode = settings.getCacheMode();
    mUserAgent = settings.getUserAgentString();
    mWebViewCache = new WebViewCacheImpl(owner.getContext());
}
 
Example 12
Source File: SystemWebViewEngine.java    From keemob with MIT License 4 votes vote down vote up
@SuppressLint({"NewApi", "SetJavaScriptEnabled"})
@SuppressWarnings("deprecation")
private void initWebViewSettings() {
    webView.setInitialScale(0);
    webView.setVerticalScrollBarEnabled(false);
    // Enable JavaScript
    final WebSettings settings = webView.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setJavaScriptCanOpenWindowsAutomatically(true);
    settings.setLayoutAlgorithm(LayoutAlgorithm.NORMAL);

    String manufacturer = android.os.Build.MANUFACTURER;
    LOG.d(TAG, "CordovaWebView is running on device made by: " + manufacturer);

    //We don't save any form data in the application
    settings.setSaveFormData(false);
    settings.setSavePassword(false);

    // Jellybean rightfully tried to lock this down. Too bad they didn't give us a whitelist
    // while we do this
    settings.setAllowUniversalAccessFromFileURLs(true);
    settings.setMediaPlaybackRequiresUserGesture(false);

    // Enable database
    // We keep this disabled because we use or shim to get around DOM_EXCEPTION_ERROR_16
    String databasePath = webView.getContext().getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
    settings.setDatabaseEnabled(true);
    settings.setDatabasePath(databasePath);


    //Determine whether we're in debug or release mode, and turn on Debugging!
    ApplicationInfo appInfo = webView.getContext().getApplicationContext().getApplicationInfo();
    if ((appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
        enableRemoteDebugging();
    }

    settings.setGeolocationDatabasePath(databasePath);

    // Enable DOM storage
    settings.setDomStorageEnabled(true);

    // Enable built-in geolocation
    settings.setGeolocationEnabled(true);

    // Enable AppCache
    // Fix for CB-2282
    settings.setAppCacheMaxSize(5 * 1048576);
    settings.setAppCachePath(databasePath);
    settings.setAppCacheEnabled(true);

    // Fix for CB-1405
    // Google issue 4641
    String defaultUserAgent = settings.getUserAgentString();

    // Fix for CB-3360
    String overrideUserAgent = preferences.getString("OverrideUserAgent", null);
    if (overrideUserAgent != null) {
        settings.setUserAgentString(overrideUserAgent);
    } else {
        String appendUserAgent = preferences.getString("AppendUserAgent", null);
        if (appendUserAgent != null) {
            settings.setUserAgentString(defaultUserAgent + " " + appendUserAgent);
        }
    }
    // End CB-3360

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
    if (this.receiver == null) {
        this.receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                settings.getUserAgentString();
            }
        };
        webView.getContext().registerReceiver(this.receiver, intentFilter);
    }
    // end CB-1405
}
 
Example 13
Source File: BaseWebFragment.java    From Android-Application-ZJB with Apache License 2.0 4 votes vote down vote up
protected void initialize() {
    //添加H5的基本参数
    UrlParserManager.getInstance().addParams(UrlParserManager.METHOD_CHANNEL, "app");
    mParamBuilder = getParams();
    mUrl = UrlParserManager.getInstance().parsePlaceholderUrl(mParamBuilder.getUrl());

    mWebView.setProgressbar(mProgressBar);
    WebSettings ws = mWebView.getSettings();
    ws.setBuiltInZoomControls(false); // 缩放
    ws.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
    ws.setUseWideViewPort(true);
    ws.setLoadWithOverviewMode(true);
    ws.setSaveFormData(true);
    ws.setDomStorageEnabled(true);//开启 database storage API 功能
    //设置自定义UserAgent
    String agent = ws.getUserAgentString();
    ws.setUserAgentString(createUserAgent(agent));
    mWebView.setDownloadListener((url, userAgent, contentDisposition, mimeType, contentLength) -> {
        if (url != null && url.startsWith("http://"))
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
    });
    mWebView.loadUrl(mUrl);
    mWebView.setOnKeyListener((v, keyCode, event) -> {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            //表示按返回键时的操作
            if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
                mWebView.goBack();   //后退
                return true;    //已处理
            }
        }
        return false;
    });

    mWebView.setDefaultHandler(new DefaultHandler());
    //注册分享方法
    mWebView.registerHandler("shareJs", (data, function) -> {
        try {
            if (!TextUtils.isEmpty(data)) {
                WebShareBean shareBean = GsonUtil.fromJson(data, WebShareBean.class);
                shareJs(shareBean);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    });

    //注册设置标题方法
    mWebView.registerHandler("webTitleJs", ((data, function) -> webTitleJs(data)));
    //注册设置分享按钮方法
    mWebView.registerHandler("disableShareJs", ((data, function) -> {
        if ("true".equals(data)) {
            disableShareJs(true);
        } else {
            disableShareJs(false);
        }

    }));

    //注册微信支付
    mWebView.registerHandler("wxPayJs", ((data, function) -> wxPayJs(data)));
}
 
Example 14
Source File: SystemWebViewEngine.java    From countly-sdk-cordova with MIT License 4 votes vote down vote up
@SuppressLint({"NewApi", "SetJavaScriptEnabled"})
@SuppressWarnings("deprecation")
private void initWebViewSettings() {
    webView.setInitialScale(0);
    webView.setVerticalScrollBarEnabled(false);
    // Enable JavaScript
    final WebSettings settings = webView.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setJavaScriptCanOpenWindowsAutomatically(true);
    settings.setLayoutAlgorithm(LayoutAlgorithm.NORMAL);

    String manufacturer = android.os.Build.MANUFACTURER;
    LOG.d(TAG, "CordovaWebView is running on device made by: " + manufacturer);

    //We don't save any form data in the application
    settings.setSaveFormData(false);
    settings.setSavePassword(false);

    // Jellybean rightfully tried to lock this down. Too bad they didn't give us a whitelist
    // while we do this
    settings.setAllowUniversalAccessFromFileURLs(true);
    settings.setMediaPlaybackRequiresUserGesture(false);

    // Enable database
    // We keep this disabled because we use or shim to get around DOM_EXCEPTION_ERROR_16
    String databasePath = webView.getContext().getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
    settings.setDatabaseEnabled(true);
    settings.setDatabasePath(databasePath);


    //Determine whether we're in debug or release mode, and turn on Debugging!
    ApplicationInfo appInfo = webView.getContext().getApplicationContext().getApplicationInfo();
    if ((appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
        enableRemoteDebugging();
    }

    settings.setGeolocationDatabasePath(databasePath);

    // Enable DOM storage
    settings.setDomStorageEnabled(true);

    // Enable built-in geolocation
    settings.setGeolocationEnabled(true);

    // Enable AppCache
    // Fix for CB-2282
    settings.setAppCacheMaxSize(5 * 1048576);
    settings.setAppCachePath(databasePath);
    settings.setAppCacheEnabled(true);

    // Fix for CB-1405
    // Google issue 4641
    String defaultUserAgent = settings.getUserAgentString();

    // Fix for CB-3360
    String overrideUserAgent = preferences.getString("OverrideUserAgent", null);
    if (overrideUserAgent != null) {
        settings.setUserAgentString(overrideUserAgent);
    } else {
        String appendUserAgent = preferences.getString("AppendUserAgent", null);
        if (appendUserAgent != null) {
            settings.setUserAgentString(defaultUserAgent + " " + appendUserAgent);
        }
    }
    // End CB-3360

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
    if (this.receiver == null) {
        this.receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                settings.getUserAgentString();
            }
        };
        webView.getContext().registerReceiver(this.receiver, intentFilter);
    }
    // end CB-1405
}
 
Example 15
Source File: SystemWebViewEngine.java    From lona with GNU General Public License v3.0 4 votes vote down vote up
@SuppressLint({"NewApi", "SetJavaScriptEnabled"})
@SuppressWarnings("deprecation")
private void initWebViewSettings() {
    webView.setInitialScale(0);
    webView.setVerticalScrollBarEnabled(false);
    // Enable JavaScript
    final WebSettings settings = webView.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setJavaScriptCanOpenWindowsAutomatically(true);
    settings.setLayoutAlgorithm(LayoutAlgorithm.NORMAL);

    String manufacturer = android.os.Build.MANUFACTURER;
    LOG.d(TAG, "CordovaWebView is running on device made by: " + manufacturer);

    //We don't save any form data in the application
    settings.setSaveFormData(false);
    settings.setSavePassword(false);

    // Jellybean rightfully tried to lock this down. Too bad they didn't give us a whitelist
    // while we do this
    settings.setAllowUniversalAccessFromFileURLs(true);
    settings.setMediaPlaybackRequiresUserGesture(false);

    // Enable database
    // We keep this disabled because we use or shim to get around DOM_EXCEPTION_ERROR_16
    String databasePath = webView.getContext().getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
    settings.setDatabaseEnabled(true);
    settings.setDatabasePath(databasePath);


    //Determine whether we're in debug or release mode, and turn on Debugging!
    ApplicationInfo appInfo = webView.getContext().getApplicationContext().getApplicationInfo();
    if ((appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
        enableRemoteDebugging();
    }

    settings.setGeolocationDatabasePath(databasePath);

    // Enable DOM storage
    settings.setDomStorageEnabled(true);

    // Enable built-in geolocation
    settings.setGeolocationEnabled(true);

    // Enable AppCache
    // Fix for CB-2282
    settings.setAppCacheMaxSize(5 * 1048576);
    settings.setAppCachePath(databasePath);
    settings.setAppCacheEnabled(true);

    // Fix for CB-1405
    // Google issue 4641
    String defaultUserAgent = settings.getUserAgentString();

    // Fix for CB-3360
    String overrideUserAgent = preferences.getString("OverrideUserAgent", null);
    if (overrideUserAgent != null) {
        settings.setUserAgentString(overrideUserAgent);
    } else {
        String appendUserAgent = preferences.getString("AppendUserAgent", null);
        if (appendUserAgent != null) {
            settings.setUserAgentString(defaultUserAgent + " " + appendUserAgent);
        }
    }
    // End CB-3360

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
    if (this.receiver == null) {
        this.receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                settings.getUserAgentString();
            }
        };
        webView.getContext().registerReceiver(this.receiver, intentFilter);
    }
    // end CB-1405
}
 
Example 16
Source File: SystemWebViewEngine.java    From pychat with MIT License 4 votes vote down vote up
@SuppressLint({"NewApi", "SetJavaScriptEnabled"})
@SuppressWarnings("deprecation")
private void initWebViewSettings() {
    webView.setInitialScale(0);
    webView.setVerticalScrollBarEnabled(false);
    // Enable JavaScript
    final WebSettings settings = webView.getSettings();
    settings.setJavaScriptEnabled(true);
    settings.setJavaScriptCanOpenWindowsAutomatically(true);
    settings.setLayoutAlgorithm(LayoutAlgorithm.NORMAL);

    String manufacturer = android.os.Build.MANUFACTURER;
    LOG.d(TAG, "CordovaWebView is running on device made by: " + manufacturer);

    //We don't save any form data in the application
    settings.setSaveFormData(false);
    settings.setSavePassword(false);

    // Jellybean rightfully tried to lock this down. Too bad they didn't give us a whitelist
    // while we do this
    settings.setAllowUniversalAccessFromFileURLs(true);
    settings.setMediaPlaybackRequiresUserGesture(false);

    // Enable database
    // We keep this disabled because we use or shim to get around DOM_EXCEPTION_ERROR_16
    String databasePath = webView.getContext().getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
    settings.setDatabaseEnabled(true);
    settings.setDatabasePath(databasePath);


    //Determine whether we're in debug or release mode, and turn on Debugging!
    ApplicationInfo appInfo = webView.getContext().getApplicationContext().getApplicationInfo();
    if ((appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) {
        enableRemoteDebugging();
    }

    settings.setGeolocationDatabasePath(databasePath);

    // Enable DOM storage
    settings.setDomStorageEnabled(true);

    // Enable built-in geolocation
    settings.setGeolocationEnabled(true);

    // Enable AppCache
    // Fix for CB-2282
    settings.setAppCacheMaxSize(5 * 1048576);
    settings.setAppCachePath(databasePath);
    settings.setAppCacheEnabled(true);

    // Fix for CB-1405
    // Google issue 4641
    String defaultUserAgent = settings.getUserAgentString();

    // Fix for CB-3360
    String overrideUserAgent = preferences.getString("OverrideUserAgent", null);
    if (overrideUserAgent != null) {
        settings.setUserAgentString(overrideUserAgent);
    } else {
        String appendUserAgent = preferences.getString("AppendUserAgent", null);
        if (appendUserAgent != null) {
            settings.setUserAgentString(defaultUserAgent + " " + appendUserAgent);
        }
    }
    // End CB-3360

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED);
    if (this.receiver == null) {
        this.receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                settings.getUserAgentString();
            }
        };
        webView.getContext().registerReceiver(this.receiver, intentFilter);
    }
    // end CB-1405
}
 
Example 17
Source File: BaseFragmentActivity.java    From android-project-wo2b with Apache License 2.0 4 votes vote down vote up
/**
 * 系统加载
 */
protected void systemLoader(Context context)
{
	if (RockySdk.getInstance().getContext() != null)
	{
		return;
	}
	
	Context application = context.getApplicationContext();
	
	// 用于获取浏览器代理
	WebView webview = new WebView(context);
	webview.layout(0, 0, 0, 0);
	WebSettings webSettings = webview.getSettings();
	
	ApplicationInfo applicationInfo = context.getApplicationInfo();
	
	VersionInfo versionInfo = ManifestTools.getVersionInfo(application);
	String userAgent = webSettings.getUserAgentString();
	
	// Structure the ClientInfo.
	ClientInfo clientInfo = new ClientInfo(application.getPackageName());
	//clientInfo.setAppicon(R.drawable.ic_launcher);
	clientInfo.setAppicon(applicationInfo.icon);
	clientInfo.setAppname(ManifestTools.getApplicationLable(application));
	clientInfo.setDeviceType(SdkConfig.Device.PHONE);
	clientInfo.setDeviceName(android.os.Build.MODEL);
	clientInfo.setAlias(android.os.Build.MODEL);
	clientInfo.setSdkVersion(android.os.Build.VERSION.SDK_INT);
	clientInfo.setMac(DeviceInfoManager.getMacAddress(application));
	
	// Webkit user-agent
	clientInfo.setUserAgent(userAgent);
	
	if (versionInfo != null)
	{
		clientInfo.setVersionCode(versionInfo.getVersionCode());
		clientInfo.setVersionName(versionInfo.getVersionName());
	}
	
	// FIXME: Take attention...
	clientInfo.addFlags(ClientInfo.FLAG_DEBUG | ClientInfo.FLAG_RELEASE);
	// clientContext.addFlags(ClientContext.FLAG_DEBUG);
	
	// TODO: 广告
	RockyConfig config = new RockyConfig.Builder(application)
		.clientInfo(clientInfo)
		.hasAdBanner(false)		// 显示积分Banner
		.hasAdPointsWall(true)	// 显示积分墙
		.build();
	
	RockySdk.getInstance().init(config);
}
 
Example 18
Source File: Global.java    From android-project-wo2b with Apache License 2.0 4 votes vote down vote up
/**
 * 系统初始化
 * 
 * @param context
 */
public static void init(Context context)
{
	if (RockySdk.getInstance().getContext() != null)
	{
		return;
	}

	Context application = context.getApplicationContext();
	
	// 用于获取浏览器代理
	WebView webview = new WebView(context);
	webview.layout(0, 0, 0, 0);
	WebSettings webSettings = webview.getSettings();
	
	ApplicationInfo applicationInfo = context.getApplicationInfo();
	
	VersionInfo versionInfo = ManifestTools.getVersionInfo(application);
	String userAgent = webSettings.getUserAgentString();
	
	// Structure the ClientInfo.
	ClientInfo clientInfo = new ClientInfo(application.getPackageName());
	//clientInfo.setAppicon(R.drawable.ic_launcher);
	clientInfo.setAppicon(applicationInfo.icon);
	clientInfo.setAppname(ManifestTools.getApplicationLable(application));
	clientInfo.setDeviceType(SdkConfig.Device.PHONE);
	clientInfo.setDeviceName(android.os.Build.MODEL);
	clientInfo.setAlias(android.os.Build.MODEL);
	clientInfo.setSdkVersion(android.os.Build.VERSION.SDK_INT);
	clientInfo.setMac(DeviceInfoManager.getMacAddress(application));
	
	// Webkit user-agent
	clientInfo.setUserAgent(userAgent);
	
	if (versionInfo != null)
	{
		clientInfo.setVersionCode(versionInfo.getVersionCode());
		clientInfo.setVersionName(versionInfo.getVersionName());
	}
	
	// FIXME: Take attention...
	clientInfo.addFlags(ClientInfo.FLAG_DEBUG | ClientInfo.FLAG_RELEASE);
	// clientContext.addFlags(ClientContext.FLAG_DEBUG);
	
	// TODO: 广告
	RockyConfig config = new RockyConfig.Builder(application)
		.clientInfo(clientInfo)
		.hasAdBanner(false)		// 显示积分Banner
		.hasAdPointsWall(true)	// 显示积分墙
		.build();
	
	RockySdk.getInstance().init(config);
}
 
Example 19
Source File: WebViewAssist.java    From DevUtils with Apache License 2.0 4 votes vote down vote up
/**
 * 获取浏览器标识 UA
 * @return 浏览器标识 UA
 */
public String getUserAgentString() {
    WebSettings webSettings = getSettings();
    return (webSettings != null) ? webSettings.getUserAgentString() : null;
}
 
Example 20
Source File: WebSettingsCompat.java    From Android_Skin_2.0 with Apache License 2.0 4 votes vote down vote up
@Override
public String getUserAgentString(WebSettings settings) {
	return settings.getUserAgentString();
}