Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 03d366e8 authored by Ben Murdoch's avatar Ben Murdoch Committed by Android (Google) Code Review
Browse files

Merge "Refactor FindActionModeCallback."

parents ebf7c743 52c9f7f9
Loading
Loading
Loading
Loading
+24 −12
Original line number Original line Diff line number Diff line
@@ -33,12 +33,15 @@ import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.EditText;
import android.widget.TextView;
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 View mCustomView;
    private EditText mEditText;
    private EditText mEditText;
    private TextView mMatches;
    private TextView mMatches;
    private WebViewClassic mWebView;
    private WebView mWebView;
    private InputMethodManager mInput;
    private InputMethodManager mInput;
    private Resources mResources;
    private Resources mResources;
    private boolean mMatchesFound;
    private boolean mMatchesFound;
@@ -46,7 +49,7 @@ class FindActionModeCallback implements ActionMode.Callback, TextWatcher,
    private int mActiveMatchIndex;
    private int mActiveMatchIndex;
    private ActionMode mActionMode;
    private ActionMode mActionMode;


    FindActionModeCallback(Context context) {
    public FindActionModeCallback(Context context) {
        mCustomView = LayoutInflater.from(context).inflate(
        mCustomView = LayoutInflater.from(context).inflate(
                com.android.internal.R.layout.webview_find, null);
                com.android.internal.R.layout.webview_find, null);
        mEditText = (EditText) mCustomView.findViewById(
        mEditText = (EditText) mCustomView.findViewById(
@@ -61,7 +64,7 @@ class FindActionModeCallback implements ActionMode.Callback, TextWatcher,
        mResources = context.getResources();
        mResources = context.getResources();
    }
    }


    void finish() {
    public void finish() {
        mActionMode.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
     * 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.
     * 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);
        mEditText.setText(text);
        Spannable span = (Spannable) mEditText.getText();
        Spannable span = (Spannable) mEditText.getText();
        int length = span.length();
        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
     * Set the WebView to search.  Must be non null.
     * startActionMode.
     */
     */
    void setWebView(WebViewClassic webView) {
    public void setWebView(WebView webView) {
        if (null == webView) {
        if (null == webView) {
            throw new AssertionError("WebView supplied to "
            throw new AssertionError("WebView supplied to "
                    + "FindActionModeCallback cannot be null");
                    + "FindActionModeCallback cannot be null");
        }
        }
        mWebView = webView;
        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.
     * Highlight all the instances of the string from mEditText in mWebView.
     */
     */
    void findAll() {
    public void findAll() {
        if (mWebView == null) {
        if (mWebView == null) {
            throw new AssertionError(
            throw new AssertionError(
                    "No WebView for FindActionModeCallback::findAll");
                    "No WebView for FindActionModeCallback::findAll");
@@ -208,7 +219,8 @@ class FindActionModeCallback implements ActionMode.Callback, TextWatcher,
    public void onDestroyActionMode(ActionMode mode) {
    public void onDestroyActionMode(ActionMode mode) {
        mActionMode = null;
        mActionMode = null;
        mWebView.notifyFindDialogDismissed();
        mWebView.notifyFindDialogDismissed();
        mInput.hideSoftInputFromWindow(mWebView.getWebView().getWindowToken(), 0);
        mWebView.setFindDialogFindListener(null);
        mInput.hideSoftInputFromWindow(mWebView.getWindowToken(), 0);
    }
    }


    @Override
    @Override
@@ -222,7 +234,7 @@ class FindActionModeCallback implements ActionMode.Callback, TextWatcher,
            throw new AssertionError(
            throw new AssertionError(
                    "No WebView for FindActionModeCallback::onActionItemClicked");
                    "No WebView for FindActionModeCallback::onActionItemClicked");
        }
        }
        mInput.hideSoftInputFromWindow(mWebView.getWebView().getWindowToken(), 0);
        mInput.hideSoftInputFromWindow(mWebView.getWindowToken(), 0);
        switch(item.getItemId()) {
        switch(item.getItemId()) {
            case com.android.internal.R.id.find_prev:
            case com.android.internal.R.id.find_prev:
                findNext(false);
                findNext(false);
+51 −1
Original line number Original line Diff line number Diff line
@@ -1329,7 +1329,8 @@ public class WebView extends AbsoluteLayout
     */
     */
    public void setFindListener(FindListener listener) {
    public void setFindListener(FindListener listener) {
        checkThread();
        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 internal stuff
    //-------------------------------------------------------------------------
    //-------------------------------------------------------------------------


    private WebViewProvider mProvider;
    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() {
    private void ensureProviderCreated() {
        checkThread();
        checkThread();
        if (mProvider == null) {
        if (mProvider == null) {
+3 −2
Original line number Original line Diff line number Diff line
@@ -3668,7 +3668,7 @@ public final class WebViewClassic implements WebViewProvider, WebViewProvider.Sc
        mCachedOverlappingActionModeHeight = -1;
        mCachedOverlappingActionModeHeight = -1;
        mFindCallback = callback;
        mFindCallback = callback;
        setFindIsUp(true);
        setFindIsUp(true);
        mFindCallback.setWebView(this);
        mFindCallback.setWebView(getWebView());
        if (showIme) {
        if (showIme) {
            mFindCallback.showSoftInput();
            mFindCallback.showSoftInput();
        } else if (text != null) {
        } else if (text != null) {
@@ -3770,7 +3770,8 @@ public final class WebViewClassic implements WebViewProvider, WebViewProvider.Sc
    /**
    /**
     * Called when the find ActionMode ends.
     * Called when the find ActionMode ends.
     */
     */
    void notifyFindDialogDismissed() {
    @Override
    public void notifyFindDialogDismissed() {
        mFindCallback = null;
        mFindCallback = null;
        mCachedOverlappingActionModeHeight = -1;
        mCachedOverlappingActionModeHeight = -1;
        if (mWebViewCore == null) {
        if (mWebViewCore == null) {
+7 −1
Original line number Original line Diff line number Diff line
@@ -240,7 +240,7 @@ public interface WebViewProvider {
    public View findHierarchyView(String className, int hashCode);
    public View findHierarchyView(String className, int hashCode);


    //-------------------------------------------------------------------------
    //-------------------------------------------------------------------------
    // Provider glue methods
    // Provider internal methods
    //-------------------------------------------------------------------------
    //-------------------------------------------------------------------------


    /**
    /**
@@ -255,6 +255,12 @@ public interface WebViewProvider {
     */
     */
    /* package */ ScrollDelegate getScrollDelegate();
    /* package */ ScrollDelegate getScrollDelegate();


    /**
     * Only used by FindActionModeCallback to inform providers that the find dialog has
     * been dismissed.
     */
    public void notifyFindDialogDismissed();

    //-------------------------------------------------------------------------
    //-------------------------------------------------------------------------
    // View / ViewGroup delegation methods
    // View / ViewGroup delegation methods
    //-------------------------------------------------------------------------
    //-------------------------------------------------------------------------