Skip to content

Commit abeb6a7

Browse files
author
Victoria Lease
committed
proposed public API for asynchronous find-on-page
Bug: 6052412 Change-Id: I63bff3bfda50eede958cb885f5068ae94bdcfe7d
1 parent 55ba267 commit abeb6a7

File tree

5 files changed

+87
-13
lines changed

5 files changed

+87
-13
lines changed

core/java/android/webkit/FindActionModeCallback.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ class FindActionModeCallback implements ActionMode.Callback, TextWatcher,
4545
private int mNumberOfMatches;
4646
private int mActiveMatchIndex;
4747
private ActionMode mActionMode;
48-
private String mLastFind;
4948

5049
FindActionModeCallback(Context context) {
5150
mCustomView = LayoutInflater.from(context).inflate(
@@ -134,13 +133,12 @@ void findAll() {
134133
mWebView.clearMatches();
135134
mMatches.setVisibility(View.GONE);
136135
mMatchesFound = false;
137-
mLastFind = null;
136+
mWebView.findAll(null);
138137
} else {
139138
mMatchesFound = true;
140139
mMatches.setVisibility(View.INVISIBLE);
141140
mNumberOfMatches = 0;
142-
mLastFind = find.toString();
143-
mWebView.findAllAsync(mLastFind);
141+
mWebView.findAllAsync(find.toString());
144142
}
145143
}
146144

@@ -150,9 +148,8 @@ public void showSoftInput() {
150148
mInput.showSoftInput(mEditText, 0);
151149
}
152150

153-
public void updateMatchCount(int matchIndex, int matchCount,
154-
String findText) {
155-
if (mLastFind != null && mLastFind.equals(findText)) {
151+
public void updateMatchCount(int matchIndex, int matchCount, boolean isNewFind) {
152+
if (!isNewFind) {
156153
mNumberOfMatches = matchCount;
157154
mActiveMatchIndex = matchIndex;
158155
updateMatchesString();
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Copyright (C) 2012 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package android.webkit;
18+
19+
/**
20+
* @hide
21+
*/
22+
public interface FindListener {
23+
/**
24+
* Notify the host application that a find result is available.
25+
*
26+
* @param numberOfMatches How many matches have been found
27+
* @param activeMatchOrdinal The ordinal of the currently selected match
28+
* @param isDoneCounting Whether we have finished counting matches
29+
*/
30+
public void onFindResultReceived(int numberOfMatches,
31+
int activeMatchOrdinal, boolean isDoneCounting);
32+
}

core/java/android/webkit/WebView.java

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,7 +1226,19 @@ public WebBackForwardList copyBackForwardList() {
12261226

12271227
}
12281228

1229-
/*
1229+
/**
1230+
* Register the interface to be used when a find-on-page result has become
1231+
* available. This will replace the current handler.
1232+
*
1233+
* @param listener An implementation of FindListener
1234+
* @hide
1235+
*/
1236+
public void setFindListener(FindListener listener) {
1237+
checkThread();
1238+
mProvider.setFindListener(listener);
1239+
}
1240+
1241+
/**
12301242
* Highlight and scroll to the next occurance of String in findAll.
12311243
* Wraps the page infinitely, and scrolls. Must be called after
12321244
* calling findAll.
@@ -1238,8 +1250,9 @@ public void findNext(boolean forward) {
12381250
mProvider.findNext(forward);
12391251
}
12401252

1241-
/*
1253+
/**
12421254
* Find all instances of find on the page and highlight them.
1255+
*
12431256
* @param find String to find.
12441257
* @return int The number of occurances of the String "find"
12451258
* that were found.
@@ -1249,6 +1262,18 @@ public int findAll(String find) {
12491262
return mProvider.findAll(find);
12501263
}
12511264

1265+
/**
1266+
* Find all instances of find on the page and highlight them,
1267+
* asynchronously.
1268+
*
1269+
* @param find String to find.
1270+
* @hide
1271+
*/
1272+
public void findAllAsync(String find) {
1273+
checkThread();
1274+
mProvider.findAllAsync(find);
1275+
}
1276+
12521277
/**
12531278
* Start an ActionMode for finding text in this WebView. Only works if this
12541279
* WebView is attached to the view system.

core/java/android/webkit/WebViewClassic.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,6 +1440,9 @@ public void onTrimMemory(int level) {
14401440
// Used to notify listeners of a new picture.
14411441
private PictureListener mPictureListener;
14421442

1443+
// Used to notify listeners about find-on-page results.
1444+
private FindListener mFindListener;
1445+
14431446
/**
14441447
* Refer to {@link WebView#requestFocusNodeHref(Message)} for more information
14451448
*/
@@ -3694,6 +3697,17 @@ public WebBackForwardList copyBackForwardList() {
36943697
return mCallbackProxy.getBackForwardList().clone();
36953698
}
36963699

3700+
/**
3701+
* Register the interface to be used when a find-on-page result has become
3702+
* available. This will replace the current handler.
3703+
*
3704+
* @param listener An implementation of FindListener
3705+
*/
3706+
public void setFindListener(FindListener listener) {
3707+
checkThread();
3708+
mFindListener = listener;
3709+
}
3710+
36973711
/**
36983712
* See {@link WebView#findNext(boolean)}
36993713
*/
@@ -3723,6 +3737,7 @@ private int findAllBody(String find, boolean isAsync) {
37233737
checkThread();
37243738
if (0 == mNativeClass) return 0; // client isn't initialized
37253739
mLastFind = find;
3740+
if (find == null) return 0;
37263741
mWebViewCore.removeMessages(EventHub.FIND_ALL);
37273742
WebViewCore.FindAllRequest request = new
37283743
WebViewCore.FindAllRequest(find);
@@ -8478,10 +8493,11 @@ public void handleMessage(Message msg) {
84788493
}
84798494

84808495
case UPDATE_MATCH_COUNT: {
8481-
if (mFindCallback != null) {
8482-
mFindCallback.updateMatchCount(msg.arg1, msg.arg2,
8483-
(String) msg.obj);
8484-
}
8496+
boolean isNewFind = mLastFind == null || !mLastFind.equals(msg.obj);
8497+
if (mFindCallback != null)
8498+
mFindCallback.updateMatchCount(msg.arg1, msg.arg2, isNewFind);
8499+
if (mFindListener != null)
8500+
mFindListener.onFindResultReceived(msg.arg1, msg.arg2, true);
84858501
break;
84868502
}
84878503
case CLEAR_CARET_HANDLE:

core/java/android/webkit/WebViewProvider.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,10 +191,14 @@ public void loadDataWithBaseURL(String baseUrl, String data,
191191

192192
public WebBackForwardList copyBackForwardList();
193193

194+
public void setFindListener(FindListener listener);
195+
194196
public void findNext(boolean forward);
195197

196198
public int findAll(String find);
197199

200+
public void findAllAsync(String find);
201+
198202
public boolean showFindDialog(String text, boolean showIme);
199203

200204
public void clearMatches();

0 commit comments

Comments
 (0)