FileDocCategorySizeDatePackage
FindActionModeCallback.javaAPI DocAndroid 5.1 API10155Thu Mar 12 22:22:10 GMT 2015android.webkit

FindActionModeCallback

public class FindActionModeCallback extends Object implements ActionMode.Callback, android.text.TextWatcher, View.OnClickListener, WebView.FindListener
hide

Fields Summary
private android.view.View
mCustomView
private android.widget.EditText
mEditText
private android.widget.TextView
mMatches
private WebView
mWebView
private android.view.inputmethod.InputMethodManager
mInput
private android.content.res.Resources
mResources
private boolean
mMatchesFound
private int
mNumberOfMatches
private int
mActiveMatchIndex
private android.view.ActionMode
mActionMode
private android.graphics.Rect
mGlobalVisibleRect
private android.graphics.Point
mGlobalVisibleOffset
Constructors Summary
public FindActionModeCallback(android.content.Context context)

        mCustomView = LayoutInflater.from(context).inflate(
                com.android.internal.R.layout.webview_find, null);
        mEditText = (EditText) mCustomView.findViewById(
                com.android.internal.R.id.edit);
        mEditText.setCustomSelectionActionModeCallback(new NoAction());
        mEditText.setOnClickListener(this);
        setText("");
        mMatches = (TextView) mCustomView.findViewById(
                com.android.internal.R.id.matches);
        mInput = (InputMethodManager)
                context.getSystemService(Context.INPUT_METHOD_SERVICE);
        mResources = context.getResources();
    
Methods Summary
public voidafterTextChanged(android.text.Editable s)

        // Does nothing.  Needed to implement TextWatcher.
    
public voidbeforeTextChanged(java.lang.CharSequence s, int start, int count, int after)

        // Does nothing.  Needed to implement TextWatcher.
    
public voidfindAll()

        if (mWebView == null) {
            throw new AssertionError(
                    "No WebView for FindActionModeCallback::findAll");
        }
        CharSequence find = mEditText.getText();
        if (0 == find.length()) {
            mWebView.clearMatches();
            mMatches.setVisibility(View.GONE);
            mMatchesFound = false;
            mWebView.findAll(null);
        } else {
            mMatchesFound = true;
            mMatches.setVisibility(View.INVISIBLE);
            mNumberOfMatches = 0;
            mWebView.findAllAsync(find.toString());
        }
    
private voidfindNext(boolean next)

        if (mWebView == null) {
            throw new AssertionError(
                    "No WebView for FindActionModeCallback::findNext");
        }
        if (!mMatchesFound) {
            findAll();
            return;
        }
        if (0 == mNumberOfMatches) {
            // There are no matches, so moving to the next match will not do
            // anything.
            return;
        }
        mWebView.findNext(next);
        updateMatchesString();
    
public voidfinish()

        mActionMode.finish();
    
public intgetActionModeGlobalBottom()

       
        if (mActionMode == null) {
            return 0;
        }
        View view = (View) mCustomView.getParent();
        if (view == null) {
            view = mCustomView;
        }
        view.getGlobalVisibleRect(mGlobalVisibleRect, mGlobalVisibleOffset);
        return mGlobalVisibleRect.bottom;
    
public booleanonActionItemClicked(android.view.ActionMode mode, android.view.MenuItem item)

        if (mWebView == null) {
            throw new AssertionError(
                    "No WebView for FindActionModeCallback::onActionItemClicked");
        }
        mInput.hideSoftInputFromWindow(mWebView.getWindowToken(), 0);
        switch(item.getItemId()) {
            case com.android.internal.R.id.find_prev:
                findNext(false);
                break;
            case com.android.internal.R.id.find_next:
                findNext(true);
                break;
            default:
                return false;
        }
        return true;
    
public voidonClick(android.view.View v)

        findNext(true);
    
public booleanonCreateActionMode(android.view.ActionMode mode, android.view.Menu menu)

        if (!mode.isUiFocusable()) {
            // If the action mode we're running in is not focusable the user
            // will not be able to type into the find on page field. This
            // should only come up when we're running in a dialog which is
            // already less than ideal; disable the option for now.
            return false;
        }

        mode.setCustomView(mCustomView);
        mode.getMenuInflater().inflate(com.android.internal.R.menu.webview_find,
                menu);
        mActionMode = mode;
        Editable edit = mEditText.getText();
        Selection.setSelection(edit, edit.length());
        mMatches.setVisibility(View.GONE);
        mMatchesFound = false;
        mMatches.setText("0");
        mEditText.requestFocus();
        return true;
    
public voidonDestroyActionMode(android.view.ActionMode mode)

        mActionMode = null;
        mWebView.notifyFindDialogDismissed();
        mWebView.setFindDialogFindListener(null);
        mInput.hideSoftInputFromWindow(mWebView.getWindowToken(), 0);
    
public voidonFindResultReceived(int activeMatchOrdinal, int numberOfMatches, boolean isDoneCounting)

        if (isDoneCounting) {
            updateMatchCount(activeMatchOrdinal, numberOfMatches, numberOfMatches == 0);
        }
    
public booleanonPrepareActionMode(android.view.ActionMode mode, android.view.Menu menu)

        return false;
    
public voidonTextChanged(java.lang.CharSequence s, int start, int before, int count)

        findAll();
    
public voidsetText(java.lang.String text)

        mEditText.setText(text);
        Spannable span = (Spannable) mEditText.getText();
        int length = span.length();
        // Ideally, we would like to set the selection to the whole field,
        // but this brings up the Text selection CAB, which dismisses this
        // one.
        Selection.setSelection(span, length, length);
        // Necessary each time we set the text, so that this will watch
        // changes to it.
        span.setSpan(this, 0, length, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        mMatchesFound = false;
    
public voidsetWebView(WebView webView)

        if (null == webView) {
            throw new AssertionError("WebView supplied to "
                    + "FindActionModeCallback cannot be null");
        }
        mWebView = webView;
        mWebView.setFindDialogFindListener(this);
    
public voidshowSoftInput()

        mInput.startGettingWindowFocus(mEditText.getRootView());
        mInput.focusIn(mEditText);
        mInput.showSoftInput(mEditText, 0);
    
public voidupdateMatchCount(int matchIndex, int matchCount, boolean isEmptyFind)

        if (!isEmptyFind) {
            mNumberOfMatches = matchCount;
            mActiveMatchIndex = matchIndex;
            updateMatchesString();
        } else {
            mMatches.setVisibility(View.GONE);
            mNumberOfMatches = 0;
        }
    
private voidupdateMatchesString()

        if (mNumberOfMatches == 0) {
            mMatches.setText(com.android.internal.R.string.no_matches);
        } else {
            mMatches.setText(mResources.getQuantityString(
                com.android.internal.R.plurals.matches_found, mNumberOfMatches,
                mActiveMatchIndex + 1, mNumberOfMatches));
        }
        mMatches.setVisibility(View.VISIBLE);