diff --git a/app/src/main/java/com/termux/app/TermuxActivity.java b/app/src/main/java/com/termux/app/TermuxActivity.java index 1381998e..3942ea39 100644 --- a/app/src/main/java/com/termux/app/TermuxActivity.java +++ b/app/src/main/java/com/termux/app/TermuxActivity.java @@ -236,7 +236,7 @@ public final class TermuxActivity extends Activity implements ServiceConnection public void onResume() { super.onResume(); - mTermuxTerminalViewClient.setSoftKeyboardState(true); + mTermuxTerminalViewClient.setSoftKeyboardState(true, false); } /** @@ -768,7 +768,7 @@ public final class TermuxActivity extends Activity implements ServiceConnection setTerminalToolbarHeight(); - mTermuxTerminalViewClient.setSoftKeyboardState(true); + mTermuxTerminalViewClient.setSoftKeyboardState(false, true); // To change the activity and drawer theme, activity needs to be recreated. // But this will destroy the activity, and will call the onCreate() again. 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 2fe87e92..93e387d1 100644 --- a/app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java +++ b/app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java @@ -54,6 +54,8 @@ public class TermuxTerminalViewClient extends TermuxTerminalViewClientBase { /** Keeping track of the special keys acting as Ctrl and Fn for the soft keyboard and other hardware keys. */ boolean mVirtualControlKeyDown, mVirtualFnKeyDown; + private Runnable mShowSoftKeyboardRunnable; + private static final String LOG_TAG = "TermuxTerminalViewClient"; public TermuxTerminalViewClient(TermuxActivity activity, TermuxTerminalSessionClient termuxTerminalSessionClient) { @@ -77,6 +79,8 @@ public class TermuxTerminalViewClient extends TermuxTerminalViewClientBase { public void onSingleTapUp(MotionEvent e) { if (!KeyboardUtils.areDisableSoftKeyboardFlagsSet(mActivity)) KeyboardUtils.showSoftKeyboard(mActivity, mActivity.getTerminalView()); + else + Logger.logVerbose(LOG_TAG, "Not showing keyboard onSingleTapUp since its disabled"); } @Override @@ -342,23 +346,40 @@ public class TermuxTerminalViewClient extends TermuxTerminalViewClientBase { - /** * Called when user requests the soft keyboard to be toggled via "KEYBOARD" toggle button in * drawer or extra keys, or with ctrl+alt+k hardware keyboard shortcut. */ public void onToggleSoftKeyboardRequest() { - // If soft keyboard is disabled by user for Termux - if (!mActivity.getPreferences().getSoftKeyboardEnabled()) { - KeyboardUtils.disableSoftKeyboard(mActivity, mActivity.getTerminalView()); - } else { - KeyboardUtils.clearDisableSoftKeyboardFlags(mActivity); - KeyboardUtils.toggleSoftKeyboard(mActivity); + // If soft keyboard toggle behaviour is enable/disabled + if (mActivity.getProperties().shouldEnableDisableSoftKeyboardOnToggle()) { + // If soft keyboard is visible + if (!KeyboardUtils.areDisableSoftKeyboardFlagsSet(mActivity)) { + Logger.logVerbose(LOG_TAG, "Disabling soft keyboard on toggle"); + mActivity.getPreferences().setSoftKeyboardEnabled(false); + KeyboardUtils.disableSoftKeyboard(mActivity, mActivity.getTerminalView()); + } else { + Logger.logVerbose(LOG_TAG, "Enabling soft keyboard on toggle"); + mActivity.getPreferences().setSoftKeyboardEnabled(true); + KeyboardUtils.clearDisableSoftKeyboardFlags(mActivity); + KeyboardUtils.showSoftKeyboard(mActivity, mActivity.getTerminalView()); + } + } + // If soft keyboard toggle behaviour is show/hide + else { + // If soft keyboard is disabled by user for Termux + if (!mActivity.getPreferences().getSoftKeyboardEnabled()) { + Logger.logVerbose(LOG_TAG, "Maintaining disabled soft keyboard on toggle"); + KeyboardUtils.disableSoftKeyboard(mActivity, mActivity.getTerminalView()); + } else { + Logger.logVerbose(LOG_TAG, "Showing/Hiding soft keyboard on toggle"); + KeyboardUtils.clearDisableSoftKeyboardFlags(mActivity); + KeyboardUtils.toggleSoftKeyboard(mActivity); + } } - } - public void setSoftKeyboardState(boolean isStartup) { + public void setSoftKeyboardState(boolean isStartup, boolean isReloadTermuxProperties) { // If soft keyboard is disabled by user for Termux if (!mActivity.getPreferences().getSoftKeyboardEnabled()) { Logger.logVerbose(LOG_TAG, "Maintaining disabled soft keyboard"); @@ -374,19 +395,25 @@ public class TermuxTerminalViewClient extends TermuxTerminalViewClientBase { if (isStartup && mActivity.getProperties().shouldSoftKeyboardBeHiddenOnStartup()) { Logger.logVerbose(LOG_TAG, "Hiding soft keyboard on startup"); KeyboardUtils.hideSoftKeyboard(mActivity, mActivity.getTerminalView()); + // Required to keep keyboard hidden when Termux app is switched back from another app + KeyboardUtils.setSoftKeyboardAlwaysHiddenFlags(mActivity); } else { - // Force show soft keyboard - Logger.logVerbose(LOG_TAG, "Showing soft keyboard"); - KeyboardUtils.showSoftKeyboard(mActivity, mActivity.getTerminalView()); + // Do not force show soft keyboard if termux-reload-settings command was run with hardware keyboard + if (isReloadTermuxProperties) + return; + + if (mShowSoftKeyboardRunnable == null) { + mShowSoftKeyboardRunnable = () -> { + Logger.logVerbose(LOG_TAG, "Showing soft keyboard on focus change"); + KeyboardUtils.showSoftKeyboard(mActivity, mActivity.getTerminalView()); + }; + } mActivity.getTerminalView().setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View view, boolean hasFocus) { - if (hasFocus) { - KeyboardUtils.showSoftKeyboard(mActivity, mActivity.getTerminalView()); - } else { - KeyboardUtils.hideSoftKeyboard(mActivity, mActivity.getTerminalView()); - } + // Force show soft keyboard if TerminalView has focus and close it if it doesn't + KeyboardUtils.setSoftKeyboardVisibility(mShowSoftKeyboardRunnable, mActivity, mActivity.getTerminalView(), hasFocus); } }); diff --git a/termux-shared/src/main/java/com/termux/shared/settings/properties/TermuxPropertyConstants.java b/termux-shared/src/main/java/com/termux/shared/settings/properties/TermuxPropertyConstants.java index 588db5d2..3e1c6218 100644 --- a/termux-shared/src/main/java/com/termux/shared/settings/properties/TermuxPropertyConstants.java +++ b/termux-shared/src/main/java/com/termux/shared/settings/properties/TermuxPropertyConstants.java @@ -10,7 +10,7 @@ import java.util.HashSet; import java.util.Set; /* - * Version: v0.6.0 + * Version: v0.7.0 * * Changelog * @@ -33,6 +33,9 @@ import java.util.Set; * * - 0.6.0 (2021-04-07) * - Updated javadocs. + * + * - 0.7.0 (2021-05-09) + * - Add `*SOFT_KEYBOARD_TOGGLE_BEHAVIOUR*`. */ /** @@ -166,6 +169,15 @@ public final class TermuxPropertyConstants { + /** Defines the key for whether toggle soft keyboard request will show/hide or enable/disable keyboard */ + public static final String SOFT_KEYBOARD_TOGGLE_BEHAVIOUR = "soft-keyboard-toggle-behaviour"; // Default: "soft-keyboard-toggle-behaviour" + + public static final String IVALUE_SOFT_KEYBOARD_TOGGLE_BEHAVIOUR_SHOW_HIDE = "show/hide"; + public static final String IVALUE_SOFT_KEYBOARD_TOGGLE_BEHAVIOUR_ENABLE_DISABLE = "enable/disable"; + public static final String DEFAULT_IVALUE_SOFT_KEYBOARD_TOGGLE_BEHAVIOUR = IVALUE_SOFT_KEYBOARD_TOGGLE_BEHAVIOUR_SHOW_HIDE; + + + /** Defines the set for keys loaded by termux @@ -198,7 +210,8 @@ public final class TermuxPropertyConstants { // String KEY_DEFAULT_WORKING_DIRECTORY, KEY_EXTRA_KEYS, - KEY_EXTRA_KEYS_STYLE + KEY_EXTRA_KEYS_STYLE, + SOFT_KEYBOARD_TOGGLE_BEHAVIOUR )); /** Defines the set for keys loaded by termux that have default boolean behaviour diff --git a/termux-shared/src/main/java/com/termux/shared/settings/properties/TermuxSharedProperties.java b/termux-shared/src/main/java/com/termux/shared/settings/properties/TermuxSharedProperties.java index 124fe6ae..617e8208 100644 --- a/termux-shared/src/main/java/com/termux/shared/settings/properties/TermuxSharedProperties.java +++ b/termux-shared/src/main/java/com/termux/shared/settings/properties/TermuxSharedProperties.java @@ -211,6 +211,8 @@ public class TermuxSharedProperties implements SharedPropertiesParser { return (String) getExtraKeysInternalPropertyValueFromValue(value); case TermuxPropertyConstants.KEY_EXTRA_KEYS_STYLE: return (String) getExtraKeysStyleInternalPropertyValueFromValue(value); + case TermuxPropertyConstants.SOFT_KEYBOARD_TOGGLE_BEHAVIOUR: + return (String) getSoftKeyboardToggleBehaviourInternalPropertyValueFromValue(value); default: // default boolean behaviour if (TermuxPropertyConstants.TERMUX_DEFAULT_BOOLEAN_BEHAVIOUR_PROPERTIES_LIST.contains(key)) @@ -373,6 +375,16 @@ public class TermuxSharedProperties implements SharedPropertiesParser { return SharedProperties.getDefaultIfNull(value, TermuxPropertyConstants.DEFAULT_IVALUE_EXTRA_KEYS_STYLE); } + /** + * Returns the value itself if it is not {@code null}, otherwise returns {@link TermuxPropertyConstants#DEFAULT_IVALUE_SOFT_KEYBOARD_TOGGLE_BEHAVIOUR}. + * + * @param value {@link String} value to convert. + * @return Returns the internal value for value. + */ + public static String getSoftKeyboardToggleBehaviourInternalPropertyValueFromValue(String value) { + return SharedProperties.getDefaultIfNull(value, TermuxPropertyConstants.DEFAULT_IVALUE_SOFT_KEYBOARD_TOGGLE_BEHAVIOUR); + } + @@ -421,6 +433,10 @@ public class TermuxSharedProperties implements SharedPropertiesParser { return (String) getInternalPropertyValue(TermuxPropertyConstants.KEY_DEFAULT_WORKING_DIRECTORY, true); } + public boolean shouldEnableDisableSoftKeyboardOnToggle() { + return (boolean) TermuxPropertyConstants.IVALUE_SOFT_KEYBOARD_TOGGLE_BEHAVIOUR_ENABLE_DISABLE.equals(getInternalPropertyValue(TermuxPropertyConstants.SOFT_KEYBOARD_TOGGLE_BEHAVIOUR, true)); + } + diff --git a/termux-shared/src/main/java/com/termux/shared/view/KeyboardUtils.java b/termux-shared/src/main/java/com/termux/shared/view/KeyboardUtils.java index e6b713a5..3891086d 100644 --- a/termux-shared/src/main/java/com/termux/shared/view/KeyboardUtils.java +++ b/termux-shared/src/main/java/com/termux/shared/view/KeyboardUtils.java @@ -19,7 +19,9 @@ public class KeyboardUtils { public static void setSoftKeyboardVisibility(@NonNull final Runnable showSoftKeyboardRunnable, final Activity activity, final View view, final boolean visible) { if (visible) { - view.postDelayed(showSoftKeyboardRunnable, 1000); + // A Runnable with a delay is used, otherwise soft keyboard may not automatically open + // on some devices, but still may fail + view.postDelayed(showSoftKeyboardRunnable, 500); } else { view.removeCallbacks(showSoftKeyboardRunnable); hideSoftKeyboard(activity, view);