Skip to content

Commit 12c8a79

Browse files
Merge pull request termux#2114 from agnostic-apollo/fix-soft-keyboard-not-showing-in-some-case
Fix issue where soft keyboard would not show in some cases
2 parents 344e96d + 2c9b783 commit 12c8a79

File tree

2 files changed

+79
-26
lines changed

2 files changed

+79
-26
lines changed

app/src/main/java/com/termux/app/TermuxActivity.java

+12
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ public final class TermuxActivity extends Activity implements ServiceConnection
129129
*/
130130
private boolean mIsVisible;
131131

132+
/**
133+
* If onResume() was called after onCreate().
134+
*/
135+
private boolean isOnResumeAfterOnCreate = false;
136+
132137
/**
133138
* The {@link TermuxActivity} is in an invalid state and must not be run.
134139
*/
@@ -160,6 +165,7 @@ public final class TermuxActivity extends Activity implements ServiceConnection
160165
public void onCreate(Bundle savedInstanceState) {
161166

162167
Logger.logDebug(LOG_TAG, "onCreate");
168+
isOnResumeAfterOnCreate = true;
163169

164170
// Check if a crash happened on last run of the app and show a
165171
// notification with the crash details if it did
@@ -251,6 +257,8 @@ public void onResume() {
251257

252258
if (mTermuxTerminalViewClient != null)
253259
mTermuxTerminalViewClient.onResume();
260+
261+
isOnResumeAfterOnCreate = false;
254262
}
255263

256264
@Override
@@ -691,6 +699,10 @@ public boolean isVisible() {
691699
return mIsVisible;
692700
}
693701

702+
public boolean isOnResumeAfterOnCreate() {
703+
return isOnResumeAfterOnCreate;
704+
}
705+
694706

695707

696708
public TermuxService getTermuxService() {

app/src/main/java/com/termux/app/terminal/TermuxTerminalViewClient.java

+67-26
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@ public class TermuxTerminalViewClient extends TermuxTerminalViewClientBase {
5757

5858
private Runnable mShowSoftKeyboardRunnable;
5959

60+
private boolean mShowSoftKeyboardIgnoreOnce;
61+
private boolean mShowSoftKeyboardWithDelayOnce;
62+
6063
private static final String LOG_TAG = "TermuxTerminalViewClient";
6164

6265
public TermuxTerminalViewClient(TermuxActivity activity, TermuxTerminalSessionClient termuxTerminalSessionClient) {
@@ -410,10 +413,19 @@ public void onToggleSoftKeyboardRequest() {
410413
mActivity.getPreferences().setSoftKeyboardEnabled(false);
411414
KeyboardUtils.disableSoftKeyboard(mActivity, mActivity.getTerminalView());
412415
} else {
416+
// Show with a delay, otherwise pressing keyboard toggle won't show the keyboard after
417+
// switching back from another app if keyboard was previously disabled by user.
418+
// Also request focus, since it wouldn't have been requested at startup by
419+
// setSoftKeyboardState if keyboard was disabled. #2112
413420
Logger.logVerbose(LOG_TAG, "Enabling soft keyboard on toggle");
414421
mActivity.getPreferences().setSoftKeyboardEnabled(true);
415422
KeyboardUtils.clearDisableSoftKeyboardFlags(mActivity);
416-
KeyboardUtils.showSoftKeyboard(mActivity, mActivity.getTerminalView());
423+
if(mShowSoftKeyboardWithDelayOnce) {
424+
mShowSoftKeyboardWithDelayOnce = false;
425+
mActivity.getTerminalView().postDelayed(getShowSoftKeyboardRunnable(), 500);
426+
mActivity.getTerminalView().requestFocus();
427+
} else
428+
KeyboardUtils.showSoftKeyboard(mActivity, mActivity.getTerminalView());
417429
}
418430
}
419431
// If soft keyboard toggle behaviour is show/hide
@@ -431,12 +443,20 @@ public void onToggleSoftKeyboardRequest() {
431443
}
432444

433445
public void setSoftKeyboardState(boolean isStartup, boolean isReloadTermuxProperties) {
446+
boolean noRequestFocus = false;
447+
434448
// If soft keyboard is disabled by user for Termux (check function docs for Termux behaviour info)
435449
if (KeyboardUtils.shouldSoftKeyboardBeDisabled(mActivity,
436-
mActivity.getPreferences().isSoftKeyboardEnabled(),
437-
mActivity.getPreferences().isSoftKeyboardEnabledOnlyIfNoHardware())) {
450+
mActivity.getPreferences().isSoftKeyboardEnabled(),
451+
mActivity.getPreferences().isSoftKeyboardEnabledOnlyIfNoHardware())) {
438452
Logger.logVerbose(LOG_TAG, "Maintaining disabled soft keyboard");
439453
KeyboardUtils.disableSoftKeyboard(mActivity, mActivity.getTerminalView());
454+
noRequestFocus = true;
455+
// Delay is only required if onCreate() is called like when Termux app is exited with
456+
// double back press, not when Termux app is switched back from another app and keyboard
457+
// toggle is pressed to enable keyboard
458+
if (isStartup && mActivity.isOnResumeAfterOnCreate())
459+
mShowSoftKeyboardWithDelayOnce = true;
440460
} else {
441461
// Set flag to automatically push up TerminalView when keyboard is opened instead of showing over it
442462
KeyboardUtils.setResizeTerminalViewForSoftKeyboardFlags(mActivity);
@@ -450,34 +470,55 @@ public void setSoftKeyboardState(boolean isStartup, boolean isReloadTermuxProper
450470
KeyboardUtils.hideSoftKeyboard(mActivity, mActivity.getTerminalView());
451471
// Required to keep keyboard hidden when Termux app is switched back from another app
452472
KeyboardUtils.setSoftKeyboardAlwaysHiddenFlags(mActivity);
453-
} else {
454-
// Do not force show soft keyboard if termux-reload-settings command was run with hardware keyboard
455-
if (isReloadTermuxProperties)
456-
return;
457-
458-
if (mShowSoftKeyboardRunnable == null) {
459-
mShowSoftKeyboardRunnable = () -> {
460-
Logger.logVerbose(LOG_TAG, "Showing soft keyboard on focus change");
461-
KeyboardUtils.showSoftKeyboard(mActivity, mActivity.getTerminalView());
462-
};
463-
}
473+
noRequestFocus = true;
474+
// Required to keep keyboard hidden on app startup
475+
mShowSoftKeyboardIgnoreOnce = true;
476+
}
477+
}
464478

465-
mActivity.getTerminalView().setOnFocusChangeListener(new View.OnFocusChangeListener() {
466-
@Override
467-
public void onFocusChange(View view, boolean hasFocus) {
468-
// Force show soft keyboard if TerminalView or toolbar text input view has
469-
// focus and close it if they don't
470-
boolean textInputViewHasFocus = false;
471-
final EditText textInputView = mActivity.findViewById(R.id.terminal_toolbar_text_input);
472-
if (textInputView != null) textInputViewHasFocus = textInputView.hasFocus();
473-
KeyboardUtils.setSoftKeyboardVisibility(mShowSoftKeyboardRunnable, mActivity, mActivity.getTerminalView(), hasFocus || textInputViewHasFocus);
479+
mActivity.getTerminalView().setOnFocusChangeListener(new View.OnFocusChangeListener() {
480+
@Override
481+
public void onFocusChange(View view, boolean hasFocus) {
482+
// Force show soft keyboard if TerminalView or toolbar text input view has
483+
// focus and close it if they don't
484+
boolean textInputViewHasFocus = false;
485+
final EditText textInputView = mActivity.findViewById(R.id.terminal_toolbar_text_input);
486+
if (textInputView != null) textInputViewHasFocus = textInputView.hasFocus();
487+
488+
if (hasFocus || textInputViewHasFocus) {
489+
if (mShowSoftKeyboardIgnoreOnce) {
490+
mShowSoftKeyboardIgnoreOnce = false; return;
474491
}
475-
});
492+
Logger.logVerbose(LOG_TAG, "Showing soft keyboard on focus change");
493+
} else {
494+
Logger.logVerbose(LOG_TAG, "Hiding soft keyboard on focus change");
495+
}
476496

477-
// Request focus for TerminalView
478-
mActivity.getTerminalView().requestFocus();
497+
KeyboardUtils.setSoftKeyboardVisibility(getShowSoftKeyboardRunnable(), mActivity, mActivity.getTerminalView(), hasFocus || textInputViewHasFocus);
479498
}
499+
});
500+
501+
// Do not force show soft keyboard if termux-reload-settings command was run with hardware keyboard
502+
// or soft keyboard is to be hidden or is disabled
503+
if (!isReloadTermuxProperties && !noRequestFocus) {
504+
// Request focus for TerminalView
505+
// Also show the keyboard, since onFocusChange will not be called if TerminalView already
506+
// had focus on startup to show the keyboard, like when opening url with context menu
507+
// "Select URL" long press and returning to Termux app with back button. This
508+
// will also show keyboard even if it was closed before opening url. #2111
509+
Logger.logVerbose(LOG_TAG, "Requesting TerminalView focus and showing soft keyboard");
510+
mActivity.getTerminalView().requestFocus();
511+
mActivity.getTerminalView().postDelayed(getShowSoftKeyboardRunnable(), 300);
512+
}
513+
}
514+
515+
private Runnable getShowSoftKeyboardRunnable() {
516+
if (mShowSoftKeyboardRunnable == null) {
517+
mShowSoftKeyboardRunnable = () -> {
518+
KeyboardUtils.showSoftKeyboard(mActivity, mActivity.getTerminalView());
519+
};
480520
}
521+
return mShowSoftKeyboardRunnable;
481522
}
482523

483524

0 commit comments

Comments
 (0)