List of usage examples for android.webkit ValueCallback ValueCallback
ValueCallback
From source file:com.ronnywu.browserview.client.Lv99JavascriptBridgeWebViewClient.java
/** * JS ./*from www . j a va2 s. c o m*/ * * @param script JS . * @param redAgateJSCallback JS . */ public void executeJavascript(String script, final JsCallback redAgateJSCallback) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { webView.evaluateJavascript(script, new ValueCallback<String>() { @Override public void onReceiveValue(String value) { if (redAgateJSCallback != null) { if (value != null && value.startsWith("\"") && value.endsWith("\"")) { value = value.substring(1, value.length() - 1).replaceAll("\\\\", ""); } redAgateJSCallback.onReceiveValue(value); } } }); } else { if (redAgateJSCallback != null) { jsCallHandler.addCallback(++uniqueId + "", redAgateJSCallback); webView.loadUrl( "javascript:window." + kInterface + ".onResultForScript(" + uniqueId + "," + script + ")"); } else { webView.loadUrl("javascript:" + script); } } }
From source file:im.ene.lab.attiq.ui.activities.ItemDetailActivity.java
private void trySetupContentView() { mContentView.setVerticalScrollBarEnabled(false); mContentView.setHorizontalScrollBarEnabled(false); mContentView.getSettings().setJavaScriptEnabled(true); mContentView.setWebChromeClient(new WebChromeClient() { @Override// w w w . j av a 2 s.c o m public void onProgressChanged(WebView view, int newProgress) { super.onProgressChanged(view, newProgress); Log.d(TAG, "newProgress = [" + newProgress + "]"); } }); mContentView.addJavascriptInterface(this, "Attiq"); mContentView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if (url != null && (url.startsWith("http://") || url.startsWith("https://"))) { startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url))); return true; } else { return false; } } @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); if (!mIsFirstTimeLoaded && mLoadingView != null) { mLoadingView.setAlpha(1.f); mLoadingView.setVisibility(View.VISIBLE); } } @Override public void onPageFinished(final WebView view, String url) { super.onPageFinished(view, url); if (PrefUtil.isMathJaxEnabled()) { view.evaluateJavascript("javascript:document.getElementById('content').innerHTML='" + doubleEscapeTeX(mArticle.getRenderedBody()) + "';", null); view.evaluateJavascript("javascript:MathJax.Hub.Queue(['Typeset',MathJax.Hub]);", new ValueCallback<String>() { @Override public void onReceiveValue(String value) { view.loadUrl("javascript:(function () " + "{document.getElementsByTagName('body')[0].style.marginBottom = '0'})()"); } }); } mIsFirstTimeLoaded = true; if (mLoadingView != null) { ViewCompat.animate(mLoadingView).alpha(0.f).setDuration(300) .setListener(new ViewPropertyAnimatorListenerAdapter() { @Override public void onAnimationEnd(View view) { if (mLoadingView != null) { mLoadingView.setVisibility(View.GONE); } } }).start(); } } }); mContentView.setFindListener(new WebView.FindListener() { @Override public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches, boolean isDoneCounting) { if (mMenuLayout != null) { mMenuLayout.closeDrawer(GravityCompat.END); } if (numberOfMatches > 0 && mMenuAnchor != null && mContentView != null) { // FIXME Doesn't work now, because WebView is staying inside ScrollView mContentView.loadUrl("javascript:scrollToElement(\"" + mMenuAnchor.text() + "\");"); } } }); }
From source file:android.webkit.cts.WebViewTest.java
@UiThreadTest public void testAddJavascriptInterface() throws Exception { if (!NullWebViewUtils.isWebViewAvailable()) { return;/*from ww w . ja va 2 s. c o m*/ } WebSettings settings = mWebView.getSettings(); settings.setJavaScriptEnabled(true); settings.setJavaScriptCanOpenWindowsAutomatically(true); final class DummyJavaScriptInterface { private boolean mWasProvideResultCalled; private String mResult; private synchronized String waitForResult() { while (!mWasProvideResultCalled) { try { wait(TEST_TIMEOUT); } catch (InterruptedException e) { continue; } if (!mWasProvideResultCalled) { Assert.fail("Unexpected timeout"); } } return mResult; } public synchronized boolean wasProvideResultCalled() { return mWasProvideResultCalled; } @JavascriptInterface public synchronized void provideResult(String result) { mWasProvideResultCalled = true; mResult = result; notify(); } } final DummyJavaScriptInterface obj = new DummyJavaScriptInterface(); mWebView.addJavascriptInterface(obj, "dummy"); assertFalse(obj.wasProvideResultCalled()); startWebServer(false); String url = mWebServer.getAssetUrl(TestHtmlConstants.ADD_JAVA_SCRIPT_INTERFACE_URL); mOnUiThread.loadUrlAndWaitForCompletion(url); assertEquals("Original title", obj.waitForResult()); // Verify that only methods annotated with @JavascriptInterface are exposed // on the JavaScript interface object. mOnUiThread.evaluateJavascript("typeof dummy.provideResult", new ValueCallback<String>() { @Override public void onReceiveValue(String result) { assertEquals("\"function\"", result); } }); mOnUiThread.evaluateJavascript("typeof dummy.wasProvideResultCalled", new ValueCallback<String>() { @Override public void onReceiveValue(String result) { assertEquals("\"undefined\"", result); } }); mOnUiThread.evaluateJavascript("typeof dummy.getClass", new ValueCallback<String>() { @Override public void onReceiveValue(String result) { assertEquals("\"undefined\"", result); } }); }
From source file:android.webkit.cts.WebViewTest.java
public void testAddJavascriptInterfaceExceptions() throws Exception { WebSettings settings = mOnUiThread.getSettings(); settings.setJavaScriptEnabled(true); settings.setJavaScriptCanOpenWindowsAutomatically(true); final AtomicBoolean mJsInterfaceWasCalled = new AtomicBoolean(false) { @JavascriptInterface// www. j av a2 s .com public synchronized void call() { set(true); // The main purpose of this test is to ensure an exception here does not // crash the implementation. throw new RuntimeException("Javascript Interface exception"); } }; mOnUiThread.addJavascriptInterface(mJsInterfaceWasCalled, "dummy"); mOnUiThread.loadUrlAndWaitForCompletion("about:blank"); assertFalse(mJsInterfaceWasCalled.get()); final CountDownLatch resultLatch = new CountDownLatch(1); mOnUiThread.evaluateJavascript("try {dummy.call(); 'fail'; } catch (exception) { 'pass'; } ", new ValueCallback<String>() { @Override public void onReceiveValue(String result) { assertEquals("\"pass\"", result); resultLatch.countDown(); } }); assertTrue(resultLatch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS)); assertTrue(mJsInterfaceWasCalled.get()); }
From source file:android.webkit.cts.WebViewTest.java
private void doSaveWebArchive(String baseName, boolean autoName, final String expectName) throws Throwable { final Semaphore saving = new Semaphore(0); ValueCallback<String> callback = new ValueCallback<String>() { @Override/* w w w . j a v a 2 s .c o m*/ public void onReceiveValue(String savedName) { assertEquals(expectName, savedName); saving.release(); } }; mOnUiThread.saveWebArchive(baseName, autoName, callback); assertTrue(saving.tryAcquire(TEST_TIMEOUT, TimeUnit.MILLISECONDS)); }
From source file:com.mario22gmail.license.nfc_project.NavigationDrawerActivity.java
public void OpenFacebook(String userName, String password) { WebView mWebview = (WebView) findViewById(R.id.webViewFb); String url = "https://www.facebook.com"; js = "javascript:document.getElementsByName('email')[0].value = '" + userName + "';document.getElementsByName('pass')[0].value='" + password + "';document.getElementsByName('login')[0].click();"; mWebview.loadUrl(url);/* w ww.j a v a2 s. c o m*/ WebSettings settings = mWebview.getSettings(); settings.setJavaScriptEnabled(true); mWebview.setWebViewClient(new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); if (Build.VERSION.SDK_INT >= 19) { view.evaluateJavascript(js, new ValueCallback<String>() { @Override public void onReceiveValue(String s) { } }); } else { view.loadUrl(js); } } }); }
From source file:com.codename1.impl.android.AndroidImplementation.java
/** * Executes javascript and returns a string result where appropriate. * @param browserPeer//w w w. j av a2 s.c o m * @param javaScript * @return */ @Override public String browserExecuteAndReturnString(final PeerComponent browserPeer, final String javaScript) { final AndroidImplementation.AndroidBrowserComponent bc = (AndroidImplementation.AndroidBrowserComponent) browserPeer; final String[] result = new String[1]; final boolean[] complete = new boolean[1]; execJSSafe(bc, javaScript, new ValueCallback<String>() { @Override public void onReceiveValue(String value) { synchronized (result) { complete[0] = true; result[0] = value; result.notify(); } } }); synchronized (result) { if (!complete[0]) { Util.wait(result, 10000); } } if (result[0] == null) { return null; } else { org.json.JSONTokener tok = new org.json.JSONTokener("{\"result\":" + result[0] + "}"); try { JSONObject jso = new JSONObject(tok); return jso.getString("result"); } catch (Throwable ex) { com.codename1.io.Log.e(ex); return null; } } }