am 03d366e8: Merge "Refactor FindActionModeCallback."

* commit '03d366e89c11352aa0ba3ff7038f371211d1ce4e':
  Refactor FindActionModeCallback.
This commit is contained in:
Ben Murdoch
2013-01-18 15:29:13 -08:00
committed by Android Git Automerger
4 changed files with 85 additions and 16 deletions

View File

@@ -33,12 +33,15 @@ import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;
class FindActionModeCallback implements ActionMode.Callback, TextWatcher,
View.OnClickListener {
/**
* @hide
*/
public class FindActionModeCallback implements ActionMode.Callback, TextWatcher,
View.OnClickListener, WebView.FindListener {
private View mCustomView;
private EditText mEditText;
private TextView mMatches;
private WebViewClassic mWebView;
private WebView mWebView;
private InputMethodManager mInput;
private Resources mResources;
private boolean mMatchesFound;
@@ -46,7 +49,7 @@ class FindActionModeCallback implements ActionMode.Callback, TextWatcher,
private int mActiveMatchIndex;
private ActionMode mActionMode;
FindActionModeCallback(Context context) {
public FindActionModeCallback(Context context) {
mCustomView = LayoutInflater.from(context).inflate(
com.android.internal.R.layout.webview_find, null);
mEditText = (EditText) mCustomView.findViewById(
@@ -61,7 +64,7 @@ class FindActionModeCallback implements ActionMode.Callback, TextWatcher,
mResources = context.getResources();
}
void finish() {
public void finish() {
mActionMode.finish();
}
@@ -69,7 +72,7 @@ class FindActionModeCallback implements ActionMode.Callback, TextWatcher,
* Place text in the text field so it can be searched for. Need to press
* the find next or find previous button to find all of the matches.
*/
void setText(String text) {
public void setText(String text) {
mEditText.setText(text);
Spannable span = (Spannable) mEditText.getText();
int length = span.length();
@@ -84,15 +87,23 @@ class FindActionModeCallback implements ActionMode.Callback, TextWatcher,
}
/*
* Set the WebView to search. Must be non null, and set before calling
* startActionMode.
* Set the WebView to search. Must be non null.
*/
void setWebView(WebViewClassic webView) {
public void setWebView(WebView webView) {
if (null == webView) {
throw new AssertionError("WebView supplied to "
+ "FindActionModeCallback cannot be null");
}
mWebView = webView;
mWebView.setFindDialogFindListener(this);
}
@Override
public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches,
boolean isDoneCounting) {
if (isDoneCounting) {
updateMatchCount(activeMatchOrdinal, numberOfMatches, numberOfMatches == 0);
}
}
/*
@@ -121,7 +132,7 @@ class FindActionModeCallback implements ActionMode.Callback, TextWatcher,
/*
* Highlight all the instances of the string from mEditText in mWebView.
*/
void findAll() {
public void findAll() {
if (mWebView == null) {
throw new AssertionError(
"No WebView for FindActionModeCallback::findAll");
@@ -208,7 +219,8 @@ class FindActionModeCallback implements ActionMode.Callback, TextWatcher,
public void onDestroyActionMode(ActionMode mode) {
mActionMode = null;
mWebView.notifyFindDialogDismissed();
mInput.hideSoftInputFromWindow(mWebView.getWebView().getWindowToken(), 0);
mWebView.setFindDialogFindListener(null);
mInput.hideSoftInputFromWindow(mWebView.getWindowToken(), 0);
}
@Override
@@ -222,7 +234,7 @@ class FindActionModeCallback implements ActionMode.Callback, TextWatcher,
throw new AssertionError(
"No WebView for FindActionModeCallback::onActionItemClicked");
}
mInput.hideSoftInputFromWindow(mWebView.getWebView().getWindowToken(), 0);
mInput.hideSoftInputFromWindow(mWebView.getWindowToken(), 0);
switch(item.getItemId()) {
case com.android.internal.R.id.find_prev:
findNext(false);

View File

@@ -1329,7 +1329,8 @@ public class WebView extends AbsoluteLayout
*/
public void setFindListener(FindListener listener) {
checkThread();
mProvider.setFindListener(listener);
setupFindListenerIfNeeded();
mFindListener.mUserFindListener = listener;
}
/**
@@ -1849,12 +1850,61 @@ public class WebView extends AbsoluteLayout
}
//-------------------------------------------------------------------------
// Package-private internal stuff
//-------------------------------------------------------------------------
// Only used by android.webkit.FindActionModeCallback.
void setFindDialogFindListener(FindListener listener) {
checkThread();
setupFindListenerIfNeeded();
mFindListener.mFindDialogFindListener = listener;
}
// Only used by android.webkit.FindActionModeCallback.
void notifyFindDialogDismissed() {
checkThread();
mProvider.notifyFindDialogDismissed();
}
//-------------------------------------------------------------------------
// Private internal stuff
//-------------------------------------------------------------------------
private WebViewProvider mProvider;
/**
* In addition to the FindListener that the user may set via the WebView.setFindListener
* API, FindActionModeCallback will register it's own FindListener. We keep them separate
* via this class so that that the two FindListeners can potentially exist at once.
*/
private class FindListenerDistributor implements FindListener {
private FindListener mFindDialogFindListener;
private FindListener mUserFindListener;
@Override
public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches,
boolean isDoneCounting) {
if (mFindDialogFindListener != null) {
mFindDialogFindListener.onFindResultReceived(activeMatchOrdinal, numberOfMatches,
isDoneCounting);
}
if (mUserFindListener != null) {
mUserFindListener.onFindResultReceived(activeMatchOrdinal, numberOfMatches,
isDoneCounting);
}
}
}
private FindListenerDistributor mFindListener;
private void setupFindListenerIfNeeded() {
if (mFindListener == null) {
mFindListener = new FindListenerDistributor();
mProvider.setFindListener(mFindListener);
}
}
private void ensureProviderCreated() {
checkThread();
if (mProvider == null) {

View File

@@ -3668,7 +3668,7 @@ public final class WebViewClassic implements WebViewProvider, WebViewProvider.Sc
mCachedOverlappingActionModeHeight = -1;
mFindCallback = callback;
setFindIsUp(true);
mFindCallback.setWebView(this);
mFindCallback.setWebView(getWebView());
if (showIme) {
mFindCallback.showSoftInput();
} else if (text != null) {
@@ -3770,7 +3770,8 @@ public final class WebViewClassic implements WebViewProvider, WebViewProvider.Sc
/**
* Called when the find ActionMode ends.
*/
void notifyFindDialogDismissed() {
@Override
public void notifyFindDialogDismissed() {
mFindCallback = null;
mCachedOverlappingActionModeHeight = -1;
if (mWebViewCore == null) {

View File

@@ -240,7 +240,7 @@ public interface WebViewProvider {
public View findHierarchyView(String className, int hashCode);
//-------------------------------------------------------------------------
// Provider glue methods
// Provider internal methods
//-------------------------------------------------------------------------
/**
@@ -255,6 +255,12 @@ public interface WebViewProvider {
*/
/* package */ ScrollDelegate getScrollDelegate();
/**
* Only used by FindActionModeCallback to inform providers that the find dialog has
* been dismissed.
*/
public void notifyFindDialogDismissed();
//-------------------------------------------------------------------------
// View / ViewGroup delegation methods
//-------------------------------------------------------------------------