Skip to content

Commit

Permalink
Fix: #1111, clear focus on searchview once soft keyboard is hidden
Browse files Browse the repository at this point in the history
  • Loading branch information
hussainmohd-a committed Jul 23, 2024
1 parent b03a709 commit bc651c2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -201,6 +204,7 @@ class AppListActivity :
override fun onResume() {
super.onResume()
setFirewallFilter(filters.value?.firewallFilter)
b.ffaAppList.requestFocus()
}

private fun initObserver() {
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -720,6 +727,44 @@ class AppListActivity :
b.ffaSearch.setOnQueryTextListener(this)
addAnimation()
remakeFirewallChipsUi()
handleKeyboardEvent()
}

private fun handleKeyboardEvent() {
// ref: stackoverflow.com/a/36259261
val rootView = findViewById<View>(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() {
Expand Down Expand Up @@ -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() }
}
}
3 changes: 3 additions & 0 deletions app/src/full/res/layout/activity_app_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">

<LinearLayout
Expand Down Expand Up @@ -38,6 +40,7 @@
android:layout_gravity="start"
android:layout_weight="0.8"
android:fontFamily="sans-serif"
android:focusable="false"
android:isScrollContainer="true"
app:iconifiedByDefault="false"
app:queryHint="@string/search_firewall_all_apps" />
Expand Down

0 comments on commit bc651c2

Please sign in to comment.