From bc651c21f530065ad574c5fd65c04bbf40872d92 Mon Sep 17 00:00:00 2001 From: hussainmohd-a Date: Tue, 23 Jul 2024 15:41:31 +0530 Subject: [PATCH] Fix: #1111, clear focus on searchview once soft keyboard is hidden --- .../bravedns/ui/activity/AppListActivity.kt | 49 +++++++++++++++++-- app/src/full/res/layout/activity_app_list.xml | 3 ++ 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/app/src/full/java/com/celzero/bravedns/ui/activity/AppListActivity.kt b/app/src/full/java/com/celzero/bravedns/ui/activity/AppListActivity.kt index dc82917d8..84f27fdbb 100644 --- a/app/src/full/java/com/celzero/bravedns/ui/activity/AppListActivity.kt +++ b/app/src/full/java/com/celzero/bravedns/ui/activity/AppListActivity.kt @@ -19,9 +19,12 @@ import android.content.Context import android.content.res.Configuration import android.graphics.PorterDuff import android.graphics.PorterDuffColorFilter +import android.graphics.Rect import android.os.Bundle +import android.util.TypedValue import android.view.LayoutInflater import android.view.View +import android.view.ViewTreeObserver import android.view.animation.Animation import android.view.animation.RotateAnimation import android.widget.CompoundButton @@ -201,6 +204,7 @@ class AppListActivity : override fun onResume() { super.onResume() setFirewallFilter(filters.value?.firewallFilter) + b.ffaAppList.requestFocus() } private fun initObserver() { @@ -239,11 +243,14 @@ class AppListActivity : override fun onPause() { filters.postValue(Filters()) + b.ffaSearch.clearFocus() + b.ffaAppList.requestFocus() super.onPause() } override fun onQueryTextSubmit(query: String): Boolean { addQueryToFilters(query) + b.ffaSearch.clearFocus() return true } @@ -720,6 +727,44 @@ class AppListActivity : b.ffaSearch.setOnQueryTextListener(this) addAnimation() remakeFirewallChipsUi() + handleKeyboardEvent() + } + + private fun handleKeyboardEvent() { + // ref: stackoverflow.com/a/36259261 + val rootView = findViewById(android.R.id.content) + + rootView.viewTreeObserver.addOnGlobalLayoutListener(object : + ViewTreeObserver.OnGlobalLayoutListener { + private var alreadyOpen = false + private val defaultKeyboardHeightDP = 100 + private val EstimatedKeyboardDP = defaultKeyboardHeightDP + 48 + private val rect = Rect() + + override fun onGlobalLayout() { + val estimatedKeyboardHeight = TypedValue.applyDimension( + TypedValue.COMPLEX_UNIT_DIP, + EstimatedKeyboardDP.toFloat(), + rootView.resources.displayMetrics + ).toInt() + rootView.getWindowVisibleDisplayFrame(rect) + val heightDiff = rootView.rootView.height - (rect.bottom - rect.top) + val isShown = heightDiff >= estimatedKeyboardHeight + + if (isShown == alreadyOpen) { + return // nothing to do + } + + alreadyOpen = isShown + + if (!isShown) { + if (b.ffaSearch.hasFocus()) { + // clear focus from search view when keyboard is closed + b.ffaSearch.clearFocus() + } + } + } + }) } private fun initListAdapter() { @@ -762,8 +807,4 @@ class AppListActivity : private fun io(f: suspend () -> Unit) { lifecycleScope.launch(Dispatchers.IO) { f() } } - - private fun ui(f: () -> Unit) { - lifecycleScope.launch(Dispatchers.Main) { f() } - } } diff --git a/app/src/full/res/layout/activity_app_list.xml b/app/src/full/res/layout/activity_app_list.xml index 2bfd3f2e5..02cdba569 100644 --- a/app/src/full/res/layout/activity_app_list.xml +++ b/app/src/full/res/layout/activity_app_list.xml @@ -4,6 +4,8 @@ android:layout_width="match_parent" android:layout_height="match_parent" android:background="?attr/background" + android:descendantFocusability="beforeDescendants" + android:focusableInTouchMode="true" app:layout_behavior="@string/appbar_scrolling_view_behavior">