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
19451e338c51e8c45efc0d21536dfae6f78f6d5e06Ignacio Sollaimport android.annotation.SystemApi;
20fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scrogginsimport android.content.Context;
21fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scrogginsimport android.content.res.Resources;
22d6ac727234b9be45c3df4021dc83584e9849c00aJohn Reckimport android.graphics.Point;
23d6ac727234b9be45c3df4021dc83584e9849c00aJohn Reckimport android.graphics.Rect;
24fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scrogginsimport android.text.Editable;
25fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scrogginsimport android.text.Selection;
26fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scrogginsimport android.text.Spannable;
27fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scrogginsimport android.text.TextWatcher;
28fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scrogginsimport android.view.ActionMode;
29fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scrogginsimport android.view.LayoutInflater;
30fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scrogginsimport android.view.Menu;
31fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scrogginsimport android.view.MenuItem;
32fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scrogginsimport android.view.View;
33fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scrogginsimport android.view.inputmethod.InputMethodManager;
34fffce6fe99f7ae80f448790371b8c0fa90277d1aJohn Reckimport android.widget.EditText;
35fffce6fe99f7ae80f448790371b8c0fa90277d1aJohn Reckimport android.widget.TextView;
36fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
3752c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch/**
3852c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch * @hide
3952c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch */
40451e338c51e8c45efc0d21536dfae6f78f6d5e06Ignacio Solla@SystemApi
4152c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdochpublic class FindActionModeCallback implements ActionMode.Callback, TextWatcher,
4252c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch        View.OnClickListener, WebView.FindListener {
43fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    private View mCustomView;
44fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    private EditText mEditText;
45fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    private TextView mMatches;
4652c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch    private WebView mWebView;
47fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    private InputMethodManager mInput;
48fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    private Resources mResources;
49fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    private boolean mMatchesFound;
50fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    private int mNumberOfMatches;
5147d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease    private int mActiveMatchIndex;
5205919f2133f8af89ab69c3a39aa34c6a32e78f05Leon Scroggins    private ActionMode mActionMode;
53fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
5452c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch    public FindActionModeCallback(Context context) {
55fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        mCustomView = LayoutInflater.from(context).inflate(
56fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                com.android.internal.R.layout.webview_find, null);
57fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        mEditText = (EditText) mCustomView.findViewById(
58fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                com.android.internal.R.id.edit);
597e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount        mEditText.setCustomSelectionActionModeCallback(new NoAction());
607014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins        mEditText.setOnClickListener(this);
61fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        setText("");
62fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        mMatches = (TextView) mCustomView.findViewById(
63fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                com.android.internal.R.id.matches);
64777ef95ebf18c61ff09e7567a06058d351c530caYohei Yukawa        mInput = context.getSystemService(InputMethodManager.class);
65fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        mResources = context.getResources();
66fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    }
67fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
6852c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch    public void finish() {
6905919f2133f8af89ab69c3a39aa34c6a32e78f05Leon Scroggins        mActionMode.finish();
7005919f2133f8af89ab69c3a39aa34c6a32e78f05Leon Scroggins    }
7105919f2133f8af89ab69c3a39aa34c6a32e78f05Leon Scroggins
72fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    /*
73fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins     * Place text in the text field so it can be searched for.  Need to press
74fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins     * the find next or find previous button to find all of the matches.
75fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins     */
7652c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch    public void setText(String text) {
77fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        mEditText.setText(text);
78fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        Spannable span = (Spannable) mEditText.getText();
79fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        int length = span.length();
80fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        // Ideally, we would like to set the selection to the whole field,
81fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        // but this brings up the Text selection CAB, which dismisses this
82fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        // one.
83fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        Selection.setSelection(span, length, length);
84fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        // Necessary each time we set the text, so that this will watch
85fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        // changes to it.
86fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        span.setSpan(this, 0, length, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
87fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        mMatchesFound = false;
88fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    }
89fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
90fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    /*
9152c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch     * Set the WebView to search.  Must be non null.
92fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins     */
9352c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch    public void setWebView(WebView webView) {
94fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        if (null == webView) {
95fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins            throw new AssertionError("WebView supplied to "
96fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                    + "FindActionModeCallback cannot be null");
97fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        }
98fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        mWebView = webView;
9952c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch        mWebView.setFindDialogFindListener(this);
10052c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch    }
10152c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch
10252c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch    @Override
10352c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch    public void onFindResultReceived(int activeMatchOrdinal, int numberOfMatches,
10452c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch            boolean isDoneCounting) {
10552c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch        if (isDoneCounting) {
10652c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch            updateMatchCount(activeMatchOrdinal, numberOfMatches, numberOfMatches == 0);
10752c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch        }
108fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    }
109fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
110fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    /*
111fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins     * Move the highlight to the next match.
112fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins     * @param next If true, find the next match further down in the document.
113fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins     *             If false, find the previous match, up in the document.
114fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins     */
115fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    private void findNext(boolean next) {
116fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        if (mWebView == null) {
117fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins            throw new AssertionError(
118fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                    "No WebView for FindActionModeCallback::findNext");
119fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        }
1207014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins        if (!mMatchesFound) {
1217014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins            findAll();
1227014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins            return;
1237014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins        }
1247014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins        if (0 == mNumberOfMatches) {
1257014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins            // There are no matches, so moving to the next match will not do
1267014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins            // anything.
1277014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins            return;
1287014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins        }
129fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        mWebView.findNext(next);
1307014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins        updateMatchesString();
131fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    }
132fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
133fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    /*
134fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins     * Highlight all the instances of the string from mEditText in mWebView.
135fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins     */
13652c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch    public void findAll() {
137fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        if (mWebView == null) {
138fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins            throw new AssertionError(
139fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                    "No WebView for FindActionModeCallback::findAll");
140fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        }
141fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        CharSequence find = mEditText.getText();
142fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        if (0 == find.length()) {
143fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins            mWebView.clearMatches();
144fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins            mMatches.setVisibility(View.GONE);
145fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins            mMatchesFound = false;
146abeb6a791501151308d06db6aebb438e16c1a784Victoria Lease            mWebView.findAll(null);
147fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        } else {
148fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins            mMatchesFound = true;
14947d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease            mMatches.setVisibility(View.INVISIBLE);
15047d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease            mNumberOfMatches = 0;
151abeb6a791501151308d06db6aebb438e16c1a784Victoria Lease            mWebView.findAllAsync(find.toString());
152fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        }
153fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    }
154fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
155571354fd29db3e4855e2f179c2c6ad47f4eefd77Leon Scroggins    public void showSoftInput() {
1565f05965f546fa42750f1a8314f8a2da01fd6bfb4Yohei Yukawa        if (mEditText.requestFocus()) {
1575f05965f546fa42750f1a8314f8a2da01fd6bfb4Yohei Yukawa            mInput.showSoftInput(mEditText, 0);
1585f05965f546fa42750f1a8314f8a2da01fd6bfb4Yohei Yukawa        }
159571354fd29db3e4855e2f179c2c6ad47f4eefd77Leon Scroggins    }
160571354fd29db3e4855e2f179c2c6ad47f4eefd77Leon Scroggins
1610b8413bbeb528c854e28c5ba1550239867da5c2eVictoria Lease    public void updateMatchCount(int matchIndex, int matchCount, boolean isEmptyFind) {
1620b8413bbeb528c854e28c5ba1550239867da5c2eVictoria Lease        if (!isEmptyFind) {
16347d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease            mNumberOfMatches = matchCount;
16447d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease            mActiveMatchIndex = matchIndex;
16547d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease            updateMatchesString();
16647d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease        } else {
1671c15b8d896229b11e13134c52579a5c9fac4d5f6Stefan Wysocki            mMatches.setVisibility(View.GONE);
16847d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease            mNumberOfMatches = 0;
16947d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease        }
17047d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease    }
17147d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease
172fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    /*
173fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins     * Update the string which tells the user how many matches were found, and
174fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins     * which match is currently highlighted.
175fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins     */
176fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    private void updateMatchesString() {
17747d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease        if (mNumberOfMatches == 0) {
17847d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease            mMatches.setText(com.android.internal.R.string.no_matches);
17947d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease        } else {
18047d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease            mMatches.setText(mResources.getQuantityString(
181fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                com.android.internal.R.plurals.matches_found, mNumberOfMatches,
18247d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease                mActiveMatchIndex + 1, mNumberOfMatches));
18347d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease        }
18447d0ee9766fb972f49d5116d2d7d3a23b5321211Victoria Lease        mMatches.setVisibility(View.VISIBLE);
185fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    }
186fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
1877014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins    // OnClickListener implementation
1887014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins
1897014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins    @Override
1907014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins    public void onClick(View v) {
1917014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins        findNext(true);
1927014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins    }
1937014b12873068f109f58b9d0ad4116fd8d4bf22fLeon Scroggins
194fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    // ActionMode.Callback implementation
195fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
196fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    @Override
197fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
198f8419a0299680ed580975b0fcb758990b4367db8Adam Powell        if (!mode.isUiFocusable()) {
199f8419a0299680ed580975b0fcb758990b4367db8Adam Powell            // If the action mode we're running in is not focusable the user
200f8419a0299680ed580975b0fcb758990b4367db8Adam Powell            // will not be able to type into the find on page field. This
201f8419a0299680ed580975b0fcb758990b4367db8Adam Powell            // should only come up when we're running in a dialog which is
202f8419a0299680ed580975b0fcb758990b4367db8Adam Powell            // already less than ideal; disable the option for now.
203f8419a0299680ed580975b0fcb758990b4367db8Adam Powell            return false;
204f8419a0299680ed580975b0fcb758990b4367db8Adam Powell        }
205f8419a0299680ed580975b0fcb758990b4367db8Adam Powell
206fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        mode.setCustomView(mCustomView);
207fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        mode.getMenuInflater().inflate(com.android.internal.R.menu.webview_find,
208fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                menu);
20905919f2133f8af89ab69c3a39aa34c6a32e78f05Leon Scroggins        mActionMode = mode;
210fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        Editable edit = mEditText.getText();
211fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        Selection.setSelection(edit, edit.length());
212fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        mMatches.setVisibility(View.GONE);
213fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        mMatchesFound = false;
214fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        mMatches.setText("0");
215fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        mEditText.requestFocus();
216fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        return true;
217fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    }
218fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
219fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    @Override
220fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    public void onDestroyActionMode(ActionMode mode) {
221fffce6fe99f7ae80f448790371b8c0fa90277d1aJohn Reck        mActionMode = null;
222fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        mWebView.notifyFindDialogDismissed();
22352c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch        mWebView.setFindDialogFindListener(null);
22452c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch        mInput.hideSoftInputFromWindow(mWebView.getWindowToken(), 0);
225fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    }
226fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
227fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    @Override
228fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
229fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        return false;
230fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    }
231fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
232fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    @Override
233fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
234261f42d64dbd6d59af8ffecb315169b747167cc1Leon Scroggins        if (mWebView == null) {
235261f42d64dbd6d59af8ffecb315169b747167cc1Leon Scroggins            throw new AssertionError(
236261f42d64dbd6d59af8ffecb315169b747167cc1Leon Scroggins                    "No WebView for FindActionModeCallback::onActionItemClicked");
237261f42d64dbd6d59af8ffecb315169b747167cc1Leon Scroggins        }
23852c9f7f95083a9d041b9261522e929073cb52fd5Ben Murdoch        mInput.hideSoftInputFromWindow(mWebView.getWindowToken(), 0);
239fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        switch(item.getItemId()) {
240fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins            case com.android.internal.R.id.find_prev:
241fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                findNext(false);
242fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                break;
243fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins            case com.android.internal.R.id.find_next:
244fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                findNext(true);
245fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                break;
246fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins            default:
247fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                return false;
248fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        }
249fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        return true;
250fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    }
251fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
252fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    // TextWatcher implementation
253fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
254fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    @Override
255fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    public void beforeTextChanged(CharSequence s,
256fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                                  int start,
257fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                                  int count,
258fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                                  int after) {
259fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        // Does nothing.  Needed to implement TextWatcher.
260fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    }
261fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
262fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    @Override
263fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    public void onTextChanged(CharSequence s,
264fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                              int start,
265fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                              int before,
266fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins                              int count) {
267fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        findAll();
268fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    }
269fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
270fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    @Override
271fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    public void afterTextChanged(Editable s) {
272fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins        // Does nothing.  Needed to implement TextWatcher.
273fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins    }
274fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins
275d6ac727234b9be45c3df4021dc83584e9849c00aJohn Reck    private Rect mGlobalVisibleRect = new Rect();
276d6ac727234b9be45c3df4021dc83584e9849c00aJohn Reck    private Point mGlobalVisibleOffset = new Point();
277d6ac727234b9be45c3df4021dc83584e9849c00aJohn Reck    public int getActionModeGlobalBottom() {
278fffce6fe99f7ae80f448790371b8c0fa90277d1aJohn Reck        if (mActionMode == null) {
279fffce6fe99f7ae80f448790371b8c0fa90277d1aJohn Reck            return 0;
280fffce6fe99f7ae80f448790371b8c0fa90277d1aJohn Reck        }
281d6ac727234b9be45c3df4021dc83584e9849c00aJohn Reck        View view = (View) mCustomView.getParent();
282d6ac727234b9be45c3df4021dc83584e9849c00aJohn Reck        if (view == null) {
283d6ac727234b9be45c3df4021dc83584e9849c00aJohn Reck            view = mCustomView;
284d6ac727234b9be45c3df4021dc83584e9849c00aJohn Reck        }
285d6ac727234b9be45c3df4021dc83584e9849c00aJohn Reck        view.getGlobalVisibleRect(mGlobalVisibleRect, mGlobalVisibleOffset);
286d6ac727234b9be45c3df4021dc83584e9849c00aJohn Reck        return mGlobalVisibleRect.bottom;
287fffce6fe99f7ae80f448790371b8c0fa90277d1aJohn Reck    }
288fffce6fe99f7ae80f448790371b8c0fa90277d1aJohn Reck
2897e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount    public static class NoAction implements ActionMode.Callback {
2907e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount        @Override
2917e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
2927e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount            return false;
2937e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount        }
2947e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount
2957e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount        @Override
2967e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
2977e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount            return false;
2987e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount        }
2997e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount
3007e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount        @Override
3017e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
3027e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount            return false;
3037e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount        }
3047e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount
3057e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount        @Override
3067e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount        public void onDestroyActionMode(ActionMode mode) {
3077e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount        }
3087e9f62ef486af98ce6fbb3e2918a45c68c0b551fGeorge Mount    }
309fe026bdd3c0fd7543ceaf0732aeb824cfddb5b23Leon Scroggins}
310