From bdaa1aafd590aa88e16cee166f8e78404d8023a4 Mon Sep 17 00:00:00 2001 From: Mathew Inwood Date: Mon, 4 Jul 2011 15:15:27 +0100 Subject: SearchBox API to determine if it's supported by the current page. Change-Id: I0119243ed0e19e237c1f51de887af5c954f96693 --- core/java/android/webkit/CallbackProxy.java | 16 +++++++++++++++- core/java/android/webkit/SearchBox.java | 9 +++++++++ core/java/android/webkit/SearchBoxImpl.java | 29 +++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) diff --git a/core/java/android/webkit/CallbackProxy.java b/core/java/android/webkit/CallbackProxy.java index f7d55f6f00af..0294e3f81600 100644 --- a/core/java/android/webkit/CallbackProxy.java +++ b/core/java/android/webkit/CallbackProxy.java @@ -119,6 +119,7 @@ class CallbackProxy extends Handler { private static final int NOTIFY_SEARCHBOX_LISTENERS = 139; private static final int AUTO_LOGIN = 140; private static final int CLIENT_CERT_REQUEST = 141; + private static final int SEARCHBOX_IS_SUPPORTED_CALLBACK = 142; // Message triggered by the client to resume execution private static final int NOTIFY = 200; @@ -796,13 +797,14 @@ class CallbackProxy extends Handler { mWebChromeClient.setInstallableWebApp(); } break; - case NOTIFY_SEARCHBOX_LISTENERS: + case NOTIFY_SEARCHBOX_LISTENERS: { SearchBoxImpl searchBox = (SearchBoxImpl) mWebView.getSearchBox(); @SuppressWarnings("unchecked") List suggestions = (List) msg.obj; searchBox.handleSuggestions(msg.getData().getString("query"), suggestions); break; + } case AUTO_LOGIN: { if (mWebViewClient != null) { String realm = msg.getData().getString("realm"); @@ -813,6 +815,12 @@ class CallbackProxy extends Handler { } break; } + case SEARCHBOX_IS_SUPPORTED_CALLBACK: { + SearchBoxImpl searchBox = (SearchBoxImpl) mWebView.getSearchBox(); + Boolean supported = (Boolean) msg.obj; + searchBox.handleIsSupportedCallback(supported); + break; + } } } @@ -1627,4 +1635,10 @@ class CallbackProxy extends Handler { sendMessage(msg); } + + void onIsSupportedCallback(boolean isSupported) { + Message msg = obtainMessage(SEARCHBOX_IS_SUPPORTED_CALLBACK); + msg.obj = new Boolean(isSupported); + sendMessage(msg); + } } diff --git a/core/java/android/webkit/SearchBox.java b/core/java/android/webkit/SearchBox.java index 57c7b035efad..5075302fb936 100644 --- a/core/java/android/webkit/SearchBox.java +++ b/core/java/android/webkit/SearchBox.java @@ -82,6 +82,11 @@ public interface SearchBox { void addSearchBoxListener(SearchBoxListener l); void removeSearchBoxListener(SearchBoxListener l); + /** + * Indicates if the searchbox API is supported in the current page. + */ + void isSupported(IsSupportedCallback callback); + /** * Listeners (if any) will be called on the thread that created the * webview. @@ -89,4 +94,8 @@ public interface SearchBox { interface SearchBoxListener { void onSuggestionsReceived(String query, List suggestions); } + + interface IsSupportedCallback { + void searchBoxIsSupported(boolean supported); + } } diff --git a/core/java/android/webkit/SearchBoxImpl.java b/core/java/android/webkit/SearchBoxImpl.java index 480f5d7a5b44..61fb2ce9f160 100644 --- a/core/java/android/webkit/SearchBoxImpl.java +++ b/core/java/android/webkit/SearchBoxImpl.java @@ -92,9 +92,19 @@ final class SearchBoxImpl implements SearchBox { = "if (window.chrome && window.chrome.searchBox &&" + " window.chrome.searchBox.on%1$s) { window.chrome.searchBox.on%1$s(); }"; + private static final String IS_SUPPORTED_SCRIPT + = "if (window.searchBoxJavaBridge_) {" + + " if (window.chrome && window.chrome.searchBox && " + + " window.chrome.searchBox.onsubmit) {" + + " window.searchBoxJavaBridge_.isSupportedCallback(true);" + + " } else {" + + " window.searchBoxJavaBridge_.isSupportedCallback(false);" + + " }}"; + private final List mListeners; private final WebViewCore mWebViewCore; private final CallbackProxy mCallbackProxy; + private IsSupportedCallback mSupportedCallback; SearchBoxImpl(WebViewCore webViewCore, CallbackProxy callbackProxy) { mListeners = new ArrayList(); @@ -173,6 +183,25 @@ final class SearchBoxImpl implements SearchBox { } } + @Override + public void isSupported(IsSupportedCallback callback) { + mSupportedCallback = callback; + dispatchJs(IS_SUPPORTED_SCRIPT); + } + + // Called by Javascript through the Java bridge. + public void isSupportedCallback(boolean isSupported) { + mCallbackProxy.onIsSupportedCallback(isSupported); + } + + public void handleIsSupportedCallback(boolean isSupported) { + IsSupportedCallback callback = mSupportedCallback; + mSupportedCallback = null; + if (callback != null) { + callback.searchBoxIsSupported(isSupported); + } + } + // This is used as a hackish alternative to javascript escaping. // There appears to be no such functionality in the core framework. private String jsonSerialize(String query) { -- cgit v1.2.3-59-g8ed1b