Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix keyboard crashes in BrowserFragment #5033

Merged
merged 6 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,9 @@ import com.duckduckgo.common.utils.ConflatedJob
import com.duckduckgo.common.utils.DispatcherProvider
import com.duckduckgo.common.utils.FragmentViewModelFactory
import com.duckduckgo.common.utils.KeyboardVisibilityUtil
import com.duckduckgo.common.utils.extensions.hideKeyboard
import com.duckduckgo.common.utils.extensions.html
import com.duckduckgo.common.utils.extensions.showKeyboard
import com.duckduckgo.common.utils.extensions.toBinaryString
import com.duckduckgo.common.utils.extensions.websiteFromGeoLocationsApiOrigin
import com.duckduckgo.common.utils.extractDomain
Expand Down Expand Up @@ -3080,7 +3082,7 @@ class BrowserTabFragment :
private fun hideKeyboard() {
if (!isHidden) {
Timber.v("Keyboard now hiding")
binding.legacyOmnibar.omnibarTextInput.postDelayed(KEYBOARD_DELAY) { binding.legacyOmnibar.omnibarTextInput?.hideKeyboard() }
hideKeyboard(binding.legacyOmnibar.omnibarTextInput)
binding.focusDummy.requestFocus()
binding.legacyOmnibar.omniBarContainer.isPressed = false
}
Expand All @@ -3096,7 +3098,7 @@ class BrowserTabFragment :
private fun showKeyboard() {
if (!isHidden) {
Timber.v("Keyboard now showing")
binding.legacyOmnibar.omnibarTextInput.postDelayed(KEYBOARD_DELAY) { binding.legacyOmnibar.omnibarTextInput?.showKeyboard() }
showKeyboard(binding.legacyOmnibar.omnibarTextInput)
binding.legacyOmnibar.omniBarContainer.isPressed = true
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ fun View.gone(): View {
}

/** Extension method to show a keyboard for View. */
@Deprecated(
"This may be unreliable. Use the showKeyboard() extension function from an Activity or Fragment instead.",
ReplaceWith("Activity.showKeyboard(editText)"),
)
fun View.showKeyboard() {
val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
this.requestFocus()
Expand All @@ -68,6 +72,10 @@ fun View.showKeyboard() {
* Try to hide the keyboard and returns whether it worked
* https://stackoverflow.com/questions/1109022/close-hide-the-android-soft-keyboard
*/
@Deprecated(
"This may be unreliable. Use the hideKeyboard() extension function from an Activity or Fragment instead.",
ReplaceWith("Activity.hideKeyboard(editText)"),
)
fun View.hideKeyboard(): Boolean {
try {
val inputMethodManager =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@
package com.duckduckgo.common.utils.extensions

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.provider.Settings
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat

/**
* Deep links to the application App Info settings
Expand Down Expand Up @@ -71,3 +75,13 @@ fun AppCompatActivity.launchIgnoreBatteryOptimizationSettings(): Boolean {

return true
}

fun Activity.showKeyboard(editText: EditText) {
editText.requestFocus()
WindowInsetsControllerCompat(window, editText).show(WindowInsetsCompat.Type.ime())
}

fun Activity.hideKeyboard(editText: EditText) {
editText.requestFocus()
WindowInsetsControllerCompat(window, editText).hide(WindowInsetsCompat.Type.ime())
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2024 DuckDuckGo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.duckduckgo.common.utils.extensions

import android.widget.EditText
import androidx.fragment.app.Fragment

fun Fragment.showKeyboard(editText: EditText) = activity?.showKeyboard(editText)

fun Fragment.hideKeyboard(editText: EditText) = activity?.hideKeyboard(editText)
Loading