Merge branch 'close-search-keyboard' into 'master'

activity list: open keyboard when clearing search term

Closes #1915

See merge request fdroid/fdroidclient!858
This commit is contained in:
Hans-Christoph Steiner 2020-01-02 13:59:51 +00:00
commit 25262e4463
3 changed files with 68 additions and 0 deletions

View File

@ -1,5 +1,6 @@
/*
* Copyright (C) 2010-12 Ciaran Gultnieks, ciaran@ciarang.com
* Copyright (C) 2019 Michael Pöhn, michael.poehn@fsfe.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@ -25,6 +26,7 @@ import android.content.pm.Signature;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
@ -45,6 +47,8 @@ import android.text.style.TypefaceSpan;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.Toast;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
@ -912,4 +916,35 @@ public final class Utils {
return true;
}
}
/**
* Keep an instance of this class as an field in an Activity for figuring out whether the on
* screen keyboard is currently visible or not.
*/
public static class KeyboardStateMonitor {
private boolean visible = false;
/**
* @param contentView this must be the top most Container of the layout used by the Activity
*/
public KeyboardStateMonitor(final View contentView) {
contentView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int screenHeight = contentView.getRootView().getHeight();
Rect rect = new Rect();
contentView.getWindowVisibleDisplayFrame(rect);
int keypadHeight = screenHeight - rect.bottom;
visible = keypadHeight >= screenHeight * 0.15;
}
}
);
}
public boolean isKeyboardVisible() {
return visible;
}
}
}

View File

@ -1,5 +1,26 @@
/*
* Copyright (C) 2016-17 Peter Serwylo,
* Copyright (C) 2017-18 Hans-Christoph Steiner
* Copyright (C) 2019 Michael Pöhn, michael.poehn@fsfe.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 3
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.fdroid.fdroid.views.apps;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
@ -21,6 +42,7 @@ import android.widget.TextView;
import com.nostra13.universalimageloader.core.ImageLoader;
import org.fdroid.fdroid.FDroidApp;
import org.fdroid.fdroid.R;
import org.fdroid.fdroid.Utils;
import org.fdroid.fdroid.data.AppProvider;
import org.fdroid.fdroid.data.Schema;
@ -43,6 +65,7 @@ public class AppListActivity extends AppCompatActivity implements LoaderManager.
private TextView emptyState;
private EditText searchInput;
private ImageView sortImage;
private Utils.KeyboardStateMonitor keyboardStateMonitor;
private interface SortClause {
String NAME = Schema.AppMetadataTable.NAME + "." + Schema.AppMetadataTable.Cols.NAME + " asc";
@ -57,6 +80,8 @@ public class AppListActivity extends AppCompatActivity implements LoaderManager.
setContentView(R.layout.activity_app_list);
keyboardStateMonitor = new Utils.KeyboardStateMonitor(findViewById(R.id.app_list_root));
searchInput = (EditText) findViewById(R.id.search);
searchInput.addTextChangedListener(new CategoryTextWatcher(this, searchInput, this));
searchInput.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@ -119,6 +144,13 @@ public class AppListActivity extends AppCompatActivity implements LoaderManager.
@Override
public void onClick(View v) {
searchInput.setText("");
searchInput.requestFocus();
if (!keyboardStateMonitor.isKeyboardVisible()) {
InputMethodManager inputMethodManager =
(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.toggleSoftInputFromWindow(v.getApplicationWindowToken(),
InputMethodManager.SHOW_FORCED, 0);
}
}
});

View File

@ -2,6 +2,7 @@
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/app_list_root"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">