-
Notifications
You must be signed in to change notification settings - Fork 99
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from qnga/fix/search
Implement a custom search box in the Webview
- Loading branch information
Showing
22 changed files
with
302 additions
and
5 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- Copyright (C) 2014 The Android Open Source Project | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
|
||
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:src="@drawable/ic_find_next_mtrl_alpha" | ||
android:tint="?attr/colorControlNormal" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<!-- Copyright (C) 2014 The Android Open Source Project | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
--> | ||
|
||
<bitmap xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:src="@drawable/ic_find_previous_mtrl_alpha" | ||
android:tint="?attr/colorControlNormal" /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:orientation="horizontal" | ||
> | ||
<EditText android:id="@+id/edit" | ||
android:importantForAutofill="no" | ||
android:layout_width="0dip" | ||
android:layout_weight="1" | ||
android:layout_height="wrap_content" | ||
android:scrollHorizontally="true" | ||
android:inputType="text" | ||
android:hint="@string/find_hint" | ||
android:imeOptions="actionDone|flagNoExtractUi|flagNoFullscreen" | ||
android:layout_marginEnd="10dip" | ||
android:layout_marginRight="10dip" | ||
android:textColor="@android:color/white" | ||
/> | ||
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<menu xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<item android:id="@+id/find_prev" | ||
android:icon="@drawable/ic_find_previous_material" | ||
android:showAsAction="always" | ||
android:title="@string/find_previous" | ||
/> | ||
<item android:id="@+id/find_next" | ||
android:icon="@drawable/ic_find_next_material" | ||
android:showAsAction="always" | ||
android:title="@string/find_next" | ||
/> | ||
</menu> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
/* | ||
* This file is heavily inspired by the Android Open Source Project | ||
* licensed under the Apache License, Version 2.0 | ||
*/ | ||
|
||
package itkach.aard2; | ||
|
||
import android.content.Context; | ||
import android.os.Build; | ||
import android.text.Editable; | ||
import android.text.Selection; | ||
import android.text.Spannable; | ||
import android.text.TextWatcher; | ||
import android.view.ActionMode; | ||
import android.view.LayoutInflater; | ||
import android.view.Menu; | ||
import android.view.MenuItem; | ||
import android.view.View; | ||
import android.view.inputmethod.InputMethodManager; | ||
import android.widget.EditText; | ||
|
||
class FindActionModeCallback implements ActionMode.Callback, TextWatcher, | ||
View.OnLongClickListener, View.OnClickListener { | ||
|
||
private View searchView; | ||
private EditText editText; | ||
private SearchableWebView webview; | ||
private InputMethodManager imManager; | ||
|
||
FindActionModeCallback(Context context, SearchableWebView webview) { | ||
this.webview = webview; | ||
searchView = LayoutInflater.from(context).inflate(R.layout.webview_find, null); | ||
|
||
editText = searchView.findViewById(R.id.edit); | ||
editText.setOnLongClickListener(this); | ||
editText.setOnClickListener(this); | ||
editText.addTextChangedListener(this); | ||
|
||
imManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); | ||
} | ||
|
||
/* Place text in the text field so it can be searched for. */ | ||
void setText(String text) { | ||
editText.setText(text); | ||
Spannable span = (Spannable) editText.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); | ||
} | ||
|
||
/* | ||
* Move the highlight to the next match. | ||
* @param next If true, find the next match further down in the document. | ||
* If false, find the previous match, up in the document. | ||
*/ | ||
private void findNext(boolean next) { | ||
webview.findNext(next); | ||
} | ||
|
||
/* | ||
* Highlight all the instances of the string from editText in webview. | ||
*/ | ||
void findAll() { | ||
String find = editText.getText().toString(); | ||
|
||
if (Build.VERSION.SDK_INT < 16) | ||
webview.findAll(find); | ||
else | ||
webview.findAllAsync(find); | ||
} | ||
|
||
void showSoftInput() { | ||
// imManager.showSoftInputMethod doesn't work | ||
imManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); | ||
} | ||
|
||
// OnLongClickListener implementation | ||
@Override | ||
public boolean onLongClick(View v) { | ||
// Override long click so that select ActionMode is not opened, which | ||
// would exit find ActionMode. | ||
return true; | ||
} | ||
|
||
// OnClickListener implementation | ||
@Override | ||
public void onClick(View v) { | ||
findNext(true); | ||
} | ||
|
||
// ActionMode.Callback implementation | ||
@Override | ||
public boolean onCreateActionMode(ActionMode mode, Menu menu) { | ||
mode.setCustomView(searchView); | ||
mode.getMenuInflater().inflate(R.menu.webview_find, menu); | ||
|
||
Editable edit = editText.getText(); | ||
Selection.setSelection(edit, edit.length()); | ||
editText.requestFocus(); | ||
return true; | ||
} | ||
|
||
@Override | ||
public void onDestroyActionMode(ActionMode mode) { | ||
webview.clearMatches(); | ||
imManager.hideSoftInputFromWindow(webview.getWindowToken(), 0); | ||
webview.setLastFind(editText.getText().toString()); | ||
} | ||
@Override | ||
public boolean onPrepareActionMode(ActionMode mode, Menu menu) { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean onActionItemClicked(ActionMode mode, MenuItem item) { | ||
imManager.hideSoftInputFromWindow(webview.getWindowToken(), 0); | ||
switch(item.getItemId()) { | ||
case R.id.find_prev: | ||
findNext(false); | ||
break; | ||
case R.id.find_next: | ||
findNext(true); | ||
break; | ||
default: | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
// TextWatcher implementation | ||
@Override | ||
public void beforeTextChanged(CharSequence s, int start, int count, int after) { | ||
// Does nothing. Needed to implement TextWatcher. | ||
} | ||
|
||
@Override | ||
public void onTextChanged(CharSequence s, int start, int before, int count) { | ||
findAll(); | ||
} | ||
|
||
@Override | ||
public void afterTextChanged(Editable s) { | ||
// Does nothing. Needed to implement TextWatcher. | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* This file is heavily inspired by the Android Open Source Project | ||
* licensed under the Apache License, Version 2.0 | ||
*/ | ||
|
||
package itkach.aard2; | ||
|
||
import android.content.Context; | ||
import android.util.AttributeSet; | ||
import android.webkit.WebView; | ||
|
||
class SearchableWebView extends WebView { | ||
|
||
private String mLastFind = null; | ||
|
||
public void setLastFind(String find) { | ||
mLastFind = find; | ||
} | ||
|
||
public SearchableWebView(Context context) { | ||
this(context, null); | ||
} | ||
|
||
public SearchableWebView(Context context, AttributeSet attrs) { | ||
super(context, attrs); | ||
} | ||
|
||
/** | ||
* Start an ActionMode for finding text in this WebView. Only works if this | ||
* WebView is attached to the view system. | ||
* | ||
* @param text If non-null, will be the initial text to search for. | ||
* Otherwise, the last String searched for in this WebView will | ||
* be used to start. | ||
* @param showIme If true, show the IME, assuming the user will begin typing. | ||
* If false and text is non-null, perform a find all. | ||
* @return boolean True if the find dialog is shown, false otherwise. | ||
*/ | ||
@Override | ||
public boolean showFindDialog(String text, boolean showIme) { | ||
FindActionModeCallback callback = new FindActionModeCallback(getContext(), this); | ||
if (getParent() == null || startActionMode(callback) == null) { | ||
// Could not start the action mode, so end Find on page | ||
return false; | ||
} | ||
|
||
if (showIme) { | ||
callback.showSoftInput(); | ||
} else if (text != null) { | ||
callback.setText(text); | ||
callback.findAll(); | ||
return true; | ||
} | ||
if (text == null) { | ||
text = mLastFind; | ||
} | ||
if (text != null) { | ||
callback.setText(text); | ||
callback.findAll(); | ||
} | ||
return true; | ||
} | ||
} |