From f7c757bf33d82b732feb2a508241cb73d5918ce4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20P=C3=B6hn?= Date: Sun, 29 Dec 2019 21:46:29 +0100 Subject: [PATCH] app list: open keyboard when X-ing search query --- .../main/java/org/fdroid/fdroid/Utils.java | 35 +++++++++++++++++++ .../fdroid/views/apps/AppListActivity.java | 32 +++++++++++++++++ app/src/main/res/layout/activity_app_list.xml | 1 + 3 files changed, 68 insertions(+) diff --git a/app/src/main/java/org/fdroid/fdroid/Utils.java b/app/src/main/java/org/fdroid/fdroid/Utils.java index 58611ba37..fe38e0cc1 100644 --- a/app/src/main/java/org/fdroid/fdroid/Utils.java +++ b/app/src/main/java/org/fdroid/fdroid/Utils.java @@ -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; + } + } } diff --git a/app/src/main/java/org/fdroid/fdroid/views/apps/AppListActivity.java b/app/src/main/java/org/fdroid/fdroid/views/apps/AppListActivity.java index 2ba51cdf1..ecca389b6 100644 --- a/app/src/main/java/org/fdroid/fdroid/views/apps/AppListActivity.java +++ b/app/src/main/java/org/fdroid/fdroid/views/apps/AppListActivity.java @@ -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); + } } }); diff --git a/app/src/main/res/layout/activity_app_list.xml b/app/src/main/res/layout/activity_app_list.xml index 2de5dac54..bcae98d02 100644 --- a/app/src/main/res/layout/activity_app_list.xml +++ b/app/src/main/res/layout/activity_app_list.xml @@ -2,6 +2,7 @@