Skip to content

Commit

Permalink
feat: Switch keyboard only if focus #1351
Browse files Browse the repository at this point in the history
  • Loading branch information
J-Jamet committed Jul 28, 2023
1 parent a98fa0d commit 215cc64
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.TextView
Expand All @@ -49,6 +49,7 @@ import androidx.appcompat.view.ActionMode
import androidx.appcompat.widget.SearchView
import androidx.appcompat.widget.Toolbar
import androidx.coordinatorlayout.widget.CoordinatorLayout
import androidx.core.content.ContextCompat
import androidx.core.view.GravityCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
Expand Down Expand Up @@ -157,6 +158,7 @@ class GroupActivity : DatabaseLockActivity(),

// Manage group
private var mSearchState: SearchState? = null
private var mAutoSearch: Boolean = false // To mainly manage keyboard
private var mMainGroupState: GroupState? = null // Group state, not a search
private var mRootGroup: Group? = null // Root group in the tree
private var mMainGroup: Group? = null // Main group currently in memory
Expand Down Expand Up @@ -202,12 +204,6 @@ class GroupActivity : DatabaseLockActivity(),
searchFiltersView?.onParametersChangeListener = mOnSearchFiltersChangeListener

addSearch()
//loadGroup()

// Back to previous keyboard
if (PreferencesUtil.isKeyboardPreviousSearchEnable(this@GroupActivity)) {
sendBroadcast(Intent(BACK_PREVIOUS_KEYBOARD_ACTION))
}
return true
}

Expand All @@ -221,6 +217,16 @@ class GroupActivity : DatabaseLockActivity(),
return true
}
}
private val mOnSearchTextFocusChangeListener = View.OnFocusChangeListener { view, hasFocus ->
if (!mAutoSearch
&& hasFocus
&& PreferencesUtil.isKeyboardPreviousSearchEnable(this@GroupActivity)) {
// Change to the previous keyboard and show it
sendBroadcast(Intent(BACK_PREVIOUS_KEYBOARD_ACTION))
ContextCompat.getSystemService(this, InputMethodManager::class.java)
?.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
}
}

private fun addSearch() {
finishNodeAction()
Expand Down Expand Up @@ -735,6 +741,7 @@ class GroupActivity : DatabaseLockActivity(),
transformSearchInfoIntent(intent)
// Get search query
if (intent.action == Intent.ACTION_SEARCH) {
mAutoSearch = true
val stringQuery = intent.getStringExtra(SearchManager.QUERY)?.trim { it <= ' ' } ?: ""
intent.action = Intent.ACTION_DEFAULT
intent.removeExtra(SearchManager.QUERY)
Expand Down Expand Up @@ -1146,6 +1153,8 @@ class GroupActivity : DatabaseLockActivity(),

private fun addSearchQueryInSearchView(searchQuery: String) {
searchView?.setOnQueryTextListener(null)
if (mAutoSearch)
searchView?.clearFocus()
searchView?.setQuery(searchQuery, false)
searchView?.setOnQueryTextListener(mOnSearchQueryTextListener)
}
Expand Down Expand Up @@ -1192,6 +1201,7 @@ class GroupActivity : DatabaseLockActivity(),
it.setOnActionExpandListener(mOnSearchActionExpandListener)
searchView = it.actionView as SearchView?
searchView?.apply {
setOnQueryTextFocusChangeListener(mOnSearchTextFocusChangeListener)
val searchManager = getSystemService(Context.SEARCH_SERVICE) as SearchManager?
(searchManager?.getSearchableInfo(
ComponentName(this@GroupActivity, GroupActivity::class.java)
Expand All @@ -1214,6 +1224,7 @@ class GroupActivity : DatabaseLockActivity(),
breadcrumbListView?.visibility = View.VISIBLE
}
mLockSearchListeners = false
mAutoSearch = false
}

super.onCreateOptionsMenu(menu)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ class MagikeyboardService : InputMethodService(), KeyboardView.OnKeyboardActionL
private fun switchToPreviousKeyboard() {
var imeManager: InputMethodManager? = null
try {
imeManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
imeManager = ContextCompat.getSystemService(this, InputMethodManager::class.java)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
switchToPreviousInputMethod()
} else {
Expand Down Expand Up @@ -270,7 +270,7 @@ class MagikeyboardService : InputMethodService(), KeyboardView.OnKeyboardActionL
KEY_BACK_KEYBOARD -> switchToPreviousKeyboard()

KEY_CHANGE_KEYBOARD -> {
(getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?)
ContextCompat.getSystemService(this, InputMethodManager::class.java)
?.showInputMethodPicker()
}
KEY_ENTRY -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import android.widget.EditText
import android.widget.FrameLayout
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.activities.helpers.ExternalFileHelper
import com.kunzisoft.keepass.activities.helpers.setOpenDocumentClickListener
Expand Down Expand Up @@ -230,8 +231,8 @@ class MainCredentialView @JvmOverloads constructor(context: Context,
fun focusPasswordFieldAndOpenKeyboard() {
passwordTextView.postDelayed({
passwordTextView.requestFocusFromTouch()
val inputMethodManager = context.getSystemService(AppCompatActivity.INPUT_METHOD_SERVICE) as? InputMethodManager?
inputMethodManager?.showSoftInput(passwordTextView, InputMethodManager.SHOW_IMPLICIT)
ContextCompat.getSystemService(context, InputMethodManager::class.java)
?.showSoftInput(passwordTextView, InputMethodManager.SHOW_IMPLICIT)
}, 100)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import android.view.inputmethod.InputMethodManager
import android.widget.FrameLayout
import android.widget.ImageView
import androidx.annotation.IdRes
import androidx.core.content.ContextCompat
import com.kunzisoft.keepass.R
import com.kunzisoft.keepass.database.element.DateInstant
import com.kunzisoft.keepass.database.element.Field
Expand Down Expand Up @@ -101,7 +102,7 @@ abstract class TemplateAbstractView<
}
buildTemplateAndPopulateInfo()
clearFocus()
(context.getSystemService(Activity.INPUT_METHOD_SERVICE) as? InputMethodManager?)
ContextCompat.getSystemService(context, InputMethodManager::class.java)
?.hideSoftInputFromWindow(windowToken, 0)
}
}
Expand Down

0 comments on commit 215cc64

Please sign in to comment.