From abeb6a791501151308d06db6aebb438e16c1a784 Mon Sep 17 00:00:00 2001 From: Victoria Lease Date: Mon, 5 Mar 2012 16:29:12 -0800 Subject: [PATCH] proposed public API for asynchronous find-on-page Bug: 6052412 Change-Id: I63bff3bfda50eede958cb885f5068ae94bdcfe7d --- .../webkit/FindActionModeCallback.java | 11 +++---- core/java/android/webkit/FindListener.java | 32 +++++++++++++++++++ core/java/android/webkit/WebView.java | 29 +++++++++++++++-- core/java/android/webkit/WebViewClassic.java | 24 +++++++++++--- core/java/android/webkit/WebViewProvider.java | 4 +++ 5 files changed, 87 insertions(+), 13 deletions(-) create mode 100644 core/java/android/webkit/FindListener.java diff --git a/core/java/android/webkit/FindActionModeCallback.java b/core/java/android/webkit/FindActionModeCallback.java index 964cf3ecc316e..6c331ac87aa66 100644 --- a/core/java/android/webkit/FindActionModeCallback.java +++ b/core/java/android/webkit/FindActionModeCallback.java @@ -45,7 +45,6 @@ class FindActionModeCallback implements ActionMode.Callback, TextWatcher, private int mNumberOfMatches; private int mActiveMatchIndex; private ActionMode mActionMode; - private String mLastFind; FindActionModeCallback(Context context) { mCustomView = LayoutInflater.from(context).inflate( @@ -134,13 +133,12 @@ class FindActionModeCallback implements ActionMode.Callback, TextWatcher, mWebView.clearMatches(); mMatches.setVisibility(View.GONE); mMatchesFound = false; - mLastFind = null; + mWebView.findAll(null); } else { mMatchesFound = true; mMatches.setVisibility(View.INVISIBLE); mNumberOfMatches = 0; - mLastFind = find.toString(); - mWebView.findAllAsync(mLastFind); + mWebView.findAllAsync(find.toString()); } } @@ -150,9 +148,8 @@ class FindActionModeCallback implements ActionMode.Callback, TextWatcher, mInput.showSoftInput(mEditText, 0); } - public void updateMatchCount(int matchIndex, int matchCount, - String findText) { - if (mLastFind != null && mLastFind.equals(findText)) { + public void updateMatchCount(int matchIndex, int matchCount, boolean isNewFind) { + if (!isNewFind) { mNumberOfMatches = matchCount; mActiveMatchIndex = matchIndex; updateMatchesString(); diff --git a/core/java/android/webkit/FindListener.java b/core/java/android/webkit/FindListener.java new file mode 100644 index 0000000000000..124f73719a0aa --- /dev/null +++ b/core/java/android/webkit/FindListener.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.webkit; + +/** + * @hide + */ +public interface FindListener { + /** + * Notify the host application that a find result is available. + * + * @param numberOfMatches How many matches have been found + * @param activeMatchOrdinal The ordinal of the currently selected match + * @param isDoneCounting Whether we have finished counting matches + */ + public void onFindResultReceived(int numberOfMatches, + int activeMatchOrdinal, boolean isDoneCounting); +} diff --git a/core/java/android/webkit/WebView.java b/core/java/android/webkit/WebView.java index a561577f576e2..5e094161a8480 100644 --- a/core/java/android/webkit/WebView.java +++ b/core/java/android/webkit/WebView.java @@ -1226,7 +1226,19 @@ public class WebView extends AbsoluteLayout } - /* + /** + * Register the interface to be used when a find-on-page result has become + * available. This will replace the current handler. + * + * @param listener An implementation of FindListener + * @hide + */ + public void setFindListener(FindListener listener) { + checkThread(); + mProvider.setFindListener(listener); + } + + /** * Highlight and scroll to the next occurance of String in findAll. * Wraps the page infinitely, and scrolls. Must be called after * calling findAll. @@ -1238,8 +1250,9 @@ public class WebView extends AbsoluteLayout mProvider.findNext(forward); } - /* + /** * Find all instances of find on the page and highlight them. + * * @param find String to find. * @return int The number of occurances of the String "find" * that were found. @@ -1249,6 +1262,18 @@ public class WebView extends AbsoluteLayout return mProvider.findAll(find); } + /** + * Find all instances of find on the page and highlight them, + * asynchronously. + * + * @param find String to find. + * @hide + */ + public void findAllAsync(String find) { + checkThread(); + mProvider.findAllAsync(find); + } + /** * Start an ActionMode for finding text in this WebView. Only works if this * WebView is attached to the view system. diff --git a/core/java/android/webkit/WebViewClassic.java b/core/java/android/webkit/WebViewClassic.java index ed43043ff8f01..064fda3f33966 100644 --- a/core/java/android/webkit/WebViewClassic.java +++ b/core/java/android/webkit/WebViewClassic.java @@ -1440,6 +1440,9 @@ public final class WebViewClassic implements WebViewProvider, WebViewProvider.Sc // Used to notify listeners of a new picture. private PictureListener mPictureListener; + // Used to notify listeners about find-on-page results. + private FindListener mFindListener; + /** * Refer to {@link WebView#requestFocusNodeHref(Message)} for more information */ @@ -3694,6 +3697,17 @@ public final class WebViewClassic implements WebViewProvider, WebViewProvider.Sc return mCallbackProxy.getBackForwardList().clone(); } + /** + * Register the interface to be used when a find-on-page result has become + * available. This will replace the current handler. + * + * @param listener An implementation of FindListener + */ + public void setFindListener(FindListener listener) { + checkThread(); + mFindListener = listener; + } + /** * See {@link WebView#findNext(boolean)} */ @@ -3723,6 +3737,7 @@ public final class WebViewClassic implements WebViewProvider, WebViewProvider.Sc checkThread(); if (0 == mNativeClass) return 0; // client isn't initialized mLastFind = find; + if (find == null) return 0; mWebViewCore.removeMessages(EventHub.FIND_ALL); WebViewCore.FindAllRequest request = new WebViewCore.FindAllRequest(find); @@ -8478,10 +8493,11 @@ public final class WebViewClassic implements WebViewProvider, WebViewProvider.Sc } case UPDATE_MATCH_COUNT: { - if (mFindCallback != null) { - mFindCallback.updateMatchCount(msg.arg1, msg.arg2, - (String) msg.obj); - } + boolean isNewFind = mLastFind == null || !mLastFind.equals(msg.obj); + if (mFindCallback != null) + mFindCallback.updateMatchCount(msg.arg1, msg.arg2, isNewFind); + if (mFindListener != null) + mFindListener.onFindResultReceived(msg.arg1, msg.arg2, true); break; } case CLEAR_CARET_HANDLE: diff --git a/core/java/android/webkit/WebViewProvider.java b/core/java/android/webkit/WebViewProvider.java index 2e8ad6d41db50..9016fbceeac2e 100644 --- a/core/java/android/webkit/WebViewProvider.java +++ b/core/java/android/webkit/WebViewProvider.java @@ -191,10 +191,14 @@ public interface WebViewProvider { public WebBackForwardList copyBackForwardList(); + public void setFindListener(FindListener listener); + public void findNext(boolean forward); public int findAll(String find); + public void findAllAsync(String find); + public boolean showFindDialog(String text, boolean showIme); public void clearMatches();