From 3c8b700f7e47bab12bdb4de6b09c03f8a4089b93 Mon Sep 17 00:00:00 2001 From: Yash Garg Date: Sat, 28 Aug 2021 01:42:29 +0530 Subject: Fix auto-focus issue on Dialogs with TextInputFields (#1492) --- .../aps/ui/dialogs/FolderCreationDialogFragment.kt | 4 ++-- .../aps/ui/dialogs/OtpImportDialogFragment.kt | 6 ++---- .../msfjarvis/aps/ui/passwords/PasswordStore.kt | 4 ++-- .../aps/util/extensions/AndroidExtensions.kt | 22 ---------------------- .../aps/util/git/operation/CredentialFinder.kt | 4 ++-- app/src/main/res/layout/folder_dialog_fragment.xml | 2 ++ .../main/res/layout/fragment_manual_otp_entry.xml | 2 ++ app/src/main/res/layout/git_credential_layout.xml | 2 ++ 8 files changed, 14 insertions(+), 32 deletions(-) (limited to 'app/src/main') diff --git a/app/src/main/java/dev/msfjarvis/aps/ui/dialogs/FolderCreationDialogFragment.kt b/app/src/main/java/dev/msfjarvis/aps/ui/dialogs/FolderCreationDialogFragment.kt index 45dfba2b..e8111552 100644 --- a/app/src/main/java/dev/msfjarvis/aps/ui/dialogs/FolderCreationDialogFragment.kt +++ b/app/src/main/java/dev/msfjarvis/aps/ui/dialogs/FolderCreationDialogFragment.kt @@ -7,6 +7,7 @@ package dev.msfjarvis.aps.ui.dialogs import android.app.Dialog import android.content.Intent import android.os.Bundle +import android.view.WindowManager import androidx.activity.result.contract.ActivityResultContracts import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity @@ -23,7 +24,6 @@ import dev.msfjarvis.aps.ui.crypto.BasePgpActivity import dev.msfjarvis.aps.ui.crypto.GetKeyIdsActivity import dev.msfjarvis.aps.ui.passwords.PasswordStore import dev.msfjarvis.aps.util.extensions.commitChange -import dev.msfjarvis.aps.util.extensions.requestInputFocusOnView import java.io.File import kotlinx.coroutines.launch import me.msfjarvis.openpgpktx.util.OpenPgpApi @@ -67,7 +67,7 @@ class FolderCreationDialogFragment : DialogFragment() { alertDialogBuilder.setPositiveButton(getString(R.string.button_create), null) alertDialogBuilder.setNegativeButton(getString(android.R.string.cancel)) { _, _ -> dismiss() } val dialog = alertDialogBuilder.create() - dialog.requestInputFocusOnView(R.id.folder_name_text) + dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE) dialog.setOnShowListener { dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener { createDirectory(requireArguments().getString(CURRENT_DIR_EXTRA)!!) diff --git a/app/src/main/java/dev/msfjarvis/aps/ui/dialogs/OtpImportDialogFragment.kt b/app/src/main/java/dev/msfjarvis/aps/ui/dialogs/OtpImportDialogFragment.kt index 5507218b..1670d263 100644 --- a/app/src/main/java/dev/msfjarvis/aps/ui/dialogs/OtpImportDialogFragment.kt +++ b/app/src/main/java/dev/msfjarvis/aps/ui/dialogs/OtpImportDialogFragment.kt @@ -8,15 +8,13 @@ package dev.msfjarvis.aps.ui.dialogs import android.app.Dialog import android.net.Uri import android.os.Bundle +import android.view.WindowManager import androidx.core.os.bundleOf import androidx.fragment.app.DialogFragment import androidx.fragment.app.setFragmentResult import com.google.android.material.dialog.MaterialAlertDialogBuilder -import com.google.android.material.textfield.TextInputEditText -import dev.msfjarvis.aps.R import dev.msfjarvis.aps.databinding.FragmentManualOtpEntryBinding import dev.msfjarvis.aps.ui.crypto.PasswordCreationActivity -import dev.msfjarvis.aps.util.extensions.requestInputFocusOnView class OtpImportDialogFragment : DialogFragment() { @@ -31,7 +29,7 @@ class OtpImportDialogFragment : DialogFragment() { ) } val dialog = builder.create() - dialog.requestInputFocusOnView(R.id.secret) + dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE) return dialog } diff --git a/app/src/main/java/dev/msfjarvis/aps/ui/passwords/PasswordStore.kt b/app/src/main/java/dev/msfjarvis/aps/ui/passwords/PasswordStore.kt index 38c97f36..a009b701 100644 --- a/app/src/main/java/dev/msfjarvis/aps/ui/passwords/PasswordStore.kt +++ b/app/src/main/java/dev/msfjarvis/aps/ui/passwords/PasswordStore.kt @@ -14,6 +14,7 @@ import android.view.KeyEvent import android.view.Menu import android.view.MenuItem import android.view.MenuItem.OnActionExpandListener +import android.view.WindowManager import androidx.activity.result.contract.ActivityResultContracts.RequestPermission import androidx.activity.result.contract.ActivityResultContracts.StartActivityForResult import androidx.activity.viewModels @@ -56,7 +57,6 @@ import dev.msfjarvis.aps.util.extensions.getString import dev.msfjarvis.aps.util.extensions.isInsideRepository import dev.msfjarvis.aps.util.extensions.isPermissionGranted import dev.msfjarvis.aps.util.extensions.listFilesRecursively -import dev.msfjarvis.aps.util.extensions.requestInputFocusOnView import dev.msfjarvis.aps.util.extensions.sharedPrefs import dev.msfjarvis.aps.util.settings.AuthMode import dev.msfjarvis.aps.util.settings.PreferenceKeys @@ -583,7 +583,7 @@ class PasswordStore : BaseGitActivity() { .setNegativeButton(R.string.dialog_skip, null) .create() - dialog.requestInputFocusOnView(R.id.folder_name_text) + dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE) dialog.show() } diff --git a/app/src/main/java/dev/msfjarvis/aps/util/extensions/AndroidExtensions.kt b/app/src/main/java/dev/msfjarvis/aps/util/extensions/AndroidExtensions.kt index ae175433..0ad122e6 100644 --- a/app/src/main/java/dev/msfjarvis/aps/util/extensions/AndroidExtensions.kt +++ b/app/src/main/java/dev/msfjarvis/aps/util/extensions/AndroidExtensions.kt @@ -15,10 +15,7 @@ import android.util.Base64 import android.util.TypedValue import android.view.View import android.view.autofill.AutofillManager -import android.view.inputmethod.InputMethodManager -import androidx.annotation.IdRes import androidx.annotation.RequiresApi -import androidx.appcompat.app.AlertDialog import androidx.core.content.ContextCompat import androidx.core.content.getSystemService import androidx.fragment.app.FragmentActivity @@ -33,25 +30,6 @@ import dev.msfjarvis.aps.R import dev.msfjarvis.aps.data.repo.PasswordRepository import dev.msfjarvis.aps.util.git.operation.GitOperation -/** - * Extension function for [AlertDialog] that requests focus for the view whose id is [id]. Solution - * based on a StackOverflow answer: https://stackoverflow.com/a/13056259/297261 - */ -fun AlertDialog.requestInputFocusOnView(@IdRes id: Int) { - setOnShowListener { - findViewById(id)?.apply { - setOnFocusChangeListener { v, _ -> - v.post { - context - .getSystemService() - ?.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT) - } - } - requestFocus() - } - } -} - /** Get an instance of [AutofillManager]. Only available on Android Oreo and above */ val Context.autofillManager: AutofillManager? @RequiresApi(Build.VERSION_CODES.O) get() = getSystemService() diff --git a/app/src/main/java/dev/msfjarvis/aps/util/git/operation/CredentialFinder.kt b/app/src/main/java/dev/msfjarvis/aps/util/git/operation/CredentialFinder.kt index 8f9b5efc..623d69ea 100644 --- a/app/src/main/java/dev/msfjarvis/aps/util/git/operation/CredentialFinder.kt +++ b/app/src/main/java/dev/msfjarvis/aps/util/git/operation/CredentialFinder.kt @@ -8,6 +8,7 @@ package dev.msfjarvis.aps.util.git.operation import android.annotation.SuppressLint import android.content.SharedPreferences import android.view.LayoutInflater +import android.view.WindowManager import androidx.annotation.StringRes import androidx.core.content.edit import androidx.core.widget.doOnTextChanged @@ -22,7 +23,6 @@ import dagger.hilt.android.EntryPointAccessors import dagger.hilt.components.SingletonComponent import dev.msfjarvis.aps.R import dev.msfjarvis.aps.injection.prefs.GitPreferences -import dev.msfjarvis.aps.util.extensions.requestInputFocusOnView import dev.msfjarvis.aps.util.git.sshj.InteractivePasswordFinder import dev.msfjarvis.aps.util.settings.AuthMode import dev.msfjarvis.aps.util.settings.PreferenceKeys @@ -100,7 +100,7 @@ class CredentialFinder(val callingActivity: FragmentActivity, val authMode: Auth create() } .run { - requestInputFocusOnView(R.id.git_auth_credential) + window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE) show() } } else { diff --git a/app/src/main/res/layout/folder_dialog_fragment.xml b/app/src/main/res/layout/folder_dialog_fragment.xml index 28a8287f..21d34805 100644 --- a/app/src/main/res/layout/folder_dialog_fragment.xml +++ b/app/src/main/res/layout/folder_dialog_fragment.xml @@ -25,6 +25,8 @@ android:layout_height="wrap_content" android:inputType="textNoSuggestions|textVisiblePassword" /> + + + + + +