From 2b7aa5e8030d731c9237a5168965f405ba239023 Mon Sep 17 00:00:00 2001 From: agnostic-apollo Date: Fri, 30 Jul 2021 00:32:46 +0500 Subject: [PATCH] Fix issue where wrong IME inputType would be set if termux was returned to from another app with text input view mode selected --- .../java/com/termux/app/TermuxActivity.java | 23 +++++++++--- .../terminal/TermuxTerminalViewClient.java | 5 +++ .../java/com/termux/view/TerminalView.java | 35 ++++++++++++------- .../com/termux/view/TerminalViewClient.java | 2 ++ .../TermuxTerminalViewClientBase.java | 5 +++ 5 files changed, 53 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/termux/app/TermuxActivity.java b/app/src/main/java/com/termux/app/TermuxActivity.java index 729a636ae2..5981778441 100644 --- a/app/src/main/java/com/termux/app/TermuxActivity.java +++ b/app/src/main/java/com/termux/app/TermuxActivity.java @@ -452,7 +452,7 @@ private void setTermuxSessionsListView() { private void setTerminalToolbarView(Bundle savedInstanceState) { - final ViewPager terminalToolbarViewPager = findViewById(R.id.terminal_toolbar_view_pager); + final ViewPager terminalToolbarViewPager = getTerminalToolbarViewPager(); if (mPreferences.shouldShowTerminalToolbar()) terminalToolbarViewPager.setVisibility(View.VISIBLE); ViewGroup.LayoutParams layoutParams = terminalToolbarViewPager.getLayoutParams(); @@ -469,8 +469,9 @@ private void setTerminalToolbarView(Bundle savedInstanceState) { } private void setTerminalToolbarHeight() { - final ViewPager terminalToolbarViewPager = findViewById(R.id.terminal_toolbar_view_pager); + final ViewPager terminalToolbarViewPager = getTerminalToolbarViewPager(); if (terminalToolbarViewPager == null) return; + ViewGroup.LayoutParams layoutParams = terminalToolbarViewPager.getLayoutParams(); layoutParams.height = (int) Math.round(mTerminalToolbarDefaultHeight * (mProperties.getExtraKeysInfo() == null ? 0 : mProperties.getExtraKeysInfo().getMatrix().length) * @@ -479,13 +480,13 @@ private void setTerminalToolbarHeight() { } public void toggleTerminalToolbar() { - final ViewPager terminalToolbarViewPager = findViewById(R.id.terminal_toolbar_view_pager); + final ViewPager terminalToolbarViewPager = getTerminalToolbarViewPager(); if (terminalToolbarViewPager == null) return; final boolean showNow = mPreferences.toogleShowTerminalToolbar(); Logger.showToast(this, (showNow ? getString(R.string.msg_enabling_terminal_toolbar) : getString(R.string.msg_disabling_terminal_toolbar)), true); terminalToolbarViewPager.setVisibility(showNow ? View.VISIBLE : View.GONE); - if (showNow && terminalToolbarViewPager.getCurrentItem() == 1) { + if (showNow && isTerminalToolbarTextInputViewSelected()) { // Focus the text input view if just revealed. findViewById(R.id.terminal_toolbar_text_input).requestFocus(); } @@ -744,6 +745,20 @@ public DrawerLayout getDrawer() { return (DrawerLayout) findViewById(R.id.drawer_layout); } + + public ViewPager getTerminalToolbarViewPager() { + return (ViewPager) findViewById(R.id.terminal_toolbar_view_pager); + } + + public boolean isTerminalViewSelected() { + return getTerminalToolbarViewPager().getCurrentItem() == 0; + } + + public boolean isTerminalToolbarTextInputViewSelected() { + return getTerminalToolbarViewPager().getCurrentItem() == 1; + } + + public void termuxSessionListNotifyUpdated() { mTermuxSessionListViewController.notifyDataSetChanged(); } diff --git a/app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java b/app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java index 0853a80f26..705838acaf 100644 --- a/app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java +++ b/app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java @@ -186,6 +186,11 @@ public boolean shouldUseCtrlSpaceWorkaround() { return mActivity.getProperties().isUsingCtrlSpaceWorkaround(); } + @Override + public boolean isTerminalViewSelected() { + return mActivity.getTerminalToolbarViewPager() == null || mActivity.isTerminalViewSelected(); + } + @Override diff --git a/terminal-view/src/main/java/com/termux/view/TerminalView.java b/terminal-view/src/main/java/com/termux/view/TerminalView.java index 432fea80e3..ef67b1c1e2 100644 --- a/terminal-view/src/main/java/com/termux/view/TerminalView.java +++ b/terminal-view/src/main/java/com/termux/view/TerminalView.java @@ -262,20 +262,29 @@ public boolean attachSession(TerminalSession session) { @Override public InputConnection onCreateInputConnection(EditorInfo outAttrs) { - if (mClient.shouldEnforceCharBasedInput()) { - // Some keyboards seems do not reset the internal state on TYPE_NULL. - // Affects mostly Samsung stock keyboards. - // https://github.com/termux/termux-app/issues/686 - outAttrs.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS; + // Ensure that inputType is only set if TerminalView is selected view with the keyboard and + // an alternate view is not selected, like an EditText. This is necessary if an activity is + // initially started with the alternate view or if activity is returned to from another app + // and the alternate view was the one selected the last time. + if (mClient.isTerminalViewSelected()) { + if (mClient.shouldEnforceCharBasedInput()) { + // Some keyboards seems do not reset the internal state on TYPE_NULL. + // Affects mostly Samsung stock keyboards. + // https://github.com/termux/termux-app/issues/686 + outAttrs.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS; + } else { + // Using InputType.NULL is the most correct input type and avoids issues with other hacks. + // + // Previous keyboard issues: + // https://github.com/termux/termux-packages/issues/25 + // https://github.com/termux/termux-app/issues/87. + // https://github.com/termux/termux-app/issues/126. + // https://github.com/termux/termux-app/issues/137 (japanese chars and TYPE_NULL). + outAttrs.inputType = InputType.TYPE_NULL; + } } else { - // Using InputType.NULL is the most correct input type and avoids issues with other hacks. - // - // Previous keyboard issues: - // https://github.com/termux/termux-packages/issues/25 - // https://github.com/termux/termux-app/issues/87. - // https://github.com/termux/termux-app/issues/126. - // https://github.com/termux/termux-app/issues/137 (japanese chars and TYPE_NULL). - outAttrs.inputType = InputType.TYPE_NULL; + // Corresponds to android:inputType="text" + outAttrs.inputType = InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_NORMAL; } // Note that IME_ACTION_NONE cannot be used as that makes it impossible to input newlines using the on-screen diff --git a/terminal-view/src/main/java/com/termux/view/TerminalViewClient.java b/terminal-view/src/main/java/com/termux/view/TerminalViewClient.java index c1f9f94a86..bd60d2d3f6 100644 --- a/terminal-view/src/main/java/com/termux/view/TerminalViewClient.java +++ b/terminal-view/src/main/java/com/termux/view/TerminalViewClient.java @@ -34,6 +34,8 @@ public interface TerminalViewClient { boolean shouldUseCtrlSpaceWorkaround(); + boolean isTerminalViewSelected(); + void copyModeChanged(boolean copyMode); diff --git a/termux-shared/src/main/java/com/termux/shared/terminal/TermuxTerminalViewClientBase.java b/termux-shared/src/main/java/com/termux/shared/terminal/TermuxTerminalViewClientBase.java index 7f0648d8df..a51ca31b1e 100644 --- a/termux-shared/src/main/java/com/termux/shared/terminal/TermuxTerminalViewClientBase.java +++ b/termux-shared/src/main/java/com/termux/shared/terminal/TermuxTerminalViewClientBase.java @@ -33,6 +33,11 @@ public boolean shouldUseCtrlSpaceWorkaround() { return false; } + @Override + public boolean isTerminalViewSelected() { + return true; + } + @Override public void copyModeChanged(boolean copyMode) { }