List of usage examples for android.webkit WebView setNetworkAvailable
public void setNetworkAvailable(boolean networkUp)
From source file:bolts.WebViewAppLinkResolver.java
@Override public Task<AppLink> getAppLinkFromUrlInBackground(final Uri url) { final Capture<String> content = new Capture<String>(); final Capture<String> contentType = new Capture<String>(); return Task.callInBackground(new Callable<Void>() { @Override/*from www . j a va2 s .co m*/ public Void call() throws Exception { URL currentURL = new URL(url.toString()); URLConnection connection = null; while (currentURL != null) { // Fetch the content at the given URL. connection = currentURL.openConnection(); if (connection instanceof HttpURLConnection) { // Unfortunately, this doesn't actually follow redirects if they go from http->https, // so we have to do that manually. ((HttpURLConnection) connection).setInstanceFollowRedirects(true); } connection.setRequestProperty(PREFER_HEADER, META_TAG_PREFIX); connection.connect(); if (connection instanceof HttpURLConnection) { HttpURLConnection httpConnection = (HttpURLConnection) connection; if (httpConnection.getResponseCode() >= 300 && httpConnection.getResponseCode() < 400) { currentURL = new URL(httpConnection.getHeaderField("Location")); httpConnection.disconnect(); } else { currentURL = null; } } else { currentURL = null; } } try { content.set(readFromConnection(connection)); contentType.set(connection.getContentType()); } finally { if (connection instanceof HttpURLConnection) { ((HttpURLConnection) connection).disconnect(); } } return null; } }).onSuccessTask(new Continuation<Void, Task<JSONArray>>() { @Override public Task<JSONArray> then(Task<Void> task) throws Exception { // Load the content in a WebView and use JavaScript to extract the meta tags. final TaskCompletionSource<JSONArray> tcs = new TaskCompletionSource<>(); final WebView webView = new WebView(context); webView.getSettings().setJavaScriptEnabled(true); webView.setNetworkAvailable(false); webView.setWebViewClient(new WebViewClient() { private boolean loaded = false; private void runJavaScript(WebView view) { if (!loaded) { // After the first resource has been loaded (which will be the pre-populated data) // run the JavaScript meta tag extraction script loaded = true; view.loadUrl(TAG_EXTRACTION_JAVASCRIPT); } } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); runJavaScript(view); } @Override public void onLoadResource(WebView view, String url) { super.onLoadResource(view, url); runJavaScript(view); } }); // Inject an object that will receive the JSON for the extracted JavaScript tags webView.addJavascriptInterface(new Object() { @JavascriptInterface public void setValue(String value) { try { tcs.trySetResult(new JSONArray(value)); } catch (JSONException e) { tcs.trySetError(e); } } }, "boltsWebViewAppLinkResolverResult"); String inferredContentType = null; if (contentType.get() != null) { inferredContentType = contentType.get().split(";")[0]; } webView.loadDataWithBaseURL(url.toString(), content.get(), inferredContentType, null, null); return tcs.getTask(); } }, Task.UI_THREAD_EXECUTOR).onSuccess(new Continuation<JSONArray, AppLink>() { @Override public AppLink then(Task<JSONArray> task) throws Exception { Map<String, Object> alData = parseAlData(task.getResult()); AppLink appLink = makeAppLinkFromAlData(alData, url); return appLink; } }); }
From source file:org.openqa.selendroid.server.model.SelendroidWebDriver.java
private void configureWebView(final WebView view) { ServerInstrumentation.getInstance().runOnUiThread(new Runnable() { @Override/*from ww w. ja va 2 s . co m*/ public void run() { view.clearCache(true); view.clearFormData(); view.clearHistory(); view.setFocusable(true); view.setFocusableInTouchMode(true); view.setNetworkAvailable(true); view.setWebChromeClient(new MyWebChromeClient()); WebSettings settings = view.getSettings(); settings.setJavaScriptCanOpenWindowsAutomatically(true); settings.setSupportMultipleWindows(true); settings.setBuiltInZoomControls(true); settings.setJavaScriptEnabled(true); settings.setAppCacheEnabled(true); settings.setAppCacheMaxSize(10 * 1024 * 1024); settings.setAppCachePath(""); settings.setDatabaseEnabled(true); settings.setDomStorageEnabled(true); settings.setGeolocationEnabled(true); settings.setSaveFormData(false); settings.setSavePassword(false); settings.setRenderPriority(WebSettings.RenderPriority.HIGH); // Flash settings settings.setPluginState(WebSettings.PluginState.ON); // Geo location settings settings.setGeolocationEnabled(true); settings.setGeolocationDatabasePath("/data/data/selendroid"); } }); }
From source file:io.selendroid.server.model.SelendroidWebDriver.java
private void configureWebView(final WebView view) { ServerInstrumentation.getInstance().getCurrentActivity().runOnUiThread(new Runnable() { @Override/*w ww . j av a 2 s . c om*/ public void run() { try { view.clearCache(true); view.clearFormData(); view.clearHistory(); view.setFocusable(true); view.setFocusableInTouchMode(true); view.setNetworkAvailable(true); // need to check the class name rather than checking instanceof // since when it is not an instanceof, it likely means the app under test // does not contain the Cordova project and this will cause a RuntimeException if (view.getClass().getSimpleName().equalsIgnoreCase("CordovaWebView")) { CordovaWebView webview = (CordovaWebView) view; CordovaInterface ci = null; chromeClient = new ExtendedCordovaChromeClient(null, webview); } else { chromeClient = new SelendroidWebChromeClient(); } view.setWebChromeClient(chromeClient); WebSettings settings = view.getSettings(); settings.setJavaScriptCanOpenWindowsAutomatically(true); settings.setSupportMultipleWindows(true); settings.setBuiltInZoomControls(true); settings.setJavaScriptEnabled(true); settings.setAppCacheEnabled(true); settings.setAppCacheMaxSize(10 * 1024 * 1024); settings.setAppCachePath(""); settings.setDatabaseEnabled(true); settings.setDomStorageEnabled(true); settings.setGeolocationEnabled(true); settings.setSaveFormData(false); settings.setSavePassword(false); settings.setRenderPriority(WebSettings.RenderPriority.HIGH); // Flash settings settings.setPluginState(WebSettings.PluginState.ON); // Geo location settings settings.setGeolocationEnabled(true); settings.setGeolocationDatabasePath("/data/data/selendroid"); } catch (Exception e) { SelendroidLogger.error("Error configuring web view", e); } } }); }