Skip to content

Commit

Permalink
Merge branch 'master' of github.com:itkach/aard2-android
Browse files Browse the repository at this point in the history
  • Loading branch information
itkach committed Apr 19, 2020
2 parents 67b9dbf + c8acd2b commit 1429053
Show file tree
Hide file tree
Showing 22 changed files with 302 additions and 5 deletions.
Binary file added res/drawable-hdpi/ic_find_next_mtrl_alpha.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-hdpi/ic_find_previous_mtrl_alpha.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-mdpi/ic_find_next_mtrl_alpha.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-mdpi/ic_find_previous_mtrl_alpha.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-xhdpi/ic_find_next_mtrl_alpha.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-xhdpi/ic_find_previous_mtrl_alpha.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-xxhdpi/ic_find_next_mtrl_alpha.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added res/drawable-xxhdpi/ic_find_previous_mtrl_alpha.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
19 changes: 19 additions & 0 deletions res/drawable/ic_find_next_material.xml
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" />
19 changes: 19 additions & 0 deletions res/drawable/ic_find_previous_material.xml
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" />
20 changes: 20 additions & 0 deletions res/layout/webview_find.xml
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>
13 changes: 13 additions & 0 deletions res/menu/webview_find.xml
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>
1 change: 1 addition & 0 deletions res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,5 @@
<string name="action_load_remote_content">Lade externen Inhalt</string>
<string name="action_fullscreen">Vollbild</string>
<string name="setting_use_volume_for_nav">Lautstärketasten für die Navigation verwenden</string>
<string name="find_hint">Suche in Seite</string>
</resources>
3 changes: 3 additions & 0 deletions res/values-es/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,8 @@
<string name="action_fullscreen">Pantalla completa</string>
<string name="setting_use_volume_for_nav">Usar botones de volumen para navegación</string>
<string name="msg_unable_to_function_without_storage_access">Acceso de archivo denegado, saliendo...</string>
<string name="find_hint">Buscar en la página</string>
<string name="find_next">Siguiente</string>
<string name="find_previous">Anterior"</string>

</resources>
3 changes: 3 additions & 0 deletions res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,8 @@
<string name="auto_style_title">Auto</string>
<string name="action_fullscreen">Plein écran</string>
<string name="setting_use_volume_for_nav">Utiliser les boutons du volume +/- pour haut/bas</string>
<string name="find_hint">Rechercher sur la page</string>
<string name="find_next">Suivant</string>
<string name="find_previous">Précédent</string>

</resources>
9 changes: 6 additions & 3 deletions res/values-it/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ is missing a quantity used in that language, otherwise the app will crash
<string name="setting_ui_theme">Stile interfaccia utente</string>
<string name="setting_ui_theme_light">Chiaro</string>
<string name="setting_ui_theme_dark">Scuro</string>
<string name="action_load_remote_content">Carica contenuto remoto</string>
<string name="action_fullscreen">Schermo intero</string>
<string name="setting_use_volume_for_nav">Usare i tasti del volume per la navigazione</string>
<string name="action_load_remote_content">Carica contenuto remoto</string>
<string name="action_fullscreen">Schermo intero</string>
<string name="setting_use_volume_for_nav">Usare i tasti del volume per la navigazione</string>
<string name="find_hint">Trova nella pagina</string>
<string name="find_next">Successivo</string>
<string name="find_previous">Precedente</string>
</resources>
1 change: 1 addition & 0 deletions res/values-ru/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,5 @@
<string name="action_load_remote_content">Загрузить удалённый контент</string>
<string name="action_fullscreen">Весь экран</string>
<string name="setting_use_volume_for_nav">Использовать кнопки громкости для навигации</string>
<string name="find_hint">Поиск на странице</string>
</resources>
3 changes: 3 additions & 0 deletions res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,7 @@
<string name="action_fullscreen">Full Screen</string>
<string name="setting_use_volume_for_nav">Use volume buttons for navigation</string>
<string name="msg_unable_to_function_without_storage_access">File access was not allowed, quitting...</string>
<string name="find_hint">Find in page</string>
<string name="find_next">Next</string>
<string name="find_previous">Previous</string>
</resources>
1 change: 0 additions & 1 deletion src/itkach/aard2/ArticleFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ private void displayBookmarked(boolean value) {
}
}

@SuppressWarnings("deprecation")
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int itemId = item.getItemId();
Expand Down
2 changes: 1 addition & 1 deletion src/itkach/aard2/ArticleWebView.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import java.util.TimerTask;
import java.util.TreeSet;

public class ArticleWebView extends WebView {
public class ArticleWebView extends SearchableWebView {

public static final String LOCALHOST = Application.LOCALHOST;
private final String styleSwitcherJs;
Expand Down
150 changes: 150 additions & 0 deletions src/itkach/aard2/FindActionModeCallback.java
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.
}
}
63 changes: 63 additions & 0 deletions src/itkach/aard2/SearchableWebView.java
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;
}
}

0 comments on commit 1429053

Please sign in to comment.