private synchronized void initWebSettings() { if (android.os.Build.VERSION.SDK_INT >= 21) WebView.enableSlowWholeDocumentDraw(); 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); } }
public static void initializeSettings(WebSettings settings, Context context) { settings.setJavaScriptEnabled(true); settings.setJavaScriptCanOpenWindowsAutomatically(true); settings.setUserAgentString(settings.getUserAgentString() + DB.USER_AGENT); settings.setSupportMultipleWindows(true); settings.setSaveFormData(false); settings.setSavePassword(false); // 设置为true,系统会弹出AlertDialog确认框 if (API < 18) { settings.setAppCacheMaxSize(Long.MAX_VALUE); } if (API < 17) { settings.setEnableSmoothTransition(true); } if (API < 19) { settings.setDatabasePath(context.getFilesDir().getAbsolutePath() + "/databases"); } settings.setDomStorageEnabled(true); settings.setAppCachePath(context.getCacheDir().toString()); settings.setAppCacheEnabled(true); settings.setCacheMode(WebSettings.LOAD_NO_CACHE); settings.setGeolocationDatabasePath(context.getCacheDir().getAbsolutePath()); settings.setAllowFileAccess(true); settings.setDatabaseEnabled(true); settings.setSupportZoom(true); settings.setBuiltInZoomControls(true); settings.setDisplayZoomControls(false); settings.setAllowContentAccess(true); settings.setDefaultTextEncodingName("utf-8"); /*if (API > 16) { settings.setAllowFileAccessFromFileURLs(false); settings.setAllowUniversalAccessFromFileURLs(false); }*/ }
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); }
@SuppressLint("SetJavaScriptEnabled") @SuppressWarnings("deprecation") private void initWebViewSettings() { this.setInitialScale(0); this.setVerticalScrollBarEnabled(false); // TODO: The Activity is the one that should call requestFocus(). if (shouldRequestFocusOnInit()) { this.requestFocusFromTouch(); } this.setInitialScale(0); this.setVerticalScrollBarEnabled(false); if (shouldRequestFocusOnInit()) { this.requestFocusFromTouch(); } // Enable JavaScript final WebSettings settings = this.getSettings(); settings.setJavaScriptEnabled(true); settings.setJavaScriptCanOpenWindowsAutomatically(true); settings.setLayoutAlgorithm(LayoutAlgorithm.NORMAL); // Set the nav dump for HTC 2.x devices (disabling for ICS, deprecated entirely for Jellybean // 4.2) try { Method gingerbread_getMethod = WebSettings.class.getMethod("setNavDump", new Class[] {boolean.class}); String manufacturer = android.os.Build.MANUFACTURER; Log.d(TAG, "CordovaWebView is running on device made by: " + manufacturer); if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB && android.os.Build.MANUFACTURER.contains("HTC")) { gingerbread_getMethod.invoke(settings, true); } } catch (NoSuchMethodException e) { Log.d(TAG, "We are on a modern version of Android, we will deprecate HTC 2.3 devices in 2.8"); } catch (IllegalArgumentException e) { Log.d(TAG, "Doing the NavDump failed with bad arguments"); } catch (IllegalAccessException e) { Log.d( TAG, "This should never happen: IllegalAccessException means this isn't Android anymore"); } catch (InvocationTargetException e) { Log.d( TAG, "This should never happen: InvocationTargetException means this isn't Android anymore."); } // 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 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) { Level16Apis.enableUniversalAccess(settings); } if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) { Level17Apis.setMediaPlaybackRequiresUserGesture(settings, false); } // Enable database // We keep this disabled because we use or shim to get around DOM_EXCEPTION_ERROR_16 String databasePath = 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 = getContext().getApplicationContext().getApplicationInfo(); if ((appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0 && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) { 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 settings.getUserAgentString(); 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(); } }; getContext().registerReceiver(this.receiver, intentFilter); } // end CB-1405 }
private void createAndInitializeWebView() { WebView webview = new WebView(this); WebSettings settings = webview.getSettings(); initializeSettings(settings); Matcher matcher = WEBVIEW_VERSION_PATTERN.matcher(settings.getUserAgentString()); if (matcher.find()) { mWebViewVersion = matcher.group(2); } else { mWebViewVersion = "-"; } setTitle(getResources().getString(R.string.title_activity_browser) + " " + mWebViewVersion); webview.setWebViewClient( new WebViewClient() { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { setUrlBarText(url); } @Override public void onPageFinished(WebView view, String url) { setUrlBarText(url); } @Override public boolean shouldOverrideUrlLoading(WebView webView, String url) { // "about:" and "chrome:" schemes are internal to Chromium; // don't want these to be dispatched to other apps. if (url.startsWith("about:") || url.startsWith("chrome:")) { return false; } return startBrowsingIntent(WebViewBrowserActivity.this, url); } @Override public void onReceivedError( WebView view, int errorCode, String description, String failingUrl) { setUrlFail(true); } }); webview.setWebChromeClient( new WebChromeClient() { @Override public Bitmap getDefaultVideoPoster() { return Bitmap.createBitmap( new int[] {Color.TRANSPARENT}, 1, 1, Bitmap.Config.ARGB_8888); } @Override public void onGeolocationPermissionsShowPrompt( String origin, GeolocationPermissions.Callback callback) { onPermissionRequest(new GeoPermissionRequest(origin, callback)); } @Override public void onPermissionRequest(PermissionRequest request) { WebViewBrowserActivity.this.requestPermissionsForPage(request); } }); mWebView = webview; getContainer() .addView(webview, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); setUrlBarText(""); }