1fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins/*
2fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins * Copyright (C) 2010 The Android Open Source Project
3fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins *
4fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins * Licensed under the Apache License, Version 2.0 (the "License");
5fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins * you may not use this file except in compliance with the License.
6fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins * You may obtain a copy of the License at
7fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins *
8fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins *      http://www.apache.org/licenses/LICENSE-2.0
9fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins *
10fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins * Unless required by applicable law or agreed to in writing, software
11fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins * distributed under the License is distributed on an "AS IS" BASIS,
12fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins * See the License for the specific language governing permissions and
14fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins * limitations under the License.
15fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins */
16fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
17fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scrogginspackage android.webkit;
18fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
19fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scrogginsimport android.content.Context;
20fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scrogginsimport android.content.res.Resources;
21d6ac727234b9be45c3df4021dc83584e9849c00aJohn Reckimport android.graphics.Point;
22d6ac727234b9be45c3df4021dc83584e9849c00aJohn Reckimport android.graphics.Rect;
23fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scrogginsimport android.text.Editable;
24fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scrogginsimport android.text.Selection;
25fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scrogginsimport android.text.Spannable;
26fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scrogginsimport android.text.TextWatcher;
27fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scrogginsimport android.view.ActionMode;
28fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scrogginsimport android.view.LayoutInflater;
29fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scrogginsimport android.view.Menu;
30fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scrogginsimport android.view.MenuItem;
31fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scrogginsimport android.view.View;
32fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scrogginsimport android.view.inputmethod.InputMethodManager;
33fffce6fe99f7ae80f448790371b8c0fa90277d1aJohn Reckimport android.widget.EditText;
34fffce6fe99f7ae80f448790371b8c0fa90277d1aJohn Reckimport android.widget.TextView;
35fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
3652c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch/**
3752c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch * @hide
3852c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch */
3952c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdochpublic class FindActionModeCallback implements ActionMode.Callback, TextWatcher,
4052c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch        View.OnClickListener, WebView.FindListener {
41fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    private View mCustomView;
42fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    private EditText mEditText;
43fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    private TextView mMatches;
4452c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch    private WebView mWebView;
45fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    private InputMethodManager mInput;
46fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    private Resources mResources;
47fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    private boolean mMatchesFound;
48fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    private int mNumberOfMatches;
4947d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease    private int mActiveMatchIndex;
5005919f2133f8af89ab69c3a39aa34c6a32e78f05Leon Scroggins    private ActionMode mActionMode;
51fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
5252c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch    public FindActionModeCallback(Context context) {
53fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        mCustomView = LayoutInflater.from(context).inflate(
54fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                com.android.internal.R.layout.webview_find, null);
55fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        mEditText = (EditText) mCustomView.findViewById(
56fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                com.android.internal.R.id.edit);
577e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount        mEditText.setCustomSelectionActionModeCallback(new NoAction());
587014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins        mEditText.setOnClickListener(this);
59fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        setText("");
60fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        mMatches = (TextView) mCustomView.findViewById(
61fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                com.android.internal.R.id.matches);
62fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        mInput = (InputMethodManager)
63fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                context.getSystemService(Context.INPUT_METHOD_SERVICE);
64fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        mResources = context.getResources();
65fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    }
66fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
6752c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch    public void finish() {
6805919f2133f8af89ab69c3a39aa34c6a32e78f05Leon Scroggins        mActionMode.finish();
6905919f2133f8af89ab69c3a39aa34c6a32e78f05Leon Scroggins    }
7005919f2133f8af89ab69c3a39aa34c6a32e78f05Leon Scroggins
71fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    /*
72fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins     * Place text in the text field so it can be searched for.  Need to press
73fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins     * the find next or find previous button to find all of the matches.
74fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins     */
7552c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch    public void setText(String text) {
76fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        mEditText.setText(text);
77fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        Spannable span = (Spannable) mEditText.getText();
78fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        int length = span.length();
79fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        // Ideally, we would like to set the selection to the whole field,
80fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        // but this brings up the Text selection CAB, which dismisses this
81fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        // one.
82fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        Selection.setSelection(span, length, length);
83fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        // Necessary each time we set the text, so that this will watch
84fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        // changes to it.
85fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        span.setSpan(this, 0, length, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
86fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        mMatchesFound = false;
87fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    }
88fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
89fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    /*
9052c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch     * Set the WebView to search.  Must be non null.
91fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins     */
9252c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch    public void setWebView(WebView webView) {
93fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        if (null == webView) {
94fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins            throw new AssertionError("WebView supplied to "
95fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                    + "FindActionModeCallback cannot be null");
96fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        }
97fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        mWebView = webView;
9852c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch        mWebView.setFindDialogFindListener(this);
9952c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch    }
10052c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch
10152c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch    @Override
10252c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch    public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches,
10352c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch            boolean isDoneCounting) {
10452c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch        if (isDoneCounting) {
10552c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch            updateMatchCount(activeMatchOrdinal, numberOfMatches, numberOfMatches == 0);
10652c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch        }
107fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    }
108fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
109fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    /*
110fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins     * Move the highlight to the next match.
111fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins     * @param next If true, find the next match further down in the document.
112fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins     *             If false, find the previous match, up in the document.
113fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins     */
114fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    private void findNext(boolean next) {
115fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        if (mWebView == null) {
116fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins            throw new AssertionError(
117fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                    "No WebView for FindActionModeCallback::findNext");
118fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        }
1197014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins        if (!mMatchesFound) {
1207014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins            findAll();
1217014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins            return;
1227014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins        }
1237014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins        if (0 == mNumberOfMatches) {
1247014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins            // There are no matches, so moving to the next match will not do
1257014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins            // anything.
1267014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins            return;
1277014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins        }
128fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        mWebView.findNext(next);
1297014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins        updateMatchesString();
130fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    }
131fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
132fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    /*
133fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins     * Highlight all the instances of the string from mEditText in mWebView.
134fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins     */
13552c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch    public void findAll() {
136fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        if (mWebView == null) {
137fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins            throw new AssertionError(
138fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                    "No WebView for FindActionModeCallback::findAll");
139fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        }
140fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        CharSequence find = mEditText.getText();
141fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        if (0 == find.length()) {
142fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins            mWebView.clearMatches();
143fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins            mMatches.setVisibility(View.GONE);
144fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins            mMatchesFound = false;
145abeb6a791501151308d06db6aebb438e16c1a784Victoria Lease            mWebView.findAll(null);
146fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        } else {
147fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins            mMatchesFound = true;
14847d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease            mMatches.setVisibility(View.INVISIBLE);
14947d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease            mNumberOfMatches = 0;
150abeb6a791501151308d06db6aebb438e16c1a784Victoria Lease            mWebView.findAllAsync(find.toString());
151fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        }
152fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    }
153fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
154571354fd29db3e4855e2f179c2c6ad47f4eefd77Leon Scroggins    public void showSoftInput() {
15518cf9a7828f0b50daf967e22f0c5308b68d2919aLeon Scroggins        mInput.startGettingWindowFocus(mEditText.getRootView());
15618cf9a7828f0b50daf967e22f0c5308b68d2919aLeon Scroggins        mInput.focusIn(mEditText);
157571354fd29db3e4855e2f179c2c6ad47f4eefd77Leon Scroggins        mInput.showSoftInput(mEditText, 0);
158571354fd29db3e4855e2f179c2c6ad47f4eefd77Leon Scroggins    }
159571354fd29db3e4855e2f179c2c6ad47f4eefd77Leon Scroggins
1600b8413bbeb528c854e28c5ba1550239867da5c2eVictoria Lease    public void updateMatchCount(int matchIndex, int matchCount, boolean isEmptyFind) {
1610b8413bbeb528c854e28c5ba1550239867da5c2eVictoria Lease        if (!isEmptyFind) {
16247d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease            mNumberOfMatches = matchCount;
16347d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease            mActiveMatchIndex = matchIndex;
16447d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease            updateMatchesString();
16547d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease        } else {
1661c15b8d896229b11e13134c52579a5c9fac4d5f6Stefan Wysocki            mMatches.setVisibility(View.GONE);
16747d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease            mNumberOfMatches = 0;
16847d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease        }
16947d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease    }
17047d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease
171fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    /*
172fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins     * Update the string which tells the user how many matches were found, and
173fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins     * which match is currently highlighted.
174fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins     */
175fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    private void updateMatchesString() {
17647d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease        if (mNumberOfMatches == 0) {
17747d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease            mMatches.setText(com.android.internal.R.string.no_matches);
17847d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease        } else {
17947d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease            mMatches.setText(mResources.getQuantityString(
180fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                com.android.internal.R.plurals.matches_found, mNumberOfMatches,
18147d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease                mActiveMatchIndex + 1, mNumberOfMatches));
18247d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease        }
18347d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease        mMatches.setVisibility(View.VISIBLE);
184fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    }
185fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
1867014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins    // OnClickListener implementation
1877014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins
1887014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins    @Override
1897014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins    public void onClick(View v) {
1907014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins        findNext(true);
1917014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins    }
1927014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins
193fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    // ActionMode.Callback implementation
194fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
195fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    @Override
196fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
197f8419a0299680ed580975b0fcb758990b4367db8Adam Powell        if (!mode.isUiFocusable()) {
198f8419a0299680ed580975b0fcb758990b4367db8Adam Powell            // If the action mode we're running in is not focusable the user
199f8419a0299680ed580975b0fcb758990b4367db8Adam Powell            // will not be able to type into the find on page field. This
200f8419a0299680ed580975b0fcb758990b4367db8Adam Powell            // should only come up when we're running in a dialog which is
201f8419a0299680ed580975b0fcb758990b4367db8Adam Powell            // already less than ideal; disable the option for now.
202f8419a0299680ed580975b0fcb758990b4367db8Adam Powell            return false;
203f8419a0299680ed580975b0fcb758990b4367db8Adam Powell        }
204f8419a0299680ed580975b0fcb758990b4367db8Adam Powell
205fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        mode.setCustomView(mCustomView);
206fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        mode.getMenuInflater().inflate(com.android.internal.R.menu.webview_find,
207fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                menu);
20805919f2133f8af89ab69c3a39aa34c6a32e78f05Leon Scroggins        mActionMode = mode;
209fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        Editable edit = mEditText.getText();
210fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        Selection.setSelection(edit, edit.length());
211fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        mMatches.setVisibility(View.GONE);
212fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        mMatchesFound = false;
213fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        mMatches.setText("0");
214fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        mEditText.requestFocus();
215fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        return true;
216fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    }
217fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
218fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    @Override
219fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    public void onDestroyActionMode(ActionMode mode) {
220fffce6fe99f7ae80f448790371b8c0fa90277d1aJohn Reck        mActionMode = null;
221fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        mWebView.notifyFindDialogDismissed();
22252c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch        mWebView.setFindDialogFindListener(null);
22352c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch        mInput.hideSoftInputFromWindow(mWebView.getWindowToken(), 0);
224fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    }
225fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
226fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    @Override
227fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
228fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        return false;
229fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    }
230fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
231fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    @Override
232fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
233261f42d64dbd6d59af8ffecb315169b747167cc1Leon Scroggins        if (mWebView == null) {
234261f42d64dbd6d59af8ffecb315169b747167cc1Leon Scroggins            throw new AssertionError(
235261f42d64dbd6d59af8ffecb315169b747167cc1Leon Scroggins                    "No WebView for FindActionModeCallback::onActionItemClicked");
236261f42d64dbd6d59af8ffecb315169b747167cc1Leon Scroggins        }
23752c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch        mInput.hideSoftInputFromWindow(mWebView.getWindowToken(), 0);
238fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        switch(item.getItemId()) {
239fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins            case com.android.internal.R.id.find_prev:
240fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                findNext(false);
241fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                break;
242fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins            case com.android.internal.R.id.find_next:
243fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                findNext(true);
244fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                break;
245fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins            default:
246fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                return false;
247fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        }
248fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        return true;
249fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    }
250fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
251fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    // TextWatcher implementation
252fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
253fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    @Override
254fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    public void beforeTextChanged(CharSequence s,
255fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                                  int start,
256fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                                  int count,
257fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                                  int after) {
258fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        // Does nothing.  Needed to implement TextWatcher.
259fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    }
260fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
261fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    @Override
262fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    public void onTextChanged(CharSequence s,
263fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                              int start,
264fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                              int before,
265fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                              int count) {
266fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        findAll();
267fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    }
268fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
269fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    @Override
270fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    public void afterTextChanged(Editable s) {
271fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        // Does nothing.  Needed to implement TextWatcher.
272fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    }
273fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
274d6ac727234b9be45c3df4021dc83584e9849c00aJohn Reck    private Rect mGlobalVisibleRect = new Rect();
275d6ac727234b9be45c3df4021dc83584e9849c00aJohn Reck    private Point mGlobalVisibleOffset = new Point();
276d6ac727234b9be45c3df4021dc83584e9849c00aJohn Reck    public int getActionModeGlobalBottom() {
277fffce6fe99f7ae80f448790371b8c0fa90277d1aJohn Reck        if (mActionMode == null) {
278fffce6fe99f7ae80f448790371b8c0fa90277d1aJohn Reck            return 0;
279fffce6fe99f7ae80f448790371b8c0fa90277d1aJohn Reck        }
280d6ac727234b9be45c3df4021dc83584e9849c00aJohn Reck        View view = (View) mCustomView.getParent();
281d6ac727234b9be45c3df4021dc83584e9849c00aJohn Reck        if (view == null) {
282d6ac727234b9be45c3df4021dc83584e9849c00aJohn Reck            view = mCustomView;
283d6ac727234b9be45c3df4021dc83584e9849c00aJohn Reck        }
284d6ac727234b9be45c3df4021dc83584e9849c00aJohn Reck        view.getGlobalVisibleRect(mGlobalVisibleRect, mGlobalVisibleOffset);
285d6ac727234b9be45c3df4021dc83584e9849c00aJohn Reck        return mGlobalVisibleRect.bottom;
286fffce6fe99f7ae80f448790371b8c0fa90277d1aJohn Reck    }
287fffce6fe99f7ae80f448790371b8c0fa90277d1aJohn Reck
2887e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount    public static class NoAction implements ActionMode.Callback {
2897e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount        @Override
2907e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
2917e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount            return false;
2927e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount        }
2937e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount
2947e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount        @Override
2957e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
2967e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount            return false;
2977e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount        }
2987e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount
2997e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount        @Override
3007e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
3017e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount            return false;
3027e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount        }
3037e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount
3047e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount        @Override
3057e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount        public void onDestroyActionMode(ActionMode mode) {
3067e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount        }
3077e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount    }
308fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins}
309